- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Evening...
I'm working my way through different parts of the API reference trying to find what I need for a larger project and writing mini test scripts. In the following example I create a user-parameterized sketch, extrude it and make it a component and then create some copies of the component and the naming the different bits along the way.
If after the script runs I change either of the user-parameters, the drawing does not update. I expect that this is because I am setting the value of the radius and separation to be equal to the "value" of the respective user-parameters (i.e. a double) and its not storing a direct reference to the user-parameters, but I'm not sure how to do this. If someone could let me know know to keep the sketches referenced to the user-parameters it would be appreciated. Also and general comments about coding best practices relative to my example are welcome too.
import adsk.core, adsk.fusion, adsk.cam, traceback
# Globals
_user_parameters = {}
def createNewComponent(rootComp):
allOccs = rootComp.occurrences
newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create())
return newOcc.component
def createParam(design, name, value, units, comment):
userValue = adsk.core.ValueInput.createByString(value)
design.userParameters.add(name, userValue, units, comment)
def run(context):
ui = None
try:
app = adsk.core.Application.get()
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent
#create user parameters
cm = design.unitsManager.defaultLengthUnits
createParam(design, 'radius', '5cm', cm, '')
createParam(design, 'separation', '10cm', cm, '')
#create a new component and name it
comp = createNewComponent(rootComp)
comp.name = "myComponent"
#create and name a circle sketch within the component
sketches = comp.sketches
sketch = sketches.add(rootComp.xZConstructionPlane)
sketch.name = "mySketch"
sketch.sketchCurves.sketchCircles.addByCenterRadius(adsk.core.Point3D.create(0,0,0), _user_parameters['radius'].value)
#create an extrusion of the circle within the component and name the body it produces
extrudes = comp.features.extrudeFeatures
prof = sketch.profiles[0]
extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance = adsk.core.ValueInput.createByReal(5)
extInput.setDistanceExtent(False, distance)
extrusion = extrudes.add(extInput)
body = extrusion.bodies.item(0)
body.name = "myBody"
#Create some copies (3x3)
allOccs = rootComp.occurrences
transform = adsk.core.Matrix3D.create()
separation = _user_parameters['separation'].value
for x in range(0,3):
for z in range (0, 3):
transform.translation = adsk.core.Vector3D.create(x * separation, 0, z * separation)
allOccs.addExistingComponent(comp, transform)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.
