Trouble adding second body with script

Trouble adding second body with script

brad.bylls
Collaborator Collaborator
1,874 Views
10 Replies
Message 1 of 11

Trouble adding second body with script

brad.bylls
Collaborator
Collaborator

Hi all,

Still learning the API, but I'm stumped.

I have cannibalized the Bolt.py example script to create socket head cap screws.

I have been able to create the screw, but having trouble adding a body to envelope the screw to be used as a cut in the combine feature to create the counterbore, clearance hole and tap drill hole for the screw.

If I run the code with the 'create the head clearance' remarked out, I get a second body in the component. Perfect.

however, when I unremark the code to join it to the 'create the body clearance' it join both of the bodies to the original screw. Back to only one body in the component. Not what I want.

I need a screw body and a cut body in the one component.

Code below:

        #create the body clearance
        subtractBodySketch = sketches.add(xyPlane)
        subtractBodySketch.sketchCurves.sketchCircles.addByCenterRadius(center, self.bodyClearance/2)
        subExtrudes = newComp.features.extrudeFeatures
        subtractBodyProf = subtractBodySketch.profiles[0]
        subtractBodyExtInput = subExtrudes.createInput(subtractBodyProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        subtractBodyExtInput.setAllExtent(adsk.fusion.ExtentDirections.NegativeExtentDirection)
        distance = adsk.core.ValueInput.createByReal(BodyLength-( BodyDiameter * ThreadEngage ))
        subtractBodyExtInput.setDistanceExtent(False, distance)
        subtractBodyExt = subExtrudes.add(subtractBodyExtInput)

        # create the head clearance
        subtractHeadSketch = sketches.add(xyPlane)
        subtractHeadSketch.sketchCurves.sketchCircles.addByCenterRadius(center, self.headClearance/2)
        subtractHeadProf = subtractHeadSketch.profiles[0]
        subtractHeadExtInput = subExtrudes.createInput(subtractHeadProf, adsk.fusion.FeatureOperations.JoinFeatureOperation)
        subtractHeadExtInput.setAllExtent(adsk.fusion.ExtentDirections.NegativeExtentDirection)
        distance = adsk.core.ValueInput.createByReal(ClearanceLength-BodyLength+( BodyDiameter * -1.5 ))
        subtractHeadExtInput.setDistanceExtent(False, distance)        
        subtractHeadExt = subExtrudes.add(subtractHeadExtInput)
If I roll back the timeline I get the two bodies. But when I go forward in the time line again for the second extrusion it goes back to only one body with all of the extrusions.
        

 

Brad Bylls
0 Likes
Accepted solutions (2)
1,875 Views
10 Replies
Replies (10)
Message 2 of 11

KrisKaplan
Autodesk
Autodesk

The second extrude is done with the JoinFeatureOperation, which means it will result in one body joined with the original. If you want to cut one body from another body and keep the original, then that sounds like you want to create a combine feature using the screw as the tool body, and with the option to 'keep tools'.

 

A suggestion is to figure out the worfklow you need to get the results that you want through the UI first, and only then try to translate those steps into a script.

 

Kris



Kris Kaplan
0 Likes
Message 3 of 11

brad.bylls
Collaborator
Collaborator
Kris,
You don't understand.
I am trying to create two bodies in one component.
The first body, the screw, is in the code above the code shown.
This code is for creating the second body that consists of two extrudes
joined together.
The problem is that the JoinFeatureOperation is joining these two
extrusions to the screw body leaving me with one body, not two.

Brad
Brad Bylls
0 Likes
Message 4 of 11

KrisKaplan
Autodesk
Autodesk

OK. I think I understand what you are describing. Again, working through this in the UI is a good first step. So within one component you have one body (the screw), and then you create another body (body clearance) with an extrude (NewBodyFeatureOperation), and then try to join another (head clearance) extrude (JoinFeatureOperation) and the result is that you join both bodies into one.

 

This is the expected behavior of of the boolean create feature operations. The boolean create operations applies to all bodies in the current component. So if that extrude-join will intersect with both the screw body and the body clearance, it will join them all into one body. If this is not what you want, then you should create a combine feature where you can select just the body clearance body as the target and the head clearance as the tool with a join operation and leave the screw body out of it.

 

Kris



Kris Kaplan
0 Likes
Message 5 of 11

brad.bylls
Collaborator
Collaborator
OK Kris.
Thanks. I will try to figure that out.

Brad
Brad Bylls
0 Likes
Message 6 of 11

brad.bylls
Collaborator
Collaborator

Hi Kris,

Trying to work with the combine feature like you suggested. Makes sense. I just can't figure it out. Looked at a lot of code in the forums, but can't seem to get any of it to work.

Code above this created the screw body and now I am trying to make the second body in the component.

Code below is the closest I got:

#create the body clearance
        subtractBodySketch = sketches.add(xyPlane)
        subtractBodySketch.sketchCurves.sketchCircles.addByCenterRadius(center, self.bodyClearance/2)
        subtractBodyProf = subtractBodySketch.profiles[0]
        subtractBodyExtInput = extrudes.createInput(subtractBodyProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        subtractBodyExtInput.setAllExtent(adsk.fusion.ExtentDirections.NegativeExtentDirection)
        distance = adsk.core.ValueInput.createByReal(BodyLength-( BodyDiameter * ThreadEngage ))
        subtractBodyExtInput.setDistanceExtent(False, distance)
        subtractBodyExt = extrudes.add(subtractBodyExtInput)

        # create the head clearance
        subtractHeadSketch = sketches.add(xyPlane)
        subtractHeadSketch.sketchCurves.sketchCircles.addByCenterRadius(center, self.headClearance/2)
        subtractHeadProf = subtractHeadSketch.profiles[0]
        subtractHeadExtInput = extrudes.createInput(subtractHeadProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        subtractHeadExtInput.setAllExtent(adsk.fusion.ExtentDirections.NegativeExtentDirection)
        distance = adsk.core.ValueInput.createByReal(ClearanceLength-BodyLength+( BodyDiameter * -1.5 ))
        subtractHeadExtInput.setDistanceExtent(False, distance)        
        subtractHeadExt = extrudes.add(subtractHeadExtInput)
        
        # create the tap drill hole
        subtractTapDrillSketch = sketches.add(xyPlane)
        subtractTapDrillSketch.sketchCurves.sketchCircles.addByCenterRadius(center, self.tapDrill/2)
        subtractTapDrillProf = subtractTapDrillSketch.profiles[0]
        subtractTapDrillExtInput = extrudes.createInput(subtractTapDrillProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        subtractTapDrillExtInput.setAllExtent(adsk.fusion.ExtentDirections.NegativeExtentDirection)
        distance = adsk.core.ValueInput.createByReal(BodyLength + BodyDiameter)
        subtractTapDrillExtInput.setDistanceExtent(False, distance)        
        subtractTapDrillExt = extrudes.add(subtractTapDrillExtInput)

        # create tap drill point
        drillPointSketch = sketches.add(xyPlane)
        center1 = adsk.core.Point3D.create(00, (BodyLength + BodyDiameter))
        distance = adsk.core.ValueInput.createByReal(BodyLength)
        distance1 = adsk.fusion.DistanceExtentDefinition.create(distance)
        deg = adsk.core.ValueInput.createByString('-57 deg')
        drillPointSketch.sketchCurves.sketchCircles.addByCenterRadius(center1, self.tapDrill/2)
        drillPointExtProf = drillPointSketch.profiles[0]
        drillPointExtInput = extrudes.createInput(drillPointExtProf, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        drillPointExtInput.setOneSideExtent(distance1, adsk.fusion.ExtentDirections.PositiveExtentDirection, deg)
        drillPointExt = extrudes.add(drillPointExtInput)

        # create subtract body with combine
        subtractBody = subtractHeadExtInput
        bodyCollection = adsk.core.ObjectCollection.create()
        bodyCollection.add(subtractHeadExtInput)
        bodyCollection.add(subtractBodyExtInput)
        bodyCollection.add(subtractTapDrillExtInput)
        bodyCollection.add(drillPointExtInput)
        model = design.activeComponent
        features = model.features
        combineFeatures = features.combineFeatures
        combineFeatureInput = combineFeatures.createInput(subtractBody, bodyCollection)
        subtractBody = combineFeatures.add(combineFeatureInput)
When I hit the line in red, I get the following error.

 

Error.png

I don't understand it at all.

Any help appreciated.

Thank you

Brad

Brad Bylls
0 Likes
Message 7 of 11

KrisKaplan
Autodesk
Autodesk
Accepted solution

Brad,

 

What that error is trying to say is that CombineFeatures.createInput is expecting the targetBody argument to be of type BRepBody but it was not. The same would be true for the items in the ObjectCollection for the toolBodies argument. It looks like you are passing in ExtrudeFeatureInputs for all of these body arguments, and that won't work. You want to be passing in the result bodies from these extrude features. I would expect you want something like:

subtractBody = subtractHeadExt.bodies[0]
...

 

Kris



Kris Kaplan
0 Likes
Message 8 of 11

brad.bylls
Collaborator
Collaborator

Thanks Kris. Worked like a charm.

Brad Bylls
0 Likes
Message 9 of 11

brad.bylls
Collaborator
Collaborator

How do I mark it as 'Accepted' ?

Tried several different browsers and nothing.

Brad Bylls
0 Likes
Message 10 of 11

KrisKaplan
Autodesk
Autodesk
Accepted solution

I'm not sure what could be preventing you. You should be able to accept any post replying to your original as the solution. I marked my last reply as a solution for you. But if you see that again, raise an issue with a forum admin (or let me know and I will).

 

Kris



Kris Kaplan
0 Likes
Message 11 of 11

brad.bylls
Collaborator
Collaborator
Thanks Kris.
I'll do that.
Brad Bylls
0 Likes