7 Replies Latest reply on Nov 19, 2018 8:28 AM by J. R.

    Inconsistent performance of API calls

    J. R.

      Hi,

       

      For a long time now, I've been coding an application in Visual Basic that connects to Solidworks, and makes various measurements or manipulations of Temporary (Virtual) Bodies. I have noticed, many times now, that Solidworks API calls sometimes happen very quickly, and sometimes they are slow, even if I'm repeating the exact same operation. Trying to track down the cause, I made absolutely sure that my application repeats the same operations identically (no broken references, no memory leaks or leftover Temporary Bodies, etc.), yet somehow, seemingly randomly, the operation that takes, say, 6 seconds normally, sometimes takes 23.

      I have tried to catch the culprit many times, adding breakpoints to see which operations take the most time, and several times I did manage to narrow down that slow performance to exact API calls (such as Operations2, Display3, CreateBodyFromBox, etc.). Thing is, sometimes they run fast, and sometimes they run slow, even if I'm running the code in identical manner (starting Solidworks, starting my IDE, running the code with API calls, noting the time elapsed, closing down everything, and then repeating all these steps again to make sure there are no memory leaks/leftover bodies, etc.)

       

      It's almost as if Solidworks has it's "good days" and "bad days". I have spent dozens of hours trying to figure this out (most of it making absolutely sure that it's not my code that's causing this), and I still have no clue. Have any of you ever noticed anything similar? Is this something that happens only during external interfacing with Solidworks (as opposed to macros or plug-ins)?

       

      P.S. I am running Solidworks 2018 x64 SP3 on a powerful PC with no other applications running. I tested the antivirus (by disabling it), but it doesn't seem to affect this. I can provide more info if needed, but I am not sure where to look for the cause.

        • Re: Inconsistent performance of API calls
          Gertjan Van Dijk

          This is something that can be caused by many things, but (in my experience), mainly because SW expects or suspects user input somewhere during an API call.

           

          in order to prevent this on expensive sections of code, all my programs that interact with solidworks contain a method that tells solidworks to ignore anything the user does, and listen only to the API calls made by the program.

           

          sub SpeedUpSW(speedUp as boolean)

               model as ModelDoc2 = swApp.ActiveDoc

               featMan as FeatureManager = model.FeatureManager

           

               featMan.EnableFeatureTree = not SpeedUp

               swApp.CommandInProgres = speedUp

               swApp.UserControl = not speedUp

               swApp.userControlBackground = speedUp

          end sub

           

          when i am about to give a lot and/or expencive commands, i call SpeedUpSW(true) to make them go by as fast as possible.

           

          one thing to note: make sure to call SpeedUp(false) as well, even when the code fails; since the situation caused by this block can only be reset by the same program, or by restarting SW

            • Re: Inconsistent performance of API calls
              J. R.

              Thank you, Gertjan. I have been using the swApp.CommandInProgres in my application for a while, and it does seem to improve the performance a lot. As for the rest of the calls in your SpeedUp sub, I tried them out now, but it didn't seem to affect performance a lot (an operation that usually takes 1 min. 16 sec. now took 1 min 15 sec.). Do these calls improve the performance for you? Could you please check if they make any difference in performance for you on their own (regardless of swApp.CommandInProgres)?

            • Re: Inconsistent performance of API calls
              Artem Taturevych

              Is it happening during debug or release? If the first one this might be related to some debug symbols being loaded by Visual Studio.

              • Re: Inconsistent performance of API calls
                William Cruz

                Depending on what type of task, try putting it on a separate thread. It could be that SW is doing something in the background at the time that you are making calls to the API. There are many ways to do this, here is one of the ways I do it in C#:

                 

                var t = Task.Factory.StartNew(() => YOURMETHOD());
                t.Wait();/*you can comment this out based on your needs*/