|
经典图书 各位大神好。笔者最近在做关于sw二次开发的项目,遇到不少困难,望各位大神能给予解决。
以下代码是根据API手册里Traverse Assembly at Component and Feature Levels Using Recursion Example (C#)改写的,其实就是将C#换成c++,最终达成的目的是从装配体中遍历零件的特征。具体代码如下:- void Firstdialog::TraverseFeatureFeatures(CComPtr<IFeature> swFeat, long nLevel, CString* MyString)
- {
- CComPtr<IFeature> swSubFeat = NULL;
- CComPtr<IFeature> swSubSubFeat = NULL;
- CComPtr<IFeature>swSubSubSubFeat =NULL;
- CComPtr<IFeature> pNextFeature = NULL;//定义特征对象
- CString sPadStr = _T(" ") ;
- BSTR B_FeatName;//定义组件的名称
- BSTR B_FeatTypeName;
- BSTR B_SubFeatName;//定义组件的名称
- BSTR B_SubFeatTypeName;
- BSTR B_SubSubFeatName;//定义组件的名称
- BSTR B_SubSubFeatTypeName;
- BSTR B_SubSubSubFeatName;//定义组件的名称
- BSTR B_SubSubSubFeatTypeName;
- long i = 0;
- for (i = 0; i <= nLevel; i++)
- {
- sPadStr += " ";
- }
- while ((swFeat != NULL))
- {
- swFeat->get_Name(&B_FeatName);
- swFeat->GetTypeName2(&B_FeatTypeName);
- *MyString = *MyString + sPadStr + B_FeatName + _T(" [") + B_FeatTypeName + _T("]")+_T(":\n");
- swFeat->IGetFirstSubFeature(&swSubFeat);
- while ((swSubFeat != NULL))
- {
- swSubFeat->get_Name(&B_SubFeatName);
- swSubFeat->GetTypeName2(&B_SubFeatTypeName);
- *MyString = *MyString + sPadStr + B_SubFeatName + _T(" [") + B_SubFeatTypeName + _T("]")+_T(":\n");
- swSubFeat->IGetFirstSubFeature(&swSubSubFeat);
- while ((swSubSubFeat != NULL))
- {
- swSubSubFeat->get_Name(&B_SubSubFeatName);
- swSubSubFeat->GetTypeName2(&B_SubSubFeatTypeName);
- *MyString = *MyString + sPadStr + B_SubSubFeatName + _T(" [") + B_SubSubFeatTypeName + _T("]")+_T(":\n");
- swSubSubFeat->IGetFirstSubFeature(&swSubSubSubFeat);
- while ((swSubSubSubFeat != NULL))
- {
- swSubSubSubFeat->get_Name(&B_SubSubSubFeatName);
- swSubSubSubFeat->GetTypeName2(&B_SubSubSubFeatTypeName);
- *MyString = *MyString + sPadStr + B_SubSubSubFeatName + _T(" [") + B_SubSubSubFeatTypeName + _T("]")+_T(":\n");
-
- pNextFeature = NULL;
- swSubSubSubFeat->IGetNextSubFeature(&pNextFeature);
- swSubSubSubFeat = NULL;
- swSubSubSubFeat = pNextFeature;
- pNextFeature = NULL;
- }
- pNextFeature = NULL;
- swSubSubFeat->IGetNextSubFeature(&pNextFeature);
- swSubSubFeat = NULL;
- swSubSubFeat = pNextFeature;
- pNextFeature = NULL;
- }
- pNextFeature = NULL;
- swSubFeat->IGetNextSubFeature(&pNextFeature);
- swSubFeat = NULL;
- swSubFeat = pNextFeature;
- pNextFeature = NULL;
- }
- pNextFeature = NULL;
- swFeat->IGetNextFeature(&pNextFeature);
- swFeat = NULL;
- swFeat = pNextFeature;
- pNextFeature = NULL;
- }
- }
- void Firstdialog::TraverseComponentFeatures(CComPtr<IComponent2> swComp, long nLevel, CString* MyString)
- {
- CComPtr<IFeature> swFeat;
- swComp->FirstFeature(&swFeat);
- TraverseFeatureFeatures(swFeat, nLevel, MyString);
- }
- void Firstdialog::TraverseComponent(CComPtr<IComponent2> swComp, long nLevel, CString* MyString)
- {
- /*CComPtr<IComponent2> *swChildComp;*/
- CString sPadStr = _T(" ") ;
- int i = 0;
- int nChildren;///定义子零件个数
- HRESULT hres ;
- BSTR Name;//定义组件的名称
- CComPtr<IComponent2> pComponent;
- for (i = 0; i <= nLevel - 1; i++)
- {
- sPadStr = sPadStr + _T(" ");
- }
- swComp->IGetChildrenCount(&nChildren);// 获得组件下子零件个数
- if ( nChildren > 0 )
- {
- CComPtr<IComponent2> *swChildComp = new CComPtr<IComponent2>[nChildren]; ///初始化字符串数组
- hres = swComp->IGetChildren((IComponent2 **)&swChildComp);//// 获得子零件
- if(S_OK == hres)
- {
- for (i=0; i<nChildren; i++ )
- {
- <font color="Red">CComPtr<IComponent2> swChildCompOut = swChildComp[i];
- swChildCompOut->get_Name2(&Name);</font>
- *MyString = *MyString + sPadStr + Name +_T(":\n");
-
- TraverseComponentFeatures(swChildComp[i], nLevel, MyString);
- TraverseComponent(swChildComp[i], nLevel + 1,MyString);
- swChildComp[i]=NULL; ////////释放子零件对象
- }
- }
- swChildComp = NULL;
- delete [] swChildComp;
- }
- }
- <div class="blockcode"><blockquote>void Firstdialog::OnBnClickedButton4()
- {CComPtr<IModelDoc2> iSwModel;
- CString MyString;
- CComPtr<IConfiguration> swConf;
- CComPtr<IComponent2> swRootComp;
- long docType = -1;
- m_iSldWorks_dlg->get_IActiveDoc2(&iSwModel);
- iSwModel->IGetActiveConfiguration(&swConf);
- swConf->IGetRootComponent2(&swRootComp);
- TraverseModelFeatures(iSwModel, 1, &MyString);
-
- iSwModel->GetType(&docType);
- if (docType == swDocASSEMBLY)
- {
- <font color="Red">TraverseComponent(swRootComp, 1, &MyString);</font>
- }
- iSwModel=NULL;
- AfxMessageBox(MyString);
- }
复制代码 |
|