Message 1 of 10
SaveAs Copy and Replace Component in Copy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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