Geting this API working.

pedro_babo
Advocate
Advocate

Geting this API working.

pedro_babo
Advocate
Advocate

Hi I've developde a scrip based on @BrianEkins CenterOfRange.

 

It creates points on each middle face of the bounding box.

I'm trying to transform it into one add-In but I always get fusion to crash.

 

Could some one look at this and figure out what is wrong?

 

the code:

 

    # TODO ******************************** Your code here ********************************

   

 

    # Get all components in the active design.

   

    doc = app.activeDocument

    des = doc.products[0]

    _ui = app.userInterface

    if not des:

        _ui.messageBox('No active Fusion design', 'Center of Bounds')

        return

       

    # Have a body selected.

    bodySelection = _ui.selectEntity('Select the body', 'MeshBodies,Bodies')       

    if not bodySelection:

        return False

 

    drawBox = False           

    if _ui.messageBox('Draw the bounding box points?', 'Bounding Box Center Points', adsk.core.MessageBoxButtonTypes.YesNoButtonType, adsk.core.MessageBoxIconTypes.QuestionIconType) == adsk.core.DialogResults.DialogYes:

        drawBox = True

 

    # Check to see if a b-rep or mesh body was selected.

    triMesh = adsk.fusion.TriangleMesh.cast(None)

    if isinstance(bodySelection.entity, adsk.fusion.BRepBody):

        brepBody = adsk.fusion.BRepBody.cast(bodySelection.entity)

        triMesh = brepBody.meshManager.displayMeshes.bestMesh

        body = brepBody

    elif isinstance(bodySelection.entity, adsk.fusion.MeshBody):

        meshBody = adsk.fusion.MeshBody.cast(bodySelection.entity)

        triMesh = meshBody.displayMesh

        body = meshBody

 

    # Calculate the bounding box of the mesh.

    smallPnt = adsk.core.Point3D.cast(triMesh.nodeCoordinates[0])

    largePnt = adsk.core.Point3D.cast(triMesh.nodeCoordinates[0])

    vertex = adsk.core.Point3D.cast(None)

    for vertex in triMesh.nodeCoordinates:

        if vertex.x < smallPnt.x:

            smallPnt.x = vertex.x

           

        if vertex.y < smallPnt.y:

            smallPnt.y = vertex.y

           

        if vertex.z < smallPnt.z:

            smallPnt.z = vertex.z

           

        if vertex.x > largePnt.x:

            largePnt.x = vertex.x

               

        if vertex.y > largePnt.y:

            largePnt.y = vertex.y

               

        if vertex.z > largePnt.z:

            largePnt.z = vertex.z

 

    #center Points.

    centerPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, (smallPnt.y+largePnt.y)/2, (smallPnt.z+largePnt.z)/2)

    centerXMinusPnt = adsk.core.Point3D.create(smallPnt.x, (smallPnt.y+largePnt.y)/2, (smallPnt.z+largePnt.z)/2)

    centerXPlusPnt = adsk.core.Point3D.create(largePnt.x, (smallPnt.y+largePnt.y)/2, (smallPnt.z+largePnt.z)/2)

    centerYMinusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, smallPnt.y, (smallPnt.z+largePnt.z)/2)

    centerYplusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, largePnt.y, (smallPnt.z+largePnt.z)/2)

 

    #center Corner Points.

    centerCornerXPlusYPlusPnt = adsk.core.Point3D.create(largePnt.x, largePnt.y, (smallPnt.z+largePnt.z)/2)

    centerCornerXPlusYMinusPnt = adsk.core.Point3D.create(largePnt.x, smallPnt.y, (smallPnt.z+largePnt.z)/2)

    centerCornerXMinusYPlusPnt = adsk.core.Point3D.create(smallPnt.x, largePnt.y, (smallPnt.z+largePnt.z)/2)

    centerCornerXMinusYminusPnt = adsk.core.Point3D.create(smallPnt.x, smallPnt.y, (smallPnt.z+largePnt.z)/2)

 

    #center Top Points.

    centerTopPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, (smallPnt.y+largePnt.y)/2,largePnt.z)

    centerTopXMinusPnt = adsk.core.Point3D.create(smallPnt.x, (smallPnt.y+largePnt.y)/2, largePnt.z)

    centerTopXPlusPnt = adsk.core.Point3D.create(largePnt.x, (smallPnt.y+largePnt.y)/2, largePnt.z)

    centerTopYMinusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, smallPnt.y, largePnt.z)

    centerTopYplusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, largePnt.y, largePnt.z)

 

    #center Bottom Points.

    centerBotomPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, (smallPnt.y+largePnt.y)/2, smallPnt.z)

    centerBotomXMinusPnt = adsk.core.Point3D.create(smallPnt.x, (smallPnt.y+largePnt.y)/2, smallPnt.z)

    centerBotomXPlusPnt = adsk.core.Point3D.create(largePnt.x, (smallPnt.y+largePnt.y)/2, smallPnt.z)

    centerBotomYMinusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, smallPnt.y, smallPnt.z)

    centerBotomYplusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, largePnt.y, smallPnt.z)

 

    # Get the root component of the active design

    rootComp = des.rootComponent

       

    # Special case for parametric or direct edit design.

    if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:

        # Check to see if a base feature named "Center of Bounds" exists.

        baseFeature = rootComp.features.itemByName('Center of Block')

        if not baseFeature:

            # Create a new base feature.

            baseFeature = rootComp.features.baseFeatures.add()

            baseFeature.name = 'Center of Block'

           

        # Begin editing the base feature.

        baseFeature.startEdit()

        inEdit = True         

 

        # Get the full path of the body.

        bodyName = getPath(body) + '/' + body.name

   

        # Check to see if there's already a sketch for this body.

        skAttribs = des.findAttributes('CenterOfBounds', bodyName)

 

        if len(skAttribs) > 0:

            skAttrib = skAttribs[0]

            if skAttrib.parent:        

                sketch = adsk.fusion.Sketch.cast(skAttrib.parent)

               

                # Delete the contents of the sketch.

                ents = adsk.core.ObjectCollection.create()

                for line in sketch.sketchCurves.sketchLines:

                    ents.add(line)

                   

                for pnt in sketch.sketchPoints:

                    ents.add(pnt)

                   

                des.deleteEntities(ents)

            else:

                skAttrib.deleteMe()

                sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)

                sketch.baseOrFormFeature = baseFeature

                sketch.attributes.add('CenterOfBounds', bodyName, '')

        else:

            sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)

            sketch.name = 'Centre of Block'

            

            if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:

                sketch.baseOrFormFeature = baseFeature

 

            sketch.attributes.add('CenterOfBounds', bodyName, '')

 

        sketch.areProfilesShown = False

        sketch.areDimensionsShown = True

 

        #center Points.

        sketch.sketchPoints.add(centerPnt)

        sketch.sketchPoints.add(centerXMinusPnt)

        sketch.sketchPoints.add(centerXPlusPnt)

        sketch.sketchPoints.add(centerYMinusPnt)

        sketch.sketchPoints.add(centerYplusPnt)

 

        #center Corner Points.

        sketch.sketchPoints.add(centerCornerXPlusYPlusPnt)

        sketch.sketchPoints.add(centerCornerXPlusYMinusPnt)

        sketch.sketchPoints.add(centerCornerXMinusYPlusPnt)

        sketch.sketchPoints.add(centerCornerXMinusYminusPnt)

 

        #center Top Points.

        sketch.sketchPoints.add(centerTopPnt)

        sketch.sketchPoints.add(centerTopXMinusPnt)

        sketch.sketchPoints.add(centerTopXPlusPnt)

        sketch.sketchPoints.add(centerTopYMinusPnt)

        sketch.sketchPoints.add(centerTopYplusPnt)

 

        #center Bottom Points.

        sketch.sketchPoints.add(centerBotomPnt)

        sketch.sketchPoints.add(centerBotomXMinusPnt)

        sketch.sketchPoints.add(centerBotomXPlusPnt)

        sketch.sketchPoints.add(centerBotomYMinusPnt)

        sketch.sketchPoints.add(centerBotomYplusPnt)

 

        if drawBox:

            lines = sketch.sketchCurves.sketchLines

           

            pnt1 = adsk.core.Point3D.create(smallPnt.x, largePnt.y, smallPnt.z)

            line = lines.addByTwoPoints(smallPnt, pnt1)

            line.isConstruction = True

            pnt1 = line.endSketchPoint

            smallPntSKT =line.startSketchPoint

           

            pnt2 = adsk.core.Point3D.create(smallPnt.x, smallPnt.y, largePnt.z)

            line = lines.addByTwoPoints(smallPnt, pnt2)

            line.isConstruction = True

            pnt2 = line.endSketchPoint

 

            pnt3 = adsk.core.Point3D.create(largePnt.x, smallPnt.y, smallPnt.z)

            line = lines.addByTwoPoints(smallPnt, pnt3)

            line.isConstruction = True

            pnt3 = line.endSketchPoint

 

            pnt4 = adsk.core.Point3D.create(smallPnt.x, largePnt.y, largePnt.z)

            line = lines.addByTwoPoints(largePnt, pnt4)

            line.isConstruction = True

            pnt4 = line.endSketchPoint

                       

            pnt5 = adsk.core.Point3D.create(largePnt.x, largePnt.y, smallPnt.z)

            line = lines.addByTwoPoints(largePnt, pnt5)

            line.isConstruction = True

            pnt5 = line.endSketchPoint

 

            pnt6 = adsk.core.Point3D.create(largePnt.x, smallPnt.y, largePnt.z)

            line = lines.addByTwoPoints(largePnt, pnt6)

            line.isConstruction = True

            pnt6 = line.endSketchPoint

           

            line = lines.addByTwoPoints(pnt1, pnt4)

            line.isConstruction = True

            line = lines.addByTwoPoints(pnt1, pnt5)

            line.isConstruction = True

            line = lines.addByTwoPoints(pnt2, pnt4)

            line.isConstruction = True

            line = lines.addByTwoPoints(pnt2, pnt6)

            line.isConstruction = True

            line = lines.addByTwoPoints(pnt3, pnt5)

            line.isConstruction = True

            line = lines.addByTwoPoints(pnt3, pnt6)

            line.isConstruction = True

 

 

 

            if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:                       

            # End the base feature edit.

                baseFeature.finishEdit()

 

def getPath(ent):

    path = ''

    if ent.assemblyContext:

        occ = ent.assemblyContext

        while occ:

            if path == '':

                path = occ.name

            else:

                path = occ.name + '/' + path

            occ = occ.assemblyContext              

        path = 'Root/' + path

    else:

        path = 'Root'

    return path

 

 

0 Likes
Reply
Accepted solutions (1)
469 Views
5 Replies
Replies (5)

kandennti
Mentor
Mentor

Hi @pedro_babo .

 

I am not sure of the details due to lack of information, but I believe you are using CommandDefinitions.addButtonDefinition to create an add-in that uses a dialog.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-04eb6198-72cd-4430-a6a4-8d68a1105b8e 

 

I remember crashing when using the UserInterface.selectEntity method with a dialog.

I would recommend using SelectionCommandInput to create the dialog instead of the selectEntity method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-8B9041D5-75CC-4515-B4BB-4CF2CD5BC359#Selecti... 

 

 

0 Likes

pedro_babo
Advocate
Advocate

Hi  @kandennti .

 

Thanks for the reply this is the first time i convert a script to one adin I will investigate.

 

0 Likes

pedro_babo
Advocate
Advocate

Hi  @kandennti .

 

The API no longer crash.

I’ve added selection input. But now it’s doing nothing. I have the Idea that I’m not getting the correct way the selected Body.

 

    # Create a simple selection command.
    bodySelection = inputs.addSelectionInput('SelectedBody', 'Select the body', 'Select one body or MeshBody')
    bodySelection.addSelectionFilter(adsk.core.SelectionCommandInput.Bodies)

 

I’m using to get the selected data

 

    # Get selected body
    inputs = args.command.commandInputs
    bodySelection: adsk.core.SelectionCommandInput = inputs.itemById('SelectedBody')
 
but this fails to get BodyRep or from the selection.
 
    # Check to see if a b-rep or mesh body was selected.
    triMesh = adsk.fusion.TriangleMesh.cast(None)
    if isinstance(bodySelection.selection, adsk.fusion.BRepBody):
        brepBody = adsk.fusion.BRepBody.cast(bodySelection.selection)
        triMesh = brepBody.meshManager.displayMeshes.bestMesh
        body = brepBody
        _ui.messageBox('Body')
    elif isinstance(bodySelection.selection, adsk.fusion.MeshBody):
        meshBody = adsk.fusion.MeshBody.cast(bodySelection.selection)
        triMesh = meshBody.displayMesh
        body = meshBody
        _ui.messageBox('Mesh')
 

The objective is to get the bounding box of the selected body.

 

Could someone help here?

0 Likes

j.han97
Advocate
Advocate

Hi @pedro_babo,

bodySelection.selection

 is a function to get one of the selections with specified index. Let's say your selection input (i.e., bodySelection) has only 1 selected item, you can get the bRepBody with

bodySelection.selection(0).entity

Please refer to the documentation here for more information:

Fusion 360 Help | SelectionCommandInput.selection Method | Autodesk

 

0 Likes

pedro_babo
Advocate
Advocate
Accepted solution

Hi  @j.han97 and @kandennti 

 

It's working many thanks.

 

 # Check to see if a b-rep or mesh body was selected.
    triMesh = adsk.fusion.TriangleMesh.cast(None)
    if isinstance(bodySelection.selection(0).entity, adsk.fusion.BRepBody):
        brepBody = adsk.fusion.BRepBody.cast(bodySelection.selection(0).entity)
        triMesh = brepBody.meshManager.displayMeshes.bestMesh
        body = brepBody
        _ui.messageBox('Body')
    elif isinstance(bodySelection.selection(0).entity, adsk.fusion.MeshBody):
        meshBody = adsk.fusion.MeshBody.cast(bodySelection.selection(0).entity)
        triMesh = meshBody.displayMesh
        body = meshBody
        _ui.messageBox('Mesh')

 

Regards

1 Like