Replace References on mulit sheet drawing with ILogic

Replace References on mulit sheet drawing with ILogic

j.brodersen
Enthusiast Enthusiast
532 Views
5 Replies
Message 1 of 6

Replace References on mulit sheet drawing with ILogic

j.brodersen
Enthusiast
Enthusiast

Hi all,

 

I spent hours to find a solution for my "problem" but unfortunately without any luck.

 

I found the rule below in another post and customised it so far, the only thing I can't get working is the Replace References part in the "privat sub".

We are working with mulit sheet drawings and I want the rule to go through each sheet and replace the referenced models which has been created in the first part of the rule but I can only get the rule to change/replace the reference on the first sheet.

 

I tried already to change the referenceddocuemntdiscriptions.item(1), added a "for each ... in .." but only with the result in getting the ERROR message.

 

Can some one either point out to me what I need to change to get the rule working or change it for me?

Sub Main()

 Dim Path As String = ThisDoc.Path & "\"
 
  Dim tPath As String = ThisDoc.WorkspacePath + "\Test Ordner\Bauteile & -gruppen\"

 Dim FileChange As Boolean = False

 

 InvDoc = ThisDoc.Document

 Dim refDocs As DocumentsEnumerator = InvDoc.AllReferencedDocuments 

 Dim refDoc As Document


 'MessageBox.show(refDoc.DisplayName)

 
Dim ext As String
 'input box to enter new file name

ext = InputBox("What is the ending for the files?", "New File Name", "-")

 'NewFileName = InputBox("What is the new file name for " & refDoc.DisplayName & "?", "New File Name", iProperties.Value(refDoc.DisplayName, "Project", "Part Number"))

   If ext = 0 Then 
   
   Exit Sub
   
   End If

     'if the new file name is blank then use the same name

     If ext = "" Then

         MessageBox.Show("Fehler darf nicht leer sein, bitte eine engabe machen", "Fehler")

     End If


 For Each refDoc in refDocs
 
 

     If refDoc.DisplayName.EndsWith(".ipt") = True Then

         refDoc.DisplayName = refDoc.DisplayName.substring(0,refDoc.DisplayName.length-4)

         'MessageBox.show(NewFileName)

     End If


NewFileName = refDoc.DisplayName + ext

     If refDoc.DisplayName <> NewFileName & ".ipt" Then

         If System.IO.File.Exists(tPath & NewFileName & ".ipt") Then

             Component.Replace(refDoc.DisplayName, tPath & NewFileName & ".ipt", True)

         Else

             Dim oldName As String = refDoc.DisplayName + ".ipt"
			 
             'if it does not exist then recreate the file

             refDoc.saveas(tPath & NewFileName & ".ipt",False)

             Call UpdateDrawing(Path, oldName, NewFileName)

         End If

       

         FileChange = True

     End If

 Next

 

 If FileChange = True Then

     NewAssemblyName = refDoc.DisplayName + ext

   

     If NewAssemblyName <> "" Then

         Dim oldName As String = ThisDoc.FileName(True) 'include extension

         ThisDoc.Document.SaveAs(tPath & NewAssemblyName & ".iam" , False)

         Call UpdateDrawing(Path, oldName, NewAssemblyName)

     End If

 End If

 

 iLogicVb.UpdateWhenDone = True

 

 End Sub

 

 Private Sub UpdateDrawing (ByVal Path As String, ByVal oldName As String, ByVal newName As String)
 
	Dim zPath As String = ThisDoc.WorkspacePath + "\Test Ordner\zeichnungen\"
    Dim tPath As String = ThisDoc.WorkspacePath + "\Test Ordner\Bauteile & -gruppen\"
	 
     Dim oDestinationDoc As DrawingDocument

     Dim strFileExtension As String 
	 
	If oldName.EndsWith(".ipt") Or oldName.EndsWith("iam") Then	 
	strFileExtension =  oldName.substring(oldName.Length-4,4)
	End If

	If oldName.EndsWith(".ipt") Or oldName.EndsWith("iam") Then	 
     oldName = oldName.substring(0,oldName.Length-4)
	 End If


     Dim sFileName As String = Path & oldName & ".idw"

   

     Try

         If System.IO.File.Exists(sFileName) Then

             oDestinationDoc = ThisApplication.Documents.Open(sFileName)

             oDestinationDoc.saveas(zPath & newName & ".idw",False)
			 

             Dim oDocDescriptor As DocumentDescriptor
			 
             oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
   
             Dim oFileDescriptor As FileDescriptor
			 
             oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor

           
             oFileDescriptor.ReplaceReference(tPath & newName & strFileExtension)

             oDestinationDoc.Update()
		
             oDestinationDoc.Close

         End If

     Catch

         oDestinationDoc.Close

         MessageBox.Show("error")

     End Try

 

 End Sub



 

 

0 Likes
533 Views
5 Replies
Replies (5)
Message 2 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

@j.brodersen,

 

Please provide non confidential files to investigate.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 6

j.brodersen
Enthusiast
Enthusiast

Hi

 

I've tried now to uploade the files from my pc at work and at home but it wound let me!?

Is there another way how I can provide the files?

 

 

 

 

 

 

0 Likes
Message 4 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

@j.brodersen,

 

Sorry for late reply,

 

Hoping that below iLogic code may be helpful.

 

Sub Main()
    Call ReplaceFileReference(ThisDoc.Document, "C:\PartA.ipt", "C:\PartB.ipt")
End Sub

Sub ReplaceFileReference(oDoc as Document, oRefToRemove As String, oRefToInclude As String)
    For Each oFD In oDoc.File.ReferencedFileDescriptors
        If oFD.FullFileName = oRefToRemove
             oFD.ReplaceReference(oRefToInclude)
        End if
    Next
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 6

j.brodersen
Enthusiast
Enthusiast

Hi 

 

no problem and thank you for your reply.

 

I tried to implement your code in my rule but unfortunately with out the result I was hoping for.

I've tried a few things to day and came up with the code below which worked the first time I ran the rule, it just showed the error msgbox in the end. But now on the second and third try it just changed again the first sheet and ignored the other two, do you have any idea why?

 

Sub Main

[...] 

End sub

Private Sub UpdateDrawing (ByVal Path As String, ByVal oldName As String, ByVal newName As String)
 
	Dim zPath As String = ThisDoc.WorkspacePath + "\Test Ordner\zeichnungen\"
    Dim tPath As String = ThisDoc.WorkspacePath + "\Test Ordner\Bauteile & -gruppen\"
	 
     Dim oDestinationDoc As DrawingDocument

     Dim strFileExtension As String 
	 
	If oldName.EndsWith(".ipt") Or oldName.EndsWith("iam") Then	 
	strFileExtension =  oldName.substring(oldName.Length-4,4)
	End If

	If oldName.EndsWith(".ipt") Or oldName.EndsWith("iam") Then	 
     oldName = oldName.substring(0,oldName.Length-4)
	 End If


     Dim sFileName As String = Path & oldName & ".idw"

   

     Try

         If System.IO.File.Exists(sFileName) Then

             oDestinationDoc = ThisApplication.Documents.Open(sFileName)

             oDestinationDoc.saveas(zPath & newName & ".idw",False)
			 

			 For Each Sheet In oDestinationDoc.Sheets
			 MessageBox.Show(Sheet.Name)
			 Sheet.activate

			 oViews = oDestinationDoc.ActiveSheet.DrawingViews
			 Dim oView As DrawingView
			 For Each oView In oViews

             Dim oDocDescriptor As DocumentDescriptor
			 
             oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
   
             Dim oFileDescriptor As FileDescriptor
			 
             oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor

           
             oFileDescriptor.ReplaceReference(tPath & newName & strFileExtension)

	 Next 
			 Next

             oDestinationDoc.Update()
		
             oDestinationDoc.Close

         End If

     Catch

         oDestinationDoc.Close

         MessageBox.Show("error")

     End Try

 

 End Sub

Thanks for your help so far and regards

 

Johann

0 Likes
Message 6 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support

@j.brodersen,

 

Try below iLogic code.

Sub Main()
    Dim oDoc As DrawingDocument
    oDoc = ThisApplication.ActiveDocument
    
    Dim oDescriptor As DocumentDescriptor
    For Each oDescriptor In oDoc.ReferencedDocumentDescriptors
        If oDescriptor.ReferencedDocument.DisplayNameOverridden = False Then
            Call oDescriptor.ReferencedFileDescriptor.ReplaceReference("C:\Temp\Newpath\" & oDescriptor.ReferencedDocument.DisplayName)
        Else
            Call oDescriptor.ReferencedFileDescriptor.ReplaceReference("C:\Temp\Newpath\" & oDescriptor.ReferencedFileDescriptor.RelativeFileName)
        End If
    Next
    
    Call oDoc.SaveAs("C:\Temp\NewPath\" & oDoc.DisplayName, True)
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network