hexagon logo

Error in Executing Python Command in Dialogue Box

I am trying to execute below python command block with gui dialogue box, but is is not working.

It works when I run through python file by just normal file import.

language switch_to python
import Adams
import os

default_folder = os.getcwd()
m = Adams.getCurrentModel()
try:
    files = [f for f in os.listdir(default_folder) if f.endswith(('.x_t', '.xmt_txt'))]
except FileNotFoundError:
    print(f"Error: Folder '{default_folder}' not found.")
    exit()

if not files:
    print(f"No Parasolid files found in '{default_folder}'.")
    exit()

for file_name in files:
    part_name = os.path.splitext(file_name)[0]
    file_path = os.path.join(default_folder, file_name)

    try:
        m.Parts.createRigidBody(name=part_name)
        Adams.read_parasolid_file(file_name=file_path, part_name=part_name)
        print(f"Successfully imported '{part_name}' from '{file_path}'.")
    except Exception as e:
        print(f"Error importing '{part_name}' from '{file_path}': {e}")

Parents
  • Do you mean that you are pasting the commands into the Command Window?  In which case, there are a few constraints.  Firstly, the language switch_to python command will not work in this context and the commands will not automatically be interpreted as Python commands; use the drop-down selection in the bottom-right of the window:

    Please see the Documentation [Adams Basic Package > Adams View > Adams View > Dialog box - F1 help > A - D > Command Window] for more information.

    Secondly, be careful of white space, for example, on line 20.  In Python mode, the Command Window will act like a Python shell.  Therefore, a blank line is effectively a press of the enter key, so it will be interpreted that the for loop from line 17 is ended.  As such, this will produce an IndentationError for line 21.

    FYI, if you use VSCode, there is an MSC Adams  extension that allows Python commands to be run in an open session of Adams as one of its features.  Please see VSMarketPlace/MSC-Adams  for more information.

Reply Children
No Data