# coding: utf-8
#
# Copyright © 2020 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#

# Author: Larry Pearce
# Revised by: Larry Pearce
# Revision History
# Date         Change                                 By
# 2020-10-13   Original code (1.0)                    Larry Pearce

import apex

import time


def processInputForce(f_in):
    if f_in == "":
        f = 0.0
    else:
        try:
            f = float(f_in)
        except:
            f = 0.0
    return f


def createForce(lbcName, forceMomentRep_, _target, _loadEventOrientation):
    forcemoment = apex.environment.createForceMoment(
       name = lbcName,
       forceMomentRep = forceMomentRep_,
       applicationMethod = apex.attribute.ApplicationMethod.Direct,
       target = _target,
       orientation = _loadEventOrientation,
       description = "")


def main(dict={}):
    apex.disableShowOutput()

    unitSystem = r'''in-slinch-s-lbf'''
    print("Using unit system: "+unitSystem)
    myModel = apex.currentModel()

    lbcName = dict["name"]
    if lbcName == "": lbcName = "Force"
    fxVal = dict["Fx"]
    fyVal = dict["Fy"]
    fzVal = dict["Fz"]

    fx = processInputForce(fxVal)
    fy = processInputForce(fyVal)
    fz = processInputForce(fzVal)

    if fx+fy+fz == 0.0:
        apex.session.displayStatusMessage("No Force values entered?")
        time.sleep(5)
        print("No Force values entered?")
        return

    coordSys = None
    coordName = dict["coordSys"]
    if coordName == "None":
        CoordSys = None
    else:
        try:
            coordSys = apex.getCoordinateSystem(name = coordName)
        except:
            coordSys = None

    _selected = apex.selection.getCurrentSelection()

    if _selected.len() == 0:
        apex.session.displayStatusMessage("Nothing selected?")
        time.sleep(5)
        print("Nothing selected?")
        return

    selNodes = apex.entityCollection()
    selVerts = apex.entityCollection()
    for sel in _selected:
        if sel.getEntityType() == apex.EntityType.Node:
            selNodes.append(sel)
        elif sel.getEntityType() == apex.EntityType.Edge:
            selNodes += sel.getNodes()
        elif sel.getEntityType() == apex.EntityType.Vertex:
            selVerts.append(sel)
        #elif sel.getEntityType() == apex.EntityType.CoordinateSystem:
        #	coordSys = sel

    print("Selected nodes: "+str(selNodes.len()))
    print("Selected vertices: "+str(selVerts.len()))
    if selNodes.len()+selVerts.len() == 0:
        apex.session.displayStatusMessage("No nodes or vertices were selected?")
        time.sleep(5)
        print("No nodes or vertices were selected?")
        return

    if coordSys is None:
        _loadEventOrientation = apex.construct.createOrientation(alpha = 0.0, beta = 0.0, gamma = 0.0)
    else:
        _loadEventOrientation = apex.construct.createOrientationByCoordinateSystem(coordSys)

    if selNodes.len() > 0:
        nodeIds = []
        nodes = apex.entityCollection()
        for node in selNodes:
            if node.getId() in nodeIds: continue
            nodeIds.append(node.getId())
            nodes.append(node)

        forceMomentRep_ = apex.environment.createForceMomentStaticRepByComponent(
             name   = lbcName,
             forceX = fx,
             forceY = fy,
             forceZ = fz,
             description = "")
        for node in nodes:
            _target = apex.entityCollection()
            _target.append(node)
            createForce(lbcName, forceMomentRep_, _target, _loadEventOrientation)

    if selVerts.len() > 0:
        forceMomentRep_ = apex.environment.createForceMomentStaticRepByComponent(
             name   = lbcName,
             forceX = fx,
             forceY = fy,
             forceZ = fz,
             description = "")
        for vert in selVerts:
            _target = apex.entityCollection()
            _target.append(vert)
            createForce(lbcName, forceMomentRep_, _target, _loadEventOrientation)





