OpenCasCade(OCCT) 7.7.0 初探(三) 坐标(C#、C++/CLI)

本文代码运行可能需要头及库文件,请自行添加,文中的公共变量均使用原始例程中的变量。

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;
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霜吹花落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值