2 Replies Latest reply on Oct 3, 2013 9:23 AM by Alex Brewer

    How do I get which line is which when using CreateCenterRectangle?

    Alex Brewer



      When calling a function like Rectangle = skMgr.CreateCenterRectangle(0#, 0#, 0#, Width / 2, Length / 2, 0#) that returns an array of sketch elements, how do I tell which line is which? I'd like to be able to know left from right, and top from bottom. I can group the top and bottom, left and right and the two diagonals by doing something like Rectangle(0).getlength() and grouping like values, but differenitating left from right is turning out to be a trick.


      Is there a way to, say, get the sketch points from a sketch entity (midpoint, ends, center of an arc/circle, etc.)?


      I searched through the API help for sketching and it's pretty difficult to find something without knowing the name of the method you're looking for.

        • Re: How do I get which line is which when using CreateCenterRectangle?
          Keith Rice



          There's a lot of ways you could do this. Ultimate you just need to get the sketch points (ISketchPoint) used in the rectangle and then use ISketchPoint::X,Y,Z to get the locations relative to one another.


          To get the sketch points, here are two options:


          1. Use ISketch::GetSketchPoints2 to get all of the sketch points in the current sketch. (You can use IModelDoc2::GetActiveSketch2 to get the ISketch pointer)


          2. ISketchManager::CreateCenterRectangle returns ISketchSegment pointers (including reference geometry). You can cast these to ISketchLine and then use ISketchLine::GetEndPoint2 and ISketchLine::GetStartPoint2 to get the start and end points, which are ISketchPoint objects.


          To cast you simply do this:


          Dim swSketchSeg as SldWorks.SketchSegment

          Dim swSketchLine as SldWorks.SketchLine


          Set swSketchLine = swSketchSeg


          Test if a segment is reference geometry like this:


          If swSketchSeg.ConstructionGeometry = 0 Then

               'Do your work

          End if



          SolidWorks API Tutorials

            • Re: How do I get which line is which when using CreateCenterRectangle?
              Alex Brewer



              Thank you for clearing that up! The second method you outlined is the direction I'm trying to go. It never would have occurred to me to re-cast the SketchSegment to a SketchLine; I didn't realize that was an option.


              For some reason, the GetStartPoint2 method wouldn't work for me; it returned the "Object doesn't support this property or method" runtime error. The obsolete GetStartPoint method, however, worked just fine.


              Here's the test code that worked for me:


              Dim swApp As Object
              Dim Part As Object

              Sub main()

              Set swApp = Application.SldWorks
              Set Part = swApp.ActiveDoc

              Dim vSkLines() As SldWorks.SketchSegment
              Dim skSeg As SldWorks.SketchLine
              vSkLines = Part.SketchManager.CreateCenterRectangle(0, 0, 0, 9.78999538527745E-02, -5.23883444770945E-02, 0)
              Set skSeg = vSkLines(0)
              end1 = skSeg.GetEndPoint

              MsgBox end1(0) & vbNewLine & end1(1) & vbNewLine & end1(2)
              Part.SketchManager.InsertSketch True


              End Sub