VS2015 NX12
关键代码
void DrawingsDatumPoint::getOriginalWCS() //获取原WCS
{
try
{
if (isGetWcs == true)
{
setNewWCS(); //设置WCS
return ;
}
NXOpen::CartesianCoordinateSystem* OriginalWcsData = workPart->WCS()->CoordinateSystem();
//获得WCS的相关信息
OriginalWcsOrigin = workPart->WCS()->Origin();
OriginalWcsData->GetDirections(&OriginalxDirection, &OriginalyDirection);
OriginalWsmtx = workPart->WCS()->CoordinateSystem()->Orientation()->Element();
//获得WCS的可见性
WcsVis=true;
WcsVis = workPart->WCS()->Visibility();
//设置WCS的可见性
if ( WcsVis == false )
{
workPart->WCS()->SetVisibility(true);
}
isGetWcs = true ;
manip0->SetOrigin(OriginalWcsOrigin);
//sprintf (msg, "WCS的原点:%.2f %.2f %.2f ", OriginalWcsOrigin.X , OriginalWcsOrigin.Y,OriginalWcsOrigin.Z);
//theSession->ListingWindow()->WriteLine(msg);
//sprintf (msg, "WCS的X轴:%.2f %.2f %.2f ", OriginalxDirection.X , OriginalxDirection.Y,OriginalxDirection.Z);
//theSession->ListingWindow()->WriteLine(msg);
//sprintf (msg, "WCS的Y轴:%.2f %.2f %.2f ", OriginalyDirection.X , OriginalyDirection.Y,OriginalyDirection.Z);
//theSession->ListingWindow()->WriteLine(msg);
//sprintf (msg, "WCS的矩阵:%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f ",
// OriginalWsmtx.Xx , OriginalWsmtx.Xy,OriginalWsmtx.Xz,
// OriginalWsmtx.Yx , OriginalWsmtx.Yy,OriginalWsmtx.Yz,
// OriginalWsmtx.Zx , OriginalWsmtx.Zy,OriginalWsmtx.Zz );
//theSession->ListingWindow()->WriteLine(msg);
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
DrawingsDatumPoint::theUI->NXMessageBox()->Show("获取原WCS", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
void DrawingsDatumPoint::setNewWCS() //设置WCS
{
try
{
isMoveWcs = false;
//获取方位器上的方向及原点
Point3d thepoint = manip0->Origin();
Vector3d theX = manip0->XAxis();
Vector3d theY = manip0->YAxis();
double yuandian[3] = { thepoint.X, thepoint.Y, thepoint.Z };
double uf_X[3] = { theX.X, theX.Y, theX.Z };
double uf_Y[3] = { theY.X, theY.Y, theY.Z };
double uf_Z[3];
UF_VEC3_cross( uf_X, uf_Y, uf_Z );
Vector3d theZ (uf_Z[0],uf_Z[1],uf_Z[2]) ;
//创建新WCS
Matrix3x3 matrix1;
matrix1.Xx = theX.X;
matrix1.Xy = theX.Y;
matrix1.Xz = theX.Z;
matrix1.Yx = theY.X;
matrix1.Yy = theY.Y;
matrix1.Yz = theY.Z;
matrix1.Zx = theZ.X;
matrix1.Zy = theZ.Y;
matrix1.Zz = theZ.Z;
workPart->WCS()->SetOriginAndMatrix(thepoint, matrix1);
isMoveWcs = true;
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
DrawingsDatumPoint::theUI->NXMessageBox()->Show("设置WCS", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
//恢复WCS
void DrawingsDatumPoint::retcoverWCS()
{
try
{
workPart->WCS()->SetOriginAndMatrix(OriginalWcsOrigin, OriginalWsmtx);
//设置WCS的可见性
if ( WcsVis == false )
{
workPart->WCS()->SetVisibility(true);
}
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
DrawingsDatumPoint::theUI->NXMessageBox()->Show("恢复WCS", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
void DrawingsDatumPoint::getBox() //调包容盒
{
try
{
if (selectObjs.size()==0)
{
return;
}
UF_initialize();
std::vector<double>x_point,y_point,z_point;
x_point.clear();
y_point.clear();
z_point.clear();
tag_t csys_tag=null_tag;;
double min_corner [ 3 ] ;
double directions [ 3 ] [ 3 ] ;
double distances [ 3 ] ;
for (int i = 0; i < selectObjs.size(); i++)
{
UF_MODL_ask_bounding_box_exact(selectObjs[i]->Tag(),csys_tag,min_corner,directions,distances);
double point_max[3]={min_corner [ 0 ]+distances [ 0], min_corner [ 1 ]+distances [ 1],min_corner [ 2]+distances [ 2],};
x_point.push_back(min_corner[0]);
x_point.push_back(point_max[0]);
y_point.push_back(min_corner[1]);
y_point.push_back(point_max[1]);
z_point.push_back(min_corner[2]);
z_point.push_back(point_max[2]);
}
//排序
sort(x_point.begin(),x_point.end());
sort(y_point.begin(),y_point.end());
sort(z_point.begin(),z_point.end());
//最大外形尺寸
BlockXvaule=(x_point.back()-x_point[0] );//计算 X 方向最大长度
BlockYvaule=(y_point.back()-y_point[0] );//计算 Y 方向最大长度
BlockZvaule=(z_point.back()-z_point[0] );//计算 Z 方向最大长度
Point3d point1 (x_point[0],y_point[0],z_point[0]);
blockOrigni = point1;
//sprintf(msg,"%0.2f %0.2f %0.2f",x_point[0],y_point[0],z_point[0]);
//theSession->ListingWindow()->WriteLine(msg);
UF_terminate();
setWCSOrigin(); //设置WCS原点
getOriginalWCS();//获取原WCS
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
DrawingsDatumPoint::theUI->NXMessageBox()->Show("调包容盒", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}
void DrawingsDatumPoint::setWCSOrigin( ) //设置WCS原点
{
try
{
//---- Enter your callback code here -----
GetUI(); //获取UI值
NXOpen::CartesianCoordinateSystem* WcsData = workPart->WCS()->CoordinateSystem();
Point3d WcsOrigin = workPart->WCS()->Origin();
WcsData->GetDirections(&xDirection, &yDirection);
double yuandian[3] = { blockOrigni.X , blockOrigni.Y, blockOrigni.Z }; //
double ufun_X[3] = { xDirection.X, xDirection.Y , xDirection.Z };
double ufun_Y[3] = { yDirection.X, yDirection.Y , yDirection.Z };
double ufun_Z[3];
UF_initialize();
UF_VEC3_cross( ufun_X, ufun_Y, ufun_Z );//通过叉乘求Z轴
Vector3d manip0Z ( ufun_Z[0], ufun_Z[1], ufun_Z[2] ); //Z轴double型转Vector3d
//左下角点
double newpoint1[3], newpoint2[3], newpoint3[3];
//硬设原点
if(enumTypeValue==0)//底面四边中心
{
UF_VEC3_affine_comb( yuandian, BlockXvaule/2, ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, BlockYvaule/2, ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, 0.0, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==1)//底面+X-Y
{
UF_VEC3_affine_comb( yuandian, BlockXvaule, ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, 0.0, ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, 0.0, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==2)//底面+X+Y
{
UF_VEC3_affine_comb( yuandian, BlockXvaule , ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, BlockYvaule , ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, 0.0, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==3)//底面-X+Y
{
UF_VEC3_affine_comb( yuandian, 0.0 , ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, BlockYvaule , ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, 0.0, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==4)//底面-X-Y
{
UF_VEC3_affine_comb( yuandian, 0.0 , ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, 0.0 , ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, 0.0, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==5)//体中心
{
UF_VEC3_affine_comb( yuandian, BlockXvaule/2, ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, BlockYvaule/2, ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, BlockZvaule/2, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==6)//顶面四边中心
{
UF_VEC3_affine_comb( yuandian, BlockXvaule/2, ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, BlockYvaule/2, ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, BlockZvaule, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==7)//顶面+X-Y
{
UF_VEC3_affine_comb( yuandian, BlockXvaule, ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, 0.0, ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, BlockZvaule, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==8)//顶面+X+Y
{
UF_VEC3_affine_comb( yuandian, BlockXvaule , ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, BlockYvaule , ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, BlockZvaule, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==9)//顶面-X+Y
{
UF_VEC3_affine_comb( yuandian, 0.0 , ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, BlockYvaule , ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, BlockZvaule, ufun_Z, newpoint3 );//
}
else if(enumTypeValue==10)//顶面-X-Y
{
UF_VEC3_affine_comb( yuandian, 0.0 , ufun_X , newpoint1 ); //
UF_VEC3_affine_comb( newpoint1, 0.0 , ufun_Y, newpoint2 ); //
UF_VEC3_affine_comb( newpoint2, BlockZvaule, ufun_Z, newpoint3 );//
}
Point3d newOrigin( newpoint3[0], newpoint3[1], newpoint3[2] );
NewWcsOrgin = newOrigin;
//theSession->ListingWindow()->WriteLine("点X:" + doubleToNXString( NewWcsOrgin.X ) +" 点Y:" + doubleToNXString( NewWcsOrgin.Y ) + " 点Z:" + doubleToNXString( NewWcsOrgin.Z ));
manip0->SetOrigin(NewWcsOrgin);
UF_terminate();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
DrawingsDatumPoint::theUI->NXMessageBox()->Show("设置WCS原点", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}