Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Round up angles script

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
nubrandao
189 Views, 8 Replies

Round up angles script

Hi, is possible to create an API script with a pop up message with a angle round?

Or could an excel sheet work?

 

Here's the situation.

In our company, he have 2 machines, that A e B body, have restricted angles

 

For example example, B body rotates 1 degree, example B: 85/86/87...

 

A body rotation is every 3 degrees 

Example A: A: 3/6/9/12../87

 

So, to calc for those machines, I have to round up the angle.

 

If I use align to view will go for example: turn 75.45678

Flip 34.56785

 

I have to round this numbers.

 

It would be perfect a script that create a named view renamed with the angle round to 0.0 degrees, then when align, I input the round numbers.

 

Would like to hear your suggestions 

 

8 REPLIES 8
Message 2 of 9
kandennti
in reply to: nubrandao

Hi @nubrandao -San.

 

It's just words, so I don't have a clear picture of what I'm looking for, but it seems like it could be done with Joint's functionality without having to create a script.

1.png

Message 3 of 9
nubrandao
in reply to: nubrandao

nubrandao_0-1728834542786.png

inside manufacture

Message 4 of 9
kandennti
in reply to: nubrandao

@nubrandao -San.

A sample script was created to round up the “Turn” and “Tilt” of the selected operation.

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.cam as cam
import math

_app: core.Application = None
_ui: core.UserInterface = None
_handlers = []

CMD_INFO = {
    "id": "kantoku_RoundUpAngles",
    "name": "RoundUp Angles",
    "tooltip": "RoundUp Angles"
}

_SelIpt: core.SelectionCommandInput = None
_infoIpt: core.TextBoxCommandInput = None
_camObj: cam.CAM = None

class MyCommandCreatedHandler(core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandCreatedEventArgs):
        try:
            app: core.Application = core.Application.get()

            global _camObj
            _camObj= app.activeProduct

            global _handlers
            cmd: core.Command = core.Command.cast(args.command)
            inputs: core.CommandInputs = cmd.commandInputs

            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onExecute = MyExecuteHandler()
            cmd.execute.add(onExecute)
            _handlers.append(onExecute)

            onInputChanged = MyInputChangedHandler()
            cmd.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)

            onPreSelect = MyPreSelectHandler()
            cmd.preSelect.add(onPreSelect)
            _handlers.append(onPreSelect)

            global _SelIpt
            _SelIpt = inputs.addSelectionInput(
                "_SelOpeId",
                "Operation",
                "Select Operation"
            )

            global _infoIpt
            _infoIpt = inputs.addTextBoxCommandInput(
                "_infoIptId",
                "",
                "-",
                2,
                True
            )

        except:
            _ui.messageBox("Failed:\n{}".format(traceback.format_exc()))


class MyPreSelectHandler(core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.SelectionEventArgs):
        ope: cam.Operation = cam.Operation.cast(args.selection.entity)

        if not ope:
            args.isSelectable = False
            return

        prm: cam.CAMParameter = ope.parameters.itemByName("overrideToolView")
        if not prm.value.value:
            args.isSelectable = False
            return


class MyInputChangedHandler(core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.InputChangedEventArgs):
        global _SelIpt, _infoIpt

        if _SelIpt.selectionCount < 1:
            _infoIpt.text = "-"
            return

        ope: cam.Operation = _SelIpt.selection(0).entity
        prmTurn: cam.CAMParameter = ope.parameters.itemByName("view_turn_from_recipe")
        prmTilt: cam.CAMParameter = ope.parameters.itemByName("view_tilt_from_recipe")

        valueTurn = prmTurn.value.value
        valueTilt = prmTilt.value.value

        roundTurn = roundUp(valueTurn, 0.1)
        roundTilt = roundUp(valueTilt, 0.1)

        _infoIpt.text = "\n".join(
            [
                f"Turn: {valueTurn} -> {roundTurn}",
                f"Tilt: {valueTilt} -> {roundTilt}",
            ]
        )


class MyExecuteHandler(core.CommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandEventArgs):
        global _SelIpt
        ope: cam.Operation = _SelIpt.selection(0).entity

        prmTurn: cam.CAMParameter = ope.parameters.itemByName("view_turn_from_recipe")
        prmTilt: cam.CAMParameter = ope.parameters.itemByName("view_tilt_from_recipe")

        valueTurn = prmTurn.value.value
        valueTilt = prmTilt.value.value

        roundTurn = roundUp(valueTurn, 0.1)
        roundTilt = roundUp(valueTilt, 0.1)

        prmTurn.value.value = float(roundTurn)
        prmTilt.value.value = float(roundTilt)

        global _camObj
        _camObj.generateToolpath(ope)


class MyCommandDestroyHandler(core.CommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandEventArgs):
        adsk.terminate()


def roundUp(v, roundUpUnit) -> str:
    roundValue = math.ceil(v / roundUpUnit) * roundUpUnit

    global _camObj
    unitsMgr = _camObj.unitsManager
    return unitsMgr.formatValue(roundValue, "", 1)


def run(context):
    try:
        global _app, _ui
        _app = core.Application.get()
        _ui = _app.userInterface

        cmdDef: core.CommandDefinition = _ui.commandDefinitions.itemById(
            CMD_INFO["id"]
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                CMD_INFO["id"],
                CMD_INFO["name"],
                CMD_INFO["tooltip"]
            )

        global _handlers
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmdDef.execute()

        adsk.autoTerminate(False)

    except:
        if _ui:
            _ui.messageBox("Failed:\n{}".format(traceback.format_exc()))

 

kandennti_0-1728889522138.png

 

Message 5 of 9
nubrandao
in reply to: nubrandao

I will try thank very much

Message 6 of 9
nubrandao
in reply to: nubrandao

can you modify to to have no decimal value? for example:      turn 87.3423 >  turn 87

flip 43.2544 >  flip 43

Message 7 of 9
nubrandao
in reply to: nubrandao

solved, i chanegd the values , perfect

Message 8 of 9
nubrandao
in reply to: nubrandao

@kandennti hey mate, that works. But I face a problem, I thought that turn and tilt were the same output angles from the machine.

 

For example, I turn 85degree and tilt 30 degree, the machine output is A 251.456 and B 243.8663

 

Those numbers are just a example.

 

In this case , I need the reversed angles

 

For example, in NC simulation  I have to find the round angle output,and the adjust turn and tilt to match the A and B body angles

Message 9 of 9
kandennti
in reply to: nubrandao

@nubrandao -San.

 

I am sorry, but I do not know the formula to obtain the A and B axes from Turn and Filp.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report