- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hey everyone!
First time poster, and I'm new to this API stuff, and I'm new to coding for that matter. So sorry if I'm missing something obvious, or use a word incorrectly.
My end-goal with this script is to automate the production of generative artwork by creating 2D artwork in an existing sketch, extruding it onto an existing model, and then regenerating and posting GCODE from the manufacturing environment. But I haven't gotten that far yet. Right now I am just trying to extrude a collection of lines.
The problem I am running into is that I have only figured out how to extrude closed profiles, rather than open curves. I am able to do it manually in the normal Fusion modeling environment, but the Thin Extrude method doesn't seem to accept open curves. Is there a way to make it work with open lines like you can in the modeling environment? or does the API not allow that?
I've attached two pictures, one show the results of my script, and the other one shows the desired outcome, which I was able to manually extrude in the modeling environment.
import adsk.core, adsk.fusion, adsk.cam, traceback, random
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
#Pattern Definitions
PatternWidth = 10 #in cm
PatternHeight = 10 #in cm
Instances = 10 #unitless
Weight = .7 #unitless
ExtrudeThickness = "1" #in cm UOS
ExtrudeDepth = "1" #in cm UOS
#Prompt User to select a sketch
Sketch=getSelectedSketchName()
#Draw Lines in pattern
for X in frange(-PatternWidth/2, PatternWidth/2, PatternWidth/Instances):
for Y in frange(-PatternHeight/2, PatternHeight/2, PatternHeight/Instances):
RandomLine(Sketch, X, Y,PatternWidth/Instances,Weight)
#Extrude Sketch
ThinExtrude(Sketch, ExtrudeThickness, ExtrudeDepth)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Get the selected sketch name; otherwise an empty string
def getSelectedSketchName():
#setup stuff
app = adsk.core.Application.get()
ui = app.userInterface
#Prompt to select sketch
Selection = ui.selectEntity("Select Sketch", "Sketches")
return Selection.entity
#Draws a line from a designated start point, to a designated end point in a given sketch
def DrawLine(mySketch,Start,End):
#start and end should be in the form (x, y).
LineStart = adsk.core.Point3D.create(Start[0],Start[1],Start[2])
LineEnd = adsk.core.Point3D.create(End[0],End[1],End[2])
# Get the SketchLines collection from an existing sketch.
lines = mySketch.sketchCurves.sketchLines
# Call an add method on the collection to create a new line.
NewLine = lines.addByTwoPoints(LineStart,LineEnd)
return NewLine
#Creates a diagnol line randomly between the specfied X and Y coordinated.
def RandomLine(Sketch,X,Y,Step,Weight):
X=float(X)
Y=float(Y)
Step=float(Step)
if (random.uniform(0,1))<Weight:
DrawLine(Sketch,(X,Y,0),(X+Step,Y+Step,0))
else:
DrawLine(Sketch,(X+Step,Y,0),(X,Y+Step,0))
return
#Creates a list of float numbers, just like the range comand, but flotats. Float range, Frange, get it?
def frange(A, L=None, D=None):
#Use float number in range() function
# if L and D argument is null set A=0.0 and D = 1.0
if L == None:
L = A + 0.0
A = 0.0
if D == None:
D = 1.0
while True:
if D > 0 and A >= L:
break
elif D < 0 and A <= L:
break
yield ("%g" % A) # return float number
A = A + D
#Do a thin extrude on everything in a sketch
def ThinExtrude(Sketch, Distance, Thickness):
app = adsk.core.Application.get()
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent
#set all inputs needed for extrude
extrudeFeatures = rootComp.features.extrudeFeatures
operation = adsk.fusion.FeatureOperations.NewBodyFeatureOperation
wallLocation = adsk.fusion.ThinExtrudeWallLocation.Center
wallThickness = adsk.core.ValueInput.createByString(Thickness)
distance = adsk.core.ValueInput.createByString(Distance)
isFullLength = True
NumberOfProfiles = Sketch.sketchCurves.count
#extrude all profiles in sketch
for I in range(0,NumberOfProfiles,1):
Profile = Sketch.profiles.item(I)
input = extrudeFeatures.createInput(Profile, operation)
input.setSymmetricExtent(distance, isFullLength)
input.setThinExtrude(wallLocation, wallThickness)
extrudeFeature = extrudeFeatures.add(input)
return extrudeFeature
Solved! Go to Solution.