之前用 VC 编写了画圆的简单例子,今天试了一下绘制样条曲线的功能。
还是按照 Automation 的基本流程:
1 录制宏命令
Language="VBSCRIPT" Sub CATMain() Set partDocument1 = CATIA.ActiveDocument Set part1 = partDocument1.Part Set bodies1 = part1.Bodies Set body1 = bodies1.Item("PartBody") Set sketches1 = body1.Sketches Set sketch1 = sketches1.Item("Sketch.1") Set factory2D1 = sketch1.OpenEdition() Set controlPoint2D1 = factory2D1.CreateControlPoint(120.000000,-30.000000) controlPoint2D1.ReportName = 5 Set controlPoint2D2 = factory2D1.CreateControlPoint(140.000000,-20.000000) controlPoint2D2.ReportName = 6 Set controlPoint2D3 = factory2D1.CreateControlPoint(170.000000,30.000000) controlPoint2D3.ReportName = 7 Set controlPoint2D4 = factory2D1.CreateControlPoint(190.000000,-20.000000) controlPoint2D4.ReportName = 8 Set controlPoint2D5 = factory2D1.CreateControlPoint(140.000000,-70.000000) controlPoint2D5.ReportName = 9 Set controlPoint2D6 = factory2D1.CreateControlPoint(120.000000,-30.000000) controlPoint2D6.ReportName = 10 Dim arrayOfObject1(5) Set arrayOfObject1(0) = controlPoint2D6 Set arrayOfObject1(1) = controlPoint2D5 Set arrayOfObject1(2) = controlPoint2D4 Set arrayOfObject1(3) = controlPoint2D3 Set arrayOfObject1(4) = controlPoint2D2 Set arrayOfObject1(5) = controlPoint2D1 Set spline2D1 = factory2D1.CreateSpline(arrayOfObject1) spline2D1.ReportName = 11 Set constraints1 = sketch1.Constraints Set reference1 = part1.CreateReferenceFromObject(controlPoint2D1) Set reference2 = part1.CreateReferenceFromObject(controlPoint2D6) Set constraint1 = constraints1.AddBiEltCst(catCstTypeOn,reference1,reference2) constraint1.Mode = catCstModeDrivingDimension sketch1.CloseEdition part1.InWorkObject = sketch1 part1.Update End Sub
2 翻译成 VC 代码
ControlPoint2DPtr cp[6]; cp[0] = factory2D->CreateControlPoint(60.000000,-70.000000); cp[1] = factory2D->CreateControlPoint(-10.000000,-90.000000); cp[2] = factory2D->CreateControlPoint(-100.000000,-50.000000); cp[3] = factory2D->CreateControlPoint(30.000000,70.000000); cp[4] = factory2D->CreateControlPoint(80.000000,20.000000); cp[5] = factory2D->CreateControlPoint(60.000000,-70.000000); SAFEARRAY * psa; SAFEARRAYBOUND rgsabound[1]; rgsabound[0].lLbound = 0; rgsabound[0].cElements = 1; psa = SafeArrayCreate(VT_VARIANT,1,rgsabound); if(psa == NULL) return E_OUTOFMEMORY; Spline2DPtr spline = factory2D->CreateSpline(psa); SafeArrayDestroy(psa);
代码中没有把 ControlPoint2D 和 Spline2D 联系起来,因为尝试了多种方式都没起作用,就暂时去除了相关代码。
本来根据宏代码,应该在CreateSpline时传递控制点信息,但是参数是 SAFEARRAY *,试了许久还是不知道如何传递才能起作用。
也罢,重新查看 Spline2D 的成员,发现了InsertControlPointAfter 方法。
spline->InsertControlPointAfter(cp[0],0); spline->InsertControlPointAfter(cp[1],1); spline->InsertControlPointAfter(cp[2],2); spline->InsertControlPointAfter(cp[3],3); spline->InsertControlPointAfter(cp[4],4); spline->InsertControlPointAfter(cp[5],5);
总体来说,用 VC 来实现 CATIA Automation 与 VB 比还是差很多,需要处理很多底层的内存操作和类型转换。
原文链接:https://www.f2er.com/vb/260128.html