明细

int CeShi::apply_cb()
{
int errorCode = 0;
try
{
//---- Enter your callback code here -----
UF_initialize();
Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());

char msg[99];
int num_views1 = 0, num_views2 = 0;
tag_t drawing_tag NULL_TAG;
tag_t drawing_view = NULL_TAG;

UF_DRAW_ask_current_drawing(&drawing_tag);//获得当前视图的tag
//UF_DRAW_ask_num_views(drawing_tag, &num_views1 );//获得视图数量
tag_p_t view_tag1;
UF_DRAW_ask_views( drawing_tag, &num_views2, &view_tag1);//获得视图数量,可以用UF_DRAW_ask_views( drawing_tag, &num_views2, NULL);不要得到的视图Tag

///UF_DRAW_ask_view_of_drawing(drawing_tag, &drawing_view);///这个好像不能用,不是单个视图的tag,也许是3D或2D的。也可能是本图纸的。鬼知道。

std::vector<tag_t> layerVis_Num;////定义一个存放可见视图图层的Tag存储数组。
std::vector<tag_t> solidBody_Num;//////定义一个存放可见图层内实体Tag的存储数组
layerVis_Num.clear();
solidBody_Num.clear();

for (int i=0;i<num_views2;i++)
{
int transitionViewTag=view_tag1[i];////操,为什么这个版本非要单独弄一个过渡名称transition。获得某个视图的tag
std::vector<Layer::StateInfo> stateArray1(256); ///定义一个<Layer::StateInfo>型的动态数组(std::vector),名为stateArray1
//Drawings::BaseView *baseView1(dynamic_cast<Drawings::BaseView *>(workPart->DraftingViews()->FindObject("TOP@1")));
//Drawings::BaseView *baseView1(dynamic_cast<Drawings::BaseView *>(NXObjectManager::Get(transitionViewTag))); ////将Tag转为NX对象
NXOpen::View *allView(dynamic_cast<NXOpen::View *>(NXObjectManager::Get(transitionViewTag))); //此处用NXOpen::View,而不用Drawings::BaseView,因为NX中还有投影,剖视,细节等其他类型。无法通用,所以这里用NXopen::View
/*
//stateArray1[0] = Layer::StateInfo(1, Layer::StateVisible);
//stateArray1[1] = Layer::StateInfo(2, Layer::StateVisible);
//stateArray1[2] = Layer::StateInfo(3, Layer::StateVisible);
//workPart->Layers()->SetObjectsVisibilityOnLayer(baseView1, stateArray1, true);
//sprintf(msg, "视图tag: %d", baseView1->Tag());
*/
workPart->Layers()->GetVisibilitiesInView(allView,stateArray1);/////获得256个图层的可见性了",1);

//int layerX = stateArray1[4].Layer; ///这个是图层的序号
//int layerS = stateArray1[4].State; //State { StateWorkLayer=0, StateSelectable=1, StateVisible=2, StateHidden=3 }

//std::vector<tag_t> layerVis_Num;////定义一个存放可见视图图层的Tag存储数组。在for外部定义
//std::vector<tag_t> solidBody_Num;//////定义一个存放可见图层内实体Tag的存储数组。在for外部定义

int layerS;///定义图层状态
for (int i=0;i<256;i++)
{
layerS = stateArray1[i].State;///获得图层状态
;
if(layerS == 2)
{
if(std::find(layerVis_Num.begin(), layerVis_Num.end(), i+1) == layerVis_Num.end()) //判断这个图层在不在已经有的“实体导图层”数组中,视图可见图层中。如果没有则加入新的图层
{
layerVis_Num.push_back(i+1);///这里要去重

}

}

}


/*这具操作不实用。用图层循环,找的烂东西太多。
tag_t object_tag = NULL_TAG;
UF_LAYER_cycle_by_layer(layerVis_Num[1],&object_tag);
int type;
int subtype;
UF_OBJ_ask_type_and_subtype(object_tag, &type,&subtype);
int j=0;
while(object_tag!=NULL_TAG)
{
j++;
UF_LAYER_cycle_by_layer(layerVis_Num[1],&object_tag);
}
*/
}
UF_free(view_tag1);

 

///////////////////////////////////////////////遍历体
//theSession->ListingWindow()->Open();

BodyCollection *bodys = workPart->Bodies();

for (BodyCollection::iterator ite = bodys->begin();ite !=bodys->end();ite++)
{
Body *body=(*ite);

/*
int type;
int subtype;
UF_OBJ_ask_type_and_subtype(body->Tag(), &type,&subtype);///既然体的过滤都是体,就不用这个功能了。只需要区分是实体还是片本即可。
if(type==70&subtype==0) ///这个功能有点扯淡,实体,片体,全是70和0.根本分不出两种,只能用:UF_MODL_ask_body_type();
{
solidBody_Num.push_back(body->Tag());
sprintf ( msg , "tag:%d,实体type:%d ,实体subtype:%d",body->Tag(),type,subtype);
theSession->ListingWindow()->WriteLine( msg);

}
*/
int body_type;
UF_MODL_ask_body_type(body->Tag(),&body_type); ////输出结果 1,5601=UF_MODL_SOLID_BODY, 2,5602=UF_MODL_SHEET_BODY, 3,0 ( 以上都不是)
if(body_type==5601) //如果等于5601,即为实体。则判断其是不是位于当前图纸可见图层中,如是,则push_back
{

UF_OBJ_disp_props_s dispprops; ///这是不是叫做联合结构体的定义,总之,dispprops下有好多属性,如颜色、图层、blank_status等
UF_OBJ_ask_display_properties(body->Tag(), &dispprops); //获取对象的显示属性(图层, 颜色, 隐藏状态, 线宽, 字体, 高亮状态)
//sprintf ( msg , "图层:%d ",dispprops.layer);
//uc1601(msg,1);
if (std::find(layerVis_Num.begin(), layerVis_Num.end(), dispprops.layer) != layerVis_Num.end()) //判断这个图层是不是在,视图可见图层中。如果在则:
{
if(std::find(solidBody_Num.begin(), solidBody_Num.end(), dispprops.layer) == solidBody_Num.end()) //判断这个图层在不在已经有的“实体导图层”数组中,视图可见图层中。如果没有则加入新的图层
{
solidBody_Num.push_back(body->Tag());
//solidBody_Num.push_back(dispprops.layer);


}
}

}
}

 

//加入明细表测试
//const char *templaName1="D:\\zk_metric.prt";/////"D:\\Program Files\\Siemens\\NX 8.0\\UGII\\table_files\\zk_metric.prt";
double origin[3]={220,110,0};
tag_t parts_list1;
UF_PLIST_create_from_template("D:\\CeShi.prt",origin,&parts_list1);///也许const char*只能是固定字符
//加入实体

logical add_components = false;
//tag_t object
for(int i=0;i<solidBody_Num.size();i++)
{
UF_PLIST_add_object(parts_list1,add_components,solidBody_Num[i]);

}
UF_PLIST_update_all_plists();
//uc1601(msg,1);


//sprintf ( msg , "实体数量:%d",solidBody_Num.size());
//theSession->ListingWindow()->WriteLine( msg);
/*这段是测试如何用std:find的
std::vector <int> vec ;
vec.push_back(1);
vec.push_back(2);

int valueToFind = 4;
if (std::find(vec.begin(), vec.end(), valueToFind) != vec.end())
{

uc1601("我操,在这",1);
}
else
{
uc1601("没找着",1);
}
*/
// sprintf(msg,"%d",type);///这就得到了在制图中某个图层的可见与否。
//sprintf(msg,"%d",j);///这就得到了在制图中某个图层的可见与否。
//uc1601(msg,1);

/*

/////设置在制图中可见
Session *theSession = Session::GetSession();
Part *workPart(theSession->Parts()->Work());
Part *displayPart(theSession->Parts()->Display());
Drawings::BaseView *baseView1(dynamic_cast<Drawings::BaseView *>(workPart->DraftingViews()->FindObject("TOP@1")));
std::vector<Layer::StateInfo> stateArray1(256);
stateArray1[0] = Layer::StateInfo(1, Layer::StateVisible);
stateArray1[1] = Layer::StateInfo(2, Layer::StateVisible);
stateArray1[2] = Layer::StateInfo(3, Layer::StateVisible);
//stateArray1[4] = Layer::StateInfo(5, Layer::StateVisible);

workPart->Layers()->SetObjectsVisibilityOnLayer(baseView1, stateArray1, true);
////////////////////
std::vector<Layer::StateInfo > stateArray2(256) ;

workPart->Layers()->GetVisibilitiesInView(workPart->DraftingViews()->FindObject("TOP@1"),stateArray2 );///Gets the visibility of all layers in a specified view.获得视图中图层的可见性
std::vector<Edge *> stass;
int layerX = stateArray2[9].Layer;
int layerS = stateArray2[4].State; //State { StateWorkLayer=0, StateSelectable=1, StateVisible=0, StateHidden=3 }
sprintf(msg,"%d",layerS);///这就得到了在制图中某个图层的可见与否。
/////测试属性
tag_t attrTag =NULL_TAG;
UF_ATTR_ask_part_attribute(&attrTag);
UF_ATTR_value_t value1;
value1.type = UF_ATTR_string;
value1.value.string = "a55bc";
UF_ATTR_assign(attrTag,"字符",value1);

/////测试表达式
//tag_t num1; //数量1
//UF_MODL_create_exp_tag("num_1=1",&num1);
//UF_MODL_edit_exp("num_1=110");
//UF_MODL_delete_exp("num_1");

 

///测试对象所在的图层
Session *theSession = Session::GetSession();
//Part *workPart(theSession->Parts()->Work());
Part* workPart;//定义工作部件
workPart = theSession->Parts()->Work();//获取工作部件
theSession->ListingWindow()->Open();

std::vector<int> BodyTagVector;//对象TAG容器
BodyTagVector.clear();

//遍历工作部件所有体的显示属性
BodyCollection* bodys = workPart->Bodies();
for(BodyCollection::iterator ite = bodys->begin(); ite != bodys->end(); ite++)
{
Body* bodys = (*ite);
BodyTagVector.push_back(bodys->Tag());

//获得对象信息
UF_OBJ_disp_props_s dispprops;
UF_OBJ_ask_display_properties(bodys->Tag(), &dispprops); //获取对象的显示属性(图层, 颜色, 隐藏状态, 线宽, 字体, 高亮状态)
sprintf(msg, "体的TAG: %d", bodys->Tag());
theSession->ListingWindow()->WriteLine(msg);
sprintf(msg, "体的图层:%d", dispprops.layer);
theSession->ListingWindow()->WriteLine(msg);
sprintf(msg, "体的颜色:%d", dispprops.color);
theSession->ListingWindow()->WriteLine(msg);
sprintf(msg, "体的隐藏状态:%d", dispprops.blank_status);
theSession->ListingWindow()->WriteLine(msg);
}
*/
UF_terminate();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
CeShi::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}

posted @ 2023-12-22 16:20  光量子  阅读(58)  评论(0编辑  收藏  举报