0

这是一个更复杂的问题,因为它连接到 Autodesk Fusion 360。

我正在致力于自动化从软件中导入 DXF 的过程。我正在使用Pynput 库来控制键盘以创建某些操作。第一个操作是通过按下快捷键 Shift + U 打开另一个窗口。之后 Pynput 命令暂停并等待该窗口关闭以恢复。我发现解决该问题的一种解决方法是线程化。

我能够完成接下来的步骤,并且它完美无缺,至少我是这么认为的。

最后,当我想创建一个循环,以便它可以遍历我选择的文件夹中的所有文件时,Fusion360 崩溃了。我的假设是,在 while 循环中,线程立即运行多次,而不是等待第一次刺激完成。

这是我的代码,任何有兴趣的人都可以看看:

# Author- Osama A
# Description- DXF Importer F360

import adsk.core, adsk.fusion, adsk.cam, traceback
from pynput.keyboard import Key
from pynput.mouse import Button
from tkinter import Tk
from tkinter.filedialog import askdirectory
import pyautogui
import pyperclip
import threading
import pynput
import time
import os

keyboard = pynput.keyboard.Controller()
mouse = pynput.mouse.Controller()
width, height = pyautogui.size()


def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        folder_path = "C:/Users/OsamaA/Desktop/Test_Folder/"
        current_dxf = [files for files in os.listdir(folder_path) if files.endswith('.dxf')]
        current_layer = [x[:-4] for x in current_dxf]

        def open_ulp():
            """Function to open up the ULP Menu in Electronics Fusion360"""
            keyboard.press(Key.shift)
            keyboard.press('u')
            keyboard.release(Key.shift)
            keyboard.release('u')

        def dxf_settings():
            """Function to locate DXFs and choose the appropriate layers for them and run them"""
            time.sleep(1)

            # choose dxf file
            keyboard.type(f"{folder_path}{current_dxf[0]}")
            for i in range(2):
                keyboard.press(Key.tab)
                keyboard.release(Key.tab)

            # Choose the appropriate layer for the dxf
            keyboard.press(Key.space)
            keyboard.release(Key.space)
            time.sleep(0.5)
            keyboard.type(current_layer[0])
            time.sleep(0.5)
            keyboard.press(Key.enter)
            keyboard.release(Key.enter)
            time.sleep(0.5)

            # Runs the DXF with the chosen settings
            for i in range(9):
                keyboard.press(Key.tab)
                keyboard.release(Key.tab)
            keyboard.press(Key.space)
            keyboard.release(Key.space)
            time.sleep(0.5)
            # Moves mouse to click on Run
            mouse.position = (((width / 2) - 60), ((height / 2) + 90))
            mouse.press(Button.left)
            mouse.release(Button.left)
            time.sleep(3)
            current_dxf.pop(0)
            current_layer.pop(0)

        def choose_ulp():
            time.sleep(1)

            for i in range(5):
                keyboard.press(Key.tab)
                keyboard.release(Key.tab)

            keyboard.type("import-dxf")

            for i in range(5):
                keyboard.press(Key.tab)
                keyboard.release(Key.tab)

            keyboard.press(Key.down)
            keyboard.release(Key.down)

            keyboard.press(Key.enter)
            keyboard.release(Key.enter)

            dxf_settings()

        thread = threading.Thread(target=choose_ulp)
        thread.start()
        open_ulp()


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

这是while循环添加:

        while len(current_dxf) > 0:
            thread = threading.Thread(target=choose_ulp)
            thread.start()
            open_ulp()

我认为我最好的选择是,如果我能够以某种方式摆脱线程并以某种方式让 pynput 在新窗口中启动命令。所以问题是,我怎样才能让它循环而不崩溃?

谢谢大家的帮助,奥萨马

4

0 回答 0