Here you go. You can run it from drawing, assembly or part. It should handle it all.
Sub Main()
Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oFD As FileDialog
Call ThisApplication.CreateFileDialog(oFD)
oFD.Filter = "Inventor Files (*.idw;*.iam;*.ipt)|*.idw;*.iam;*.ipt"
oFD.FilterIndex = 1
oFD.DialogTitle = "Select folder and new filename for new drawing / assembly / file."
oFD.InitialDirectory = oDoc.FullFileName
oFD.ShowSave()
Dim oNewName As String = oFD.FileName
If oNewName = vbNullString Then Exit Sub
Dim FNP As Integer = InStrRev(oNewName, ".", -1)
oNewName = Left(oNewName, FNP)
If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
FromDrawing(oDoc, oNewName)
Else
FromPart(oDoc, oNewName)
End If
End Sub
Sub FromDrawing(oDoc As DrawingDocument, oNewName As String)
Dim oSheet As Sheet = oDoc.Sheets.Item(1)
Dim pNewName As String = vbNullString
If oSheet.DrawingViews.Count > 0 Then
Dim oView As DrawingView = oSheet.DrawingViews.Item(1)
Dim pDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
pNewName = SavePart(pDoc, oNewName)
End If
SaveDrawing(oDoc, oNewName, pNewName)
End Sub
Sub FromPart(oDoc As DrawingDocument, oNewName As String)
Dim OldName As String = oDoc.FullFileName
Dim pNewName As String = SavePart(oDoc, oNewName)
Dim FNP As Integer = InStrRev(OldName, ".", -1)
OldName = Left(OldName, FNP) & "idw"
Dim pDoc As DrawingDocument = ThisApplication.Documents.Open(OldName, True)
SaveDrawing(pDoc, oNewName, pNewName)
End Sub
Function SavePart(oDoc As DrawingDocument, oNewName As String) As String
Dim Ext As String = GetExt(oDoc)
If Ext = vbNullString Then Return vbNullString
Dim NewName As String = NewName & Ext
oDoc.SaveAs(NewName, False)
Return NewName
End Function
Sub SaveDrawing(oDoc As DrawingDocument, oNewName As String, pNewName As String)
Dim NewName As String = NewName & "idw"
oDoc.SaveAs(NewName, False)
oDoc.Close(True)
If pNewName = vbNullString Then Exit Sub
Dim nDoc As DrawingDocument = ThisApplication.Documents.Open(NewName, True)
Dim oRefFile As Inventor.FileDescriptor = nDoc.File.ReferencedFileDescriptors(1)
Try
oRefFile.ReplaceReference(pNewName)
Catch
End Try
nDoc.Save()
End Sub
Function GetExt(oDoc As Document) As String
Select Case oDoc.DocumentType
Case DocumentTypeEnum.kPartDocumentObject: Return "ipt"
Case DocumentTypeEnum.kAssemblyDocumentObject: Return "iam"
End Select
Return vbNullString
End Function
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods