04-05-2023
01:59 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
04-05-2023
01:59 PM
I know its old but this might help someone, this only works if you run it from within a assembly that contains the assembly you want to copy and the code is a mess.
' Display a message box to the user with instructions
Dim userResponse = MessageBox.Show("To use this function a few things to note:" & vbNewLine & _
" - You use it from the Main Assembly, not from the assembly you want to copy" & vbNewLine & _
" - It doesn't work for assemblies within assemblies", "Frametek Ilogic Copy V.1.0", MessageBoxButtons.OKCancel)
' If the user clicked OK in the message box
If userResponse = vbOK Then
' Prompt the user to pick the assembly to copy
Dim pickedAssembly As Object
pickedAssembly = ThisServer.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Pick Assembly To Copy")
' Cast the picked assembly as a Document
Dim pickedDocument As Document
pickedDocument = pickedAssembly.Definition.Document
' Get the filename of the picked document
Dim pickedDocFileName As String = Right(pickedDocument.FullDocumentName, (Len(pickedDocument.FullDocumentName) - (Left(pickedDocument.FullDocumentName, Len(pickedDocument.FullDocumentName)).LastIndexOf("\") + 1)))
MessageBox.Show(pickedDocFileName , "Title")
' Ask the user how many copies they would like
Dim numCopies = InputBox("How many times would you like to copy the assembly?", "How Many Copies?", "1")
' Set a prefix for the copied files
Dim filePrefix As Integer = 0
' Loop to create the copies
Do
filePrefix += 1
' For each copy
For i = 1 To numCopies
' Determine the save location for the copied assembly
Dim saveLocation As String
saveLocation = Left(pickedDocument.FullFileName, Len(pickedDocument.FullFileName) - 4) & "" & filePrefix & ".iam"
' If a file already exists at the save location, skip to the next iteration
If System.IO.File.Exists(saveLocation) Then
Continue Do
End If
' Save a copy of the picked document
pickedDocument.SaveAs(saveLocation, True)
' Open the copied document
Dim copiedDoc As Document
copiedDoc = ThisApplication.Documents.Open(saveLocation, False)
' Get the component definition of the copied document
Dim copiedDocCompDef As AssemblyComponentDefinition = copiedDoc.ComponentDefinition
' For each component occurrence in the copied document
For Each compOcc As ComponentOccurrence In copiedDocCompDef.Occurrences
' If the component occurrence name does not end with ":1", skip to the next iteration
If Right(compOcc.Name, 2) <> ":1" Then
Continue For
End If
' Open the referenced document of the component occurrence
Dim refDoc As Document = ThisApplication.Documents.Open(compOcc.ReferencedDocumentDescriptor.FullDocumentName, False)
' Determine the save location for the referenced document
Dim refDocSaveLocation As String = Left(refDoc.FullFileName, (Left(refDoc.FullFileName, Len(refDoc.FullFileName)).LastIndexOf("\") + 2)) & _
Left(Strings.Mid(refDoc.FullFileName, (Left(refDoc.FullFileName, Len(refDoc.FullFileName)).LastIndexOf("\") + 2)), Len(Strings.Mid(refDoc.FullFileName, (Left(refDoc.FullFileName, Len(refDoc.FullFileName)).LastIndexOf("\") + 2))) - 4) & _
"_" & filePrefix & ".ipt"
' If a file already exists at the save location, skip to the next iteration
If System.IO.File.Exists(refDocSaveLocation) Then
Continue For
End If
' Save a copy of the referenced document
refDoc.SaveAs(refDocSaveLocation, True)
' Replace the component occurrence with the copied referenced document
compOcc.Replace(refDocSaveLocation, True)
refDoc.Close()
Next
' Update and save the copied document
copiedDoc.Update()
copiedDoc.Save()
copiedDoc.Close()
Next
Loop Until filePrefix >= numCopies
End If