Hide Table of Contents

SolidWorks

Get BCurve Spline Points Example (C#)

 //---------------------------------------------------------------------------------------------------------

 // This macro demonstrates how to get knots, control points, and other parameters for a selected spline

 // (or any selected curve).

 //

 // 1. Open a part document that contains a sketch with a spline curve

 //     (e.g., <install_dir>\samples\tutorial\molds\telephone.sldprt).

 // 2. Select the spline curve (select Shape Feature1 in the FeatureManager design tree of telephone.sldprt and then select the top blue spline curve).

 // 3. Open an immediate window to view messages.

 // 4. Run the Main() subroutine.

 // 5. View the following information about the selected spline in the immediatge window:

 //    * dimension

 //    * order or degree

 //    * number of control points

 //    * periodicity

 //    * number of knots:

 //         if periodicity = 1,

 //            number of control points + 1

 //         else

 //             number of control points + order

 //    * each knot's double value between 0 and 1, inclusive

 //    * coordinates of each control point for the given dimension:

 //          1-D:  X

 //          2-D:  X, Y

 //          3-D:  X, Y, Z

 //          4-D:  X, Y, Z, W (where W is the weight of the control point)

 //---------------------------------------------------------------------------------------------------------

 

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

using System.Diagnostics;

namespace GetBCurveParams4_CSharp.csproj

{

    public partial class SolidWorksMacro

    {public SldWorks swApp;

        public ModelDoc2 Part;

        public SelectionMgr swSelectMgr;

        public SketchSegment swSketchSeg;

        public Curve swCurveIn;

        public Double[] varSplineParams;

        public Double[] dSplineParams;

        // Extract a long value out of a packed double at a specific index

        public long ExtractFields(double dValue, int index)

        {

            byte[] byteArray = BitConverter.GetBytes(dValue);

            return BitConverter.ToInt32(byteArray, index);

        }

        public void Main()

        {

            int i = 0;

            int j = 0;

            // swApp = Interaction.CreateObject("SldWorks.Application");

            Part = (ModelDoc2)swApp.ActiveDoc;

            swSelectMgr = (SelectionMgr)Part.SelectionManager;

            swSketchSeg = (SketchSegment)swSelectMgr.GetSelectedObject5(1);

            swCurveIn = (Curve)swSketchSeg.GetCurve();

            // Get non-cubic, rational, nonperiodic, closed spline parameters

            varSplineParams = (Double[])swCurveIn.GetBCurveParams4(false, false, true, true);

            long iNumKnots = 0;

            long iNumCtrlPts = 0;

            long iDimension = 0;

            long iOrder = 0;

            long iPeriodicity = 0;

            long iSplineArraySize = 0;

            double dTmpValue1 = 0;

            double dTmpValue2 = 0;

            int iSplineIndex = 0;

            int iVarIndex = 0;

            // Obtain the curve's dimension and its order (degree) from the first packed double

            dTmpValue1 = varSplineParams[0];

            iDimension = ExtractFields(dTmpValue1, 0);

            iOrder = ExtractFields(dTmpValue1, 4);

            // Obtain the curve's number of control points and its periodicity from the second packed double

            dTmpValue2 = varSplineParams[1];

            iNumCtrlPts = ExtractFields(dTmpValue2, 0);

            iPeriodicity = ExtractFields(dTmpValue2, 4);

            // Calculate the number of knots according to periodicity and order (degree) of the spline

            if ((iPeriodicity == 1))

            {

                iNumKnots = iNumCtrlPts + 1;

            }

            else

            {

                iNumKnots = iNumCtrlPts + iOrder;

            }

            //Calculate the size of the spline parameters array

            iSplineArraySize = 2 + iNumKnots + (iDimension * iNumCtrlPts);

            // Populate spline parameters double array using data returned from GetBCurveParams4

            dSplineParams = new Double[iSplineArraySize];

            // First packed double contains the dimension and order

            dSplineParams[0] = dTmpValue1;

            // Second packed double contains the number of control points and the periodicity

            dSplineParams[1] = dTmpValue2;

            // Process remaining double values in varSplineParams (knots and control points)

            iSplineIndex = 2;

            iVarIndex = 2;

            Debug.Print("*******************************");

            Debug.Print("GetCurveParam4 ");

            Debug.Print("*******************************");

            Debug.Print("Dimension: " + iDimension);

            Debug.Print("Order or degree: " + iOrder);

            Debug.Print("Number of control points: " + iNumCtrlPts);

            Debug.Print("Periodicity: " + iPeriodicity);

            Debug.Print("Number of knots: " + iNumKnots);

            // Knots

            Debug.Print("*******************************");

            Debug.Print("Knots");

            Debug.Print("--------------");

            for (i = 0; i <= (iNumKnots - 1); i++)

            {

                // Populate spline parameters array with knot values

                dSplineParams[iSplineIndex] = varSplineParams[iVarIndex];

                // Print double value between 0 and 1, inclusive, for each knot in the curve

                Debug.Print(varSplineParams[iVarIndex].ToString());

                iSplineIndex = iSplineIndex + 1;

                iVarIndex = iVarIndex + 1;

            }

            // Control points

            Debug.Print("*******************************");

            Debug.Print("Control points");

            for (i = 0; i <= (iNumCtrlPts - 1); i++)

            {

                Debug.Print("--------------");

                for (j = 1; j <= iDimension; j++)

                {

                    // Populate spline parameters array with control points

                    dSplineParams[iSplineIndex] = varSplineParams[iVarIndex];

                    // Print coordinates of each control point

                    if ((j == 1))

                    {

                        Debug.Print("X:" + varSplineParams[iVarIndex].ToString());

                    }

                    else if ((j == 2))

                    {

                        Debug.Print("Y:" + varSplineParams[iVarIndex].ToString());

                    }

                    else if ((j == 3))

                    {

                        Debug.Print("Z:" + varSplineParams[iVarIndex].ToString());

                    }

                    else if ((j == 4))

                    {

                        Debug.Print("W:" + varSplineParams[iVarIndex].ToString());

                    }

                    iSplineIndex = iSplineIndex + 1;

                    iVarIndex = iVarIndex + 1;

                }

            }

            object varSplinePtParams = null;

            varSplinePtParams = dSplineParams;

            object varSplinePts = null;

            // Pass spline parameters as variant array to GetSplinePts(),

            // which returns a variant array of doubles for coordinates of each spline point:

            // [ x1, y1, z1, x2, y2, z2,....]

            varSplinePts = swCurveIn.GetSplinePts((varSplinePtParams));

        }

    }

}



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get BCurve Spline Points Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2010 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.