4 Replies Latest reply on Aug 15, 2016 3:20 AM by Edwin Thompson

    API crashing Solidworks with 3D Sketch

    Edwin Thompson

      My program draws inserts a 3D Sketch to programmatically draw a 3D line.  However after between a few to 20 tries Solidworks will lock up when the Sketch is created.  What am I doing wrong?  These crashes have been consistent for both Assembly and Part Model space (swModel).  Here is the code:

       

      Dim swFeat As SolidWorks.Interop.sldworks.Feature

      Dim swSkPoint1 As SolidWorks.Interop.sldworks.SketchPoint

      Dim swSkPoint2 As SolidWorks.Interop.sldworks.SketchPoint

      Dim swSkLine As SolidWorks.Interop.sldworks.SketchLine

      Dim bRet As Boolean

       

      swModel.SetAddToDB(True)

      swModel.Insert3DSketch2(False)

      swSkPoint1 = swModel.CreatePoint2(P1x, P1y, P1z)

      swSkPoint2 = swModel.CreatePoint2(P2x, P2y, P2z)

      swSkLine = swModel.CreateLine2(P1x, P1y, P1z, P2x, P2y, P2z)

       

      swModel.SetAddToDB(False)

      TempSketch = swModel.GetActiveSketch2

      swFeat = TempSketch

      swFeat.Name = "DispMeaSketch"

      swModel.Insert3DSketch2(True)

      bRet = swModel.EditRebuild3

        • Re: API crashing Solidworks with 3D Sketch
          Peter Brinkhuis

          Why do you use solidworks.interop.sldworks.feature instead of just SldWorks.Feature? The same for the other three variables? From what I understand it is required when you want to start SolidWorks from another application. Is that what you're doing?

           

          Can you comment out sections or replace then with hardcoded lines to keep the error from appearing?

           

          From what I read here you need to define a Sketch Manager variable first and use that one to add a sketch with. Like in swSketchManager.Insert3DSketch. You're using the model doc to add a sketch to, that method is now obsolete.

          • Re: API crashing Solidworks with 3D Sketch
            Viktor Bovzdarenko

            Hi Edwin
            This code is ok
            I added loop to your code to insert 100x 3D sketches and nothing crashed.
            Check the way you attached to SolidWorks. Is there a chance that the memory could be cleaned and some variables (like model) could be set back to nothing or overwritten by another command in the code? 

             

            Dim swApp       As SldWorks.SldWorks

            Dim swModel     As SldWorks.ModelDoc2

            Sub main()

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc   

                Dim swFeat As Feature

                Dim swSkPoint1 As SketchPoint

                Dim swSkPoint2 As SketchPoint

                Dim swSkLine As SketchLine

                Dim bRet As Boolean

                Dim i As Integer    

                For i = 0 To 100

                    swModel.SetAddToDB (True)

                    swModel.Insert3DSketch2 (False)

                    Dim P1x As Double: P1x = 0

                    Dim P1y As Double: P1y = i * 0.1

                    Dim P1z As Double: P1z = 0

                    Dim P2x As Double: P2x = 0.1 + i * 0.1

                    Dim P2y As Double: P2y = i * 0.1

                    Dim P2z As Double: P2z = 0

                   

                    Set swSkPoint1 = swModel.CreatePoint2(P1x, P1y, P1z)

                    Set swSkPoint2 = swModel.CreatePoint2(P2x, P2y, P2z)

                    Set swSkLine = swModel.CreateLine2(P1x, P1y, P1z, P2x, P2y, P2z)

                    

                    swModel.SetAddToDB (False)

                    Set TempSketch = swModel.GetActiveSketch2

                    Set swFeat = TempSketch

                    swFeat.Name = "DispMeaSketch_" & i

                    swModel.Insert3DSketch2 (True)

                    bRet = swModel.EditRebuild3

                Next i

            End Sub

              • Re: API crashing Solidworks with 3D Sketch
                Edwin Thompson

                Viktor, fantastic job and I really appreciate all of your previous help.  In the current version of the project Solidworks is attached "dynamically".  For awhile, there were some significant problems but they seem to be straightened out.  Given some of the battles, I would agree that the "attachment" is a strong potential candidate.  However, in the previous sets (before the dynamic attachment) and the attachment was clean,  the crashing was happening within the about 10 manual processes. 

              • Re: API crashing Solidworks with 3D Sketch
                Edwin Thompson

                I really appreciate your help.  From your feedback I tried the following code, which hasn't crashed SW yet (fairly minimal testing):

                 

                Dim swSketchMgr As SketchManager = swModel.SketchManager

                Dim swSketchSegment As SketchSegment

                 

                                    swSketchMgr.InsertExplodeLineSketch()

                                    swSketchMgr.CreatePoint(P1x, P1y, P1z)

                                    swSketchMgr.CreatePoint(P2x, P2y, P2z)

                                    swSketchSegment = swSketchMgr.CreateLine(P1x, P1y, P1z, P2x, P2y, P2z)

                 

                                    TempSketch = swModel.GetActiveSketch2

                                    swFeat = TempSketch

                                    swFeat.Name = "DispMeaSketch"

                                    swModel.Insert3DSketch2(True)

                 

                This codes uses SketchManager and apparently doesn't need a few of the lines that were previously used.  Hopefully, this will keep working.

                 

                :-)