Importing components into the current document using the Fusion 360 API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm struggling to link newly created components into a new document, and need some help.
I have a script that creates breadboards based on some parameters, all in one go. The same script also generates the components for those breadboards. Examples include:
- switching between metric and imperial measurements
- the distance between mounting holes
The components and breadboards are all distinct and manufactured independently, so the script I wrote saves them into their own files. I then create "configurations". One configuration may be where components a,b,c, and d need to be mounted, and another would be where a,b, e, f, and g need to be mounted. I can virtually inspect and arrange things and then proceed to get a quote, manufacture things for real-world testing or adjust the parameters, and rerun the script. Creating these configurations is, at best, time-consuming and error-prone when doing it manually.
I created a simplified script to reproduce the problem:
#Author-
#Description-
import time
import adsk.core, adsk.fusion, adsk.cam, traceback, math
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
project: adsk.core.DataProject = app.data.activeProject
folder: adsk.core.DataFolder = project.rootFolder
lib = app.materialLibraries.itemByName('Fusion 360 Material Library')
pcb = lib.materials.itemByName('FR4')
steel = lib.materials.itemByName('Steel')
#
# Component 1
#
width = 6
depth = 8
edge = 1
height = 0.166
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
name = 'Component 1'
design = doc.products[0]
units_mgr = design.unitsManager
units_mgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits
root = design.rootComponent
component = root.occurrences.addNewComponent(adsk.core.Matrix3D.create()).component
component.name = name
component.material = pcb
sketch = component.sketches.add(component.xYConstructionPlane)
sketch.name = component.name
lines = sketch.sketchCurves.sketchLines
p1 = adsk.core.Point3D.create(0, 0, 0)
p2 = adsk.core.Point3D.create(width, depth, 0)
rectangle = lines.addTwoPointRectangle(p1, p2)
sketch.geometricConstraints.addHorizontal(rectangle.item(0))
sketch.geometricConstraints.addHorizontal(rectangle.item(2))
sketch.geometricConstraints.addVertical(rectangle.item(1))
sketch.geometricConstraints.addVertical(rectangle.item(3))
sketch.sketchDimensions.addDistanceDimension(rectangle.item(0).startSketchPoint,
rectangle.item(0).endSketchPoint,
adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation,
adsk.core.Point3D.create(width / 2, -1))
sketch.sketchDimensions.addDistanceDimension(rectangle.item(3).startSketchPoint,
rectangle.item(3).endSketchPoint,
adsk.fusion.DimensionOrientations.VerticalDimensionOrientation,
adsk.core.Point3D.create(-1, depth / 2))
extrudes = component.features.extrudeFeatures
prof = sketch.profiles.item(0)
distance = adsk.core.ValueInput.createByString("1.6 mm")
extrude = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
body = extrude.bodies.item(0)
body.name = sketch.name
face = body.faces.item(4) # Top Face
sketch = component.sketches.addWithoutEdges(face)
sketch.name = 'Mounting'
offset_sketch_points = sketch.sketchPoints
point1 = offset_sketch_points.add(adsk.core.Point3D.create(edge, edge, 0))
point2 = offset_sketch_points.add(adsk.core.Point3D.create(edge, depth - edge, 0))
point3 = offset_sketch_points.add(adsk.core.Point3D.create(width - edge, edge, 0))
point4 = offset_sketch_points.add(adsk.core.Point3D.create(width - edge, depth - edge, 0))
points = adsk.core.ObjectCollection.create()
points.add(point1)
points.add(point2)
points.add(point3)
points.add(point4)
holes = component.features.holeFeatures
fastener_diameter = '3 mm'
hole = holes.createSimpleInput(adsk.core.ValueInput.createByString(fastener_diameter))
hole.setPositionBySketchPoints(points)
hole.setDistanceExtent(distance)
hole = holes.add(hole)
doc.saveAs(name, folder, '', '')
doc.close(False)
#
# Component 2
#
width = 30
depth = 64
space_x = 4
space_y = 6
edge = 2
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
name = 'Breadboard 1'
design = doc.products[0]
units_mgr = design.unitsManager
units_mgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits
root = design.rootComponent
component = root.occurrences.addNewComponent(adsk.core.Matrix3D.create()).component
component.name = name
component.material = steel
sketch = component.sketches.add(component.xYConstructionPlane)
sketch.name = component.name
lines = sketch.sketchCurves.sketchLines
p1 = adsk.core.Point3D.create(0, 0, 0)
p2 = adsk.core.Point3D.create(width, depth, 0)
rectangle = lines.addTwoPointRectangle(p1, p2)
sketch.geometricConstraints.addHorizontal(rectangle.item(0))
sketch.geometricConstraints.addHorizontal(rectangle.item(2))
sketch.geometricConstraints.addVertical(rectangle.item(1))
sketch.geometricConstraints.addVertical(rectangle.item(3))
sketch.sketchDimensions.addDistanceDimension(rectangle.item(0).startSketchPoint,
rectangle.item(0).endSketchPoint,
adsk.fusion.DimensionOrientations.HorizontalDimensionOrientation,
adsk.core.Point3D.create(width / 2, -1))
sketch.sketchDimensions.addDistanceDimension(rectangle.item(3).startSketchPoint,
rectangle.item(3).endSketchPoint,
adsk.fusion.DimensionOrientations.VerticalDimensionOrientation,
adsk.core.Point3D.create(-1, depth / 2))
extrudes = component.features.extrudeFeatures
prof = sketch.profiles.item(0)
distance = adsk.core.ValueInput.createByString("1.66 mm")
extrude = extrudes.addSimple(prof, distance, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
body = extrude.bodies.item(0)
body.name = sketch.name
face = body.faces.item(4) # Top Face
sketch = component.sketches.addWithoutEdges(face)
sketch.name = 'Mounting'
points = adsk.core.ObjectCollection.create()
offset_sketch_points = sketch.sketchPoints
count_x = math.ceil((width - 2 * edge) / space_x)
padding_x = (width - (count_x - 1) * space_x)/2
count_y = math.ceil((depth - 2 * edge) / space_y)
padding_y = (depth - (count_y - 1) * space_y)/2
for w in range(0, count_x):
for d in range(0, count_y):
x = w * space_x + padding_x
y = d * space_y + padding_y
p = offset_sketch_points.add(adsk.core.Point3D.create(x, y, 0))
points.add(p)
holes = component.features.holeFeatures
fastener_diameter = '3 mm'
hole = holes.createSimpleInput(adsk.core.ValueInput.createByString(fastener_diameter))
hole.setPositionBySketchPoints(points)
hole.setDistanceExtent(distance)
hole = holes.add(hole)
doc.saveAs(name, folder, '', '')
doc.close(False)
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
design = doc.products[0]
units_mgr = design.unitsManager
units_mgr.distanceDisplayUnits = adsk.fusion.DistanceUnits.MillimeterDistanceUnits
root = design.rootComponent
doc.saveAs("Config 1", folder, '', '')
# TODO Import components
doc.save()
doc.close(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
At the TODO statement, I added the following
# TODO Import components
dataFiles = project.rootFolder.dataFiles
for file in dataFiles:
if file.name == 'Component 1' or file.name == 'Breadboard 1':
occs = root.occurrences
occs.addByInsert(file, adsk.core.Matrix3D.create(), True)
which gave me this error
I can't find a good example of how to insert a component from a file. I suspect that both "Breadboard 1" and "Component 1" may not be available to be inserted into "Config 1".
I found that I could just run three different scripts and wait in-between, but that isn't ideal when there are tens or hundreds of them. I'd rather run the script and spend time with our daughter, or attend to other important matters.
https://wernerstrydom.com