Reference Leader's GeometryIntent

Reference Leader's GeometryIntent

River.EngumYTTWH
Enthusiast Enthusiast
296 Views
2 Replies
Message 1 of 3

Reference Leader's GeometryIntent

River.EngumYTTWH
Enthusiast
Enthusiast

I am working on a script that attaches a custom sketch symbol to welding symbols that have been placed on sheets already. Turns out that to attach it confidently to the DrawingWeldingSymbol, I must reference a GeometryIntent of the object. Unfortunately, it seems as though there is no built in way to reference the DrawingWeldingSymbol's GeometryIntent, despite being able to do so manually as a user. Specifically, it would be great to be able to create/have GeometryIntent from an arbitrary node from a Leader.

 

For some reason I am able to get the sketch symbols to attach, though I cannot consistently recognize what is allowing this. It seems like if I have the sketch symbol placed exactly on the point of a node of a Leader, it attaches to that node in a satisfactory way. As this is inconsistent, which supposedly nothing I can do to guarantee it, I am unable to use the as a solution with my current information.

 

Does anyone have any idea's on how to either utilize GeomtryIntent in this situation, or have details on how to attach sketch symbols to weld symbols without this integrated feature with consistency and reliability?

 

Code for reference, and to help future forum travelers:

Sub Main()
    If Not ActiveDocIsDwg() Then Exit Sub
    
    If Not RefreshSymbolFromLibrary("Weld No. (WIP)") Then Exit Sub

    Dim weldSymbols As ObjectCollection = FindAllWeldSymbols()
    If weldSymbols Is Nothing Then Exit Sub

    For Each weldSym As DrawingWeldingSymbol In weldSymbols
        Logger.Trace("Processing weld symbol on sheet '" & weldSym.Parent.Name & "'.")
        If Not AttachWeldNumbering(weldSym) Then
            Logger.Error("Failed to attach weld numbering to weld symbol on sheet '" & weldSym.Parent.Name & "'.")
        End If
    Next

    'TestCreateSymbol()

End Sub

Function AttachWeldNumbering(weldSym As DrawingWeldingSymbol) As Boolean
    Dim leaderPoints As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
    
    Dim weldSymLeader As Leader = weldSym.Leader
    'Dim rootNode As LeaderNode = weldSymLeader.RootNode
    'Dim geomIntent As GeometryIntent = rootNode.AttachedEntity()
    Dim attachPoint As Point2d
    'Logger.Debug("Node Count: " & weldSymLeader.AllNodes.Count)
    'For Each node As LeaderNode In weldSymLeader.AllNodes
    '    'Logger.Debug()
    '    'Logger.Debug("Node: " & node.)
    '    geomIntent = node.AttachedEntity()
    'Next

    attachPoint = weldSymLeader.AllNodes.Item(1).Position
    Dim geomIntent As GeometryIntent 
    'geomIntent = Sheet.CreateGeometryIntent(weldSymLeader.AllNodes.Item(1), attachPoint)
    'leaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(10, 8))
    'leaderPoints.Add(geomIntent)
    'leaderPoints.Add(weldSym.Position)
    leaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(attachPoint.x-4, attachPoint.y-2))
    leaderPoints.Add(attachPoint)

    Dim weldNumberSymbol As SketchedSymbol = CreateWeldNumberSymbol(leaderPoints, "A")
    Logger.Trace("Attached weld number symbol to weld symbol on sheet '" & weldSym.Parent.Name & "'.")
End Function

Function FindAllWeldSymbols() As ObjectCollection
    Dim foundSymbols As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

    For Each sheet As Sheet In ThisApplication.ActiveDocument.Sheets
        For Each weldSymbol As DrawingWeldingSymbol In sheet.WeldingSymbols
            foundSymbols.Add(weldSymbol)
            Logger.Trace("Found weld symbol on sheet '" & sheet.Name & "'.")
        Next
    Next

    Return foundSymbols
End Function

Sub TestCreateSymbol()
    Dim testLeaderPoints As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
    testLeaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(5, 4))
    testLeaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(10, 8))

    CreateWeldNumberSymbol(testLeaderPoints, "A")
End Sub

Function CreateWeldNumberSymbol(leaderPoints As ObjectCollection, prompt As String) As SketchedSymbol
    Dim sheet As Sheet = ThisApplication.ActiveDocument.ActiveSheet
    Dim insertPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(5, 4)

    weldNumberSymbol = ThisApplication.ActiveDocument.SketchedSymbolDefinitions.Item("Weld No. (WIP)")

    Dim newWeldSym As SketchedSymbol 
    newWeldSym = sheet.SketchedSymbols.AddWithLeader(weldNumberSymbol, leaderPoints, 0, 1, {prompt})
    newWeldSym.LeaderVisible = True
    Return newWeldSym
End Function

Function RefreshSymbolFromLibrary(symbolName As String) As Boolean
    Dim allSketchDefs As SketchedSymbolDefinitions = ThisApplication.ActiveDocument _
        .SketchedSymbolDefinitions

    Dim allSketchLibraries As SketchedSymbolDefinitionLibraries = allSketchDefs.SketchedSymbolDefinitionLibraries

    Dim foundSymDefLib As SketchedSymbolDefinitionLibrary = Nothing
    
    For Each sketchLib As SketchedSymbolDefinitionLibrary In allSketchLibraries
        If sketchLib.Name <> "Sketch Symbols.idw" Then
             'Logger.Trace("Skipping library: " & sketchLib.Name)
             Continue For
        End If
        Logger.Trace("Checking library: " & sketchLib.Name)
        Dim libSketchDefs As LibrarySketchedSymbolDefinitions = sketchLib.SketchedSymbolDefinitions
        For Each libDef As LibrarySketchedSymbolDefinition In libSketchDefs
            'Logger.Trace(" - Found symbol: " & libDef.Name)
            If libDef.Name = symbolName Then
                Logger.Trace("Found symbol: " & symbolName)
                foundSymDefLib = sketchLib
                Exit For
            End If
        Next
    Next

    Dim resultingSymDef As SketchedSymbolDefinition = allSketchDefs.AddFromLibrary(foundSymDefLib, symbolName, True)'True: replace Existing

    If foundSymDefLib IsNot Nothing Then Return True

    MsgBox("Symbol '" & symbolName & "' not found in library.", vbExclamation, "Symbol Not Found")
    Logger.Trace("Symbol '" & symbolName & "' not found in library.")
    Return False
End Function

Function ActiveDocIsDwg() As Boolean
    If ThisApplication.ActiveDocumentType = kDrawingDocumentObject Then
        activeDocIsDwg = True
        Logger.Trace("Active document is a drawing.")
    Else
        MsgBox("This rule only works with drawing documents.", vbExclamation, "Wrong Document Type")
        activeDocIsDwg = False
        Logger.Trace("Active document is not a drawing.")
    End If
End Function

 

0 Likes
297 Views
2 Replies
Replies (2)
Message 2 of 3

River.EngumYTTWH
Enthusiast
Enthusiast

I found what was causing the symbols to inconsistently attach. When a leader is placed within the nominal bounding rectangle of a view, that object will intelligently move with the view. I didn't realize at that point that it was moving with the view, and not the weld symbol unfortunately.

 

Additionally I found that the leader must be placed at the DrawingWeldingSymbol.Position, rather than a node of the leader. There seems to be no way to get a GeometryIntent from the DrawingWeldSymbol (other than what the symbol is attached to, which is not useful in my case). I guess this is an unfortunate aspect of DrawingWeldingSymbol's being so new (I am in 2025, and I understand that they only were added in 2024). 

0 Likes
Message 3 of 3

River.EngumYTTWH
Enthusiast
Enthusiast

Further testing has seemed to reveal that even if I have a valid GeometryIntent of the DrawingWeldingSymbol (by attaching a DrawingWeldingSymbol to another DrawingWeldingSymbol and referencing that nodes AttachedEntity), the oSheet.SketchedSymbols.AddWithLeader() function fails with an unspecified error (below). This looks like it is the nail in the coffin for achieving my goals for this unfortunately.

 

Unspecified error (0x80004005 (E_FAIL))

 

WIP code for future debuggers:

Sub Main()
    If Not ActiveDocIsDwg() Then Exit Sub
    
    If Not RefreshSymbolFromLibrary("Weld No. (WIP)") Then Exit Sub

    Dim weldSymbols As ObjectCollection = FindAllWeldSymbols()
    If weldSymbols Is Nothing Then Exit Sub

    For Each weldSym As DrawingWeldingSymbol In weldSymbols
        'Logger.Trace("Processing weld symbol on sheet '" & weldSym.Parent.Name & "'.")

        'AppendWeldNumbering(weldSym)
        'Continue For    'Skip while testing AppendWleldNumbering separately

        AttachWeldNumbering(weldSym)
    Next

    'TestCreateSymbol()

End Sub

Sub AppendWeldNumbering(weldSym As DrawingWeldingSymbol)
    Dim symDef As DrawingWeldingSymbolDefinition = weldSym.Definitions(1)
    symDef.TailNote = symDef.TailNote & vbCrLf & "W-1"
End Sub

Function AttachWeldNumbering(weldSym As DrawingWeldingSymbol) As Boolean
    'Try
        Dim leaderPoints As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
        
        'Dim weldSymLeader As Leader = weldSym.Leader
        'Dim rootNode As LeaderNode = weldSymLeader.RootNode
        'Dim geomIntent As GeometryIntent = rootNode.AttachedEntity()
        'Logger.Debug("Node Count: " & weldSymLeader.AllNodes.Count)
        'For Each node As LeaderNode In weldSymLeader.AllNodes
        '    'Logger.Debug()
        '    'Logger.Debug("Node: " & node.)
        '    geomIntent = node.AttachedEntity()
        'Next

        'Dim node As LeaderNode = weldSymLeader.RootNode
        RoundPosition(weldSym, 4)
        Dim originPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)

        Dim oldPoint As Point2d = weldSym.Leader.RootNode.Position()

        'weldSym.Leader.RootNode.Position() = originPoint
        weldSym.Position() = originPoint
        ThisApplication.ActiveDocument.Update()

        Dim attachPoint As Point2d = weldSym.Position()
        Dim geomIntent As GeometryIntent 
        'geomIntent = Sheet.CreateGeometryIntent(weldSym.RootNode.Position, Inventor.IntentTypeEnum.kPoint2dIntent)
        Logger.Trace("Nodes Found: " & weldSym.Leader.AllNodes.Count)

        For Each node As LeaderNode In weldSym.Leader.AllNodes
            Logger.Trace("Checking leader node for geometry intent attachment: " & node.Position.ToString())
            If node.AttachedEntity() IsNot Nothing Then
                geomIntent = node.AttachedEntity()
                Logger.Trace("Found geometry intent attached to leader node")
                Exit For
            End If
        Next

        if geomIntent Is Nothing Then
            Logger.Error("No geometry intent found attached to leader nodes.")
            MsgBox("No geometry intent found attached to leader nodes.", vbExclamation, "Error")
            Return False
        End If
        'geomIntent = Sheet.CreateGeometryIntent(weldSymLeader.AllNodes.Item(1), attachPoint)
        'leaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(10, 8))
        'leaderPoints.Add(weldSym.Position)
        'leaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(attachPoint.x-4, attachPoint.y-2))
        leaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(geomIntent.PointOnSheet.x-4, geomIntent.PointOnSheet.y-2))
        leaderPoints.Add(geomIntent)
        'leaderPoints.Add(attachPoint)

        'Logger.Trace("Prepared leader points for weld number symbol:" & "(" & attachPoint.x & ", " & attachPoint.y & ").")

        Dim weldNumberSymbol As SketchedSymbol = CreateWeldNumberSymbol(leaderPoints, "A")
        Logger.Trace("Attached weld number symbol to weld symbol on sheet '" & weldSym.Parent.Name & "'.")

        'weldSym.Leader.RootNode.Position() = oldPoint

        Return True
    'Catch ex As Exception
    '    Logger.Error("Error attaching weld numbering: " & ex.ToString())
    '    MsgBox("Error attaching weld numbering: " & ex.ToString(), vbExclamation, "Error")
    '    Return False
    'End Try
End Function

Sub RoundPosition(obj As DrawingWeldingSymbol, dec As Integer)
    Try
        Dim pos As Point2d = obj.Position
        'Logger.Trace("Old Position:" & "(" & pos.x & ", " & pos.y & ").")
        Dim roundedPos As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(Round(pos.x, dec), Round(pos.y, dec))
        'Logger.Trace("PreNew Position:" & "(" & roundedPos.x & ", " & roundedPos.y & ").")
        obj.Position = roundedPos
        'Logger.Trace("PostNew Position:" & "(" & obj.Position.x & ", " & obj.Position.y & ").")
    Catch ex As Exception
        Logger.Error("Error rounding position: " & ex.ToString())
        MsgBox("Error rounding position: " & ex.ToString(), vbExclamation, "Error")
    End Try
End Sub

Function FindAllWeldSymbols() As ObjectCollection
    Dim foundSymbols As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection

    For Each sheet As Sheet In ThisApplication.ActiveDocument.Sheets
        For Each weldSymbol As DrawingWeldingSymbol In sheet.WeldingSymbols
            foundSymbols.Add(weldSymbol)
            Logger.Trace("Found weld symbol on sheet '" & sheet.Name & "'.")
        Next
    Next

    Return foundSymbols
End Function

Sub TestCreateSymbol()
    Dim testLeaderPoints As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
    testLeaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(5, 4))
    testLeaderPoints.Add(ThisApplication.TransientGeometry.CreatePoint2d(10, 8))

    CreateWeldNumberSymbol(testLeaderPoints, "A")
End Sub

Function CreateWeldNumberSymbol(leaderPoints As ObjectCollection, prompt As String) As SketchedSymbol
    Dim sheet As Sheet = ThisApplication.ActiveDocument.ActiveSheet
    Dim insertPoint As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(5, 4)

    weldNumberSymbol = ThisApplication.ActiveDocument.SketchedSymbolDefinitions.Item("Weld No. (WIP)")

    Dim newWeldSym As SketchedSymbol 
    newWeldSym = sheet.SketchedSymbols.AddWithLeader(weldNumberSymbol, leaderPoints, 0, 1, {prompt})
    newWeldSym.LeaderVisible = True
    Return newWeldSym
End Function

Function RefreshSymbolFromLibrary(symbolName As String) As Boolean
    Dim allSketchDefs As SketchedSymbolDefinitions = ThisApplication.ActiveDocument _
        .SketchedSymbolDefinitions

    Dim allSketchLibraries As SketchedSymbolDefinitionLibraries = allSketchDefs.SketchedSymbolDefinitionLibraries

    Dim foundSymDefLib As SketchedSymbolDefinitionLibrary = Nothing
    
    For Each sketchLib As SketchedSymbolDefinitionLibrary In allSketchLibraries
        If sketchLib.Name <> "Sketch Symbols.idw" Then
             'Logger.Trace("Skipping library: " & sketchLib.Name)
             Continue For
        End If
        Logger.Trace("Checking library: " & sketchLib.Name)
        Dim libSketchDefs As LibrarySketchedSymbolDefinitions = sketchLib.SketchedSymbolDefinitions
        For Each libDef As LibrarySketchedSymbolDefinition In libSketchDefs
            'Logger.Trace(" - Found symbol: " & libDef.Name)
            If libDef.Name = symbolName Then
                Logger.Trace("Found symbol: " & symbolName)
                foundSymDefLib = sketchLib
                Exit For
            End If
        Next
    Next

    Dim resultingSymDef As SketchedSymbolDefinition = allSketchDefs.AddFromLibrary(foundSymDefLib, symbolName, True)'True: replace Existing

    If foundSymDefLib IsNot Nothing Then Return True

    MsgBox("Symbol '" & symbolName & "' not found in library.", vbExclamation, "Symbol Not Found")
    Logger.Trace("Symbol '" & symbolName & "' not found in library.")
    Return False
End Function

Function ActiveDocIsDwg() As Boolean
    If ThisApplication.ActiveDocumentType = kDrawingDocumentObject Then
        activeDocIsDwg = True
        Logger.Trace("Active document is a drawing.")
    Else
        MsgBox("This rule only works with drawing documents.", vbExclamation, "Wrong Document Type")
        activeDocIsDwg = False
        Logger.Trace("Active document is not a drawing.")
    End If
End Function

Function LargestWeldNumber(weldSymbols As ObjectCollection) As Integer
    Dim largestNum As Integer = 0
    Dim numPart As String
    Dim numValue As Integer

    For Each weldSym As DrawingWeldingSymbol In weldSymbols
        'Assumes weld number is in tail note and formatted like "W-1", "W-2", etc.
        numPart = Split(weldSym.Definitions(1).TailNote, "-")(1)
        If Integer.TryParse(numPart, numValue) Then
            If numValue > largestNum Then largestNum = numValue
        End If
    Next

    Return largestNum
End Function
0 Likes