admin 发表于 2018-2-22 13:25:32

Catia二次开发源码分享:创建草图 Sketch,约束,曲线等

有两种方式可以创建草图:
1.通过参考平面创建
//获得参考平面
CATLISTV(CATISpecObject_var) spRefPlanes = spPart->GetReferencePlanes();
//初始化草图工厂
CATISketchFactory_var spSketchFactory(spContainer);
//在XY plane 上创建草图
CATISketch_var spSketch = spSketchFactory->CreateSketch(spRefPlanes));
2.通过原点和两个矢量方向
该方法通过定义一个原点和两个方向 pH、pV 进行创建。
定义原点和方向:
double pOrigin={0,0,10};
double pH={1,0,0};
double pV={0,1,0};
CATISketchFactory_var spSketchFactory(spContainer);
CATISketch_var spSketch = spSketchFactory->CreateSketch(pOrigin, pH, pV);


sp2DFactory(spSketch);
//下面创建点
CATI2DPoint_var spPt_bottom_left, spPt_bottom_right, spPt_top_right, spPt_top_left;
double pt_bottom_left = {10, 10};
double pt_bottom_right = {50, 10};
double pt_top_right = {50, 50};
double pt_top_left = {10, 50};
spPt_bottom_left = sketch2DFactory->CreatePoint(pt_bottom_left);
spPt_bottom_right = sketch2DFactory->CreatePoint(pt_bottom_right);
spPt_top_right = sketch2DFactory->CreatePoint(pt_top_right);
spPt_top_left = sketch2DFactory->CreatePoint(pt_top_left);
//开始创建线
CATI2DLine_var spLine1, spLine2, spLine3, spLine4;
spLine1 = sketch2DFactory->CreateLine(pt_bottom_left,pt_bottom_right);
spLine2 = sketch2DFactory->CreateLine(pt_bottom_right,pt_top_right);
spLine3 = sketch2DFactory->CreateLine(pt_top_right,pt_top_left);
spLine4 = sketch2DFactory->CreateLine(pt_top_left,pt_bottom_left);
//将线的首尾连接起来

CATI2DCurve_var spCurve1 (spLine1);
CATI2DCurve_var spCurve2 (spLine2);
CATI2DCurve_var spCurve3 (spLine3);
CATI2DCurve_var spCurve4 (spLine4);
spCurve1->SetStartPoint(spPt_bottom_left);
spCurve1->SetEndPoint(spPt_bottom_right);
spCurve2->SetStartPoint(spPt_bottom_right);
spCurve2->SetEndPoint(spPt_top_right);
spCurve3->SetStartPoint(spPt_top_right);
spCurve3->SetEndPoint(spPt_top_left);
spCurve4->SetStartPoint(spPt_top_left);
spCurve4->SetEndPoint(spPt_bottom_left);
//然后退出草图:
spSketch->CloseEdition();



创建草图约束
CATI2DConstraintFactory_var spConstraint2DFactory(spSketch);
//定义spLine1 为水平约束
spConstraint2DFactory->CreateConstraint( spLine1, NULL, NULL, NULL, NULL, NULL,
NULL, Cst2DType_Horizontal, 0, 0 );
//定义spLine2 为垂直约束
spConstraint2DFactory->CreateConstraint( spLine2, NULL, NULL, NULL, NULL, NULL,
NULL, Cst2DType_Vertical, 0, 0 );
//定义spLine3 为水平约束
spConstraint2DFactory->CreateConstraint( spLine3, NULL, NULL, NULL, NULL, NULL,
NULL, Cst2DType_Horizontal, 0, 0 );
//定义spLine4 为垂直约束
spConstraint2DFactory->CreateConstraint( spLine4, NULL, NULL, NULL, NULL, NULL,
NULL, Cst2DType_Vertical, 0, 0 );
//定义spLine2 的长度约束
spConstraint2DFactory->CreateConstraint( spLine2, NULL, NULL, NULL, NULL, NULL,
NULL, Cst2DType_Length, 0, 0 );

//定义spLine2 与spLine4 的距离约束
spConstraint2DFactory->CreateConstraint( spLine2, NULL, spLine4, NULL, NULL, NULL,
NULL, Cst2DType_Distance, 0, 0 );
//定义spPt_bottom_left 与X 轴的距离约束
CATI2DAxis_var spSupport = NULL_var;
spSketch->GetAbsolute2DAxis(spSupport);
spConstraint2DFactory->CreateConstraint( spPt_bottom_left, NULL,
spSupport->GetHDirection(), NULL, NULL, NULL, NULL,Cst2DType_Distance, 0, 0 );
//定义spPt_bottom_left 与Y 轴的距离约束
spConstraint2DFactory->CreateConstraint( spPt_bottom_left, NULL,
spSupport->GetVDirection(), NULL, NULL, NULL, NULL,Cst2DType_Distance, 0, 0 );


页: [1]
查看完整版本: Catia二次开发源码分享:创建草图 Sketch,约束,曲线等