Message 1 of 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
I have the following routine which runs in its own application that is started using Process.start(AppName) from the Inventor ribbonbar:
''' <summary> ''' Calls our changereference method and includes the path, projectprefix and new folder path. ''' </summary> ''' <param name="selectedItem"></param> Private Sub CopyFiles(selectedItem As String) 'MessageBox.Show(selectedItem.name + " " + selectedItem.path) '<-debugging ChangeReferenceApprentice(selectedItem, NewProject.Prefix, TBNewPath.Text) MessageBox.Show("Completed copy design") 'get the existing Inventor application object or start one. Dim InventorApp As Inventor.Application = GetApplicationObject() If Not InventorApp.Visible = True Then InventorApp.Visible = True End If InventorApp.Documents.Open(newParentDocumentName, True) End Sub 'start of Apprentice Code Private Sub ChangeReferenceApprentice(ByVal ParentDocumentName As String, ByVal ProjectPrefix As String, ByVal ProjectPath As String) Dim oApprentice As New ApprenticeServerComponent ' Open a document. Dim oDoc As ApprenticeServerDocument = oApprentice.Open(ParentDocumentName) If oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then ChangeDrawingReference(ParentDocumentName, ProjectPrefix, ProjectPath) End If If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then ChangeAssemblyReference(ParentDocumentName, ProjectPrefix, ProjectPath) End If If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then MessageBox.Show("This tool is not designed to work with individual Part files.") Exit Sub End If End Sub
Private Sub ChangeAssemblyReference(ByVal ParentDocumentName As String, ByVal projectprefix As String, ByVal ProjectPath As String) Dim oApprentice As New ApprenticeServerComponent ' Open a document. Dim oDoc As ApprenticeServerDocument = oApprentice.Open(ParentDocumentName) For Each oRefFileDesc As FileDescriptor In oDoc.File.ReferencedFileDescriptors If oRefFileDesc.FullFileName.Contains(My.Settings.ContentCentrePath) Then Continue For 'this line should recursively affect sub assemblies and their part files except it doesn't work for drawings currently. If oRefFileDesc.ReferencedFileType = FileTypeEnum.kAssemblyFileType Then ChangeAssemblyReference(oRefFileDesc.FullFileName, projectprefix, ProjectPath) End If Dim oldFilename As String = System.IO.Path.GetFileName(oRefFileDesc.FullFileName) Dim newFilename As String = projectprefix + My.Settings.ProjectDelimiter + oldFilename Dim oldpath As String = System.IO.Path.GetDirectoryName(oRefFileDesc.FullFileName) Dim newfileref As String = ProjectPath + "\" + newFilename 'copy old to new If Not IO.File.Exists(newfileref) Then IO.File.Copy(oRefFileDesc.FullFileName, newfileref) End If ' Replace the reference. oRefFileDesc.ReplaceReference(newfileref) '<- errors here if you open an assembly file referenced by a drawing. Next ' Save the assembly. newParentDocumentName = System.IO.Path.GetFileName(ParentDocumentName) newParentDocumentName = ProjectPath + "\" + projectprefix + My.Settings.ProjectDelimiter + newParentDocumentName oApprentice.FileSaveAs.AddFileToSave(oDoc, newParentDocumentName) oApprentice.FileSaveAs.ExecuteSaveAs() End Sub
Private Sub ChangeDrawingReference(ByVal ParentDocumentName As String, ByVal ProjectPrefix As String, ByVal ProjectPath As String) Dim oApprentice As New ApprenticeServerComponent Dim oDoc As ApprenticeServerDocument = oApprentice.Open(ParentDocumentName) For Each oRefFileDesc As FileDescriptor In oDoc.File.ReferencedFileDescriptors If oRefFileDesc.FullFileName.Contains(My.Settings.ContentCentrePath) Then Continue For 'this line should recursively affect sub assemblies and their part files except it doesn't work for drawings currently.
'commented section If oRefFileDesc.ReferencedFileType = FileTypeEnum.kAssemblyFileType Then ChangeAssemblyReference(oRefFileDesc.FullFileName, ProjectPrefix, ProjectPath) End If
'end commented section Dim oldFilename As String = System.IO.Path.GetFileName(oRefFileDesc.FullFileName) Dim newFilename As String = ProjectPrefix + My.Settings.ProjectDelimiter + oldFilename Dim oldpath As String = System.IO.Path.GetDirectoryName(oRefFileDesc.FullFileName) Dim newfileref As String = ProjectPath + "\" + newFilename 'copy old to new If Not IO.File.Exists(newfileref) Then IO.File.Copy(oRefFileDesc.FullFileName, newfileref) End If ' Replace the reference. oRefFileDesc.ReplaceReference(newfileref) Next ' Save the assembly. newParentDocumentName = System.IO.Path.GetFileName(ParentDocumentName) newParentDocumentName = ProjectPath + "\" + ProjectPrefix + My.Settings.ProjectDelimiter + newParentDocumentName oApprentice.FileSaveAs.AddFileToSave(oDoc, newParentDocumentName) oApprentice.FileSaveAs.ExecuteSaveAs() End Sub Function GetApplicationObject() As Inventor.Application Dim application As Inventor.Application ' Check whether there is an Outlook process running. If Process.GetProcessesByName("OUTLOOK").Count() > 0 Then ' If so, use the GetActiveObject method to obtain the process and cast it to an Application object. application = DirectCast(Marshal.GetActiveObject("Inventor.Application"), Inventor.Application) Else ' If not, create a new instance of Outlook and log on to the default profile. application = CreateObject("Inventor.Application") 'Dim ns As Outlook.NameSpace = application.GetNamespace("MAPI") 'ns.Logon("", "", Missing.Value, Missing.Value) 'ns = Nothing End If ' Return the Outlook Application object. Return application End Function
This routine works fine if I work with an Assembly that contains only parts, and with a drawing it works if I comment out the section in "ChangeDrawingReference" that recursively opens a referenced assembly.
The error message you get from the routine when it does error is tantamount to useless.
Can anyone offer an insight?
Thanks,
Alex.
----------------------------------------------------------------
Alex Fielder
Inventor Expert
https://github.com/alexfielder/
LinkedIn - Github Inventor Extension Server - Bonkers polygon iLogic thing
Top ten iLogic Tips - API Shortcut In Google Chrome - Assembly Extrusion Example
Alex Fielder
Inventor Expert
https://github.com/alexfielder/
LinkedIn - Github Inventor Extension Server - Bonkers polygon iLogic thing
Top ten iLogic Tips - API Shortcut In Google Chrome - Assembly Extrusion Example
Solved! Go to Solution.