- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Denunciar
Hello everyone,
So Fusion behaves very weirdly when you try to cause the Execute Preview event from a custom event, everything happens seemingly without issue, but if a fillet (and probably other features) has an error, then the fillet features of the component become "broken" and don't work anymore. This problem doesn't occur when using a separate thread to trigger that event. I the code below I have a test figure that first creates itself normally and then, when the preview event is called again either through a separate thread or a custom event, creates itself with a value for the fillet too big for it to work. Then it gradually reduces this value until it can be created. However, when the preview is caused by a custom event, the first error causes a problem with the fillet features and the creation fails altogether.
Honestly I have absolutely no idea what could cause this. Does this also happen on other machines (simply change threadMode to True of False to test both possibilities) ? Does anyone have any idea what is actually happening here ?
import adsk.core, adsk.fusion, adsk.cam, traceback
import threading
# Affectations usuelles
handlers = []
ui = None
app : adsk.core.Application = adsk.core.Application.get()
if app:
ui = app.userInterface
threadMode = False
first = True
class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandCreatedEventArgs):
try:
cmd = args.command
inputs = cmd.commandInputs
txt = inputs.addBoolValueInput('id', '', True,'', True)
onExecutePreview = CommandExecuteHandler()
cmd.executePreview.add(onExecutePreview)
handlers.append(onExecutePreview)
onExecutePreview = CommandExecuteHandler()
cmd.execute.add(onExecutePreview)
handlers.append(onExecutePreview)
if not threadMode:
app.unregisterCustomEvent('TestTrucCustomEvent')
customEvent = app.registerCustomEvent('TestTrucCustomEvent')
onTestEvent = TestEventHandler()
onTestEvent.cmd = cmd
customEvent.add(onTestEvent)
handlers.append(onTestEvent)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class CommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandEventArgs):
try:
command: adsk.core.Command = args.firingEvent.sender
testFigure = TestFigure()
global first
if not first:
testFigure.parameter = 2
testFigure.build()
if first:
first = False
if threadMode:
thread = BackgroundThread()
thread.command = command
thread.start()
else:
app.fireCustomEvent('TestTrucCustomEvent')
else:
adsk.autoTerminate(True)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class TestEventHandler(adsk.core.CustomEventHandler):
def __init__(self):
super().__init__()
self.cmd = adsk.core.Command
def notify(self, args: adsk.core.CustomEventArgs):
try:
app.executeTextCommand('Command.SetBool id 0')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class BackgroundThread(threading.Thread):
def __init__(self):
super().__init__()
self.running = True
def run(self):
while self.running:
app: adsk.core.Application = adsk.core.Application.get()
app.executeTextCommand('Command.SetBool id 0')
self.running = False
def run(context):
ui = None
try:
ui = app.userInterface
cmdDefinitions = ui.commandDefinitions
btnCmdDefinition = cmdDefinitions.itemById('testTrucCommand')
if btnCmdDefinition:
btnCmdDefinition.deleteMe()
btnCmdDefinition = cmdDefinitions.addButtonDefinition('testTrucCommand', '', '')
onCreated = CommandCreatedHandler()
btnCmdDefinition.commandCreated.add(onCreated)
handlers.append(onCreated)
btnCmdDefinition.execute()
adsk.autoTerminate(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def createNewComponent() -> adsk.fusion.Component:
# Get the active design.
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent
allOccs = rootComp.occurrences
newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create())
return newOcc.component
class TestFigure:
def __init__(self) -> None:
self.parameter = 0.4
def build(self):
newComp = createNewComponent()
sketch: adsk.fusion.Sketch = newComp.sketches.add(newComp.xYConstructionPlane)
sketch.sketchCurves.sketchLines.addCenterPointRectangle(adsk.core.Point3D.create(), adsk.core.Point3D.create(1, 1, 0))
extrudes = newComp.features.extrudeFeatures
distance = adsk.core.ValueInput.createByReal(1)
extrude = extrudes.addSimple(sketch.profiles.item(0), distance, 0)
edges = extrude.bodies.item(0).edges
fillets = newComp.features.filletFeatures
objColl: adsk.core.ObjectCollection = adsk.core.ObjectCollection.create()
for edge in edges:
objColl.add(edge)
radius1Value = self.parameter
while radius1Value > 0.2:
try:
input1 = fillets.createInput()
radius1 = adsk.core.ValueInput.createByReal(radius1Value)
input1.addConstantRadiusEdgeSet(objColl, radius1, True)
fillet1 = fillets.add(input1)
radius1Value = 0
except:
fillets = newComp.features.filletFeatures
fillet1 = fillets.item(fillets.count - 1)
if fillet1.healthState: # Si le dernier fillet crée est en bonne santé, la valeur est de 0
fillet1.deleteMe()
radius1Value -= 0.5
if radius1Value <= 0.2:
app.log('Echec de la création du congé entre la partie supérieure et la base de la fourche')
¡Resuelto! Ir a solución.