对同一高度的体着色

 1 #include <uf.h>
 2 #include <uf_ui.h>
 3 #include <uf_obj.h>
 4 #include <uf_modl_primitives.h>
 5 #include <uf_csys.h>
 6 #include <uf_layer.h>
 7 #include <uf_object_types.h>
 8 #include <uf_modl_utilities.h>
 9 #include <uf_part.h>
10 #include <uf_modl.h>
11 #include <uf_group.h>
12 #include <uf_ui.h>
13 #include <uf_layer.h>
14 #include <uf_obj.h>
15 #include <uf_object_types.h>
16 #include <uf_defs.h>
17 #include <uf_evalsf.h>
18 #include <vector>
19 #include <algorithm>
20 #include <NXOpen/ugmath.hxx>
21 #include <NXOpen/Body.hxx>
22 #include <NXOpen/NXObjectManager.hxx>
23 #include <NXOpen/Face.hxx>
24 using namespace std;
25 using namespace NXOpen;

vector<int> color_num;
vector<double> heigth;
vector<tag_t> face1;
vector<const string> info;
int sss[20] = { 11,18,32,69,71,78,87,90,93,103,108,136,141,145,160,163,176,190,191,205 };
color_num.insert(color_num.begin(), sss, sss + 20);
sort(color_num.begin(), color_num.end());//排序
//color_num.erase(unique(color_num.begin(), color_num.end()), color_num.end());//去重


UF_initialize();


//方法1:UF_OBJ_cycle_objs_in_part
std::vector<tag_t> SolidVector;
tag_t ObjectTag = NULL_TAG;
int Type, SubType, Body_Type;
UF_OBJ_disp_props_t disp_props;
UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_solid_type, &ObjectTag);
while (ObjectTag != NULL_TAG)
{
UF_OBJ_ask_display_properties(ObjectTag, &disp_props);
UF_OBJ_ask_type_and_subtype(ObjectTag, &Type, &SubType);
if (SubType == UF_solid_body_subtype && disp_props.blank_status == UF_OBJ_NOT_BLANKED)
{
UF_MODL_ask_body_type(ObjectTag, &Body_Type);
if (Body_Type == UF_MODL_SOLID_BODY)
{
SolidVector.push_back(ObjectTag);
}
}
UF_OBJ_cycle_objs_in_part(UF_PART_ask_display_part(), UF_solid_type, &ObjectTag);
}//获取体的数量
for (int i = 0; i < SolidVector.size(); i++)
{


Body *body1(dynamic_cast<Body *>(NXObjectManager::Get(SolidVector[i])));
std::vector<Face*> face = body1->GetFaces();//通过体获得所有面
double z = -10000;
double z1 = 10000;
tag_t highest = NULL;//最高面TAG
for (int i = 0; i < face.size(); i++)//循环找到所有的面
{
//UF_DISP_set_highlight(face[i]->Tag(), 0);
UF_EVALSF_p_t evaluator = NULL;
double uv_min_max[4] = { 0.0, 1.0, 0.0, 1.0 };
UF_EVALSF_initialize_2(face[i]->Tag(), &evaluator);//初始化一个面评估器结构
UF_EVALSF_ask_face_uv_minmax(evaluator, uv_min_max);//计算u,v参数空间一个面的最小值、最大值
double uv_pair[2] = { 0.5 * (uv_min_max[0] + uv_min_max[1]), 0.5 * (uv_min_max[2] + uv_min_max[3]) };
UF_MODL_SRF_VALUE_t surf_eval;
UF_EVALSF_evaluate(evaluator, UF_MODL_EVAL_ALL, uv_pair, &surf_eval);
Point3d origin(surf_eval.srf_pos[0], surf_eval.srf_pos[1], surf_eval.srf_pos[2]);//得到UV0.5的XYZ点坐标
Vector3d vector1(surf_eval.srf_unormal[0], surf_eval.srf_unormal[1], surf_eval.srf_unormal[2]);//得到UV面的向量
UF_EVALSF_free(&evaluator);
if (z > surf_eval.srf_pos[2])//判断当Z值大于UV点Z值的时候
{
z = z;
}
else//当Z值小于UV点Z值的时候
{
z = surf_eval.srf_pos[2];//Z就等于UV点Z最大值
highest = face[i]->Tag();//找到这个面
}
}
//设置面高亮显示
//UF_DISP_set_highlight(highest, 0);
face1.push_back(highest);
}



//获取高度数据
for (int i = 0; i < face1.size(); i++)
{
int Type;
double Point[3];
double Dir[3];
double Box[6];
double Radius[3];
double RadData[3];
int NormDir;
int aaa = UF_MODL_ask_face_data(face1[i], &Type, Point, Dir, Box, Radius, RadData, &NormDir);
double zzz= floor(Box[5] * 10000.0f + 0.5) / 10000.0f;


heigth.push_back(zzz);
}


sort(heigth.begin(), heigth.end());//排序
heigth.erase(unique(heigth.begin(), heigth.end()), heigth.end());//去重
if (heigth.size() < 21)
{
//获取面数据
for (int j = 0; j < face1.size(); j++)
{
int Type;
double Point[3];
double Dir[3];
double Box[6];
double Radius[3];
double RadData[3];
int NormDir;


UF_MODL_ask_face_data(face1[j], &Type, Point, Dir, Box, Radius, RadData, &NormDir);
double zzz1 = floor(Box[5] * 10000.0f + 0.5) / 10000.0f;
tag_t facess = NULL_TAG;
UF_MODL_ask_face_body(face1[j], &facess);
vector<double>::iterator it = find(heigth.begin(), heigth.end(), zzz1);
int res = it - heigth.begin();


UF_OBJ_set_color(facess, color_num[res]);
//输出信息
UF_UI_open_listing_window();
char msg[256];
sprintf_s(msg, "色号%d=%.2f\n", color_num[res], zzz1);
info.push_back(msg);
}
}
else
{
UF_UI_open_listing_window();
//char msg[256];
//sprintf_s(msg, "%d,", res);
UF_UI_write_listing_window("高度个数超过20个,请重新排版!");
}
UF_UI_open_listing_window();
UF_terminate();


sort(info.begin(), info.end());//排序
info.erase(unique(info.begin(), info.end()), info.end());//去重
for (int k=0;k<info.size();k++)
{
char HH[256];
sprintf(HH, "%d,%s",k+1,info[k].c_str());
UF_UI_write_listing_window(HH);
}

 

参考文章https://www.cnblogs.com/nxopen2018/p/10957425.html

posted @ 2020-03-04 22:22  老婆饼里有老婆  阅读(245)  评论(0编辑  收藏  举报