- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Finally, generate the loft feature to get the twist beam.
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.
Solved! Go to Solution.