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: 

Getting unit out of SheetMetalRule

6 REPLIES 6
Reply
Message 1 of 7
jesse
156 Views, 6 Replies

Getting unit out of SheetMetalRule

Is there a way to get the unit assigned to a SheetMetalRule object in the API?  I want to retrieve the thickness of a sheet metal part and the unit for that rule, so I can represent the thickness in the intended unit.

 

As an illustration, here's a screenshot of a simple flange with a rule that is in different units than the design; the unit I want is in the blue box.  The expression is "unitless" so I assume the unit is held elsewhere.  Is there a way to get that unit out of the API, and if not, could y'all add one?  Thanks!

 

cropped.png

6 REPLIES 6
Message 2 of 7
kandennti
in reply to: jesse

Hi @jesse -San.

 

I got the units from the value of the SheetMetalRule.thickness.expression property.

 

# Fusion360API Python script

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

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

        for comp in des.allComponents:
            rule: fusion.SheetMetalRule = comp.activeSheetMetalRule
            if rule:
                unit = get_sheetMetal_rule_unit(rule)
            else:
                unit = "-"

            app.log(
                f"{comp.name}:{unit}"
            )

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def get_sheetMetal_rule_unit(
    rule: fusion.SheetMetalRule,
) -> str:

    return re.sub(r"[0-9. ]+", "", rule.thickness.expression)

 

Message 3 of 7
jesse
in reply to: kandennti

Hi kandennti:

 

Appreciate the response!  I considered that too but it is possible to declare an expression for your sheet metal rule that does not contain units.  See the screenshot I posted; I stripped the units off the expression, but the rule still has units as indicated in the data panel (in the blue square).  So I think it has some inherent units that do not need to be included in the expression.

 

I also noticed that sheet metal parts contain design parameters for the different rule thicknesses, and the correct unit is assigned there.  Unfortunately these parameters are not available in Design.allParameters, but I assume the params and the rules are linked somehow?

 

Thanks!

 

Jesse

Message 4 of 7
Jorge_Jaramillo
in reply to: jesse

Hi,

 

I believe there is a bug in the Sheet Metal Rules dialog, which always display units (mm in my case, but inches in your case)  if the expression wasn't defined with a unit.  It is like adding default system units, instead of default document units.

 

From the documentation:

Jorge_Jaramillo_0-1699386487681.png

 

It states that default design units will be used, but it isn't using them, as the value property is always consistent with the units displayed in the dialog.

 

So, I'm not sure from where the defaults units are coming.

 

Regards,

Jorge Jaramillo

Software Engineer

 

Message 5 of 7
kandennti
in reply to: jesse

@jesse -San.

 

I did not realize that the rule is valid even when there are no units.

It is possible that I have not checked it enough, but I think that if I set the units in the document settings or preferences to inches and create a sheet metal rule with no units, it will be "mm".

1.png

 

I changed the function like this.

def get_sheetMetal_rule_unit(
    rule: fusion.SheetMetalRule,
) -> str:

    units = re.sub(r"[0-9. ]+", "", rule.thickness.expression)
    return "mm" if len(units) < 1 else units

 

Originally, I think it should be an error for sheet metal rules without units.

Message 6 of 7
Jorge_Jaramillo
in reply to: kandennti

Hi @kandennti ,

 

My default units are mm (in preference and in the design I tested).

I discovered that if I work with a Sheet Metal Rule that is inch based (like "Stainless steel (in)") it keeps the default units to inches, no matter what the default units you have in the preferences or in the design.

 

So, from this set of tests, I couldn't find a way to infer the units if they are not present in the expression property.

Somehow, it is kept inside the sheet metal rule but is not publish in data model.  Make sense?

 

@jesse : could you please confirm from which sheet metal rule you defined the "JR Test 1" rule?

 

My conclusion is: the behavior of the units doesn't match with the description of the property in the product documentation.  Either this is a bug or there is a mistake in the description.  If it is the latest, an extra property should be added to the metal sheet rule to indicate the default units.

 

Regards,

Jorge Jaramillo

Software Engineer

 

Message 7 of 7
jesse
in reply to: jesse

@kandennti , @Jorge_Jaramillo :

 

The JR Test 1 rule in my example was cloned from Stainless steel (in), which is why I think it has "in" units (like you described Jorge).  I actually don't know how to create a sheet metal rule purely from scratch, or if that's even possible, so my impression is any rule will have a unit designation.

 

Jorge, is this the line you're referring to? " If no units are specified, the document default units are implied".  I believe it may not be entirely wrong, if you interpret it as "the expression must have a unit, because the evaluated value then will be converted to the unit of the sheet metal rule, and so if no unit is specified, we will assign the default document unit". I think this is consistent with other expressions in Fusion 360.  Really, and I think you hit the nail on the head with your last statement, I think there just needs to be a property to get the SheetMetalRule unit, so we know how to scale the value (which will always be returned in cm/internal units) to match the intent of the sheet metal rule.

 

Thanks!

 

Jesse

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report