最近开始对 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, &result, pApp, 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, &result, documents, L"Add", 1 , arg1); buffer.vt = VT_DISPATCH; buffer.pdispVal = result.pdispVal; IDispatch *partDocument = buffer.pdispVal; // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, partDocument, L"Part", 0); buffer.vt = VT_DISPATCH; buffer.pdispVal = result.pdispVal; IDispatch *part = buffer.pdispVal; // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, 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, &result, bodies, L"Item", 1, arg); buffer.vt = VT_DISPATCH; buffer.pdispVal = result.pdispVal; IDispatch *body = buffer.pdispVal; // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, body, L"Sketches", 0); buffer.vt = VT_DISPATCH; buffer.pdispVal = result.pdispVal; IDispatch *sketches1 = buffer.pdispVal; // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, part, L"OriginElements", 0); buffer.vt = VT_DISPATCH; buffer.pdispVal = result.pdispVal; IDispatch *originElements = buffer.pdispVal; // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, 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, &result, sketches1, L"Add", 1, 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, 1, rgsabound); // //hr = SafeArrayAccessData(psa); //hr = SafeArrayUnaccessData(psa); //VARIANT array; //VariantInit(&array); //array.vt = VT_ARRAY; //array.pparray = &psa; // //hr = AutoWrap(DISPATCH_METHOD, &result, sketch1, L"SetAbsoluteAxisData", 1, array); VARIANT arg4; VariantInit(&arg4); arg4.vt = VT_DISPATCH; arg4.pdispVal = sketch1; hr = AutoWrap(DISPATCH_PROPERTYPUT|DISPATCH_METHOD, &result, part, L"InWorkObject", 1, arg4); //buffer.vt = VT_DISPATCH; //buffer.pdispVal = result.pdispVal; //IDispatch *sketch1 = buffer.pdispVal; // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, sketch1, 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, &result, factory2D1, L"CreateClosedCircle", 3, a1, a2, a3); // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, sketch1, L"CloseEdition", 0); // hr = AutoWrap(DISPATCH_PROPERTYGET|DISPATCH_METHOD, &result, part, 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安装程序,以后再续