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: 

Body Highlighted in Blue in Command Preview

0 REPLIES 0
Reply
Message 1 of 1
nnamfoh
1517 Views, 0 Replies

Body Highlighted in Blue in Command Preview

I have built a system that highlights portions of objects based on whether or not they interfer with other objects. One of the colors should be green when something intersects something else. However, during the command preview that highlights these selections, the interference is blue, the same blue as when you select a body. I can't figure out why this is.

If I change the preview to be an execution, which should never happen but can be useful for debugging, the whole system crashes, closing both Fusion 360 and Spider. I get no error messages on the console. Can anyone see where this error occurs?

# Command Code:
class CommandCreatedEventObjectValidation(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = args.command
            inputs = cmd.commandInputs
            onInput = InputChangedObjectValidationHandler()
            onExecutePreview = CommandValidateObjectHandler()
            cmd.executePreview.add(onExecutePreview)
            
            cmd.inputChanged.add(onInput)
            # keep the handler referenced beyond this function
            Globals.handlers.append(onExecutePreview)
            Globals.handlers.append(onInput)
            # Keep the handler referenced beyond this function
            objType = Globals.selectedObject.__class__
            if objType == DesignerObject:
                constraints = Globals.selectedObject.customPositionalConstraints
                Globals.ui.messageBox(str(constraints))
            else:
                constraints = objType.PositionalConstraintTypes
            for key in constraints:
                tab = inputs.addTabCommandInput(key, key)
                BuildConstraintCommandIntoTab(constraints[key], tab)
        except:
            if Globals.ui:
                Globals.ui.messageBox('Create command created failed: {}'.format(traceback.format_exc()))
class InputChangedObjectValidationHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
#            Globals.ui.messageBox("Input Changed")
            cmdInput = args.input
            if cmdInput.classType() == adsk.core.CommandInput.classType():
                inputs = cmdInput.commandInputs
                for inpt in inputs:
                    if inpt.classType()== adsk.core.TabCommandInput.classType():
                        if not inpt.isActive: #TODO: Unfortunate bug, if anything is selected ahead of time this seems to crash fusion
                            for child in inpt.children:#BUGFIX: unfortunatly, Fusion 360 Crashes if you do not clear selections
                            #TODO: fix has not been validated for paramters that are not selections
                                if child.classType() == adsk.core.SelectionCommandInput.classType():
#                                    Globals.ui.messageBox(child.id+" selecting "+child.selection(0).entity.classType())
                                    child.isEnabled =False
#                                    child.clearSelection()   
#                                    Globals.ui.messageBox("Selection count: "+str(child.selectionCount))
                        else:
                            for child in inpt.children:
                                if child.classType() == adsk.core.SelectionCommandInput.classType():
#                                    Globals.ui.messageBox(child.id+" selecting "+child.selection(0).entity.classType())
                                    child.isEnabled =True
#                                    child.clearSelection()   
#                                    Globals.ui.messageBox("Selection count: "+str(child.selectionCount))
#            Globals.ui.messageBox("Adapted To Input Change")
        except:
            if Globals.ui:
                Globals.ui.messageBox('Input changed event failed: {}'.format(traceback.format_exc()))
class CommandValidateObjectHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            command = args.firingEvent.sender
            inputs = command.commandInputs
            constraintName = None
            for inpt in inputs:
                if inpt.classType()== adsk.core.TabCommandInput.classType():
                    if inpt.isActive:
                        constraintName= inpt.id
            objType = type(Globals.selectedObject)
            if objType == DesignerObject:
                constraints = Globals.selectedObject.customPositionalConstraints
            else:
                constraints = objType.PositionalConstraintTypes
            constraintType = constraints[constraintName]
            try:
                activeConstraint = constraintType(Globals.selectedObject)
                activeConstraint.activate()
            except:
#                Globals.ui.messageBox('command executed failed: {}'.format(traceback.format_exc()))
                pass #TODO: work around for things that don't have parameters filled
        except:
            if Globals.ui:
                Globals.ui.messageBox('command executed failed: {}'.format(traceback.format_exc()))


#Code that does the interference highlighting. Assume that this attached to a complex system and well tested
def splitInterference(self):
        modelBodies = []
        compBodies = []
        for body in self.copyModel.bRepBodies:
            modelBodies.append(body)
        for body in self.copyComponent.bRepBodies:
            compBodies.append(body)
        if len(modelBodies)==0:
            self.uncontainedBodies = compBodies
            self.ui.messageBox("No other model geometry found")
            return
#        self.ui.messageBox("Collected models and object geometry")
        tools = adsk.core.ObjectCollection.create()
        for body in compBodies:
            tools.add(body)
#        self.ui.messageBox("Tools collection contains component bodies")
        for body in modelBodies:
#            self.ui.messageBox("Consider model body: "+body.name)
            intersection = combineBodies(self.objectInstance.objectInstance, body, tools, adsk.fusion.FeatureOperations.IntersectFeatureOperation, True)
#            self.ui.messageBox(body.name+" is intersected with tools collection")
            for ibody in intersection.bodies:
#                self.ui.messageBox(ibody.name+" Is an intersection body of comp with model")
                self.intersectionBodies.append(ibody)
#                self.ui.messageBox("Intersections is now: "+str(self.intersectionBodies))
        tools = adsk.core.ObjectCollection.create()
#        self.ui.messageBox("Tools is reset")
        for body in self.intersectionBodies:
            tools.add(body)
#        self.ui.messageBox("Tool contains intersection bodies")
        if tools.count ==0:
#            self.ui.messageBox("There were no intersections")
            for body in compBodies:
                self.uncontainedBodies.append(body)
#            self.ui.messageBox("All comp bodies are set as uncontained")
            return
#        self.ui.messageBox("Not all bodies were uncontained")
        for body in compBodies:
#            self.ui.messageBox("Consider comp body: "+body.name)
            intersection = combineBodies(self.objectInstance.objectInstance, body, tools, adsk.fusion.FeatureOperations.CutFeatureOperation, True)
#            self.ui.messageBox(body.name+" is intersected with tools from intersections")
            for ubody in intersection.bodies:
#                self.ui.messageBox(ubody.name+"is an uncontained body")
                self.uncontainedBodies.append(ubody)
#                self.ui.messageBox("Uncontained bodies are now: "+str(self.uncontainedBodies))

Capture.PNG

0 REPLIES 0

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