UG/NX二次开发:PK测量对象(体积、质量、表面积、质心),函数:PK_TOPOL_eval_mass_props()


void Pk_MeasuringObj(tag_t obj,double& volume,double&quality,double centroid[3],double &area)
{   
    tag_t ps_tag=NULL_TAG;
    UF_PS_ask_ps_tag_of_object(obj_tag,&pstag);
    PK_TOPOL_t topol=ps_tag;
    PK_TOPOL_eval_mass_props_o_t props;
    PK_TOPOL_eval_mass_props_o_m(props);
    props.mass=PK_mass_c_of_g_c;
    double amount[3],mass[3], c_of_g[3],m_of_i[9],periphery[3];
    PK_TOPOL_eval_mass_props(1,&topol,0.99,&props,amount,mass,c_of_g,m_of_i,periphery);
    //体积mm^3
    volume = fabs(amount[0]*1000.0*1000.0*1000.0);
    //质量kg
	quality = mass[0];
    //质心
	for (int i = 0;i<3;i++){
      centroid[i] = c_of_g[i]*1000.0;
    }
    //面积mm^2
    area = fabs(periphery[0]*1000.0*1000.0*1000.0); 
}

好的,下面是一个简单的UG/Open API函数结合C++写的OBB算法获取对象的最小包容盒的示例代码: ```c++ // UG/Open API头文件 #include <NXOpen/NXOpenAPI.hxx> #include <NXOpen/Body.hxx> #include <NXOpen/BodyCollection.hxx> #include <NXOpen/DisplayManager.hxx> #include <NXOpen/Part.hxx> #include <NXOpen/PartCollection.hxx> #include <NXOpen/Point.hxx> #include <NXOpen/Point3d.hxx> #include <NXOpen/Vector.hxx> #include <NXOpen/Unit.hxx> // C++头文件 #include <vector> #include <algorithm> using namespace NXOpen; // 计算最小包容盒主函数 void computeOBB(Body* body, Point3d& center, Vector& direction, double& length, double& width, double& height) { // 获取体积信息 double volume, cgx, cgy, cgz, ixx, iyy, izz, ixy, ixz, iyz; body->ComputeVolumeProperties(volume, cgx, cgy, cgz, ixx, iyy, izz, ixy, ixz, iyz); // 计算质心 center = Point3d(cgx, cgy, cgz); // 计算惯性主轴 Matrix3x3 inertiaMatrix(ixx, ixy, ixz, ixy, iyy, iyz, ixz, iyz, izz); std::vector<double> eigenvalues; std::vector<Vector> eigenvectors; inertiaMatrix.ComputeEigenvaluesAndEigenvectors(eigenvalues, eigenvectors); direction = eigenvectors[0]; // 计算长度、宽度和高度 std::vector<Point3d> points; body->AskPoints(points); std::vector<double> projections; for (std::vector<Point3d>::const_iterator iter = points.begin(); iter != points.end(); ++iter) { Vector v = Vector(center, *iter); projections.push_back(v.Dot(direction)); } std::sort(projections.begin(), projections.end()); length = projections.back() - projections.front(); width = 0.0; height = 0.0; for (std::vector<Point3d>::const_iterator iter = points.begin(); iter != points.end(); ++iter) { Vector v = Vector(center, *iter); double projection = v.Dot(direction); Vector projectionVector = direction * projection; Vector orthogonalVector = v - projectionVector; width += orthogonalVector.Magnitude(); height += projectionVector.Magnitude(); } width /= static_cast<double>(points.size()); height /= static_cast<double>(points.size()); } // 主函数 int main() { // 打开当前工作部件 Part* part = dynamic_cast<Part*>(Session::GetSession()->Parts()->Work()); if (!part) { std::cerr << "No active part found." << std::endl; return 1; } // 获取当前选择的实体 Selection::Instance()->SetPickFilterMaskBits(Selection::FilterObjectBody); Selection::Instance()->SetSelectionLimits(1, 1); Selection::Instance()->Clear(); int response = Selection::Instance()->SelectObjects("Select a body:"); if (response != UF_UI_SEL_SUCCESS) { std::cerr << "Selection failed." << std::endl; return 1; } std::vector<TaggedObject*> selectedObjects = Selection::Instance()->GetSelectedObjects(); if (selectedObjects.empty()) { std::cerr << "No object selected." << std::endl; return 1; } Body* body = dynamic_cast<Body*>(selectedObjects[0]); if (!body) { std::cerr << "Selected object is not a body." << std::endl; return 1; } // 计算最小包容盒 Point3d center; Vector direction; double length, width, height; computeOBB(body, center, direction, length, width, height); // 输出结果 std::cout << "Center: (" << center.X() << ", " << center.Y() << ", " << center.Z() << ")" << std::endl; std::cout << "Direction: (" << direction.X() << ", " << direction.Y() << ", " << direction.Z() << ")" << std::endl; std::cout << "Length: " << length << std::endl; std::cout << "Width: " << width << std::endl; std::cout << "Height: " << height << std::endl; // 显示结果 DisplayManager* dm = part->DisplayManager(); Marker* centerMarker = dm->CreateMarker(center, Marker::MarkerTypeDisc, 5.0, Color::Orange, "Center", true, false); Arrow* directionArrow = dm->CreateArrow(center, center + direction * length * 0.5, Arrow::ArrowheadStyleFilled, 2.0, 0.5, Color::Blue, "Direction", true, false); Box* box = dm->CreateBox(center, direction, length, width, height, Color::Green, "OBB", true, false); return 0; } ``` 这个示例代码中,OBB算法的主要计算过程在`computeOBB`函数中实现。该函数以一个`Body`指针作为输入参数,计算出该实体的最小包容盒的中心点、方向、长度、宽度和高度,并将这些结果保存在输出参数中。主函数中,我们通过用户交互选择一个实体,然后调用`computeOBB`函数计算最小包容盒,最后输出结果并在图形窗口中显示最小包容盒。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值