Example of worflow automation: create geometry, model setup, and postprocess

Member, Moderator, Employee Posts: 873
100 Answers 500 Comments 250 Likes Second Anniversary
✭✭✭✭
edited June 2023 in 3D Design

Is there an example of workflow automation with geometry creation, model setup and postprocessing?

Welcome!

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

Answers

  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭
    edited May 2023

    Below is an example.

    First, we'll create the geometry.

    The script below will work both in SpaceClaim and in Discovery. It:

    • creates a base block and four smaller blocks on the top
    • defines a name for the base block
    • creates four named selections (one for the base block body, one for the smaller blocks bodies, one for the bottom base face and one for the upper faces)
    1. # Python Script, API Version = V23
    2. ClearAll()
    3.  
    4. # Geometry dimensions
    5. par_L = MM(50) # length of base block
    6. par_W = MM(25) # width of base block
    7. par_H = MM(10) # height of base block
    8.  
    9. par_l = MM(20) # length of small block
    10. par_w = MM(10) # width of small block
    11. par_h = MM(5) # height of small block
    12. offset = MM(2.5)
    13.  
    14. # Create base block
    15. base_block = BlockBody.Create(Point.Create(MM(0), MM(0), MM(0)), Point.Create(par_L, par_W, par_H ), ExtrudeType.ForceAdd)
    16. base_block.CreatedBodies[0].SetName('Base Block Body')
    17. my_body_selection = Selection.Create(base_block.CreatedBodies[0])
    18. my_body_selection.CreateAGroup('Base Block NS')
    19. my_faces_selection = my_body_selection.ConvertToFaces()
    20. for face in my_faces_selection.Items:
    21.     if face.EvalMid().Point.Z == MM(0):
    22.         Selection.Create(face).CreateAGroup('Base Face NS')
    23.  
    24.  # Create smaller blocks
    25. small_block_1 = BlockBody.Create(Point.Create(offset, offset, par_H ), Point.Create( par_L/2 - offset, par_w, par_H + par_h ), ExtrudeType.ForceIndependent)
    26. small_block_2 = BlockBody.Create(Point.Create(par_L/2 + offset, offset, par_H ), Point.Create(par_L-offset , par_w, par_H + par_h ), ExtrudeType.ForceIndependent)
    27. small_block_3 = BlockBody.Create(Point.Create(offset, par_W - offset-par_w, par_H ), Point.Create( par_L/2 - offset, par_W - offset , par_H + par_h ), ExtrudeType.ForceIndependent)
    28. small_block_4 = BlockBody.Create(Point.Create(par_L/2 + offset, par_W - offset-par_w, par_H  ), Point.Create(par_L-offset , par_W - offset, par_H + par_h), ExtrudeType.ForceIndependent)
    29. my_body_selection = Selection.Create(small_block_1.CreatedBodies[0], small_block_2.CreatedBodies[0], small_block_3.CreatedBodies[0], small_block_4.CreatedBodies[0])
    30. my_body_selection.CreateAGroup('Small Blocks NS')
    31. my_faces_selection = my_body_selection.ConvertToFaces()
    32. top_face_list = []
    33. for face in my_faces_selection.Items:
    34.     if face.EvalMid().Point.Z == par_H + par_h :
    35.         top_face_list.append(face)
    36. Selection.Create(top_face_list).CreateAGroup('Top Faces NS')

    In SpaceClaim, the resulting tree can be found in the "Structure" tab, and the named selections in the "Groups" tab:

    In Discovery, the tree is on the top left:

    and the named selections can be found in the "Advanced Selection" menu at the bottom right:


  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭

    Then, we'll create the Mechanical model setup and postprocessing.

    For the script to work, it is assumed that two materials have already been added in Engineering Data:

    (these could also be created through scripting). It is also assumed that a Steady State thermal analysis is considered (the code could easily be adapted for other boundary conditions for other types of analyses).

    The script will:

    • add material assignements
    • add boundary conditions based on named selections
    • insert results
    • export these results to a specified folder to extract screenshots of the plots, animations of the plots, and values of the plots



  • Member, Moderator, Employee Posts: 873
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭

    The Mechanical script is:

    1. # Retrieve bodies
    2. all_bodies = ExtAPI.DataModel.Project.Model.Geometry.GetChildren(DataModelObjectCategory.Body,True)
    3. base_block_body = [body for body in all_bodies if body.Name.Contains('Base')]
    4. small_block_bodies = [body for body in all_bodies if body.Name.Contains('Solid')]
    5.  
    6.  
    7. # Define material assignation
    8. material_assignation_steel = ExtAPI.DataModel.Project.Model.Materials.AddMaterialAssignment()
    9. sel = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    10. sel.Ids = [body.GetGeoBody().Id for body in base_block_body]
    11. material_assignation_steel.Location = sel
    12. material_assignation_steel.Material = 'Structural Steel'
    13.  
    14.  
    15. material_assignation_iron = ExtAPI.DataModel.Project.Model.Materials.AddMaterialAssignment()
    16. sel = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
    17. sel.Ids = [body.GetGeoBody().Id for body in small_block_bodies]
    18. material_assignation_iron.Location = sel
    19. material_assignation_iron.Material = 'Aluminum'
    20.  
    21.  
    22. # Define boundary conditions based on Named Selections
    23.  
    24.  
    25. analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    26.  
    27.  
    28. temp_base = analysis.AddTemperature()
    29. temp_base.Location = ExtAPI.DataModel.GetObjectsByName("Base Face NS")[0]
    30. temp_base.Magnitude.Output.SetDiscreteValue(0, Quantity(50, "C"))
    31.  
    32.  
    33. convection = analysis.AddConvection()
    34. convection.Location = ExtAPI.DataModel.GetObjectsByName("Top Faces NS")[0]
    35. convection.FilmCoefficient.Output.SetDiscreteValue(0, Quantity(25, "W m^-1 m^-1 C^-1"))
    36. convection.AmbientTemperature.Output.SetDiscreteValue(0, Quantity(5, "C"))
    37.  
    38.  
    39. # Solve model
    40. analysis.Solve()
    41.  
    42.  
    43. # Insert results
    44. solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution
    45. temp_plot = solution.AddTemperature()
    46. temp_plot_on_ns = solution.AddTemperature()
    47. temp_plot_on_ns.Location = ExtAPI.DataModel.GetObjectsByName("Top Faces NS")[0]
    48. solution.EvaluateAllResults()
    49.  
    50.  
    51. # Export results
    52. import os
    53. export_folder = r'D:\Data'
    54.  
    55.  
    56. all_results = solution.GetChildren(DataModelObjectCategory.Result,True)
    57. for result in all_results:
    58.     export_name = os.path.join(export_folder,result.Name)
    59.     ExtAPI.Graphics.ExportImage(export_name +'.png')
    60.     result.ExportAnimation(export_name + '.avi')
    61.     result.ExportToTextFile(export_name + '.txt')

Welcome!

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