# 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

import math


def createInterpolateBetweenPoints(locs, sel0, sel1, createNodes):
    s0 = sel0.getLocation()
    s1 = sel1.getLocation()

    p0 = [s0.getX(), s0.getY(), s0.getZ()]
    p1 = [s1.getX(), s1.getY(), s1.getZ()]

    dd = []
    for i in range(3):
        dd.append(p1[i]-p0[i])

    dist = 0.0
    for d in dd:
        dist += d**2
    if dist == 0.0:
        print("Distance between selected locations is ZERO!")
        return
    dist = math.sqrt(dist)

    for loc in locs:
        i = -1
        newXYZ = []
        for d in dd:
            i += 1
            newXYZ.append(p0[i]+d*loc)
        point = apex.geometry.createPointXYZ(newXYZ[0], newXYZ[1], newXYZ[2])
        if createNodes:
            _verts = point.getVertices()
            apex.mesh.createNodeByPickLocation(target = _verts[0], location = apex.Coordinate(point.getX(), point.getY(), point.getZ()))



def main(dict={}):
    myModel = apex.currentModel()

    numPoints = int(dict["numPoints"])
    print("Num Points: "+str(numPoints))

    createNodes = False
    if dict["createNodes"].upper() == "TRUE": createNodes = True


    _selected = apex.selection.getCurrentSelection()
    #print(_selected)

    numSelected = _selected.len()
    if numSelected == 0:
        print("Nothing selected!")
        return

    locs = []
    delta = 1.0/(numPoints+1)
    for i in range(numPoints):
        locs.append((i+1)*delta)

    if numSelected == 2:
        eTypes = []
        eTypes.append(_selected[0].getEntityType())
        eTypes.append(_selected[1].getEntityType())
        point2point = True
        for eType in eTypes:
            if eType != apex.EntityType.Point and eType != apex.EntityType.Vertex and eType != apex.EntityType.Node:
                point2point = False
        if point2point:
            createInterpolateBetweenPoints(locs, _selected[0], _selected[1], createNodes)
            return

    for sel in _selected:
        if sel.getEntityType() == apex.EntityType.Curve:
            edges = sel.getEdges()
            for edge in edges:
                for loc in locs:
                    bb = edge.getParametricRange()
                    dist = bb["uEnd"] - bb["uStart"]
                    aa = edge.evaluateEdgeParametricCoordinate(bb["uStart"]+loc*dist)
                    point = apex.geometry.createPointXYZ(aa.getX(), aa.getY(), aa.getZ())
                    if createNodes:
                        _verts = point.getVertices()
                        apex.mesh.createNodeByPickLocation(target = _verts[0], location = apex.Coordinate(point.getX(), point.getY(), point.getZ()))
        elif sel.getEntityType() == apex.EntityType.Edge:
            bb = sel.getParametricRange()
            dist = bb["uEnd"] - bb["uStart"]
            for loc in locs:
                aa=sel.evaluateEdgeParametricCoordinate(bb["uStart"]+loc*dist)
                point = apex.geometry.createPointXYZ(aa.getX(), aa.getY(), aa.getZ())
                if createNodes:
                    _verts = point.getVertices()
                    apex.mesh.createNodeByPickLocation(target = _verts[0], location = apex.Coordinate(point.getX(), point.getY(), point.getZ()))



