Issue
When using C++, I have problems converting an AcGePoint3d type to a VARIANT. Is there sample code that does this?
Solution
The following code demonstrates how to convert an AcGePoint3d type to a VARIANT by calling asDblArray() global function from AcGePoint3d to a double array. Notice that the C++ code is generic.
Note that there is minimal error checking code for code brevity.
//convert to point from variant
HRESULT getDblArrayFromVariant(double pt[3],
const VARIANT* pVal)
{
if (!(pVal->vt & VT_ARRAY && pVal->vt & VT_R8))
return E_INVALIDARG;
SAFEARRAY* sPt = pVal->parray;
if (SafeArrayGetDim(sPt) != 1)
return E_INVALIDARG;
long lLbound;
long lUbound;
SafeArrayGetLBound(sPt, 1, &lLbound);
SafeArrayGetUBound(sPt, 1, &lUbound);
if ((lUbound - lLbound + 1) != 3)
return E_INVALIDARG;
HRESULT hr;
for (long i = 0; i < 3; i++)
if ((hr = SafeArrayGetElement(sPt, &i, &pt[i]))!=S_OK)
return hr;
return S_OK;
}
//
//
////convert to variant from point
HRESULT getVariantFromDblArray(VARIANT* pVal,
const double pt[3])
{
pVal->vt = VT_ARRAY | VT_R8;
SAFEARRAYBOUND rgsaBound;
rgsaBound.lLbound = 0L;
rgsaBound.cElements = 3;
pVal->parray =
SafeArrayCreate(VT_R8, 1, &rgsaBound);
if (! pVal->parray)
return E_OUTOFMEMORY;
HRESULT hr;
for (long i = 0; i < 3; i++)
if ((hr = SafeArrayPutElement(
pVal->parray, &i,
(void*)&pt[i]))!=S_OK)
return hr;
return S_OK;
}
static void test()
{
AcGePoint3d pt3d = AcGePoint3d(10,20,30);
HRESULT hr;
// AcGePoint3d to Variant
VARIANT vt;
hr = getVariantFromDblArray(&vt,asDblArray(pt3d));
//Variant to AcGePoint3d
double pt[3];
hr = getDblArrayFromVariant(pt,&vt);
AcGePoint3d pt3d_1 = AcGePoint3d(pt[0],pt[1],pt[2]);
}