OpenCasCade(OCCT) 7.7.0 初探(四) 移动和旋转(C#、C++/CLI)

移动和旋转时可以直接移动和旋转AIS_Shape,也可以再生成另外一个TopoDs_Shape。另外,有必要说一下,上一节的局部坐标有必要定义成为一个函数过程,这样在移动或旋转时可以很清晰的看出移动及旋转。有两种方式进行移动和旋转,一种是移动AIS_Shape,一种是生成新的TopoDS_Shape再创建AIS_Shape。

	void DisplayTrihedron(gp_Ax2 theAxis)
	{
		//gp_Ax2 Position;
		//Position.SetLocation(origin_xyz);
		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->SetTextColor(Quantity_NOC_BLACK);
		aisTrihedron->SetSize(60);

		aisTrihedron->SetTransformPersistence(new Graphic3d_TransformPers(Graphic3d_TMF_ZoomPers, anOrg));
		aisTrihedron->Attributes()->SetZLayer(Graphic3d_ZLayerId_Topmost);
		aisTrihedron->SetInfiniteState(true);
		myAISContext()->Display(aisTrihedron, true);
	}

1.移动比较简单

	void testMove()
	{
		//在0,0,0显示一个坐标
		gp_Pnt gps(0, 0, 0);
		gp_Ax2 ax2 = gp_Ax2(gps, gp_Dir(0, 0, 1));
		DisplayTrihedron(ax2);
		//从原点0,0,0和X,Y,Z轴方向绘制一个矩形
		TopoDS_Shape tshapea = BRepPrimAPI_MakeBox(100, 50, 80);
		//为了直观我把每次变化前后都用不同的颜色显示出来
		//显示蓝色的矩形->刚开始绘制的
		Handle(AIS_Shape) ashapea = new AIS_Shape(tshapea);
		ashapea->SetColor(Quantity_NOC_BLUE);
		myAISContext()->Display(ashapea, true);
		//将矩形移动
		gp_Pnt gpe(110, 60, 90);
		gp_Trsf trsfa;
		trsfa.SetTranslation(gps, gpe);
		//显示移动后的红色矩形
		//方式一:移动AIS_Shape
		//ashapea->SetLocalTransformation(trsfa);
		//ashapea->SetColor(Quantity_NOC_RED);
		//myAISContext()->Display(ashapea, true);
		//方式二:重新生成TopoDs_Shape
		TopLoc_Location loca(trsfa);
		TopoDS_Shape tshapeb = tshapea.Moved(loca);
		ax2 = gp_Ax2(gpe, gp_Dir(0, 0, 1));
		DisplayTrihedron(ax2);
		Handle(AIS_Shape) ashapeb = new AIS_Shape(tshapeb);
		ashapeb->SetColor(Quantity_NOC_RED);
		myAISContext()->Display(ashapeb, true);

		return;
	}

2.旋转,旋转需要最重要的一点就是确定旋转轴,当连续旋转时还要知道是按图纸坐标的静态旋转还是按刚刚旋转完的坐标进行动态旋转,这种旋转角度和坐标,OCCT用的是右手坐标系。旋转时,是使用gp_Ax1作为旋转轴进行的,旋转时,都是绕Z轴旋转,所以要变化Z的方向,静态旋转直接指定方向即可,动态旋转可以借助gp_Ax2旋转后方向的确定。

绕固定轴旋转

	void testRotation()
	{
		myAISContext()->EraseAll(true);
		//在0,0,0显示一个坐标
		gp_Pnt gps(0, 0, 0);
		gp_Ax2 ax2 = gp_Ax2(gps, gp_Dir(0, 0, 1));
		DisplayTrihedron(ax2);
		//从原点0,0,0和X,Y,Z轴方向绘制一个矩形
		TopoDS_Shape tshapea = BRepPrimAPI_MakeBox(100, 50, 80);
		//为了直观我把每次变化前后都用不同的颜色显示出来
		//显示蓝色的矩形->刚开始绘制的
		Handle(AIS_Shape) ashapea = new AIS_Shape(tshapea);
		ashapea->SetColor(Quantity_NOC_BLUE);
		myAISContext()->Display(ashapea, true);
		//绕固定轴Z旋转30度,需要转换为弧度
		//指定ax1在(0,0,0),指定Z轴方向,默认都是以Z轴旋转,所以在旋转过程中,都需要指定Z轴方向
		gp_Ax1 ax1 = gp_Ax1(gps, gp_Dir(0, 0, 1));//当前Z轴与图纸坐标一致
		gp_Trsf trsfa;
		trsfa.SetRotation(ax1, AngleChange(30, true));

		//显示旋转后的红色矩形
		//方式一:AIS_Shape
		//ashapea->SetLocalTransformation(trsfa);
		//ashapea->SetColor(Quantity_NOC_RED);
		//myAISContext()->Display(ashapea, true);
		//方式二:重新生成TopoDs_Shape
		TopLoc_Location loca(trsfa);
		TopoDS_Shape tshapeb = tshapea.Moved(loca);
		Handle(AIS_Shape) ashapeb = new AIS_Shape(tshapeb);
		ashapeb->SetColor(Quantity_NOC_RED);
		myAISContext()->Display(ashapeb, true);
		ax2.SetAxis(ax1);
		ax2.Rotate(ax1, AngleChange(30, true));
		DisplayTrihedron(ax2);
		//再绕固定Y轴(图纸Y轴)旋转30度
		//上面提到了,旋转都是绕Z轴,那么此时ax1需要指定图纸Y轴方向为Z轴
		ax1 = gp_Ax1(gps, gp_Dir(0, 1, 0));
		gp_Trsf trsfb;
		trsfb.SetRotation(ax1, AngleChange(30, true));
		//方式一略
		//方式二
		TopLoc_Location locb(trsfb);
		TopoDS_Shape tshapec = tshapeb.Moved(locb);
		Handle(AIS_Shape) ashapec = new AIS_Shape(tshapec);
		ashapec->SetColor(Quantity_NOC_YELLOW);
		myAISContext()->Display(ashapec, true);
		ax2.SetAxis(ax1);
		ax2.Rotate(ax1, AngleChange(30, true));
		DisplayTrihedron(ax2);
		return;
	}

//!角度转弧度或弧度转角度,angle=true 角度转弧度
double AngleChange(double num, bool angle)
{
	return angle ? (num * (System::Math::PI / 180)) : (num * (180 / System::Math::PI));
}

绕动态轴旋转,绕静态和动态轴旋转变化不大,请注意代码中使用了ax2,此时ax2不止是用于显示坐标,还给旋转提供了旋转轴。

	void testRotation()
	{
		//在0,0,0显示一个坐标
		gp_Pnt gps(0, 0, 0);
		gp_Ax2 ax2 = gp_Ax2(gps, gp_Dir(0, 0, 1));
		DisplayTrihedron(ax2);
		//从原点0,0,0和X,Y,Z轴方向绘制一个矩形
		TopoDS_Shape tshapea = BRepPrimAPI_MakeBox(100, 50, 80);
		//为了直观我把每次变化前后都用不同的颜色显示出来
		//显示蓝色的矩形->刚开始绘制的
		Handle(AIS_Shape) ashapea = new AIS_Shape(tshapea);
		ashapea->SetColor(Quantity_NOC_BLUE);
		myAISContext()->Display(ashapea, true);
		//绕固定轴Z旋转30度,需要转换为弧度
		//指定ax1在(0,0,0),指定Z轴方向,默认都是以Z轴旋转,所以在旋转过程中,都需要指定Z轴方向
		gp_Ax1 ax1 = gp_Ax1(gps, gp_Dir(0, 0, 1));//当前Z轴与图纸坐标一致
		gp_Trsf trsfa;
		trsfa.SetRotation(ax1, AngleChange(30, true));

		//显示旋转后的红色矩形
		//方式一:AIS_Shape
		//ashapea->SetLocalTransformation(trsfa);
		//ashapea->SetColor(Quantity_NOC_RED);
		//myAISContext()->Display(ashapea, true);
		//方式二:重新生成TopoDs_Shape
		TopLoc_Location loca(trsfa);
		TopoDS_Shape tshapeb = tshapea.Moved(loca);
		Handle(AIS_Shape) ashapeb = new AIS_Shape(tshapeb);
		ashapeb->SetColor(Quantity_NOC_RED);
		myAISContext()->Display(ashapeb, true);
		ax2.SetAxis(ax1);//绕动态轴需要使用
		ax2.Rotate(ax1, AngleChange(30, true));//绕动态轴需要使用
		DisplayTrihedron(ax2);
		//此位置为绕动态和静态旋转的区别
		//再绕动态Y轴旋转30度
		//上面提到了,旋转都是绕Z轴,那么此时ax1需要绕旋转后的Y轴旋转
		//此时需要用ax2,ax2旋转后的Y轴方向为ax2.YDirection()
		ax1 = ax2.Axis();
		ax1.SetDirection(ax2.YDirection());
		gp_Trsf trsfb;
		trsfb.SetRotation(ax1, AngleChange(30, true));
		//方式一略
		//方式二
		TopLoc_Location locb(trsfb);
		TopoDS_Shape tshapec = tshapeb.Moved(locb);
		Handle(AIS_Shape) ashapec = new AIS_Shape(tshapec);
		ashapec->SetColor(Quantity_NOC_YELLOW);
		myAISContext()->Display(ashapec, true);
		ax2.SetAxis(ax1);
		ax2.Rotate(ax1, AngleChange(30, true));
		DisplayTrihedron(ax2);
		return;
	}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

霜吹花落

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

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

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

打赏作者

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

抵扣说明:

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

余额充值