Inventor supports the “Open Drawing” command that you can access from the context menu when clicking on a part or assembly node in the browser.
The equivalent functionality is not available through the Inventor API. However, the logic behind the command is very simple and not difficult to do on your own using Inventor’s API and features built into your programming language. A part or assembly doesn’t know if there are any drawings referencing it so the command just takes the filename of the part or assembly and looks for a file within the project of the same name as the part or assembly but with a .idw or .dwg extension.
Below is a VBA Sub that passes in the active part or assembly document to a function that either returns the full filename to the found drawing or an empty string if it can’t find a drawing.
Public Sub FindDrawingTest() Dim partDoc As PartDocument Set partDoc = ThisApplication.ActiveDocument ' Call the function to get the drawing. Dim drawingFilename As String drawingFilename = FindDrawingFile(partDoc) ' Display the result. If drawingFilename "" Then MsgBox "The drawing for """ & partDoc.fullFilename & """ was found: " & vbCr & drawingFilename Else MsgBox "No drawing was found for """ & partDoc.fullFilename & """" End If End Sub ' Find the drawing for the specified part of assembly. Private Function FindDrawingFile(PartOrAssemblyDoc As Document) Dim fullFilename As String fullFilename = PartOrAssemblyDoc.fullFilename ' Extract the path from the full filename. Dim path As String path = Left$(fullFilename, InStrRev(fullFilename, "")) ' Extract the filename from the full filename. Dim filename As String filename = Right$(fullFilename, Len(fullFilename) - InStrRev(fullFilename, "")) ' Replace the extension with "dwg" filename = Left$(filename, InStrRev(filename, ".")) & "dwg" ' Find if the drawing exists. Dim drawingFilename As String drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(path, filename) ' Check the result. If drawingFilename = "" Then ' Try again with idw extension. filename = Left$(filename, InStrRev(filename, ".")) & "idw" ' Find if the drawing exists. drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(path, filename) ' Return the result. If drawingFilename "" Then FindDrawingFile = drawingFilename Else FindDrawingFile = "" End If Else ' Return the result. FindDrawingFile = drawingFilename End If End Function
And here’s the same thing implemented in Visual Basic .NET
Private Sub btnFindDrawing_Click(sender As Object, e As EventArgs) Handles btnFindDrawing.Click Dim invApp As Inventor.Application = GetObject(, "Inventor.Application") Dim partDoc As Inventor.PartDocument = invApp.ActiveDocument ' Call the function to get the drawing. Dim drawingFilename As String = FindDrawingFile(partDoc) ' Display the result. If drawingFilename "" Then MsgBox("The drawing for """ & partDoc.FullFileName & """ was found: " & vbCr & drawingFilename) Else MsgBox("No drawing was found for """ & partDoc.FullFileName & """") End If End Sub ' Find the drawing for the specified part of assembly. Private Function FindDrawingFile(PartOrAssemblyDoc As Inventor.Document) Dim fullFilename As String = PartOrAssemblyDoc.FullFileName ' Extract the path from the full filename. Dim path As String = System.IO.Path.GetFullPath(fullFilename) ' Extract the filename from the full filename. Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(fullFilename) Dim invApp As Inventor.Application = PartOrAssemblyDoc.Parent ' Find if the dwg exists.
Dim drawingFilename As String drawingFilename = invApp.DesignProjectManager.ResolveFile(path, filename & ".dwg") ' Check the result. If drawingFilename = "" Then ' Try again with idw extension. drawingFilename = invApp.DesignProjectManager.ResolveFile(path, filename & ".idw") ' Return the result. If drawingFilename "" Then Return drawingFilename Else Return "" End If Else ' Return the result. Return drawingFilename End If End Function
-Brian


Leave a Reply