15 Replies Latest reply on Apr 2, 2018 11:58 AM by Deepak Gupta

    Solidworks crash during macro run over 500 instances

    Jason Miller

      I am using Solidworks 2017 and a macro through Microsoft Visual Basic for Applications to create and save drawings in a .png format from a model with 1500-2000 configurations.  I have been using Solidworks for many years but I am new to macro's so I have very little experience with how to create or edit them (don't mind some of the commented out notes in the macro that I have to remind me what each line does).  Through other forum discussions I have been able to put together the attached macro and it works ok except for after you get to somewhere around 400-500 loops you begin to get Solidworks Resources dangerously low pop up messages and then eventually it will just lock up and crash Solidworks.  Below are the steps I take in my process

      Step 1 - I open the model in Solidworks

      Step 2 - In Solidworks go to Tools/Macro/Edit and open the attached macro

      Step 3 - Click Run on the macro

      Step 4 - Macro opens a Solidworks drawing template (.drwdot file type)

      Step 5 - Macro updates the frame size

      Step 6 - Macro pulls the default model into a single predefined view

      Step 7 - Macro updates the model view in the template to the next configuration

      Step 8 - Macro adds annotations, adjusts zoom

      Step 9 - Macro saves file as .png

      Step 10 - Macro closes template file

      Step 11 - Loop back to Step 4

      I can't help but think that it is an issue with cache or something that is building up.  If I run 300 and then close Solidworks and start back at Step 1 I have never gotten an issue.  If I run 300 and then start back at Step 1 and run another 300 without closing Solidworks I still get a crash.  Was wondering if there are a couple of lines of code or something that I could add to the macro to clear the cache or not?

       

      Jason

        • Re: Solidworks crash during macro run over 500 instances
          Michael Spens

          You're probably right Jason, switching configurations on the model repeatedly may be the cause of the resource overload.  Your best option would be to close the SOLIDWORKS session every 100 configurations, but if you have to stick to a VBA macro, you may be able to flush the memory by closing the part regularly.  Here's a method that may work for you...

           

          First, hold the pathname of the part in memory like you do with your drawing template.

           

          Sub main()

             

              Set swApp = Application.SldWorks

             

              Set swModel = swApp.ActiveDoc

             

              Dim modelPath As String

              modelPath = swModel.GetPathName

           

           

          At the top of your For loop, close and re-open the part every 100 configurations (or whatever number works best).  Your array of configuration names is already in memory so you shouldn't have to worry about that piece.

           

              For i = 0 To UBound(vConfs)

                  If i = 100 Or i = 200 Or i = 300 Or i = 400 Then

                      swApp.CloseDoc modelPath

                      Dim errors, warnings As Long

                      Set swModel = swApp.OpenDoc6(modelPath, swDocPART, swOpenDocOptions_Silent, vConfs(i), errors, warnings)

                  End If

           

          Let us know how it goes.

          Mike

            • Re: Solidworks crash during macro run over 500 instances
              Jason Miller

              Thank you for your quick response.  I added the if statement that you recommended but received error messages when I tried to run the macro.  Attached are screenshots of the error messages it gave.  I supposed it is always possible that I didn't add them correctly since I'm new to this but it seemed like it should be pretty simple...

              After I click run I get a compile error and it highlights the word "errors" in the code (top image)

              I decided to just try taking our the word "errors" to see what happened and I got a different compile error (middle image)

              When I type the "errors," back in there it brings up what looks like is a help script to try to guide you but honestly I don't know enough about reading it to understand what it wants (bottom image)Error 1.JPGError 2.JPGError 3.JPG

                • Re: Solidworks crash during macro run over 500 instances
                  Matt Peneguy

                  Try changing that Dim statement directly above to Integer:

                  • Re: Solidworks crash during macro run over 500 instances
                    Michael Spens

                    Yeah, sometimes VBA makes me lazy.  My mistake.  I left out the type for the errors variable.  VBA initializes every un-typed variable as Variant and the Open6 method expects an Integer (yes, Long works in place of Integer).

                     

                    Dim errors As Long, warnings As Long

                     

                    Try that.

                     

                    Mike

                      • Re: Solidworks crash during macro run over 500 instances
                        Jason Miller

                        Ok, so that makes it work which is great.  Thanks!  However, when I get to 100 I now get this (top image), if I click debug then I get this (middle image), and my Solidworks hangs up (bottom image).  To me it looks like it opened the template file and then somehow couldn't find the model view it was supposed to insert but the model is still open so I'm not sure why that would be.  Any other advice?

                        Run Time Error.JPGRun Time Error Debug.JPG

                        screen.JPG

                          • Re: Solidworks crash during macro run over 500 instances
                            Jacob Corder

                            SwDraw or SwModel is not set to anything. this is why your getting this issue

                            null checking is important. 

                            if not SwDraw is nothing then

                                 if not SwModel is nothing then

                                      SwDraw.InsertModelInPredefinedView SwModel.GetPathname()

                                 end if

                            end if

                              • Re: Solidworks crash during macro run over 500 instances
                                Jason Miller

                                Ok.  Thanks!  I have added that which allows the macro to continue past 100 without error.  It hits 100, closes, and then reopens but it quits populating the predefined view in the drawing.  Top image is image 100.  Middle image is a screen capture of what my screen looks like after image 100.  Bottom image is image 101.  I guess it's just not pulling in the model view anymore maybe once it reopens?  Sorry, I'm really not very familiar with writing macros so I'm trying to learn on the fly.  I put the current version of my macro at the bottom...

                                blank 3.JPG

                                blank 2.JPG

                                blank.JPG

                                Dim swApp As SldWorks.SldWorks

                                Dim swModel As SldWorks.ModelDoc2

                                Dim swDraw As SldWorks.DrawingDoc

                                Dim boolstatus As Boolean

                                 

                                 

                                Dim vConfs As Variant

                                Dim i As Integer

                                 

                                Dim sDrTemplatePath As String

                                Dim sDrTemplate As String

                                Dim lDrSize As Long

                                Dim myModelView As Object

                                 

                                Const sOutputFolder As String = "M:\Share\Configurator\TUBE AND WIRE\" 

                                 

                                Sub main()

                                   

                                    Set swApp = Application.SldWorks

                                   

                                    Set swModel = swApp.ActiveDoc

                                   

                                    Dim modelPath As String

                                    modelPath = swModel.GetPathName

                                   

                                    'Path for the drawing template.

                                    'sDrTemplate = "C:\Workspace\Configurator\Screwplug\Test\Config Drawing Graphics Template02.drwdot"

                                    sDrTemplate = "C:\WAT_PDM_PRD\COMMON\CONFIGURATOR\Templates\TW_TC_Tube 90 Drawing Template.drwdot"

                                 

                                 

                                   

                                    'Can also use this code IF the desired drawing template is marked as your default template

                                    'sDrTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplateDrawing)

                                   

                                   

                                    vConfs = swModel.GetConfigurationNames()

                                 

                                 

                                   

                                    For i = 0 To UBound(vConfs)

                                   

                                    If i = 100 Or i = 200 Or i = 300 Or i = 400 Then

                                        swApp.CloseDoc modelPath

                                        Dim errors As Long, warnings As Long

                                        Set swModel = swApp.OpenDoc6(modelPath, swDocPART, swOpenDocOptions_Silent, vConfs(i), errors, warnings)

                                    End If

                                 

                                    'If Left(vConfs(i), 10) = "22_2_5_0_4" Then   'used to run specific set of images

                                    'If vConfs(i) = "22_2_5_0_0_00" Then     'used to run one specific image

                                   

                                        'Open a new drawing based on the seleted drawing template

                                        Set swDraw = swApp.NewDocument(sDrTemplate, 0, 0, 0)

                                   

                                       

                                        'Take the view to Normal state instead of "Maximized"

                                        Set myDrawView = swDraw.ActiveView

                                        myDrawView.FrameState = swWindowState_e.swWindowNormal

                                   

                                        'Set the window position and size

                                        myDrawView.FrameLeft = 0

                                        myDrawView.FrameTop = 0

                                        myDrawView.FrameWidth = 810     'FRAME WIDTH  = TARGET WIDTH + 255

                                        myDrawView.FrameHeight = 800    'FRAME HEIGHT = TARGET HEIGHT + 53

                                      

                                        

                                        If Not swDraw Is Nothing Then

                                          If Not swModel Is Nothing Then

                                            swDraw.InsertModelInPredefinedView swModel.GetPathName()

                                          End If

                                        End If

                                       

                                        Dim swView As SldWorks.View

                                        Set swView = swDraw.GetFirstView

                                       

                                        While Not swView Is Nothing

                                           

                                            swView.ReferencedConfiguration = vConfs(i)

                                           

                                            Set swView = swView.GetNextView

                                           

                                        Wend

                                       

                                        Dim swDrawModel As SldWorks.ModelDoc2

                                        Set swDrawModel = swDraw

                                              

                                        swDrawModel.ForceRebuild3 False

                                 

                                        Dim vAnnotations As Variant

                                        vAnnotations = swDrawModel.InsertModelAnnotations3(0, 32840, True, True, False, True)

                                       

                                         

                                        'Zoom Full

                                        swDrawModel.ViewZoomtofit2

                                       

                                        'Save the file as drawing file

                                        'swDrawModel.Extension.SaveAs sOutputFolder + "DWG[" + CStr(i) + "]" + vConfs(i) + ".slddrw", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0

                                       

                                        'Zoom in

                                        swDrawModel.ActiveView.ZoomByFactor (1.25)     ' This changes the zoom amount. 2.0 appears to be the max value

                                             

                                        'Save the file as image file

                                        'Use if you need image numbers added to file name

                                        'swDrawModel.Extension.SaveAs sOutputFolder + "_IMG[" + CStr(i) + "]" + vConfs(i) + ".png", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0

                                        swDrawModel.Extension.SaveAs sOutputFolder + vConfs(i) + ".png", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, 0, 0

                                               

                                        'Close the file

                                        swApp.CloseDoc swDrawModel.GetTitle()

                                   

                                    'End If

                                       

                                    Next

                                   

                                End Sub

                                  • Re: Solidworks crash during macro run over 500 instances
                                    Michael Spens

                                    Jason - everything runs find when I test your code.  The only thing I see that might cause problems (if another model might be open) is that you don't need to get the model path again when you insert views.  You have this...

                                     

                                            If Not swDraw Is Nothing Then

                                              If Not swModel Is Nothing Then

                                                swDraw.InsertModelInPredefinedView swModel.GetPathName()

                                              End If

                                            End If

                                     

                                    But you already have a variable for the path to the model declared as modelPath.  This might be more consistent if you change to...

                                     

                                            If Not swDraw Is Nothing Then

                                              If Not swModel Is Nothing Then

                                                swDraw.InsertModelInPredefinedView modelPath

                                              End If

                                            End If

                                     

                                    For testing purposes, you could try closing the model every drawing as well.

                                     

                                    Mike

                                      • Re: Solidworks crash during macro run over 500 instances
                                        Jason Miller

                                        Mike,

                                          I'm glad you said that it worked for you because that caused me to think of something that I hadn't mentioned before (didn't know it might be relevant).  My model is a .sldasm file and not a .sldprt file.  I went out and created a generic cylindrical .sldprt file with configurations of 110 different lengths and you're correct it ran just fine.  It closed and reopened at 100 and continued right along properly.  I then created a 2nd cylindrical .sldprt file with 110 different lengths and it ran fine also.  I took those 2 .sldprt files and created an .sldasm with 110 different combinations/configurations of the 2 .sldprt files.  When I went to run that I'm back to getting blank images as I have shown above.  Is it possible that this is due to the file type?

                                        Jason

                                          • Re: Solidworks crash during macro run over 500 instances
                                            Michael Spens

                                            Good catch Jason.  That's the issue.  OpenDoc6 requires you to specify the document type being opened as the second argument.

                                             

                                            swApp.OpenDoc6(modelPath, swDocPART, ...

                                             

                                            Replace swDocPART with swDocASSEMBLY and you should be good to go.

                                             

                                            Mike

                                              • Re: Solidworks crash during macro run over 500 instances
                                                Jason Miller

                                                THAT DID IT!  Thanks for all of your help!  I'll do some more testing next week but at least I can go into my holiday weekend with that off of my plate.

                                                 

                                                Jason

                                                  • Re: Solidworks crash during macro run over 500 instances
                                                    Jason Miller

                                                    I have one last question.  Is it possible to have the model open back up to the "default" configuration as the first configuration once it closes and reopens?  So basically image 1, 101, 201, 301, etc would all be the default configuration.  The reason I ask is that in order to get consistent zoom level and positioning I always start the macro with the model open to the default configuration (which I have set to be the tallest/widest version of the part that could exist).  If I don't start with the default I end up with results like below which I believe is due to the fact that it is setting the fit view on the first configuration that it runs and there is a difference in overall sizes of the parts from configuration to configuration.  Image 100 is on the left and image 101 is on the right

                                                      • Re: Solidworks crash during macro run over 500 instances
                                                        Deepak Gupta

                                                        Jason Miller wrote:

                                                         

                                                        I have one last question. Is it possible to have the model open back up to the "default" configuration as the first configuration once it closes and reopens?

                                                        Away from the machine but try this. Change vConfs(i) to "Default" in the following line 

                                                         

                                                        Set swModel = swApp.OpenDoc6(modelPath, swDocPART, swOpenDocOptions_Silent, vConfs(i), errors, warnings)

                                                         

                                                        And add this line below above line to switch the config.

                                                         

                                                        swModel.ShowConfiguration2 vConfs(i)

                                          • Re: Solidworks crash during macro run over 500 instances
                                            Ivana Kolin

                                            did you add this from Michael Spens message?

                                            Dim modelPath As String

                                            modelPath = swModel.GetPathName