Mechanical ACT - "expected int, got str" error

Options
mszlez
mszlez Member Posts: 7
Name Dropper First Comment

Best Answer

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 804
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓
    Options

    Hi @mszlez , I think I figured it out. I'll share my thought process so that you can reuse this (ugly) debugging method in the future.

    When I need to understand what's happening with a custom object, I usually create global variables for debugging:

    Then, in Mechanical, take the actions to trigger the callback calling that function (here, click on the "Generate Analyses" button). From the scripting console, switch to the tab related to your extension:

    (in my case that extension is called "Stress Postprocessing", but in your case that would be "AF_Wizards_Rev_E".

    You're then able to print those global variables in Mechanical:

    From the above we see that "test2" variable (refering to "object"variable in the code) is actually the analysis. And indeed, this button is in the toolbar, so the variable passed to the function called in the "onclick" callback is the active analysis, in my case a static structural one:

    This analysis object does not have a "pipe_side" property, it only has the properties shown in the details' view:

    Hope this helps!

Answers

  • mszlez
    mszlez Member Posts: 7
    Name Dropper First Comment
    Options

    Hi all.

    I'm trying to create some ACT Extension for ANSYS Mechanical. I've defined some properties in my .xml file, i.e.:

    <simdata context="Mechanical">
    <!-- Define <object> with affectsSolution attribute set to false. -->
    <object name="an_set" version="1" caption="Analyses setup" icon="group" isload="false" color="#0000FF" affectsSolution="false">
    <target type="datamodel"/>
    <callbacks></callbacks>
    <property name="input_file" caption= "FEA Input File" control="fileopen"><attributes filters="txt files (*.txt)|*.txt|All files (*.*)|*.*" /></property>
    <property control="select" name="pipeside" caption="Pipeline side" persistent="False" parameterizable="False">
    <attributes options="Side A,Side B" /></property>
    </object>
    </simdata>Then, when I try to assign property value to the variable in my Python script, i.e.:

    def GenerateAnalyses(object):

    in_file=object.Properties["input_file"].Value
    side=object.Properties["pipeside"].Value

    In the extension log I see "expected int, got str" error. Have no clue why I'm seeing this as variables "in_file" and "side" are not used anywhere else in my script, i.e. it's their first declaration. How can I fix this?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 804
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    Hi @mszlez , I tried your code and get no error messages on my side. A couple of things that come to mind:

    • Make sure the extension is reloaded and that there are no remaining values assigned to global variables. Easiest way to do that is close Mechanical + WB project and reopen.
    • Try converting the format explicitly, for example in_file = str(object.Properties["input_file"].Value)
    • Are you sure that the error comes from these exact portions of the xml and .py code you shared? In the .xml there are no callbacks defined so GenerateAnalyses() will not be called.

  • mszlez
    mszlez Member Posts: 7
    Name Dropper First Comment
    edited July 2023
    Options

    Hi @Pernelle Marone-Hitz,

    Many thanks for your answer. Actually that's only a small portion of the whole code, didn't want to paste everything as that's quite a lot of lines and most of them are not relevant 🙂 But, let me share with you whole .xml + relevant python functions and describe what I'm trying to do. So:

    1. I'm creating an extension that consists of two wizards (not visible in .xml I share, as the comment will be to long) and also adds a toolbar in Mechanical that consists of couple of buttons. See below whole .xml file.
    2. Wizards are working fine, however the problem for myself and other users of the extension have is that if you have to stop/switch off your PC or there will be some error during execution of the wizard (causing wizard to close), you cannot come back to specific step.
    3. Therefore, my plan is to add the buttons mentioned before, which after clicking them will invoke functions normally invoked while going through the wizards.
    4. During wizards execution there are some fields to be filled by the user, so for doing the same using buttons I planned to create some objects in the Mechanical tree, where there will be properties representing same fields from the wizards.

    And then I'm in the place where I am now, i.e. I cannot read any property value due to the "expected int, got str" error 🙂 Actually I managed to overcome an issue with "fileopen" using python command "FileDialog.ShowOpenDialog", not the best solution I'd like to have, but working. But still, I have an issue with "select" control property, i.e. property named "pipe_side" in the code below.

    Maybe one more thing, in ACT log it says:

    line 2181, in GenerateAnalyses
    TypeError: expected int, got str

    And it's clearly pointing towards the line:

    side=str(object.Properties["pipe_side"].StringValue)
    

    .XML file:

    <!-- autogenerated by XmlWriter ( 19/04/2023 11:42:15 ) --><extension name="AF_Wizards_Rev_E">
    <imagedirectory>.</imagedirectory>
    <guid>22b6ec19-bf37-4224-97ab-efd37d8148a9</guid>
    <script src="IDEGeneratedMain.py" />
    <interface context="Mechanical">
    <images>images</images>
    <toolbar name="AF toolbar" caption="AF toolbar">
    <entry name="Setup analyses" icon="button1Red">
    <callbacks>
    <onclick>SteupAnalyses</onclick>
    </callbacks>
    </entry>
    <entry name="Generate analyses" icon="button2Red">
    <callbacks>
    <onclick>GenerateAnalyses</onclick>
    </callbacks>
    </entry>
    <separator/>

    </toolbar>
    </interface>
    <simdata context="Mechanical">
    <!-- Define <object> with affectsSolution attribute set to false. -->
    <object name="an_set" version="1" caption="Analyses setup" icon="group" isload="false" color="#0000FF" affectsSolution="false">
    <target type="datamodel"/>
    <callbacks></callbacks>
    <property control="select" name="pipe_side" caption="Pipeline side" persistent="False" parameterizable="False">
    <attributes options="Side A,Side B" /></property>
    </object>
    </simdata>


    </extension>Relevant python functions for the buttons:
    def SteupAnalyses(analysis):
    ExtAPI.DataModel.CreateObject("an_set", ExtAPI.ExtensionManager.CurrentExtension)
    def GenerateAnalyses(object):
    # Reading input file
    filters = "txt files (.txt)|.txt|All files (.)|."
    dir = "c:\\"
    res = FileDialog.ShowOpenDialog(ExtAPI.UserInterface.MainWindow,dir,filters,2,"Open FEA input file","")
    in_file=res[1]
    side=str(object.Properties["pipe_side"].Value)
    with open(in_file, 'r') as f:
    inputs = [[float(num) for num in line.split(',')] for line in f]

    # Initial definitions
    model=ExtAPI.DataModel.Project.Model
    model.Geometry.DisplayStyle=PrototypeDisplayStyleType.Material

    Pe_empty=inputs[0][7]
    Pi_flooded=inputs[1][6]
    Pe_flooded=inputs[1][7]
    Pe_ops=inputs[2][7]
    Pi_ops=inputs[2][6]

  • mszlez
    mszlez Member Posts: 7
    Name Dropper First Comment
    edited July 2023
    Options

    Hi again @Pernelle Marone-Hitz,

    Ok, seems like I found workaround. Instead of generating analyses with the button, I used a tree child object (i.e. the one visible after right click on the parent object in the tree) to invoke GenerateAnalyses() function. Works as expected and I'm pretty happy with that solution :) Not sure what was the problem with the one below, if you can see the issue and can let me know I'd be grateful :)

    xml:

    <!-- autogenerated by XmlWriter ( 19/04/2023 11:42:15 ) --><extension name="AF_Wizards_Rev_E">
    <imagedirectory>.</imagedirectory>
    <guid>22b6ec19-bf37-4224-97ab-efd37d8148a9</guid>
    <script src="IDEGeneratedMain.py" />
    <interface context="Mechanical">
    <images>images</images>
    <toolbar name="AF toolbar" caption="AF toolbar">
    <entry name="Setup analyses" icon="button1Red">
    <callbacks>
    <onclick>SteupAnalyses</onclick>
    </callbacks>
    </entry>
    <entry name="Generate analyses" icon="button2Red">
    <callbacks>
    <onclick>GenerateAnalyses</onclick>
    </callbacks>
    </entry>
    <separator/>
    <entry name="Setup results" icon="button3Red">
    <callbacks>
    <onclick>OnClickTB1Button3</onclick>
    </callbacks>
    </entry>
    <entry name="Generate results" icon="button1Blue">
    <callbacks>
    <onclick>OnClickTB2Button1</onclick>
    </callbacks>
    </entry>
    <separator/>
    <entry name="Setup output" icon="button2Blue">
    <callbacks>
    <onclick>OnClickTB2Button2</onclick>
    </callbacks>
    </entry>
    <entry name="Generate output" icon="button3Blue">
    <callbacks>
    <onclick>OnClickTB2Button3</onclick>
    </callbacks>
    </entry>
    </toolbar>
    </interface>
    <simdata context="Mechanical">
    <!-- Define <object> with affectsSolution attribute set to false. -->
    <object name="an_set" version="1" caption="Analyses setup" icon="group" isload="false" color="#0000FF" affectsSolution="false">
    <target type="datamodel"/>
    <callbacks></callbacks>
    <property control="select" name="pipeside" caption="Pipeline side" persistent="False" parameterizable="False">
    <attributes options="Side A,Side B" /></property>
    </object>
    </simdata>

    </extension>

    .py:

    def SteupAnalyses(analysis):
    ExtAPI.DataModel.CreateObject("an_set", ExtAPI.ExtensionManager.CurrentExtension)
    def GenerateAnalyses(object):
    # Reading input file
    filters = "txt files (.txt)|.txt|All files (.)|."
    dir = "c:\"
    res = FileDialog.ShowOpenDialog(ExtAPI.UserInterface.MainWindow,dir,filters,2,"Open FEA input file","")
    in_file=res[1]
    side=str(object.Properties["pipe_side"].ValueString)
    with open(in_file, 'r') as f:
    inputs = [[float(num) for num in line.split(',')] for line in f]

    # Initial definitions
    model=ExtAPI.DataModel.Project.Model
    model.Geometry.DisplayStyle=PrototypeDisplayStyleType.Material

    Pe_empty=inputs[0][7]
    Pi_flooded=inputs[1][6]
    Pe_flooded=inputs[1][7]
    Pe_ops=inputs[2][7]
    Pi_ops=inputs[2][6]

  • mszlez
    mszlez Member Posts: 7
    Name Dropper First Comment
    Options

    Hi @Pernelle Marone-Hitz,

    Amazing, thank you very much for your help. Many thanks for showing me the way how to debug, actually I was wondering if there is any possibility apart from looking on the errors in extension log :) Works perfectly now!

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 804
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    Hi @mszlez , thanks for your message, glad I could help :-) In think this debug method is the easiest one to use.