General Mechanical

General Mechanical

Python Code Object not running when Static Structural updates

    • ianv
      Subscriber

      I am trying to export snapshots of Von-Mises stress results with some Python code:

          Model=ExtAPI.DataModel.Project.Model
          result=Model.Analyses[0].Solution.Children[1]
         
          ExtAPI.DataModel.Tree.Activate(result)
          
          Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Front)
          
          Graphics.Camera.SetFit()
          
          directory = "C:\\Users\\ianve\\Desktop\\WORK\\CURRENT\\Tridem\\Results"
          
          file_stress_side = directory + "\\stress_side.jpg"
          file_stress_top = directory + "\\stress_top.jpg"
          file_stress_bottom = directory + "\\stress_bottom.jpg"
          file_stress_iso = directory + "\\stress_iso.jpg"
          
          setting2d = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
          setting2d.Width = 1920
          setting2d.Height = 1080
          setting2d.Background = GraphicsBackgroundType.White
          setting2d.CurrentGraphicsDisplay = False
          
          Graphics.ExportImage(file_stress_side, GraphicsImageExportFormat.JPG, setting2d)
          
          Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Top)
          Graphics.Camera.SetFit()
          Graphics.ExportImage(file_stress_top, GraphicsImageExportFormat.JPG, setting2d)
          
          Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Bottom)
          Graphics.Camera.SetFit()
          Graphics.ExportImage(file_stress_bottom, GraphicsImageExportFormat.JPG, setting2d)
          
          Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Iso)
          Graphics.Camera.SetFit()
          Graphics.ExportImage(file_stress_iso, GraphicsImageExportFormat.JPG, setting2d)

      The problem I encounter is every time I solve the system from Workbench, the snapshots are not saved. When I export the stress results as a text file with:

          top_result = DataModel.GetObjectsByName("Top")[0]
          top_result.ExportToTextFile("C:\\Users\\ianve\\Desktop\\WORK\\CURRENT\\Tridem\\Results\\top.txt")
          
          bot_result = DataModel.GetObjectsByName("Bottom")[0]
          bot_result.ExportToTextFile("C:\\Users\\ianve\\Desktop\\WORK\\CURRENT\\Tridem\\Results\\bot.txt")
          
          side_result = DataModel.GetObjectsByName("Side")[0]
          side_result.ExportToTextFile("C:\\Users\\ianve\\Desktop\\WORK\\CURRENT\\Tridem\\Results\\side.txt")

      the code executes every time. When I solve from Mechanical itself, the snapshots are created and saved. How can I fix this or work around it?

    • Erik Kostson
      Ansys Employee

       

       

      First of all try it in 2022 R2.

       

      Now if it does not work then the below works in 2022 R2 (not on previous versions):

       

      One way to do this is to run a python script from workbench (wb) – go to workbench->file->scripting->run script file … and choose the script (say saveimage.py) that contains say the following sample code to save some results to text file (you need to change it as needed, and it is outisde the help I can provide to adjust it to your needs – perhaps other users can help with that if needed – the below assumes you have only on system in the project, so you might need to account for that – to learn more about wb scripting you can record them in wb):

       

      SetScriptVersion(Version=”22.2.192″)
      Update()
      cmd=”'
      res=ExtAPI.DataModel.GetObjectsByName(“Total Deformation”)[0]
      ExtAPI.DataModel.Tree.Activate(res)
      res.ExportToTextFile(“D:/OTHER/sunbatch/testexpfile.txt”)
      graphics=Graphics.ExportImage(“D:/OTHER/sunbatch/testexpfile.jpeg”)
      ”'
      system1 = GetSystem(Name=”SYS”)
      model1 = system1.GetContainer(ComponentName=”Model”)
      model1.Edit(Interactive=False)
      model1.SendCommand(Language=”Python”, Command=cmd)

       

      • peteroznewman
        Subscriber

        Hi Erik,

        I tried your Python script in Workbench 2022R2. This was my first time using a Python script in Workbench. I copied the lines from your post and pasted them into a text editor to save the script. Unfortunately, something (this website?) converted the ASCII character ” into a Non-ASCII character ” which causes Iron Python to throw an error.

        I went through the script and replaced all the Non-ASCII characters such as shown on the first line with ASCII characters such as shown on the second line, but ended up with other errors.

        I was just curious to see Python running in Workbench and may learn it at some point time, but thought I would reply in case someone else tried your code and found this problem.  I expect if the formatting in the reply is set to Preformatted instead of Paragraph, it might prevent the characters from being changed.

        Regards,

        Peter

    • ianv
      Subscriber

      Thank you for the response. I am using 2022 R1 at the moment and the export to textfile works without any problems but I want to export snapshots of the stress plots in Mechanical rather than text files of nodal results. Reason being I do not want to do post processing in Python. I have done it before, but it is a lot of extra work for something that is already being done in ANSYS automatically.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.