For those who wanna open a drawing directly from another drawing BOM. Stop searching for a solution because this post will give you on. Happy reading, wachting and opening drawings.
Note:The drawings needs to be in the same folder and having the same filename as the part/assembly you want to open from BOM. Example
Here is a example how it looks like.
VBA CODE
' ###################################################
' # Title: Open Drawing From BOM #
' # Version: 21.9.6 #
' # Author: Stefan Sterk #
' # Company: Idee Techniek Engineering B.V. #
' # #
' # This macro will try to open the drawing for the #
' # selected component(s) in the Bill of Meterials. #
' # #
' # NOTE: Drawing file must be in same folder as #
' # component and must have the same filename #
' ###################################################
Option Explicit
Dim swApp As SldWorks.SldWorks
Sub main()
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swTblAnn As SldWorks.TableAnnotation
Dim swBOMTbl As SldWorks.BomTableAnnotation
Dim swComp As SldWorks.Component2
Dim i As Integer, selType As Integer
Dim frtRow As Long, lstRow As Long
Dim frtCol As Long, lstCol As Long
Dim Row As Integer
Dim vComps As Variant
Dim CfgName As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then Exit Sub
If Not swModel.GetType = swDocDRAWING Then Exit Sub
Set swSelMgr = swModel.SelectionManager
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
selType = swSelMgr.GetSelectedObjectType3(i, -1)
If selType <> 98 Then
MsgBox "Please select a cel from BOM!"
Exit Sub
End If
Set swTblAnn = swSelMgr.GetSelectedObject6(i, -1)
Set swBOMTbl = swTblAnn
swTblAnn.GetCellRange frtRow, lstRow, frtCol, lstCol
For Row = frtRow To lstRow
CfgName = swBOMTbl.BomFeature.GetConfigurations(True, True)(0)
vComps = swBOMTbl.GetComponents2(Row, CfgName)
If Not IsEmpty(vComps) Then
Set swComp = swBOMTbl.GetComponents2(Row, CfgName)(0)
openComponentDrawing swComp
End If
Next Row
Next i
End Sub
Private Function openComponentDrawing(swComp As Component2)
Dim compPath As String
compPath = swComp.GetPathName
Dim drwPath As String
drwPath = Left(compPath, InStrRev(compPath, ".") - 1) & ".slddrw"
' Try Open Drawing
Dim swDrw As SldWorks.DrawingDoc
Dim errors As Long, warnings As Long
Set swDrw = swApp.OpenDoc6(drwPath, swDocDRAWING, 0, "", errors, warnings)
If errors <> 0 Then
If errors = 2 Then
Dim partNumber As String
partNumber = Right(drwPath, Len(drwPath) - InStrRev(drwPath, "\"))
partNumber = Left(partNumber, InStrRev(partNumber, ".") - 1)
MsgBox "Couldn't find drawing for following part number: " & partNumber
End If
Else
swApp.ActivateDoc3 drwPath, False, 0, errors
End If
End Function
I’ve followed the instructions in your video, but nothing happens when I use the mouse gesture; no feedback of any sort. The drawings are in the same directory as the models and the filenames are identical.
AlexLachance,
Looking at it, it doesn’t seem it would be too difficult to troubleshoot this macro. Have you tried editing it and opening the Immediate window, then running it to see if there are any errors output?
I haven’t tried yet because I have a few jobs to do this morning but might give it a shot this afternoon. It’s not doing anything at all though, not even sending one of the error messages that it should. I am selecting a cell, I tried selecting a row also, selecting the cell with the number.
I also tried using it directly from the bar, with mouse movements, and as a keyboard shortcut. None worked.
are you able to use macro recording and run that macro?
If so you can copy paste the code in there to test your environment.
Are you saving the macro in a directory that has no restrictions on user level? You can try using C:\temp in that case.
Is it possible that the drawing you want to open is already open?
The code just opens it if it is not already, it doesn’t activate it to show on top of other windows if it was already there.
could you “Tile Horizontally” and show a print screen here?
Like Eddy Alleman the macro doesn’t activated the drawing if it’s already open. I updated the code in this topic which fixes this issue.
But this doesn’t seems to be the issue for you guys.
Are you guys opening the drawing in detailing mode?
For me the macro also doesn’t work when I have a drawing opened in detailing mode, Lightweight and Resolved are working fine for me.
Hey Eddy,
I’m not too familiar with Visual Basic, I actually had to find where to click to open the References window once I pressed OK to remove the missing ones
Nonetheless, I got it! Thanks for the advice, I will try to think of it next time if it happens again.