Get Spline Points Example (C++ COM)
This example shows how to get the points on a spline using IInterface and SafeDoubleArray
templates.
Sample.cpp: console application:
///////////////////////////////////////////////////////////////////////
//
// Preconditions:
// (1)
Model document containing a sketch of spline is open.
// (2)
The sketch of spline in the FeatureManager design tree is selected.
//
// Postconditions: None
//
// NOTE: Scroll down to see the code for the Sample.h,
resource.h,
// smartvars.h,
and stdafx.h header files.
///////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Sample.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#import "sldworks.tlb"
no_namespace raw_interfaces_only
#import "swconst.tlb"
no_namespace
#include "smartvars.h"
// The application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// Initialize MFC and print and error on
failure
if (!AfxWinInit(::GetModuleHandle(NULL),
NULL, ::GetCommandLine(), 0))
{
// TODO: Change error code to suit your
needs
_tprintf(_T("Fatal Error: MFC initialization
failed\n"));
nRetCode = 1;
}
else
{
//Initialize COM
::CoInitialize(NULL);{
ISldWorksPtr iApp;
iApp.CreateInstance(L"SldWorks.Application",
NULL, CLSCTX_LOCAL_SERVER);
IModelDoc2Ptr iDoc;
iApp->get_IActiveDoc2(&iDoc);
ISelectionMgrPtr iSelMgr;
iDoc->get_ISelectionManager(&iSelMgr);
IDispatchPtr iDisp;
iSelMgr->GetSelectedObject6(1,
-1, &iDisp);
IFeaturePtr iFeat(iDisp);
iDisp = NULL;
iFeat->GetSpecificFeature2(&iDisp);
ISketchPtr iSketch(iDisp);
iDisp = NULL;
VARIANT vSplines;
iSketch->GetSplinesInterpolate(&vSplines);
SafeDoubleArray arrSplines(vSplines);
for (int idx = 0; idx < arrSplines.getSize();
idx++)
{
::AfxTrace("arrSplines(%d) = %f\n",
idx, arrSplines[idx]);
}
}
//Initialize COM
::CoUninitialize();
}
return nRetCode;
}
Sample.h: header file
#pragma once
#include "resource.h"
resource.h: header file:
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include
file.
// Used by Sample.rc
//
#define IDS_APP_TITLE 103
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
smartvars.h: header file:
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];}
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;
};
typedef SafeArray<double, VT_R8>
SafeDoubleArray
;
typedef SafeArray<long, VT_I4>
SafeLongArray
;
typedef SafeArray<BSTR, VT_BSTR>
SafeBSTRArray
;
typedef SafeArray<LPDISPATCH, VT_DISPATCH>
SafeDISPATCHArray;
typedef SafeArray<LPVARIANT, VT_VARIANT>
SafeVARIANTArray;
stdafx.h: header file
// stdafx.h : include file for standard-system include
files
// or project-specific include files that are used frequently,
but
// are changed infrequently
//
#pragma once
#include <iostream>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString
constructors will be explicit
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from
Windows headers
#endif
#include <afx.h>
#include <afxwin.h> //
MFC core and standard components
#include <afxext.h> //
MFC extensions
#include <afxdtctl.h> //
MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> //
MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
// TODO: reference additional headers your program requires
here