Graphics.Camera.SetFit()

cobonz
cobonz Member Posts: 10
First Comment
**

I have a code where I use the SetFit() command. Unfortunately it seems that in the batch mode (closed mechanical) it does not work probably often the images generated are not zoomed in or cut half. Is there an work around.

                for view in views:
                    if m % 3 == 0:
                            html_content += '  <tr>\n'
                    m = m+1
                    Graphics.Camera.SetSpecificViewOrientation(view)
                    ExtAPI.Graphics.Camera.SetFit()
                    a= StressResult.AddFigure()
                    a.Name = a.Parent.Parent.Name+' '+name+' '+str(view)
                    output_image_path = r"R:\delete_tmp\{0} {1} {2}.png".format(a.Parent.Parent.Name,name,str(view))
                    Graphics.ExportImage(output_image_path, GraphicsImageExportFormat.PNG, image_settings)
                    html_content += '<td><img src="{}" alt="Image"></td>\n'.format(output_image_path)
                    FigureList.append(a)

I also tried
import context_menu
context_menu.SetFit() which did not work either.

Answers

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited December 2023

  • cobonz
    cobonz Member Posts: 10
    First Comment
    **

    I have no permission to the link

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited December 2023

    Sorry, I have removed the link and copied a summary of the conversation here:

    With "Interactive=False" most of the graphics-related APIs are not available.

    The best possibility would be to use the hidden mode "Hidden=True" that acts like a batch mode but still loads those graphics APIs.

  • cobonz
    cobonz Member Posts: 10
    First Comment
    **

    I reckon I do not understand much.

    Interacitve = False is if I close the GUI? and its currently basically not possible to use the camera without the GUI correctly

    Hidden = True --> you mean that I could run the script in the GUI but the GUI is not updating until the script is done?

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Interactive = False means there will be no graphics operations
    Hidden = True means graphics operations will be running but not displayed. (The GUI will be hidden)

    How are you launching Mechanical and running your script?

  • cobonz
    cobonz Member Posts: 10
    First Comment
    **

    I open mechanical through workbench.

    I inserted a pythoncode under solution as After Post command and "solve" the python code.

    I could also run the script with the script editor, but I wish I could run it in the batch mode. Therefore, I inserted the After Post command which works fine beside the camera operation SetFit()

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    When you open Mechanical through Workbench use Hidden = True instead of Interactive = False

  • cobonz
    cobonz Member Posts: 10
    First Comment
    **

    Hi Landon,
    a late call back.
    I think what I lacked to describe back in December was my wished work progress.

    What my script is currently doing is that it automates some of the post processing and a report with pictures (where the zoom does not work correctly in batch mode).

    What I tried with the phyton post command under the solution branch is that if I close mechanical and press update project in workbench the project is sent for example over night to a cluster and the solution will be optained. After the solution is done Ansys will retrieve the results and then excute the post command. The next morning when I start the PC I do not have to launch the phyton code it will already be executed and I have a report at hand.
    So the way I want to run it is just click on update project in Ansys Workbench. Problem was/is that the picture and zoom function will create some strange views instead of the expected ones.

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    This sounds like a bug. I recommend you contact your Ansys Tech Support provider.

  • M
    M Member, Employee Posts: 244
    50 Answers 100 Comments 100 Likes Second Anniversary
    ✭✭✭✭
    edited November 14

    @cobonz
    There might be some confusion.
    If you are using Workbench to control Mechanical and sending scripts, you can do that 'batch' or in the background, or you can force Mechanical to open and then you can use the graphics available in Mechanical (and save fancy plots).

    You would need to get **mySystem **first (this would be your workbench static structural system, for example).

    Then you would get the model container (which is Mechanical) and then edit/open it. I think you can still send command to it even when open.

    modelContainer = mySystem.GetContainer(ComponentName="Model")
    modelContainer.Edit(Interactive=True) # True opens mechanical and allows for active plots
    modelContainer.SendCommand(Language="Python", Command="""Mechanical ACT Commands""")
    

    Most graphic commands will not act correctly if the Mechanical interface is not open. Opening it will definitely slow down any workflow. Limitation.

  • cobonz
    cobonz Member Posts: 10
    First Comment
    **

    Thanks for the command. Actually thats the work around I also found. To also contribute here and help others.
    My current script is:

    "
    systemList = ["SYS 27","SYS 28","SYS 29","SYS 30"]

    system = GetSystem(systemList[0])
    model = system.GetContainer(ComponentName="Model")

    for System in systemList:
    try:
    system = GetSystem(System)
    model = system.GetContainer(ComponentName="Model")

        solutionComponent = system.GetComponent(Name="Solution")
        solutionComponent.Update(AllDependencies=True)
    
        Save(Overwrite=True)
    except Exception as e:
        print "Error System could not be updated"
    

    EvaluationScript2 = ""
    with open('R:\MechanicalScriptingCommands.txt', 'r') as file:
    # Read the contents of the file
    EvaluationScript2 = file.read()

    model.Edit()
    model.SendCommand(Language="Python",Command=MechanicalScriptingCommands)
    model.Exit()
    "

    The script will update the containers. In my case the four containers are based on the same model. And in the end I excute post command. To avoid using licenses not require I also close the model after excution. Bascially, Instead of submitting a job to a cluster from mechanical I do all via workbench and have in my case a picture report on the next day. Works pretty well. The try except loop was implemented as sth. there can be connections issues or if i got one of the systems settings wrong.

  • cobonz
    cobonz Member Posts: 10
    First Comment
    **

    Sorry, some how I managed not put the code with right taps. Anyway. "MechanicalScriptingCommand" are the command you put into the script editor within mechanical. Just copy your script into a textfile and you can run it with above commands controlled from workbench.