# 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-07-29   Original code                          Larry Pearce

import apex



def processLocation(selectedData):
    # Input:   variable        datatype          description
    #          selectedData    entityCollection  selected items
    # Output:  variable       datatype           description
    #          <none>
    #
    # The selected data should be geometric points, vertices, nodes, curves, edges, surfaces, faces, or solids.
    # This script echoes the location of points, nodes, vertices as well as the midpoint of curves, edges and
    # centroids of surfaces, faces, and solids.  All of this is echoed to the standard output

    # I am not returning the information to the form because it could be a lot of information that
    # would require a spreadsheet for the output.  This could be added in the future.

    returnData = []

    for data in selectedData:
        entType = data.getEntityType()
        if entType == apex.EntityType.Point:
            entityName = "Point: "+data.getName()
            loc = data.getLocation()
        elif entType == apex.EntityType.Vertex:
            entityName = "Vertex"
            loc = data.getLocation()
        elif entType == apex.EntityType.Node:
            entityName = "Node: "+str(data.getId())
            loc = data.getLocation()
        elif entType == apex.EntityType.Surface:
            entityName = "Surface: "+data.getName()
            loc = data.getCentroid()
        elif entType == apex.EntityType.Face:
            entityName = "Face"
            loc = data.getCentroid()
        elif entType == apex.EntityType.Curve:
            entityName = "Curve: "+data.getName()
            loc = data.getMidPoint()
        elif entType == apex.EntityType.Edge:
            entityName = "Edge"
            loc = data.getMidPoint()
        elif entType == apex.EntityType.Solid:
            entityName = "Solid: "+data.getName()
            loc = data.getCentroid()
        else:
            continue

        returnData.append([entityName, loc])

        print(entityName)
        print("X: %7.4g" % (loc.getX()))
        print("Y: %7.4g" % (loc.getY()))
        print("Z: %7.4g" % (loc.getZ()))

    return returnData


def main(dict={}):
    # Define a dictionary of unit systems
    unitSystems = {}
    unitSystems["inch"] = r'''in-slinch-s-lbf'''
    unitSystems["feet"] = r'''ft-slug-s-lbf'''
    unitSystems["mm"]   = r'''mm-t-s-N'''
    unitSystems["m"]    = r'''m-kg-s-N'''

    myModel = apex.currentModel()

    apex.setScriptUnitSystem(unitSystemName = unitSystems[dict["unit"]]) # set the unit system to report data

    print("Selected base unit: "+dict["unit"])

    _selected = apex.selection.getCurrentSelection()

    if _selected.len() == 0:
         if "test" in dict.keys():
            _selected = dict["test"]
         else:
            print("Nothing selected!")
            return

    # Extract information for the selected entities
    
    data = processLocation(_selected)

    if "test" in dict.keys(): return data
