Copy and Replace Drawing Sheet/Reference

Sometimes while we are working on a drawing we have multiple parts that are very similar to each other. In fact many times these parts were created by performing a ‘Save Copy As’ command to create a new instance of the same part. Inventor 2013 brought the very much welcomed ‘Replace Model Reference’ command. This command allowed us to reuse our drawing by just replacing the underlying model reference.

The new command was great. But after using it for a while there was an issue. If we had multiple parts that could benefit from ‘Replace Model Reference’ in the same document we couldn’t copy the sheet so easily. But there was a way to do it. Since it could be done manually there may be a way to automate it as well. Let’s document our steps and try to automate them.

Goal: Copy a sheet to the same document and replace the model reference on the new sheet.

Manual Steps:

1. Copy the original sheet.

2. Create a new drawing document.

3. Paste the original sheet into the new document.

4, Activate the new sheet.

image

5. Replace the model reference.

image

5. Copy the new sheet with the replaced model reference.

6. Paste the new sheet in the original drawing document.

7. Activate the new sheet.

 

Automating the Steps

The automation steps include, copy, new drawing, paste, replace, copy, old drawing, paste. After the last step the new drawing is no longer needed.

Code Listing:

Sub Main()
   
'Check that we are in a drawing document.
    If ThisDoc.Document.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
       
'Get a reference to the current drawing.
        Dim dwgDoc As DrawingDocument = ThisDoc.Document
       
       
'Check that we have atleast one view on the sheet.
        If dwgDoc.ActiveSheet.DrawingViews.Count > 0 Then
           
           
'Get a reference to the original model in the view.
            Dim compDoc As Document = dwgDoc.ActiveSheet.DrawingViews(1).ReferencedDocumentDescriptor.ReferencedDocument
           
           
'Get the filename of the replacement model
            Dim newFileName As String = GetNewReference() 'Sub to show dialog and get file name.
           
           
'Open the replacement model.

            Dim newFile As Document
           
If Not String.IsNullOrEmpty(newFileName) Then
               
Try
                   
newFile = ThisApplication.Documents.Open(newFileName, False)
               
Catch
                   
MessageBox.Show("Could not open target file.")
               
End Try
               
               
'Check to see if we have valid documents to work with.
                If Not newFile Is Nothing Then
                   
Dim okContinue As Boolean = True
                   
If Not newFile.InternalName = compDoc.InternalName Then
                       
'Display warning if inner names of models do not math.
                        msgResult = MessageBox.Show("Warning, files do not match." & vbCrLf & " Results can be unpredictable." & vbCrLf & _
                           
"Do you want To Continue", "Copy/Replace Sheet", MessageBoxButtons.YesNo)
                       
If Not msgResult = DialogResult.Yes Then
                           
okContinue = False
                       
End If
                   
End If
                   
If okContinue Then
                       
Dim newDwg As DrawingDocument
                       
Try
                           
'Create the new drawing document.
                            newDwg = ThisApplication.Documents.Add(kDrawingDocumentObject, Nothing, False)
                           
                           
'Copy the active sheet to the new document.
                            Dim newSheet As Sheet = dwgDoc.ActiveSheet.CopyTo(newDwg)
                           
                           
'Replace the model reference on the new sheet with the replacement reference
                            newSheet.DrawingViews(1).ReferencedDocumentDescriptor. _

                            ReferencedFileDescriptor.ReplaceReference(newFileName)
                           
                           
'Copy the sheet back to the original drawing.
                            Dim copiedSheet As Sheet = newSheet.CopyTo(dwgDoc)
                           
newDwg.Close(True)
                           
                           
'Activate the new sheet.
                            copiedSheet.Activate()
                           
InventorVb.DocumentUpdate()
                       
Catch
                           
MessageBox.Show("Error occurred copying sheet.")
                       
End Try
                   
End If
               
End If
           
End If
       
End If
   
End If
End Sub


Function GetNewReference() As String
   
' Create a new FileDialog object.
    Dim oFileDlg As Inventor.FileDialog
   
Call ThisApplication.CreateFileDialog(oFileDlg)

   
' Define the filter to select part and assembly files or any file.
    oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"

   
' Define the part and assembly files filter to be the default filter.
    oFileDlg.FilterIndex = 1

   
' Set the title for the dialog.
    oFileDlg.DialogTitle = "Copy/Replace Sheet - Select Replacement Model"

   
' Set the initial directory that will be displayed in the dialog.
    oFileDlg.InitialDirectory = ThisDoc.Path

   
' Set the flag so an error will be raised if the user clicks the Cancel button.
    oFileDlg.CancelError = True

   
' Show the open dialog.  The same procedure is also used for the Save dialog.
    ' The commented code can be used for the Save dialog.
    Try
       
oFileDlg.ShowOpen
   
Catch
       
'MessageBox.Show("User cancelled out of dialog", "Replace Reference")
    Finally
       
If Not oFileDlg.FileName = "" Then
           
'MessageBox.Show("File " & oFileDlg.FileName & " was selected.", "Replace Reference")
            GetNewReference = oFileDlg.FileName
       
End If
   
End Try
End Function

After the rule is run, select a view and update. The drawing will update to the replacement references.

With a little bit of error checking for nice error messages and sanity checks we have a new command that will copy and replace the model reference.