Can't get bRepBodies from imported STEP file

Can't get bRepBodies from imported STEP file

11713013
Contributor Contributor
595 Views
2 Replies
Message 1 of 3

Can't get bRepBodies from imported STEP file

11713013
Contributor
Contributor

Hi, there

I am writing a script to automatically generate a twist beam.

Here is my plan:

First, import the start joint and end joint to a Fusion 360 design.

Second, put them at a specific position with specific posture.

Then, get the faces for loft features.

11713013_0-1648456406216.png

Finally, generate the loft feature to get the twist beam.

11713013_1-1648456443698.png

 

By now, I have finished my first two steps with Fusion 360 API script.

I think I should get bRepBody first and then get the face from the bRepBody for the profile for loft.

But I am getting trouble for get bRepBodies from a component:

        allBodies = joint_down_comp.bRepBodies
        if allBodies.count < 1:
            ui.messageBox("Body count is not exist")
            return
        else:
            ui.messageBox("Find " + str(allBodies.count) + " bRepBodies")
            return

 It show that the number of bRepBodies is zero.

I get zero bRepBody from rootComp either.

So, how can I get the bRepBody from the imported STEP file for getting the faces to loft?

Here is my full code:

import os, sys, math
import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        # ui.messageBox('Hello script')

        importManager = app.importManager # get importManager

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        rootComp = design.rootComponent # get the root component
        objCollection = adsk.core.ObjectCollection.create()

        # Create a new occurrence to create an associated component to hold the first joint step
        trans1 = adsk.core.Matrix3D.create() 
        occ1 = rootComp.occurrences.addNewComponent(trans1) # the default trans matrix do not move the component from rootComp
        joint_up_comp = occ1.component
        joint_up_comp.name = "Up-Joint"

        # Import the up joint step to the origin point
        stepFileName1 = os.path.join(os.path.abspath(os.path.dirname(__file__)), "./STEP/joint_part_up.STEP")
        stepImportOptions1 = importManager.createSTEPImportOptions(stepFileName1)
        objUp = importManager.importToTarget2(stepImportOptions1, joint_up_comp)
        objCollection.add(objUp)

        # Create another occurrence to create an associated component to hold the below joint
        trans2 = adsk.core.Matrix3D.create()
        length = 10.0 # it is the length for loft(10cm)
        vec2 = adsk.core.Vector3D.create(0.0, 0.0, length+4) # the extral part of two joint is 40mm
        trans2.translation = vec2
        occ2 = rootComp.occurrences.addNewComponent(trans2)
        joint_down_comp = occ2.component
        joint_down_comp.name = "Down-Joint"

        # Import the down joint step to the setting point
        stepFileName2 = os.path.join(os.path.abspath(os.path.dirname(__file__)), "./STEP/joint_part_down.STEP")
        stepImportOptions2 = importManager.createSTEPImportOptions(stepFileName2)
        importManager.importToTarget(stepImportOptions2, joint_down_comp) 

        # Using transform to move the occurrence
        origin = adsk.core.Point3D.create() # to store the origine point of the occurrence's coordinate system
        xAxis = adsk.core.Vector3D.create() # to store the xAxis of the occurrence's coordinate system
        yAxis = adsk.core.Vector3D.create() # to store the yAxis of the occurrence's coordiante system
        zAxis = adsk.core.Vector3D.create() # to store the zAxis of the occurrence's coordiante system
        (origin, xAxis, yAxis, zAxis) = occ2.transform.getAsCoordinateSystem() # get the occurrence's coordinate system
        rotY = adsk.core.Matrix3D.create()
        rotY.setToRotation(math.pi, yAxis, origin)
        trans3 = occ2.transform
        trans3.transformBy(rotY)
        occ2.transform = trans3

        # twist the down joint
        theta = math.pi/6 # twist angle
        (origin, xAxis, yAxis, zAxis) = occ2.transform.getAsCoordinateSystem() # re-get the occurrence's coordinate system
        rotZ = adsk.core.Matrix3D.create()
        rotZ.setToRotation(theta, zAxis, origin)
        trans4 = occ2.transform
        trans4.transformBy(rotZ)
        occ2.transform = trans4 
        # ui.messageBox(str(xAxis.length))

        # add assembly clearance
        (origin, xAxis, yAxis, zAxis) = occ2.transform.getAsCoordinateSystem() # re-get the occurrence's coordinate system
        trans5 = occ2.transform
        # transY = yAxis.scaleBy(0.1) # translation alonge the y axis by 0.1 cm
        transY = adsk.core.Matrix3D.create()
        vecY = adsk.core.Vector3D.create(yAxis.x / 10, yAxis.y / 10, yAxis.z / 10)
        transY.translation = vecY
        trans5.transformBy(transY)
        occ2.transform = trans5

        # get the faces for loft feature
        allBodies = joint_down_comp.bRepBodies
        if allBodies.count < 1:
            ui.messageBox("Body count is not exist")
            return
        else:
            ui.messageBox("Find " + str(allBodies.count) + " bRepBodies")
            return

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Also, I put my script as a zip file in the attachment.

0 Likes
Accepted solutions (1)
596 Views
2 Replies
Replies (2)
Message 2 of 3

tykapl.breuil
Advocate
Advocate
Accepted solution

Hey there, just tested it out, when importing you are already creating a component which means everything is nested one more time hence why you can't find the bodies ;).

tykaplbreuil_0-1648458413911.png

I would advise to rename the component created by the import rather than creating a new one and your script should work wonders.

 

Message 3 of 3

11713013
Contributor
Contributor

Thank you so much for your reply.

You are right! The problem is caused by nesting the components. I just put the STEP file in the rootComp and solved this problem.

But here is another problem for getting the profiles of loft features.

I rewrite my code into this:

import os, sys, math
import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        # ui.messageBox('Hello script')

        importManager = app.importManager # get importManager

        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        rootComp = design.rootComponent # get the root component

        # Import the up joint step to the origin point
        stepFileName1 = os.path.join(os.path.abspath(os.path.dirname(__file__)), "./STEP/joint_part_up.STEP")
        stepImportOptions1 = importManager.createSTEPImportOptions(stepFileName1)
        importManager.importToTarget(stepImportOptions1, rootComp)

        # Import the down joint step to the setting point
        stepFileName2 = os.path.join(os.path.abspath(os.path.dirname(__file__)), "./STEP/joint_part_down.STEP")
        stepImportOptions2 = importManager.createSTEPImportOptions(stepFileName2)
        importManager.importToTarget(stepImportOptions2, rootComp) 

        occ1 = rootComp.occurrences.item(0) # refers to joint_part_up:1
        occ2 = rootComp.occurrences.item(1) # refers to joint_part_down:1

        ## Using transform to move the occurrence
        # rotate joint_part_down for 180 degrees along its y axis
        origin = adsk.core.Point3D.create() # to store the origine point of the occurrence's coordinate system
        xAxis = adsk.core.Vector3D.create() # to store the xAxis of the occurrence's coordinate system
        yAxis = adsk.core.Vector3D.create() # to store the yAxis of the occurrence's coordiante system
        zAxis = adsk.core.Vector3D.create() # to store the zAxis of the occurrence's coordiante system
        (origin, xAxis, yAxis, zAxis) = occ2.transform.getAsCoordinateSystem() # get the occurrence's coordinate system
        rotY = adsk.core.Matrix3D.create()
        rotY.setToRotation(math.pi, yAxis, origin)
        trans3 = occ2.transform
        trans3.transformBy(rotY)
        occ2.transform = trans3

        # translate along the world coordinate z axis
        length = 10.0 # the distance between two faces for loft, unit: cm
        transZ = adsk.core.Matrix3D.create()
        transZ.translation = adsk.core.Vector3D.create(0.0, 0.0, length+4) # the extral part of two joint is 40 mm
        trans = occ2.transform
        trans.transformBy(transZ)
        occ2.transform = trans

        # twist the down joint 
        theta = math.pi/6 # twist angle
        (origin, xAxis, yAxis, zAxis) = occ2.transform.getAsCoordinateSystem() # re-get the occurrence's coordinate system
        rotZ = adsk.core.Matrix3D.create()
        rotZ.setToRotation(theta, zAxis, origin)
        trans = occ2.transform
        trans.transformBy(rotZ)
        occ2.transform = trans

        # add assembly clearance
        (origin, xAxis, yAxis, zAxis) = occ2.transform.getAsCoordinateSystem() # re-get the occurrence's coordinate system
        # transY = yAxis.scaleBy(0.1) # translation alonge the y axis by 0.1 cm
        transY = adsk.core.Matrix3D.create()
        vecY = adsk.core.Vector3D.create(yAxis.x / 10, yAxis.y / 10, yAxis.z / 10)
        transY.translation = vecY
        trans = occ2.transform
        trans.transformBy(transY)
        occ2.transform = trans

        # get bRepFaces for loft feature
        point1 = adsk.core.Point3D.create(0.0, 0.0, 2.0) 
        face1 = rootComp.findBRepUsingPoint(point1, adsk.fusion.BRepEntityTypes.BRepFaceEntityType)
        # loops1 = face1.item(0).loops # bRepLoops
        # loop1 = loops1.item(0) # bRepLoop
        # edges1 = loop1.edges
        edges1 = face1.item(0).edges
        # ui.messageBox(str(edges1.count))
        profile1 = rootComp.createBRepEdgeProfile(edges1)
        # path1 = adsk.fusion.Path.create(edges1, adsk.fusion.ChainedCurveOptions.tangentChainedCurves)

        point2 = adsk.core.Point3D.create(0.0, 0.0, length+2.0)
        face2 = rootComp.findBRepUsingPoint(point2, adsk.fusion.BRepEntityTypes.BRepFaceEntityType)
        # loops2 = face2.item(0).loops
        # loop2 = loops2.item(0)
        # edges2 = loop2.edges
        edges2 = face2.item(0).edges
        profile2 = rootComp.createBRepEdgeProfile(edges2, False)
        # path2 = adsk.fusion.Path.create(edges2, adsk.fusion.ChainedCurveOptions.tangentChainedCurves)

        # create loft feature input
        loftFeats = rootComp.features.loftFeatures
        loftInput = loftFeats.createInput(adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        loftSectionsObj = loftInput.loftSections
        loftSectionsObj.add(profile1)
        loftSectionsObj.add(profile2)
        loftInput.isSolid = False
        loftInput.isClosed = False
        loftInput.isTangentEdgesMerged = True

        loftFeats.add(loftInput) # create loft feature


    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

However, a new problem comes out:

Failed:
Traceback (most recent call last):
  File "C:/Users/61648/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/LegGenerating/LegGenerating.py", line 81, in run
    profile1 = rootComp.createBRepEdgeProfile(edges1)
  File "C:/Users/61648/AppData/Local/Autodesk/webdeploy/production/50d1a2b00ac928c7781cbca6551e586a5384d498/Api/Python/packages\adsk\fusion.py", line 42403, in createBRepEdgeProfile
    return _fusion.Component_createBRepEdgeProfile(self, edges, chainEdges)
RuntimeError: 3 : invalid input edges

But I think I got correct bRepEdges because I output the number of edges and it shows the number of edges1 is 8(

ui.messageBox(str(edges1.count))).
Fusion360 API seems not easy for me when getting problems and it costs me a lot time.
Hoping for your reply!
0 Likes