Read mass/ volume properties of the part on a drawing sheet and create text to show the value

Read mass/ volume properties of the part on a drawing sheet and create text to show the value

jerinc101
Contributor Contributor
1,407 Views
9 Replies
Message 1 of 10

Read mass/ volume properties of the part on a drawing sheet and create text to show the value

jerinc101
Contributor
Contributor

Hi All,

I am trying to find an ilogic code to call the mass properties of the part in drawing sheet. After that I need to put a text box and show the weight of the part. But I need to show weight of two material using same volume. Can you help me anyone? Thanks in advance!

0 Likes
Accepted solutions (1)
1,408 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Hi @jerinc101.  You may be able to do most of this with normal user interface tools, without needing any code.  If you have your drawing open, and have at least one view on the active sheet that is referencing a model document, then try the following.  On the annotate tab, click the Text tool, then click somewhere on your sheet to specify where to place it, then we can use the drop-down menus to place the two pieces of information needed.  Just above the text window, on the left is a drop-down list labeled 'Type'.  Choose "Physical Properties - Model" from that list.  Now, click on the drop-down list labeled 'Source', and choose the one starting with "<Primary Model>" (or similar).  Now click on the drop-down list labeled 'Property', and choose either "MASS" or "VOLUME".  Now place your cursor where you want to insert that information, then click on the button that looks like a cursive 'X', with a downward pointing blue arrow to insert a link to that information into the text box area.  Now click OK on the main Format Text dialog to place that note on your drawing.

WCrihfield_0-1710773164098.png

WCrihfield_1-1710773437696.png

This does not show the mass for two different materials, but you could have a different ModelState in the part for each material (the part is one material while the one ModelState is active, and the other material while the other ModelState is active), then you could reference each of the two different ModelState versions of the part to get the two different values.

 

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 10

WCrihfield
Mentor
Mentor

Hi @jerinc101.  It might not let you select one of the other ModelState versions of that model in the 'Source' drop-down list in some situations, such as in a regular text note with no leader.  In cases like that I have found that if I place a view of the other ModelState version of the model on the sheet, then use a leader note, instead of just a regular text, then attach that leader note to the view of the other ModelState, it will then let me include data that is linked to that other ModelState version of the model.  When in the Format Text dialog for a leader note that is attached to the geometry of a model, the text within the 'Source' drop-down list that you would want to select then says "<Attached Model>", making it simple.  The leader could then be deleted, leaving the text in place, if needed.  However, when you do it this way, if you suppress or delete the view that the leader note was attached to, it will also remove that leader note, even if you deleted the leader, because it is still associated with that view.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 10

jerinc101
Contributor
Contributor

Hi @WCrihfield,

Thank you for your reply. Is there any way to read volume of the part in the drawing via ilogic code? If I can get that. I can use the volume of the part, then manually enter the density. then I can get the mass using multiply volume and density. 

Also if I get the ilogic rule. It can apply in all sheet. I have to do lot more drawing sheet.

0 Likes
Message 5 of 10

FINET_Laurent
Advisor
Advisor

Hello @jerinc101,

 

Let's start with the basic. Here a code that reads the volume property (properties has to be up to date before hand) : 

Dim v As Inventor.DrawingView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Pick a drawing view :")
Dim refDoc As Inventor.Document = v.ReferencedDocumentDescriptor.ReferencedDocument
Dim vol As Double = refDoc.PropertySets.Item("Design Tracking Properties").Item("Volume").Value
MsgBox(vol

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 6 of 10

jerinc101
Contributor
Contributor

Hi @FINET_Laurent ,

Thanks for the reply. That rule really help me to get the solution. my drawing sheet contain one part at every sheet. so can I get part volume without selecting the view? that makes me to get the value by just run the rule. 

Thanks in advance!

0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

Hi @jerinc101.  There are several ways to get the Volume of a model document being shown in a drawing sheet by code.  You can get it from the model's iProperty, if it has been updated, as @FINET_Laurent mentioned above, or you can get it from the source MassProperties object, within the ComponentDefinition of the model document (AssemblyComponentDefinition.MassProperties or PartComponentDefinition.MassProperties).  If you get it from the MassProperties object, you have the opportunity to ensure you are obtaining updated values by essentially forcing it to recalculate before getting the values.  Below is another example iLogic rule which uses the MassProperties object, makes sure data is updated and accurate, then gets current values, then converts database units to document units for you.  Then it shows you the current data, prompts you to enter new Density, then multiplies that by the current Volume to get a new Mass, then reports that new Mass to the user in various ways.  I did not know exactly what type of output you were looking for (just message, output to iLogic Log window, or other).

 

Sub Main
	Dim oDDoc As DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oViews As DrawingViews = oDDoc.ActiveSheet.DrawingViews
	If oViews.Count = 0 Then Return
	Dim oView As DrawingView = oViews.Item(1)
	Dim oModelDoc As Inventor.Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oMassProps As MassProperties = oModelDoc.ComponentDefinition.MassProperties
	oMassProps.CacheResultsOnCompute = False 'avoids dirtying document
	'change accuracy to force recompute
	If oMassProps.AvailableAccuracy = MassPropertiesAccuracyEnum.k_None Or _
		oMassProps.AvailableAccuracy = MassPropertiesAccuracyEnum.k_Low Then
		oMassProps.Accuracy = MassPropertiesAccuracyEnum.k_Medium
	Else
		oMassProps.Accuracy = MassPropertiesAccuracyEnum.k_Low
	End If
	'prepare all variables for numerical values
	Dim dMass, dVol, dDefaultDensity, dNewDensity, dNewMass As Double
	'prepare stuff needed for potentially converting default database units to document units
	Dim UOM As UnitsOfMeasure = oModelDoc.UnitsOfMeasure
	Dim sDBMassUnits As String = UOM.GetStringFromType(UnitsTypeEnum.kDatabaseMassUnits)
	Dim sDocMassUnits As String = UOM.GetStringFromType(UOM.MassUnits)
	Dim sDBLengthUnits As String = UOM.GetStringFromType(UnitsTypeEnum.kDatabaseLengthUnits)
	Dim sDocLengthUnits As String = UOM.GetStringFromType(UOM.LengthUnits)
	Dim sDBVolumeUnits As String = sDBLengthUnits & "^3"
	Dim sDocVolumeUnits As String = sDocLengthUnits & "^3"
	Dim sDBDensityUnits As String = "kg/m^3"
	Dim sDocDensityUnits As String = sDocMassUnits & "/" & sDocLengthUnits & "^3"
	'get Mass
	dMass = oMassProps.Mass
	'convert Mass units if needed
	If UnitsTypeEnum.kDatabaseMassUnits <> UOM.MassUnits Then
		dMass = UOM.ConvertUnits(dMass, sDBMassUnits, sDocMassUnits)
	End If
	'get volume
	dVol = oMassProps.Volume
	'convert volume units if needed
	If sDBVolumeUnits <> sDocVolumeUnits Then
		dVol = UOM.ConvertUnits(dVol, sDBVolumeUnits, sDocVolumeUnits)
	End If
	'get default density
	dDefaultDensity = (dMass / dVol)
	'convert density units if needed
	If sDBDensityUnits <> sDocDensityUnits Then
		dDefaultDensity = UOM.ConvertUnits(dDefaultDensity, sDBDensityUnits, sDocDensityUnits)
	End If
	Dim sCurrentDataReport As String = "Current Mass = " & dMass.ToString & vbCrLf & _
	"Current Volume = " & dVol.ToString & vbCrLf & _
	"Current Density = " & dDefaultDensity.ToString
	Logger.Info(sCurrentDataReport)
	Dim sPrompt As String = sCurrentDataReport & vbCrLf & vbCrLf & _
	"Enter different Density to calculate different Mass."
	Dim sSpecDensity As String = InputBox(sPrompt, "Mass / Volume / Density", "")
	If sSpecDensity = "" Then Return 'nothing was specified, so exit routine
	dNewDensity = CDbl(sSpecDensity)
	dNewMass = (dNewDensity * dVol)
	'write new mass to iLogic Log window, so that it is selectable (can be copied)
	Logger.Info("New Mass = " & dNewMass.ToString)
	'show new mass in simple MsgBox (value not selectable)
	MsgBox("New Mass = " & dNewMass.ToString, vbInformation, "New Mass")
	'show new mass as default value of InputBox, so that it is selectable (can be copied)
	a = InputBox("","New Mass", dNewMass.ToString)
End Sub

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 10

WCrihfield
Mentor
Mentor

There is also another direct, manual way to obtain current MassProperties type data from the model while editing the drawing, without needing any iLogic code.  But this way would not prompt you to enter new density or calculate new mass automatically for you.  To try this, while the drawing is active, and you have a drawing view of the model on your screen, expand the model browser node for the drawing view until you see the node for the model.  Then right-click on that model node, and choose iProperties.  Then go to the Physical tab, and there is the information for the model that you can select and copy as needed.  The density value shown there is obtained from the active material, because that is one of the specifications within the material's definition.  But you can select/copy the Volume value (may not be in document units), then paste it into the system calculator to multiply it by a density value you enter to get a different Mass value, which you can then copy and paste into a text note on your drawing. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

jerinc101
Contributor
Contributor

Hi @WCrihfield@FINET_Laurent ,

Thanks for your support. I have conclude my ilogic code that are combine your codes together. 

 

Dim oDDoc As DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
If oDDoc Is Nothing Then Return
Dim oViews As DrawingViews = oDDoc.ActiveSheet.DrawingViews
If oViews.Count = 0 Then Return
Dim oView As DrawingView = oViews.Item(1)
Dim oModelDoc As Inventor.Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim vol As Double = oModelDoc.PropertySets.Item("Design Tracking Properties").Item("Volume").Value
 
Dim weight As Double = Round(((7.85 * vol) / 1000) ,3)
Dim ssweight As Double = Round(((8 * vol) / 1000) ,3)
'  a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
 
'  a reference to the active sheet.
Dim oActiveSheet As Sheet
oActiveSheet = oDrawDoc.ActiveSheet
 
'  a reference to the GeneralNotes object
Dim oGeneralNotes As GeneralNotes
oGeneralNotes = oActiveSheet.DrawingNotes.GeneralNotes
 
oKeywords = iProperties.Volume
 
Dim oDoc As DrawingDocument
oDoc = ThisDrawing.Document
 
 
Dim oTG As TransientGeometry
oTG = ThisApplication.TransientGeometry
 
' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.
Dim sText As String
sText = " TOTAL WEIGHT (PG) : " & weight & " kg <Br/>TOTAL WEIGHT (SS) : " & ssweight & " kg"
 
Dim oGeneralNote As GeneralNote
oGeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(18, 3), sText)
 
 
Thank you @WCrihfield , @FINET_Laurent !!!!
 
 
 
0 Likes
Message 10 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @jerinc101.  Glad to hear you got something working OK for you.

But, it looks like your combined code is obtaining a reference to the drawing document in 3 different places, and using a different phrase for setting its value each time.  That can lead to working with different documents in different areas of the code in some situations.  I also saw that you had created a variable named "oKeywords" and set its value using yet another means of obtaining the Volume, but it was not being used correctly yet, and you were not using that variable for anything after that point.  When you just type out "iProperties.Volume", with no further specification, it will attempt to get the value from the current document (the drawing in this case, which would not work).  However, if you want to use that to access the Volume in 'the model', you can specify the 'DisplayName' of 'the model' within the ( & ) ....  One advantage of getting the Volume with that iLogic shortcut snippet, is that it returns the value in document units, instead of database units.  That way you usually do not need to convert the units.  But since I did not know what units you wanted the results in, I just tried to make the units conversion portion of the code as universal as possible.

Anyways, I copied your code, then edited it a bit to clean up a couple things, then I am posting it again below.  I added a couple more lines of code around where it gets the Volume, to show that alternate route, just in case you might like that better, but left them commented out for now.

Dim oDDoc As DrawingDocument = TryCast(ThisDoc.Document, Inventor.DrawingDocument)
If oDDoc Is Nothing Then Return
Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
Dim oViews As DrawingViews = oSheet.DrawingViews
If oViews.Count = 0 Then Return
Dim oView As DrawingView = oViews.Item(1)
Dim oModelDoc As Inventor.Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
'gets Volume in database units, if it has already been calculated
Dim vol As Double = oModelDoc.PropertySets.Item("Design Tracking Properties").Item("Volume").Value
'Logger.Info("Volume (Database Units) = " & vol)
'gets Volume In document units, From a referenced document Or Component, By its name.
'Dim dVol As Double = iProperties.Volume(oModelDoc.DisplayName)
'Logger.Info("Volume (Document Units) = " & dVol)
Dim weight As Double = Round(((7.85 * vol) / 1000), 3)
Dim ssweight As Double = Round(((8 * vol) / 1000), 3)
'  a reference to the GeneralNotes object
Dim oGeneralNotes As GeneralNotes = oSheet.DrawingNotes.GeneralNotes
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
' Create text with simple string as input. Since this doesn't use
' any text overrides, it will default to the active text style.
Dim sText As String = " TOTAL WEIGHT (PG) : " & weight & " kg <Br/>TOTAL WEIGHT (SS) : " & ssweight & " kg"
Dim oGeneralNote As GeneralNote = oGeneralNotes.AddFitted(oTG.CreatePoint2d(18, 3), sText)

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)