- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
When using the UI, I'm able to do the following:
- Create new component
- Create a sketch for the component
- Create a rectangle on the sketch
- Add dimension constraints on the rectangle
- Exit the sketch
- Extrude
- Select the top face
- Create a new sketch on the top face
- Add points to the new sketch
- Add dimension constraints between the points on the new sketch and the rectangle created earlier such that the points are a specific distance from each corner.
- Exit the sketch
- Make sketches visible
- Create holes for each point
When trying to do this using the API, Fusion 360 crashes as in it dumps (see error report 318009543, if you're working at Autodesk).
Here's the code:
# Author-
# Description-
import math
import adsk.core, adsk.fusion, adsk.cam, traceback
def r(x, n):
return math.ceil(x / n) * n
def get_top_face(component):
body: adsk.fusion.BRepBody = component.bRepBodies[0]
faces: adsk.fusion.BRepFaces = body.faces
return max(faces, key=(lambda f: f.centroid.z))
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
tray_doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
tray_design = tray_doc.products[0]
root = tray_design.rootComponent
units_mgr = tray_design.unitsManager
units_mgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits
lib = app.materialLibraries.itemByName('Fusion 360 Material Library')
pcb = lib.materials.itemByName('FR4')
steel = lib.materials.itemByName('Steel')
horizontal = adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation
vertical = adsk.fusion.DimensionOrientations.VerticalDimensionOrientation
s = 2.5
x = 7.0
y = 5.0
c = (r(0.8 + 2, 2)) / 10
w = c * 2 + r(x, s)
d = c * 2 + r(y, s)
parameters = tray_design.userParameters
parameters.add('Space', adsk.core.ValueInput.createByReal(s), 'mm', '')
parameters.add('HoleDiameter', adsk.core.ValueInput.createByReal(0.42), 'mm', '')
parameters.add('Width', adsk.core.ValueInput.createByReal(x), 'mm', '')
parameters.add('Depth', adsk.core.ValueInput.createByReal(y), 'mm', '')
parameters.add('Height', adsk.core.ValueInput.createByReal(0.2), 'mm', '')
parameters.add('HoleClearance', adsk.core.ValueInput.createByReal(c), 'mm', '')
parameters.add('MountingWidth', adsk.core.ValueInput.createByString("ceil(Width/Space)*Space"), 'mm', '')
parameters.add('MountingDepth', adsk.core.ValueInput.createByString("ceil(Depth/Space)*Space"), 'mm', '')
component = root.occurrences.addNewComponent(adsk.core.Matrix3D.create()).component
component.name = "Tray"
component.material = steel
sketch = component.sketches.add(component.xYConstructionPlane)
sketch.name = "Tray"
lines = sketch.sketchCurves.sketchLines
rectangle = lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(w, d, 0))
# Add Constraints
sketch.geometricConstraints.addHorizontal(rectangle.item(0))
sketch.geometricConstraints.addHorizontal(rectangle.item(2))
sketch.geometricConstraints.addVertical(rectangle.item(1))
sketch.geometricConstraints.addVertical(rectangle.item(3))
dim = sketch.sketchDimensions.addDistanceDimension(rectangle.item(0).startSketchPoint,
rectangle.item(0).endSketchPoint,
horizontal,
adsk.core.Point3D.create(w / 2, -1))
dim.parameter.expression = "MountingWidth + HoleClearance*2"
dim = sketch.sketchDimensions.addDistanceDimension(rectangle.item(3).startSketchPoint,
rectangle.item(3).endSketchPoint,
vertical,
adsk.core.Point3D.create(-1, d / 2))
dim.parameter.expression = "MountingDepth + HoleClearance*2"
extrudes = component.features.extrudeFeatures
prof = sketch.profiles.item(0)
distance = adsk.core.ValueInput.createByString("Height")
extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
# Create Sketch for Holes
face = get_top_face(component)
sketch = component.sketches.addWithoutEdges(face)
sketch.name = "Mounting"
points = adsk.core.ObjectCollection.create()
point1 = sketch.sketchPoints.add(adsk.core.Point3D.create(c, c, 0))
point2 = sketch.sketchPoints.add(adsk.core.Point3D.create(c, d - c, 0))
point3 = sketch.sketchPoints.add(adsk.core.Point3D.create(w - c, c, 0))
point4 = sketch.sketchPoints.add(adsk.core.Point3D.create(w - c, d - c, 0))
points.add(point1)
points.add(point2)
points.add(point3)
points.add(point4)
dim = sketch.sketchDimensions.addDistanceDimension(rectangle.item(0).startSketchPoint,
point1,
horizontal,
adsk.core.Point3D.create(0.5, -0.5))
dim.parameter.expression = "HoleClearance"
dim = sketch.sketchDimensions.addDistanceDimension(rectangle.item(1).startSketchPoint,
point1,
vertical,
adsk.core.Point3D.create(-0.5, 0.5))
dim.parameter.expression = "HoleClearance"
dim = sketch.sketchDimensions.addDistanceDimension(rectangle.item(2).startSketchPoint,
point4,
vertical,
adsk.core.Point3D.create(w + 0.5, d - 0.5))
dim.parameter.expression = "HoleClearance"
dim = sketch.sketchDimensions.addDistanceDimension(rectangle.item(1).startSketchPoint,
point4,
horizontal,
adsk.core.Point3D.create(w - 0.5, d + 0.5))
dim.parameter.expression = "HoleClearance"
dim = sketch.sketchDimensions.addDistanceDimension(point2,
point1,
vertical,
adsk.core.Point3D.create(-0.5, d / 2))
dim.parameter.expression = "MountingDepth"
dim = sketch.sketchDimensions.addDistanceDimension(point3,
point1,
horizontal,
adsk.core.Point3D.create(w / 2, -0.5))
dim.parameter.expression = "MountingWidth"
dim = sketch.sketchDimensions.addDistanceDimension(point3,
point4,
vertical,
adsk.core.Point3D.create(w + 0.5, d / 2))
dim.parameter.expression = "MountingDepth"
dim = sketch.sketchDimensions.addDistanceDimension(point4,
point2,
horizontal,
adsk.core.Point3D.create(w / 2, d + 0.5))
dim.parameter.expression = "MountingWidth"
hole_size = adsk.core.ValueInput.createByString("HoleDiameter")
holes = component.features.holeFeatures
hole = holes.createSimpleInput(hole_size)
hole.setPositionBySketchPoints(points)
hole.setAllExtent(adsk.fusion.ExtentDirections.PositiveExtentDirection)
hole = holes.add(hole)
hole.name = "Mounting"
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Software Engineer
https://wernerstrydom.com
https://wernerstrydom.com
Solved! Go to Solution.