API: Create a sketch on a surface of a body

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
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()))