How can I convert coordinates from global to local CS in Mechanical?

Pierre Thieffry
Pierre Thieffry Member, Moderator, Employee Posts: 87
25 Answers 10 Comments 25 Likes First Anniversary

When I select an entity in Mechanical, I can choose a coordinate system to display coordinates in the selection information. Is there a way to do it by script?

Answers

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 87
    25 Answers 10 Comments 25 Likes First Anniversary
    edited November 16

    Here's an example for converting coordinates to a local coordinate system. Pay attention to proper unit conversions. The example works on cartesian coordinate systems and would require little work to apply to cylindrical CS as well.

    def convertToCS(coords,cs_name,convert=True):
        # Converts coordinates to a given coordinate system
        # If convert=True, 'coords' is assumed to be in users unit
        # If convert=False, 'coords' must be in meters
        import units
    
        # Retrieve conversion factor from current length unit to meters
        toUnit = "m" # Always generate the result to display in SI unit
        fromUnit=ExtAPI.DataModel.AnalysisList[0].CurrentConsistentUnitFromQuantityName("Length")
        convFact = units.ConvertUnit(1, fromUnit, toUnit, "Length")
    
        # Retrieve axis from coordinate system
        # Assume cs_name is unique (if not, the first one will be used)
        csM = ExtAPI.DataModel.GetObjectsByName(cs_name)[0]
        csXAxis = csM.XAxis
        csYAxis = csM.YAxis
        csZAxis = csM.ZAxis
    
        # Convert to 3D vectors
        xAxis = Vector3D(csXAxis[0], csXAxis[1], csXAxis[2])
        yAxis = Vector3D(csYAxis[0], csYAxis[1], csYAxis[2])
        zAxis = Vector3D(csZAxis[0], csZAxis[1], csZAxis[2])
    
        # create transformation matrix
        # We use a 4x4 matrix with rotation and translations
        idM  =  Matrix4D()
        transM = idM.CreateSystem(xAxis,yAxis,zAxis)
        # Transpose to ensure proper transformation from global to local
        transM.Transpose()
    
        # Translation part of the transformation matrix
        origin=csM.Origin # Origin will be in user units, so convert to m
        origin_transfd=transM.Transform(Vector3D(origin[0],origin[1],origin[2]))
    
        for i in range(0,3):
            transM[12+i]=-origin_transfd[i]
    
        # Now convert point to new CS
        # First convert to meters if needed
        if convert:
            vec=Vector3D(coords[0]*convFact, coords[1]*convFact, coords[2]*convFact)
        else:
            vec=Vector3D(coords[0], coords[1], coords[2])
    
        # Rotate
        vec_transfd=transM.Transform(vec)
    
        # Return new coordinates in user units
        if convert:
            invFact=1/convFact
            return [vec_transfd[0]*invFact,vec_transfd[1]*invFact,vec_transfd[2]*invFact]
        else:
            return [vec_transfd[0],vec_transfd[1],vec_transfd[2]]
    
    # Test function on current selection's centroid 
    cursel=ExtAPI.SelectionManager.CurrentSelection
    # using convert=False in function call as Centroid is always in meters
    print(convertToCS(cursel.Entities[0].Centroid,'myCS',False)) 
    
  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 123
    25 Likes 5 Answers 10 Comments First Anniversary

    Here are some similarly useful functions.