iLogic Rule to Resolve Broken Links in Drawing

iLogic Rule to Resolve Broken Links in Drawing

CA_Wald
Explorer Explorer
130 Views
1 Reply
Message 1 of 2

iLogic Rule to Resolve Broken Links in Drawing

CA_Wald
Explorer
Explorer

Hello,

 

I am having trouble using iLogic to Resolving broken links in a drawing file. I have a code that works but only half way. The first pic is before i run the rule and the second pic is what happens after i run the rule. The rule fixes the broken link to the part, but the drawing views are not fixed. When i save and close the part and then reopen it, the drawing views fix them self. 

 

I would like to have help fixing my code so that it also solves the drawing views. 

1.png

CA_Wald_0-1747748184255.png

Sub Main()
    Dim oDrawDoc As DrawingDocument = ThisDoc.Document
    Dim oApp As Inventor.Application = ThisApplication

    Dim drawingFolder As String = System.IO.Path.GetDirectoryName(oDrawDoc.FullFileName)
    Dim drawingBaseName As String = System.IO.Path.GetFileNameWithoutExtension(oDrawDoc.FullFileName)
    Dim oFileDescriptors = oDrawDoc.File.ReferencedFileDescriptors

    Dim targetPart As String = System.IO.Path.Combine(drawingFolder, drawingBaseName & ".ipt")
    Dim targetAsm As String = System.IO.Path.Combine(drawingFolder, drawingBaseName & ".iam")

    Dim newModelPath As String = ""
    Dim updatedRefs As New List(Of String)
    Dim failedRefs As New List(Of String)
    Dim skippedRefs As New List(Of String)

    ' ---- Step 1: Fix file references ----
    For Each oFD As FileDescriptor In oFileDescriptors
    Dim refPath As String = oFD.FullFileName
    Dim refFolder As String = System.IO.Path.GetDirectoryName(refPath)
    Dim refFileName As String = System.IO.Path.GetFileNameWithoutExtension(refPath)

    ' Replace if folder is different OR file name does not match drawing base name
    If String.Compare(refFolder, drawingFolder, True) <> 0 OrElse _
       String.Compare(refFileName, drawingBaseName, True) <> 0 Then

        If System.IO.File.Exists(targetPart) Then
            oFD.ReplaceReference(targetPart)
            newModelPath = targetPart
            updatedRefs.Add(oFD.FullFileName & " → " & targetPart)
        ElseIf System.IO.File.Exists(targetAsm) Then
            oFD.ReplaceReference(targetAsm)
            newModelPath = targetAsm
            updatedRefs.Add(oFD.FullFileName & " → " & targetAsm)
        Else
            failedRefs.Add(oFD.FullFileName & " (Tried: " & targetPart & " and " & targetAsm & ")")
        End If
    Else
        skippedRefs.Add(refPath)
    End If
Next

    ' ✅ Update all sheets if references were changed
    If updatedRefs.Count > 0 Then
        For Each oSheet As Sheet In oDrawDoc.Sheets
            oSheet.Update()
        Next
    End If


    ' ---- Final document update ----
    If updatedRefs.Count > 0 Then oDrawDoc.Update()

    ' ---- Summary message ----
    Dim msg As String = ""
    If updatedRefs.Count > 0 Then
        msg &= "✅ Updated file references:" & vbCrLf & String.Join(vbCrLf, updatedRefs) & vbCrLf & vbCrLf
    End If
    If failedRefs.Count > 0 Then
        msg &= "❌ Failed to find replacements:" & vbCrLf & String.Join(vbCrLf, failedRefs) & vbCrLf & vbCrLf
    End If
    If skippedRefs.Count > 0 Then
        msg &= "ℹ️ Already in correct folder:" & vbCrLf & String.Join(vbCrLf, skippedRefs)
    End If
    If msg = "" Then msg = "No references found."

    MessageBox.Show(msg, "Reference & View Replacement Summary")
	
End Sub

 

0 Likes
131 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor

Hi @CA_Wald.  Sometimes these references need to be updated in several steps, and sometimes even updated a second time.  For example, when replacing the file reference, your telling the drawing to reference a different model file that it was not referencing before.  So, that other model file needs to be opened (invisibly), then most likely updated.  Then the drawing document most likely needs to be updated, because it was a document level file reference that was changed.  Then the individual sheets may need to be updated, to force the view geometry updates, and checking all the 'geometry intent' related stuff, where annotations may be attached to view geometry, and making sure all that stuff gets reattached to the new geometry.  It may even help to open the other model document before replacing that file reference, which may make the change go smoother.

 

For example, I have manually:

  • opened a drawing
  • saw lightning bolt(s) on views
  • clicked the main update button, which updated the views
    • That fixed that problem, but exposed other problems.
    • Now have lightning bolts next to model nodes under the views.
    • When you hover the symbols, it says model needs to be updated.
  • Right-click on that and tell it to update the model.
    • That fixed that problem, then exposed another problem.
    • Views now need to be updated again
  • Clicked main update button again
    • Finally nothing else need to be updated

Edit:  If the models involved have multiple ModelStates in them, and some views in the drawing are set to different ModelStates, that can complicate things further.  Then you have to pay attention to the FullDocumentName, instead of just the FullFileName.  The FullDocumentName will start with the FullFileName, then sometimes includes extra information about which ModelState 'member' within that Document is being referred to, within "<" & ">" brackets, just after the file extension, with no space.  If the other file does not have those same ModelStates, that may cause the file replacement to not work out well also.  With ModelStates, we can have a single 'File' on disk, but it can contain multiple Documents...potentially one for each ModelState.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes