旋转角度 坐标系旋转 组件旋转 UF_MTX3_rotate_about_axis

void MyClass::坐标旋转()//测试函数
{
	tag_t w = 0;
	UF_CSYS_ask_wcs(&w);

   //获取当前坐标系的信息
	UGAPI::CoordinateSystem coord = UGAPI::CSYS::Ask_wcs_inf();

	double mtx[9] = { 0 };
	double angle = -45;
	//angle = angle * 180 / 3.14159;
	angle = angle * 3.14159 / 180;
	UF_MTX3_rotate_about_axis(coord.z_axis, angle, mtx);
	//UGAPI::msg::print(coord.z_axis, 3);
	//UGAPI::msg::print(mtx, 9);

    //根据矩阵 原点 创建坐标系
	UGAPI::CSYS::CreateTempWCS_mtx(coord.origin, mtx, false);

	if (0)
	{
		旋转矩阵
		double matrix[16] = { 0 }; //矩阵
		int status;
		uf5945(coord.origin, coord.z_axis, &angle, matrix, &status);
		UGAPI::msg::print(matrix, 16);

        // 和上面函数的结果是一样的
		UF_MTX4_rotation(coord.origin, coord.z_axis, angle, matrix);
		UGAPI::msg::print(matrix, 16);
	}

}


	/**
	* 逆时针旋转WCS坐标系
	* FucnName:wcsRotate
	* param axisStr 绕哪个轴旋转X/Y/Z
	 * param angle  旋转角度
	*  例子  wcsRotate("Y",90.0)
	*/
	void CSYS::wcsRotate(string axisStr, double angle)
	{

		//获得当前工作坐标
		tag_t wcs_id;
		UF_CSYS_ask_wcs(&wcs_id);

		//获得坐标 相关的矩阵和原点
		tag_t matrix_id = NULL_TAG;
		double o_point[3];
		UF_CSYS_ask_csys_info(wcs_id, &matrix_id, o_point);

		//获得 3x3矩阵数据
		double wsc_9[9] = { 0 };
		UF_CSYS_ask_matrix_values(matrix_id, wsc_9);

		//输入一个矩阵返回z方向向量 //z轴
		double vec[3];
		if (axisStr == "X" || axisStr == "x")
		{
			UF_MTX3_x_vec(wsc_9, vec);
		}
		else if (axisStr == "Y" || axisStr == "y")
		{
			UF_MTX3_y_vec(wsc_9, vec);
		}
		else
		{
			UF_MTX3_z_vec(wsc_9, vec);
		}

		//创建一个临时的坐标系统
		tag_t wsc_t = NULL_TAG;
		UF_CSYS_create_temp_csys(o_point, matrix_id, &wsc_t);

		//旋转矩阵
		double matrix[16]; //矩阵
		int status;
		uf5945(o_point, vec, &angle, matrix, &status);


		//矩阵的实现
		int objects = 1;		//数量
		int  move = 2;			// 1 :移动 2 :复制
		int  layer = -1;		//0:最初层; -1: 工作层; 1 - 256 : 指定层
		int trace_curves = 2;	//轨迹状态, 1 开, 2 关
		tag_t wsc_t2 = NULL_TAG; //变化后坐标tag
		uf5947(matrix, &wsc_t, &objects, &move, &layer, &trace_curves, &wsc_t2, NULL, &status);

		//设置工作坐标
		UF_CSYS_set_wcs(wsc_t2);
		//UF_CSYS_set_wcs_display(1);
	}



    /* 拖拽方位器 旋转组件子函数 按角度 旋转组件  单独旋转  */
 	void MODL::ASSEM_RoateComponent(vector< NXOpen::Assemblies::Component*>& vT, double ori[3], double vec[3], double angle) {

		NXOpen::Session* theSession = NXOpen::Session::GetSession();
		NXOpen::Part* workPart(theSession->Parts()->Work());
		NXOpen::Part* displayPart(theSession->Parts()->Display());
		// ----------------------------------------------
		//   Menu: 装配(A)->组件位置(P)->移动组件(E)...
		// ----------------------------------------------

		NXOpen::Positioning::ComponentPositioner* componentPositioner1;
		componentPositioner1 = workPart->ComponentAssembly()->Positioner();

		componentPositioner1->ClearNetwork();

		NXOpen::Assemblies::Arrangement* arrangement1(dynamic_cast<NXOpen::Assemblies::Arrangement*>(workPart->ComponentAssembly()->Arrangements()->FindObject("Arrangement 1")));
		componentPositioner1->SetPrimaryArrangement(arrangement1);

		componentPositioner1->BeginMoveComponent();

		bool allowInterpartPositioning1;
		allowInterpartPositioning1 = theSession->Preferences()->Assemblies()->InterpartPositioning();


		NXOpen::Positioning::Network* network1;
		network1 = componentPositioner1->EstablishNetwork();

		NXOpen::Positioning::ComponentNetwork* componentNetwork1(dynamic_cast<NXOpen::Positioning::ComponentNetwork*>(network1));
		componentNetwork1->SetMoveObjectsState(true);

		NXOpen::Assemblies::Component* nullNXOpen_Assemblies_Component(NULL);
		componentNetwork1->SetDisplayComponent(nullNXOpen_Assemblies_Component);

		componentNetwork1->SetNetworkArrangementsMode(NXOpen::Positioning::ComponentNetwork::ArrangementsModeExisting);

		NXOpen::Session::UndoMarkId markId2;
		markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, "Move Component Update");
		componentNetwork1->RemoveAllConstraints();
		std::vector<NXOpen::NXObject*> movableObjects1;
		for (int i = 0; i < vT.size(); i++)
		{
			movableObjects1.push_back(vT[i]);
		}
		componentNetwork1->SetMovingGroup(movableObjects1);


		bool loaded3;
		loaded3 = componentNetwork1->IsReferencedGeometryLoaded();

		componentNetwork1->BeginDrag();

		double mtx[9] = { 0 };

        // 计算变化的矩阵
		UF_MTX3_rotate_about_axis(vec, angle, mtx);

		NXOpen::Vector3d translation2(ori[0], ori[1], ori[2]);
		NXOpen::Matrix3x3 rotation1;
		rotation1.Xx = mtx[0];
		rotation1.Xy = mtx[1];
		rotation1.Xz = mtx[2];
		rotation1.Yx = mtx[3];
		rotation1.Yy = mtx[4];
		rotation1.Yz = mtx[5];
		rotation1.Zx = mtx[6];
		rotation1.Zy = mtx[7];
		rotation1.Zz = mtx[8];;
        // 核心是这句
		componentNetwork1->DragByTransform(translation2, rotation1);

		componentNetwork1->EndDrag();

		componentNetwork1->ResetDisplay();

		componentNetwork1->ApplyToModel();


		componentNetwork1->Solve();

		componentPositioner1->ClearNetwork();

		int nErrs1;
		nErrs1 = theSession->UpdateManager()->AddToDeleteList(componentNetwork1);

		componentPositioner1->DeleteNonPersistentConstraints();

		int nErrs2;
		nErrs2 = theSession->UpdateManager()->DoUpdate(markId2);



		componentPositioner1->EndMoveComponent();

		NXOpen::Assemblies::Arrangement* nullNXOpen_Assemblies_Arrangement(NULL);
		componentPositioner1->SetPrimaryArrangement(nullNXOpen_Assemblies_Arrangement);

	}


	/* 旋转平移 组件子函数 */
	void MODL::ASSEM_moveComponent(vector< NXOpen::Assemblies::Component*>& vT, double tran[16]) {

		NXOpen::Session* theSession = NXOpen::Session::GetSession();
		NXOpen::Part* workPart(theSession->Parts()->Work());
		NXOpen::Part* displayPart(theSession->Parts()->Display());
		// ----------------------------------------------
		//   Menu: 装配(A)->组件位置(P)->移动组件(E)...
		// ----------------------------------------------

		NXOpen::Positioning::ComponentPositioner* componentPositioner1;
		componentPositioner1 = workPart->ComponentAssembly()->Positioner();

		componentPositioner1->ClearNetwork();

		NXOpen::Assemblies::Arrangement* arrangement1(dynamic_cast<NXOpen::Assemblies::Arrangement*>(workPart->ComponentAssembly()->Arrangements()->FindObject("Arrangement 1")));
		componentPositioner1->SetPrimaryArrangement(arrangement1);

		componentPositioner1->BeginMoveComponent();

		bool allowInterpartPositioning1;
		allowInterpartPositioning1 = theSession->Preferences()->Assemblies()->InterpartPositioning();

		NXOpen::Positioning::Network* network1;
		network1 = componentPositioner1->EstablishNetwork();

		NXOpen::Positioning::ComponentNetwork* componentNetwork1(dynamic_cast<NXOpen::Positioning::ComponentNetwork*>(network1));
		componentNetwork1->SetMoveObjectsState(true);

		NXOpen::Assemblies::Component* nullNXOpen_Assemblies_Component(NULL);
		componentNetwork1->SetDisplayComponent(nullNXOpen_Assemblies_Component);

		componentNetwork1->SetNetworkArrangementsMode(NXOpen::Positioning::ComponentNetwork::ArrangementsModeExisting);

		NXOpen::Session::UndoMarkId markId2;
		markId2 = theSession->SetUndoMark(NXOpen::Session::MarkVisibilityInvisible, "Move Component Update");
		componentNetwork1->RemoveAllConstraints();
		std::vector<NXOpen::NXObject*> movableObjects1;
		for (int i = 0; i < vT.size(); i++)
		{
			movableObjects1.push_back(vT[i]);
		}
		componentNetwork1->SetMovingGroup(movableObjects1);

		bool loaded3;
		loaded3 = componentNetwork1->IsReferencedGeometryLoaded();

		componentNetwork1->BeginDrag();

		NXOpen::Vector3d translation2(tran[3], tran[7], tran[11]);
		NXOpen::Matrix3x3 rotation1;
		rotation1.Xx = tran[0];
		rotation1.Xy = tran[1];
		rotation1.Xz = tran[2];
		rotation1.Yx = tran[4];
		rotation1.Yy = tran[5];
		rotation1.Yz = tran[6];
		rotation1.Zx = tran[8];
		rotation1.Zy = tran[9];
		rotation1.Zz = tran[10];;
		componentNetwork1->DragByTransform(translation2, rotation1);

		componentNetwork1->EndDrag();

		componentNetwork1->ResetDisplay();

		componentNetwork1->ApplyToModel();

		componentNetwork1->Solve();

		componentPositioner1->ClearNetwork();

		int nErrs1;
		nErrs1 = theSession->UpdateManager()->AddToDeleteList(componentNetwork1);

		componentPositioner1->DeleteNonPersistentConstraints();

		int nErrs2;
		nErrs2 = theSession->UpdateManager()->DoUpdate(markId2);

		componentPositioner1->EndMoveComponent();

		NXOpen::Assemblies::Arrangement* nullNXOpen_Assemblies_Arrangement(NULL);
		componentPositioner1->SetPrimaryArrangement(nullNXOpen_Assemblies_Arrangement);

	}


   /* 装配移动 旋转平移一起 传入 旧的点和 x   y  轴*/
	int  MODL::ASSEM_moveAndRoteCompent(vector<NXOpen::Assemblies::Component*>vComp, Point3d& old_point, Vector3d& old_x, Vector3d& old_y, Point3d& new_point, Vector3d& new_x, Vector3d& new_y)
	{

		double b_p[3] = { old_point.X,old_point.Y,old_point.Z };
		double b_x[3] = { old_x.X,old_x.Y,old_x.Z };
		double b_y[3] = { old_y.X,old_y.Y,old_y.Z };

		double e_p[3] = { new_point.X,new_point.Y,new_point.Z };
		double e_x[3] = { new_x.X,new_x.Y,new_x.Z };
		double e_y[3] = { new_y.X,new_y.Y,new_y.Z };

		double mtx[16] = { 0 };
		UF_MTX4_csys_to_csys(b_p, b_x, b_y,
			e_p, e_x, e_y, mtx);

		ASSEM_moveComponent(vComp, mtx);

		old_x.X = new_x.X;
		old_x.Y = new_x.Y;
		old_x.Z = new_x.Z;
		old_y.X = new_y.X;
		old_y.Y = new_y.Y;
		old_y.Z = new_y.Z;
		old_point.X = new_point.X;
		old_point.Y = new_point.Y;
		old_point.Z = new_point.Z;



		return 0;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值