本文代码运行可能需要头及库文件,请自行添加,文中的公共变量均使用原始例程中的变量。
1.世界坐标
这个坐标是图纸的坐标,OCCT本身就有,使用下面的代码即可建立坐标。
创建右上角的三面体
//定义三面体
NCollection_Haft <Handle(AIS_ViewCube)> H_AisViewCube;
//创建三面体,显示
void ViewCube()
{
//视图立方体
H_AisViewCube() = new AIS_ViewCube();
H_AisViewCube()->SetBoxSideLabel(V3d_Xpos, "Right");
H_AisViewCube()->SetBoxSideLabel(V3d_Ypos, "Back");
H_AisViewCube()->SetBoxSideLabel(V3d_Zpos, "Top");
H_AisViewCube()->SetBoxSideLabel(V3d_Xneg, "Left");
H_AisViewCube()->SetBoxSideLabel(V3d_Yneg, "Front");
H_AisViewCube()->SetBoxSideLabel(V3d_Zneg, "Bottom");
H_AisViewCube()->SetDrawAxes(false);
H_AisViewCube()->SetSize(50, true);
H_AisViewCube()->SetTransparency(0.5);
H_AisViewCube()->SetTextColor(Quantity_Color(Quantity_NOC_MATRABLUE));
H_AisViewCube()->SetFontHeight(15);
H_AisViewCube()->SetMaterial(Graphic3d_MaterialAspect(Graphic3d_NOM_ALUMINIUM));
H_AisViewCube()->SetTransformPersistence(
new Graphic3d_TransformPers(
Graphic3d_TMF_TriedronPers,
Aspect_TOTP_RIGHT_UPPER,
Graphic3d_Vec2i(70, 70)));
myAISContext()->Display(H_AisViewCube(), Standard_True);
}
显示左下角的坐标轴
void Triedron()
{
if (!myView().IsNull())
{
myView()->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_ALICEBLUE, 0.1, V3d_ZBUFFER);
}
}
2.局部坐标的建立,这个局部坐标系非常有用,在很多情况下需要查看平移、旋转情况时就需要有个参考。
gp_Ax2 theAxis;
double theSize = 60;//大小
//设置原点
theAxis.SetLocation(gp_Pnt(10, 10, 10));
//以世界坐标轴为准,设置Z,X轴方向
theAxis.SetDirection(gp_Dir(0, 0, 1));
theAxis.SetXDirection(gp_Dir(1, 0, 0));
gp_Pnt anOrg = theAxis.Location();
Handle(Geom_Axis2Placement) axis = new Geom_Axis2Placement(theAxis);
axis->SetLocation(gp::Origin());
Handle(AIS_Trihedron) aisTrihedron = new AIS_Trihedron(axis);
aisTrihedron->SetDatumDisplayMode(Prs3d_DM_Shaded);
aisTrihedron->SetDrawArrows(true);
aisTrihedron->Attributes()->DatumAspect()->LineAspect(Prs3d_DP_XArrow)->SetWidth(2);
aisTrihedron->Attributes()->DatumAspect()->LineAspect(Prs3d_DP_YArrow)->SetWidth(2);
aisTrihedron->Attributes()->DatumAspect()->LineAspect(Prs3d_DP_ZArrow)->SetWidth(2);
aisTrihedron->Attributes()->DatumAspect()->LineAspect(Prs3d_DP_XAxis)->SetWidth(1.5);
aisTrihedron->Attributes()->DatumAspect()->LineAspect(Prs3d_DP_YAxis)->SetWidth(1.5);
aisTrihedron->Attributes()->DatumAspect()->LineAspect(Prs3d_DP_ZAxis)->SetWidth(1.5);
aisTrihedron->SetDatumPartColor(Prs3d_DP_XArrow, Quantity_NOC_RED2);
aisTrihedron->SetDatumPartColor(Prs3d_DP_YArrow, Quantity_NOC_GREEN2);
aisTrihedron->SetDatumPartColor(Prs3d_DP_ZArrow, Quantity_NOC_BLUE2);
aisTrihedron->SetDatumPartColor(Prs3d_DP_XAxis, Quantity_NOC_RED2);
aisTrihedron->SetDatumPartColor(Prs3d_DP_YAxis, Quantity_NOC_GREEN2);
aisTrihedron->SetDatumPartColor(Prs3d_DP_ZAxis, Quantity_NOC_BLUE2);
aisTrihedron->SetLabel(Prs3d_DP_XAxis, "X");
aisTrihedron->SetLabel(Prs3d_DP_YAxis, "Y");
aisTrihedron->SetLabel(Prs3d_DP_ZAxis, "Z");
aisTrihedron->SetSize(theSize);
aisTrihedron->SetTransformPersistence(new Graphic3d_TransformPers(Graphic3d_TMF_ZoomPers, anOrg));
aisTrihedron->Attributes()->SetZLayer(Graphic3d_ZLayerId_Topmost);
aisTrihedron->SetInfiniteState(true);
myAISContext()->Display(aisTrihedron, true);
3.坐标的转换,鼠标点与图纸点的相互转换
//!Windows控件点转图纸点
void PointToPnt(int X, int Y, double% x, double% y, double% z)
{
Standard_Real x1, y1, z1;
myView()->Convert(X, Y, x1, y1, z1);
x = SetPrecision(x1, Precision);
y = SetPrecision(y1, Precision);
z = SetPrecision(z1, Precision);
}
//!图纸点转Windows控件点
void PntToPoint(double x, double y, double z, int% X, int% Y)
{
Standard_Real x1 = x, y1 = y, z1 = z;
Standard_Integer X1, Y1;
myView()->Convert(x1, y1, z1, X1, Y1);
X = X1;
Y = Y1;
}
//!截取小数位数
double SetPrecision(double num, int length)
{
int a = 1;
for (int i = 0; i < length; i++) a *= 10;
return round(num * a) / a;
}