I'm trying to update mass/volume properties & remove any mass/volume overrides before running an export command with the settings I want.
The problem I'm running into is that I can't overwrite the iProperty dialog/value as seen in the picture below unless I do it manually. Even though the output of this code changes the Mass Override from True to False, the dialog still shows it as overwritten & the values still match the overwritten values.
' DEFINE DOCUMENT VARIABLES.
Public oDocument As Document = g_inventorApplication.ActiveDocument
Public oFileName As String = oDocument.DisplayName.Replace(".ipt", "")
Public oFilePath As String = oDocument.FullFileName.Replace("\" & oFileName & ".ipt", "")
' DEFINE PART DOCUMENT VARIABLES.
Public oPartDocument As PartDocument = g_inventorApplication.ActiveDocument
Public oPartCompDef As PartComponentDefinition = oPartDocument.ComponentDefinition
Public oMassProps As MassProperties = oPartCompDef.MassProperties
Public oMass As Double = oMassProps.Mass
Public oMassOverW As Boolean = oMassProps.MassOverridden
Public oVolume As Double = oPartCompDef.MassProperties.Volume
Public oVolumeOverW As Boolean = oMassProps.VolumeOverridden
' DEFINE COMMAND MANAGER VARIABLES.
Public oCommandManager As CommandManager = g_inventorApplication.CommandManager
Public oControlDefs As ControlDefinitions = oCommandManager.ControlDefinitions
Public oControlDef As ControlDefinition
' DEFINE PROPERTY VARIABLS.
Public oPropSets As PropertySets = oPartDocument.PropertySets
Public oProjectPropertySet As PropertySet = oPartDocument.PropertySets.Item("Design Tracking Properties") 'PROPERTIESFORDESIGNTRACKINGPROPERTIESENUM
Public oMassProperty As Inventor.Property = oProjectPropertySet.Item("Mass")
Public oMassValue As Double = oMassProperty.Value
' DEFINE UNITS OF MEASURE VARIABLES.
Public oUnitOfMeas_g As Object = oPartDocument.UnitsOfMeasure.MassUnits
Public oUnitOfMeas_kg As Object = oPartDocument.UnitsOfMeasure.MassUnits
Public oUnitOfMeas_lb As Object = oPartDocument.UnitsOfMeasure.MassUnits
Public Sub Export_To_STL()
Debug.Print("Before Test") 'MsgBox("Before Test")
UpdateMassProps()
End Sub
Private Sub UpdateMassProps()
' DEFINE UNITS OF MEASURE VARIABLES.
oUnitOfMeas_g = kGramMassUnits 'UNITSTYPEENUM
oUnitOfMeas_kg = kKilogramMassUnits 'UNITSTYPEENUM
oUnitOfMeas_lb = kLbMassMassUnits 'UNITSTYPEENUM
If oMassProps.AvailableAccuracy <> k_High Then 'Or k_VeryHigh Then
oMassProps.Accuracy = k_High
' RESULTS ARE NOT SAVED WITH THE DOCUMENT & DOCUMENT IS NOT 'DIRTIED'.
oMassProps.CacheResultsOnCompute = False
End If
oMass = Convert_Units(oMass, oUnitOfMeas_kg, oUnitOfMeas_lb)
oMassValue = Convert_Units(oMassValue, oUnitOfMeas_g, oUnitOfMeas_lb)
Debug.Print("oMassOverW Before: " & oMassOverW) 'MsgBox("oMassOverW Before: " & oMassOverW)
Debug.Print("oMassProp Dirty Before: " & oMassProperty.Dirty) 'MsgBox("oMassProp Dirty Before: " & oMassProperty.Dirty)
Debug.Print("oMass Before: " & oMass) 'MsgBox("oMass Before: " & oMass)
Debug.Print("oMassValue Before: " & oMassValue) 'MsgBox("oMassValue Before: " & oMassValue)
If oMassOverW = True Then
oMassOverW = False
End If
oControlDefs.Item("AppUpdateMassPropertiesCmd").Execute() 'Execute2(True)
oPartDocument.Update() 'Update2(True)
oPartDocument.Save()
Debug.Print("oMassOverW After: " & oMassOverW) 'MsgBox("oMassOverW After: " & oMassOverW)
Debug.Print("oMassProp Dirty After: " & oMassProperty.Dirty) 'MsgBox("oMassProp Dirty After: " & oMassProperty.Dirty)
Debug.Print("oMass After: " & oMass) 'MsgBox("oMass After: " & oMass)
Debug.Print("oMassValue After: " & oMassValue) 'MsgBox("oMassValue After: " & oMassValue)
End Sub
' CONVERT UNITS OF MEASUREMENT.
Private Function Convert_Units(oInputValue As Double, oInputUnits As Object, oOutputUnits As Object) As String
Dim oPartDocument As PartDocument
oPartDocument = g_inventorApplication.ActiveDocument
Convert_Units = oPartDocument.UnitsOfMeasure.ConvertUnits(oInputValue, oInputUnits, oOutputUnits).ToString
End Function
I've referenced a number of other forum posts talking about this but it doesn't seem to fix the problem I'm running into.
https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/calculate-mass/m-p/10047266
Thanks for the help in advance!
Tyler
Solved! Go to Solution.
Solved by tyler.warner. Go to Solution.
Solved by llorden4. Go to Solution.
If you have all your other settings (u/m, etc) set as desired, then just send the desired override value to "iProperties.Mass". Send a value of -1 to restore to an Inventor controlled dynamic value. Example:
iProperties.Mass = MyValue 'your override value
iProperties.Mass = -1 'restore Inventor calculated value
Your second link shows pretty much the same thing, just tested this and it works for me. Perhaps it's a version issue. I'm working 2022.3 at present.
Isn't "iProperties.Mass" only in iLogic? I want to do this using the API in an Add-In.
I tried changing the mass value for both types of property to -1 (as seen below), the debug output value changes to -1 for both.
If oMassOverW = True Then
oMass = -1
oMassValue = -1
oMassOverW = False
End If
But...
-it doesn't reset the output value to the calculated mass
-the iProperty Window --> Physical Tab still shows the overwrite values.
Am I missing something?
This may not be anything, but sounds appropriate to point out...
The MassProperties object has a Property called 'CacheResultsOnCompute' that may need to be intentionally set in a situation like this. Just a thought.
Edit: Oops, didn't look close enough...I see you already have that in there.
Wesley Crihfield
(Not an Autodesk Employee)
I don't dabble in VB.net, but I can use iLogic to call the API and simply setting the the MassOverridden to False set it back to dynamically managed for me, didn't have to send the -1 value.
iLogic API call:
ThisDoc.Document.ComponentDefinition.MassProperties.MassOverridden = False
For your code, try...
If oMassOverW = True Then oMassOverW = False
Maybe a document update immediately thereafter?
Figured it out after looking at your comments & the links I posted a few more times. The part where I was messing up was changing the variable "oMassOverW" to "False" but not the actual property of the document as seen in the snippet below.
If oMassOverW = True Then
'oMassOverW = False ' INCORRECT
oMassProps.MassOverridden = False ' CORRECT
oMass = oMassProps.Mass
End If
If oVolumeOverW = True Then
'oVolumeOverW = False ' INCORRECT
oMassProps.VolumeOverridden = False
oVolume = oMassProps.Volume
End If
The entire corrected command has been posted below for future reference.
' DEFINE DOCUMENT VARIABLES.
Public oDocument As Document = g_inventorApplication.ActiveDocument
' DEFINE PART DOCUMENT VARIABLES.
Public oPartDocument As PartDocument = g_inventorApplication.ActiveDocument
Public oPartCompDef As PartComponentDefinition = oPartDocument.ComponentDefinition
Public oMassProps As MassProperties = oPartCompDef.MassProperties
Public oMass As Double = oMassProps.Mass
Public oMassOverW As Boolean = oMassProps.MassOverridden
Public oVolume As Double = oPartCompDef.MassProperties.Volume
Public oVolumeOverW As Boolean = oMassProps.VolumeOverridden
' DEFINE COMMAND MANAGER VARIABLES.
Public oCommandManager As CommandManager = g_inventorApplication.CommandManager
Public oControlDefs As ControlDefinitions = oCommandManager.ControlDefinitions
' DEFINE UNITS OF MEASURE VARIABLES.
Public oUnitOfMeas_g As Object = oPartDocument.UnitsOfMeasure.MassUnits
Public oUnitOfMeas_kg As Object = oPartDocument.UnitsOfMeasure.MassUnits
Public oUnitOfMeas_lb As Object = oPartDocument.UnitsOfMeasure.MassUnits
Public Sub UpdateMassProps()
' CHECK THAT PART FILE IS OPEN.
If oDocument.DocumentType <> kPartDocumentObject Then 'DOCUMENTTYPEENUM
MsgBox("The open file is not a part file. The command has been canceled.")
Exit Sub
End If
' DEFINE UNITS OF MEASURE VARIABLES.
oUnitOfMeas_g = kGramMassUnits 'UNITSTYPEENUM
oUnitOfMeas_kg = kKilogramMassUnits 'UNITSTYPEENUM
oUnitOfMeas_lb = kLbMassMassUnits 'UNITSTYPEENUM
If oMassProps.Accuracy <> k_High Or k_VeryHigh Then
oMassProps.Accuracy = k_High
End If
If oMassOverW = True Then
oMassProps.MassOverridden = False
oMass = oMassProps.Mass
End If
If oVolumeOverW = True Then
oMassProps.VolumeOverridden = False
oVolume = oMassProps.Volume
End If
oControlDefs.Item("AppUpdateMassPropertiesCmd").Execute()
oPartDocument.Update()
oMass = Convert_Units(oMass, oUnitOfMeas_kg, oUnitOfMeas_lb)
oMass = Math.Round(oMass, 3, MidpointRounding.ToEven) 'SET PRECISION TO 3 DECIMAL PLACES
oVolume *= (0.3937008 ^ 3) 'CONVERT RESULT FROM CM^3 TO IN^3 'NOT AVAILABLE UNITSTYPEENUM
oVolume = Math.Round(oVolume, 3, MidpointRounding.ToEven) 'SET PRECISION TO 3 DECIMAL PLACES
End Sub
' CONVERT UNITS OF MEASUREMENT.
Private Function Convert_Units(oInputValue As Double, oInputUnits As Object, oOutputUnits As Object) As String
Dim oPartDocument As PartDocument = g_inventorApplication.ActiveDocument
Convert_Units = oPartDocument.UnitsOfMeasure.ConvertUnits(oInputValue, oInputUnits, oOutputUnits).ToString
End Function
Can't find what you're looking for? Ask the community or share your knowledge.