# 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-12   Original code (1.0)                    Larry Pearce

import apex


def returnFloat(inVal):

    try:
        outVal = float(inVal)
        return outVal
    except:
        return 0.0


def main(dict={}):

    myModel = apex.currentModel()

    partOpt = dict["part"]

    x = returnFloat(dict["x"])
    y = returnFloat(dict["y"])
    z = returnFloat(dict["z"])

    parts = myModel.getParts(True)
    if parts.len() == 0:
        myModel.createPart()

    currentPart = myModel.getCurrentPart()

    if partOpt == "Selected":
        _selected = apex.selection.getCurrentSelection()
        if _selected.len() == 0: return
        assy = _selected[0].getParent()
        if assy is None:
            myModel.setCurrentPart("/"+_selected[0].getName())
        else:
            partPath = _selected[0].getPath()[_selected[0].getPath().find("/")+1:]+"/"+_selected[0].getName()
            myModel.setCurrentPart(partPath)

    locs = apex.IPhysicalCollection()
    locs.append(apex.Coordinate(x, y, z))
    points = apex.geometry.createPointLocation(locs)
    _verts = points[0].getVertices()

    apex.mesh.createNodeByPickLocation(target = _verts[0], location = locs[0])

    if partOpt == "Current": return

    assy = currentPart.getParent()
    if assy is None:
        myModel.setCurrentPart("/"+currentPart.getName())
    else:
        partPath = currentPart.getPath()[currentPart.getPath().find("/")+1:]+"/"+currentPart.getName()
        myModel.setCurrentPart(partPath)

