I'm trying to obtain the name of each component in an assembly file using an out-of-process C++ code. I have the assembly pointer IAssemblyDoc, and figured that the best way to do this would be to use the GetComponents member of this pointer. But I'm having difficulty figuring out how to convert my VARIANT return value into an array of component pointers, each of which would store the name for the given component. Here is my code so far:
CComPtr
CComPtr
CComPtr
HRESULT hres = NOERROR;
int i;
long fileError, fileWarning, num_com;
CoInitialize(NULL);
hres = swApp.CoCreateInstance(__uuidof(SldWorks), NULL, CLSCTX_LOCAL_SERVER);
hres = swApp->put_UserControl(VARIANT_TRUE);
hres = swApp->put_Visible(VARIANT_FALSE);
TCHAR szFileName[255];
_tcscpy(szFileName, _T("C:\\\\Temp\\\\ujoint.sldasm"));
_bstr_t sPartName(szFileName);
CComBSTR bsPartName;
bsPartName.AppendBSTR(sPartName);
CComBSTR sDefaultConfiguration(L"Default");
hres = swApp->OpenDoc6(bsPartName, swDocASSEMBLY, swOpenDocOptions_Silent, sDefaultConfiguration, &fileError, &fileWarning, &swModel);
hres = swModel->QueryInterface(IID_IAssemblyDoc, (void**)&swAssembly);
if (hres == S_OK && swAssembly)
{
VARIANT swComponentArray;
hres = swAssembly->GetComponentCount(VARIANT_TRUE, &num_com);
hres = swAssembly->GetComponents(VARIANT_TRUE, &swComponentArray);
but I'm at a loss at how to proceed. My goal is to obtain an array of pointers to IComponents2, so that I can use the member get_Name2 to obtain the name of each component in the assembly. Thank you!
SolidworksApi macros
The IComponent pointers are stored in a SafeArray. I do something like this to get them out:
=======================
VARIANT vComps;
HRESULT hr = swAssembly->GetComponents(VARIANT_TRUE, &vComps);
IDispatch* HUGEP *pDispData;
HRESULT hr = SafeArrayAccessData(vComps.parray, (void**)&pDispData);
long bound = 0;
hr = SafeArrayGetUBound(vComps.parray, 1, &bound);
for (int i = 0; i < count; i++)
{
IComponent2 *nextComp = NULL;
hr = pDispData[i]->QueryInterface(IID_IComponent2, (void**)&nextComp);
//do stuff with Component pointer
}
========================
Jim S.
Hi Jim,
Thanks - this is exactly what I needed!! I have one follow-up question for you. I used IAssemblyDoc::GetComponentCount to get the number of components in a model, and this number agrees with my expectations for a particular file (value = 9). I assume that in your code above that "count" is really "bound", and that the value of "bound" found from SafeArrayGetUBound is supposed to represent the number of arrays stored in the VARIANT pointer, which should equal the number of components in the assembly model. But in my case I get a number that is smaller by one compared to what I got from IAssemblyDoc::GetComponentCount (value = 8 instead of the expected value of 9). Do you know why? Should the subsequent FOR loop go from i = 0 to i <= bound (not just i < bound)?
Cut and paste error. There is a line missing in my code above. The SafeArrayGetUBound() method will return the index of the highest element in the array. The array uses zero based indices, so for a 10 element array, SafeArrayGetUBound will return 9. When using arrays I prefer to work with the total element count, so there should be a line after the SafeArrayGetUBound() call:
long count = bound + 1;
Jim S.
That's it - thanks again Jim!!