如何获取当前零件的Part特征

本文原作者:Luca Gutierrez,经授权后转发

扫码关注微信公众号:CATIA二次开发助手

扫码关注微信公众号:CATIA二次开发助手

获取当前零件对应的CATPrtPart接口经常使用,可封装成公有函数方便使用。首先调用GetCurDocument函数获得当前环境对应的CATDocument指针,然后通过QueryInterface获得CATInit接口指针,再通过CATInit接口的GetRootContainer方法获取CATIPrtContainerCATIPrtPart可以通过CATIPrtContainer接口的GetPart函数获取。
示例代码如下:

  1. 1/* 
  2. 2* 获取当前环境对应的CATDocument的CATPrtPart指针 
  3. 3*/ 
  4. 4CATIPrtPart_var GetCurPart()
  5. 5 CATDocument* pDoc=GetCurDocument(); 
  6. 6 if(NULL==pDoc) 
  7. 7 return NULL_var; 
  8. 8  
  9. 9 CATInit *pDocAsInit = NULL
  10. 10 HRESULT rc= pDoc->QueryInterface(IID_CATInit, (void**)&pDocAsInit) ; 
  11. 11 if( FAILED(rc) ) 
  12. 12
  13. 13 cout << "Error, the document does not implement CATInit"<< endl
  14. 14 return NULL_var; 
  15. 15
  16. 16 // 
  17. 17 // Gets root container of the document 
  18. 18 // 
  19. 19 CATIPrtContainer *pSpecContainer = NULL
  20. 20 pSpecContainer = (CATIPrtContainer*)pDocAsInit->GetRootContainer("CATIPrtContainer"); 
  21. 21  
  22. 22 pDocAsInit->Release(); 
  23. 23 pDocAsInit = NULL
  24. 24  
  25. 25 if( NULL == pSpecContainer ) 
  26. 26
  27. 27 cout <<"Error, the root container is NULL" << endl
  28. 28 return NULL_var; 
  29. 29 }  
  30. 30 // 
  31. 31 // Retrieves the MechanicalPart of the document 
  32. 32 // 
  33. 33 CATIPrtPart_var spPart(pSpecContainer->GetPart())
  34. 34 if ( NULL_var == spPart ) 
  35. 35
  36. 36 cout <<"Error, the MechanicalPart is NULL" << endl
  37. 37 return NULL_var; 
  38. 38
  39. 39 pSpecContainer->Release(); 
  40. 40 pSpecContainer = NULL
  41. 41 return spPart;  
  42. 42