- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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?
Solved! Go to Solution.