How can we automatically (say via mechanical scripting) create directional point masses?

Member, Moderator, Employee Posts: 311
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭
edited January 16 in Structures

How can we automatically (say via mechanical scripting) create directional point masses, say by first reading some data from a csv file (where the first column in that file gives the named selection to scope to, and the other columns contain the directional mass in x, y , z respectively for that point mass - so every row contains data for a point mass)?

Best Answer

  • Member, Moderator, Employee Posts: 311
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited January 16 Answer ✓

    There are many ways of doing this. An attempt of such script is seen below.

    It will read in the point mass data from the csv file , and based on this, create the point masses with their apdl command snippets that define the directional point mass data specified in the csv file.

    1. import csv
    2. mx=[]
    3. my=[]
    4. mz=[]
    5. names=[]
    6. with open('D:\mymass.csv', 'rb') as f:
    7. reader = csv.reader(f)
    8. for row in reader:
    9. mz.append(row[3])
    10. my.append(row[2])
    11. mx.append(row[1])
    12. names.append(row[0])
    13.  
    14. geometry = Model.Geometry
    15.  
    16. for ii in range(0,len(names)):
    17. point_mass = geometry.AddPointMass()
    18. cs=point_mass.AddCommandSnippet()
    19. ns= DataModel.GetObjectsByName(str(names[ii]))[0]
    20. point_mass.Location=ns
    21. point_mass.Mass=Quantity('0.001 [kg]')
    22. cs.AppendText("keyopt,_tid,1,0\n")
    23. cs.AppendText("keyopt,_tid,3,0\n")
    24. cs.AppendText("r," +"_tid," +str(mx[ii]) + "," + str(my[ii]) +"," +str(mz[ii]) + "," + str(0) +"," + str(0) + "," + str(0) + "\n")

    or in a single loop:

    1. geometry = Model.Geometry
    2. with open('D:\mymass.csv', 'rb') as f:
    3. for line in f:
    4. mz=(line.Split(',')[3]).Split('\n')[0]
    5. my=(line.Split(',')[2])
    6. mx=(line.Split(',')[1])
    7. names=(line.Split(',')[0])
    8. point_mass = geometry.AddPointMass()
    9. cs=point_mass.AddCommandSnippet()
    10. ns= DataModel.GetObjectsByName(str(names))[0]
    11. point_mass.Location=ns
    12. point_mass.Mass=Quantity('0.001 [kg]')
    13. cs.AppendText("keyopt,_tid,1,0\n")
    14. cs.AppendText("keyopt,_tid,3,0\n")
    15. cs.AppendText("r," +"_tid," +str(mx) + "," + str(my) +"," +str(mz) + "," + str(0) +"," + str(0) + "," + str(0) + "\n")

    and something unrelated:

    (retrieve selected object in the mechanical tree, and if point mass print yes)

    1. ob=Tree.FirstActiveObject
    2. if (ob.GetType())==Ansys.ACT.Automation.Mechanical.PointMass:
    3. print('yes')
    4. else:
    5. print('no')
This discussion has been closed.