9 Replies Latest reply on Jul 21, 2016 5:53 AM by Felix Sigwart

    Inconsistent API calls?

    Felix Sigwart

      Hello,

      the last few days I wrote my first SolidWorks macro in VB.NET and i found some (for me) strange behaviour. The macro works fine on the PC I developed it, but if I execute the macro on another PC some API functions doesn't seem to work. Their deprecated versions work on both PCs.
      On both PCs is SolidWorks 2016 SP 3.0, Windows 7 Professional. On the development PC is the SDK installed, but i doubt that the missing SDK is the problem.

      If I make a new macro on my second PC I can select the specific functions and pass the parameters. The compilation works also.

       

      E.g. IDrawingDoc:NewSheet3 and IDrawingDoc:NewSheet4

      Some testcode

      Imports SolidWorks.Interop.sldworks
      Imports SolidWorks.Interop.swconst
      Imports System.Runtime.InteropServices
      Imports System
      Partial Class SolidWorksMacro
          Public Sub main()
              Dim swdoc As ModelDoc2 = swApp.ActiveDoc
              Dim swdraw As DrawingDoc = CType(swdoc, DrawingDoc)
              swdraw.NewSheet3("foo", swDwgPaperSizes_e.swDwgPapersUserDefined, _
                  swDwgTemplates_e.swDwgTemplateNone, 1, 1, True, "", 100, 200, "")
              swdraw.ActivateSheet("foo")
              swdraw.NewSheet4("bar", swDwgPaperSizes_e.swDwgPapersUserDefined, _
                  swDwgTemplates_e.swDwgTemplateNone, 1, 1, True, "", 100, 200, "", 0, 0, 0, 0, 0, 0)
              swdraw.ActivateSheet("bar")
          End Sub
          ''' <summary>
          ''' The SldWorks swApp variable is pre-assigned for you.
          ''' </summary>
          Public swApp As SldWorks
      End Class
      

      On my development PC both functions are working and they create the new sheets. On the second PC NewSheet4 fails with a System.AccessViolationException.
      The same with the function ICustomPropertyManager:Get4 and ICustomPropertyManager:Get5.

      Is this a known problem? Did I miss something vital?

       

      Thanks in advance
      Felix

        • Re: Inconsistent API calls?
          Felix Sigwart

          Hey,

           

          Does no one have an idea?

          I wrote the testcode from above with NewSheet3 and NewSheet4 into a VBA-macro and it worked. The new sheets have been created on both PCs, the development and my test PC.

          Dim swApp As Object
          Sub main()
          
          
           Set swApp = Application.SldWorks
           Set swDraw = swApp.ActiveDoc
           boolstatus = swDraw.NewSheet3("foo", 12, 13, 1, 1, True, "", 100, 200, "")
           swDraw.ActivateSheet ("foo")
          
           boolstatus = swDraw.NewSheet4("bar", 12, 13, 1, 1, True, "", 100, 200, "", 0, 0, 0, 0, 0, 0)
           swDraw.ActivateSheet ("bar")
          
          End Sub
          

           

          Am I really the only/first one with that problem?

           

          Best regards

          Felix

            • Re: Inconsistent API calls?
              Deepak Gupta

              It could be related to various other reasons. One of them could be FrameWork version. Check the .NETFrameWork version on both pc.

               

              Also can you upload the exe here or codes for some to compile and test.

                • Re: Inconsistent API calls?
                  Felix Sigwart

                  Hi Deepak,

                   

                  thank you for your response.

                  The .Net-Frameworks are the same version on both PCs.

                  I attached both testmacros from above.

                   

                  VbNetTestMacro is the macro in VB.Net which works on the development PC and not on the testpc.

                  The compiled macro is in VbNetTestMacro\SwMacro\bin

                  Imports SolidWorks.Interop.sldworks
                  Imports SolidWorks.Interop.swconst
                  Imports System.Runtime.InteropServices
                  Imports System
                  
                  Partial Class SolidWorksMacro
                  
                  
                      Public Sub main()
                          Dim swdoc As ModelDoc2 = swApp.ActiveDoc
                          Dim swdraw As DrawingDoc = CType(swdoc, DrawingDoc)
                          swdraw.NewSheet3("foo", swDwgPaperSizes_e.swDwgPapersUserDefined, _
                              swDwgTemplates_e.swDwgTemplateNone, 1, 1, True, "", 100, 200, "")
                          swdraw.ActivateSheet("foo")
                          swdraw.NewSheet4("bar", swDwgPaperSizes_e.swDwgPapersUserDefined, swDwgTemplates_e.swDwgTemplateNone, 1, 1, True, "", 100, 200, "", 0, 0, 0, 0, 0, 0)
                          swdraw.ActivateSheet("bar")
                      End Sub
                  
                  
                      ''' <summary>
                      ''' The SldWorks swApp variable is pre-assigned for you.
                      ''' </summary>
                      Public swApp As SldWorks
                  
                  
                  End Class
                  

                   

                  VbaTestMacro.swp is the VBA macro

                  Dim swApp As Object
                  Sub main()
                  
                  
                   Set swApp = Application.SldWorks
                   Set swDraw = swApp.ActiveDoc
                   boolstatus = swDraw.NewSheet3("foo", 12, 13, 1, 1, True, "", 100, 200, "")
                   swDraw.ActivateSheet ("foo")
                  
                   boolstatus = swDraw.NewSheet4("bar", 12, 13, 1, 1, True, "", 100, 200, "", 0, 0, 0, 0, 0, 0)
                   swDraw.ActivateSheet ("bar")
                  
                  End Sub
                  

                   

                  Thanks for your help.

                   

                  best regards

                  Felix

                • Re: Inconsistent API calls?
                  Christian Chu

                  Seems like there is something to do with .NET Framework - Can you check the diff. between the 2 computers?

                  Opp.. never mind ! Deepak already mentioned it

                • Re: Inconsistent API calls?
                  Amen Allah Jlili

                  AccessViolent means that SolidWorks is trying to write a protected memory. So there must be something interfering there. Have you tried creating new sheets manually with the two computers and noticed any difference in the process?