Open Drawing From BOM

Library for macros
User avatar
Stefan Sterk
Posts: 30
Joined: Tue Aug 10, 2021 2:40 am
Answers: 2
x 43
x 62

Open Drawing From BOM

Unread post by Stefan Sterk »

Well Hello,

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.
OpenFromDrawingBom_Example.gif
ADD MACRO AS MOUSE GUESTURE (VIDEO)
This video shows how to add the macro as a mouse guesture. So that you can open a drawing from BOM with just a right click swipe.
OpenDrawingFromBom_MouseGuesture.mp4
(4.01 MiB) Downloaded 124 times

VBA CODE

Code: Select all

' ###################################################
' # 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
OpenDrawingFromBom.zip
OpenDrawingFromBom.swp
(13.07 KiB) Downloaded 118 times
User avatar
Eddy Alleman
Posts: 44
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 78
x 86
Contact:

Re: Open Drawing From BOM

Unread post by Eddy Alleman »

Hi Stefan

Nice work!

Eddy
Uncle_Hairball
Posts: 179
Joined: Fri Mar 19, 2021 12:21 pm
Answers: 2
x 27
x 90

Re: Open Drawing From BOM

Unread post by Uncle_Hairball »

Hi Stephan,

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.

Any suggestions?
User avatar
Eddy Alleman
Posts: 44
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 78
x 86
Contact:

Re: Open Drawing From BOM

Unread post by Eddy Alleman »

Hi Uncle,

I tested it and it works fine here.

Can you run the macro without using a gesture?

Menu Tools > Macro > Run and select the OpenDrawingFromBom.swp file
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Open Drawing From BOM

Unread post by AlexLachance »

Thanks Stefan, I've downloaded the macro and shared it with my coworkers. All of them agreed that it was a macro that they did not know they wanted!

Edit: Hrm doesn't seem to work on my side either, most likely because I don't use gestures I'm guessing?
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Open Drawing From BOM

Unread post by AlexLachance »

Hrm... Even with mouse gestures it's not working... Wish this guy would come back and tell us what's up
User avatar
mattpeneguy
Posts: 1380
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2487
x 1888

Re: Open Drawing From BOM

Unread post by mattpeneguy »

AlexLachance wrote: Thu Aug 26, 2021 8:08 am Hrm... Even with mouse gestures it's not working... Wish this guy would come back and tell us what's up
@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?
image.png
User avatar
Eddy Alleman
Posts: 44
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 78
x 86
Contact:

Re: Open Drawing From BOM

Unread post by Eddy Alleman »

you have to select a BOM cell first (not in a header) using LMB
click.png
click.png (12.28 KiB) Viewed 2827 times
then you press RMB and drag to the left.

That should do it.

If nothing happens, then follow my previous question/advice to run the macro directly...
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Open Drawing From BOM

Unread post by AlexLachance »

mattpeneguy wrote: Thu Aug 26, 2021 8:48 am @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?
image.png
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.
User avatar
Eddy Alleman
Posts: 44
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 78
x 86
Contact:

Re: Open Drawing From BOM

Unread post by Eddy Alleman »

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.
Uncle_Hairball
Posts: 179
Joined: Fri Mar 19, 2021 12:21 pm
Answers: 2
x 27
x 90

Re: Open Drawing From BOM

Unread post by Uncle_Hairball »

Eddy Alleman wrote: Thu Aug 26, 2021 5:33 am Hi Uncle,

I tested it and it works fine here.

Can you run the macro without using a gesture?

Menu Tools > Macro > Run and select the OpenDrawingFromBom.swp file
Nope. Nothing happens.

I have admin rights on the directory where the macros are stored.
User avatar
Eddy Alleman
Posts: 44
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 78
x 86
Contact:

Re: Open Drawing From BOM

Unread post by Eddy Alleman »

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?
Uncle_Hairball
Posts: 179
Joined: Fri Mar 19, 2021 12:21 pm
Answers: 2
x 27
x 90

Re: Open Drawing From BOM

Unread post by Uncle_Hairball »

Hi Eddy,

The drawing is not open.

I don't know what a screen print would show you, so I recorded a video, but cannot find a way to attach it.

Matt, is it possible to attach my upload here?
User avatar
Stefan Sterk
Posts: 30
Joined: Tue Aug 10, 2021 2:40 am
Answers: 2
x 43
x 62

Re: Open Drawing From BOM

Unread post by Stefan Sterk »

Uncle_Hairball wrote: Wed Aug 25, 2021 8:14 pm Hi Stephan,

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.

Any suggestions?
AlexLachance wrote: Thu Aug 26, 2021 7:57 am Thanks Stefan, I've downloaded the macro and shared it with my coworkers. All of them agreed that it was a macro that they did not know they wanted!

Edit: Hrm doesn't seem to work on my side either, most likely because I don't use gestures I'm guessing?
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.
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Open Drawing From BOM

Unread post by AlexLachance »

Stefan Sterk wrote: Mon Sep 06, 2021 8:03 am 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 Stefan!

I am using Resolved, but my SolidWorks is in french rather then english. Could that be the cause?

This is the error I get on Visual Basic. 2nd image to show how I selected. 3rd image to show file location.
image.png
image.png (5.1 KiB) Viewed 2588 times
image.png
image.png
User avatar
Eddy Alleman
Posts: 44
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 78
x 86
Contact:

Re: Open Drawing From BOM

Unread post by Eddy Alleman »

AlexLachance wrote: Tue Sep 07, 2021 9:02 am This is the error I get on Visual Basic. 2nd image to show how I selected. 3rd image to show file location.
Hi Alex,

Can you check your references:
Menu tools > References
image.png
image.png (4.69 KiB) Viewed 2584 times
Eddy
User avatar
Stefan Sterk
Posts: 30
Joined: Tue Aug 10, 2021 2:40 am
Answers: 2
x 43
x 62

Re: Open Drawing From BOM

Unread post by Stefan Sterk »

AlexLachance wrote: Tue Sep 07, 2021 9:02 am Hey Stefan!

I am using Resolved, but my SolidWorks is in french rather then english. Could that be the cause?

This is the error I get on Visual Basic. 2nd image to show how I selected. 3rd image to show file location.

image.png
image.png
image.png
language shouldn't be the issue.

That error you get should be a easy fix. I made the macro (.swp) file with SolidWorks 2021 and you're prob using a lower version of SolidWorks. Check out the following link which will guide you to fix this error; https://www.codestack.net/solidworks-ap ... eferences/
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Open Drawing From BOM

Unread post by AlexLachance »

Stefan Sterk wrote: Tue Sep 07, 2021 9:23 am language shouldn't be the issue.

That error you get should be a easy fix. I made the macro (.swp) file with SolidWorks 2021 and you're prob using a lower version of SolidWorks. Check out the following link which will guide you to fix this error; https://www.codestack.net/solidworks-ap ... eferences/
Hrm, it was as simple as fixing these references and now it works!

Thanks Stefan, wonderful work!
User avatar
Eddy Alleman
Posts: 44
Joined: Thu Apr 01, 2021 10:32 am
Answers: 8
Location: Belgium
x 78
x 86
Contact:

Re: Open Drawing From BOM

Unread post by Eddy Alleman »

Hi Alex,

TIP: Sometimes it's easier to create a new macro with macro recorder and then copy paste the code in there.
Those references will be ok already then.

Eddy
User avatar
AlexLachance
Posts: 1994
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2157
x 1847

Re: Open Drawing From BOM

Unread post by AlexLachance »

Eddy Alleman wrote: Tue Sep 07, 2021 10:26 am Hi Alex,

TIP: Sometimes it's easier to create a new macro with macro recorder and then copy paste the code in there.
Those references will be ok already then.

Eddy

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 :lol:

Nonetheless, I got it! Thanks for the advice, I will try to think of it next time if it happens again.
Post Reply