DigiPara Liftdesigner LDX API
Loading...
Searching...
No Matches
More consistent LDX class names: ObjectName2

API method: ILDXObject5.ObjectName2

Treename: Shaft0.

 ' from Samples/Src/LDXAPIExamples/Example_LDXObject.vb

 Private Shared Sub ObjectName2Recursive(ldxObject As DigiPara.LDX.LDXObject, objectIdsHandled As System.Collections.Generic.HashSet(Of DigiPara.LDX.ObjectId))
     ' collect subobjects
     ' this is our new iteration; it is faster than just 'in ldxObject', can be customized and the items are never null
     ' also, we can just keep the return value, it is a regular array that does not expire even when ldxObject gets destroyed
     Dim list() As DigiPara.LDX.LDXObject = ldxObject.GetChildren()

     ' depth recursion first
     For Each subObject In list
         ObjectName2Recursive(subObject, objectIdsHandled)
     Next

     Dim objectId As DigiPara.LDX.ObjectId = ldxObject.ObjectId
     If objectIdsHandled.Contains(objectId) Then
         Return
     End If

     objectIdsHandled.Add(objectId)

     Dim idName As String = objectId.ToString()
     Dim objName As String = ldxObject.ObjectName
     Dim objName2 As String = ldxObject.ObjectName2
     Dim className As String = Microsoft.VisualBasic.Information.TypeName(ldxObject.LDXType)

     ' the return of .ObjectName2() and the stringified value of .ObjectId match, apart from the different prefix
     System.Diagnostics.Debug.Assert(objName2.Replace("LDX", "id") = idName)

     ' and if the return type of LDXType is non-generic
     If className <> "ILDXObject5" And className <> "ILDXComp5" And className <> "_ComObject" Then
         ' its name also matches the return of .ObjectName2 in some way

         Dim expected1 As String = "I" + objName2 + "5" ' most commonly, you get the interface name
         Dim expected2 As String = objName2 + "Class" ' sometimes, the name of the COClass implementation
         Dim expected3 As String = objName2.Replace("LDXWb", "ILDXObject") + "5" ' and sometimes, there was an inconsistency we could have only fixed by either renaming the ObjectID enum or the interface class.
         Dim expected4 As String = "I" + objName2 ' and sometimes, the interface does not have the trailing 5, also unfixable
         System.Diagnostics.Debug.Assert(className = expected1 Or className = expected2 Or className = expected3 Or className = expected4)
     End If

     ' point out inconsistencies
     If objName <> objName2 Then
         Dim b As Text.StringBuilder = New Text.StringBuilder()
         b.AppendLine(String.Format("ObjectName() = {0}", objName))
         b.AppendLine(String.Format("ObjectName2() = {0}", objName2))
         b.AppendLine(String.Format("ObjectId = {0}", idName))
         b.AppendLine(String.Format("className = {0}", className))
         System.Windows.Forms.MessageBox.Show(b.ToString(), "Inconsistent ObjectName()")
     End If

 End Sub

 <APIHelpAttribute(APIHelpModes.FireCalcModel Or APIHelpModes.LoadLD3File, {APIHelpModes.InputParaLDXDoc}, "Shaft0.", "LDXObject~ObjectName2", "More consistent LDX class names: ObjectName2")>
 Public Shared Sub ObjectName2(ByRef ldxDocument As DigiPara.LDX.LDXDocument)

     If ldxDocument Is Nothing Then
         MsgBox("Not a valid document.")
         Exit Sub
     End If

     ' The old function LDXObject.ObjectName() is supposed to give a useful LDX class name.
     ' For some leaf classes, however, the return value was inconsistent with the LDX classname or the
     ' name of the ObjectId enum.
     ' To maintain compatibility, the old function could not be changed. Instead, 
     ' we introduced LDXObject.ObjectName2(), which does not suffer from inconsistencies.
     ' This demo verifies that and points out where ObjectName() was inconsistent.
     ' Wherever you can, try to change ObjectName() to ObjectName2() in your .NET client code.

     Dim objectIdsLeft As System.Collections.Generic.HashSet(Of DigiPara.LDX.ObjectId) = New HashSet(Of DigiPara.LDX.ObjectId)

     ' recurse through data tree
     ObjectName2Recursive(ldxDocument.Object, objectIdsLeft)
     ObjectName2Recursive(ldxDocument.Application.RenderView.Object, objectIdsLeft)

 End Sub