自动布线

Session *(ZiDongWanTou::theSession) = NULL;
UI *(ZiDongWanTou::theUI) = NULL;
char msg[100];
//定义循环曲线
std::vector<tag_t> lineNum;
std::vector<Line*> vecLine;
Line *line1=NULL;
Line *line2=NULL;
Line *line7;
Line *line8;

std::vector<Face*> vecFaceA; //FaceA数组
std::vector<Face*> vecFaceB; //FaceB数组
std::vector<Assemblies::Component*> compNum; //组件序号
std::vector<Assemblies::Component*> compZGNum; //组件序号
std::vector<tag_t> compFaceA_TagNum; //FaceA tag
std::vector<tag_t> compFaceB_TagNum; //FaceB tag
Assemblies::Component *component1;
Assemblies::Component *component2;
std::vector<double> disVector; //直管长度

tag_t num2; //数量2 为阵列做准备
tag_t dis2; //距离2 为阵列做准备

//////////////原弯头创建的参数
// 定义一些全局变量(ZG是当前最高函数体,下边的show() apply()为其子函数
//------------------------------------------------------------------------------
char * envPath;
char oldPartName[222],newPartName[222];
char newZGName[222]; //直管名称
NXString workPath;
string basePath;
double d1,ang,s1,s2,r1,r2,R,fb;
double pn;
double xh1;//零件序号,用于表达式中
bool fb1;
char pn_f[100],pn_e[100],xh1_f[100];
char d1_f[100],ang_f[100],s1_f[100],s2_f[100],r1_f[100],r2_f[100],R_f[100],fb_f[100],ht1_f[100],fb1_f[100],zght1_f[100],l1_f[100];
char d1_e[100],ang_e[100],s1_e[100],s2_e[100],r1_e[100],r2_e[100],R_e[100],fb_e[100],fb1_e[100],xh1_e[100],zght1_e[100],l1_e[100];
string ht1_s,xh1_s;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

int ZiDongWanTou::apply_cb()
{

int errorCode = 0;
try
{
//---- Enter your callback code here -----+
////////1,初始化//////////////////////////////
UF_initialize();
char msg[100];
Session *theSession = Session::GetSession();
Part *displayPart(theSession->Parts()->Display());
Part *workPart(theSession->Parts()->Work());
tag_t parentPart = UF_PART_ask_display_part();

////////2,循环曲线,共多少个夹角//////////////////////////////

cycleLine();

////////4,由循环到的夹角数量,“动态” 定义要装配的组件数量//////////////////////////////

//////////添加组件

///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义
//std::vector<Assemblies::Component*> compNum;
for(int i=0;i<lineNum.size()-1;i++)
{

Assemblies::Component *component0;
compNum.push_back(component1);

}
///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义*/

for(int i=0;i<lineNum.size()-1;i++)
{

line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针
line2 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i+1])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针

//添加组件
Point3d basePoint1(0.0, 0.0, 0.0);
Matrix3x3 orientation1;

Point3d p11(0.0, 0.0, 0.0);
Point3d p12(0.0, 0.0, 0.0);
Point3d p21(0.0, 0.0, 0.0);
Point3d p22(0.0, 0.0, 0.0);
p11 = line1->StartPoint();
p12 = line1->EndPoint();
p21 = line2->StartPoint();
p22 = line2->EndPoint();

basePoint1 = p12;
orientation1.Xx = p12.X-p11.X;
orientation1.Xy = p12.Y-p11.Y;
orientation1.Xz = p12.Z-p11.Z;
orientation1.Yx = p21.X-p22.X;
orientation1.Yy = p21.Y-p22.Y;
orientation1.Yz = p21.Z-p22.Z;
orientation1.Zx = 0.0;
orientation1.Zy = 0.0;
orientation1.Zz = 0.0;
PartLoadStatus *partLoadStatus1;

/////////////////////测量两线夹角//////////////////
NXObject *nullNXObject(NULL);
MeasureAngleBuilder *measureAngleBuilder1;
measureAngleBuilder1 = workPart->MeasureManager()->CreateMeasureAngleBuilder(nullNXObject);

measureAngleBuilder1->Object1()->SetValue(line1);
measureAngleBuilder1->Object2()->SetValue(line2);
Unit *nullUnit(NULL);
MeasureAngle *measureAngle1;
measureAngle1 = workPart->MeasureManager()->NewAngle(nullUnit, line1, MeasureManager::EndpointTypeStartPoint, line2, MeasureManager::EndpointTypeEndPoint, true, false);

//Measure *measure1;
//measure1 = measureAngle1->CreateFeature(); //只需得到值不创建

ang = measureAngle1->Value();//获取测量的值
delete measureAngle1;
measureAngleBuilder1->Destroy();

if(ang>90)
{
ang = 180-ang;
}
/*sprintf(msg,"%g",ang);
uc1601(msg,1);*/
//////////////////测量两线夹角结束///////////////////////////


//////////////////////调用创建弯头程序//////////////////////////
zdy_step1(); //打开库零件,并重命名
//编辑表达式
FILE *mmfp;
mmfp = fopen(newPartName,"r");
if(mmfp != NULL)
{
goto pos_1;
}

zdy_step2(); //表达式的准备工作

//xh1_s = btxh1->GetProperties()->GetString("Value").GetLocaleText(); //序号的转换
//sprintf(xh1_f,"\"%s\"",xh1_s.c_str());//字符串转char,这里加不加c_str()都可以
/* sprintf(xh1_e,"\"xh1=%s\"",xh1_s);*/////废句子

//Session *theSession = Session::GetSession(); //成功的代码
workPart = theSession->Parts()->Work();
displayPart = theSession->Parts()->Display();

 

Expression *expression1(dynamic_cast<Expression *>(workPart->Expressions()->FindObject("xh1")));
Unit *nullUnit1(NULL);
workPart->Expressions()->EditWithUnits(expression1, nullUnit1, xh1_f);
////应用表达式
UF_MODL_edit_exp(d1_e);
UF_MODL_edit_exp(ang_e);
UF_MODL_edit_exp(s1_e);
UF_MODL_edit_exp(s2_e);
UF_MODL_edit_exp(r1_e);
UF_MODL_edit_exp(r2_e);
UF_MODL_edit_exp(fb_e);
UF_MODL_edit_exp(pn_e);
UF_MODL_edit_exp(xh1_e);


fb1 = btfb1->GetProperties()->GetLogical("Value");//如果非标弯头选项勾选,则执行以下指令
if(fb1==1)
{
UF_MODL_edit_exp(R_e);
//char msg[111];
//sprintf(msg,"%s",R_e);
//uc1601(R_e,1);
}

UF_MODL_update();

ziLj();

UF_PART_save();/////////保在零件//////////////////

UF_PART_close(UF_PART_ask_display_part(),1,1);

UF_PART_set_display_part(parentPart);
workPart = theSession->Parts()->Work();
displayPart = theSession->Parts()->Display();
////////////弯头程序结束/////////////////////////////////////////
/*///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义
std::vector<Assemblies::Component*> compNum;
Assemblies::Component *component1;
compNum.push_back(component1);
///自悟新方法,原来是*component1,现在定义为std::vector,成一数组,可以在i不确定的情况下动态定义*/

//Assemblies::Component *component1;////原函数

////////////////////////////////////////goto语句跳转位置////////////////////////////////////////
pos_1:
////////////////////////////////////////goto语句跳转位置////////////////////////////////////////
const char * fspec1=newPartName;
int ftype1=2; //.prt格式
char occ_name1[100];
uc4574(fspec1,ftype1,occ_name1); ///分割路径为目录和文件名uc4576 ,char dspec1[100],fname1[100];
stringstream tn1;
tn1<<occ_name1;
string tn2=tn1.str();

compNum[i] = workPart->ComponentAssembly()->AddComponent(newPartName, "model", tn2, basePoint1, orientation1, -1, &partLoadStatus1); //此处tn2本是NX string类型。这里有了string好像也行。
delete partLoadStatus1;

component1 = compNum[i];
assemPart();
FaceAppend(i);

//tag_t part_tag;
//tag_t occ_tag;
//occ_tag = component1[0]->Tag(); //但是此处只是一个occ Tag.不是原零件的tag
//part_tag = UF_ASSEM_ask_prototype_of_occ(occ_tag);
//char msg[111];
//
///////获取选择对象的进行动态转换(这个是成功的)
///*Assemblies::Component *component1(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject("COMPONENT WT57-10H0-001 1")));
//std::vector<TaggedObject*>objects1 = this->selection0->GetProperties()->GetTaggedObjectVector("SelectedObjects");
//Line *line7=NULL;
//line7 = dynamic_cast<Line *>(objects1[0]);
//theSession->ListingWindow()->Open();
//sprintf(msg,"数量tag:%d",line7->Tag());
//theSession->ListingWindow()->WriteLine(msg);*/
//////////////
////Assemblies::Component *component1(dynamic_cast<Assemblies::Component *>(workPart->ComponentAssembly()->RootComponent()->FindObject("COMPONENT WT57-10H0-001 1")));

//tag_t feature= NULL_TAG ;
//tag_t feature1= NULL_TAG ;
//std::vector<tag_t> line7Num;
//std::vector<tag_t> line8Num;
//int type = UF_line_type; //可以查询line, sketch,其他的edge,feature,solid摸不清规律
//const char * name7= "LINE7" ; ///一条直线也分特征名和(对象)名称,注意此处一定是对象名
//UF_OBJ_cycle_by_name_and_type(parentPart,name7,type, TRUE, &feature);
//

////while ( feature != NULL_TAG )
////{
//// line7Num.push_back(feature);
//// UF_OBJ_cycle_by_name_and_type(part_tag, name7,type, TRUE, &feature);//此功能和UF_OBJ_cycle_by_name一样,主要还是遍历特征树这内的内容。而不会遍历边 体等不在树中的对象。否则请用UF_OBJ_cycle_by_by_name_and_type
////
////}
///*theSession->ListingWindow()->Open();
//sprintf(msg,"数量tag:%d",line7Num[i]);
//theSession->ListingWindow()->WriteLine(msg);*/
//
//Line *line7 = dynamic_cast<Line*>(NXObjectManager::Get(feature));
//

//const char * name8= "LINE8" ; ///一条直线也分特征名和(对象)名称,注意此处一定是对象名
//feature= NULL_TAG;
//UF_OBJ_cycle_by_name_and_type(parentPart,name8,type, TRUE, &feature);
////while ( feature != NULL_TAG )
////{
//// line8Num.push_back(feature);
//// UF_OBJ_cycle_by_name_and_type(part_tag, name8,type, TRUE, &feature);//此功能和UF_OBJ_cycle_by_name一样,主要还是遍历特征树这内的内容。而不会遍历边 体等不在树中的对象。否则请用UF_OBJ_cycle_by_by_name_and_type

////}

//Line *line8 = dynamic_cast<Line*>(NXObjectManager::Get(feature));
}

MeasueDisance(vecFaceA,vecFaceB);
ZhiGuan();
UF_terminate();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ZiDongWanTou::cycleLine()

{

 

Session *theSession = Session::GetSession();

Part *displayPart(theSession->Parts()->Display());

Part *workPart(theSession->Parts()->Work());

/////链先取方式

std::vector<TaggedObject*>objects1 = this->edge_select0->GetProperties()->GetTaggedObjectVector("SelectedObjects");

line1 = dynamic_cast<Line*>(objects1[0]);

lineNum.push_back(line1->Tag());//这个是Line的tag

vecLine.push_back(line1);//这个是Line的obj

 

/*Point3d  p11(0.0, 0.0, 0.0);*/

Point3d  p12(0.0, 0.0, 0.0);

Point3d  p21(0.0, 0.0, 0.0);

//Point3d  p22(0.0, 0.0, 0.0);

for(int i=0;i<objects1.size()-1;i++)

{

for(int j=0;i<objects1.size()-1;j++)

{

line2 = dynamic_cast<Line*>(objects1[j]);

 

p12=line1->EndPoint();

p21=line2->StartPoint();  ////p12=p21

 

if((p12.X==p21.X)&&(p12.Y==p21.Y)&&(p12.Z==p21.Z))

{

lineNum.push_back(line2->Tag());

vecLine.push_back(line2);

line1 = line2;

 

break;

 

}

}

 

}

 

/////链先取方式/////链先取方式

 

 

////以下为全部遍历方式

//Features::Feature *fcList;

//std::vector<NXObject *> entitiesArray; //std::vector是一种数组定义方式

//Features::FeatureCollection *fc=workPart->Features();//迭代

//Features::FeatureCollection::iterator itr=fc->begin();//获得第一个特征

 

 

//for (;itr!=fc->end();++itr) //循环获得所有特征

//{

//fcList=(*itr); //获得特征赋值给fcList

//NXString featType = NULL;

//featType = fcList->FeatureType();

 

///*theSession->ListingWindow()->WriteLine(fcList->FeatureType());*/

//if(!strcmp(featType.GetLocaleText(),"LINE")) 

//{

////lineNum.push_back(fcList->Tag());

 

//entitiesArray = fcList->GetEntities(); //GetEntities方法,实体下0对象,Lineg下1对对象,基准下7对象,点集下对象最多

//lineNum.push_back(entitiesArray[0]->Tag());

 

//}

 

//}

//以下为全部遍历方式

 

//line1 =dynamic_cast<Line *>(entitiesArray[0]);

 

/*for(int i=0;i<lineNum.size();i++)

//{

////NXObjectManager::Get(lineNum[i]);//通过tag得到 TaggedObject 对象指针. 

////line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针

}*/

 

/*line1 = dynamic_cast<Line*>(entitiesArray[0]);

line2 = dynamic_cast<Line*>(entitiesArray[0]); */

//line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[0])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针

//line2 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[1])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针

//以下为全部遍历方式//以下为全部遍历方式////以下为全部遍历方式

}

 

void ZiDongWanTou::assemPart()

{

 

Session *theSession = Session::GetSession();

Part *workPart(theSession->Parts()->Work());

Part *displayPart(theSession->Parts()->Display());

tag_t parentPart = UF_PART_ask_display_part();

 

/*借鉴一下

disp_part_tag=UF_PART_ask_display_part ( ); //得到当前部件文件标识

UF_OBJ_cycle_by_name_and_type (disp_part_tag,obj_name,type,use_occ,&next_tag);

while(next_tag!=NULL_TAG )

{

num++; 

UF_OBJ_set_color(next_tag,20);    // 对象修改颜色

UF_OBJ_cycle_by_name_and_type (disp_part_tag,"MM",UF_solid_type,use_occ,&next_tag);

}

*/

 

/////////////////获取Line7,Line8

tag_t feature= NULL_TAG ;

int type = UF_line_type;//可以查询line, sketch,其他的edge,feature,solid摸不清规律

const char * name7= "LINE7" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名

UF_OBJ_cycle_by_name_and_type(parentPart,name7,type, TRUE, &feature); 

Line *line7 = dynamic_cast<Line*>(NXObjectManager::Get(feature));  //feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。

 

feature= NULL_TAG;

const char * name8= "LINE8" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名

UF_OBJ_cycle_by_name_and_type(parentPart,name8,type, TRUE, &feature); 

Line *line8 = dynamic_cast<Line*>(NXObjectManager::Get(feature));//feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。

///////////////////////////////////////

 

Positioning::ComponentPositioner *componentPositioner1;

componentPositioner1 = workPart->ComponentAssembly()->Positioner();

 

componentPositioner1->ClearNetwork();

componentPositioner1->BeginAssemblyConstraints();

 

Positioning::Network *network1;

network1 = componentPositioner1->EstablishNetwork();

 

Positioning::ComponentNetwork *componentNetwork1(dynamic_cast<Positioning::ComponentNetwork *>(network1));

componentNetwork1->SetMoveObjectsState(true);

 

Assemblies::Component *nullAssemblies_Component(NULL);

componentNetwork1->SetDisplayComponent(nullAssemblies_Component);

 

//第一组

Positioning::Constraint *constraint1;

constraint1 = componentPositioner1->CreateConstraint();

 

Positioning::ComponentConstraint *componentConstraint1(dynamic_cast<Positioning::ComponentConstraint *>(constraint1));

componentConstraint1->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);

componentConstraint1->SetConstraintType(Positioning::Constraint::TypeTouch);

/////定义约束对象

/////获取选择对象的进行动态转换(这个是成功的)

/*std::vector<TaggedObject*>objects1 = this->selection01->GetProperties()->GetTaggedObjectVector("SelectedObjects");

Line *line3=NULL;

line3 = dynamic_cast<Line *>(objects1[0]); */

 

////装配进来的件就不用选择了

Positioning::ConstraintReference *constraintReference1;

constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line7, false, false, false);

 

/*////获取选择对象的进行动态转换(这个是成功的)

std::vector<TaggedObject*>objects2 = this->edge_select01->GetProperties()->GetTaggedObjectVector("SelectedObjects");

//objects2[0]->Tag();

Line *line4=NULL;

line4 = dynamic_cast<Line *>(objects2[0]);*/

 

 

///定义约束对象

/*Positioning::ConstraintReference *constraintReference1;

constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line3, false, false, false);*/

 

Positioning::ConstraintReference *constraintReference2;

constraintReference2 = componentConstraint1->CreateConstraintReference(workPart->ComponentAssembly(), line1, false, false, false);

 

constraintReference2->SetFixHint(true);

 

componentNetwork1->Solve();

//第二组

Positioning::Constraint *constraint2;

constraint2 = componentPositioner1->CreateConstraint();

 

Positioning::ComponentConstraint *componentConstraint2(dynamic_cast<Positioning::ComponentConstraint *>(constraint2));

componentConstraint2->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);

componentConstraint2->SetConstraintType(Positioning::Constraint::TypeTouch);

 

/////获取选择对象的进行动态转换(这个是成功的)

Positioning::ConstraintReference *constraintReference3;

constraintReference3 = componentConstraint2->CreateConstraintReference(component1, line8, false, false, false);

Positioning::ConstraintReference *constraintReference4;

constraintReference4 = componentConstraint2->CreateConstraintReference(workPart->ComponentAssembly(), line2, false, false, false);

 

constraintReference4->SetFixHint(true);

componentNetwork1->Solve();

 

componentPositioner1->ClearNetwork(); 

componentPositioner1->EndAssemblyConstraints(); 

}

 

////////////////以为为弯头创建子程序

void ZiDongWanTou::zdy_step1()//打开库零件,并重命名

{

UF_initialize();

 

d1 = btd1->GetProperties()->GetDouble("Value");

 

//ang = btang->GetProperties()->GetDouble("Value");  //此参数将有程序获得

 

pn = btpn->GetProperties()->GetDouble("Value");//压力

string ht1_ss;

ht1_ss = btht1->GetProperties()->GetEnumAsString("Value").GetLocaleText();

xh1_s = btxh1->GetProperties()->GetString("Value").GetLocaleText();

ht1_s = ht1_ss.substr(0,1);//ht1_s为字符串类型,此处获得活套数量0,1,2

sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用

sprintf(pn_f,"%g",pn);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

sprintf(ht1_f,"%s",ht1_s.c_str());//字符串转char

sprintf(xh1_f,"%s",xh1_s.c_str());//字符串转char

 

 

 

 

//------------------------------------------------------------------------------

//定义  环境变量、原标准件库  路径

 //获取环境变量地址

//------------------------------------------------------------------------------

//path = getenv("UGII_BASE_DIR");

char *  envPath;

UF_translate_variable("UGII_BASE_DIR",&envPath);

string basedir1 =envPath;

string basedir2 ="\\lib\\";

basePath =basedir1+basedir2;

//定义  工作部件   置路径

//workPath = btlibpath->Path();

workPath = btlibpath->GetProperties()->GetString("Path");//获取路径,(开始已经定义为全局变量)此处是NX string类型

//-----------------------------------------------------------------------

//-------------定义新老零件路径名称------------------------

//------------------------------------------------------------------------------

sprintf(oldPartName,"%sWT%s-%sH%s-001.prt",basePath.c_str(),d1_f,pn_f,ht1_f);//此处basePath是sting,所以要转为char*.而char转char*,用sprintf();

sprintf(ang_f,"%g",ang);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

sprintf(s1_f,"%g",s1);

sprintf(newPartName,"%s\\WT%sX%s-%sH%s-%s.prt",workPath.GetLocaleText(),d1_f,ang_f,pn_f,ht1_f,xh1_f);

//打开文件

 

 

//-----------------------------------------------------------------------

UF_terminate();

}

void ZiDongWanTou::zdy_step2()//表达式的准备工作

{

UF_initialize();

/////////////把step1的放这,以便用goto语句

tag_t partTag = NULL_TAG;

UF_PART_load_status_t  error_status ;

UF_PART_open(oldPartName,&partTag,&error_status);//此处需要char*类型,若是string组合的需要s0.c_str()

UF_PART_free_load_status(&error_status);

 

//uc1601(newPartName,1);

 

//重命名文件

UF_PART_rename(partTag,newPartName);

/////////////把step1的放这,以便用goto语句

//定义  以(外径+压力)为基础的 文件名称:ZG-630-10-001

//外径、长度等驱动尺寸

/*ang = btang->GetProperties()->GetDouble("Value");*/

s1 = bts1->GetProperties()->GetDouble("Value");

s2 = bts2->GetProperties()->GetDouble("Value");

r1 = btr1->GetProperties()->GetDouble("Value");

r2 = btr2->GetProperties()->GetDouble("Value");

fb = btfb->GetProperties()->GetDouble("Value");

pn = btpn->GetProperties()->GetDouble("Value");

R = btR->GetProperties()->GetDouble("Value");

 

xh1_s = btxh1->GetProperties()->GetString("Value").GetLocaleText();//序号的转换

sprintf(xh1_f,"\"%s\"",xh1_s.c_str());//字符串转char,这里加不加c_str()都可以

 

sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用

sprintf(ang_f,"%g",ang);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

sprintf(s1_f,"%g",s1);

sprintf(s2_f,"%g",s2);

sprintf(r1_f,"%g",r1);

sprintf(r2_f,"%g",r2);

sprintf(fb_f,"%g",fb);

sprintf(pn_f,"%g",pn);

sprintf(R_f,"%g",R);

 

//--------------------------------------------------------------------

sprintf(d1_e,"d1=%f",d1);//生产直径参数,供生成文件名称用

sprintf(ang_e,"ang=%f",ang);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

sprintf(s1_e,"s1=%f",s1);

sprintf(s2_e,"s2=%f",s2);

sprintf(r1_e,"r1=%f",r1);

sprintf(r2_e,"r2=%f",r2);

sprintf(fb_e,"fb=%f",fb);

sprintf(pn_e,"pn=%f",pn);

sprintf(R_e,"R=%f",R);

 

//派生尺寸

double d2 = d1-2*s1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

 

UF_terminate();

}

void ZiDongWanTou::ziLj()//子零件复制

{

UF_initialize();

tag_t root_part_occ = NULL_TAG;

tag_t displayPart = NULL_TAG;

displayPart = UF_PART_ask_display_part();//显示部件的TAG

root_part_occ = UF_ASSEM_ask_root_part_occ(displayPart);//根部件的事件pat_TAG_OCC

 

  int i;

  int component_count = 0;

  //tag_t parent = NULL_TAG;

  //tag_t member = NULL_TAG;

  tag_t *child_components = NULL;

component_count = UF_ASSEM_ask_part_occ_children(root_part_occ,

                                          &child_components);//Array of tags of child part occurrences.

/* //第一种方法,事件法

tag_t parTag = UF_ASSEM_ask_prototype_of_occ(child_components[0]);//获得part tag 从occurrences tag

char parNm[200];

UF_PART_ask_part_name(parTag, parNm);//从parTag获取ParName,但是这个必需组件加载*/

 

//第二种方法--实例法InTag

for (int i = 0; i < component_count; i++)

{

char ziOldParNme[200];

tag_t parInTag = UF_ASSEM_ask_inst_of_part_occ(child_components[i]);

UF_ASSEM_ask_part_name_of_child(parInTag,ziOldParNme); //这个可以不加载组件

 

stringstream tn1 ;

tn1<<ziOldParNme;

string tn2 = tn1.str();

int star1 = tn2.find_last_of("\\");

int end1 = tn2.find_last_of(".");

string lastName1 = tn2.substr(star1,end1-star1);

 

//workPath = btlibpath->Path();//获取路径

char ziNewPartName [100];

sprintf(ziNewPartName,"%s\\%s.prt",workPath.GetLocaleText(),lastName1.c_str());

uc4567(ziOldParNme,ziNewPartName,UF_CFI_COPY_NEVER_REPLACE,2,2);

/*char msg[111];

sprintf(msg,"%s",ziNewPartName);

uc1601(msg,1);*/

}

UF_free(child_components);

UF_terminate();

}

void ZiDongWanTou::FaceAppend(int i)

{

UF_initialize();

tag_t displayPart = UF_PART_ask_display_part();

tag_t object_occur=NULL_TAG;

 

//ot = UF_ASSEM_cycle_ents_in_part_occ(part_occur,object_occur);

///////////////////////////A面循环////////////////////////////////

//tag_t compFeature= NULL_TAG ;//全局变量

tag_t objectOcc = NULL_TAG;

int type = UF_solid_type;

const char * nameA= "FACE_A" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名

const char * nameB= "FACE_B" ;

 

UF_OBJ_cycle_by_name_and_type(displayPart,nameA,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences //返回面FaceA的occtag

 

Face *face1;

tag_t com_Temp_Tag= NULL_TAG ;///组件的临时Tag

face1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));

vecFaceA.push_back((face1));

compFaceA_TagNum.push_back(face1->Tag());

objectOcc = NULL_TAG;

UF_OBJ_cycle_by_name_and_type(displayPart,nameB,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences //返回面FaceA的occtag

face1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));

vecFaceB.push_back((face1));

compFaceB_TagNum.push_back(face1->Tag());

//////////////////本要下面的循环,但是这里为了程序运算速度 ,还是偷懒吧/////////////////////

/*while(objectOcc!=NULL_TAG)

{

face1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));//feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。

com_Temp_Tag = face1->OwningComponent()->Tag();//由faceA的occtag获得其在装配中的组件occtag

vecFaceA.push_back((face1));

if(com_Temp_Tag=compNum[i]->Tag())

{

compFaceA_TagNum.push_back(face1->Tag());

}

 

 

UF_OBJ_cycle_by_name_and_type(displayPart,nameA,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences

 

}

////////////////重复一次B面循环//////////////////////

objectOcc = NULL_TAG;//复0

UF_OBJ_cycle_by_name_and_type(displayPart,nameB,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences //返回面FaceA的occtag

while(objectOcc!=NULL_TAG)

{

 

face1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));//feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。

com_Temp_Tag = face1->OwningComponent()->Tag();//由faceA的occtag获得其在装配中的组件occtag

vecFaceB.push_back((face1));

 

if(com_Temp_Tag=compNum[i]->Tag())

{

compFaceB_TagNum.push_back(face1->Tag());

}

 

 

UF_OBJ_cycle_by_name_and_type(displayPart,nameB,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences

 

}*/

/////////////////////////////////////// /////////////////////////////////////////此组等效,返回的是本组件的OCC_Tag

/*tag_t part_tag= NULL_TAG ;

UF_ASSEM_ask_parent_component(39582,&part_tag);

face1->OwningComponent()->Tag();

sprintf(msg,"UF_OWN:%d",part_tag);

uc1601(msg,1);

 

face1->OwningComponent()->Tag();

sprintf(msg,"NXOpen_OWN:%d",face1->OwningComponent()->Tag());//此组等效,返回的是本组件的OCC_Tag

uc1601(msg,1);*/

 

UF_terminate();

 

}

 

void ZiDongWanTou::MeasueDisance(std::vector<Face*> vecFaceA,std::vector<Face*> vecFaceB)

{

Session *theSession = Session::GetSession();

Part *workPart(theSession->Parts()->Work());

Part *displayPart(theSession->Parts()->Display());

NXObject *nullNXObject(NULL);

//std::vector<double> disVector;//全局变量

int n = vecFaceA.size()-1;

for(int i=0;i<n;i++)

{

MeasureDistanceBuilder *measureDistanceBuilder1;

measureDistanceBuilder1 = workPart->MeasureManager()->CreateMeasureDistanceBuilder(nullNXObject);

Unit *unit1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));

 

measureDistanceBuilder1->SetMtype(MeasureDistanceBuilder::MeasureTypeMinimum);

 

Line *line1(dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i+1])));

Direction *direction1;

direction1 = workPart->Directions()->CreateDirection(line1, SenseForward, SmartObject::UpdateOptionAfterModeling);

 

measureDistanceBuilder1->SetProjectionVector(direction1);

measureDistanceBuilder1->Object1()->SetValue( vecFaceB[i]);

measureDistanceBuilder1->Object1()->SetValue( vecFaceA[i+1]);

 

MeasureDistance *measureDistance1;

measureDistance1 = workPart->MeasureManager()->NewDistance(NULL, vecFaceB[i], vecFaceA[i+1], direction1, MeasureManager::ProjectionTypeMinimum);

 

 

//Measure *measure1;

//measure1 = measureDistance1->CreateFeature();//只需得到值不创建

double distance;

distance = measureDistance1->Value();//获取测量的值

disVector.push_back(distance);

/*sprintf(msg,"TAG:%g",disVector[i]);

uc1601(msg,1);*/

}

}

 

void ZiDongWanTou::ZhiGuan()

{

 

double l1_max, l1, l1_dj;

char zght1_f[100],l1_max_f[100],l1_dj_f[100];

 

 

 

string  zght1_ss;//活套数量获得

l1_max= btl1->GetProperties()->GetDouble("Value");//活套数量获得

zght1_ss = btzght1->GetProperties()->GetEnumAsString("Value").GetLocaleText();//活套数量获得

 

sprintf(l1_max_f,"%.1f",l1_max);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出。//6米直管长度获得

double mod1;

int n1,n2;

 

int n = vecFaceA.size()-1;

for(int i=0;i<n;i++)

{

mod1 = fmod(disVector[i],l1_max) ;

n1 = disVector[i]/l1_max;//整除

 

n2 = (disVector[i]-150)/l1_max;//不整除

l1_dj = disVector[i]-n2*l1_max;

 

l1_max = atof(l1_max_f);

 

char l1_dj_f[100];

sprintf(l1_dj_f,"%.1f",l1_dj);

l1_dj = atof(l1_dj_f);

 

if(mod1==0) //取余等0,则整除,没有短节

{

if(n1!=0) //如果为此,则先装一个长直管

{

uc1601("大于0件,先装1件长管",1);

 

ZGCreat(l1_max,zght1_ss); //创建直管

}

else if(n1>1)

{

uc1601("大于1件,阵列",1);

 

}

 

//else(等于0)什么也不做,也不可能等于0

 

}

 

else //不整除

{

 

//uc1601("不管如何,先装短节",1);

ZGCreat(l1_dj,zght1_ss); //创建直管短节

ZGAssemble(i,1,vecFaceA);//这里用J=1或者J=0动态选择不同弯头

 

if(n2==1) //如果为此,则先装一个长直管

{

ZGCreat(l1_max,zght1_ss); //创建直管

ZGAssemble(i,0,vecFaceB);//这里用J=1或者J=0动态选择不同弯头

}

else if(n2>1)

{

ZGCreat(l1_max,zght1_ss); //创建直管

ZGAssemble(i,0,vecFaceB);//这里用J=1或者J=0动态选择不同弯头

 

ZGArray(i,n2,l1_max);

 

//uc1601("大于1件,阵列",1);

 

}

 

}

 

}

}

 

 

void ZiDongWanTou::ZGCreat(double l1,string zght1_ss)

{

//////////////////////////void ZhiGuan::zdy_step1()//////////////////////////

 

UF_initialize();

tag_t parentPart = UF_PART_ask_display_part();

/*

d1 = btd1->GetProperties()->GetDouble("Value");

l1 = btl1->GetProperties()->GetDouble("Value");//形参传入

pn = btpn->GetProperties()->GetDouble("Value");//压力

string ht1_ss;

ht1_ss = btht1->GetProperties()->GetEnumAsString("Value").GetLocaleText();

ht1_s = ht1_ss.substr(0,1);//ht1_s为字符串类型,此处获得活套数量0,1,2

sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用

sprintf(PN_f,"%g",pn);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

sprintf(ht1_f,"%s",ht1_s.c_str());//字符串转char

*/

//------------------------------------------------------------------------------

//定义  环境变量、原标准件库  路径

 //获取环境变量地址

//------------------------------------------------------------------------------

//path = getenv("UGII_BASE_DIR");

char *  envPath;

UF_translate_variable("UGII_BASE_DIR",&envPath);

string basedir1 =envPath;

string basedir2 ="\\lib\\";

string basePath =basedir1+basedir2;

//定义  工作部件   置路径

//workPath = btlibpath->Path();

workPath = btlibpath->GetProperties()->GetString("Path");//获取路径,(开始已经定义为全局变量)此处是NX string类型

 

//-----------------------------------------------------------------------

//-------------定义新老零件路径名称------------------------

//------------------------------------------------------------------------------

char oldZGName [222];

string  zght1_s;

zght1_s = zght1_ss.substr(0,1);//活套数量获得

sprintf(zght1_f,"%s",zght1_s.c_str());//字符串转char//活套数量获得

sprintf(l1_f,"%g",l1);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

 

sprintf(oldZGName,"%sZG%s-%sH%s-001.prt",basePath.c_str(),d1_f,pn_f,ht1_f);//此处basePath是sting,所以要转为char*.而char转char*,用sprintf();

 

sprintf(newZGName,"%s\\ZG%s-%sH%s-%s.prt",workPath.GetLocaleText(),d1_f,pn_f,zght1_f,l1_f);//l1分别代表l1_maxt和l1_dj在不同情况下的值

 

FILE *mmfp;       

mmfp = fopen(newZGName,"r"); 

if(mmfp != NULL)

{

return;

}

//打开文件

tag_t partTag = NULL_TAG;

UF_PART_load_status_t  error_status ;

UF_PART_open(oldZGName,&partTag,&error_status);//此处需要char*类型,若是string组合的需要s0.c_str()

UF_PART_free_load_status(&error_status);

/*uc1601(oldPartName,1);

uc1601(newZGLmaxName,1);*/

 

//重命名文件

UF_PART_rename(partTag,newZGName);

 

//-----------------------------------------------------------------------

 

 

////////////////////////////void ZhiGuan::zdy_step2()//////////////////////////

 

//定义  以(外径+压力)为基础的 文件名称:ZG-630-10-001

//外径、长度等驱动尺寸

//l1 = btl1->GetProperties()->GetDouble("Value"); // 由形参传入

s1 = bts1->GetProperties()->GetDouble("Value");

s2 = bts2->GetProperties()->GetDouble("Value");

fb = btfb->GetProperties()->GetDouble("Value");

 

sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用

sprintf(l1_f,"%g",l1);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

sprintf(s1_f,"%g",s1);

sprintf(s2_f,"%g",s2);

sprintf(fb_f,"%g",fb);

 

//--------------------------------------------------------------------

sprintf(d1_e,"d1=%f",d1);//生产直径参数,供生成文件名称用

sprintf(l1_e,"l1=%f",l1);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出

sprintf(s1_e,"s1=%f",s1);

sprintf(s2_e,"s2=%f",s2);

sprintf(fb_e,"fb=%f",fb);

//派生尺寸

double d2 = d1-2*s1;     

 

//---------------------zdy_step2之后-----------------------------------------------

 

UF_MODL_edit_exp(d1_e);

UF_MODL_edit_exp(l1_e);

UF_MODL_edit_exp(s1_e);

UF_MODL_edit_exp(s2_e);

UF_MODL_edit_exp(fb_e);

 

UF_MODL_update();

 

////////////////////////////void ZhiGuan::ziLj()//子零件复制//////////////////////////

 

tag_t root_part_occ = NULL_TAG;

tag_t displayPart = NULL_TAG;

displayPart = UF_PART_ask_display_part();//显示部件的TAG

root_part_occ = UF_ASSEM_ask_root_part_occ(displayPart);//根部件的事件pat_TAG_OCC

 

  //int i;

  int component_count = 0;

  //tag_t parent = NULL_TAG;

  //tag_t member = NULL_TAG;

  tag_t *child_components = NULL;

component_count = UF_ASSEM_ask_part_occ_children(root_part_occ,

                                          &child_components);//Array of tags of child part occurrences.

/* //第一种方法,事件法

tag_t parTag = UF_ASSEM_ask_prototype_of_occ(child_components[0]);//获得part tag 从occurrences tag

char parNm[200];

UF_PART_ask_part_name(parTag, parNm);//从parTag获取ParName,但是这个必需组件加载*/

 

//第二种方法--实例法InTag

for (int i = 0; i < component_count; i++)

{

char ziOldParNme[200];

tag_t parInTag = UF_ASSEM_ask_inst_of_part_occ(child_components[i]);

UF_ASSEM_ask_part_name_of_child(parInTag,ziOldParNme); //这个可以不加载组件

 

stringstream tn1 ;

tn1<<ziOldParNme;

string tn2 = tn1.str();

int star1 = tn2.find_last_of("\\");

int end1 = tn2.find_last_of(".");

string lastName1 = tn2.substr(star1,end1-star1);

 

//workPath = btlibpath->Path();//获取路径

char ziNewPartName [100];

sprintf(ziNewPartName,"%s\\%s.prt",workPath.GetLocaleText(),lastName1.c_str());

uc4567(ziOldParNme,ziNewPartName,UF_CFI_COPY_NEVER_REPLACE,2,2);

/*char msg[111];

sprintf(msg,"%s",ziNewPartName);

uc1601(msg,1);*/

}

UF_free(child_components);

UF_PART_save();/////////保在零件//////////////////

 

UF_PART_close(UF_PART_ask_display_part(),1,1);

 

 

UF_PART_set_display_part(parentPart);

UF_terminate();

 

}

void ZiDongWanTou::ZGAssemble(int i,int j,std::vector<Face*> vecFace)

{

UF_initialize();

char msg[100];

Session *theSession = Session::GetSession();

Part *displayPart(theSession->Parts()->Display());

Part *workPart(theSession->Parts()->Work());

tag_t parentPart = UF_PART_ask_display_part();

if(i<lineNum.size()-2)

{

 

line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针

line2 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i+1])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针

 

//添加组件

Point3d basePoint1(0.0, 0.0, 0.0);

Matrix3x3 orientation1;

 

Point3d  p11(0.0, 0.0, 0.0);

Point3d  p12(0.0, 0.0, 0.0);

Point3d  p21(0.0, 0.0, 0.0);

Point3d  p22(0.0, 0.0, 0.0);

p11 = line1->StartPoint();

p12 = line1->EndPoint();

p21 = line2->StartPoint();

p22 = line2->EndPoint();

 

basePoint1 = p12;

orientation1.Xx = p12.X-p11.X;

orientation1.Xy = p12.Y-p11.Y;

orientation1.Xz = p12.Z-p11.Z;

orientation1.Yx = p21.X-p22.X;

orientation1.Yy = p21.Y-p22.Y;

orientation1.Yz = p21.Z-p22.Z;

PartLoadStatus *partLoadStatus2;

 

const char * fspec1=newZGName;

int ftype1=2;//.prt格式

char occ_name1[100];

uc4574(fspec1,ftype1,occ_name1);///分割路径为目录和文件名uc4576 ,char dspec1[100],fname1[100];

stringstream tn1;

tn1<<occ_name1;

string tn2=tn1.str();

 

for(int i=0;i<lineNum.size()-1;i++)

{

compZGNum.push_back(component1);

}

compZGNum[i] = workPart->ComponentAssembly()->AddComponent(newZGName, "model",tn2, basePoint1, orientation1, -1, &partLoadStatus2);

/*sprintf(msg,"%s",newZGName);

uc1601(msg,1);*/

component1 = compZGNum[i];

delete partLoadStatus2;

 

std::vector<Face*> vecFaceZGA;//FaceA数组

std::vector<Face*> vecFaceZGB;//FaceB数组

std::vector<Face*> vecFaceZGC;//FaceC数组

 

 

tag_t objectOcc = NULL_TAG;

int type = UF_solid_type;//可以查询line, sketch,其他的edge,feature,solid摸不清规律

const char * faceZGName_A= "FACE_A" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名

//const char * faceZGName_B= "FACE_B" ;///都用A端,这个就用不到了

const char * faceZGName_C= "FACE_C" ;

 

UF_OBJ_cycle_by_name_and_type(parentPart,faceZGName_A,type, TRUE, &objectOcc);

Face *faceZG_A;

faceZG_A = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));

vecFaceZGA.push_back((faceZG_A));

 

Positioning::ComponentPositioner *componentPositioner1;

componentPositioner1 = workPart->ComponentAssembly()->Positioner();

 

componentPositioner1->ClearNetwork();

componentPositioner1->BeginAssemblyConstraints();

 

Positioning::Network *network1;

network1 = componentPositioner1->EstablishNetwork();

 

Positioning::ComponentNetwork *componentNetwork1(dynamic_cast<Positioning::ComponentNetwork *>(network1));

componentNetwork1->SetMoveObjectsState(true);

 

Assemblies::Component *nullAssemblies_Component(NULL);

componentNetwork1->SetDisplayComponent(nullAssemblies_Component);

 

//第一组

 

Positioning::Constraint *constraint1;

constraint1 = componentPositioner1->CreateConstraint();

 

Positioning::ComponentConstraint *componentConstraint1(dynamic_cast<Positioning::ComponentConstraint *>(constraint1));

componentConstraint1->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);

componentConstraint1->SetConstraintType(Positioning::Constraint::TypeTouch);

/////定义约束对象

/////获取选择对象的进行动态转换(这个是成功的)

/*std::vector<TaggedObject*>objects1 = this->selection01->GetProperties()->GetTaggedObjectVector("SelectedObjects");

Line *line3=NULL;

line3 = dynamic_cast<Line *>(objects1[0]); */

 

////装配进来的件就不用选择了

Positioning::ConstraintReference *constraintReference1;

constraintReference1 = componentConstraint1->CreateConstraintReference(component1, faceZG_A, false, false, false);

 

/*////获取选择对象的进行动态转换(这个是成功的)

std::vector<TaggedObject*>objects2 = this->edge_select01->GetProperties()->GetTaggedObjectVector("SelectedObjects");

//objects2[0]->Tag();

Line *line4=NULL;

line4 = dynamic_cast<Line *>(objects2[0]);*/

 

 

///定义约束对象

/*Positioning::ConstraintReference *constraintReference1;

constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line3, false, false, false);*/

 

Positioning::ConstraintReference *constraintReference2;

constraintReference2 = componentConstraint1->CreateConstraintReference(compNum[i+j], vecFace[i+j], false, false, false);

 

constraintReference2->SetFixHint(true);

 

componentNetwork1->Solve();

 

//第二组

//////////////////////////必要的参数//////////////////

objectOcc = NULL_TAG;

UF_OBJ_cycle_by_name_and_type(parentPart,faceZGName_C,type, TRUE, &objectOcc);

Face *faceZG_C;

faceZG_C = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));

vecFaceZGC.push_back((faceZG_C));

 

//////////////////////////////////////////////

Positioning::Constraint *constraint2;

constraint2 = componentPositioner1->CreateConstraint();

 

Positioning::ComponentConstraint *componentConstraint2(dynamic_cast<Positioning::ComponentConstraint *>(constraint2));

componentConstraint2->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);

componentConstraint2->SetConstraintType(Positioning::Constraint::TypeTouch);

 

/////获取选择对象的进行动态转换(这个是成功的)

Positioning::ConstraintReference *constraintReference3;

constraintReference3 = componentConstraint2->CreateConstraintReference(component1, faceZG_C,true, false, false);

Positioning::ConstraintReference *constraintReference4;

constraintReference4 = componentConstraint2->CreateConstraintReference(workPart->ComponentAssembly(), vecLine[i+1], false, false, false);

 

constraintReference4->SetFixHint(true);

componentNetwork1->Solve();

 

componentPositioner1->ClearNetwork(); 

componentPositioner1->EndAssemblyConstraints(); 

 

UF_terminate();

 

}

 

}

 

void ZiDongWanTou::ZGArray(int i,int n,double l1)

{

UF_initialize();

tag_t displayPart = UF_PART_ask_display_part();

tag_t num1;//数量1

//tag_t num2;//数量2

tag_t dis1;//距离1

//tag_t dis2;//距离2

char  *numPref = "Num";

char num_1[111],dis_1[111],num_2[111],dis_2[111];

sprintf(num_1,"%s%d=%d",numPref,lineNum[i+1],n);

sprintf(num_2,"%s%d_2=1",numPref,lineNum[i+1]);

 

UF_MODL_create_exp_tag(num_1,&num1);//注意这里参数输入需要以表达式形式

//UF_MODL_create_exp_tag(num_2,&num2);

///////////////////////////////////////////////

const char * left_hand_side = "Num_2";

bool  is_exp_in_part ;

UF_MODL_is_exp_in_part(displayPart, left_hand_side,  &is_exp_in_part);

 

if(is_exp_in_part==0)

{

UF_MODL_create_exp_tag("Num_2=1",&num2);//注意这里参数输入需要以表达式形式

}

 

//////////////////////////////////////////////////

/*sprintf(msg,"%d",is_exp_in_part);

uc1601(msg,1);*/

char  *disPref = "Dis";

sprintf(dis_1,"%s%d=%g",disPref,lineNum[i+1],l1);

sprintf(dis_2,"%s%d_2=100",disPref,lineNum[i+1]);

 

UF_MODL_create_exp_tag(dis_1,&dis1);//创建表达式

//UF_MODL_create_exp_tag(dis_2,&dis2);

//////////////////////////////////////////////////

UF_MODL_is_exp_in_part(displayPart, "Dis_2",  &is_exp_in_part);

if(is_exp_in_part==0)

{

UF_MODL_create_exp_tag("Dis_2=100",&dis2);//创建表达式

}

//////////////////////////////////////////////////

 

////////////////////////定义参数//////////////////////

UF_ASSEM_mc_array_data_t array_data;

 

tag_t partTag = component1->Tag();

 

//tag_t directTag1 = lineNum[i+1];//方向1

tag_t directTag1 = vecFaceB[i]->Tag();//方向1

tag_t directTag2 = lineNum[i];//方向2,这个实际用不到

 

array_data.array_subtype = UF_ASSEM_linear_array;//UF_ASSEM_circular_array

array_data.master_component = partTag;

array_data.template_component = NULL_TAG;

/*tag_t arrayNum[2]={4,NULL};*/

array_data.dimensions[0] = num1;

array_data.dimensions[1] = num2;

 

//tag_t arrayDirect[2]={69278,NULL};

array_data.axis_definitions[0] = directTag1;

array_data.axis_definitions[1] = directTag2;

 

array_data.offsets[0] = dis1;

array_data.offsets[1] = dis2;

 

char  *partNamePref = "ArraPN";

char ArraPN[111];

sprintf(ArraPN,"%s%d",partNamePref,lineNum[i+1]);

 

array_data.array_name = ArraPN;//原型array_data.array_name ="partname1"

tag_t partArray_tag; 

////////////////////////定义参数//////////////////////

UF_ASSEM_create_mc_array(&array_data,&partArray_tag);

//int k =0;

//uc1601(msg,1);

//char  *old_exp_name = "num1";

//char  *newNamePref = "Num";

//char new_Name[111] ;

//sprintf(new_Name,"%s%d",newNamePref,lineNum[i+1]);

//k++;

//UF_MODL_rename_exp(old_exp_name, new_Name);//这里的char*和char[]是等价的

//UF_free(array_data.array_name);///When this value is returned from an enquiry function, it is动态分配时 dynamically allocated, and must be freed with UF_free.这里不需要

UF_terminate();

}

void ZiDongWanTou::cycleLine(){
Session *theSession = Session::GetSession();Part *displayPart(theSession->Parts()->Display());Part *workPart(theSession->Parts()->Work());/////链先取方式std::vector<TaggedObject*>objects1 = this->edge_select0->GetProperties()->GetTaggedObjectVector("SelectedObjects");line1 = dynamic_cast<Line*>(objects1[0]);lineNum.push_back(line1->Tag());//这个是Line的tagvecLine.push_back(line1);//这个是Line的obj/*Point3d  p11(0.0, 0.0, 0.0);*/Point3d  p12(0.0, 0.0, 0.0);Point3d  p21(0.0, 0.0, 0.0);//Point3d  p22(0.0, 0.0, 0.0);for(int i=0;i<objects1.size()-1;i++){for(int j=0;i<objects1.size()-1;j++){line2 = dynamic_cast<Line*>(objects1[j]);
p12=line1->EndPoint();p21=line2->StartPoint();  ////p12=p21
if((p12.X==p21.X)&&(p12.Y==p21.Y)&&(p12.Z==p21.Z)){lineNum.push_back(line2->Tag());vecLine.push_back(line2);line1 = line2;
break;
}}
}
/////链先取方式/////链先取方式
////以下为全部遍历方式//Features::Feature *fcList;//std::vector<NXObject *> entitiesArray; //std::vector是一种数组定义方式//Features::FeatureCollection *fc=workPart->Features();//迭代//Features::FeatureCollection::iterator itr=fc->begin();//获得第一个特征

//for (;itr!=fc->end();++itr) //循环获得所有特征//{//fcList=(*itr); //获得特征赋值给fcList//NXString featType = NULL;//featType = fcList->FeatureType();
///*theSession->ListingWindow()->WriteLine(fcList->FeatureType());*///if(!strcmp(featType.GetLocaleText(),"LINE")) //{////lineNum.push_back(fcList->Tag());
//entitiesArray = fcList->GetEntities(); //GetEntities方法,实体下0对象,Lineg下1对对象,基准下7对象,点集下对象最多//lineNum.push_back(entitiesArray[0]->Tag());
//}
//}//以下为全部遍历方式
//line1 =dynamic_cast<Line *>(entitiesArray[0]);
/*for(int i=0;i<lineNum.size();i++)//{////NXObjectManager::Get(lineNum[i]);//通过tag得到 TaggedObject 对象指针. ////line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针}*/
/*line1 = dynamic_cast<Line*>(entitiesArray[0]);line2 = dynamic_cast<Line*>(entitiesArray[0]); *///line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[0])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针//line2 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[1])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针//以下为全部遍历方式//以下为全部遍历方式////以下为全部遍历方式}
void ZiDongWanTou::assemPart(){
Session *theSession = Session::GetSession();Part *workPart(theSession->Parts()->Work());Part *displayPart(theSession->Parts()->Display());tag_t parentPart = UF_PART_ask_display_part();
/*借鉴一下disp_part_tag=UF_PART_ask_display_part ( ); //得到当前部件文件标识UF_OBJ_cycle_by_name_and_type (disp_part_tag,obj_name,type,use_occ,&next_tag);while(next_tag!=NULL_TAG ){num++; UF_OBJ_set_color(next_tag,20);    // 对象修改颜色UF_OBJ_cycle_by_name_and_type (disp_part_tag,"MM",UF_solid_type,use_occ,&next_tag);} *//////////////////获取Line7,Line8tag_t feature= NULL_TAG ;int type = UF_line_type;//可以查询line, sketch,其他的edge,feature,solid摸不清规律const char * name7= "LINE7" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名UF_OBJ_cycle_by_name_and_type(parentPart,name7,type, TRUE, &feature); Line *line7 = dynamic_cast<Line*>(NXObjectManager::Get(feature));  //feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。feature= NULL_TAG;const char * name8= "LINE8" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名UF_OBJ_cycle_by_name_and_type(parentPart,name8,type, TRUE, &feature); Line *line8 = dynamic_cast<Line*>(NXObjectManager::Get(feature));//feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。///////////////////////////////////////
Positioning::ComponentPositioner *componentPositioner1;componentPositioner1 = workPart->ComponentAssembly()->Positioner();
componentPositioner1->ClearNetwork();componentPositioner1->BeginAssemblyConstraints();
Positioning::Network *network1;network1 = componentPositioner1->EstablishNetwork();
Positioning::ComponentNetwork *componentNetwork1(dynamic_cast<Positioning::ComponentNetwork *>(network1));componentNetwork1->SetMoveObjectsState(true);
Assemblies::Component *nullAssemblies_Component(NULL);componentNetwork1->SetDisplayComponent(nullAssemblies_Component);
//第一组Positioning::Constraint *constraint1;constraint1 = componentPositioner1->CreateConstraint();
Positioning::ComponentConstraint *componentConstraint1(dynamic_cast<Positioning::ComponentConstraint *>(constraint1));componentConstraint1->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);componentConstraint1->SetConstraintType(Positioning::Constraint::TypeTouch);/////定义约束对象/////获取选择对象的进行动态转换(这个是成功的)/*std::vector<TaggedObject*>objects1 = this->selection01->GetProperties()->GetTaggedObjectVector("SelectedObjects");Line *line3=NULL;line3 = dynamic_cast<Line *>(objects1[0]); */
////装配进来的件就不用选择了Positioning::ConstraintReference *constraintReference1;constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line7, false, false, false);
/*////获取选择对象的进行动态转换(这个是成功的)std::vector<TaggedObject*>objects2 = this->edge_select01->GetProperties()->GetTaggedObjectVector("SelectedObjects");//objects2[0]->Tag();Line *line4=NULL;line4 = dynamic_cast<Line *>(objects2[0]);*/

///定义约束对象/*Positioning::ConstraintReference *constraintReference1;constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line3, false, false, false);*/
Positioning::ConstraintReference *constraintReference2;constraintReference2 = componentConstraint1->CreateConstraintReference(workPart->ComponentAssembly(), line1, false, false, false);
constraintReference2->SetFixHint(true);
componentNetwork1->Solve();//第二组Positioning::Constraint *constraint2;constraint2 = componentPositioner1->CreateConstraint();
Positioning::ComponentConstraint *componentConstraint2(dynamic_cast<Positioning::ComponentConstraint *>(constraint2));componentConstraint2->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);componentConstraint2->SetConstraintType(Positioning::Constraint::TypeTouch);
/////获取选择对象的进行动态转换(这个是成功的)Positioning::ConstraintReference *constraintReference3;constraintReference3 = componentConstraint2->CreateConstraintReference(component1, line8, false, false, false);Positioning::ConstraintReference *constraintReference4;constraintReference4 = componentConstraint2->CreateConstraintReference(workPart->ComponentAssembly(), line2, false, false, false);
constraintReference4->SetFixHint(true);componentNetwork1->Solve();
componentPositioner1->ClearNetwork(); componentPositioner1->EndAssemblyConstraints(); }
////////////////以为为弯头创建子程序void ZiDongWanTou::zdy_step1()//打开库零件,并重命名{UF_initialize();d1 = btd1->GetProperties()->GetDouble("Value");
//ang = btang->GetProperties()->GetDouble("Value");  //此参数将有程序获得
pn = btpn->GetProperties()->GetDouble("Value");//压力string ht1_ss;ht1_ss = btht1->GetProperties()->GetEnumAsString("Value").GetLocaleText();xh1_s = btxh1->GetProperties()->GetString("Value").GetLocaleText();ht1_s = ht1_ss.substr(0,1);//ht1_s为字符串类型,此处获得活套数量0,1,2sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用sprintf(pn_f,"%g",pn);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出sprintf(ht1_f,"%s",ht1_s.c_str());//字符串转charsprintf(xh1_f,"%s",xh1_s.c_str());//字符串转char


//------------------------------------------------------------------------------//定义  环境变量、原标准件库  路径 //获取环境变量地址//------------------------------------------------------------------------------//path = getenv("UGII_BASE_DIR");char *  envPath;UF_translate_variable("UGII_BASE_DIR",&envPath);string basedir1 =envPath;string basedir2 ="\\lib\\";basePath =basedir1+basedir2;//定义  工作部件   置路径//workPath = btlibpath->Path();workPath = btlibpath->GetProperties()->GetString("Path");//获取路径,(开始已经定义为全局变量)此处是NX string类型//-----------------------------------------------------------------------//-------------定义新老零件路径名称------------------------//------------------------------------------------------------------------------sprintf(oldPartName,"%sWT%s-%sH%s-001.prt",basePath.c_str(),d1_f,pn_f,ht1_f);//此处basePath是sting,所以要转为char*.而char转char*,用sprintf();sprintf(ang_f,"%g",ang);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出sprintf(s1_f,"%g",s1);sprintf(newPartName,"%s\\WT%sX%s-%sH%s-%s.prt",workPath.GetLocaleText(),d1_f,ang_f,pn_f,ht1_f,xh1_f);//打开文件
//-----------------------------------------------------------------------UF_terminate();}void ZiDongWanTou::zdy_step2()//表达式的准备工作{UF_initialize();/////////////把step1的放这,以便用goto语句tag_t partTag = NULL_TAG;UF_PART_load_status_t  error_status ;UF_PART_open(oldPartName,&partTag,&error_status);//此处需要char*类型,若是string组合的需要s0.c_str()UF_PART_free_load_status(&error_status);
//uc1601(newPartName,1);
//重命名文件UF_PART_rename(partTag,newPartName);/////////////把step1的放这,以便用goto语句//定义  以(外径+压力)为基础的 文件名称:ZG-630-10-001//外径、长度等驱动尺寸/*ang = btang->GetProperties()->GetDouble("Value");*/s1 = bts1->GetProperties()->GetDouble("Value");s2 = bts2->GetProperties()->GetDouble("Value");r1 = btr1->GetProperties()->GetDouble("Value");r2 = btr2->GetProperties()->GetDouble("Value");fb = btfb->GetProperties()->GetDouble("Value");pn = btpn->GetProperties()->GetDouble("Value");R = btR->GetProperties()->GetDouble("Value");
xh1_s = btxh1->GetProperties()->GetString("Value").GetLocaleText();//序号的转换sprintf(xh1_f,"\"%s\"",xh1_s.c_str());//字符串转char,这里加不加c_str()都可以sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用sprintf(ang_f,"%g",ang);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出sprintf(s1_f,"%g",s1);sprintf(s2_f,"%g",s2);sprintf(r1_f,"%g",r1);sprintf(r2_f,"%g",r2);sprintf(fb_f,"%g",fb);sprintf(pn_f,"%g",pn);sprintf(R_f,"%g",R);//--------------------------------------------------------------------sprintf(d1_e,"d1=%f",d1);//生产直径参数,供生成文件名称用sprintf(ang_e,"ang=%f",ang);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出sprintf(s1_e,"s1=%f",s1);sprintf(s2_e,"s2=%f",s2);sprintf(r1_e,"r1=%f",r1);sprintf(r2_e,"r2=%f",r2);sprintf(fb_e,"fb=%f",fb);sprintf(pn_e,"pn=%f",pn);sprintf(R_e,"R=%f",R);
//派生尺寸double d2 = d1-2*s1;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
UF_terminate();}void ZiDongWanTou::ziLj()//子零件复制{UF_initialize();tag_t root_part_occ = NULL_TAG;tag_t displayPart = NULL_TAG;displayPart = UF_PART_ask_display_part();//显示部件的TAGroot_part_occ = UF_ASSEM_ask_root_part_occ(displayPart);//根部件的事件pat_TAG_OCC
  int i;  int component_count = 0;  //tag_t parent = NULL_TAG;  //tag_t member = NULL_TAG;  tag_t *child_components = NULL; component_count = UF_ASSEM_ask_part_occ_children(root_part_occ,                                          &child_components);//Array of tags of child part occurrences. /* //第一种方法,事件法 tag_t parTag = UF_ASSEM_ask_prototype_of_occ(child_components[0]);//获得part tag 从occurrences tag char parNm[200]; UF_PART_ask_part_name(parTag, parNm);//从parTag获取ParName,但是这个必需组件加载*/
//第二种方法--实例法InTag for (int i = 0; i < component_count; i++) {char ziOldParNme[200];tag_t parInTag = UF_ASSEM_ask_inst_of_part_occ(child_components[i]);UF_ASSEM_ask_part_name_of_child(parInTag,ziOldParNme); //这个可以不加载组件
stringstream tn1 ;tn1<<ziOldParNme;string tn2 = tn1.str();int star1 = tn2.find_last_of("\\");int end1 = tn2.find_last_of(".");string lastName1 = tn2.substr(star1,end1-star1);
//workPath = btlibpath->Path();//获取路径char ziNewPartName [100];sprintf(ziNewPartName,"%s\\%s.prt",workPath.GetLocaleText(),lastName1.c_str());uc4567(ziOldParNme,ziNewPartName,UF_CFI_COPY_NEVER_REPLACE,2,2);/*char msg[111];sprintf(msg,"%s",ziNewPartName);uc1601(msg,1);*/ } UF_free(child_components); UF_terminate();}void ZiDongWanTou::FaceAppend(int i){UF_initialize();tag_t displayPart = UF_PART_ask_display_part();tag_t object_occur=NULL_TAG;//ot = UF_ASSEM_cycle_ents_in_part_occ(part_occur,object_occur);///////////////////////////A面循环//////////////////////////////////tag_t compFeature= NULL_TAG ;//全局变量tag_t objectOcc = NULL_TAG;int type = UF_solid_type;const char * nameA= "FACE_A" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名const char * nameB= "FACE_B" ;
UF_OBJ_cycle_by_name_and_type(displayPart,nameA,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences //返回面FaceA的occtag
Face *face1;tag_t com_Temp_Tag= NULL_TAG ;///组件的临时Tagface1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));vecFaceA.push_back((face1));compFaceA_TagNum.push_back(face1->Tag());objectOcc = NULL_TAG;UF_OBJ_cycle_by_name_and_type(displayPart,nameB,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences //返回面FaceA的occtagface1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));vecFaceB.push_back((face1));compFaceB_TagNum.push_back(face1->Tag());//////////////////本要下面的循环,但是这里为了程序运算速度 ,还是偷懒吧//////////////////////*while(objectOcc!=NULL_TAG){face1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));//feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。com_Temp_Tag = face1->OwningComponent()->Tag();//由faceA的occtag获得其在装配中的组件occtagvecFaceA.push_back((face1));if(com_Temp_Tag=compNum[i]->Tag()){compFaceA_TagNum.push_back(face1->Tag());}
UF_OBJ_cycle_by_name_and_type(displayPart,nameA,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences
}////////////////重复一次B面循环//////////////////////objectOcc = NULL_TAG;//复0UF_OBJ_cycle_by_name_and_type(displayPart,nameB,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences //返回面FaceA的occtagwhile(objectOcc!=NULL_TAG){face1 = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));//feature应该恰好是第一个,而正好是我们当下需要的那个。这是个巧合。com_Temp_Tag = face1->OwningComponent()->Tag();//由faceA的occtag获得其在装配中的组件occtagvecFaceB.push_back((face1));
if(com_Temp_Tag=compNum[i]->Tag()){compFaceB_TagNum.push_back(face1->Tag());}
UF_OBJ_cycle_by_name_and_type(displayPart,nameB,type, TRUE, &objectOcc);  //TRUE = include occurrences FALSE = exclude occurrrences
}*//////////////////////////////////////// /////////////////////////////////////////此组等效,返回的是本组件的OCC_Tag/*tag_t part_tag= NULL_TAG ;UF_ASSEM_ask_parent_component(39582,&part_tag);face1->OwningComponent()->Tag();sprintf(msg,"UF_OWN:%d",part_tag);uc1601(msg,1);
face1->OwningComponent()->Tag();sprintf(msg,"NXOpen_OWN:%d",face1->OwningComponent()->Tag());//此组等效,返回的是本组件的OCC_Taguc1601(msg,1);*/UF_terminate();}
void ZiDongWanTou::MeasueDisance(std::vector<Face*> vecFaceA,std::vector<Face*> vecFaceB){Session *theSession = Session::GetSession();Part *workPart(theSession->Parts()->Work());Part *displayPart(theSession->Parts()->Display());NXObject *nullNXObject(NULL);//std::vector<double> disVector;//全局变量int n = vecFaceA.size()-1;for(int i=0;i<n;i++){MeasureDistanceBuilder *measureDistanceBuilder1;measureDistanceBuilder1 = workPart->MeasureManager()->CreateMeasureDistanceBuilder(nullNXObject);Unit *unit1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));
measureDistanceBuilder1->SetMtype(MeasureDistanceBuilder::MeasureTypeMinimum);
Line *line1(dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i+1])));Direction *direction1;direction1 = workPart->Directions()->CreateDirection(line1, SenseForward, SmartObject::UpdateOptionAfterModeling); measureDistanceBuilder1->SetProjectionVector(direction1);measureDistanceBuilder1->Object1()->SetValue( vecFaceB[i]);measureDistanceBuilder1->Object1()->SetValue( vecFaceA[i+1]);
MeasureDistance *measureDistance1;measureDistance1 = workPart->MeasureManager()->NewDistance(NULL, vecFaceB[i], vecFaceA[i+1], direction1, MeasureManager::ProjectionTypeMinimum);
//Measure *measure1;//measure1 = measureDistance1->CreateFeature();//只需得到值不创建double distance;distance = measureDistance1->Value();//获取测量的值disVector.push_back(distance);/*sprintf(msg,"TAG:%g",disVector[i]);uc1601(msg,1);*/}}
void ZiDongWanTou::ZhiGuan(){double l1_max, l1, l1_dj;char zght1_f[100],l1_max_f[100],l1_dj_f[100];


string  zght1_ss;//活套数量获得l1_max= btl1->GetProperties()->GetDouble("Value");//活套数量获得zght1_ss = btzght1->GetProperties()->GetEnumAsString("Value").GetLocaleText();//活套数量获得sprintf(l1_max_f,"%.1f",l1_max);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出。//6米直管长度获得double mod1;int n1,n2;
int n = vecFaceA.size()-1;for(int i=0;i<n;i++){mod1 = fmod(disVector[i],l1_max) ;n1 = disVector[i]/l1_max;//整除
n2 = (disVector[i]-150)/l1_max;//不整除l1_dj = disVector[i]-n2*l1_max;l1_max = atof(l1_max_f);
char l1_dj_f[100];sprintf(l1_dj_f,"%.1f",l1_dj);l1_dj = atof(l1_dj_f);
if(mod1==0) //取余等0,则整除,没有短节{if(n1!=0) //如果为此,则先装一个长直管{uc1601("大于0件,先装1件长管",1);ZGCreat(l1_max,zght1_ss); //创建直管}else if(n1>1){uc1601("大于1件,阵列",1);}//else(等于0)什么也不做,也不可能等于0
}
else //不整除{//uc1601("不管如何,先装短节",1);ZGCreat(l1_dj,zght1_ss); //创建直管短节ZGAssemble(i,1,vecFaceA);//这里用J=1或者J=0动态选择不同弯头
if(n2==1) //如果为此,则先装一个长直管{ZGCreat(l1_max,zght1_ss); //创建直管ZGAssemble(i,0,vecFaceB);//这里用J=1或者J=0动态选择不同弯头}else if(n2>1){ZGCreat(l1_max,zght1_ss); //创建直管ZGAssemble(i,0,vecFaceB);//这里用J=1或者J=0动态选择不同弯头ZGArray(i,n2,l1_max);
//uc1601("大于1件,阵列",1);}}
}}

void ZiDongWanTou::ZGCreat(double l1,string zght1_ss){//////////////////////////void ZhiGuan::zdy_step1()//////////////////////////
UF_initialize();tag_t parentPart = UF_PART_ask_display_part();/*d1 = btd1->GetProperties()->GetDouble("Value");l1 = btl1->GetProperties()->GetDouble("Value");//形参传入pn = btpn->GetProperties()->GetDouble("Value");//压力string ht1_ss;ht1_ss = btht1->GetProperties()->GetEnumAsString("Value").GetLocaleText();ht1_s = ht1_ss.substr(0,1);//ht1_s为字符串类型,此处获得活套数量0,1,2sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用sprintf(PN_f,"%g",pn);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出sprintf(ht1_f,"%s",ht1_s.c_str());//字符串转char*///------------------------------------------------------------------------------//定义  环境变量、原标准件库  路径 //获取环境变量地址//------------------------------------------------------------------------------//path = getenv("UGII_BASE_DIR");char *  envPath;UF_translate_variable("UGII_BASE_DIR",&envPath);string basedir1 =envPath;string basedir2 ="\\lib\\";string basePath =basedir1+basedir2;//定义  工作部件   置路径//workPath = btlibpath->Path();workPath = btlibpath->GetProperties()->GetString("Path");//获取路径,(开始已经定义为全局变量)此处是NX string类型//-----------------------------------------------------------------------//-------------定义新老零件路径名称------------------------//------------------------------------------------------------------------------char oldZGName [222];string  zght1_s;zght1_s = zght1_ss.substr(0,1);//活套数量获得sprintf(zght1_f,"%s",zght1_s.c_str());//字符串转char//活套数量获得sprintf(l1_f,"%g",l1);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出
sprintf(oldZGName,"%sZG%s-%sH%s-001.prt",basePath.c_str(),d1_f,pn_f,ht1_f);//此处basePath是sting,所以要转为char*.而char转char*,用sprintf();
sprintf(newZGName,"%s\\ZG%s-%sH%s-%s.prt",workPath.GetLocaleText(),d1_f,pn_f,zght1_f,l1_f);//l1分别代表l1_maxt和l1_dj在不同情况下的值
FILE *mmfp;       mmfp = fopen(newZGName,"r"); if(mmfp != NULL){ return;}//打开文件tag_t partTag = NULL_TAG;UF_PART_load_status_t  error_status ;UF_PART_open(oldZGName,&partTag,&error_status);//此处需要char*类型,若是string组合的需要s0.c_str()UF_PART_free_load_status(&error_status);/*uc1601(oldPartName,1);uc1601(newZGLmaxName,1);*///重命名文件UF_PART_rename(partTag,newZGName);
//-----------------------------------------------------------------------
////////////////////////////void ZhiGuan::zdy_step2()//////////////////////////
//定义  以(外径+压力)为基础的 文件名称:ZG-630-10-001//外径、长度等驱动尺寸//l1 = btl1->GetProperties()->GetDouble("Value"); // 由形参传入s1 = bts1->GetProperties()->GetDouble("Value");s2 = bts2->GetProperties()->GetDouble("Value");fb = btfb->GetProperties()->GetDouble("Value");sprintf(d1_f,"%g",d1);//生产直径参数,供生成文件名称用sprintf(l1_f,"%g",l1);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出sprintf(s1_f,"%g",s1);sprintf(s2_f,"%g",s2);sprintf(fb_f,"%g",fb);
//--------------------------------------------------------------------sprintf(d1_e,"d1=%f",d1);//生产直径参数,供生成文件名称用sprintf(l1_e,"l1=%f",l1);//%.2f小数(保留2位),e%指数,g%前两者较短,且消0输出sprintf(s1_e,"s1=%f",s1);sprintf(s2_e,"s2=%f",s2);sprintf(fb_e,"fb=%f",fb);//派生尺寸double d2 = d1-2*s1;     
//---------------------zdy_step2之后-----------------------------------------------UF_MODL_edit_exp(d1_e);UF_MODL_edit_exp(l1_e);UF_MODL_edit_exp(s1_e);UF_MODL_edit_exp(s2_e);UF_MODL_edit_exp(fb_e);
UF_MODL_update();////////////////////////////void ZhiGuan::ziLj()//子零件复制//////////////////////////
tag_t root_part_occ = NULL_TAG;tag_t displayPart = NULL_TAG;displayPart = UF_PART_ask_display_part();//显示部件的TAGroot_part_occ = UF_ASSEM_ask_root_part_occ(displayPart);//根部件的事件pat_TAG_OCC
  //int i;  int component_count = 0;  //tag_t parent = NULL_TAG;  //tag_t member = NULL_TAG;  tag_t *child_components = NULL; component_count = UF_ASSEM_ask_part_occ_children(root_part_occ,                                          &child_components);//Array of tags of child part occurrences. /* //第一种方法,事件法 tag_t parTag = UF_ASSEM_ask_prototype_of_occ(child_components[0]);//获得part tag 从occurrences tag char parNm[200]; UF_PART_ask_part_name(parTag, parNm);//从parTag获取ParName,但是这个必需组件加载*/
//第二种方法--实例法InTag for (int i = 0; i < component_count; i++) {char ziOldParNme[200];tag_t parInTag = UF_ASSEM_ask_inst_of_part_occ(child_components[i]);UF_ASSEM_ask_part_name_of_child(parInTag,ziOldParNme); //这个可以不加载组件
stringstream tn1 ;tn1<<ziOldParNme;string tn2 = tn1.str();int star1 = tn2.find_last_of("\\");int end1 = tn2.find_last_of(".");string lastName1 = tn2.substr(star1,end1-star1);
//workPath = btlibpath->Path();//获取路径char ziNewPartName [100];sprintf(ziNewPartName,"%s\\%s.prt",workPath.GetLocaleText(),lastName1.c_str());uc4567(ziOldParNme,ziNewPartName,UF_CFI_COPY_NEVER_REPLACE,2,2);/*char msg[111];sprintf(msg,"%s",ziNewPartName);uc1601(msg,1);*/ } UF_free(child_components); UF_PART_save();/////////保在零件//////////////////
UF_PART_close(UF_PART_ask_display_part(),1,1);
  UF_PART_set_display_part(parentPart); UF_terminate();}void ZiDongWanTou::ZGAssemble(int i,int j,std::vector<Face*> vecFace){UF_initialize();char msg[100];Session *theSession = Session::GetSession();Part *displayPart(theSession->Parts()->Display());Part *workPart(theSession->Parts()->Work());tag_t parentPart = UF_PART_ask_display_part();if(i<lineNum.size()-2){
line1 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针line2 =dynamic_cast<Line *>( NXObjectManager::Get(lineNum[i+1])); //dynamic_cast 将 TaggedObject对象指针转换为实际的 NX 对象指针
//添加组件Point3d basePoint1(0.0, 0.0, 0.0);Matrix3x3 orientation1;
Point3d  p11(0.0, 0.0, 0.0);Point3d  p12(0.0, 0.0, 0.0);Point3d  p21(0.0, 0.0, 0.0);Point3d  p22(0.0, 0.0, 0.0);p11 = line1->StartPoint();p12 = line1->EndPoint();p21 = line2->StartPoint();p22 = line2->EndPoint();
basePoint1 = p12;orientation1.Xx = p12.X-p11.X;orientation1.Xy = p12.Y-p11.Y;orientation1.Xz = p12.Z-p11.Z;orientation1.Yx = p21.X-p22.X;orientation1.Yy = p21.Y-p22.Y;orientation1.Yz = p21.Z-p22.Z;PartLoadStatus *partLoadStatus2;
const char * fspec1=newZGName;int ftype1=2;//.prt格式char occ_name1[100];uc4574(fspec1,ftype1,occ_name1);///分割路径为目录和文件名uc4576 ,char dspec1[100],fname1[100];stringstream tn1;tn1<<occ_name1;string tn2=tn1.str();for(int i=0;i<lineNum.size()-1;i++){compZGNum.push_back(component1);}compZGNum[i] = workPart->ComponentAssembly()->AddComponent(newZGName, "model",tn2, basePoint1, orientation1, -1, &partLoadStatus2);/*sprintf(msg,"%s",newZGName);uc1601(msg,1);*/component1 = compZGNum[i];delete partLoadStatus2;
std::vector<Face*> vecFaceZGA;//FaceA数组std::vector<Face*> vecFaceZGB;//FaceB数组std::vector<Face*> vecFaceZGC;//FaceC数组

tag_t objectOcc = NULL_TAG;int type = UF_solid_type;//可以查询line, sketch,其他的edge,feature,solid摸不清规律const char * faceZGName_A= "FACE_A" ;///一条直线也分特征名和(对象)名称,注意此处一定是对象名//const char * faceZGName_B= "FACE_B" ;///都用A端,这个就用不到了const char * faceZGName_C= "FACE_C" ;
UF_OBJ_cycle_by_name_and_type(parentPart,faceZGName_A,type, TRUE, &objectOcc);Face *faceZG_A;faceZG_A = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));vecFaceZGA.push_back((faceZG_A));
Positioning::ComponentPositioner *componentPositioner1;componentPositioner1 = workPart->ComponentAssembly()->Positioner();
componentPositioner1->ClearNetwork();componentPositioner1->BeginAssemblyConstraints();
Positioning::Network *network1;network1 = componentPositioner1->EstablishNetwork();
Positioning::ComponentNetwork *componentNetwork1(dynamic_cast<Positioning::ComponentNetwork *>(network1));componentNetwork1->SetMoveObjectsState(true);
Assemblies::Component *nullAssemblies_Component(NULL);componentNetwork1->SetDisplayComponent(nullAssemblies_Component);
//第一组
Positioning::Constraint *constraint1;constraint1 = componentPositioner1->CreateConstraint();
Positioning::ComponentConstraint *componentConstraint1(dynamic_cast<Positioning::ComponentConstraint *>(constraint1));componentConstraint1->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);componentConstraint1->SetConstraintType(Positioning::Constraint::TypeTouch);/////定义约束对象/////获取选择对象的进行动态转换(这个是成功的)/*std::vector<TaggedObject*>objects1 = this->selection01->GetProperties()->GetTaggedObjectVector("SelectedObjects");Line *line3=NULL;line3 = dynamic_cast<Line *>(objects1[0]); */
////装配进来的件就不用选择了Positioning::ConstraintReference *constraintReference1;constraintReference1 = componentConstraint1->CreateConstraintReference(component1, faceZG_A, false, false, false);
/*////获取选择对象的进行动态转换(这个是成功的)std::vector<TaggedObject*>objects2 = this->edge_select01->GetProperties()->GetTaggedObjectVector("SelectedObjects");//objects2[0]->Tag();Line *line4=NULL;line4 = dynamic_cast<Line *>(objects2[0]);*/

///定义约束对象/*Positioning::ConstraintReference *constraintReference1;constraintReference1 = componentConstraint1->CreateConstraintReference(component1, line3, false, false, false);*/
Positioning::ConstraintReference *constraintReference2;constraintReference2 = componentConstraint1->CreateConstraintReference(compNum[i+j], vecFace[i+j], false, false, false);
constraintReference2->SetFixHint(true);
componentNetwork1->Solve();
//第二组//////////////////////////必要的参数//////////////////objectOcc = NULL_TAG;UF_OBJ_cycle_by_name_and_type(parentPart,faceZGName_C,type, TRUE, &objectOcc);Face *faceZG_C;faceZG_C = dynamic_cast<Face*>(NXObjectManager::Get(objectOcc));vecFaceZGC.push_back((faceZG_C));
//////////////////////////////////////////////Positioning::Constraint *constraint2;constraint2 = componentPositioner1->CreateConstraint();
Positioning::ComponentConstraint *componentConstraint2(dynamic_cast<Positioning::ComponentConstraint *>(constraint2));componentConstraint2->SetConstraintAlignment(Positioning::Constraint::AlignmentInferAlign);componentConstraint2->SetConstraintType(Positioning::Constraint::TypeTouch);
/////获取选择对象的进行动态转换(这个是成功的)Positioning::ConstraintReference *constraintReference3;constraintReference3 = componentConstraint2->CreateConstraintReference(component1, faceZG_C,true, false, false);Positioning::ConstraintReference *constraintReference4;constraintReference4 = componentConstraint2->CreateConstraintReference(workPart->ComponentAssembly(), vecLine[i+1], false, false, false);
constraintReference4->SetFixHint(true);componentNetwork1->Solve();componentPositioner1->ClearNetwork(); componentPositioner1->EndAssemblyConstraints(); 
UF_terminate();
}
}
void ZiDongWanTou::ZGArray(int i,int n,double l1){UF_initialize();tag_t displayPart = UF_PART_ask_display_part();tag_t num1;//数量1//tag_t num2;//数量2tag_t dis1;//距离1//tag_t dis2;//距离2char  *numPref = "Num";char num_1[111],dis_1[111],num_2[111],dis_2[111];sprintf(num_1,"%s%d=%d",numPref,lineNum[i+1],n);sprintf(num_2,"%s%d_2=1",numPref,lineNum[i+1]);
UF_MODL_create_exp_tag(num_1,&num1);//注意这里参数输入需要以表达式形式//UF_MODL_create_exp_tag(num_2,&num2);///////////////////////////////////////////////const char * left_hand_side = "Num_2";bool  is_exp_in_part ;UF_MODL_is_exp_in_part(displayPart, left_hand_side,  &is_exp_in_part);if(is_exp_in_part==0){UF_MODL_create_exp_tag("Num_2=1",&num2);//注意这里参数输入需要以表达式形式}///////////////////////////////////////////////////*sprintf(msg,"%d",is_exp_in_part);uc1601(msg,1);*/char  *disPref = "Dis";sprintf(dis_1,"%s%d=%g",disPref,lineNum[i+1],l1);sprintf(dis_2,"%s%d_2=100",disPref,lineNum[i+1]);
UF_MODL_create_exp_tag(dis_1,&dis1);//创建表达式//UF_MODL_create_exp_tag(dis_2,&dis2);//////////////////////////////////////////////////UF_MODL_is_exp_in_part(displayPart, "Dis_2",  &is_exp_in_part);if(is_exp_in_part==0){UF_MODL_create_exp_tag("Dis_2=100",&dis2);//创建表达式}//////////////////////////////////////////////////////////////////////////定义参数//////////////////////UF_ASSEM_mc_array_data_t array_data;
tag_t partTag = component1->Tag();//tag_t directTag1 = lineNum[i+1];//方向1tag_t directTag1 = vecFaceB[i]->Tag();//方向1tag_t directTag2 = lineNum[i];//方向2,这个实际用不到array_data.array_subtype = UF_ASSEM_linear_array;//UF_ASSEM_circular_arrayarray_data.master_component = partTag;array_data.template_component = NULL_TAG;/*tag_t arrayNum[2]={4,NULL};*/array_data.dimensions[0] = num1;array_data.dimensions[1] = num2;
//tag_t arrayDirect[2]={69278,NULL};array_data.axis_definitions[0] = directTag1;array_data.axis_definitions[1] = directTag2;array_data.offsets[0] = dis1;array_data.offsets[1] = dis2;
char  *partNamePref = "ArraPN";char ArraPN[111];sprintf(ArraPN,"%s%d",partNamePref,lineNum[i+1]);
array_data.array_name = ArraPN;//原型array_data.array_name ="partname1"tag_t partArray_tag; ////////////////////////定义参数//////////////////////UF_ASSEM_create_mc_array(&array_data,&partArray_tag);//int k =0;//uc1601(msg,1);//char  *old_exp_name = "num1";//char  *newNamePref = "Num";//char new_Name[111] ;//sprintf(new_Name,"%s%d",newNamePref,lineNum[i+1]);//k++;//UF_MODL_rename_exp(old_exp_name, new_Name);//这里的char*和char[]是等价的//UF_free(array_data.array_name);///When this value is returned from an enquiry function, it is动态分配时 dynamically allocated, and must be freed with UF_free.这里不需要UF_terminate();}

posted @ 2021-10-31 10:58  光量子  阅读(260)  评论(0编辑  收藏  举报