# Get original and deformed lengths of paths in Mechanical

Options
Member, Employee Posts: 19
✭✭✭

I need a create a path in Mechanical and measure its original and deformed lengths, how can I achieve this via scripting?

Tagged:

• Member, Employee Posts: 19
✭✭✭
Options

After a path is created, you can select the path and use the following script:

```import clr
from System.Windows.Forms import MessageBox

obj = Tree.ActiveObjects[0]

solution = DataModel.AnalysisList[0].Solution
dirs = [ NormalOrientationType.XAxis , NormalOrientationType.YAxis , NormalOrientationType.ZAxis ]

def Distance( a , b ):
return ( ( a[0] - b[0] )**2 + ( a[1] - b[1] )**2 + ( a[2] - b[2] )**2 )**0.5

with Transaction(True):
# Create results
directional_deformation.ScopingMethod = GeometryDefineByType.Path
directional_deformation.Location = obj
data = []
unit = ''
for d in range( 3 ):
directional_deformation.NormalOrientation = dirs[d]
directional_deformation.EvaluateAllResults()
pd = directional_deformation.PlotData
if d == 0:
data.append( pd['X Coordinate'] )
data.append( pd['Y Coordinate'] )
data.append( pd['Z Coordinate'] )
v = pd['Values']
unit = v.Unit
data.append( list(v) )
# Calculate lengths
length_old , length_new = 0. , 0.
for i in range( len(data[0]) ):
# Calculate deformed coords
for j in range( 3 ):
data[ 3 + j ][ i ] += data[ j ][ i ]
# Calculate segment lengths
if i > 0:
p1 = [ data[0][i] , data[1][i] , data[2][i] ]
p2 = [ data[0][i-1] , data[1][i-1] , data[2][i-1] ]
length_old += Distance( p1 , p2 )

p1 = [ data[3][i] , data[4][i] , data[5][i] ]
p2 = [ data[3][i-1] , data[4][i-1] , data[5][i-1] ]
length_new += Distance( p1 , p2 )
# Get current units used in Mechanical
L_unit = DataModel.CurrentUnitFromQuantityName("Length")
length_old = Quantity( length_old , unit ).ConvertUnit( L_unit )
length_new = Quantity( length_new , unit ).ConvertUnit( L_unit )
MessageBox.Show('Original path length = ' + str(length_old) + '\nDeformed path length = ' + str(length_new))
```