Macro for extracting dimensional dimensions from Solidworks

Hi all

I am working on the automation of a CAD/CAM process and I would like to be able to extract from a part or a Solidworks assembly the dimensions and their dimensions (for each function of the part(s) to an external file.

Thanks to Mr. Boutherand on an old article of ''myCadblog'', I found a macro that allows you to extract this data via an excel file. Only this macro seems to be obsolete (see code below).

Could you help me update it? (FYI: I'm on Solidworks 2014)

Thank you very much in advance for your feedback!

Roman

 

  • Sub AddAllDimensions()
  • Dim swDisplayDimension As SldWorks.DisplayDimension
  • Dim swDimension As SldWorks.Dimension
  • Dim swFeature As SldWorks.Feature
  • On Error GoTo SiErr
  • 'Retrieves the SolidWorks application
  • Set swApp = CreateObject("SldWorks.Application")
  • 'We get the active document
  • Set swDoc = swApp.ActiveDoc
  • 'Retrieves the first function
  • Set swFeature = swDoc.FirstFeature
  •     
  • If swFeature Is Nothing Then
  • MsgBox "the document is empty", vbExclamation
  • Exit Sub
  • End If
  •  
  • Do
  • 'On each function we retrieve the dimensions
  • Set swDisplayDimension = swFeature.GetFirstDisplayDimension
  • If Not swDisplayDimension Is Nothing Then
  • Do
  • Set swDimension = swDisplayDimension.GetDimension
  • Add to the list
  • Call AddDimToSheet(swDisplayDimension)
  • Set swDisplayDimension = swFeature.GetNextDisplayDimension(swDisplayDimension)
  • Loop Until swDisplayDimension Is Nothing
  • End If
  • Set swFeature = swFeature.GetNextFeature
  • Loop Until swFeature Is Nothing
  •  
  • Exit Sub
  •  
  • SiErr:
  • Set swDisplayDimension = Nothing
  • Summary Next
  • End Sub

 

 

1 Like

may be a 32-bit to 64-bit or VBA macro language issue

http://help.solidworks.com/2013/French/WhatsNew/c_application_programming_interface_wn2012.htm?format=P

@+ ;-)

1 Like

I changed the macro slightly, it now lists the odds and their values

All that remains is to add your treatments

There she is 

 


macro1.zip
8 Likes

@gt22: Thank you  I'll look at the doc closely.

 

@jfaradon: Thank you for the macro modification, indeed it recovers the dimensions but only under solidworks by displaying a msgbox right?

Basically, the macro runs in an excel file to be able to extract the dimensions in cells.

 

Is it possible to run it in excel? 

 

Thank you for your quick answers!

Good evening

 

just a quick question extract the values ok, but if you have tolerances how will it be with CAD/CAM machining?

Because I find the question very interesting but this point on tolerances makes me turlupine!

 

Thank you.

2 Likes

Good evening Matthieu, 

 

Tolerances don't matter in our industrial process (strange statement taken out of context:) ) but it is. The application is quite special, far from the classic mechanical precisions...

What I need in the design is thanks to a library of functions where the machining is standardized in its shapes (sketch + material removal function) and for which I redefine the dimensions of the dimensions each time, to draw my parts and assemble them. 

From the drawing of each part or the assembly, I want to extract a nomenclature of each part, the functions applied to each part (the machining in production) and the dimensions I used for each function. The manufacturing program only needs the names of the parts, the features applied to those parts, and some of the dimensions that define the positioning of the features on the part (the position of the machining in production).

 

That's the story. :)

 

Good night. 

2 Likes

Hi all

I finally managed to get the macro to work! 

See the result attached...

On the other hand, it only seems to work for parts, and not for assemblies, while there is no .part specification in the macro code, is this normal?

 

Have a nice day!

 

 


extraction_test.jpg

Roman

 

Here is a modification to take into account the assemblies

The code on the feature value retrieve part is the same

 

 

 

Dim swApp As SldWorks.SldWorks

Dim swDoc As SldWorks.ModelDoc2

Dim swAss As SldWorks.AssemblyDoc

Dim swPart As SldWorks.PartDoc

Dim swFeature As SldWorks.Feature

 

Sub main()

 

 

    On Error GoTo SiErr

    'Retrieves the SolidWorks application

    Set swApp = CreateObject("SldWorks.Application")

    'We get the active document

    Set swDoc = swApp.ActiveDoc

    

    Select Case swDoc.GetType

        Box swDocumentTypes_e.swDocPART

            Set swPart = swDoc

            Call TraverseFeature(swPart)

        Box swDocumentTypes_e.swDocASSEMBLY

            Set swAss = swDoc

            Dim swComponents As Variant

            swComponents = swAss.GetComponents(False)

            For i = 0 TB UBound(swComponents)

                Call TraverseFeature(swComponents(i). GetModelDoc)

            Next i

        Box swDocumentTypes_e.swDocDRAWING

            MsgBox "Not on a plan", vbExclamation

            End

    End Select

    

 

SiErr:

    MsgBox Err.Description, vbCritical

    End

End Sub

 

Sub TraverseFeature(ByRef swDoc As SldWorks.ModelDoc2)

    Dim swDisplayDimension As SldWorks.DisplayDimension

    Dim swDimension As SldWorks.Dimension

    Dim swFeature As SldWorks.Feature

 

    ' If the document does not exist (e.g. not available, component deleted)

    If swDoc Is Nothing Then Exit Sub

    

    

    'Retrieves the first function

    Set swFeature = swDoc.FirstFeature

    

    If swFeature Is Nothing Then

        MsgBox "the document is empty", vbExclamation

        Exit Sub

    End If

 

    Do

        'On each function we retrieve the dimensions

        Set swDisplayDimension = swFeature.GetFirstDisplayDimension

        If Not swDisplayDimension Is Nothing Then

            Do

                Set swDimension = swDisplayDimension.GetDimension

                Add to the list

                If MsgBox(swDimension.FullName & " = " & swDimension.GetValue2(""), vbOKCancel, swDoc.GetTitle) = vbCancel Then End

                Set swDisplayDimension = swFeature.GetNextDisplayDimension(swDisplayDimension)

            Loop Until swDisplayDimension Is Nothing

        End If

        Set swFeature = swFeature.GetNextFeature

    Loop Until swFeature Is Nothing

 

    Exit Sub

 

SiErr:

    MsgBox Err.Description, vbCritical

    End

End Sub

9 Likes

Good evening jfaradon, 

 

Sorry I just realized that it was you who had developed the original macro and the associated excel file... :S

 

Thank you for your new code, but I have trouble integrating it into the current set of macros in the excel file. Do I have to replace the whole ''Sub AddAlldimensions()'' code with the new one? I wasn't at the BE today to test...

 

Thank you very much.

 

Good night. 

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hello!

 

I integrated the whole thing this morning, it works!!

I declared ''i'' As Integer and that's okay. 

 

On the other hand I just have an error msgbox at the end (see attached image) I can't see where it comes from...

 

Thank you very much in any case. I didn't have the reflex of forums when it comes to design and your skills and responsiveness is extraordinary.

 

Have a nice day!

 

Roman


msgbox_error.jpg

Hello, what do you need to add to extract its in an excel file? or txt? 

2 Likes

Hello and thank you for this code. I tried to use it as inspiration to extract dimensions as annotations in an assembly. I therefore ask my question here, which seems to me to be more judicious.

The dimensions (between 2 components) have a name of the RD1@annotations type. Is it possible to access their values via a macro? I looked in the API, but I'm drying up (FeatureByName?)

Second question of the same order, I would like to control the value of a contarrinte in the assembly.

The goal is to vary an angle and retrieve the measured dimensions by annotations directly in Excel.

Thank you for your help.

1 Like
Hello, the old questions are very little followed on this forum because there is no possibility to sort by last activity. If you have a question, don't hesitate to ask it on the forum, and there you will have quick answers!