API: Create a sketch on a surface of a body

API: Create a sketch on a surface of a body

Anonymous
Not applicable
2,295 Views
6 Replies
Message 1 of 7

API: Create a sketch on a surface of a body

Anonymous
Not applicable

Hi Everyone,

 

I am fairly new to programming Fusion 360 via python. I want to create a sketch on a surface of a body. From this sketch I want to extrude a geometry as a new body. The code is underneath.

I have two issues:

1) How do I know the index of the face I want to create the sketch on (body.faces.item(???))

2) I get following error when I run the program. Does anybody know what it means? It is connected to the "start_from"-object.

 

Thanks for any help

Sören

 

Bildschirmfoto 2018-06-12 um 23.22.10.png

 

import adsk.core, adsk.fusion, traceback

 

def run(context):

ui = None

try:

app = adsk.core.Application.get()

ui = app.userInterface

 

# Create a document.

doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

product = app.activeProduct

design = adsk.fusion.Design.cast(product)

 

# Get the root component of the active design

rootComp = design.rootComponent

 

# Create a new sketch on the xy plane.

sketches = rootComp.sketches;

xyPlane = rootComp.xYConstructionPlane

sketch = sketches.add(xyPlane)

 

# Draw a rectangle by two points.

dimRec=25

lines = sketch.sketchCurves.sketchLines;

recLines = lines.addCenterPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(dimRec/2, dimRec/2, 0))

prof =sketch.profiles.item(0)

 

# Draw a second rectangle by two points.

dimRec=0.2

lines1 = sketch.sketchCurves.sketchLines;

recLines1 = lines1.addCenterPointRectangle(adsk.core.Point3D.create(2, 2, 0), adsk.core.Point3D.create(dimRec/2, dimRec/2, 0))

profRec1=sketch.profiles.item(0)

 

# Create distance value inputs

mm10 = adsk.core.ValueInput.createByString("10 mm")

 

#Get extrude features

features = rootComp.features

extrudes = features.extrudeFeatures

extInput = extrudes.createInput(prof,adsk.fusion.FeatureOperations.NewComponentFeatureOperation)

extInput.setDistanceExtent(False, adsk.core.ValueInput.createByReal(0.8))

extFront = extrudes.add(extInput)

bodyFront=extFront.bodies.item(0)

bodyFront.name= "Frontplatte"

 

# Create an extrusion that starts from an entity and goes the specified distance.

extInput0 = extrudes.createInput(profRec1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

# Create a distance extent definition

extent_distance_2 = adsk.fusion.DistanceExtentDefinition.create(mm10)

# Create a start extent that starts from a brep face with an offset of 10 mm.

start_from = adsk.fusion.FromEntityStartDefinition.create(bodyFront.faces.item(0), mm10)

# taperAngle should be 0 because extrude start face is not a planar face in this case

extInput0.setOneSideExtent(extent_distance_2, adsk.fusion.ExtentDirections.PositiveExtentDirection)

extInput0.startExtent = start_from

# Create the extrusion

extrude3 = extrudes.add(extInput0)

body3 = extrude3.bodies.item(0)

body3.name = "Connector"

 

except:

if ui:

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

0 Likes
2,296 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Hi There, I see there are a couple of views and no answers: Can you give me a hint about If there is anything wrong with the post? Code too long, solution too obvious? Or am I too unpatient 🙂

 

Yours Sören

0 Likes
Message 3 of 7

kandennti
Mentor
Mentor

Hi soeren.bergmann

I am also studying, so I can not write code well. Therefore, I write only the idea.


The 'BRepFace' object has a 'tempId' property.
The 'BRepBody' object has a 'findByTempId' method, which allows you to find a surface from the ID.
However, I do not know the numbering rule of 'tempId'....

It may be necessary to search by geometry processing.

 

I think that it is good to make extrudeFeature by using ConstructionPlane.

0 Likes
Message 4 of 7

kandennti
Mentor
Mentor

After carefully examining the 'ExtrudeFeature' object, there was an 'endFaces' property.
opposite the start faces could be acquired with this.

 

・・・
        #start_from = adsk.fusion.FromEntityStartDefinition.create(bodyFront.faces.item(0), mm10)

        endsurf = extFront.endFaces
        start_from = adsk.fusion.FromEntityStartDefinition.create(endsurf.item(0), mm10)
・・・

1.png

 

 

I also learned a lot.

 

 

0 Likes
Message 5 of 7

Anonymous
Not applicable

Hi kandennti,

 

thanks that you looked into the problem. I replaced the start_from... line with your solutions. When I run the script I still get the error message I posted initially. Did you get the error-message with the original code?

 

Sören

0 Likes
Message 6 of 7

kandennti
Mentor
Mentor

I ran it with this code.
(Win 10 + Fusion 360ver 2.0 423)

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface
        
        # Create a document.
        doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        
        # Get the root component of the active design
        rootComp = design.rootComponent
        
        # Create a new sketch on the xy plane.
        sketches = rootComp.sketches;
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)
        
        # Draw a rectangle by two points.
        dimRec=25
        lines = sketch.sketchCurves.sketchLines;
        recLines = lines.addCenterPointRectangle(adsk.core.Point3D.create(0, 0, 0), 
                                                 adsk.core.Point3D.create(dimRec/2, dimRec/2, 0))
        prof =sketch.profiles.item(0)
        
        # Draw a second rectangle by two points.
        dimRec=0.2
        lines1 = sketch.sketchCurves.sketchLines;
        recLines1 = lines1.addCenterPointRectangle(adsk.core.Point3D.create(2, 2, 0), 
                                                   adsk.core.Point3D.create(dimRec/2, dimRec/2, 0))
        profRec1=sketch.profiles.item(0)
        
        # Create distance value inputs
        mm10 = adsk.core.ValueInput.createByString("10 mm")
        
        #Get extrude features
        features = rootComp.features
        extrudes = features.extrudeFeatures
        extInput = extrudes.createInput(prof,
                            adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        extInput.setDistanceExtent(False, adsk.core.ValueInput.createByReal(0.8))
        extFront = extrudes.add(extInput)
        
        bodyFront=extFront.bodies.item(0)
        bodyFront.name= "Frontplatte"
        
        # Create an extrusion that starts from an entity and goes the specified distance.
        extInput0 = extrudes.createInput(profRec1, 
                            adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
        
        # Create a distance extent definition
        extent_distance_2 = adsk.fusion.DistanceExtentDefinition.create(mm10)
        
        # Create a start extent that starts from a brep face with an offset of 10 mm.
        endsurf = extFront.endFaces
        start_from = adsk.fusion.FromEntityStartDefinition.create(endsurf.item(0), mm10)
        
        # taperAngle should be 0 because extrude start face is not a planar face in this case
        extInput0.setOneSideExtent(extent_distance_2, 
                            adsk.fusion.ExtentDirections.PositiveExtentDirection)
        extInput0.startExtent = start_from
        
        # Create the extrusion
        extrude3 = extrudes.add(extInput0)
        body3 = extrude3.bodies.item(0)
        body3.name = "Connector"
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Message 7 of 7

Anonymous
Not applicable

Hi,

 

OK, thats interesting. I didn't get a error message, that's good. I also got rid of the offset, now the thing is, that the extrude is the negative of the one I want to gain. Means, I want to have a small cube on front of the bigger plate. But I will look into that, thanks for your support, this got me much further. When I have time I will compare it more closely with my original code because I still not understand what caused the error message. And I still not exactly understand how I can access any surface I want, e.g. when I want to make a sketch on the thin sides of the plate I wouldn't know how to access it either.

 

Cheers!

 

Sören

0 Likes