How to find the rigid transformation data for External Data?

Member, Moderator, Employee Posts: 108
25 Answers Second Anniversary 10 Comments 25 Likes
✭✭✭✭
edited June 2023 in Structures

External Data allows to enter a rigid transformation in the global coordinate system in order to orient data that could be misaligned with the geometry. Finding the proper angles and translation data is not obvious when the data is not oriented along the axis of the global CS and could require a long "trial and error" process.

How can we automate this within Mechanical? The assumption is that we have the "geometry" of the data to be imported either in geometric form or as STL or as a mesh.

Tagged:

Answers

  • Member, Moderator, Employee Posts: 108
    25 Answers Second Anniversary 10 Comments 25 Likes
    ✭✭✭✭
    Answer ✓

    Here's a proposal. For comprehensiveness, it is assumed that the user has been able to get a representation of the data to be imported as geometry in Mechanical (either from a CAD/STL file or a mesh). The geometry is used to visually check the alignment. Yet it is not mandatory, as the script will only compute the transformation if no body is selected.

    The principle is the following: the user will define two coordinate systems to be aligned. One is created on the target geometry (the one on which data will be mapped) and another one on the data to be imported. The user then selects in the tree both coordinate systems and the body to be moved (the one correspionding the data to be imported). A Part Transform will be created to show how the alignment has been achieved. If no body is selected, a message box with the rigid transformation between the two coordinate systems is displayed.

    1. # Compute Rigid Transformation between two coordinate systems
    2. # Optionally create a Part transform on a body
    3. # Created by P. Thieffry
    4. # Last update: 2021/04/07
    5.  
    6.  
    7. import math
    8. import units
    9.  
    10. clr.AddReference("Ans.UI.Toolkit.Base")
    11. clr.AddReference("Ans.UI.Toolkit")
    12. from Ansys.UI.Toolkit import *
    13.  
    14.  
    15. # Utility functions
    16.  
    17.  
    18.  
    19. def dotProduct(v1,v2):
    20. return v2[0]*v1[0]+v2[1]*v1[1]+v2[2]*v1[2]
    21. def matProduct(m1,m2):
    22. nr1=len(m1)
    23. nc1=len(m1[0])
    24. nc2=len(m2[0])
    25. resM = [([0]*nc2) for i in range(nr1)]
    26. for i in range(0,nr1):
    27. for j in range(0,nc2):
    28. val=0.
    29. for k in range(0,nc1):
    30. val+=resM[i][j]+m1[i][k]*m2[k][j]
    31. resM[i][j]=val
    32. return resM
    33. def InverseTransformationMatrix(m):
    34. mt=[[0.,0.,0.,0.],[0.,0.,0.,0.],[0.,0.,0.,0.],[0.,0.,0.,0.]]
    35. for i in range(0,4):
    36. for j in range(0,4):
    37. mt[i][j]=m[j][i]
    38. tVec=matProduct(mt,[[-m[0][3]],[-m[1][3]],[-m[2][3]],[-m[3][3]]])
    39. for i in range(0,4):
    40. mt[i][3]=tVec[i][0]
    41. mt[3][i]=0.
    42. mt[3][3]=1.
    43. return mt
    44.  
    45.  
    46. def CSBodyDistance(csys,body):
    47. op_unit=csys.OriginX.Unit
    48. orX=units.ConvertUnit(csys.OriginX.Value, fromUnit=op_unit, toUnit='m')
    49. orY=units.ConvertUnit(csys.OriginY.Value, fromUnit=op_unit, toUnit='m')
    50. orZ=units.ConvertUnit(csys.OriginZ.Value, fromUnit=op_unit, toUnit='m')
    51. dist=(body.Centroid[0]-orX)**2+(body.Centroid[1]-orY)**2+(body.Centroid[2]-orZ)**2
    52. return dist
    53.  
    54. def CreateSelection(body):
    55. # Create selection from body
    56. ExtAPI.SelectionManager.ClearSelection()
    57. temp_sel= ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    58. temp_sel.Ids=[body.Id]
    59. ExtAPI.SelectionManager.NewSelection(temp_sel)
    60. def CreateCSOnBody(body):
    61. # Create coordinate system on body so axes are along the axis of inertia of the body
    62. CreateSelection(body)
    63. csGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystems)[0]
    64. newCS=csGroup.AddCoordinateSystem()
    65. newCS.Name='CS_body_'+body.Name
    66. # newCS.OriginDefineBy=Ansys.Mechanical.DataModel.Enums.CoordinateSystemAlignmentType.Fixed
    67. return newCS
    68.  
    69.  
    70. def anglesFromTransMatrix(tmat):
    71. pi=math.pi
    72. sY=-tmat[2][0]
    73. if sY < 1.:
    74. if (sY > -1.):
    75. rotY=math.asin(sY)
    76. rotX = math.atan2(tmat[2][1],tmat[2][2])
    77. rotZ = math.atan2(tmat[1][0],tmat[0][0])
    78. else: # sY = -1
    79. rotY= -pi/2
    80. rotX = -math.atan2(tmat[1][0],tmat[1][1])
    81. rotZ = 0.
    82. else: # sY=1
    83. rotY= pi/2
    84. rotX = -math.atan2(tmat[0][1],tmat[1][1])
    85. rotZ = 0.
    86. return rotX*180./pi,rotY*180./pi,rotZ*180./pi
    87. def TransformationMatrix(origCS,targCS):
    88. # compute transformation matrix to align origCS to targCS
    89. # All coordinates are expressed in the global CS
    90. #
    91. # Retrieve CS origins in m
    92. op_unit=origCS.OriginX.Unit
    93. orX=units.ConvertUnit(origCS.OriginX.Value, fromUnit=op_unit, toUnit='m')
    94. orY=units.ConvertUnit(origCS.OriginY.Value, fromUnit=op_unit, toUnit='m')
    95. orZ=units.ConvertUnit(origCS.OriginZ.Value, fromUnit=op_unit, toUnit='m')
    96. taX=units.ConvertUnit(targCS.OriginX.Value, fromUnit=op_unit, toUnit='m')
    97. taY=units.ConvertUnit(targCS.OriginY.Value, fromUnit=op_unit, toUnit='m')
    98. taZ=units.ConvertUnit(targCS.OriginZ.Value, fromUnit=op_unit, toUnit='m')
    99. tMat=[[dotProduct(origCS.XAxis,targCS.XAxis),dotProduct(origCS.XAxis,targCS.YAxis),dotProduct(origCS.XAxis,targCS.ZAxis),-orX+taX],
    100. [dotProduct(origCS.YAxis,targCS.XAxis),dotProduct(origCS.YAxis,targCS.YAxis),dotProduct(origCS.YAxis,targCS.ZAxis),-orY+taY],
    101. [dotProduct(origCS.ZAxis,targCS.XAxis),dotProduct(origCS.ZAxis,targCS.YAxis),dotProduct(origCS.ZAxis,targCS.ZAxis),-orZ+taZ],
    102. [0.,0.,0.,1.]]
    103. return tMat
    104. def TransformationData(origCS,targCS):
    105. # compute angles and translations to be used in part transforms
    106. # based on transformation matrix between two CS
    107. #
    108. csGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystems)[0]
    109. globalCS=csGroup.Children[0] # get global CS as this is the one we want angles and translations expressed in
    110. # First transform from globalCS to origCS
    111. mGlobalToOrig=TransformationMatrix(globalCS,origCS)
    112.  
    113. # Compute inverse transformation
    114. mOrigToGlobal=InverseTransformationMatrix(mGlobalToOrig)
    115. # Transfom from global to targ
    116. mGlobalToTarg=TransformationMatrix(globalCS,targCS)
    117. # Now combine them
    118. mOrigToTarg=matProduct(mGlobalToTarg,mOrigToGlobal)
    119. # Now retrieve angle for transformation in global CS
    120. rotX,rotY,rotZ=anglesFromTransMatrix(mOrigToTarg)
    121. # Get translations in global CS
    122. trX=mOrigToTarg[0][3]
    123. trY=mOrigToTarg[1][3]
    124. trZ=mOrigToTarg[2][3]
    125.  
    126. return trX,trY,trZ,rotX,rotY,rotZ
    127.  
    128. def CreateTransform(body,bodyCS,targCS):
    129. # Transform body from bodyCS to targCS
    130. # bodyCS and targCS should be oriented along axes of inertia
    131. # Angles and translations to be computed based on global Coordinate System so they can be reused in External Data
    132. #
    133. csGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystems)[0]
    134. globalCS=csGroup.Children[0] # get global CS as this is the one we want angles and translations expressed in
    135. # get translations and angles
    136. tX,tY,tZ,rotX,rotY,rotZ=TransformationData(bodyCS,targCS)
    137. # create part transform
    138. # first check if Transforms group exist, if not create it with a dummy part transform
    139. trGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.PartTransformGroup)
    140. if len(trGroup)==0:
    141. dummyT=ExtAPI.DataModel.Project.Model.AddPartTransform()
    142. dummyT.Delete()
    143. trGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.PartTransformGroup)[0]
    144. # create part transform
    145. newTR=trGroup.AddPartTransform()
    146. # Set geometry scoping on body
    147. CreateSelection(body)
    148. newTR.Location=ExtAPI.SelectionManager.CurrentSelection
    149. # All data expressed in globalCS for reuse in External Data
    150. newTR.CoordinateSystem=globalCS
    151. # Set transformation data
    152. newTR.TranslationX=Quantity(tX,'m')
    153. newTR.TranslationY=Quantity(tY,'m')
    154. newTR.TranslationZ=Quantity(tZ,'m')
    155.  
    156. newTR.RotationX=Quantity(rotX,'deg')
    157. newTR.RotationY=Quantity(rotY,'deg')
    158. newTR.RotationZ=Quantity(rotZ,'deg')
    159. # transform gemetry
    160. newTR.TransformGeometry()
    161. return newTR
    162.  
    163. curSel=ExtAPI.DataModel.Tree.ActiveObjects
    164.  
    165. # Need to have two CS selected, otherwise stop
    166. mess_line1 = 'Please select at least 2 coordinate systems in the tree and a maximum of one body if you want to visually check the transformation'
    167. if (curSel.Count<2):
    168. MessageBox.Show(mess_line1)
    169. pass
    170. else:
    171. bodies=[]
    172. cSyst=[]
    173. for sel in curSel:
    174. if sel.GetType()==Ansys.ACT.Automation.Mechanical.Body:
    175. bodies.append(sel.GetGeoBody())
    176. if sel.GetType()==Ansys.ACT.Automation.Mechanical.CoordinateSystem:
    177. cSyst.append(sel)
    178. if cSyst.Count!=2:
    179. MessageBox.Show(mess_line1)
    180. pass
    181. if cSyst.Count==2 and bodies.Count>1:
    182. MessageBox.Show(mess_line1)
    183. pass
    184.  
    185. if bodies.Count == 1: # a body has been selected, we'll create a part transform
    186. #find CS closest to selected body
    187. d1=CSBodyDistance(cSyst[0],bodies[0])
    188. d2=CSBodyDistance(cSyst[1],bodies[0])
    189. if d1<d2:
    190. origCS=cSyst[0]
    191. targCS=cSyst[1]
    192. else:
    193. origCS=cSyst[1]
    194. targCS=cSyst[0]
    195. CreateTransform(bodies[0],origCS,targCS)
    196. else: # no body has been selected, just compute transformation data and display in a message box
    197. origCS=cSyst[0] # first selected CS is the one corresponding to external data
    198. targCS=cSyst[1] # second CS is the one corresponding to actual geometry to be mapped on
    199. tX,tY,tZ,rotX,rotY,rotZ=TransformationData(origCS,targCS)
    200. op_Unit=origCS.OriginX.Unit
    201. mess_line = 'Computed data for rigid transformation:\n\n'
    202. mess_line+=' Orign X : '+str(Quantity(units.ConvertUnit(tX, fromUnit='m', toUnit=op_Unit),op_Unit))+'\n'
    203. mess_line+=' Orign Y : '+str(Quantity(units.ConvertUnit(tY, fromUnit='m', toUnit=op_Unit),op_Unit))+'\n'
    204. mess_line+=' Orign Z : '+str(Quantity(units.ConvertUnit(tZ, fromUnit='m', toUnit=op_Unit),op_Unit))+'\n'
    205. mess_line+='\n'
    206. mess_line+=' Rotation X : '+ str(Quantity(rotX,'deg'))+'\n'
    207. mess_line+=' Rotation Y : '+ str(Quantity(rotY,'deg'))+'\n'
    208. mess_line+=' Rotation Z : '+ str(Quantity(rotZ,'deg'))+'\n'
    209. MessageBox.Show(mess_line)
  • Member, Moderator, Employee Posts: 108
    25 Answers Second Anniversary 10 Comments 25 Likes
    ✭✭✭✭
    Answer ✓

    Erratum: I realized the order of transformations is NOT the same in Mechanical and External data. A part transform in Mechanical will do RX, RY, RZ and then translations. External data does RY RX RZ then translations (RX and RY are in reverse order). So the following script will compute both configurations and display the ones for External Data in a message box.

    BTW, it will also copy the data to the clipboard for further reuse.

    1. # Compute Rigid Transformation between two coordinate systems
    2. # Optionally create a Part transform on a body
    3. # Created by P. Thieffry
    4. # Last update: 2021/04/07
    5.  
    6.  
    7. import math
    8. import units
    9. import clr
    10.  
    11.  
    12. clr.AddReference("Ans.UI.Toolkit.Base")
    13. clr.AddReference("Ans.UI.Toolkit")
    14. from Ansys.UI.Toolkit import *
    15.  
    16. clr.AddReference('System.Windows.Forms')
    17. from System.Windows.Forms import Clipboard
    18. def setText(text):
    19. Clipboard.SetText(text)
    20. def getText():
    21. return Clipboard.GetText()
    22.  
    23.  
    24.  
    25. # Utility functions
    26.  
    27. def dotProduct(v1,v2):
    28. return v2[0]*v1[0]+v2[1]*v1[1]+v2[2]*v1[2]
    29. def matProduct(m1,m2):
    30. nr1=len(m1)
    31. nc1=len(m1[0])
    32. nc2=len(m2[0])
    33. resM = [([0]*nc2) for i in range(nr1)]
    34. for i in range(0,nr1):
    35. for j in range(0,nc2):
    36. val=0.
    37. for k in range(0,nc1):
    38. val+=resM[i][j]+m1[i][k]*m2[k][j]
    39. resM[i][j]=val
    40. return resM
    41. def InverseTransformationMatrix(m):
    42. mt=[[0.,0.,0.,0.],[0.,0.,0.,0.],[0.,0.,0.,0.],[0.,0.,0.,0.]]
    43. for i in range(0,4):
    44. for j in range(0,4):
    45. mt[i][j]=m[j][i]
    46. tVec=matProduct(mt,[[-m[0][3]],[-m[1][3]],[-m[2][3]],[-m[3][3]]])
    47. for i in range(0,4):
    48. mt[i][3]=tVec[i][0]
    49. mt[3][i]=0.
    50. mt[3][3]=1.
    51. return mt
    52.  
    53.  
    54. def CSBodyDistance(csys,body):
    55. op_unit=csys.OriginX.Unit
    56. orX=units.ConvertUnit(csys.OriginX.Value, fromUnit=op_unit, toUnit='m')
    57. orY=units.ConvertUnit(csys.OriginY.Value, fromUnit=op_unit, toUnit='m')
    58. orZ=units.ConvertUnit(csys.OriginZ.Value, fromUnit=op_unit, toUnit='m')
    59. dist=(body.Centroid[0]-orX)**2+(body.Centroid[1]-orY)**2+(body.Centroid[2]-orZ)**2
    60. return dist
    61.  
    62. def CreateSelection(body):
    63. # Create selection from body
    64. ExtAPI.SelectionManager.ClearSelection()
    65. temp_sel= ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    66. temp_sel.Ids=[body.Id]
    67. ExtAPI.SelectionManager.NewSelection(temp_sel)
    68. def CreateCSOnBody(body):
    69. # Create coordinate system on body so axes are along the axis of inertia of the body
    70. CreateSelection(body)
    71. csGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystems)[0]
    72. newCS=csGroup.AddCoordinateSystem()
    73. newCS.Name='CS_body_'+body.Name
    74. # newCS.OriginDefineBy=Ansys.Mechanical.DataModel.Enums.CoordinateSystemAlignmentType.Fixed
    75. return newCS
    76.  
    77.  
    78. def anglesFromTransMatrixXYZ(tmat):
    79. pi=math.pi
    80. sY=-tmat[2][0]
    81. if sY < 1.:
    82. if (sY > -1.):
    83. rotY=math.asin(sY)
    84. rotX = math.atan2(tmat[2][1],tmat[2][2])
    85. rotZ = math.atan2(tmat[1][0],tmat[0][0])
    86. else: # sY = -1
    87. rotY= -pi/2
    88. rotX = -math.atan2(tmat[1][0],tmat[1][1])
    89. rotZ = 0.
    90. else: # sY=1
    91. rotY= pi/2
    92. rotX = -math.atan2(tmat[0][1],tmat[1][1])
    93. rotZ = 0.
    94. return rotX*180./pi,rotY*180./pi,rotZ*180./pi
    95. def anglesFromTransMatrixYXZ(tmat):
    96. pi=math.pi
    97. sX=tmat[2][1]
    98. if sX < 1.:
    99. if (sX > -1.):
    100. rotX=math.asin(sX)
    101. rotY = math.atan2(-tmat[2][0],tmat[2][2])
    102. rotZ = math.atan2(-tmat[0][1],tmat[1][1])
    103. else: # sX = -1
    104. rotX= -pi/2
    105. rotY = -math.atan2(tmat[1][0],tmat[1][2])
    106. rotZ = 0.
    107. else: # sX=1
    108. rotX= pi/2
    109. rotY = -math.atan2(tmat[1][0],tmat[1][2])
    110. rotZ = 0.
    111. return rotX*180./pi,rotY*180./pi,rotZ*180./pi
    112. def TransformationMatrix(origCS,targCS):
    113. # compute transformation matrix to align origCS to targCS
    114. # All coordinates are expressed in the global CS
    115. #
    116. # Retrieve CS origins in m
    117. op_unit=origCS.OriginX.Unit
    118. orX=units.ConvertUnit(origCS.OriginX.Value, fromUnit=op_unit, toUnit='m')
    119. orY=units.ConvertUnit(origCS.OriginY.Value, fromUnit=op_unit, toUnit='m')
    120. orZ=units.ConvertUnit(origCS.OriginZ.Value, fromUnit=op_unit, toUnit='m')
    121. taX=units.ConvertUnit(targCS.OriginX.Value, fromUnit=op_unit, toUnit='m')
    122. taY=units.ConvertUnit(targCS.OriginY.Value, fromUnit=op_unit, toUnit='m')
    123. taZ=units.ConvertUnit(targCS.OriginZ.Value, fromUnit=op_unit, toUnit='m')
    124. tMat=[[dotProduct(origCS.XAxis,targCS.XAxis),dotProduct(origCS.XAxis,targCS.YAxis),dotProduct(origCS.XAxis,targCS.ZAxis),-orX+taX],
    125. [dotProduct(origCS.YAxis,targCS.XAxis),dotProduct(origCS.YAxis,targCS.YAxis),dotProduct(origCS.YAxis,targCS.ZAxis),-orY+taY],
    126. [dotProduct(origCS.ZAxis,targCS.XAxis),dotProduct(origCS.ZAxis,targCS.YAxis),dotProduct(origCS.ZAxis,targCS.ZAxis),-orZ+taZ],
    127. [0.,0.,0.,1.]]
    128. return tMat
    129. def TransformationData(origCS,targCS):
    130. # compute angles and translations to be used in part transforms
    131. # based on transformation matrix between two CS
    132. #
    133. csGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystems)[0]
    134. globalCS=csGroup.Children[0] # get global CS as this is the one we want angles and translations expressed in
    135. # First transform from globalCS to origCS
    136. mGlobalToOrig=TransformationMatrix(globalCS,origCS)
    137.  
    138. # Compute inverse transformation
    139. mOrigToGlobal=InverseTransformationMatrix(mGlobalToOrig)
    140. # Transfom from global to targ
    141. mGlobalToTarg=TransformationMatrix(globalCS,targCS)
    142. # Now combine them
    143. mOrigToTarg=matProduct(mGlobalToTarg,mOrigToGlobal)
    144. # Now retrieve angle for transformation in global CS
    145. rotX,rotY,rotZ=anglesFromTransMatrixXYZ(mOrigToTarg)
    146. # Get translations in global CS
    147. trX=mOrigToTarg[0][3]
    148. trY=mOrigToTarg[1][3]
    149. trZ=mOrigToTarg[2][3]
    150. rXExtData,rYExtData,rZExtData=anglesFromTransMatrixYXZ(mOrigToTarg)
    151. return trX,trY,trZ,rotX,rotY,rotZ,rXExtData,rYExtData,rZExtData
    152.  
    153. def CreateTransform(body,bodyCS,targCS):
    154. # Transform body from bodyCS to targCS
    155. # bodyCS and targCS should be oriented along axes of inertia
    156. # Angles and translations to be computed based on global Coordinate System so they can be reused in External Data
    157. #
    158. csGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystems)[0]
    159. globalCS=csGroup.Children[0] # get global CS as this is the one we want angles and translations expressed in
    160. # get translations and angles
    161. tX,tY,tZ,rotX,rotY,rotZ,rXExtData,rYExtData,rZExtData=TransformationData(bodyCS,targCS)
    162. # create part transform
    163. # first check if Transforms group exist, if not create it with a dummy part transform
    164. trGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.PartTransformGroup)
    165. if len(trGroup)==0:
    166. dummyT=ExtAPI.DataModel.Project.Model.AddPartTransform()
    167. dummyT.Delete()
    168. trGroup=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.PartTransformGroup)[0]
    169. # create part transform
    170. newTR=trGroup.AddPartTransform()
    171. # Set geometry scoping on body
    172. CreateSelection(body)
    173. newTR.Location=ExtAPI.SelectionManager.CurrentSelection
    174. # All data expressed in globalCS for reuse in External Data
    175. newTR.CoordinateSystem=globalCS
    176. # Set transformation data
    177. newTR.TranslationX=Quantity(tX,'m')
    178. newTR.TranslationY=Quantity(tY,'m')
    179. newTR.TranslationZ=Quantity(tZ,'m')
    180.  
    181. newTR.RotationX=Quantity(rotX,'deg')
    182. newTR.RotationY=Quantity(rotY,'deg')
    183. newTR.RotationZ=Quantity(rotZ,'deg')
    184. # transform gemetry
    185. newTR.TransformGeometry()
    186. # return newly created object and rotations for ExternalData
    187. return newTR,tX,tY,tZ,rotX,rotY,rotZ,rXExtData,rYExtData,rZExtData
    188.  
    189. curSel=ExtAPI.DataModel.Tree.ActiveObjects
    190.  
    191. # Need to have two CS selected, otherwise stop
    192. mess_line1 = 'Please select at least 2 coordinate systems in the tree and a maximum of one body if you want to visually check the transformation'
    193. if (curSel.Count<2):
    194. MessageBox.Show(mess_line1)
    195. pass
    196. else:
    197. bodies=[]
    198. cSyst=[]
    199. for sel in curSel:
    200. if sel.GetType()==Ansys.ACT.Automation.Mechanical.Body:
    201. bodies.append(sel.GetGeoBody())
    202. if sel.GetType()==Ansys.ACT.Automation.Mechanical.CoordinateSystem:
    203. cSyst.append(sel)
    204. if cSyst.Count!=2:
    205. MessageBox.Show(mess_line1)
    206. pass
    207. if cSyst.Count==2 and bodies.Count>1:
    208. MessageBox.Show(mess_line1)
    209. pass
    210.  
    211. if bodies.Count == 1: # a body has been selected, we'll create a part transform
    212. #find CS closest to selected body
    213. d1=CSBodyDistance(cSyst[0],bodies[0])
    214. d2=CSBodyDistance(cSyst[1],bodies[0])
    215. if d1<d2:
    216. origCS=cSyst[0]
    217. targCS=cSyst[1]
    218. else:
    219. origCS=cSyst[1]
    220. targCS=cSyst[0]
    221. pTrans,tX,tY,tZ,rotX,rotY,rotZ,rotXExtData,rotYExtData,rotZExtData=CreateTransform(bodies[0],origCS,targCS)
    222. else: # no body has been selected, just compute transformation data and display in a message box
    223. origCS=cSyst[0] # first selected CS is the one corresponding to external data
    224. targCS=cSyst[1] # second CS is the one corresponding to actual geometry to be mapped on
    225. tX,tY,tZ,rotX,rotY,rotZ,rotXExtData,rotYExtData,rotZExtData=TransformationData(origCS,targCS)
    226. # Message Box with results
    227. op_Unit=origCS.OriginX.Unit
    228. orX=units.ConvertUnit(origCS.OriginX.Value, fromUnit='m', toUnit=op_Unit)
    229. mess_line = 'Computed data for rigid transformation in External data:\n'
    230. mess_line+='\tOrign X : '+str(Quantity(units.ConvertUnit(tX, fromUnit='m', toUnit=op_Unit),op_Unit))+'\n'
    231. mess_line+='\tOrign Y : '+str(Quantity(units.ConvertUnit(tY, fromUnit='m', toUnit=op_Unit),op_Unit))+'\n'
    232. mess_line+='\tOrign Z : '+str(Quantity(units.ConvertUnit(tZ, fromUnit='m', toUnit=op_Unit),op_Unit))+'\n'
    233. mess_line+='\n'
    234. mess_line+='\tRotation Theta XY :'+ str(Quantity(rotZExtData,'deg'))+'\n'
    235. mess_line+='\tRotation Theta YZ :'+ str(Quantity(rotXExtData,'deg'))+'\n'
    236. mess_line+='\tRotation Theta ZX :'+ str(Quantity(rotYExtData,'deg'))+'\n'
    237. MessageBox.Show(mess_line)
    238. Clipboard.SetText(mess_line)

Welcome!

It looks like you're new here. Sign in or register to get started.