why the wrong size?

why the wrong size?

keshka_007
Explorer Explorer
347 Views
1 Reply
Message 1 of 2

why the wrong size?

keshka_007
Explorer
Explorer

am using Grok and Copilot to generate scrips. However, everything scales wrong. Something simple like a 25mm cube with a 15mm pocket 10mm deep and I get a 250mm cube. Here is a sample script: 

import adsk.core, adsk.fusion, traceback

def createCubeWithPocket():
    app = adsk.core.Application.get()
    ui = app.userInterface
    design = app.activeProduct

    # Ensure units are set to millimeters
    unitsMgr = design.unitsManager
    unitsMgr.defaultLengthUnits = "mm"

    # Create a new component
    rootComp = design.rootComponent
    allOccs = rootComp.occurrences
    newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create())
    newComp = newOcc.component

    # Create a new sketch on the XY plane
    sketches = newComp.sketches
    xyPlane = newComp.xYConstructionPlane
    sketch = sketches.add(xyPlane)
    
    # Draw the base square
    lines = sketch.sketchCurves.sketchLines
    lines.addTwoPointRectangle(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(2.5, 2.5, 0))

    # Extrude the base square to create a cube
    prof = sketch.profiles.item(0)
    extrudes = newComp.features.extrudeFeatures
    extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
    distance = adsk.core.ValueInput.createByReal(2.5)  # Scale by 0.1
    extInput.setDistanceExtent(False, distance)
    extrude = extrudes.add(extInput)

    # Create a new sketch on the top face of the cube
    face = extrude.endFaces.item(0)
    sketch = sketches.add(face)

    # Draw the pocket square
    lines = sketch.sketchCurves.sketchLines
    lines.addTwoPointRectangle(adsk.core.Point3D.create(0.75, 0.75, 0), adsk.core.Point3D.create(1.75, 1.75, 0))

    # Extrude the pocket square to create the pocket
    prof = sketch.profiles.item(0)
    extInput = extrudes.createInput(prof, adsk.fusion.FeatureOperations.CutFeatureOperation)
    distance = adsk.core.ValueInput.createByReal(1.0)  # Scale by 0.1
    extInput.setDistanceExtent(False, distance)
    extrudes.add(extInput)

    # Save the document
    doc = app.activeDocument
    doc.saveAs('CubeWithPocket', 'Local', 'Fusion 360 Archive Files (*.f3d)', '')

try:
    createCubeWithPocket()
except:
    if ui:
        ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Accepted solutions (1)
348 Views
1 Reply
Reply (1)
Message 2 of 2

BrianEkins
Mentor
Mentor
Accepted solution

Length units in the API area ALWAYS centimeters. Look at the topic in the User Manual on units for more details. 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes