6 Replies Latest reply on Nov 23, 2017 6:45 AM by Simon Turner

    Sketch / reference planes intersection

    Marco Maso

      Hello, I have a sketch with a spline and several planes intersecting it. Is there a way to get the points where the reference planes intersect the spline programmatically?

       

      Maybe using 3D sketches?

       

      Thank you in advance.

        • Re: Sketch / reference planes intersection
          Matthew Cempa

          One thought is that if you are doing it in a sketch you could probably use add sketch relations to a point to be on each curve and that would be the intersection.  Another approach is to create a surface body and use the IntersectCurve2 method.  I'm surprised there is no plane intersection method for a sketch segment.

          • Re: Sketch / reference planes intersection
            Newell Voss

            if the points you are after fall on the intersecting planes then why not just sketch a point on each plane and apply the pierce relation to the 3D spline.

            • Re: Sketch / reference planes intersection
              Simon Turner

              Just for the record, if you do want to do this programmatically, it is possible. But the answer is too long to give here.

              In summary, you get a Curve object from the sketch spline.

              Next you create a Surface object consisting of a single planar surface which represents the plane. You can use the Modeler object to create a temporary body which does not appear in the feature manager.

              Finally you call ISurface.IntersectCurve2 to get the coordinates of the intersection.

              • Re: Sketch / reference planes intersection
                Marco Maso

                I'm replying to the original post because both Simon Turner and Matthew Cempa gave me a similar response. Thank you for your answers.

                 

                The suggestion to use Planar Surfaces and the relative IntersectCurve2 is correct and works fine if we follow the example. Unfortunately I'm having problems when trying to change the plane orientation and with intersection points Z positions. Please see the following screens.

                 

                This is the result of the macro on a curve on a 2D sketch sitting at -0.640 m. The planar surface has been created with vRootPoint (0.0, 0.0, 0.0) and vNormal (1.0, 0.0, 0.0) as requested from 2017 SOLIDWORKS API Help - CreatePlanarSurface Method (IModeler). The macro automatically insert a 3D sketch with a line and a reference plane for visualization purposes only.

                 

                As you can see everything is ok except for the Z of the points (it's at 0.0 instead of -0.64).

                 

                What happens if we slightly rotate the normal?

                 

                 

                The points are at the same position as before. I'm not sure if I'm doing something wrong or if it's supposed to work like that. Here are some snippets of code.

                 

                Retrieving curve and curve bounds:

                 

                Curve mCurve = (Curve)mSegment.GetCurve();
                double sp, ep;
                bool closed, periodic;
                mCurve.GetEndParams(out sp, out ep, out closed, out periodic);
                double[] spt = (double[])mCurve.Evaluate(sp);
                double[] ept = (double[])mCurve.Evaluate(ep);
                double[] curveBounds = new double[6] { spt[0], spt[1], spt[2], ept[0], ept[1], ept[2] };
                

                 

                Creating planar surface:

                 

                Surface planarSurface = default(Surface);
                double[] vRootPoint = new double[3];
                double[] vNormalPoint = new double[3];
                vRootPoint[0] = 0.0;
                vRootPoint[1] = 0.0;
                vRootPoint[2] = 0.0;
                vNormalPoint[0] = 1.0; // 0.905 in second example
                vNormalPoint[1] = 0.0; // 0.0 in second example
                vNormalPoint[2] = 0.0; // 0.307 in second example
                planarSurface = (Surface)swModeler.CreatePlanarSurface((vRootPoint), (vNormalPoint));
                

                 

                Any help is appreciated. Thanks in advance.

                  • Re: Sketch / reference planes intersection
                    Simon Turner

                    I just got this to work by using the ModelToSketchTransform for the sketch that contains the spline.

                    Step 1: multiply the coordinates of the root point and vector of the plane by the ModelToSketchTransform matrix

                    Step 2: multiply the results of IntersectCurve2 by the inverse of that matrix

                    Here is a snippet from a VBA macro I used:

                     

                     

                        Dim myRootPoint(2) As Double

                        Dim vRootPoint As Variant

                        Dim myNormalPoint(2) As Double

                        Dim vNormalPoint As Variant

                        Dim myTran As MathTransform

                        Dim mPt As MathPoint

                        Dim vPt As MathVector

                        Dim myUtil As MathUtility

                        Dim mySketch As Sketch

                       

                        Set myUtil = swApp.GetMathUtility

                        Set mySketch = mySeg.GetSketch

                        Set myTran = mySketch.ModelToSketchTransform

                       

                        myRootPoint(0) = 0#

                        myRootPoint(1) = 0#

                        myRootPoint(2) = 0#

                        Set mPt = myUtil.CreatePoint(myRootPoint)

                        Set mPt = mPt.MultiplyTransform(myTran)

                        vRootPoint = mPt.ArrayData

                       

                        myNormalPoint(0) = 1# ' 0.905 in second example

                        myNormalPoint(1) = 0# ' 0.0 in second example

                        myNormalPoint(2) = 0# ' 0.307 in second example

                        Set vPt = myUtil.CreateVector((myNormalPoint))

                        Set vPt = vPt.MultiplyTransform(myTran)

                        vNormalPoint = vPt.ArrayData

                       

                        Set planarSurface = swModeler.CreatePlanarSurface((vRootPoint), (vNormalPoint))

                    ......

                     

                        If IsEmpty(pointArray) = False Then

                            Set myTran = mySketch.ModelToSketchTransform

                            Set myTran = myTran.Inverse

                       

                            Part.ClearSelection2 True

                            Part.Insert3DSketch2 False

                            Part.SketchManager.AddToDB = True

                            For i = 0 To UBound(pointArray) - 2 Step 3

                                p(0) = pointArray(i)

                                p(1) = pointArray(i + 1)

                                p(2) = pointArray(i + 2)

                                Set mPt = myUtil.CreatePoint((p))

                                Set mPt = mPt.MultiplyTransform(myTran)

                                sa = mPt.ArrayData

                                Part.SketchManager.CreatePoint sa(0), sa(1), sa(2)

                            Next

                            Part.SketchManager.AddToDB = False

                            Part.ClearSelection2 True

                            Part.Insert3DSketch2 True

                            Part.ClearSelection2 True

                        End If