Hide Table of Contents

Get Spline Points Example (C++ COM)

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 spline feature in the FeatureManager design
//    tree.
// 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. Create a header file called smartvars.h containing this code and
//    add the header file to your project.
// 8. Click Debug > Start Debugging
// 9. Click Yes.
//
// Postconditions: Prints to the Immediate Window
// the number of spline points followed by the 
// x, y, z values of each of point in the spline.
//-------------------------------------------------------
//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   // SoldWorks constants type library
#include <iostream>
#include <sstream> 
#include "smartvars.h"
 
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 spline 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 of the spline
		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;
		pSwSketch->GetSplinesInterpolate(&vSplines);
 
		SafeDoubleArray arrSplines(vSplines);
		for (int idx = 0; idx < arrSplines.getSize(); idx++)
		{
			_bstr_t s;
			s = (double)arrSplines[idx];
			//Print the spline values to the Immediate Window as: 
			//Number of spline points followed by the 
			//x, y, z values of each of point in the spline
			OutputDebugString(s + "\n");
		}
 
	}
 
	//Initialize COM
	::CoUninitialize();
 
	return(0);
}
 
//smartvars.h
#pragma once
 
#include <cassert>
 
template <class T,int type> class SafeArray
 
{
 
		public:
 
		SafeArray(VARIANT *input):
 
			m_input(input),
 
			m_access(false),
 
			m_bCreate(false),
 
			m_pSafeArray(NULL),
 
			m_arrayData(NULL),
 
			m_result(S_OK)
 
{
 
				if(m_input != NULL)
 
				{
 
					if (V_VT(m_input) != VT_EMPTY)
 
				{
 
						m_pSafeArray =  V_ARRAY (m_input);
 
						m_result =  SafeArrayAccessData ( m_pSafeArray, (void HUGEP**)&m_arrayData);
 
						m_access = m_result == S_OK ; 
 
				}
 
				else
 
						m_access = true;
 
				}
 
				else
 
				{
 
					ASSERT(FALSE);
 
					m_input = &m_target;
 
				}
 
		}
 
		SafeArray(const VARIANT &input) :
 
			m_access(true), 
 
			m_bCreate(false),
 
			m_pSafeArray(NULL),
 
			m_arrayData(NULL),
 
			m_result(S_OK)
 
		{
 
				m_input = (VARIANT *) &input;
 
				if (V_VT(m_input) != VT_EMPTY)
 
				{
 
					m_pSafeArray =  V_ARRAY (m_input);
 
					m_result =  SafeArrayAccessData ( m_pSafeArray, (void HUGEP**)&m_arrayData);
 
					m_access = m_result == S_OK ; 
 
				}
 
				else
 
					m_access = true;
 
		}
 
		SafeArray(unsigned int size,unsigned int dims = 1):
 
			m_input(&m_target),
 
			m_access(false),
 
			m_bCreate(true),
 
			m_pSafeArray(NULL),
 
			m_arrayData(NULL),
 
			m_result(S_OK)
 
		{
 
		ASSERT(size >= 0);
 
		if(size > 0)
 
		{
 
			m_rgsabound[0].lLbound = 0;
 
			m_rgsabound[0].cElements = (int) size;
 
			m_pSafeArray =  SafeArrayCreate(type, (int)dims, m_rgsabound);
 
			V_VT(m_input) = VT_ARRAY | type;
 
			m_result = SafeArrayAccessData( m_pSafeArray, (void HUGEP**)&m_arrayData);
 
			m_access = m_result == S_OK ; 
 
			if(!m_access)
 
				V_VT(m_input) = VT_EMPTY;
 
		} 
 
		else
 
		{
 
			V_VT(m_input) = VT_EMPTY;
 
			m_access = true;
 
		}
 
	}
 
 
 
	inline HRESULT status(){return m_result;}
 
	inline int getSize(int index = 0) {return (m_access&&m_pSafeArray)?m_pSafeArray->rgsabound[index].cElements:0;}
 
 
 
	~SafeArray(){UnaccessData();}
 
 
 
	operator T* () {ASSERT(m_access);return m_arrayData;}
 
	operator VARIANT () {ASSERT(m_access);UnaccessData();return *m_input;}
 
	T & operator[](int i) {assert(m_access&&m_arrayData);return m_arrayData[i];}
 
	SAFEARRAY* getSafeArrayData() {return m_pSafeArray;}
 
 
private:
 
		void UnaccessData(){
 
			if(m_access){
 
				m_result = SafeArrayUnaccessData( m_pSafeArray );
 
				if(m_bCreate && m_result == S_OK)
 
					V_ARRAY(m_input) = m_pSafeArray;
 
				m_access = false;
 
			}
 
		}
 
		bool m_bCreate;
 
		bool m_access;
 
		VARIANT *m_input;
 
		VARIANT m_target;
 
		T *m_arrayData ;  
 
		SAFEARRAY *m_pSafeArray;
 
		SAFEARRAYBOUND  m_rgsabound[1]; 
 
		HRESULT m_result;
 
	};
 
template <typename T> inline T GetSpecificInterface(IUnknown* pUnk, const IID& iid, T &specificInterface)
{
	specificInterface = NULL;
 
	If (NULL != pUnk)
	{
		HRESULT res = pUnk->QueryInterface(iid, (LPVOID*)&specificInterface);
		if (specificInterface)
			specificInterface->Release
	}
 
	return specificdInterface;
}
 
typedef SafeArray<VARIANT_BOOL, VT_BOOL>		SafeBooleanArray ;
typedef SafeArray<double,       VT_R8>			SafeDoubleArray ;
typedef SafeArray<long,         VT_I4>			SafeLongArray ; 
typedef SafeArray<BYTE,         VT_I1>			SafeByteArray ;
typedef SafeArray<BYTE,			VT_UI1>			SafeUByteArray ;
typedef SafeArray<BSTR,         VT_BSTR>		SafeBSTRArray ;
typedef SafeArray<LPDISPATCH,   VT_DISPATCH>	SafeDISPATCHArray ;
typedef SafeArray<LPVARIANT,    VT_VARIANT>		SafeVARIANTArray ;
 



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 Spline Points Example (C++ COM)
*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) 2014 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.