Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Threading stops on document change

Joshua.mursic
Advocate

Threading stops on document change

Joshua.mursic
Advocate
Advocate

Hello everyone, I have created a thread to watch a setup so the user can see its generation status even if they are in a different document.

 

 

import adsk.core,adsk.fusion,adsk.cam,traceback,threading,time
app = adsk.core.Application.get()
ui = app.userInterface
ACTIVE_THREADS = []

#Monitors a setups progress
class SetupMonitor(threading.Thread):
    def __init__(self, setup:adsk.cam.Setup):
        '''setup is a object collection of setups'''
        threading.Thread.__init__(self)
        global ACTIVE_THREADS
        self.running = True
        self.setup = setup
        self.graphicsGroup = None
        self.createText()
        ACTIVE_THREADS.append(self)

    def createText(self):
        try:
            global ACTIVE_THREADS
            design:adsk.fusion.Design = app.activeDocument.products.itemByProductType('DesignProductType')
            cam:adsk.cam.CAM = app.activeDocument.products.itemByProductType('CAMProductType')
            root = design.rootComponent

            #get the number of completed operations
            completed = 0
            for operation in self.setup.operations:
                complete = cam.checkToolpath(operation)
                if complete:
                    completed += 1

            #create new graphics group
            graphics = root.customGraphicsGroups.add()
            graphics.isVisible = False
            offset = len(ACTIVE_THREADS)*5
            graphicsText = graphics.addText(f'{self.setup.name}: {completed}/{self.setup.allOperations.count}', 'Arial', 3, adsk.core.Matrix3D.create())
            graphicsText.isBold = True
            graphicsText.color =  adsk.fusion.CustomGraphicsSolidColorEffect.create(adsk.core.Color.create(255,255,255,255))
            billBoard = adsk.fusion.CustomGraphicsBillBoard.create(adsk.core.Point3D.create(0,0,0))
            billBoard.billBoardStyle = adsk.fusion.CustomGraphicsBillBoardStyles.ScreenBillBoardStyle 
            graphicsText.billBoarding = billBoard

            #use exsisting or create a new anchor point
            if self.graphicsGroup: #use the exsisting anchor point
                text:adsk.fusion.CustomGraphicsText = self.graphicsGroup.item(0)
                anchorPoint = text.viewPlacement.anchorPoint
                #delete old graphics
                self.graphicsGroup.deleteMe()
            else:
                anchorPoint = adsk.core.Point3D.create(graphicsText.width,graphicsText.height+offset,0)

            # Set the text to use view scaling.
            graphicsText.viewScale = adsk.fusion.CustomGraphicsViewScale.create(4, adsk.core.Point3D.create(0,0,0))
            viewPlace = adsk.fusion.CustomGraphicsViewPlacement.create(anchorPoint,adsk.fusion.ViewCorners.upperRightViewCorner,adsk.core.Point2D.create(10, 80))
            graphicsText.viewPlacement = viewPlace

            self.graphicsGroup = graphics
            graphics.isVisible = True
            app.activeViewport.refresh()
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

    def run(self):
        while self.running:
            cam:adsk.cam.CAM = app.activeDocument.products.itemByProductType('CAMProductType')
            toolpathsComplete = cam.checkToolpath(self.setup)
           
            if toolpathsComplete:
                self.graphicsGroup.deleteMe()
            
            else:
                #update the progress
                time.sleep(5)
                app.log('working')
                self.createText()
                adsk.doEvents()
         
    def stop(self):
        global ACTIVE_THREADS
        app.log("Stopping Thread")
        self.running = False
        ACTIVE_THREADS.remove(self)

 

 

The issue I am having is that when the user moves to a different document the threading stops which is unexpected. The threading continues even if you stop the addon that is running it. so i am confused as to why it is stopping on a document change.

 

The threading class is inside the addons main code, the one with the start() and stop() functions. (I am not sure if this is the correct naming, I am not a coder). So when the command that creates the thread is destroyed, the thread should keep going right?

0 Likes
Reply
Accepted solutions (1)
286 Views
4 Replies
Replies (4)

BrianEkins
Mentor
Mentor
Accepted solution

I'm not sure exactly what's going on, but I don't believe what you're attempting to do will work. Most of Fusion is single-threaded. There are a few things where Fusion will start background threads to do some computation (like process CAM operations), but they're the exception. The API runs in the single main thread; you can't call it from another thread. A workaround to be able to make calls from another thread is use a Custom Event, which is described here: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-F9FD4A6D-C59F-4176-9003-CE04F7558CCC

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

Joshua.mursic
Advocate
Advocate

I understand, thank you for the information @BrianEkins. Would the built in setupChanged event trigger when an operation completes its calculation? Are there events for operations?

0 Likes

BrianEkins
Mentor
Mentor

Events are fired on the main thread, so your program can listen for them and make API calls in reaction. I don't believe there are events for operations yet. Exposing events is tricky because they need to be fired when it's safe for the client to use the API.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

Joshua.mursic
Advocate
Advocate

Thank you for your help 

0 Likes