SaveAs Copy and Replace Component in Copy

SaveAs Copy and Replace Component in Copy

Victor.Tuguinay
Enthusiast Enthusiast
653 Views
9 Replies
Message 1 of 10

SaveAs Copy and Replace Component in Copy

Victor.Tuguinay
Enthusiast
Enthusiast

Hi! So i have this code which does a Saveas Copy of my assembly. Problem is that the component that I wanted replaced is not being replaced. It is still referencing the original component.

 

Code runs like this.. Inventor SaveAs Copy the assembly and name it "NewAssemblyName", opens the newly saved assembly, then it is supposed to replace the component "SHELL NOZZLE PIPE" or "ROOF NOZZLE PIPE" depending on the parameter called "Location" with the new component "NewPipePath".

 

 

 

'define the active document
oDoc = ThisDoc.Document

CurrentFileName = ThisDoc.PathAndFileName(False)

'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

Dim Filetype As String

'check file type and set dialog filter
If oDoc.DocumentType = kPartDocumentObject Then
    oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
    Filetype = ".ipt"
ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
    oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
    Filetype = ".iam"
ElseIf oDoc.DocumentType = kDrawingDocumentObject Then
    oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
    Filetype = ".idw"
End If

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
'oFileDlg.InitialDirectory = ModelsPath
'set the file name string to use in the input box
oFileDlg.FileName = NewAssemblyName 'without extension

'work with an error created by the user backing out of the save
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog (rather than a open dialog)
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
    'MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
    MyFile = oFileDlg.FileName
    'save the file
    oDoc.SaveAs(MyFile, True) 'True = Save As Copy & False = Save As
End If
	
	'Open the newly saved copy
	Dim oNewDoc As Document
	oNewDoc = ThisApplication.Documents.Open(MyFile)
 
 	oNewDoc.Activate()
	
	If Not oNewDoc Is Nothing Then
    oNewDoc.Activate()
	
    Dim oAssemblyDoc As AssemblyDocument
    oAssemblyDoc = oNewDoc ' Assuming oNewDoc is an AssemblyDocument

    If  oNewDoc.Parameters.Item(Location) = "SHELL" Then
        ' Replace component in the assembly
        oAssemblyDoc.ComponentOccurrences("SHELL NOZZLE PIPE").Replace(NewPipePath, True)
		
    ElseIf  oNewDoc.Parameters.Item(Location) = "ROOF" Then
        ' Replace component in the assembly
        oAssemblyDoc.ComponentOccurrences("ROOF NOZZLE PIPE").Replace(NewPipePath, True)

    MessageBox.Show("Pipe Replaced", "Title")

    ' Close the newly opened copy
    oNewDoc.Close(True)  ' Set True to save changes, or False to discard changes
End If

End If

 

0 Likes
654 Views
9 Replies
Replies (9)
Message 2 of 10

A.Acheson
Mentor
Mentor

Hi @Victor.Tuguinay 

any error messages? Can you check the local parameters in the assembly are still available with a message box or logger statement? Also maybe try retrieve the occurrence you want to replace first by name to ensure that is happening. Without your data set this can be a hard one to trouble shoot. But if you go down through the lines and determine the exact place it is erroring out. Does the replace work if you were to exit the code before the replace and do it manually? 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 10

BM_Ashraf
Advocate
Advocate

Hi,

You can use the Copy Assistant Free Add-in to do the job.
You can also Replace Components and copy the drawings too.

Autodesk Store: Copy Assistant

BlueMech :Copy Assistant (Latest version) 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

0 Likes
Message 4 of 10

jvtuguinay
Explorer
Explorer

Thanks for your response. This part seem to be not working as intended. 

Code works well from SaveAs Copy to opening the newly saved copy. 

Then it seems to go through the replacing. Then the msgbox shows up saying "Pipe Replaced". 

Then inventor saves and closes the new assembly.

 

Could it be that inventor is still referring to the original assembly by memory? Maybe the original is still stored in the background and it is still referring to it? 

 

jvtuguinay_0-1705555919617.png

 

0 Likes
Message 5 of 10

jvtuguinay
Explorer
Explorer

Thank you but it is not what I am looking for.

Message 6 of 10

BM_Ashraf
Advocate
Advocate

You might be right.

Please give it a try and replace.

'save the file
    oDoc.SaveAs(MyFile, True) 'True = Save As Copy & False = Save As
oDoc.SaveAs(MyFile, False) 'True = Save As Copy & False = Save As

if not, i can send you later a smaple code.

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!

0 Likes
Message 7 of 10

m_baczewski
Advocate
Advocate

Please delete this post

0 Likes
Message 8 of 10

m_baczewski
Advocate
Advocate

Hi, 

 

The issue is that, during the document save, you are skipping the saving process.

 

oNewDoc.Close(False)
or
oNewDoc.Close()

 

 

2024-01-18_07h30_27.png

But the biggest question for me is how you replace the part because it seems to me that there is no public member ComponentOccurrences for AssemblyDocument. In my opinion, you should perform the replacement of the element in the following way:

 

Dim oAssemblyDoc As AssemblyDocument
    oAssemblyDoc = oNewDoc ' Assuming oNewDoc is an AssemblyDocument
	
	Dim oCompDefinition As ComponentDefinition
	oCompDefinition = oAssemblyDoc.ComponentDefinition

    If  oNewDoc.Parameters.Item(Location) = "SHELL" Then
        ' Replace component in the assembly
'        oAssemblyDoc.ComponentOccurrences("SHELL NOZZLE PIPE").Replace(NewPipePath, True)
		oCompDefinition.Occurrences("SHELL NOZZLE PIPE").Replace(NewPipePath, True)
		
		
    ElseIf  oNewDoc.Parameters.Item(Location) = "ROOF" Then
        ' Replace component in the assembly
'        oAssemblyDoc.ComponentOccurrences("ROOF NOZZLE PIPE").Replace(NewPipePath, True)
		oCompDefinition.Occurrences("ROOF NOZZLE PIPE").Replace(NewPipePath, True)

    MessageBox.Show("Pipe Replaced", "Title")

    ' Close the newly opened copy
    oNewDoc.Close(False)  ' Set True to save changes, or False to discard changes

 

Message 9 of 10

charles4L34S
Contributor
Contributor

Hi @m_baczewski

 

I do the same thing @jvtuguinay is describing, I think. 

 

I create an assembly using standard components, with set browser names. 

Gusset_Browser_Name = Component.InventorComponent("GUSSET-A") 'Both Gussets use same origional part, no need to get part name for both
Gusset_Doc = Gusset_Browser_Name.Definition.Document 'Gets current part name for gusset

Then, when it comes to saving I simply do 

Gusset_Doc.SaveAs(GUSSET_FullName, False)

 This saves the standard component with a new filename and path (FullName being a path.combine of a previously selected path and an automatically generated filename) 

 

I am aware of the component replace, but I'm not really sure why I haven't used it, this just saves the part currently in the assembly as a completely new part, with a new filename at a new path.

0 Likes
Message 10 of 10

BM_Ashraf
Advocate
Advocate

Here is a sample of a working script.

Please adjust it to your needs 🙂

imports system.IO

 

Sub main

ThisApplication.SilentOperation = True
	
'Get Active Doc 
Dim oModelDoc As Document = ThisDoc.Document
'Get Full file name
Dim oModelFullFileName As String = oModelDoc.FullFileName
Dim oNewModelFullFileName As String = f_ChangeName(oModelFullFileName)
CopyDoc(oModelDoc,oNewModelFullFileName)
Dim oNewModelDoc As Document = ThisApplication.Documents.Open(oNewModelFullFileName, True)

Dim oExtension As String = System.IO.Path.GetExtension(oModelFullFileName)
Dim oOldDrawingFullFileName As String = oModelFullFileName.Replace(oExtension, ".idw")
Dim oOldDrawDoc As DrawingDocument = ThisApplication.Documents.Open(oOldDrawingFullFileName, False)
Dim oNewDrawDocDullFileName  As String =  f_ChangeName(oOldDrawingFullFileName)
CopyDoc(oOldDrawDoc, oNewDrawDocDullFileName)
oOldDrawDoc.Close(True)
Dim oNewDrawDoc As Document = ThisApplication.Documents.Open(oNewDrawDocDullFileName, False)

'Replace Reference
 Dim oFD As FileDescriptor = Nothing
 For Each oDocDescriptor As DocumentDescriptor In oNewDrawDoc.ReferencedDocumentDescriptors
     If oDocDescriptor.ReferencedFileDescriptor.FullFileName = oModelFullFileName Then
         oFD = oDocDescriptor.ReferencedFileDescriptor
         Exit For
     End If
 Next
If oFD Is Nothing Then oNewDrawDoc.Close(True) : RemoveSilent : Return
Try
    oFD.ReplaceReference(oNewModelFullFileName)
Catch 
	Err.Clear
End Try

oNewDrawDoc.Update2(True)
oNewDrawDoc.Save2(False)

oNewDrawDoc.Close(True)

End Sub
Private Function CopyDoc (oDoc As Document, oNewFullFileName As String) As Boolean

'Copy the Active Document 
oDoc.SaveAs(oNewFullFileName,True)

End Function

Private Function f_ChangeName(oFullFileName As String ) As String
'Get its Name 
Dim oFileName As String = System.IO.Path.GetFileNameWithoutExtension(oFullFileName)
'Get Extension 
Dim oExtension As String = System.IO.Path.GetExtension(oFullFileName)
'Get ModelPath
Dim oPath As String = System.IO.Path.GetDirectoryName(oFullFileName)
'New Model Name 
Dim oNewName As String = (oFileName+("_1")+oExtension)
Dim oNewFullFileName As String = System.IO.Path.Combine(oPath,oNewName)
Return  oNewFullFileName
End Function

Private Function RemoveSilent As Boolean
	ThisApplication.SilentOperation = False
End Function

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.

Blue Mech

Add-ins for Inventor!