I have tweaked the ilogic somewhat succesfully. A few outstanding issues:
1. The new files no longer contain any rules, forms, or parameters, these need to be kept from the original.
2. If the top level assembly contains a subassembly, the rule fails, I think something is weird about nesting the oAsmSaveAs function inside itself
3. If the assembly contains multiple occurrences of the same part, it wants to make additional copies rather than replacing all occurrences.
4. I'm open to code improvements, I suspect there are better ways of changing paths and file extensions: maybe "ThisDoc.ChangeExtension"? Not sure on how to implement though
Sub main
Dim oDoc As Document = ThisApplication.ActiveDocument
'Set directory for new file creation
Dim dlgFolder = New FolderBrowserDialog()
dlgFolder.SelectedPath = "F:\AFE Crane\2022 Jobs\0 end stop target"
dlgFolder.ShowNewFolderButton = True
dlgFolder.Description = "Select target folder for new files"
dlgFolder.ShowDialog()
Dim oPath As String = dlgFolder.SelectedPath & "\"
'is file a part or assembly
If oDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
Dim oNewPartName As String
oNewPartName = oGetFileName(oDoc, oPath) 'run oGetFileName function
oDocSaveAs(oDoc, oNewPartName) 'run oDocSaveAs function
ElseIf oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
Dim oAsmDoc As AssemblyDocument
oAsmDoc=oDoc
oAsmSaveAs(oAsmDoc, oPath) 'run oAsmSaveAs function
End If
End Sub
Private Function oAsmSaveAs(oDoc As AssemblyDocument,oPath As String)
Dim oFileName As String = oGetFileName(oDoc, oPath) 'new assembly name
Dim oNewAsmDoc As AssemblyDocument
'check if same new file already exist and that oGetFileName function was not cancelled and did not return a blank string
If Not System.IO.File.Exists(oFileName) And Not oFileName = Nothing Then
oNewAsmDoc=ThisApplication.Documents.Add(Inventor.DocumentTypeEnum.kAssemblyDocumentObject,,True)
oNewAsmDoc.SaveAs(oFileName, False)
Dim oCurrentDrw As String
oCurrentDrw = Left(oDoc.FullFileName, InStrRev(oDoc.FullFileName, ".")) & "idw"
If System.IO.File.Exists(oCurrentDrw) 'does current assembly have a drawing? Save it and update references
Dim oNewDrw As String
oNewDrw = Left(oFileName, InStrRev(oFileName, ".")) & "idw"
'open original drawing
oDestinationDoc = ThisApplication.Documents.Open(oCurrentDrw)
oDestinationDoc.saveas(oNewDrw,True)
oDestinationDoc.Close
'open new drawing
oDestinationDoc = ThisApplication.Documents.Open(oNewDrw)
Dim oDocDescriptor As DocumentDescriptor
oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
Dim oFileDescriptor As FileDescriptor
oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
oFileDescriptor.ReplaceReference(oFileName)
oDestinationDoc.Update()
oDestinationDoc.Save
oDestinationDoc.Close
End If
Else
MessageBox.Show("Operation Cancelled")
Exit Function
End If
Dim oNewdef As AssemblyComponentDefinition
oNewdef = oNewAsmDoc.ComponentDefinition 'create new assembly object
Dim oNewCC As ComponentOccurrence
Dim odef As AssemblyComponentDefinition
odef = oDoc.ComponentDefinition
Dim oCC As ComponentOccurrence
Dim oNewPartName As String
For Each oCC In odef.Occurrences
If oCC.DefinitionDocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
Dim oPrt As PartDocument
oPrt = oCC.Definition.Document
oNewPartName = oGetFileName(oPrt, oPath)
Call oDocSaveAs(oPrt,oNewPartName)
oNewCC = oNewdef.Occurrences.Add(oNewPartName, oCC.Transformation)
oNewCC.Grounded = True
oNewCC.Name = oCC.Name
ElseIf oCC.DefinitionDocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
Dim oSubAsm As AssemblyDocument
oSubAsm = oCC.Definition.Document
Call oAsmSaveAs(oSubAsm, oPath)
oNewPartName = oGetFileName(oSubAsm, oPath)
oNewCC = oNewdef.Occurrences.Add(oNewPartName, oCC.Transformation)
oNewCC.Grounded = True
oNewCC.Name = oCC.Name
End If
Next
oNewAsmDoc.Close
End Function
Private Function oDocSaveAs(ByVal oDoc As Document,ByVal oNewPartName As String)
If Not System.IO.File.Exists(oNewPartName) And Not oNewPartName = Nothing Then
oDoc.SaveAs(oNewPartName, True)
Dim oCurrentDrw As String
oCurrentDrw = Left(oDoc.FullFileName, InStrRev(oDoc.FullFileName, ".")) & "idw"
If System.IO.File.Exists(oCurrentDrw)
Dim oNewDrw As String
oNewDrw = Left(oNewPartName, InStrRev(oNewPartName, ".")) & "idw"
'open original drawing
oDestinationDoc = ThisApplication.Documents.Open(oCurrentDrw)
oDestinationDoc.saveas(oNewDrw,True)
oDestinationDoc.Close
'open new drawing
oDestinationDoc = ThisApplication.Documents.Open(oNewDrw)
Dim oDocDescriptor As DocumentDescriptor
oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1)
Dim oFileDescriptor As FileDescriptor
oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor
oFileDescriptor.ReplaceReference(oNewPartName)
oDestinationDoc.Update()
oDestinationDoc.Save
oDestinationDoc.Close
End If
Else
MessageBox.Show("File not saved")
Exit Function
End If
End Function
Private Function oGetFileName(ByVal oDoc As Document,ByVal oPath As String) As String
oGetFileName = Nothing
Dim oDesc As String = oDoc.PropertySets.Item("Design Tracking Properties").Item("Description").Value
'determine file path and name parts
Dim opartName As String
opartName = oDoc.FullFileName 'full file name with path and extension
Dim ostart As Integer = InStrRev(opartName, "\")
Dim oend As Integer = InStrRev(opartName, ".") -1
Dim oFullName As String = Right(opartName, Len(opartName) -ostart) 'file name with extension
Dim oFilesuffix As String = Right(opartName, Len(opartName) -oend) 'extension
Dim oFileNameBody As String = Left(oFullName, Len(oFullName)-(Len(opartName) - oend)) 'file name without extension
Dim oInput As String
Dim TryFileName As String
While oGetFileName = Nothing
oInput = InputBox("Description: " & oDesc _ 'launch input box for new file name, line 1
& vbCrLf & " " _ 'line 2
& vbCrLf & "Source Path: " & oDoc.FullFileName & "\" _ 'line 3
& vbCrLf & " " _ 'line 4
& vbCrLf & "Target Path: " & oPath _ 'line 5
& vbCrLf & " " _ 'line 6
& vbCrLf & "New File name: ", "Save As", oFileNameBody & "_copy") 'line7, intial input box content
TryFileName = oPath & oInput & oFilesuffix
If oInput = "" Then
Exit Function
Else
If System.IO.File.Exists(TryFileName) Then
MessageBox.Show("The file already exists")
Else
oGetFileName = TryFileName
End If
End If
End While
End Function