2 Replies Latest reply on Apr 12, 2018 10:15 AM by Harold Black

    Unable to figure out debugging issues

    Harold Black

      I've been working on some code that creates rectangles that approximates a curve, basically discretizing a spline. For the most part my code works. Some issues I have outlined below.


      1.     Some widths of the rectangles are not constant. I have debugged and still can't see where the problem is.

      2.     Sometimes a vertical line is extended past a horizontal line, even though I have a test to see if an extended operation is needed. When I run the code again, there are no extended line problems.

      3.     Not really an issue but I would like to figure out a way to change my conditional tests in the CreateEndSegment function to allow floating point calculations. I've tried this before but the logical statements didn't work for me.



      Outline of how the code works.


      1.     A spline is selected and the endpoints found. The spacing is found with this and the number of piles (rectangles).

      2.     A copy of this spline is created for each pile. The reason for this is that I need to draw a tangent line to the spline local to each rectangle.

      3.     In the For loop, I create the vertical lines and trim them below the spline (which is why I need the long horizontal line). I save the endpoints.

      4.     I check to see if the sketch segment I'm looping through is of the type SPLINE. If it is, I perform a trim operation on the copied spline so that only the portion in the current region is kept. I save the endpoints of the spline to a variable SplinePoints.

      5.     A line is created and constrained to be tangent to the spline segment. A point is created and constrained to be coincident to both the tangent line and the spline segment. I get the y coordinate of the point called TanY.

      6.     I run various checks to determine the following.


                1) Is the tangent point higher than both of the spline segment endpoints. If yes, draw the short horizontal line starting from the lesser y value of the spline segment endpoints.

                2) Is the tangent point lower than both of the spline segment endpoints. If yes, draw the short horizontal line using TanY as the y value.

                3) Is the tangent point between the spline segment endpoints. If yes, find which spline segment endpoint is lower and draw the short horizontal line segment starting from there.


      7.     After a short horizontal line is drawn, the correct vertical line (swPileSeg) is selected depending on what case I tested into. The line is then extended.

      8.     Since there maybe be another horizontal line drawn below the previous one, I need to check to see if the endpoint of swPileSeg is greater than the next SplinePoint (2*i+1). If it is greater, I need to extend the line again.

      9.     Lastly, I delete the construction geometry i.e. spline segments, tangent line, and tangent point.


      I have posted on this forum before on other issues with my code that I have fixed. I'm hoping to figure the rest of these issues out. I've supplied the code and the test file I'm using. I'm using SolidWorks 2018.

        • Re: Unable to figure out debugging issues
          Josh Brady

          Almost any time you create sketch geometry using API, you want to use SetAddToDB.  This is very often the culprit for inconsistent behavior in macros that create sketch geometry.


          If you don't turn on SetAddToDB, geometry creation will act as though you clicked with the mouse, and automatic entity snapping and inference etc. will happen, and they will happen at the scale of the current model view.  Even though you specify coordinates, if the point is too close to another entity it will snap there, and also create automatic relations etc.


          If I run your macro at a very far-out zoom level such that the geometry is very small on screen, it crashes completely because things snap on top of one another.  Putting "swModel.SetAddToDB True" makes it at least run consistently.  You can then work on debugging other issues.