Notes for using the COM interface to obtain Array data during SolidWorks asynchronous mode development

Source: Internet
Author: User

This article is a problem encountered in the actual development process. I have received help from my colleagues and would like to thank you.

In the secondary development process of SolidWorks, the asynchronous mode is used, and the following code has a problem in use (where the problem may occur, I have watched it)

BOOL CSWFun: GetProfileLength (CComPtr <ILoop2> pLoop, double & dLength) {long edgeCount = 0; pLoop-> GetEdgeCount (& edgeCount); if (edgeCount> 0) {double dLenthTemp = 0.0; VARIANT_BOOL isClosed = VARIANT_FALSE; incluisperiodic = VARIANT_FALSE; incluvbret = VARIANT_FALSE; IEdge ** pEdgeArr = NULL; pEdgeArr = new IEdge * [edgeCount]; zeroMemory (pEdgeArr, edgeCount * sizeof (IEdge *); pLoop-> IGetEdges (pEdgeArr); for (int I = 0; I <edgeCount; ++ I) {CComPtr <IEdge> pEdge = pEdgeArr [I]; // The possible pointer is NULL if (pEdge = NULL) {continue;} CComPtr <ICurve> pCurve; pEdge-> IGetCurve (& pCurve); if (NULL = pCurve) {continue;} double dStart = 0.0; double dEnd = 0.0; pCurve-> GetEndParams (& dStart, & dEnd, & isClosed, & isPeriodic, & vbRet ); // if (vbRet! = VARIANT_FALSE) {double dTemp = 0.0; pCurve-> getleng2( dStart, dEnd, & dTemp); if (dTemp> 0) {dLenthTemp + = dTemp ;}}} delete [] pEdgeArr; pEdgeArr = NULL; dLength = dLenthTemp;} return TRUE ;}

After the code is changed to the following, it will be normal

BOOL CSWFun::GetProfileLength(CComPtr<ILoop2>pLoop, double &dLength){long edgeCount = 0;pLoop->GetEdgeCount(&edgeCount);if (edgeCount > 0){double dLenthTemp = 0.0;VARIANT_BOOL isClosed = VARIANT_FALSE;VARIANT_BOOL isPeriodic = VARIANT_FALSE;VARIANT_BOOL vbRet = VARIANT_FALSE;CComVariant vEdgeArray;pLoop->GetEdges(&vEdgeArray);if (V_VT(&vEdgeArray) == VT_EMPTY){return FALSE;}SAFEARRAY *pSaveEdgeArray = V_ARRAY(&vEdgeArray);LPDISPATCH *pEdgeDispArray = NULL;SafeArrayAccessData(pSaveEdgeArray, (void**)&pEdgeDispArray);if (pEdgeDispArray == NULL){return FALSE;}for (int i=0; i<edgeCount; ++i){CComQIPtr<IEdge> pEdge = pEdgeDispArray[i];if (pEdge == NULL){continue;}CComPtr<ICurve> pCurve;pEdge->IGetCurve(&pCurve);if (NULL == pCurve){continue;}double dStart = 0.0;double dEnd = 0.0;pCurve->GetEndParams(&dStart, &dEnd, &isClosed, &isPeriodic, &vbRet);if (vbRet != VARIANT_FALSE){double dTemp = 0.0;pCurve->GetLength2(dStart, dEnd, &dTemp);if (dTemp > 0){dLenthTemp += dTemp;}}}dLength = dLenthTemp;}return TRUE;}

Invalid interface functions are mainly functions that get or set data starting with 'I. The corresponding value is IGetEdges. It is normal to change it to GetEdges. The COM interface is recommended for all add-in DLL projects. for executable (.exe) implementations, you can use the COM interface unless the method or property passes an array. in this case, use the equivalent
Dispatch method that uses a VARIANT to encapsulate the array. Therefore, during development, if it is in asynchronous mode, you need to obtain array-type data to avoid using functions starting with "I.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.