How can I export the name of a part from mechanical to Python

Options
Mr.G
Mr.G Member Posts: 4
First Comment
**
edited June 12 in Structures
 def get_body(self):
        model_path = self.file_path_var.get()
        raw_list = self.controller.get_body_list(model_path)
        self.part_list = [str(item) for item in raw_list]
        self.update_all_comboboxes()
    def get_body_list(self,model_path):

        try:
            server_port = self.wb.start_mechanical_server(system_name='MODEL')
            self.mechanical = launch_mechanical(start_instance=False, port=server_port)
        except Exception as e:
            messagebox.showerror("错误", str(e))
            raise

        script2 = f'''
geometry = DataModel.GetObjectById(12)
geometry.Import(r"{model_path}")
part_list = [part.Name for part in DataModel.GetObjectsByType(DataModelObjectCategory.Part)]
return part_list'''
        part_list = self.mechanical.run_python_script(script2)
        return part_list

Answers

  • James Derrick
    James Derrick Administrator, Employee Posts: 379
    Ancient Membership 100 Comments 100 Likes 25 Answers
    admin

    Hi @Mr.G Can you elaborate on your question? I am not sure I understand the issue here.

  • Mr.G
    Mr.G Member Posts: 4
    First Comment
    **

    I designed a GUI interface in PyCharm with a button inside:
    ‘Button(material_placement_frame, text='确认', command=self.get_body, font='宋体 9', width=5).place(x=200, y=4)
    def get_body(self):
    model_path = self.file_path_var.get()
    raw_list = self.controller.get_body_list(model_path)
    self.part_list = [str(item) for item in raw_list]
    self.update_all_comboboxes()’
    Clicking this button will run the mechanical script:
    ‘ def get_body_list(self,model_path):

        try:
            server_port = self.wb.start_mechanical_server(system_name='MODEL')
            self.mechanical = launch_mechanical(start_instance=False, port=server_port)
        except Exception as e:
            messagebox.showerror("错误", str(e))
            raise
    
        script2 = f'''
    

    geometry = DataModel.GetObjectById(12)
    geometry.Import(r"{model_path}")
    part_list = [part.Name for part in DataModel.GetObjectsByType(DataModelObjectCategory.Part)]
    return part_list'''
    part_list = self.mechanical.run_python_script(script2)
    return part_list’
    I need to return part_list to set options for combobox,But this return value is'['I', 'r', 'o', 'n', 'P', 'y', 't', 'h', 'o', 'n', '.', 'R', 'u', 'n', 't', 'i', 'm', 'e', '.', 'L', 'i', 's', 't']'(IronPython.Runtime.List)
    Thank you for your reply

  • Mr.G
    Mr.G Member Posts: 4
    First Comment
    **

    click the button--run mechanical script--return part.name from mechanical to my GUI as a list(part_list)--use the list to be a combobox option