Get Spline Points Example (C++)
This example shows how to get the points on a spline.
//-------------------------------------------------------
// Preconditions:
// 1. Start up SOLIDWORKS and open a new part document.
// 2. Sketch a spline.
// 3. Select the Sketch1 feature of the spline in the
// FeatureManager design tree.
//
// NOTE: Do not select the spline in the graphics area. This
// causes an assert, which results in you having to abort and
// terminate the debug session.
//
// 4. Start Microsoft Visual Studio 2010.
// a. Click File > New > Project > Visual C++ >
// Win32 Console Application.
// b. Type the name of your project in Name.
// c. Click OK.
// d. Click Next.
// e. Select ATL and click Finish.
// f. Click Project > projectname Properties > Configuration Properties >
// C/C++ and type the path to sldworks.tlb and swconst.tlb,
// typically C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS,
// in Additional Include Directories.
// g. Click OK.
// h. Click Tools > Options > Debugging > General and select
// Redirect all Output Window Text to the Immediate Window.
// i. Click OK.
// 5. Open the Immediate Window.
// 6. Replace the code in the code window with this code.
// 7. Click Debug > Start Debugging.
// 8. Click Yes.
//
// Postconditions:
// 1. Prints to the Immediate Window the number
// of spline points followed by the x, y, z
// values of each point in the spline.
// 2. Examine the Immediate Window.
//-------------------------------------------------------
//This code
#include "stdafx.h"
#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids // SOLIDWORKS type library
#import "swconst.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids // SOLIDWORKS constants type library
#include <iostream>
#include <sstream>
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM
// Do this before using ATL smart pointers so that
// COM is available
::CoInitialize(NULL);
{
//Use ATL smart pointers
CComPtr<ISldWorks> pSwApp;
if(pSwApp.CoCreateInstance(L"Sldworks.Application", NULL, CLSCTX_LOCAL_SERVER) != S_OK) {
return(0);
}
CComPtr<IModelDoc2> pSwModel;
pSwApp->get_IActiveDoc2(&pSwModel);
//Get the selected sketch feature
CComPtr<ISelectionMgr> pSwSelMgr;
pSwModel->get_ISelectionManager(&pSwSelMgr);
IDispatch *pDisp;
pSwSelMgr->GetSelectedObject6(1, -1, &pDisp);
CComPtr<IFeature> pSwFeat;
if(S_OK == pDisp->QueryInterface(IID_IFeature, (void**)&pSwFeat));
pDisp = NULL;
//Get the sketch feature
pSwFeat->GetSpecificFeature2(&pDisp);
CComPtr<ISketch> pSwSketch;
if(S_OK == pDisp->QueryInterface(IID_ISketch, (void**)&pSwSketch));
pDisp = NULL;
//Gets the spline points by interpolation
VARIANT vSplines;
::VariantInit(&vSplines);
pSwSketch->GetSplinesInterpolate(&vSplines);
CComPtr<IDispatch> pDispatchSafeArray = NULL;
CComPtr<ISafeArrayUtility> pSwSafeArray = NULL;
HRESULT hres;
hres = pSwApp->GetSafeArrayUtility(&pDispatchSafeArray);
hres = pDispatchSafeArray.QueryInterface<ISafeArrayUtility>(&pSwSafeArray);
//First value in vSplines array is the number of
//sketch (or spline) points, followed by the x, y, z
//coordinates of each point in the spline, so calculate
//the size of the array
long nbrSketchPoints;
pSwSketch->GetSketchPointsCount2(&nbrSketchPoints);
long splineArraySize = 1 + (nbrSketchPoints * 3);
//Print to the Immediate Window the
//number of spline points followed by the
//x, y, z values of each of point in the spline
for (int idx = 0; idx < splineArraySize; idx++)
{
double sketchPointValue;
pSwSafeArray->GetDouble(vSplines, idx, &sketchPointValue);
_bstr_t s;
s = (double)sketchPointValue;
OutputDebugString(s + "\n");
}
}
//Initialize COM
::CoUninitialize();
return(0);
}