How to bild a second sketch auf die random face of first entity and do cut in this entity?

How to bild a second sketch auf die random face of first entity and do cut in this entity?

AkusoWang
Participant Participant
802 Views
5 Replies
Message 1 of 6

How to bild a second sketch auf die random face of first entity and do cut in this entity?

AkusoWang
Participant
Participant

Hallo,teatchers and friends, I am the newbie to learn python and Fusion 360 API. I want to achive some functions. First create a entity (Cylinder or cuboid, in my present example is cuboid now). I need to make an effective digging on the surface of the first body. I have tired to use the advice from the chatGPT and Copilot, but there are always an error. I cant find the similar solution.  I hopt that friends who will know to solve this problem.

I have 3 ideas to achieve this.

1: The first is to directly create a sketch by taking the surface of the first body.

2: The second is to create a sketch first, randomly project it to a certain surface of the entity, and cut it.
3: The third is to obtain the data set of the point position on the surface of the entity, then randomly select a point, create a rectangular or circular sketch, and perform entity cutting.
May I ask which method is feasible? The following is a part of my code of the first way.I want this method to work with entities of different shapes. Thank you! 

 

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(10, 20)
z0 = random.uniform(10, 20)
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, -z0, 0))

prof = sketch.profiles.item(0)
extrudes = rootComp.features.extrudeFeatures
distance = adsk.core.ValueInput.createByReal(L)
extrudeInput = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
body_1 =extrudeInput.bodies.item(0
 
faces = body_1.faces 
faceCollection = adsk.core.ObjectCollection.create() 

selectedFace = faces.item(random.randint(0, faces.count-1))
faceCollection.add(selectedFace)
planeInput = rootComp.constructionPlanes.createInput()
 
planeInput.setByOffset(selectedFace, 0)
 
newPlane = rootComp.constructionPlanes.add(planeInput)
sketch2 = rootComp.sketches.add(rootComp.xZConstructionPlane)
 
lines = sketch2.sketchCurves.sketchLines
recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(x0, y0, 0))
prof = sketch2.profiles.item(0)
 
sketch2.project(selectedFace)
extrudes = rootComp.features.extrudeFeatures
distance = adsk.core.ValueInput.createByReal(L)
 
if random.randint(0,1) == 0:
extrudeInput = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
else:
extrudeInput = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.CutFeatureOperation)
extrudes.add(extrudeInput
0 Likes
Accepted solutions (1)
803 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor

Hi @AkusoWang .

 

I think what you want is probably possible to create.
However, I cannot visualize what you want.

 

Is it possible for you to provide me with a manually created f3d file of what you would like to create?

0 Likes
Message 3 of 6

AkusoWang
Participant
Participant

Hello, thanks for your answer, my task is actually a randomly generated milling part, but it is still in the basic stage. First step is creatting a entity (cuboid or cylinder), the second step is to randomly make holes on the surface  or stretch a new small body of the first solid 20 times. These sketches must not overlap. Those holes  must be effective in actual cutting.

0 Likes
Message 4 of 6

kandennti
Mentor
Mentor

@AkusoWang .

 

After all, words alone are not enough to visualize the image.
We need to be provided with f3d files to work on it.


Just in case, here is the first script modified to be error-free.

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(10, 20)
z0 = random.uniform(10, 20)
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, -z0, 0)
)

prof = sketch.profiles.item(0)
extrudes = rootComp.features.extrudeFeatures
distance = adsk.core.ValueInput.createByReal(L)
extrudeInput = extrudes.addSimple(
    prof, 
    distance, 
    adsk.fusion.FeatureOperations.NewBodyFeatureOperation
)
body_1 =extrudeInput.bodies.item(0) 

faces = body_1.faces 
faceCollection = adsk.core.ObjectCollection.create() 

selectedFace = faces.item(random.randint(0, faces.count-1))
faceCollection.add(selectedFace)
planeInput = rootComp.constructionPlanes.createInput()
 
planeInput.setByOffset(
    selectedFace,
    # 0
    adsk.core.ValueInput.createByReal(0)
)
 
newPlane = rootComp.constructionPlanes.add(planeInput)
sketch2 = rootComp.sketches.add(rootComp.xZConstructionPlane)
 
lines = sketch2.sketchCurves.sketchLines
recLines = lines.addTwoPointRectangle(
    adsk.core.Point3D.create(0, 0, 0),
    adsk.core.Point3D.create(x0, y0, 0)
)
prof: adsk.fusion.Profile = sketch2.profiles.item(0)

# sketch2.project(selectedFace)
extrudes = rootComp.features.extrudeFeatures
distance = adsk.core.ValueInput.createByReal(L)

if random.randint(0,1) == 0:
    extrudeInput = extrudes.addSimple(
        prof,
        distance,
        adsk.fusion.FeatureOperations.NewBodyFeatureOperation
    )
else:
    extrudeInput = extrudes.addSimple(
        prof,
        distance,
        adsk.fusion.FeatureOperations.CutFeatureOperation
    )
    extrudeInput = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.CutFeatureOperation)
# extrudes.add(extrudeInput)
Message 5 of 6

AkusoWang
Participant
Participant

Thank you for your help.

Btw. small changing. ( 45: sketch2 = rootComp.sketches.add(newPlane).
This first step is finised. but i have still some problems. If i set the "for i in range 20: " loop

1. It happed sometimes, that the new selected plan inside of body and that selected plan is from concave surface of cutting at body. (line 39: why did the code everytime get the new surface of body1 after one loop, not the original face of body1)

2. I dont want to create the new sketch cover the old sketch in the same place of the same surface.

3. There are alway "RuntimeError: 5 : No target entities were found to cut or intersect! "

Thank you.

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(random.randint(0, faces.count-1)) 
for i in range(20):
        x1 = random.uniform(-x0, x0)
        y1 = random.uniform(-y0, y0)
        planeInput = rootComp.constructionPlanes.createInput() 
        planeInput.setByOffset(selectedFace, 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)
        selectedFace = faces.item(random.randint(0, faces.count-1))

 

 

king2012hl_0-1680287866050.png

 

0 Likes
Message 6 of 6

kandennti
Mentor
Mentor
Accepted solution

@AkusoWang .

 

Thanks for the sample.

 

Here is the question.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/how-to-bild-a-second-sketch-auf-die-random... 

>1: The first is to directly create a sketch by taking the surface of the first body.
To create a sketch without including the edges of the surface, use the Sketches.addWithoutEdges method.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-1DC465D6-BC5B-4424-82FC-64CE59675C92 

 

The question here.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/how-to-bild-a-second-sketch-auf-die-random... 

>2. I dont want to create a new sketch covering the old sketch in the same place of the same surface.
This is difficult.
I have a few ideas, but I don't know if I can handle it correctly and it will take time to develop.

 

>3. There is alway "RuntimeError: 5 : No target entities were found to cut or intersect!"

You are probably right on the error message.
Create a new document in the GUI and create a profile in the sketch.
Then run the extrude command and set the operation to "Cut" and you will get the same error.
This error occurs when there is nothing to cut at all.

1.png

 

Since it is difficult to create a sketch that avoids duplicates, we have created a sample that randomly selects a face and creates a sketch point within the face.

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)  

# get original faces
faces = [f for f in body_1.faces]

vp: adsk.core.Viewport = app.activeViewport
vp.fit()
for _ in range(20):
    selectedFace: adsk.fusion.BRepFace = faces[random.randint(0, len(faces)-1)]

    # create sketch-WithoutEdges
    sketch2: adsk.fusion.Sketch = rootComp.sketches.addWithoutEdges(selectedFace)

    # get sketch area
    skt2Points: adsk.fusion.SketchPoints = sketch2.sketchPoints
    bBox: adsk.core.BoundingBox3D = selectedFace.boundingBox
    minPnt = sketch2.modelToSketchSpace(bBox.minPoint)
    maxPnt = sketch2.modelToSketchSpace(bBox.maxPoint)

    # get random coordinate value
    x1 = random.uniform(minPnt.x, maxPnt.x)
    y1 = random.uniform(minPnt.y, maxPnt.y)

    # create sketch point
    sketch2.sketchPoints.add(
        adsk.core.Point3D.create(
            x1,
            y1,
            0,
        )
    )

    vp.refresh()
    adsk.doEvents()