Hi @williama_mann. This is an odd sounding situation, so a little difficult to imagine. There are a few possibilities that come to mind though. Perhaps the first would be including an extra 'check' early in the code, before getting the entity or measuring it. This would be checking the Document.RequiresUpdate property's value. That is a ReadOnly property with a Boolean type value that gets set automatically by native Inventor processes when certain things happen to the document. It does not necessarily cover everything possible, but is a good one to keep in mind. If we check its value early in the code, and find its Value to be True, then we can update the document (using either Document.Update or Document.Update2 method) at that point, before accessing the entity or measuring it. I have also seen situations where the 'RequiresUpdate' property apparently did not know that something had changed, and so the Update method did not do its thing, because it did not believe it was necessary. In cases like that, we can use either the Document.Rebuild or Document.Rebuild2 method, which is supposed to ignore whether 'RequiresUpdate is true or not, and forces an update anyways.
Another thought that comes to mind is that the 'units' of measurement involved here may not be understood. You may already know this, but most numerical values we get by code from Inventor API code, and even some from uniquely iLogic code, is usually always in what is known as 'database units'. Database units are an unchangeable constant that are dictated by the software, not by any user accessible settings, and are always metric, instead of imperial. The database units for length or distance is always centimeters, no matter what your document units are set to. On the other hand, when using the user interface's Measure tool, with its convenient dialog, that will default to 'document units', but that dialog will also offer the ability to switch the units you want to see, or offer dual units. Document units are specified within the Document Settings (Tools Tab - Options Panel), on the Units tab.
In my example code, the first two lines are mostly using uniquely iLogic API code, because 'ThisDoc', and 'NamedEntities' objects are defined by the iLogic add-in, but the rest of the lines are primarily using Inventor API code with some general vb.net code elements mixed in. So, the measurement portion of the task is returning the value in database units (centimeters).
There is the possibility that something very difficult to explain is happening here. Things like edges, and faces are considered BRep entities (Boundary Representation), and can be somewhat temporary or unstable. This is because if you add more features to the model, and one of those features changes a pre-existing edge or face in any way, then a 'new' edge or face may be created to replace the previously existing one. This can make it very difficult, or maybe even impossible to find or retrieve the pre-existing entities, without doing something like rewinding the feature tree (modeling history). Assigning attributes to entities like these is one strategy commonly used to help us find entities like that later, but I don't recall if it works 100% of the time, in 100% of all possible situations. This is why assigning names to entities within a model is usually the very last thing done before saving, creating a drawing for it, or using it in an assembly. But I'm not sure if this 'behavior' has anything to do with the current issue.
Below is a slightly different version of my earlier code. In this one, I am getting the Document object first. If it is not a Part, then it will exit the rule, because it is only available in parts, as far as I know. Then it checks that property, and updates the part, if needed. I also put a line of code in there using the rebuild method, but left that commented out for now. If the property check and update line does not fix the odd situation you are experiencing, then try uncommenting (delete the leading apostrophe) the line for rebuilding the document next. Then, I am showing another uniquely iLogic way of getting the 'NamedEntities' from a specific Document object. Then the measurement is done the same. Then I added some code to automatically convert the units of the measured result to document units, if they are different units than the database units, before the final feedback message.
'get 'current' document, and try to Cast it to the PartDocument Type, without error
Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, PartDocument)
'if it was not a PartDocument, then no value was set to the variable in previous line
If oPDoc Is Nothing Then Return 'Return exits the routine
'check if Document needs an update, and if so, do it
If oPDoc.RequiresUpdate Then oPDoc.Update2(True)
'possibly 'Rebuild' the model, if needed
'oPDoc.Rebuild2(True)
'another way to get 'NamedEntities' which lets you specify the Document to get it from
Dim oNEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oPDoc)
Dim sEntityName As String = "SharedEdge"
Dim oMyNamedEdge As Edge = oNEs.TryGetEntity(sEntityName)
If oMyNamedEdge Is Nothing Then
MsgBox("Could Not Find Named Entity In Specified Document!", vbCritical, "Named Entity Not Found")
Exit Sub
End If
'get the evaluator for this specific entity
Dim oEval As CurveEvaluator = oMyNamedEdge.Evaluator
'declare the variables we will need to use
Dim oMinP, oMaxP, oLength As Double
'sets the values of those first two variables
oEval.GetParamExtents(oMinP, oMaxP)
'uses those first two values, and sets value of last variable
oEval.GetLengthAtParam(oMinP, oMaxP, oLength)
'Get Document Units, then convert units of measurement, if different
Dim UOM As UnitsOfMeasure = oPDoc.UnitsOfMeasure
If UOM.LengthUnits <> UnitsTypeEnum.kDatabaseLengthUnits Then
oLength = UOM.ConvertUnits(oLength, UnitsTypeEnum.kDatabaseLengthUnits, UOM.LengthUnits)
End If
'notify user of results
MsgBox("Length of Edge Named '" & sEntityName & "' = " & oLength.ToString, vbInformation, "Named Edge Length")
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

(Not an Autodesk Employee)