Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Importing Electrical DXFs

1 REPLY 1
SOLVED
Reply
Message 1 of 2
osamaolabi
286 Views, 1 Reply

Importing Electrical DXFs

Hello everyone,

 

So I created a script that is able to import multiple DXFs and assign the correct layer to them. Since in my job I do get a lot of DXFs that need to be put together to create the final PCB.

 

Here is how the script works:

1. Opens the ULP window

2. Finds and runs the "import-dxf' ulp

3. Find the path of the folder that contains the dxfs (still in progress, right now it's a set path).

4. Based on the dxf name the layer is chosen (Dxf names are important)

5. Run the ULP.

 

The initial problem was that when the ULP window opens, my code freezes and only resumes after I close the ULP window. A workaround was to create a thread that runs the rest of the code simultaneously which allowed the rest of the code to excute when the ULP window opens.

 

Now at the end where I would like to create a Loop so it goes through all the dxfs in the folder, it stops working correctly. My assumption is that the threading is starting the next run before the first run ends.

 

So my question is, is there a way to get rid of the threading and be able to run the rest of the code when the ULP window opens? or is there a way to implement the loop with the threading?

 

Here is my full code:

 

 

# 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 that should be changed
        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"""
            with keyboard.pressed(Key.shift):
                keyboard.tap("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.tap(Key.tab)

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

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

        def choose_ulp():
            """This function is to find and choose the 'import-dxf' ULP"""
            time.sleep(1)

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

            keyboard.type("import-dxf")

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

            keyboard.tap(Key.down)
            keyboard.tap(Key.enter)

            dxf_settings()

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


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

 

 

This code needs to run while in "PCB Document"

Also the Folder_Path is going to be different since I am using my own path.

 

I inculded a small video on how it is working right now. Sorry for the bad quality. The loop should start the ULP menu again after it has done importing the first dxf and it should go until it has gone through all the dxf in the folder.

 

 

Thank you so much for your help in advance. I know this is a lot of code, so I really appreciate the help,

Osama A

Tags (3)
1 REPLY 1
Message 2 of 2
osamaolabi
in reply to: osamaolabi

OMG! Somehow after so much testing I was able to solve it, in a very weird way.

I noticed that the problem was happening for the first instance of the ULP window. so here is what I did

# 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 useless():
            """The Magical Function to rule them all"""
            pyautogui.hotkey('shift', 'u')
            pyautogui.press('esc')

            open_ulp()

        def open_ulp():
            """Function to open up the ULP Menu in Electronics Fusion360"""
            while len(current_dxf) > 0:
                pyautogui.hotkey('shift', 'u')
                choose_ulp()

        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.tap(Key.tab)

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

            # Runs the DXF with the chosen settings
            for i in range(9):
                keyboard.tap(Key.tab)
            keyboard.tap(Key.space)
            time.sleep(0.5)
            # Moves mouse to click on Run
            mouse.position = (((width / 2) - 60), ((height / 2) + 90))
            mouse.click(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.tap(Key.tab)

            keyboard.type("import-dxf")

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

            keyboard.tap(Key.down)
            keyboard.tap(Key.enter)

            dxf_settings()

        thread = threading.Thread(target=useless)
        thread.start()
        pyautogui.hotkey('shift', 'u')



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

 

If anyone would like to use this go ahead. The only thing left to do is to allow the user to choose the path they want, instead of having the path in the code fixed.

 

I am so happy I honestly started tearing. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report