I am able to read MassProperties.Mass, however failing to find a way to get units using API, like: kg(kilograms) or punds

I am able to read MassProperties.Mass, however failing to find a way to get units using API, like: kg(kilograms) or punds

konstantinas_kuznecovas
Contributor Contributor
658 Views
9 Replies
Message 1 of 10

I am able to read MassProperties.Mass, however failing to find a way to get units using API, like: kg(kilograms) or punds

konstantinas_kuznecovas
Contributor
Contributor

To whom it may concern,

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=MassProperties_Mass

Using C# general purpose programming language and API from Inventor
I am able to extract values for "MassProperties.Mass Property",
however, failing to find how to extract units like kilograms or
pounds from Assembly(Bill Of Materials).

By any chance it is possible, or it is not yet supported?


Ha nice and shiny day for work, plus stay safe!
Many regards,
Konstantinas Kuznecovas

0 Likes
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @konstantinas_kuznecovas.  The assembly BOM gets its Mass units from the document settings of each model document.  Manually, if you have a part open, go to the Tools tab, then within the Options panel, click on the Document Settings button.  Then go to the Units tab.  That is where you can specify which units that document should use for its Mass.  However, the API MassProperties.Mass will always return the Mass in 'database units', instead of document units.  But the BOM should show the Mass in document units, if you are showing a column for that aspect.  Then, if you go to the Bill of Materials tab within that Document Settings dialog.  There is where the quantities you see in the BOM are based on.  We can access all those same document settings by code too.  But accessing Mass through the BOM by code is a bit more complicated, because it is not like a Table that can easily be navigate by rows, columns, and cells.  You must go into the actual Component/ComponentDefinition/Document that the row represents, then dig for it the hard way, like accessing that MassProperties.Mass property.  We can use some code to convert the units though.

 

Edit:  If forgot to mention that you may be able to get the Mass in document units from the document's iProperty, after it has been updated.  But in an odd way.  Then regular iProperty named "Mass", within the third PropertySet named "Design Tracking Properties" will still return the Mass value in database units, just like the API property, but the iLogic snippet "iProperties.Mass" will return the Mass in document units.  But that snippet is supposed to target the document that the rule is running within.  That would be obvious if the rule were an internal rule (saved within an Inventor document), because it would be the same document that the rule is within.  But if used from an external iLogic rule, or some other external source, it will likely target whichever document happens to be 'active' (visibly showing in the Inventor screen) at that moment.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 10

konstantinas_kuznecovas
Contributor
Contributor
Accepted solution

Hi WCrihfield,

1) Thanks a lot for previous, we will try to make sense of it :).
2) If you have some code examples how to get it please send along :).


Ha nice and shiny day for work, plus stay safe!
Many regards,
Konstantinas Kuznecovas

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @konstantinas_kuznecovas.  First of all, I may be confused about what you are actually looking for here, due to how you worded your original post.

  • Are you looking for a numerical Mass value only, but a value that is in specific units?
  • Are you unsure about what units the numerical Mass value you are getting are in, and want a way to figure out which units they are in?
  • Are you just looking for a way to include the mass units specification (String) along with the numerical value you get for the Mass?
  • Are you just looking for a way to show units specification string within the cells you see in your assembly BOM Mass column?

I was under the impression that you were looking for a way to get the Mass value in 'Document Units', instead of in 'Database Units' (numerical value only, without units specifier).

 

I will try to break this down a bit more for you, to simplify.

 

If accessing the assembly BOM by code to get Mass... The BOMRow API object (a row within the BOM API object, within an assembly) does not have a direct property for getting the 'Mass' of the items that that row represents, and there is no easy way to get it from that row, in document units.  It is not as simple as it looks in the BOM dialog, because there is no 'table' we can simply navigate by code.  We still must dig down into one of the referenced documents that the row represents, get its Mass from within its ComponentDefinition.MassProperties.Mass, convert that value from database units to document units, then multiply that by one of the available quantity type properties of the BOMRow.

 

If getting Mass from a document's iProperties, instead of from its ComponentDefinition.MassProperties.Mass, then the regular iProperty at the following location:

oDoc.PropertySets.Item("Design Tracking Properties").Item("Mass").Value

...will still be returned in 'database units', and not document units, and it is possible that this iProperty's value may not be up to date in certain conditions.  That means of accessing that iProperty that I just mentioned is going through the Inventor API system.

However, there is also an iLogic API related way to access Mass, which also seems to be going through the iProperties, but will return the value in document units directly, instead of in database units.  There are multiple ways to use this line of code, and I do not know in which context you would be using it, so I do not know which variation of it would be best for you.  If you used it like the following:

iProperties.Mass

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=4da3c892-51fa-b527-88d1-b7a84d8075a9 

...then, as you can see, there is no indication of which document it will be targeting, and very little documentation about that aspect of how it functions.  It says that it will target the part or assembly that contains this rule, but that only applies to when it is used within an 'internal' rule, because those are saved within an Inventor document.  But what about when it is used in an external rule, or elsewhere...no mention of how it would work there.  Then there is a second way to use this line of code, which requires some additional input, but specifically what input is required is another detail that is not documented very well.  If used in a rule while an assembly is active, and you wanted to get the mass of a single, specific assembly component, then you can input the name of that component, as seen in the model browser.

iProperties.Mass("Part1:1")

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=f4341525-6d90-8881-d4f7-2b17d1172432 

Or, instead of the name of an assembly component, you can specify the name of a document.  But it does not tell you what portion of its name (include path ?, name only ? , include file extension?).  Usually we can use the value we get from Document.DisplayName property, but that is a Read/Write property, sometimes including file extension, sometimes not, and it can also be overwritten, so...

We can use that line of code to 'Get' a value, or to 'Set' a value, and as mentioned, its value is in document units.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 10

konstantinas_kuznecovas
Contributor
Contributor
Accepted solution

Hi WCrihfield,

Sorry for original confusion, we are first time
so deep in it :).

What we have is drawings and assemblies.
We did created solution for drawings and here we have "Mass+(kg or pounds)"
We are looking for same in assemblies, for now we are getting Mass
in C# language in following way:
var partComponentDefinition = compDef as PartComponentDefinition;
partComponentDefinition.MassProperties.Mass

and we need following scenario mentioned by you before :).
"Are you just looking for a way to include the mass units specification (String) along with the numerical value you get for the Mass?"

We will continue for a way in 🙂

 

Ha nice and shiny day for work, plus stay safe!
Many regards,
Konstantinas Kuznecovas

0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor
Accepted solution

@konstantinas_kuznecovas

Ok.  That sounds simple enough.  If you are writing your code in C#, then getting the mass from ComponentDefinition.MassProperties.Mass is the best way, because it is strictly Inventor API code, and has the most options for accuracy.  When getting the value from that, the value will always be in database units, which is kilograms, no matter what your document units may be.  So, if having the value in kilograms is OK with you, then the next task would be to convert that Double data type value into a String, then adding the units specifying text at the end of that numerical value, with a single space between the numerical value and the units specifier text.

I am not fluent in C#, so my example below is in vb.net format.

Dim oPDoc As PartDocument = ThisApplication.ActiveDocument
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
Dim dMass As Double = oPDef.MassProperties.Mass
Dim sMassString As String = CStr(dMass) & " kg"
MessageBox.Show("sMassString = " & sMassString, "Mass With Units String")

Line 3 is getting the Mass numerical value, in database units (kilogram).

Then Line 4 is converting that Double into a String, using the CStr() (Link1, Link2) conversion function, the adding the space and units specifier with " kg" string.  But you could probably just use something like:

dMass.ToString & " kg"

https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=net-8.0 

but I am not sure how you would need to do it in C#.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 10

WCrihfield
Mentor
Mentor
Accepted solution

By the way, here is a simple iLogic rule, using purely Inventor API code in vb.net that you can run to explore what the differences are between your active document units and the 'database' units.

'Dim UOM As UnitsOfMeasure = ThisApplication.UnitsOfMeasure
Dim UOM As UnitsOfMeasure = ThisApplication.ActiveDocument.UnitsOfMeasure
Dim Doc_LengthUnits As String = UOM.GetStringFromType(UOM.LengthUnits)
Dim DB_LengthUnits As String = UOM.GetStringFromType(UnitsTypeEnum.kDatabaseLengthUnits)
Dim Doc_AngleUnits As String = UOM.GetStringFromType(UOM.AngleUnits)
Dim DB_AngleUnits As String = UOM.GetStringFromType(UnitsTypeEnum.kDatabaseAngleUnits)
Dim Doc_MassUnits As String = UOM.GetStringFromType(UOM.MassUnits)
Dim DB_MassUnits As String = UOM.GetStringFromType(UnitsTypeEnum.kDatabaseMassUnits)
Dim Doc_TimeUnits As String = UOM.GetStringFromType(UOM.TimeUnits)
Dim DB_TimeUnits As String = UOM.GetStringFromType(UnitsTypeEnum.kDatabaseTimeUnits)
Dim DB_TemperatureUnits As String = UOM.GetStringFromType(UnitsTypeEnum.kDatabaseTemperatureUnits)
Dim sReport As String
sReport = "Document Length Units = " & Doc_LengthUnits _
& vbCrLf & "Database Length Units = " & DB_LengthUnits _
& vbCrLf _
& vbCrLf & "Document Angle Units = " & Doc_AngleUnits _
& vbCrLf & "Database Angle Units = " & DB_AngleUnits _
& vbCrLf _
& vbCrLf & "Document Mass Units = " & Doc_MassUnits _
& vbCrLf & "Database Mass Units = " & DB_MassUnits _
& vbCrLf _
& vbCrLf & "Document Time Units = " & Doc_TimeUnits _
& vbCrLf & "Database Time Units = " & DB_TimeUnits _
& vbCrLf _
& vbCrLf & "Database Temperature Units = " & DB_TemperatureUnits
MessageBox.Show(sReport, "Inventor API Units")

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 10

konstantinas_kuznecovas
Contributor
Contributor
Accepted solution

Hi WCrihfield,

Thanks a lot for previous,
we will use following from your description:
"When getting the value from that, the value will always be in database units, which is kilograms, no matter what your document units may be".

The only thing we need more is to know which rounding for mass is used in Inventor
so that we could match it to current double value for example:
10,1234567 + " " + "kg" 🙂
is this rounding is used or some other?
10,12 + " " + "kg" 🙂

By any chance you know?

Sorry for all these questions, however it looks like we are very close
to "Accepting solution" :).

 

Ha nice and shiny day for work, plus stay safe!
Many regards,
Konstantinas Kuznecovas

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor
Accepted solution

@konstantinas_kuznecovas

I do not know for sure what Autodesk may be doing behind the scenes to round off the value you get from the ComponentDefinition.MassProperties.Mass property, but you do have some control over its 'accuracy'.  As you may have noticed within the iProperties dialog, on the Physical tab, there is a drop-down list allowing you to select/change a setting being called 'Requested Accuracy'.  Well, we can control that by code too, through that same MassProperties object you are already working with.  That object has 2 properties (MassProperties.AvailableAccuracy and MassProperties.Accuracy), and 1 method (MassProperties.AchievedAccuracy) related to accuracy.  The one named AvailableAccuracy is ReadOnly, and seems to just be for 'Reading' what the current value's accuracy is.  The one just named Accuracy is Read/Write, and if for actually setting the level of accuracy you want.  The method named AchievedAccuracy seems to only be for 'getting' (not setting) the accuracy of the Area and Volume values.  In fact, some folks have mentioned not getting an up-to-date value from the Mass property directly, by only accessing its value.  So, some folks change the Accuracy from its current setting to another setting (and/or back again) then get the value of the Mass property, to force it to re-evaluate the Mass.  However, as you can see by reviewing the MassPropertiesAccuracyEnum documentation, it does not mention a specific number of decimal places in those settings.  Beyond that, the Double data type is capable of holding values all the way out to 15 decimal places, which can make comparing one Double value with another complicated...often requiring rounding of values before comparison, or allowing for a tolerance when comparing.  

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 10

konstantinas_kuznecovas
Contributor
Contributor
Accepted solution

Hi WCrihfield,

Thanks a lot for previous,
we will keep it simple for now:
10,123456789 + " " + "kg" 🙂

and come back to previous description if some changes are to take place :).

Accepting resolution, thanks a lot for being there 🙂

Ha nice and shiny day for work, plus stay safe!
Many regards,
Konstantinas Kuznecovas

0 Likes