04-06-2023
06:01 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
04-06-2023
06:01 PM
Thank you and others for your starting code. Here's an ilogic macro for copying the part and drawing together. Basically does the same thing as a copy design in Vault.
Change log:
1. Doesn't replace it in the assembly anymore.
2. Extension will be dropped if user puts in the extension as part of its name.
3. Workaround implemented to correctly show the initial folder
4. Commented out touching the vault addin. Undesirable behaviour for my application.
5. Close out background openings of files.
6. Changed filetype to idw instead of dwg for my purposes.
7. Commented out some iproperty stuff I didn't want for what I need.
'Adapted from darrell.wcd at https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/copy-model-and-drawing/td-p/11505007 Sub main ' Dim oVault As ApplicationAddIn ' For Each oVault In ThisApplication.ApplicationAddIns ' If oVault.DisplayName = "Inventor Vault" Then Exit For ' Next ' If oVault IsNot Nothing Then oVault.Deactivate 'SilentOperation = True ''' Set drawing extension Dim DWGType As String = ".idw" Dim BoxNote As String = "Drawing Automation" Dim filex As String 'file extension opendoc = ThisDoc.PathAndFileName() ''' Initial file dialogue box declarations oDoc = ThisDoc.Document filePath = ThisDoc.Path 'MessageBox.Show(filePath) ''' check file type and set dialog filter If oDoc.DocumentType = kPartDocumentObject Then 'oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt" filex = ".ipt" Else If oDoc.DocumentType = kAssemblyDocumentObject Then 'oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam" filex = ".iam" Else If oDoc.DocumentType = kDrawingDocumentObject Then 'oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw" filex = ".idw" End If 'doc = ThisApplication.Documents.Open(opendoc & filex,True) ''' Get current filename CurrentFilename = ThisDoc.PathAndFileName(False) oPart = ThisDoc.PathAndFileName(False) ''' Check that the drawing for this assembly can be found If Not System.IO.File.Exists(CurrentFilename & DWGType) Then MessageBox.Show("Unable to locate the drawing file below for this part:" & vbLf & vbLf & _ CurrentFilename & DWGType & vbLf & vbLf & _ "Please make sure it's in the same folder as this part, has the same name, and has the file extension. '"& DWGType & "'", BoxNote) Return End If 'define the active document oDoc = ThisDoc.Document ''' create a file dialog box Dim oFileDlg As Inventor.FileDialog = Nothing InventorVb.Application.CreateFileDialog(oFileDlg) oFileDlg.InitialDirectory = ThisDoc.Path() ' Bug since Sept 2021 causes this line to be ignored at the .ShowAs() line 'Work Around for ShowSave() bug. Thank you ThaleFUWKH (https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/file-dialog-initial-directory-stuck-on-workspace/m-p/6883307#M70261) Dim iFileName As String = ThisDoc.Path() + "\" + ThisDoc.FileName(False) 'set the file name string to use in the input box oFileDlg.FileName = iFileName 'iProperties.Value("Project", "Part Number") 'work with an error created by the user backing out of the save oFileDlg.CancelError = True Logger.Debug("oFileDlg.FileName " & oFileDlg.FileName) On Error GoTo 1: 'Error catch for if user cancels out 'specify the file dialog as a save dialog (rather than a open dialog) oFileDlg.ShowSave() 'On Error GoTo 1:'Resume Next ''' catch an empty string in the input If Err.Number <> 0 Then Logger.Debug("Error at file dialogue box") MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled") ElseIf oFileDlg.FileName <> "" Then MyFile = oFileDlg.FileName Logger.Debug("No Error for file save dialogue") ''' Get new filename (without extension) NewFileName = MyFile'filePath &"\"& FileName NewFileName = System.IO.Path.ChangeExtension(NewFileName, Nothing) 'If Str.Contains(MyFile, ".ipt") Or Str.Contains(MyFile, ".iam") Or Str.Contains(MyFile, ".idw") Or Str.Contains(MyFile, ".dwg") Then 'MyFile = Right(MyFile, (Len(MyFile) -4)) 'End If Logger.Debug("NewFileName/MyFile: " + MyFile + filex) ''' Perform Save As Of Assembly/Part ThisDoc.Document.SaveAs(NewFileName & filex, True) Logger.Debug("New file save successful") ''' Update model iProperties iNewFileName = ThisDoc.FileName(False) oDoc.PropertySets("Design Tracking Properties").Item("Part Number").Value = iNewFileName Logger.Debug("File save attempted") ''' Open the current drawing (in same folder location) Dim oDrawingDoc As DrawingDocument = ThisApplication.Documents.Open(CurrentFilename & DWGType,False) oDrawingDoc.SaveAs(NewFileName & DWGType, True) oDrawingDoc.Close oDrawingDoc = ThisApplication.Documents.Open(NewFileName & DWGType,False) Logger.Debug("File save successful :" & NewFileName & DWGType) ''' Replace reference to assembly model Dim oFD As FileDescriptor oFD = oDrawingDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor oFD.ReplaceReference(NewFileName & filex) oDrawingDoc.Update() 'oDrawingDoc.Activate() ' Perform 'Save As' on the drawing Dim newdwgname As String = NewFileName & DWGType Logger.Debug("newdwgname :" + newdwgname) oDrawingDoc.Save() ' ThisApplication.Documents.Open(oDWGPath, True) oDrawingDoc.Close Logger.Debug("Replaced model reference") Call Prop End If 1: 'ThisDoc.Document.Activate 'Call Prop 'SilentOperation = False 'oVault.Activate Logger.Debug("completed") End Sub Public Sub Prop oDoc = ThisApplication.ActiveDocument iProperties.Value("Summary", "Author") = ThisApplication.GeneralOptions.UserName iProperties.Value("Project", "Designer") = ThisApplication.GeneralOptions.UserName 'iProperties.Value("Project", "Stock Number") = "=<Part Number>" iProperties.Value("Project", "Creation Date" ) = Now InventorVb.DocumentUpdate() ' Save the document and its dependents. oDoc.Save2(True) oDoc.Save2(True) End Sub