My first script - Doesn't seem to work??

My first script - Doesn't seem to work??

HarrisonClassic
Advocate Advocate
301 Views
2 Replies
Message 1 of 3

My first script - Doesn't seem to work??

HarrisonClassic
Advocate
Advocate

Hi all,

 

Trying to create a simple cube via the following script, buyt all it seems to do is create a line in the sketch that is 1000mm long!

 

What am I missing?

 

___________________

 

import adsk.core
import adsk.fusion
import traceback

def create_cube():
app = adsk.core.Application.get()
design = app.activeProduct
root = design.rootComponent

# Define cube dimensions
width = 100 # mm
length = 200 # mm
depth = 25 # mm

# Create a new sketch on the XY plane
sketches = root.sketches
xy_plane = root.xYConstructionPlane
sketch = sketches.add(xy_plane)

# Create a rectangle in the sketch
sketch_lines = sketch.sketchCurves.sketchLines
point1 = adsk.core.Point3D.create(0, 0, 0)
point2 = adsk.core.Point3D.create(width, 0, 0)
point3 = adsk.core.Point3D.create(width, length, 0)
point4 = adsk.core.Point3D.create(0, length, 0)

lines = sketch_lines.addByTwoPoints(point1, point2)
lines.addByTwoPoints(point2, point3)
lines.addByTwoPoints(point3, point4)
lines.addByTwoPoints(point4, point1)

# Create an extrusion feature from the sketch
extrudes = root.features.extrudeFeatures
profile = sketch.profiles.item(0)
extrude_input = extrudes.createInput(profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)

# Set the extrusion distance
extrude_input.setDistanceExtent(False, adsk.core.ValueInput.createByReal(depth))

extrude = extrudes.add(extrude_input)

try:
create_cube()
except Exception as e:
print('Failed: {}'.format(traceback.format_exc()))

 

 

David Harrison
Harrison Classic Boats

Win 10 / I7-11700K @ 4.9GHz / 64Gb RAM / SSD's
0 Likes
302 Views
2 Replies
Replies (2)
Message 2 of 3

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

The line you are creating is 100 cm long (1000 mm), not mm as it is commented out.

I'd suggest reading this article regarding units in Fusion 360: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-A81B295F-984A-4039-B1CF-B449BEE893D7

 

As per the other lines of the cube base, you have to create them the same way you create the first, it is, with the sketch_lines.addByTwoPoints() method.

 

Regards,

Jorge Jaramillo

Software Engineer

 

Message 3 of 3

HarrisonClassic
Advocate
Advocate

thanks - I forgot about cm being the base unit for Metric.

 

Let me sort - thanks again

David Harrison
Harrison Classic Boats

Win 10 / I7-11700K @ 4.9GHz / 64Gb RAM / SSD's
0 Likes