After digging a hole in a bRep face of cuboid, cant use it again to dig another hole

After digging a hole in a bRep face of cuboid, cant use it again to dig another hole

AkusoWang
Participant Participant
433 Views
2 Replies
Message 1 of 3

After digging a hole in a bRep face of cuboid, cant use it again to dig another hole

AkusoWang
Participant
Participant

I want to create a randomly generated milling part, but it is still in the basic stage. First step is creatting a entity (cuboid), the second step is to randomly dig holes on the surface or stretch a new small body of the first solid 20 times

1. After digging a hole in a bRep face of cuboid, cant use it again to dig another hole

2. If cant get the same bRep face after digging a hole, how can i transfer bRepface to constructionPlane.

 

 

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

app = adsk.core.Application.get()   
ui = app.userInterface
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
design = adsk.fusion.Design.cast(app.activeProduct)
rootComp = design.rootComponent
x0 = random.uniform(10, 20)
y0 = random.uniform(20, 10)
L = random.uniform(10, 20)

rectanglePlan = random.randint(0,2)
sketch = rootComp.sketches.add(rootComp.xZConstructionPlane)
lines = sketch.sketchCurves.sketchLines
recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(x0, y0, 0))
prof = sketch.profiles.item(0)
extrudes = rootComp.features.extrudeFeatures
distance_1 = adsk.core.ValueInput.createByReal(L)
extrudeInput = extrudes.addSimple(prof, distance_1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
body_1 =extrudeInput.bodies.item(0)  

faces = body_1.faces
selectedFace=[faces.item(0),faces.item(1),faces.item(2),faces.item(3),faces.item(4),faces.item(5)]
for i in range(6):
        x1 = random.uniform(-x0, x0)
        y1 = random.uniform(-y0, y0)  
        
        planeInput = rootComp.constructionPlanes.createInput()
        planeInput.setByOffset(selectedFace[i-1], adsk.core.ValueInput.createByReal(0))
        newPlane = rootComp.constructionPlanes.add(planeInput)  
        sketch2 = rootComp.sketches.add(newPlane) 
        lines = sketch2.sketchCurves.sketchLines
        recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(x1, y1, 0), adsk.core.Point3D.create(x1/5, y1/5, 0))
        prof:adsk.fusion.Profile = sketch2.profiles.item(0)  

        extrudes = rootComp.features.extrudeFeatures
        distance_2 = adsk.core.ValueInput.createByReal(3)
        distance_Cut = adsk.core.ValueInput.createByReal(-3)
        extrudeInput = extrudes.addSimple(prof, distance_Cut, adsk.fusion.FeatureOperations.CutFeatureOperation)

 

 

 

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

BrianEkins
Mentor
Mentor
Accepted solution

When a body is modified, typically any references to that body or its children (BRepFace, BRepEdge, etc.) become invalid, and you need to get them again. The easiest way to do that is to get the entity's entity token before you make the change and then use the token to get the entity again after you've made the change. It would be something like this:

 

# Get the token from the entity you'll need later.
token = face.entityToken

# Make a change to the body.

# Get the face, using the token.
result = design.findEntityByToken(token)

# An array is returned. It will usually contain one entity but could be 
# none if the entity no longer exists or more than one if the entity was
# split into multiple pieces by the modeling change.
if len(result) > 0:
    newFace = result[0]
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 3

AkusoWang
Participant
Participant

Thank you!

0 Likes