Why does my Script use So much Ram and run So Slow? Is it the Sweeps?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi Guys and Girls,
I hope this question is not to basic but I just can not work out why this script runs so slowly and uses so much ram.
The following script creates a single body and performs multiple sweeps in loops.
Disclammer: Its my first day scripting in fusion360 + I am new to python.
Why so much ram (~8 Gb for 100 - 200 sweeps) and why so slow? Is the script fine and this is just how it should be or is there something I could do to speed things up and more importantly increase the number of sweeps I can perform? I only have 16Gb of ram, and was hopeing to be able to do 1000-2000 sweeps in a single script.
kind regards,
Trent
import adsk.core, adsk.fusion, traceback
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
design = app.activeProduct
# 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 two connected lines.
lines = sketch.sketchCurves.sketchLines;
#Create our block
# Draw a rectangle by two points the extrude.
#Define size
halfXSize = 10 #cm
halfYSize = 10 #cm
zSize = 1 #cm
recLines = lines.addTwoPointRectangle(adsk.core.Point3D.create(-halfXSize, -halfYSize, 0), adsk.core.Point3D.create(halfXSize, halfYSize, 0))
# Get the profile defined by the rectangle
prof = sketch.profiles.item(0)
# Create an extrusion input
extrudes = rootComp.features.extrudeFeatures
extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distance = adsk.core.ValueInput.createByReal(zSize)
extInput.setDistanceExtent(False, distance)
# Create the extrusion
ext = extrudes.add(extInput)
#define shape of holes - circular for testing purposes
# Create sketch cuting holes
#define focalPoint
focalPoint = 20 #distance of focal point from slab = focalpoint - zSize (ie 19)
#Circle
#sketches = rootComp.sketches
#sketch = sketches.add(rootComp.xYConstructionPlane)
#sketchCircles = sketch.sketchCurves.sketchCircles
#centerPoint = adsk.core.Point3D.create(0, 0, focalPoint) # position of "focal point"
#circle = sketchCircles.addByCenterRadius(centerPoint, 0.25)
#prof2 = sketch.profiles.item(0)
#Polygon - no function for polygon in API....ok, from indivifdual lines
sketches = rootComp.sketches;
xyPlane = rootComp.xYConstructionPlane
sketch = sketches.add(xyPlane)
lines = sketch.sketchCurves.sketchLines;
line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, .25, focalPoint), adsk.core.Point3D.create(.2165, .125, focalPoint))
line2 = lines.addByTwoPoints(line1.endSketchPoint, adsk.core.Point3D.create(.2165, -.125, focalPoint))
line3 = lines.addByTwoPoints(line2.endSketchPoint, adsk.core.Point3D.create(0, -.25, focalPoint))
line4 = lines.addByTwoPoints(line3.endSketchPoint, adsk.core.Point3D.create(-.2165, -.125, focalPoint))
line5 = lines.addByTwoPoints(line4.endSketchPoint, adsk.core.Point3D.create(-.2165, .125, focalPoint))
line6 = lines.addByTwoPoints(line5.endSketchPoint, adsk.core.Point3D.create(0, .25, focalPoint))
prof2 = sketch.profiles.item(0)
#draw lines from focal point to other side of and cut hole along line
# Create a new sketch on the xy plane.
sketches = rootComp.sketches
xyPlane = rootComp.xYConstructionPlane
sketch = sketches.add(xyPlane)
#Draw two connected lines.
lines = sketch.sketchCurves.sketchLines
#factor to adjust the centre of lines from the focal point at the surface
holeSeparation = 0.8 #cm
#No.of holes each side of centre
xHoles = 2 #cm
yHoles = 3
for i in range(-xHoles,xHoles): #iterate nested loop across all hole positions
for j in range(-yHoles,yHoles):
line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, focalPoint), adsk.core.Point3D.create(i*holeSeparation, j*holeSeparation, 0)) #Note: add variable for focal point
path = rootComp.features.createPath(line1);
sweeps = rootComp.features.sweepFeatures;
sweepInput = sweeps.createInput(prof2, path, adsk.fusion.FeatureOperations.CutFeatureOperation);
sweep = sweeps.add(sweepInput)
for i in range(-xHoles,xHoles): #Seconf set of loops for when the holes are tessalating polygons
for j in range(-yHoles,yHoles):
line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, focalPoint), adsk.core.Point3D.create((i+0.5)*holeSeparation, (j+0.5)*holeSeparation, 0)) #Note: add variable for focal point
path = rootComp.features.createPath(line1);
sweeps = rootComp.features.sweepFeatures;
sweepInput = sweeps.createInput(prof2, path, adsk.fusion.FeatureOperations.CutFeatureOperation);
sweep = sweeps.add(sweepInput);
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))