最近开始对 CATIA 进行一些自动化的开发,本来想找 CAA 来进行研究,可惜一直没时间和机会去找,暂时就利用 CATIA automation 来开发了。
由于利用 VB 或 VB.NET 将CATIA 脚本转化为程序确实方便,不过利用C++来 *** 作更适合于我这种开发者。
方法有好多种,这里以画圆作为一个简单例子。
1 利用 Idispatch 接口来编程
HRESulT hr; CLSID AppClsID; Idispatch *pApp; ::CoInitialize(NulL); // ::CLSIDFromProgID (L"CATIA.Application",&AppClsID); //get the unique ID of CATIA // hr = CoCreateInstance(AppClsID,NulL,CLSCTX_LOCAL_SERVER,IID_Idispatch,(voID**)&pApp); VARIANT result,buffer; Variantinit(&result); VARIANT arg2; Variantinit(&arg2); arg2.vt = VT_BOol; arg2.boolVal = TRUE; hr = autoWrap(disPATCH_PROPERTYPUT|disPATCH_METHOD,&result,pApp,L"Visible",1,arg2); hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,L"documents",0);//here there is no argument,so we put 0 buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *documents = buffer.pdispVal; VARIANT arg1; Variantinit(&arg1); arg1.vt = VT_BSTR; arg1.bstrVal = ::SysAllocString(L"Part"); //VARIANT result,buffer; Variantinit(&result); hr = autoWrap(disPATCH_METHOD,documents,L"Add",arg1); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *partdocument = buffer.pdispVal; // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,partdocument,L"Part",0); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *part = buffer.pdispVal; // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,part,L"BodIEs",0); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *bodIEs = buffer.pdispVal; // VARIANT arg; Variantinit(&arg); arg.vt = VT_BSTR; arg.bstrVal = ::SysAllocString(L"Partbody"); hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,bodIEs,L"Item",arg); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *body = buffer.pdispVal; // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,body,L"Sketches",0); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *sketches1 = buffer.pdispVal; // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,L"OriginElements",0); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *originElements = buffer.pdispVal; // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,originElements,L"PlaneXY",0); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *reference1 = buffer.pdispVal; // VARIANT arg3; Variantinit(&arg3); arg3.vt = VT_disPATCH; arg3.pdispVal = reference1; hr = autoWrap(disPATCH_METHOD,sketches1,arg3); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *sketch1 = buffer.pdispVal; //// //SAFEARRAY * psa; //SAFEARRAYBOUND rgsabound[1]; //rgsabound[0].lLbound = 0; //rgsabound[0].cElements = 9; //psa = SafeArrayCreate(VT_VARIANT,rgsabound); // //hr = SafeArrayAccessData(psa); //hr = SafeArrayUnaccessData(psa); //VARIANT array; //Variantinit(&array); //array.vt = VT_ARRAY; //array.pparray = &psa; // //hr = autoWrap(disPATCH_METHOD,sketch1,L"SetabsoluteAxisData",array); VARIANT arg4; Variantinit(&arg4); arg4.vt = VT_disPATCH; arg4.pdispVal = sketch1; hr = autoWrap(disPATCH_PROPERTYPUT|disPATCH_METHOD,L"InWorkObject",arg4); //buffer.vt = VT_disPATCH; //buffer.pdispVal = result.pdispVal; //Idispatch *sketch1 = buffer.pdispVal; // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,L"OpenEdition",0); buffer.vt = VT_disPATCH; buffer.pdispVal = result.pdispVal; Idispatch *factory2D1 = buffer.pdispVal; // VARIANT a1,a2,a3; Variantinit(&a1); Variantinit(&a2); Variantinit(&a3); a1.vt = VT_R4;a1.fltVal = 50.f; a2.vt = VT_R4;a2.fltVal = 0.f; a3.vt = VT_R4;a3.fltVal = 0.f; hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,factory2D1,L"CreateClosedCircle",3,a1,a3); // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,L"CloseEdition",0); // hr = autoWrap(disPATCH_PROPERTYGET|disPATCH_METHOD,L"Update",0); if (pApp) pApp->Release(); ::CoUninitialize();
2 利用CATIA自带的TLB转化成 tlh/tli 来编程
先用VC的 #import 功能进行转化,主要的几个 TLB 为:
InfTypelib.tlb
KweTypelib.tlb
PSTypelib.tlb
MecModTypelib.tlb
CATGitTypelib.tlb
试验的 CATIA V5 R18 版本在转换之后有些小错误,之后就可以方便地写代码了。VC6的话,需要把转换后的UTF8编码文件转换成ANSI。
int main(int argc,char* argv[]){ ::CoInitialize(NulL); ApplicationPtr catia; HRESulT hr = S_OK; hr = catia.GetActiveObject("CATIA.Application"); if (Failed(hr)) { hr = catia.CreateInstance("CATIA.Application"); } catia->PutVisible(VARIANT_TRUE); // documentsPtr documents; documents = catia->Getdocuments(); BSTR AddPart = _com_util::ConvertStringToBSTR("Part"); PartdocumentPtr partdocument; partdocument = documents->Add(&AddPart); PartPtr part = partdocument->GetPart(); BodIEsPtr bodIEs = part->GetBodIEs(); BodyPtr body = part->GetMainBody(); HybrIDBodIEsPtr hybrIDBodIEs = part->GetHybrIDBodIEs(); FactoryPtr shapeFactory = part->GetShapeFactory(); HybrIDShapeFactoryPtr hybrIDShapeFactory = part->GetHybrIDShapeFactory(); SketchesPtr sketches = body->GetSketches(); OriginElementsPtr originElements = part->GetoriginElements(); AnyObjectPtr planeXY = originElements->GetPlaneXY(); ReferencePtr r1 = part->CreateReferenceFromObject(planeXY); SketchPtr sketch = sketches->Add(r1); part->PutInWorkObject(sketch); Factory2DPtr factory2D = sketch->OpenEdition(); factory2D->CreateClosedCircle(0.0,0.0,50.0); sketch->CloseEdition(); part->Update(); ::CoUninitialize(); return 0;}
3 利用 CAA
还没有找到CAA安装程序,以后再续
总结以上是内存溢出为你收集整理的CATIA Automation 编程初探全部内容,希望文章能够帮你解决CATIA Automation 编程初探所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)