Need API to create multi block

Need API to create multi block

nubrandao
Collaborator Collaborator
390 Views
2 Replies
Message 1 of 3

Need API to create multi block

nubrandao
Collaborator
Collaborator

Hi, tried to use chatgpt to create api to create a box bY inputing Box X, Box Y and Box Z.

 

It work the input but nothing happens.

 

Anyway, Im wondering if someone any API to create One or Multiple box.

 

My need is because i work with 8 to 10 diferent mould inserts (body)

 

And each body has his own size stock.

I usually have to Open a new Window, draw a box with required size and then copy to no project One by One.

 

For now, i have a box in project with defined parAmeters, so i ONLY Change the size and then copy One by One.

 

If anyone hás a better ideia, please share

0 Likes
391 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

Hi @nubrandao -San.

 

We created a sample that creates a block when you enter a size in the input box.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        des: fusion.Design = app.activeProduct
        root: fusion.Component = des.rootComponent

        inputTxt, res = ui.inputBox(
            "Please enter 'x,y,z' for the block size\ne.g.) 10,20,30",
            "create block sample",
            "10,20,30",
        )
        if res: return

        try:
            xyz = [float(v) for v in inputTxt.split(",")]
        except:
            ui.messageBox(
                "Input value is invalid.",
                "Error")
            return

        unitsMgr: core.UnitsManager = des.unitsManager
        ratio = unitsMgr.convert(
            1,
            unitsMgr.defaultLengthUnits,
            unitsMgr.internalUnits,
        )

        tmpMgr: fusion.TemporaryBRepManager = fusion.TemporaryBRepManager.get()
        tmpBox: fusion.BRepBody = tmpMgr.createBox(
            core.OrientedBoundingBox3D.create(
                core.Point3D.create(0,0,0),
                core.Vector3D.create(0,1,0),
                core.Vector3D.create(1,0,0),
                xyz[1] * ratio,
                xyz[0] * ratio,
                xyz[2] * ratio
            )
        )

        baseFeat: fusion.BaseFeature = None
        if des.designType == fusion.DesignTypes.ParametricDesignType:
            baseFeat = root.features.baseFeatures.add()

        bodies: fusion.BRepBodies = root.bRepBodies
        if baseFeat:
            try:
                baseFeat.startEdit()
                bodies.add(tmpBox, baseFeat)
            except:
                pass
            finally:
                baseFeat.finishEdit()
        else:
            bodies.add(tmpBox)

        ui.messageBox('Done')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Message 3 of 3

nubrandao
Collaborator
Collaborator

That is perfect, awesome . You have been helping a lot.