General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

Mechanical stl export with Python in Workbench

    • Dayana Behrens
      Subscriber

      Hello,

      I'm trying to export an STL result from Ansys Mechanical using a Python script I run from Workbench (I have left out the part where I set up the model.).

      I found two options in the Ansys online help to export an STL, but neither works within my script. Both options function when executed directly in the Mechanical scripting window in the Mechanical UI. Interestingly, I can export pictures and text files using the same script.

      I'm unsure what's causing the issue. Can you help me?

      commands="""
      STATIC_STRUCTURAL=Model.Analyses[0]
      SOLUTION=STATIC_STRUCTURAL.Solution
      setting2d= Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
      setting3d = Ansys.Mechanical.Graphics.Graphics3DExportSettings() #######1st option
      TOTAL_DEFORMATION_RESULT=SOLUTION.AddTotalDeformation()
      CAMERA=Graphics.Camera

      SOLUTION.ClearGeneratedData()
      SOLUTION.Solve()

      TOTAL_DEFORMATION_RESULT.Activate()
      CAMERA.SetFit()
      FOLDER_PATH = 'C:\Users\dbehrens\Desktop'
      Graphics.ExportImage(FOLDER_PATH+'\\DEF7.png', GraphicsImageExportFormat.PNG, setting2d)
      TOTAL_DEFORMATION_RESULT.ExportToTextFile(FOLDER_PATH+'\\Txt1.txt')
      Graphics.Export3D(FOLDER_PATH+'\\test.stl', Graphics3DExportFormat.BinarySTL, setting3d) #######1st option
      result = SOLUTION.Children[1] #######2nd option
      result.ExportToSTLFile("C:\Users\dbehrens\Desktop\test1.stl") #######2nd option
      """
      model3 = system2.GetContainer("Results") 
      model3.SendCommand(Language="Python", Command=commands)

    • Dayana Behrens
      Subscriber

      I think the visibility of the results window is an issue. 
      I use Ansys 2023 r1. 
      If you want to export the stl file in the Mechanical Scripting window, it works in 2024 r1 even if the result window is not visible. In 2023 r1 the result window must be open.
      However, I want to export via Workbench because I want to automate everything. I have tested my script in the free 2024 r1 version and it does not work via Workbench either. 
      In 2024 r1 it is also possible to export a stl file with "Python Code After Solve". It does not work in 2023 r1.
      I think this does not work directly via Workbench as I wanted.

    • mjmiddle
      Ansys Employee

      You can send commends from workbench. You just have to make sure you use commands to ensure Mechanical is open graphically for earlier versions:

      model3 = system2.GetContainer("Results")
      model3.Edit(Interactive=False)
      model3.SendCommand(Language="Python", Command=commands)

       

    • Dayana Behrens
      Subscriber

      Hi,

      thank you. 

      The problem was the name of the stl file in this line:

      Graphics.Export3D(FOLDER_PATH+'\\test.stl', Graphics3DExportFormat.BinarySTL, setting3d)

      When I use test.stl, it doesn't work. With Test.stl it works... I have not used test as a variable anywhere.

      That's right, Mechanical 2023 must be open for the stl export to work.

    • mjmiddle
      Ansys Employee

      I haven't checked but maybe it doesn't overwrite existing files of the same name. Make sure test.stl doesn't currently exist. Also, you can do folder dividers in 3 ways:

      r'path\file.ext'
      'path\\file.ext'
      'path/file.ext'

      The "r" in front means raw text in the first line. Sometimes the first two methods can give you trouble. The second one can especially be trouble when your code does double substitutions and you didn't realize. So sometimes you will see four backslashes in the path because of this in code, and it gets a bit messy. The 3rd method, using a forward slash, has worked for me in all cases, on Windows and Linux.

      There is also an os.path.join() method you can use to build up directory and file structure that is going to do it correctly. You need to "import os" first.

       

    • Dayana Behrens
      Subscriber

      Hi,

      thank you very much. In fact, the third notation also works with test.stl. I am using Windows.

Viewing 5 reply threads
  • The topic ‘Mechanical stl export with Python in Workbench’ is closed to new replies.