CAA工程创建
一、新建Workspace
选择New CAA V5 Workspace新建CAA工程,弹出对话框,第一项默认使用Mkmk编译,第二项选择工程存储路径,不同工程分开用0不同路径,第三项选择所基于CATIA版本,点击Next。
点击Close弹出下图,选择Create new framework,->Finish。
弹出下图,按如下配置->OK。
二、新建Module
Add CAAV5 Project ->New Module,->OK。
创建Module后配置CAA CATIA环境,将CATIA安装路径添加进工程,选择第一项,点击Add添加。
三、添加插件(工具条)
Add CAAV5 Item ->Compoent Extension,
第一个扩展名为新建扩展后创建的类名,第三项与第一项相似命名即可,
点击TIE mode下的Add,配置插件基于哪个环境(零件设计、装配环境等),选择CATIA安装目录后取消下方勾选,零件设计环境选择 CATIPrtWksAddin,装配环境选择 CATIPRDWorkShopAddin。
新建的命令条在该module下生成类,头文件、源文件:
在Imakefile.mk中添加引用:
# DO NOT EDIT :: THE CAA2 WIZARDS WILL ADD CODE HERE
WIZARD_LINK_MODULES = JS0GROUP
JS0FM JS0GROUP DraftingItfCPP CATDfiUUID
DI0PANV2 CATApplicationFrame
JS0FM JS0GROUP CATMechanicalModelerUI MechanicalModelerUIUUID
DI0PANV2 CATMathematics CATDialogEngine
CATDraftingInterfaces CATTPSItf
CATPspUtilities
CATApplicationFrame CATViz
CATMecModInterfaces CATObjectSpecsModeler
CATGeometricObjects CATMathStream CATTopologicalObjects
CATGeometricOperators CATObjectModelerBase KnowledgeItf
CATGitInterfaces CATSketcherInterfaces CATVisualization
CATMeasureGeometryInterfaces CATInteractiveInterfaces
CATProductStructure1 CATProcessInterfaces CATPartInterfaces
CATTopologicalOperators CATManufacturingInterfaces CATMechanicalModeler
JS0FM JS0GROUP CATXMLParserItf
AD0XXBAS CD0FRAME
DI0STATE AC0SPBAS
ProcessInterfaces
CATManufacturingInterfaces
CATSurfaceMachiningInterfaces
CATProductStructure1
CATLiteralFeatures KnowledgeItf
CATIAApplicationFrame
CATProductStructure1
KnowledgeItf
CATPartInterfaces CATSketcherInterfaces
CATMechanicalModeler
CATGitInterfaces
CATVisualization
CATConstraintModeler
CATMathStream
CATConstraintModelerItf
CATUdfInterfaces
JS0SCBAK
CATTopologicalOperators
CATInteractiveInterfaces
Mathematics
CATPrismaticMachiningInterfaces CATPartInterfaces
PrismaticMachiningInterfacesUUID
CATSketcherInterfaces CATUdfInterfaces
CATSaiSpaceAnalysisItf
CATMechanicalCommands
CATVisualization
CATCclInterfaces
CATInteractiveInterfaces
CATMeasureGeometryInterfaces AC0XXLNK
SpecsModeler
CK0FEAT JS0CORBA
YP00IMPL YN000MAT
CATCGMGeoMath
CATMechanicalModelerUI
CATTopologicalObjects
CATConstraintModeler
CATGeometricOperators
InfItf
InfItfCPP
CATTTRSItf
CATTTRSUUID
InfProIDL
GeoNurbsTools
CATFreeFormOperators
ProductStructurePubIDL
CATTessellation
CATTPSItfCPP
CATJNIAnnotationTypeLib
CATTPSProIDL
CATTPSPubIDL
CATTPSUUID
JS0GROUP JS0FM
CATPartInterfaces
CATSketcherInterfaces
CATVisualization
CATGeometricOperators
CATProductStructure1
CATMathStream
CATSaiSpaceAnalysisItf
JS0SCBAK
SimulationItf
CATMechanicalModelerUI
CATMechanicalModeler
CATProductStructure1
CATTTRSItf
CATTTRSUUID
CATInteractiveInterfaces
CATTPSUUID
CATMathStream
CATCGMGeoMath
CATTopologicalObjects
DI0PANV2
CATObjectModelerBase
CATPrsWksPRDWorkshop ProductStructureUIUUID
CATMatInterfaces
CATGitInterfaces
CATPrint
CATPrintBase
# END WIZARD EDITION ZONE
在框架下的IdentityCard.h文件中添加引用如下
AddPrereqComponent("System",Protected);
AddPrereqComponent("MechanicalModelerUI",Protected);
AddPrereqComponent("DialogEngine",Protected);
AddPrereqComponent("Mathematics",Protected);
AddPrereqComponent("Dialog",Protected);
AddPrereqComponent("ProductStructureUI",Public);
AddPrereqComponent("ATICatalogKeyword",Public);
AddPrereqComponent("ConstraintModelerUI",Public);
AddPrereqComponent("ProductStructure",Public);
AddPrereqComponent("KnowledgeInterfaces",Public);
AddPrereqComponent("ObjectModelerBase",Public);
AddPrereqComponent("ApplicationFrame",Public);
AddPrereqComponent("CATIAApplicationFrame",Public);
AddPrereqComponent("PartInterfaces",Public);
AddPrereqComponent("ObjectSpecsModeler",Public);
AddPrereqComponent("ConstraintModeler",Public);
AddPrereqComponent("ConstraintModelerInterfaces",Public);
AddPrereqComponent("SketcherInterfaces",Public);
AddPrereqComponent("GeometricOperators",Public);
AddPrereqComponent("ProductStructureInterfaces",Public);
AddPrereqComponent("SpaceAnalysisInterfaces",Public);
AddPrereqComponent("SimulationBase",Public);
AddPrereqComponent("SimulationInterfaces",Public);
AddPrereqComponent("GSMInterfaces",Public);
AddPrereqComponent("GSOInterfaces",Public);
AddPrereqComponent("LiteralFeatures",Public);
AddPrereqComponent("DraftingInterfaces",Public);
AddPrereqComponent("CATTTRSInterfaces",Public);
AddPrereqComponent("CATTPSInterfaces",Public);
AddPrereqComponent("ManufacturingInterfaces",Public);
AddPrereqComponent("SurfaceMachiningInterfaces",Public);
AddPrereqComponent("DMAPSInterfaces",Public);
AddPrereqComponent("GeometricObjects",Public);
AddPrereqComponent("LiteralsEditor",Public);
AddPrereqComponent("MechanicalModeler",Public);
AddPrereqComponent("Tessellation",Public);
AddPrereqComponent("Visualization",Public);
AddPrereqComponent("VisualizationBase",Public);
AddPrereqComponent("MecModInterfaces",Public);
AddPrereqComponent("NewTopologicalObjects",Public);
AddPrereqComponent("MechanicalCommands",Public);
AddPrereqComponent("TopologicalOperators",Public);
AddPrereqComponent("InteractiveInterfaces",Public);
AddPrereqComponent("PrismaticMachiningInterfaces",Public);
AddPrereqComponent("ToolPathEditorInterfaces",Public);
AddPrereqComponent("FreeFormOperators",Public);
AddPrereqComponent("ComponentsCatalogsInterfaces",Public);
AddPrereqComponent("ProcessPlatformBase",Public);
AddPrereqComponent("ProcessPlatformVisu",Public);
AddPrereqComponent("ObjectModelerInterfaces",Public);
AddPrereqComponent("SketcherModeler",Public);
AddPrereqComponent("MeasureGeometryInterfaces",Public);
AddPrereqComponent("InfInterfaces",Public);
AddPrereqComponent("XMLParser",Public);
AddPrereqComponent("CATPlantShipModeler",Public);
AddPrereqComponent("ATImmENOVIAProvider",Public);
AddPrereqComponent("CATMatInterfaces",Public);
AddPrereqComponent("CATPDMBase",Public);
AddPrereqComponent("CATSmaInterfaces",Public);
AddPrereqComponent("Print",Public);
AddPrereqComponent("PrintBase",Public);
至此,工程所需环境、依赖添加完毕。
四、工具条配置、显示
1、添加命令
在MyAdn.h中添加头文件 #include "CATCmdContainer.h",并声明函数:
void CreateCommands();
CATCmdContainer* CreateToolbars();
在MyAdn.cpp中:
#include "CATCommandHeader.h"
#include "CATCreateWorkshop.h"
#include "CATUnicodeString.h"
添加声明: MacDeclareHeader(MyCommandHeader);
对头文件中声明的函数进行实现:(注意:SetAccessChild
为命令条中第一个命令创建方法,后续使用SetAccessNext
;SetAccessChild(Next)
参数为(前继命令,本命令)),MyCommandHeader
中前三个参数分别在资源管理中使用、新建module添加命令实现时module名,在新建的module中新建的command名,一一对应实现。
void MyAdn::CreateCommands()
{
new MyCommandHeader("CMDTest1","CmdModule","CMD1",(void*)NULL);
new MyCommandHeader("CMDTest2","CmdModule","CMD2",(void*)NULL);
new MyCommandHeader("CMDTest3","CmdModule","CMD3",(void*)NULL);
new MyCommandHeader("CMDTest4","CmdModule","CMD4",(void*)NULL);
new MyCommandHeader("CMDTest5","CmdModule","CMD5",(void*)NULL);
}
CATCmdContainer* MyAdn::CreateToolbars ()
{
NewAccess (CATCmdContainer, pPartBenchTlb, PartBenchTlb) ;
NewAccess (CATCmdStarter, pFirstCmd, FirstCmd) ;
SetAccessCommand (pFirstCmd, "CMDTest1") ;
SetAccessChild (pPartBenchTlb, pFirstCmd) ;
NewAccess (CATCmdStarter, pSecondCmd, SecondCmd) ;
SetAccessCommand (pSecondCmd, "CMDTest2") ;
SetAccessNext(pFirstCmd, pSecondCmd) ;
NewAccess (CATCmdStarter, pThirdCmd, ThirdCmd) ;
SetAccessCommand (pThirdCmd, "CMDTest3");
SetAccessNext (pSecondCmd, pThirdCmd) ;
NewAccess (CATCmdStarter, pFourthCmd, FourthCmd) ;
SetAccessCommand (pFourthCmd, "CMDTest4") ;
SetAccessNext (pThirdCmd, pFourthCmd) ;
NewAccess (CATCmdStarter, pFifthCmd, FifthCmd) ;
SetAccessCommand (pFifthCmd, "CMDTest5") ;
SetAccessNext (pFourthCmd, pFifthCmd) ;
AddToolbarView (pPartBenchTlb, 1,Right) ;
NewAccess(CATCmdContainer, pPartBenchMenu, PartBenchMenu) ;
NewAccess (CATCmdContainer , pTestMenu, TestMenu);
SetAccessChild (pPartBenchMenu, pTestMenu);
NewAccess (CATCmdStarter, pTestMenu1 , TestMenu1) ;
SetAccessCommand (pTestMenu1, "CMDTest1") ;
SetAccessChild (pTestMenu, pTestMenu1) ;
NewAccess (CATCmdStarter, pTestMenu2 , TestMenu2) ;
SetAccessCommand (pTestMenu2, "CMDTest2") ;
SetAccessNext (pTestMenu1, pTestMenu2) ;
NewAccess (CATCmdStarter, pTestMenu3 , TestMenu3) ;
SetAccessCommand (pTestMenu3, "CMDTest3") ;
SetAccessNext (pTestMenu2, pTestMenu3) ;
NewAccess (CATCmdStarter, pTestMenu4 , TestMenu4) ;
SetAccessCommand (pTestMenu4, "CMDTest4") ;
SetAccessNext (pTestMenu3, pTestMenu4) ;
NewAccess (CATCmdStarter, pTestMenu5 , TestMenu5) ;
SetAccessCommand (pTestMenu5, "CMDTest5") ;
SetAccessNext (pTestMenu4, pTestMenu5) ;
SetAddinMenu (pPartBenchTlb, pPartBenchMenu) ;
return pPartBenchTlb;
}
2、配置命令条资源
在工程存储目录下找到文件夹CNext ->resources,新建文件夹graphic,继续新建子文件夹Normal,在Normal文件夹下添加22x22bmp位图作为命令条显示图标,在msgcatalog下新建四个文件如图(1,3文件后缀为CATNls
)。
在MyAdn.CATNls中添加:
PartBenchTlb.Title = "我的工具条";
PartBenchMenu.Title = "我的菜单";
TestMenu.Title = "我的菜单";
在MyCommandHeader.CATNls中添加:
MyCommandHeader.CMDTest1.Title="显示产品信息";
MyCommandHeader.CMDTest1.Help="help1";
MyCommandHeader.CMDTest1.ShortHelp="显示产品信息";
MyCommandHeader.CMDTest1.LongHelp="longhelp1";
MyCommandHeader.CMDTest2.Title="添加属性";
MyCommandHeader.CMDTest2.Help="help1";
MyCommandHeader.CMDTest2.ShortHelp="添加属性";
MyCommandHeader.CMDTest2.LongHelp="longhelp2";
MyCommandHeader.CMDTest3.Title="设置属性";
MyCommandHeader.CMDTest3.Help="help3";
MyCommandHeader.CMDTest3.ShortHelp="设置属性";
MyCommandHeader.CMDTest3.LongHelp="longhelp3";
MyCommandHeader.CMDTest4.Title="显示属性";
MyCommandHeader.CMDTest4.Help="help4";
MyCommandHeader.CMDTest4.ShortHelp="显示属性";
MyCommandHeader.CMDTest4.LongHelp="longhelp4";
MyCommandHeader.CMDTest5.Title="遍历结构树";
MyCommandHeader.CMDTest5.Help="help5";
MyCommandHeader.CMDTest5.ShortHelp="遍历结构树";
MyCommandHeader.CMDTest5.LongHelp="longhelp5";
MyCommandHeader.CMDTest6.Title="创建圆柱体";
MyCommandHeader.CMDTest6.Help="help6";
MyCommandHeader.CMDTest6.ShortHelp="创建圆柱体";
MyCommandHeader.CMDTest6.LongHelp="longhelp6";
MyCommandHeader.CMDTest7.Title="对话框";
MyCommandHeader.CMDTest7.Help="help7";
MyCommandHeader.CMDTest7.ShortHelp="对话框";
MyCommandHeader.CMDTest7.LongHelp="longhelp7";
在MyCommandHeader.CATRsc中添加:
MyCommandHeader.CMDTest1.Icon.Normal="I_MBDBatchEdit";
MyCommandHeader.CMDTest2.Icon.Normal="I_PDMSave";
MyCommandHeader.CMDTest3.Icon.Normal="I_LinkResume";
MyCommandHeader.CMDTest4.Icon.Normal="I_Login";
MyCommandHeader.CMDTest5.Icon.Normal="I_Lock";
MyCommandHeader.CMDTest6.Icon.Normal="I_MBDCopyNote";
MyCommandHeader.CMDTest7.Icon.Normal="I_MBDTemplateInit";
点击Project ->Refresh...
Build ->mkmk编译(生成)工程,编译通过会在输出窗口显示 正在创建库...
创建/刷新运行时窗口
最后打开窗口,输入cnext打开CATIA,编译好的命令条会以插件的形式显示在CATIA菜单栏及浮动工具条。
五、命令实现
注:在所有命令的cpp文件中统一加上所有CAA头文件引用如下:
#include "CATIProduct.h"
#include "CATDocument.h"
#include "CATApplicationFrame.h"
#include "CATDlgGridConstraints.h"
#include "CATMsgCatalog.h"
#include "CATDlgNotification.h"
#include "CATFrmEditor.h"
#include "CATIContainerOfDocument.h"
#include "CATIGSMFactory.h"
#include "CATIGSMPoint.h"
#include "CATIGSMProceduralView.h"
#include "CATIContainer.h"
#include "CATISpecObject.h"
#include "CATIPrtContainer.h"
#include "CATISketchFactory.h"
#include "CATIPrtFactory.h"
#include "CATIPrtPart.h"
#include "CATISketch.h"
#include "CATI2DWFFactory.h"
#include "CATIPrism.h"
#include "CATIPad.h"
#include "CATLimitDefs.h"
#include "CATIDocRoots.h"
#include "CATIPrdProperties.h"
#include "CATInit.h"
#include "CATICkeParmFactory.h"
#include "CATIGSMPlaneNormal.h"
#include "CATIVisProperties.h"
#include "CATIModelEvents.h"
#include "CATModifyVisProperties.h"
#include "CATIGSMIntersect.h"
CAA装配环境下(product)读取文档根节点代码如下,后面不再重复添加:
//读取文档根节点---------------------------------------------------------------
CATFrmEditor *pEditor = CATFrmEditor::GetCurrentEditor();
CATDocument *pDoc = pEditor->GetDocument();
CATIDocRoots *piDocRootsOnDoc = NULL;
HRESULT rc;
rc = pDoc->QueryInterface(IID_CATIDocRoots, (void**) &piDocRootsOnDoc);
if ( FAILED(rc) )
{
MessageBoxA(NULL, "ERROR: QI on CATIDocRoots failed", "tip", MB_OK);
return (CATStatusChangeRCAborted);
//枚举型(CATStatusChangeRCAborted,CATStatusChangeRCCompleted)(类似布尔型0,1)
}
CATListValCATBaseUnknown_var *pRootProducts = piDocRootsOnDoc->GiveDocRoots();
CATIProduct_var spRootProduct = NULL_var; //文档根节点
if( NULL != pRootProducts )
{
if( 0 != pRootProducts->Size() )
{
spRootProduct = (*pRootProducts)[1];
delete pRootProducts;
pRootProducts = NULL;
}
piDocRootsOnDoc->Release(); //文档读取完毕后释放内存
piDocRootsOnDoc = NULL;
}
if (NULL_var == spRootProduct)
{
MessageBoxA(NULL, "ERROR: spRootProduct failed", "tip", MB_OK);
return (CATStatusChangeRCAborted);
}
1、显示产品属性(PartNumber)
CATStatusChangeRC CMD1::Activate( CATCommand *iFromClient, CATNotification *iEvtDat)//CAA命令响应函数
{
//读取文档根节点---------------------------------------------------------------
CATUnicodeString _sPartNumber = spRootProduct->GetPartNumber(); //CAA字符串类型
MessageBoxA(NULL, "产品编号:"+_sPartNumber, "tip", MB_OK);
CATListValCATBaseUnknown_var* ListChildren = NULL;
if( spRootProduct != NULL_var )
{
ListChildren = spRootProduct->GetChildren(); //读取根节点下子节点,GetAllChildren读取所有子节点
if (ListChildren != NULL)
{
CATISpecObject_var spSpec = spRootProduct;
if (ListChildren->Size()>0)
{
for (int x = 1;x<=ListChildren->Size();x++)
{
CATBaseUnknown* pBUProduct = NULL;
CATUnicodeString sDirPartNumber("");
pBUProduct = (*ListChildren)[x];
CATIProduct* pDirChildren = NULL;
pBUProduct->QueryInterface(IID_CATIProduct,(void**)&pDirChildren);
if (pDirChildren)
{
CATUnicodeString sChildNum = pDirChildren->GetPartNumber();
MessageBoxA(NULL, "零件编号:"+sChildNum, "tip", MB_OK);
}
//读取所有子节点至List列表内,读取列表内每个节点值,赋给CAA节点CATIProduct。CAA没有列表节点,要得到子节点列表需通过BaseUnknown转换,跟C#、MFC窗体程序中listview、treeview添加item时类似。
}
}
}
}
RequestDelayedDestruction(); //取消当前命令激活状态,否则需手动点选命令才能进行其他操作。
return (CATStatusChangeRCCompleted);
}
2、添加属性
在头文件中声明添加属性函数void CreatePropertyOnProd(CATIProduct* pProd,CATUnicodeString sKey,CATUnicodeString sVal);
,在源文件中添加实现:
void CMD2::CreatePropertyOnProd(CATIProduct* pProd,CATUnicodeString sKey,CATUnicodeString sVal)
{
//参数1-->CATIProduct指针,参数2-->属性名称, 参数3-->属性值
if (!pProd)
{
MessageBoxA(NULL,"pProd == null", "tip",MB_OK);
return;
}
CATUnicodeString sPartNumber = pProd->GetPartNumber();
CATIProduct_var spRef = pProd->GetReferenceProduct();
CATILinkableObject *piLinkableOnProduct = NULL;
pProd->QueryInterface(IID_CATILinkableObject,(void**)&piLinkableOnProduct);
if (NULL == piLinkableOnProduct)
{
MessageBoxA(NULL, "piLinkableOnProduct = NULL", "tip", MB_OK);
return ;
}
CATDocument *pChildDoc = piLinkableOnProduct -> GetDocument();
if (NULL == pChildDoc)
{
MessageBoxA(NULL, "pChiidDoc = NULL", "tip", MB_OK);
return ;
}
CATInit_var spInitOnDoc = NULL_var;
pChildDoc->QueryInterface(IID_CATInit,(void**)&spInitOnDoc);
if(NULL_var == spInitOnDoc)
{
MessageBoxA(NULL, "spInitOnDoc = NULL_var", "tip", MB_OK);
return;
}
CATIContainer *pCont =
(CATIContainer*) spInitOnDoc->GetRootContainer("CATIContainer");
if(NULL == pCont)
{
MessageBoxA(NULL, "pCont = NULL", "tip", MB_OK);
return;
}
CATICkeParmFactory_var spFact= NULL_var;
pCont->QueryInterface(IID_CATICkeParmFactory,(void**)&spFact);
if(NULL_var == spFact)
{
MessageBoxA(NULL, "Could not get parm factory", "tip", MB_OK);
return;
}
//CATISpecObject_var spNumProperty = NULL_var;
//创建字符串类型属性
CATICkeParm_var spParmOfCust = spFact->CreateString(sKey, sVal);
CATIPrdProperties_var spProp(spRef);
if(spProp != NULL_var)
{
CATIParmPublisher* pPublisher = NULL;
spProp->GetUserProperties(pPublisher, TRUE);
pPublisher->Append(CATISpecObject_var(spParmOfCust));
}
//pListTemp.Append(sDirPartNumber);
}
命令响应函数中:
CATStatusChangeRC CMD2::Activate( CATCommand * iFromClient, CATNotification * iEvtDat)
{
//读取文档根节点---------------------------------------------------------------
CATUnicodeString sKey=" ";
CATUnicodeString sVal=" ";
CATListValCATBaseUnknown_var* ListChildren = NULL;
if( spRootProduct != NULL_var )
{
sKey = "姓名";
sVal = "李海洋";
CreatePropertyOnProd(spRootProduct,sKey,sVal);
sKey = "性别";
sVal = "男";
CreatePropertyOnProd(spRootProduct,sKey,sVal);
ListChildren = spRootProduct->GetAllChildren();
if (ListChildren != NULL)
{
CATISpecObject_var spSpec = spRootProduct;
if (ListChildren->Size()>0)
{
for (int x = 1;x<=ListChildren->Size();x++)
{
CATBaseUnknown* pBUProduct = NULL;
CATUnicodeString sDirPartNumber("");
pBUProduct = (*ListChildren)[x];
CATIProduct* pDirChildren = NULL;
pBUProduct->QueryInterface(IID_CATIProduct,(void**)&pDirChildren);
sKey = "姓名";
sVal = "李海洋";
CreatePropertyOnProd(pDirChildren,sKey,sVal);
sKey = "性别";
sVal = "男";
CreatePropertyOnProd(pDirChildren,sKey,sVal);
}
}
}
}
RequestDelayedDestruction();
return (CATStatusChangeRCCompleted);
}
3、修改属性
在头文件中添加头文件:#include "CATIProduct.h"
,添加声明:
bool SetProdPropsValue(CATIProduct* pPrd,CATUnicodeString sKey,CATUnicodeString sVal);
HRESULT StringToVector(const CATUnicodeString &iStr, const CATUnicodeString &iToken, CATLISTV
(CATUnicodeString) &oStrVector);
CATIProduct_var GetActiveProd();
源文件中实现:
HRESULT CMD3::StringToVector(const CATUnicodeString &iStr, const CATUnicodeString &iToken, CATLISTV
(CATUnicodeString) &oStrVector)
{
HRESULT rc = S_OK;
if (iStr != "" && iToken != "")
{
CATUnicodeString aStr(iStr);
int nStringLen = aStr.GetLengthInChar();
int nTokenLen = iToken.GetLengthInChar();
CATUnicodeString subStr("");
int nIndex = aStr.SearchSubString(iToken, 0, CATUnicodeString::CATSearchModeForward);
while (nIndex >= 0)
{
// one occurence found
subStr = aStr.SubString(0, nIndex);
oStrVector.Append(subStr);
// reset input
aStr = aStr.SubString(nIndex + nTokenLen, nStringLen - nIndex - nTokenLen);
nStringLen = aStr.GetLengthInChar();
nIndex = aStr.SearchSubString(iToken, 0, CATUnicodeString::CATSearchModeForward);
}
// put the left part
oStrVector.Append(aStr);
}
return rc;
}
bool CMD3::SetProdPropsValue(CATIProduct* pPrd,CATUnicodeString sKey,CATUnicodeString sVal)
{
//寻找属性名相同的属性(CAA属性名设置后不可改),更改其对应的属性值。
if (pPrd == NULL)
{
return false;
}
CATUnicodeString sName = pPrd->GetPartNumber();
CATIProduct_var spRef;
spRef=pPrd->GetReferenceProduct();
CATIPrdProperties_var spProp = spRef;
if(!!spProp)
{
CATListValCATUnicodeString pStrVetor;
CATUnicodeString token="\";
CATIParmPublisher* pPublisher = NULL;
spProp->GetUserProperties(pPublisher, TRUE);
CATListValCATISpecObject_var spListProperty = 0;
pPublisher->GetDirectChildren("CATICkeParm", spListProperty);
int nSize = spListProperty.Size();
CATUnicodeString sSize("");
sSize.BuildFromNum(nSize);
if (nSize<1)
{
MessageBoxA(NULL, sName+"无自定义属性! ", "tip", MB_OK);
return false;
}
bool bResult =false;
for( int i=1; i<=nSize; i++)
{
pStrVetor.RemoveAll(CATCollec::ReleaseAllocation);
CATISpecObject_var spSpecParam = spListProperty[i];
CATICkeParm_var spCkParam = spSpecParam;
if( spCkParam != NULL_var )
{
CATUnicodeString sPathName = spCkParam->Pathname(pPublisher);
CATUnicodeString sCkParamName("");
CATUnicodeString sCkParamVal = spCkParam->Value()->AsString();
int iFlg = sPathName.SearchSubString("\");
if (iFlg>0)
{
StringToVector(sPathName,token,pStrVetor);
sCkParamName = pStrVetor[2];
}
else
{
sCkParamName = sPathName;
}
if (sCkParamName == sKey)
{
spCkParam->Valuate(sVal);
bResult = true;
break;
}
}
}
return bResult;
}
return false;
}
CATIProduct_var CMD3::GetActiveProd() //获取当前选中零部件
{
CATIProduct_var spActiveProd=NULL_var;
CATFrmLayout * pCurrentLayout = CATFrmLayout::GetCurrentLayout();
if ( NULL != pCurrentLayout )
{
CATFrmWindow * pCurrentWindow = pCurrentLayout->GetCurrentWindow();
if ( NULL != pCurrentWindow )
{
CATFrmEditor * pEditor = pCurrentWindow->GetEditor();
if ( pEditor )
{
//得到激活的装配件的CATIProduct指针
CATPathElement ActiveElement=pEditor->GetUIActiveObject();
CATBaseUnknown_var pUIActiveObject = NULL_var;
pUIActiveObject = ActiveElement[ActiveElement.GetSize()-1];
if( pUIActiveObject == NULL_var )
{
MessageBoxA(NULL, "UtilityTools::GetActiveProd()---> Get pUIActiveObject Failed!", "tip", MB_OK);
return NULL_var;
}
CATISpecObject_var spActiveObject(pUIActiveObject);
if(spActiveObject==NULL_var)
{
MessageBoxA(NULL, "UtilityTools::GetActiveProd()---> Get pUIActiveObject Failed!", "tip", MB_OK);
return NULL_var;
}
CATIProduct_var spRootProduct(spActiveObject);
if( spRootProduct == NULL_var )
{
MessageBox(NULL,L"Get spRootProduct Failed!",L"提示",MB_OK);
CATIPrtPart * piPrtPart = NULL;
ActiveElement.Search(IID_CATIPrtPart,(void**)&piPrtPart) ;
if( !!piPrtPart )
{
spRootProduct = piPrtPart->GetProduct();
if( spRootProduct == NULL_var )
{
MessageBox(NULL,L"Get CATIProduct By Part Failed---------!",L"提示",MB_OK);
return NULL_var;
}
}
else
{
MessageBox(NULL,L"Get PartObj Failed---------!",L"提示",MB_OK);
}
}
spActiveProd = spRootProduct;
}
}
}
return spActiveProd;
}
CATStatusChangeRC CMD3::Activate( CATCommand * iFromClient, CATNotification * iEvtDat)
{
//读取文档根节点---------------------------------------------------------------
CATIProduct_var spActiveProd=GetActiveProd();
CATUnicodeString sKey="";
CATUnicodeString sVal="";
sKey = "姓名";
sVal = "王丽";
SetProdPropsValue(spActiveProd,sKey,sVal);
sKey = "性别";
sVal = "女";
SetProdPropsValue(spActiveProd,sKey,sVal);
RequestDelayedDestruction();
return (CATStatusChangeRCCompleted);
}
4、显示属性
头文件加引用#include "CATIProduct.h"
,添加声明:
bool GetProdPropsValue(CATIProduct* pPrd);
HRESULT StringToVector(const CATUnicodeString &iStr, const CATUnicodeString &iToken, CATLISTV
(CATUnicodeString) &oStrVector);
源文件实现,StringToVector
与上条命令实现相同,GetProdPropsValue
只需把SetProdPropsValue
此处
改为:
响应函数中,读取文档后调用GetProdPropsValue
即可。
5、遍历文档结构树
头文件添加#include "CATIProduct.h"
,CATListValCATBaseUnknown_var GetChild(CATIProduct_var spRootPro);
源文件实现:
CATListValCATBaseUnknown_var CMD5::GetChild(CATIProduct_var spRootPro) //递归遍历
{
if( spRootPro != NULL_var )
{
CATListValCATBaseUnknown_var* ListChildren = NULL;
ListChildren = spRootPro->GetChildren();
if (ListChildren != NULL)
{
CATISpecObject_var spSpec = spRootPro;
if (ListChildren->Size()>0)
{
for (int x = 1;x<=ListChildren->Size();x++)
{
CATBaseUnknown* pBUProduct = NULL;
CATUnicodeString sDirPartNumber("");
pBUProduct = (*ListChildren)[x];
CATIProduct* pDirChildren = NULL;
pBUProduct->QueryInterface(IID_CATIProduct,(void**)&pDirChildren);
if (pDirChildren)
{
CATUnicodeString sChildNum = pDirChildren->GetPartNumber();
MessageBoxA(NULL, "零件编号:"+sChildNum, "tip", MB_OK);
GetChild(pDirChildren);
}
}
}
}
}
return (CATStatusChangeRCCompleted);
}
CATStatusChangeRC CMD5::Activate( CATCommand * iFromClient, CATNotification * iEvtDat)
{
//读取文档-----------------------------------------------------
CATUnicodeString _sPartNumber = spRootProduct->GetPartNumber();
MessageBoxA(NULL, "产品编号:"+_sPartNumber, "tip", MB_OK);
GetChild(spRootProduct);
RequestDelayedDestruction();
return (CATStatusChangeRCCompleted);
}
6、创建圆柱体
新建一个part环境命令条,步骤与之前相同,得到两个新module。
在圆柱体命令源文件中添加实现:
CATStatusChangeRC CMD6::Activate( CATCommand * iFromClient, CATNotification * iEvtDat)
{
//part环境下读取根节点方法稍有不同
CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
CATDocument* pDoc = pEditor->GetDocument();
CATInit_var spInitOnDoc(pDoc);
if (pDoc==NULL)
{
MessageBoxA(NULL, "pDoc空", "tip", MB_OK);
}
CATIPrtContainer *piPrtCont = (CATIPrtContainer *)spInitOnDoc->GetRootContainer("CATIPrtContainer");
if (piPrtCont==NULL)
{
MessageBoxA(NULL, "piPrtCont空", "tip", MB_OK);
}
CATIPrtPart_var spPrt = piPrtCont->GetPart();
CATLISTV(CATISpecObject_var) spListRefPlanes = spPrt->GetReferencePlanes();
CATISpecObject_var spSketchPlane = spListRefPlanes[3];
CATISketchFactory_var spSketchFactOnPrtCont(piPrtCont);
if ( NULL_var == spSketchFactOnPrtCont ) return (CATStatusChangeRCCompleted);
CATISketch_var spSketch = spSketchFactOnPrtCont->CreateSketch(spSketchPlane);
if ( NULL_var == spSketch ) return (CATStatusChangeRCCompleted);
spSketch->OpenEdition(); //画草图
CATI2DWFFactory_var spWF2DFactOnSketch(spSketch);
double centerPoint[2]={0.0,0.0};
CATISpecObject_var spCirCle = spWF2DFactOnSketch->CreateCircle(centerPoint,10);
spSketch->CloseEdition();
CATIPrtFactory_var spPrtFactOnPrtCont(piPrtCont);
CATISpecObject_var contPad = spPrtFactOnPrtCont->CreatePad(spSketch);
CATIPad_var ospPad = contPad;
ospPad->ModifyEndType(catOffsetLimit);
ospPad->ModifyEndOffset(20.00);
contPad->Update();
CATIGSMProceduralView_var spPadObj = contPad;
return (CATStatusChangeRCCompleted);
}
7、对话框GSM实例
创建对话框,点击命令弹出对话框,通过对话框画线,初步了解对话框。
对话框创建后生成3个文件:
在CMD7.cpp中添加DlgTest.h引用,在响应函数中添加:
DlgTest *pDlg = new DlgTest();
pDlg->Build();
pDlg->SetVisibility(CATDlgShow);
RequestDelayedDestruction();
在DlgTset.h中声明:
bool IsGood(CATISpecObject_var ispSpecObject);
void InsertView(CATISpecObject_var spObj, bool bShowState);
CATBoolean SetObjectShowState(CATBaseUnknown_var ispBaseUnknown, CATBoolean ShowState);
源文件中:
bool DlgTest::IsGood(CATISpecObject_var ispSpecObject)
{
if (NULL_var==ispSpecObject)
{
return false;
}
CATTry
{
ispSpecObject->Update(NULL_var);
}
CATCatch(CATError,error)
{
if (NULL != error)
{
return false;
}
}
CATEndTry
return true;
}
void DlgTest::InsertView(CATISpecObject_var spObj, bool bShowState)
{
CATIGSMProceduralView_var spPVP = spObj;
if (NULL_var != spPVP)
{
spPVP->InsertInProceduralView();
}
if (!bShowState)
{
SetObjectShowState(spObj,bShowState);
}
}
CATBoolean DlgTest::SetObjectShowState(CATBaseUnknown_var ispBaseUnknown, CATBoolean ShowState)
{
if(NULL_var == ispBaseUnknown) return FALSE;
HRESULT rc(S_OK);
CATIVisProperties_var spVisProperties = NULL_var;
spVisProperties = ispBaseUnknown;
if(NULL_var == spVisProperties) return FALSE;
CATVisPropertiesValues VisPropertiesValues;
if(ShowState) rc = VisPropertiesValues.SetShowAttr(CATShowAttr);
else rc = VisPropertiesValues.SetShowAttr(CATNoShowAttr);
if(FAILED(rc)) return FALSE;
spVisProperties->SetPropertiesAtt(VisPropertiesValues,CATVPShow,CATVPGlobalType,0,0);
CATIModelEvents_var spModelEvents = ispBaseUnknown;
if(NULL_var==spModelEvents) return FALSE;
CATModifyVisProperties ModifyVisProperties(ispBaseUnknown,
CATPathElement(ispBaseUnknown),CATVPGlobalType,CATVPShow,VisPropertiesValues);
spModelEvents->Dispatch(ModifyVisProperties);
return TRUE;
}
void DlgTest::OnGSMPushBtnPushBActivateNotification(CATCommand* cmd, CATNotification* evt, CATCommandClientData data)
{
CATFrmEditor* pEditor = CATFrmEditor::GetCurrentEditor();
CATDocument* pDoc = pEditor->GetDocument();
CATInit_var spInitOnDoc(pDoc);
CATIContainer *piCont = (CATIContainer *)spInitOnDoc->GetRootContainer("CATIContainer");
CATIGSMFactory_var spGSMFactory = NULL_var;
spGSMFactory = piCont;
CATMathPoint _Point,_Point2,_Point3,_Point4;
_Point.SetCoord(10,10,10);
_Point2.SetCoord(70,70,70);
_Point3.SetCoord(0,50,50);
_Point4.SetCoord(0,200,200);
CATIGSMPoint_var spPoint1 = spGSMFactory->CreatePoint(_Point);
if (IsGood(spPoint1))
{
InsertView(spPoint1,true);
}
CATIGSMPoint_var spPoint2 = spGSMFactory->CreatePoint(_Point2);
if (IsGood(spPoint2))
{
InsertView(spPoint2,true);
}
CATIGSMPoint_var spPoint3 = spGSMFactory->CreatePoint(_Point3);
if (IsGood(spPoint3))
{
InsertView(spPoint3,true);
}
CATIGSMPoint_var spPoint4 = spGSMFactory->CreatePoint(_Point4);
if (IsGood(spPoint4))
{
InsertView(spPoint4,true);
}
CATIGSMLine_var spLine = spGSMFactory->CreateLine(_Point,_Point2);
if (IsGood(spLine))
{
InsertView(spLine,true);
}
CATIGSMLine_var spLine2 = spGSMFactory->CreateLine(_Point3,_Point4);
if (IsGood(spLine2))
{
InsertView(spLine2,true);
}
CATIGSMPlaneNormal_var spPlane = spGSMFactory->CreatePlaneNormal(spLine,spPoint2);
if (IsGood(spPlane))
{
InsertView(spPlane,true);
}
CATIGSMIntersect_var spIntersect = spGSMFactory->CreateIntersect(spPlane,spLine2);
if (IsGood(spIntersect))
{
InsertView(spIntersect,true);
}
}
六、遇到的问题
1、工程里移除并在文件夹中删除某个.h及对应.cpp文件后,重新打开工程还是会显示这些文件,但打不开。
问题原因:CAA编译出错,不能及时更新文件(CMake出错)???
解决方法:在工程里此文件所在module找到对应.vcproj文件,打开,删除已删除文件原路径,并在需要显示的module对应.vcproj文件里添加相应的路径,重新打开工程即可。
2、从别的地方复制解决方案在vs打开后,原本包含的多个project丢失连接,不显示,需手动添加。
3、对话框设计文件有时打不开。