C++ AddIn: Trouble with GetMates

Has anyone successfully used GetMates Method from (IComponent2)?  I have an array of the components and I am trying to move through the array of components and get the mates from each component.  The code I have compiles and runs but when I watch the output it shows a safearray that contains bad pointers.  I have tried many different things but to no avail.  I think my problem is with the Variants and the SafeArrays which I do not completely understand.

Code:

     

      IComponent2** componentArray = NULL;

      componentArray = new IComponent2 * [componentCount];

     

      swAssemDoc->IGetComponents(false, componentCount, componentArray);

     

      VARIANT compMates;     

      SAFEARRAY* pSafeArrayOfMates = NULL;

           

      for(long i = 0; i < componentCount; i++){

            CComBSTR sComponentName(L"");

            componentArray[i]->get_Name(&sComponentName);

           

            componentArray[i]->GetMates(&compMates);

            pSafeArrayOfMates = V_ARRAY(&compMates);

                       

      }

      delete [] componentArray;

      swAssemDoc.Release();  

}

Thanks,

Eric O.

SolidworksApi macros
Answers 2
SF
SF 2011-12-05 Best Answer

Eric,

Did you figure this out? I am working on a similar code to parse through faces in a body and I get all the faces. I can calculate surface area too, but just that the value of SafeArray contains bad pointers. Code works great but I dont know if I should worry about bad pointers.

If anybody else have come across this type of issue before, I would like your insight.

Thank you,

Aravind Shanthakumar

SF
SF 2011-12-05

Aravind,

I have gotten part of my code working but not exactly like I want it to and I believe it is because the SafeArray of bad pointers.  The code below successfully uses the GetMates function and I then use the get_Type function to identify what the mate type is.  I have code that iterates through the components in an assembly and gets all of their mates. 

CODE:

IComponent2** componentArray = NULL;

componentArray = new IComponent2 * [componentCount];

swAssemDoc->IGetComponents(false, componentCount, componentArray);

CComPtr swComp;

VARIANT compMates2;

SAFEARRAY* pSafeArrayOfMates2 = NULL;

CComSafeArray mMate;

        for(long i = 1; i < componentCount; i++){

                    CComBSTR sComponentName(L"");

                    componentArray[i]->get_Name(&sComponentName);

                    CW2A component(sComponentName);

                    swComp = componentArray[i];

                    swComp->GetMates(&compMates2);

                    pSafeArrayOfMates2 = V_ARRAY(&compMates2);

                    mMate = V_ARRAY(&compMates2);

                    long nn;

                    long mType;

                    nn = sizeof(&mMate);

                    psafearraybound = &((pSafeArrayOfMates2->rgsabound)[0]);  ///these 2 lines give the size of the mate array

                    unsigned long numOfMates = psafearraybound->cElements;

                    for(long n = 0; n < numOfMates; ++n){ ///this for loop iterates and gets the type of mate

                                   mate2 = mMate[n];

                                   mate2->get_Type(&mType); //this works

                    }

     }

My goal from here was going to be to compare the mates from each component to identify which parts are connected with which mates.  I tried adding an if statement to compare the value of mate2 in the for loop above to a specified mate from another component but it did not recognize this. 

I thought that a mate between the components would share the same pointer, is this the case?

Thank you for your time,

Eric Owensby