By Adam Nagy
If you want to show the current volume of the model inside a sketch text box, then you either need to keep updating a TextBox with the latest information yourself or base the TextBox information on a User or Model Parameter that is updated from the actual Volume of the model.
In case of our simple model you could easily calculate the Volume yourself based on the 3 dimensions of this box model. However, in case of a complicated model you would need to rely on the calculated value provided by Inventor, e.g. iProperties.Volume. This however requires a model geometry recalculation to be correct. So if you use it right after a model parameter changed, but before the model geometry got recalculated, then it will be incorrect.
If you subscribed the Rule to both Any Model Parameter Change and Part Geometry Change and your Rule relies on information that is calculated from geometry, like Volume, then Any Model Parameter Change event is too early and better to remove it from the list of events that trigger your rule:
If you did not do the above and your Rule is not forcing an update explicitly (e.g. using InventorVb.DocumentUpdate(False)) before reading the iProperties.Volume property then this would happen when changing parameters directly in the Parameters dialog:
So the above was enough to make sure that our own Volume User Parameter will always be up-to-date. Then to make the TextBox up-to-date as well, we can simply do this which will trigger an update after our Rule finished:
iLogicVb.UpdateWhenDone = True
We are also checking the Volume based on liters, so we use UnitsOfMeasure object to convert the Volume to liters. Here is the Rule code:
' This is in mm^3 because of the ' Part Document's unit settings ' You have to set the rule ' so that it's only triggered for ' "Part Geometry Change" event, so ' that the iProperties.Volume is correct Volume = iProperties.Volume ' If we want it in liters Dim uom As UnitsOfMeasure uom = ThisApplication.ActiveDocument.UnitsOfMeasure Dim lengthUnit As String lengthUnit = uom.GetStringFromType(uom.LengthUnits) ' mm^3 to liters VV = uom.ConvertUnits(Volume, lengthUnit + "^3", "l") ' In liters V_needed = 2500 ' Need to use accuracy acc = .0001 ' The values are equal or VV is bigger If VV > V_needed - acc Then iProperties.PartColor = "Green Pastel" Else iProperties.PartColor = "Red" End If ' This is enough to get the sketch text updated ' to the new Volume value iLogicVb.UpdateWhenDone = True
Sample model: Download Tank volume 2014