Hide Table of Contents

Get Each Spline's Parameters Example (C#)

This example shows how to get each spline's parameters in a sketch containing multiple splines.

//----------------------------------------------------
// Preconditions:
// 1. Verify that the specified part template exists.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Creates a sketch containing two splines.
// 2. Gets each spline's dimension, order, periodicity,
//    control point, and knot point data.
// 3. Examine the Immediate window.
//-----------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
 
namespace Macro1CSharp.csproj
{
    public partial class SolidWorksMacro
    { 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            SelectionMgr swSelMgr = default(SelectionMgr);
            SketchSegment swSketchSegment = default(SketchSegment);
            Feature swFeature = default(Feature);
            Sketch swSketch = default(Sketch);
            SplineParamData swSplineParamData = default(SplineParamData);
            SketchManager swSketchMgr = default(SketchManager);
            double[] points = new double[12];
            object pointArray = null;
            object varCtrlPoints = null;
            double[] ctrlPoints;
            object varKnotPoints = null;
            double[] knotPoints;
            bool status = false;
            int i = 0;
            int j = 0;
            int k = 0;
            int splineCount = 0;
            int splinePointCount = 0;
 
            swModel = (ModelDoc2)swApp.NewDocument("C:\\ProgramData\\SolidWorks\\SOLIDWORKS 2017\\templates\\Part.prtdot", 0, 0, 0);
 
            //Create a sketch with two splines
            //First spline
            points[0] = -0.185955019567672;
            points[1] = 0.0416208582005027;
            points[2] = 0;
            points[3] = -0.0862492383138544;
            points[4] = 0.0403922105323034;
            points[5] = 0;
            points[6] = -0.0672740896322921;
            points[7] = 0.0540598971292923;
            points[8] = 0;
            points[9] = -0.0141436733240425;
            points[10] = -0.00570437188125084;
            points[11] = 0;
            pointArray = points;
            swSketchMgr = swModel.SketchManager;
            swSketchSegment = swSketchMgr.CreateSpline((pointArray));
            swModel.ClearSelection2(true);
            //Second spline
            points[0] = -0.0838342193907238;
            points[1] = -0.0380341664350112;
            points[2] = 0;
            points[3] = -0.0655490761158148;
            points[4] = -0.0179490921124739;
            points[5] = 0;
            points[6] = -0.0179387030603664;
            points[7] = -0.0681344637902441;
            points[8] = 0;
            points[9] = 0.0634819349185705;
            points[10] = -0.0329692207162395;
            points[11] = 0;
            pointArray = points;
            swSketchSegment = (SketchSegment)swSketchMgr.CreateSpline((pointArray));
            swModel.ClearSelection2(true);
            //Sketch
            swSketchMgr.InsertSketch(true);
 
            //Get each spline's dimension, order, periodicity, control point, and knot data
            status = swModel.Extension.SelectByID2("Sketch1""SKETCH", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
            swFeature = (Feature)swSelMgr.GetSelectedObject6(1, -1);
            swSketch = (Sketch)swFeature.GetSpecificFeature2();
            Debug.Print(swFeature.Name);
            Debug.Print("");
            splineCount = swSketch.GetSplineCount(ref splinePointCount);
            for (i = 1; i <= splineCount; i++)
            {
                Debug.Print("Spline " + i + ": ");
                swSplineParamData = (SplineParamData)swSketch.GetSplineParams5(true, i - 1);
                Debug.Print("  Dimension: " + swSplineParamData.Dimension);
                Debug.Print("  Order: " + swSplineParamData.Order);
                Debug.Print("  Periodicity: " + swSplineParamData.Periodic);
                Debug.Print("  Number of control points: " + swSplineParamData.ControlPointsCount);
                status = swSplineParamData.GetControlPoints(out varCtrlPoints);
                Debug.Print("  Control points:");
                ctrlPoints = (double[])varCtrlPoints;
                for (j = 0; j <= ctrlPoints.GetUpperBound(0); j++)
                {
                    Debug.Print("      " + ctrlPoints[j].ToString());
                }
                Debug.Print("  Number of knots: " + swSplineParamData.KnotPointsCount);
                status = swSplineParamData.GetKnotPoints(out varKnotPoints);
                Debug.Print("    Knot points:");
                knotPoints = (double[])varKnotPoints;
                for (k = 0; k <= knotPoints.GetUpperBound(0); k++)
                {
                    Debug.Print("      " + knotPoints[k].ToString());
                }
            }
 
        }
 
        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
    }
}


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 Each Spline's Parameters 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) 2018 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.