8 Replies Latest reply on Sep 22, 2017 1:27 AM by Seth Morgan-Ruhan

    sketch segments projected intersection

    Seth Morgan-Ruhan

      Hi, I've been looking for a method to determine the intersection of sketch segments (line,Arc, spline etc)  where the segments either intersect or the projected intersection point. I've used ICurve.IntersectCurve, Moddoc:ClosestDistance etc which don't work for projected, and inserting an actual point can be difficult particularly with projections and splines. Is there any methods that I've missed that will determine intersection points for both true intersections & projected intersections for sketch segments?

        • Re: sketch segments projected intersection
          Steven Barry

          What about adding an arbitrary sketch point, adding an intersection relation, and then retrieving the X,Y,Z coordinates of the point?

           

          2017 SOLIDWORKS API Help - CreatePoint Method (ISketchManager)

          2017 SOLIDWORKS API Help - AddRelation Method (ISketchRelationManager)

           

          You would probably want to:

               - make sure the two other sketch segments are fully defined

                    - If not, maybe temporarily "Fix" them

               - Possibly delete the point after extracting its location, if you don't intend to keep it.

            • Re: sketch segments projected intersection
              Seth Morgan-Ruhan

              Thanks for the suggestion Steven. I have already explored this route. It is not perfect but assumed there must be a better method that I've missed.

               

              The fundamental task is to offset edges of a face at different intervals but join these offset lines together for an extrude.

               

              Below is what I have, basically it determines if they already intersect, if not if they are lines\arcs check if they intercept via a Create point\Vertex method and if there is a spline in the mix add a tangent line and repeat.

               

              Entry is

                    Dim sktseg1 As SketchSegment = selmgr.GetSelectedObject6(1, -1)

                   Dim sktseg As SketchSegment = selmgr.GetSelectedObject6(2, -1)

                   Dim point As Object = GetIntersectionPoint(moddoc, sktseg1, sktseg)

              and the functions

              Function GetIntersectionPoint(ByVal moddoc As ModelDoc2, ByVal seg1 As SketchSegment, ByVal seg2 As SketchSegment) As Double()

                      ''Check if they intersect now

                      Dim Pnt As Double() = GetIntersection(seg1, seg2)

               

               

                      If (Pnt IsNot Nothing) Then

                          Return Pnt

                      Else 'Get projected intersection

                          Dim res As SketchPoint = GetProjectedIntersection(moddoc, seg1, seg2)

                          If (res IsNot Nothing) Then

                              ReDim Pnt(2)

                              Pnt(0) = res.X

                              Pnt(1) = res.Y

                              Pnt(2) = res.Z

                              'TODO delete point

                              Return Pnt

                          Else

                              Return Nothing

                          End If

                      End If

                  End Function

                  Function GetIntersection(ByRef swSketchSegA As SketchSegment, ByRef swSketchSegB As SketchSegment) As Double()

                      'Optional ByRef RealIntersection, Optional ByRef PossibleIntersection

                      'Returns an array of doubles containing the x, y, z location of the tessellation points and a code for each point

                      '  Null if no intersection is found

                      Dim swCurveA, swCurveB As Curve

                      Dim Astart(), Aend(), Bstart(), Bend() As Double

                      Dim pointArray As Object

               

               

                      'Assume failure

               

               

                      swCurveA = swSketchSegA.GetCurve

                      If Not GetStartEndPoint(swSketchSegA, Astart, Aend) Then Return Nothing

                      swCurveB = swSketchSegB.GetCurve

                      If Not GetStartEndPoint(swSketchSegB, Bstart, Bend) Then Return Nothing

               

               

                      pointArray = swCurveA.IntersectCurve(swCurveB, Astart, Aend, Bstart, Bend)

                      If (DBNull.Value Is pointArray) Then Return Nothing

                      Return pointArray

               

               

                      'How can I decide between real and possible intersection points here?

               

               

                  End Function

                  Function GetProjectedIntersection(ByVal moddoc As ModelDoc2, ByVal seg1 As SketchSegment, ByVal seg2 As SketchSegment) As SketchPoint

                      Dim selmgr As SelectionMgr = moddoc.SelectionManager

                      Dim sktmgr As SketchManager = moddoc.SketchManager

                      Dim skt As Sketch = sktmgr.ActiveSketch

                      Dim relmgr As SketchRelationManager = skt.RelationManager

               

               

               

               

               

               

                      Dim SPcnt As Object = skt.GetSketchPoints2

                      seg1.Select2(False, -1)

                      seg2.Select2(True, -1)

               

               

                      Dim Sp As SketchPoint = moddoc.SketchManager.CreatePoint(0, 0, 0)

               

               

                      Dim SPcnt2 As Object = skt.GetSketchPoints2

               

               

                      If (UBound(SPcnt) + 2 = UBound(SPcnt2)) Then 'Check if vertex was added

                          Sp.Select2(False, -1)

                          moddoc.DeleteSelection(True)

                          Return SPcnt2(UBound(SPcnt2) - 2)

               

               

                      ElseIf (seg1.GetType = swSketchSegments_e.swSketchSPLINE OrElse (seg2.GetType = swSketchSegments_e.swSketchSPLINE)) Then 'Check if they are a spline

                          Sp.Select2(False, -1)

                          moddoc.DeleteSelection(True)

               

               

                          Dim seg1Start, seg1End, seg2Start, Seg2End As Double()

                          Dim Seg1Cvt, Seg2Cvt As Boolean

               

               

               

               

                          'If either are spline create tangent line and check if we can get an intersection

                          'TODO Still need to chedk if we should use start or end point of spline.

                          If (seg1.GetType = swSketchSegments_e.swSketchSPLINE) Then

                              Seg1Cvt = True

                              seg1 = LineTangentToSpline(sktmgr, seg1, True)

                              seg1.ConstructionGeometry = True

                              GetStartEndPoint(seg1, seg1Start, seg1End)

                          Else

                              GetStartEndPoint(seg1, seg1Start, seg1End)

                          End If

               

               

                          If (seg2.GetType = swSketchSegments_e.swSketchSPLINE) Then

                              Seg2Cvt = True

                              seg2 = LineTangentToSpline(sktmgr, seg2, True)

                              seg2.ConstructionGeometry = True

                              GetStartEndPoint(seg2, seg2Start, Seg2End)

                          Else

                              GetStartEndPoint(seg2, seg2Start, Seg2End)

                          End If

               

               

                          Dim AllPointsPre As Object = skt.GetSketchPoints2

                          seg1.Select2(False, -1)

                          seg2.Select2(True, -1)

                          Dim point As SketchPoint = moddoc.SketchManager.CreatePoint(0, 0, 0)

                          Dim AllPointsPost As Object = skt.GetSketchPoints2

               

               

                          If (UBound(AllPointsPre) + 2 = UBound(AllPointsPost)) Then

                              point.Select2(False, -1)

                              moddoc.DeleteSelection(True)

                              Return AllPointsPost(AllPointsPost.Count - 2)

                          End If

                      End If

               

               

                      Return Nothing

                  End Function

                  Function LineTangentToSpline(ByVal sktmgr As SketchManager, ByVal sktseg As SketchSegment, ByVal useEndPoint As Boolean) As SketchLine

                      Dim skt As Sketch = sktmgr.ActiveSketch

                      Dim relmgr As SketchRelationManager = skt.RelationManager

               

               

                      Dim swSkSpline As SketchSpline = sktseg

                      Dim swSkPoints As Object = swSkSpline.GetPoints2

               

               

                      Dim startpt As SketchPoint = swSkPoints(0)

                      Dim endpt As SketchPoint = swSkPoints(UBound(swSkPoints))

                      Dim int As Double = 0.01

                      Dim swLine As SketchLine = sktmgr.CreateLine(endpt.X, endpt.Y, endpt.Z, endpt.X + int, endpt.Y + int, endpt.Z)

               

               

                      If (swLine IsNot Nothing) Then

               

               

               

               

                          Dim ents(1) As SketchSegment

                          ents(0) = swSkSpline

                          ents(1) = swLine

                          Dim rel As SketchRelation = relmgr.AddRelation(ents, swConstraintType_e.swConstraintType_TANGENT)

                          If (rel IsNot Nothing) Then

                              Return swLine

                          Else

                              swLine.ConstructionGeometry = True

                              Return Nothing

                          End If

                      Else

                          Return Nothing

                      End If

                  End Function

                  Function GetStartEndPoint(ByRef swSegment As SketchSegment, ByRef StartPoint As Double(), ByRef EndPoint As Double()) As Boolean

                      'Based on https://forum.solidworks.com/message/393341

                      Dim swSkPoint1 As SketchPoint

                      Dim swSkPoint2 As SketchPoint

               

               

                      If TypeOf swSegment Is SketchSegment Then

                          Select Case swSegment.GetType

                              Case swSketchSegments_e.swSketchLINE

                                  Dim swSkLine As SketchLine

                                  swSkLine = swSegment

                                  swSkPoint1 = swSkLine.GetStartPoint2()

                                  swSkPoint2 = swSkLine.GetEndPoint2()

                              Case swSketchSegments_e.swSketchARC

                                  Dim swSkArc As SketchArc

                                  swSkArc = swSegment

                                  swSkPoint1 = swSkArc.GetStartPoint2()

                                  swSkPoint2 = swSkArc.GetEndPoint2()

                              Case swSketchSegments_e.swSketchSPLINE

                                  Dim swSkSpline As SketchSpline

                                  swSkSpline = swSegment

                                  Dim swSkPoints

                                  swSkPoints = swSkSpline.GetPoints2

                                  swSkPoint1 = swSkPoints(0)

                                  swSkPoint2 = swSkPoints(UBound(swSkPoints))

                              Case swSketchSegments_e.swSketchELLIPSE

                                  Dim swSkEllipse As SketchEllipse

                                  swSkEllipse = swSegment

                                  swSkPoint1 = swSkEllipse.GetStartPoint2()

                                  swSkPoint2 = swSkEllipse.GetEndPoint2()

                              Case swSketchSegments_e.swSketchPARABOLA

                                  Dim swParabola As SketchParabola

                                  swParabola = swSegment

                                  swSkPoint1 = swParabola.GetStartPoint2()

                                  swSkPoint2 = swParabola.GetEndPoint2()

                              Case Else

                                  Exit Function

                          End Select

                          ReDim StartPoint(0 To 2)

                          StartPoint(0) = swSkPoint1.X

                          StartPoint(1) = swSkPoint1.Y

                          StartPoint(2) = swSkPoint1.Z

                          ReDim EndPoint(0 To 2)

                          EndPoint(0) = swSkPoint2.X

                          EndPoint(1) = swSkPoint2.Y

                          EndPoint(2) = swSkPoint2.Z

                      ElseIf TypeOf swSegment Is Edge Then

                          Dim swEdge As Edge

                          swEdge = swSegment

                          Dim swCurveParamData As CurveParamData

                          swCurveParamData = swEdge.GetCurveParams3

                          StartPoint = swCurveParamData.StartPoint

                          EndPoint = swCurveParamData.EndPoint

                      Else

                          Exit Function

                      End If

                      Return True

               

               

               

               

               

               

                  End Function

               

               

               

                • Re: sketch segments projected intersection
                  Ratnadeep Mukkirwar

                  Did you try using a combination of ICurve::IntersectCurve and ISketchManager::IntersectCurve. You can only get the intersection points using ICurve::IntersectCurve upto the point where you have trimmed your curves and not infinitely, so trim your curve to the maximum projection distance. This should provide you projected points. And then you can also use ISketchManager::IntersectCurve to get offsets curves for intersection of two edges.

                  • Re: sketch segments projected intersection
                    Nadav Gover

                    Seems like you have the true intersection points if I understood correctly. if not let me know I have it written in c#.

                    I think though, that what you have now also gives you the intersection points even if the intersection is at the SketchSegment end/start point. It might be what you need I don't know.

                    Anyway, to get the projection points I think it will be best if you use calculus methods instead of SolidWorks methods in this case. Not advanced calculus don't worry

                    If you have the start and end point of a line than you can get the line equation, which will be helpful.

                    If it is an arc so you need the center point and its radius (which you can get from SolidWorks) in order to know the equation of the arc/circle.

                    Similar thing to parabola and ellipse. Spline is kinda not supported with this method because it doesn't really have an equation.

                    Once you have the equations you can find the intersection points. This will give you both true and projected points at the same time.

                    Take a look here 3D Coordinate Geometry - Equation of a Line | Brilliant Math & Science Wiki to see how to calculate a line's equation given to points. Arc (etc.) equation will be similar.

                    I know it is not a true Solid way to do that, but this will give you the results your after.

                      • Re: sketch segments projected intersection
                        Seth Morgan-Ruhan

                        Thanks Nadav for your suggestion, I ideally wanted to do it mathematically, however handling ellipses & splines and where they intersect partially if you are projected is a bit beyond me. The models I'm dealing with come from laser scanning (automatically) so splines do happen which is why I was looking for a robust method. If you think your function might have the key it would be very much appreciated if you were happy to share.