4 Replies Latest reply on Aug 10, 2017 11:58 AM by John Belding

    CreateCornerRectangle() Method help

    John Belding

      Good morning all,

       

      The question I have is in regards to the CreateCornerRectangle() method in the SketchManager interface. I can successfully create the rectangle sketch, however, the rectangle created seems to ignore one of my Y coordinate values (almost seems to be snapping to the nearest face on the part in question).

       

      The following lines of code are in questions:

       

      ' --------------------------------------

       

      ' Ensure no selections are active before selecting the Z = 0 plane (Front Plane) as a sketch plane

      '---------------------------------------

      swPart.ClearSelection2 True

      boolstatus = swModelDocExt.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

         

      ' ---------------------------------------

      ' Create a new sketch on the now selected plane

      ' ---------------------------------------

      swSketchManager.InsertSketch True

         

      ' ---------------------------------------

      ' Create a corner rectangle using the calculated Stock Body dimensions - Function ignore Z values as we are sketching on the Z = 0 plane

      ' ---------------------------------------

      vSkLines = swSketchManager.CreateCornerRectangle(Round(SBDimensions(0), 8), _

                                                                                              Round(SBDimensions(4), 8), _

                                                                                              0, _

                                                                                              Round(SBDimensions(3), 8), _

                                                                                              Round(SBDimensions(1), 8), _

                                                                                              0)                    

      ' ---------------------------------------

      ' Finalize sketch

      ' ---------------------------------------

      swSketchManager.InsertSketch True

       

      The values for the SBDimension() are as follows:

      •     : SBDimensions(0) : -0.150000005960464 : Double
      •     : SBDimensions(1) : -0.171043044798851 : Double
      •     : SBDimensions(3) : 0.150000005960464 : Double
      •     : SBDimensions(4) : 0.170000001788139 : Double
      •     I have the Z values for each point set to 0 as I am sketching on the Z = 0 plane (Front Plane)

       

      The problem is, the created rectangle has the following values for the first and second point

      Upper Left corner - (-0.15000001, 0.17000000, 0) - This is correct

      Lower Right corner - (0.15000000, -0.16990000, 0) - This is incorrect (Y value should be -0.17104304 as per the function definition)

      Like I mentioned above, the Y value seems to be snapping coincident to the face of the part in question (when I am trying to sketch a stock body that has an offset (0.045")  from said face) - Is this the likely problem, if so, how can I work around this?

       

      I look forward to any outside opinions on this.

       

      Kind Regards,

      Brad

        • Re: CreateCornerRectangle() Method help
          Josh Brady

          Creating sketch entities through API doesn't automatically turn off snapping.  Look up SetAddToDB in the API help.

            • Re: CreateCornerRectangle() Method help
              John Belding

              Good morning Josh,

               

              Thank you for your reply. I tried setting swSketchManager.AddToDB = True before creating the rectangle and then resetting to False afterwards. This doesn't seem to disable the snapping.

               

              I can work around this by creating Sketch(1) over sized to the point where it will not snap, then deleting Sketch(1) and creating Sketch(2) with the desired values (At this point it seems as though snapping has been disabled by SolidWorks somehow). I would like to implement a more robust method utilizing the AddToDB functionality, however, I am unsure about potential next steps with this method.

               

              Kind Regards,

              Brad

                • Re: CreateCornerRectangle() Method help
                  John Alexander

                  Interesting, every time that I've seen AddToDB problems on the forum, it involves sketch entities snapping to other sketch entities in the same sketch.

                   

                  The Solidworks help doesn't exactly clarify whether or not this includes non-sketch features.

                  "One of the benefits of adding sketch entities directly to the database is that you can avoid grid and entity snapping. For example, if you create a sketch line whose endpoint is near another entity or near a grid point, the new line endpoint snaps to the other item or grid point. Setting ISketchManager::AddToDB to true avoids this behavior during sketch entity creation."

                   

                  Have you tested the rectangle creation in another context that would demonstrate that the discrepancy between the corner coordinate's actual vs desired values are due to snapping?

                    • Re: CreateCornerRectangle() Method help
                      John Belding

                      Hi John and Josh,

                       

                      I figured out the easiest way to do this. I think you are right, in that the AddToDB problems are confined to sketch entities. The lower right corner point was snapping to a face on an existing solid body. In order to get around this, I disabled snapping temporarily using the code below:

                       

                      swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swSketchInference, False

                       

                      My code is working just as expected now (able to accurately determine and create a Stock Body (Solid Body) that encompasses an existing Part body - while applying offset values dependent on the color of the extreme faces of the part - this was determined using Tess Triangle analysis)

                       

                      Thank you both for your swift responses.

                       

                      Kind Regards,

                      Brad