自动弯头(米)

//==============================================================================
// WARNING!! This file is overwritten by the Block UI Styler while generating
// the automation code. Any modifications to this file will be lost after
// generating the code again.
//
// Filename: D:\BTools\lib\ZiDongWanTou.cpp
//
// This file was generated by the NX Block UI Styler
// Created by: Administrator
// Version: NX 8
// Date: 10-08-2021 (Format: mm-dd-yyyy)
// Time: 10:53 (Format: hh-mm)
//
//==============================================================================

//==============================================================================
// Purpose: This TEMPLATE file contains C++ source to guide you in the
// construction of your Block application dialog. The generation of your
// dialog file (.dlx extension) is the first step towards dialog construction
// within NX. You must now create a NX Open application that
// utilizes this file (.dlx).
//
// The information in this file provides you with the following:
//
// 1. Help on how to load and display your Block UI Styler dialog in NX
// using APIs provided in NXOpen.BlockStyler namespace
// 2. The empty callback methods (stubs) associated with your dialog items
// have also been placed in this file. These empty methods have been
// created simply to start you along with your coding requirements.
// The method name, argument list and possible return values have already
// been provided for you.
//==============================================================================

//------------------------------------------------------------------------------
//These includes are needed for the following template code
//------------------------------------------------------------------------------
#include "ZiDongWanTou.hpp"
using namespace NXOpen;
using namespace NXOpen::BlockStyler;

//------------------------------------------------------------------------------
// Initialize static variables
//------------------------------------------------------------------------------
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;
long double ang;
double d1,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;
//------------------------------------------------------------------------------
// Constructor for NX Styler class
//------------------------------------------------------------------------------
ZiDongWanTou::ZiDongWanTou()
{
try
{
// Initialize the NX Open C++ API environment
ZiDongWanTou::theSession = NXOpen::Session::GetSession();
ZiDongWanTou::theUI = UI::GetUI();
theDlxFileName = "ZiDongWanTou.dlx";
theDialog = ZiDongWanTou::theUI->CreateDialog(theDlxFileName);
// Registration of callback functions
theDialog->AddApplyHandler(make_callback(this, &ZiDongWanTou::apply_cb));
theDialog->AddOkHandler(make_callback(this, &ZiDongWanTou::ok_cb));
theDialog->AddUpdateHandler(make_callback(this, &ZiDongWanTou::update_cb));
theDialog->AddInitializeHandler(make_callback(this, &ZiDongWanTou::initialize_cb));
theDialog->AddDialogShownHandler(make_callback(this, &ZiDongWanTou::dialogShown_cb));
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
throw;
}
}

//------------------------------------------------------------------------------
// Destructor for NX Styler class
//------------------------------------------------------------------------------
ZiDongWanTou::~ZiDongWanTou()
{
if (theDialog != NULL)
{
delete theDialog;
theDialog = NULL;
}
}
//------------------------------- DIALOG LAUNCHING ---------------------------------
//
// Before invoking this application one needs to open any part/empty part in NX
// because of the behavior of the blocks.
//
// Make sure the dlx file is in one of the following locations:
// 1.) From where NX session is launched
// 2.) $UGII_USER_DIR/application
// 3.) For released applications, using UGII_CUSTOM_DIRECTORY_FILE is highly
// recommended. This variable is set to a full directory path to a file
// containing a list of root directories for all custom applications.
// e.g., UGII_CUSTOM_DIRECTORY_FILE=$UGII_ROOT_DIR\menus\custom_dirs.dat
//
// You can create the dialog using one of the following way:
//
// 1. USER EXIT
//
// 1) Create the Shared Library -- Refer "Block UI Styler programmer's guide"
// 2) Invoke the Shared Library through File->Execute->NX Open menu.
//
//------------------------------------------------------------------------------
extern "C" DllExport void ufusr(char *param, int *retcod, int param_len)
{
ZiDongWanTou *theZiDongWanTou = NULL;
try
{
theZiDongWanTou = new ZiDongWanTou();
// The following method shows the dialog immediately
theZiDongWanTou->Show();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
if(theZiDongWanTou != NULL)
{
delete theZiDongWanTou;
theZiDongWanTou = NULL;
}
}

//------------------------------------------------------------------------------
// This method specifies how a shared image is unloaded from memory
// within NX. This method gives you the capability to unload an
// internal NX Open application or user exit from NX. Specify any
// one of the three constants as a return value to determine the type
// of unload to perform:
//
//
// Immediately : unload the library as soon as the automation program has completed
// Explicitly : unload the library from the "Unload Shared Image" dialog
// AtTermination : unload the library when the NX session terminates
//
//
// NOTE: A program which associates NX Open applications with the menubar
// MUST NOT use this option since it will UNLOAD your NX Open application image
// from the menubar.
//------------------------------------------------------------------------------
extern "C" DllExport int ufusr_ask_unload()
{
//return (int)Session::LibraryUnloadOptionExplicitly;
return (int)Session::LibraryUnloadOptionImmediately;
//return (int)Session::LibraryUnloadOptionAtTermination;
}

//------------------------------------------------------------------------------
// Following method cleanup any housekeeping chores that may be needed.
// This method is automatically called by NX.
//------------------------------------------------------------------------------
extern "C" DllExport void ufusr_cleanup(void)
{
try
{
//---- Enter your callback code here -----
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

int ZiDongWanTou::Show()
{
try
{
theDialog->Show();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}

//------------------------------------------------------------------------------
//---------------------Block UI Styler Callback Functions--------------------------
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//Callback Name: initialize_cb
//------------------------------------------------------------------------------
void ZiDongWanTou::initialize_cb()
{
try
{
wizard = dynamic_cast<NXOpen::BlockStyler::Wizard*>(theDialog->TopBlock()->FindBlock("wizard"));
Step01 = theDialog->TopBlock()->FindBlock("Step01");
group1 = theDialog->TopBlock()->FindBlock("group1");
edge_select0 = theDialog->TopBlock()->FindBlock("edge_select0");
btd1 = theDialog->TopBlock()->FindBlock("btd1");
btpn = theDialog->TopBlock()->FindBlock("btpn");
separator0 = theDialog->TopBlock()->FindBlock("separator0");
btht1 = theDialog->TopBlock()->FindBlock("btht1");
separator01 = theDialog->TopBlock()->FindBlock("separator01");
btlibpath = theDialog->TopBlock()->FindBlock("btlibpath");
separator03 = theDialog->TopBlock()->FindBlock("separator03");
btl1 = theDialog->TopBlock()->FindBlock("btl1");
btzght1 = theDialog->TopBlock()->FindBlock("btzght1");
Step02 = theDialog->TopBlock()->FindBlock("Step02");
btgg1 = theDialog->TopBlock()->FindBlock("btgg1");
btxh1 = theDialog->TopBlock()->FindBlock("btxh1");
btpny1 = theDialog->TopBlock()->FindBlock("btpny1");
separator04 = theDialog->TopBlock()->FindBlock("separator04");
btang = theDialog->TopBlock()->FindBlock("btang");
bts1 = theDialog->TopBlock()->FindBlock("bts1");
bts2 = theDialog->TopBlock()->FindBlock("bts2");
btr1 = theDialog->TopBlock()->FindBlock("btr1");
btr2 = theDialog->TopBlock()->FindBlock("btr2");
btfb = theDialog->TopBlock()->FindBlock("btfb");
separator05 = theDialog->TopBlock()->FindBlock("separator05");
btfb1 = theDialog->TopBlock()->FindBlock("btfb1");
btR = theDialog->TopBlock()->FindBlock("btR");
btby1 = theDialog->TopBlock()->FindBlock("btby1");
btby2 = theDialog->TopBlock()->FindBlock("btby2");
group = theDialog->TopBlock()->FindBlock("group");
btgz = theDialog->TopBlock()->FindBlock("btgz");
btjz = theDialog->TopBlock()->FindBlock("btjz");
btzz = theDialog->TopBlock()->FindBlock("btzz");
drawingArea0 = theDialog->TopBlock()->FindBlock("drawingArea0");
separator02 = theDialog->TopBlock()->FindBlock("separator02");
//------------------------------------------------------------------------------
//Registration of Wizard specific callbacks
//------------------------------------------------------------------------------
//wizard->SetStepNotifyPreHandler(make_callback(this, &ZiDongWanTou::StepNotifyPreCallback));;

//wizard->SetStepNotifyPostHandler(make_callback(this, &ZiDongWanTou::StepNotifyPostCallback));;

//wizard->SetIsStepOkayHandler(make_callback(this, &ZiDongWanTou::IsStepOkayCallback));;

//wizard->SetOnSubNodeHandler(make_callback(this, &ZiDongWanTou::OnSubNodeCallback));;

//wizard->SetOnMenuHandler(make_callback(this, &ZiDongWanTou::OnMenuCallback));;

//wizard->SetOnMenuSelectionHandler(make_callback(this, &ZiDongWanTou::OnMenuSelectionCallback));;

}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//------------------------------------------------------------------------------
//Callback Name: dialogShown_cb
//This callback is executed just before the dialog launch. Thus any value set
//here will take precedence and dialog will be launched showing that value.
//------------------------------------------------------------------------------
void ZiDongWanTou::dialogShown_cb()
{
try
{
//---- Enter your callback code here -----
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
}

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
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, "SS", 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;
}

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int ZiDongWanTou::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
try
{
if(block == edge_select0)
{
//---------Enter your code here-----------
}
else if(block == btd1)
{
//---------Enter your code here-----------
}
else if(block == btpn)
{
//---------Enter your code here-----------
}
else if(block == separator0)
{
//---------Enter your code here-----------
}
else if(block == btht1)
{
//---------Enter your code here-----------
}
else if(block == separator01)
{
//---------Enter your code here-----------
}
else if(block == btlibpath)
{
//---------Enter your code here-----------
}
else if(block == separator03)
{
//---------Enter your code here-----------
}
else if(block == btl1)
{
//---------Enter your code here-----------
}
else if(block == btzght1)
{
//---------Enter your code here-----------
}
else if(block == btgg1)
{
//---------Enter your code here-----------
}
else if(block == btxh1)
{
//---------Enter your code here-----------
}
else if(block == btpny1)
{
//---------Enter your code here-----------
}
else if(block == separator04)
{
//---------Enter your code here-----------
}
else if(block == btang)
{
//---------Enter your code here-----------
}
else if(block == bts1)
{
//---------Enter your code here-----------
}
else if(block == bts2)
{
//---------Enter your code here-----------
}
else if(block == btr1)
{
//---------Enter your code here-----------
}
else if(block == btr2)
{
//---------Enter your code here-----------
}
else if(block == btfb)
{
//---------Enter your code here-----------
}
else if(block == separator05)
{
//---------Enter your code here-----------
}
else if(block == btfb1)
{
//---------Enter your code here-----------
}
else if(block == btR)
{
//---------Enter your code here-----------
}
else if(block == btby1)
{
//---------Enter your code here-----------
}
else if(block == btby2)
{
//---------Enter your code here-----------
}
else if(block == btgz)
{
//---------Enter your code here-----------
}
else if(block == btjz)
{
//---------Enter your code here-----------
}
else if(block == btzz)
{
//---------Enter your code here-----------
}
else if(block == drawingArea0)
{
//---------Enter your code here-----------
}
else if(block == separator02)
{
//---------Enter your code here-----------
}
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return 0;
}

//------------------------------------------------------------------------------
//Callback Name: ok_cb
//------------------------------------------------------------------------------
int ZiDongWanTou::ok_cb()
{
int errorCode = 0;
try
{
errorCode = apply_cb();
}
catch(exception& ex)
{
//---- Enter your exception handling code here -----
errorCode = 1;
ZiDongWanTou::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
}
return errorCode;
}
//------------------------------------------------------------------------------
//Wizard specific callbacks
//------------------------------------------------------------------------------
//int ZiDongWanTou::StepNotifyPreCallback(Wizard* wizard, int nextStep)
//{
//}

//void ZiDongWanTou::StepNotifyPostCallback(Wizard* wizard, int previousStep)
//{
//}

//bool ZiDongWanTou::IsStepOkayCallback(Wizard* wizard, int step)
//{
//}

//void ZiDongWanTou::OnSubNodeCallback(Wizard* wizard, int step, int subNodeId, Wizard::SubNodeAction action)
//{
//}

//void ZiDongWanTou::OnMenuCallback(Wizard* wizard, Wizard::TaskNavigatorItem item, int step, int subNodeId)
//{
//}

//void ZiDongWanTou::OnMenuSelectionCallback(Wizard* wizard, Wizard::TaskNavigatorItem item, int step, int subNodeId, int commandIndex)
//{
//}


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,"%.0f",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,"%.0f",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=%.9f",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();//获取测量的值

distance = distance*1000;//毫米变为米

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, "SS",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::AlignmentContraAlign); ///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/1000); ///阵列毫米转换为米
sprintf(dis_2,"%s%d_2=1",disPref,lineNum[i+1]); ////这里默认为1吧,不要太大,否则乘1000,超差

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=1",&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();
}

 

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

#include <uf_defs.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_ui_types.h>
#include <iostream>
#include <uf_assem.h>
#include <UF_MODL.h>
#include <UF_OBJ.h>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/ListingWindow.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/LineCollection.hxx>
#include <NXOpen/NXObject.hxx>
#include <uf_part.h> //Part头文件
#include <NXOpen/Part.hxx> //Part头文件
#include <NXOpen/PartCollection.hxx> //Part头文件
#include <NXOpen/Positioning_ComponentConstraint.hxx>
#include <NXOpen/Positioning_ComponentNetwork.hxx>
#include <NXOpen/Positioning_ComponentPositioner.hxx>
#include <NXOpen/Positioning_Constraint.hxx>
#include <NXOpen/Positioning_ConstraintReference.hxx>
#include <NXOpen/Positioning_Network.hxx>
#include <NXOpen/Positioning_Positioner.hxx>
#include <NXOpen/Preferences_SessionAssemblies.hxx>
#include <NXOpen/Preferences_SessionPreferences.hxx>
#include <NXOpen/Sketch.hxx>
#include <NXOpen/SketchCollection.hxx>
#include <NXOpen/Update.hxx>
#include <NXOpen/Assemblies_Component.hxx>
#include <NXOpen/Assemblies_ComponentAssembly.hxx>
#include <NXOpen/Body.hxx>
#include <NXOpen/BodyCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Line.hxx>
#include <NXOpen/Part.hxx> //有关部件头文件
#include <NXOpen/PartCollection.hxx> //有关部件头文件
#include <NXOpen/Features_Feature.hxx> //有关特征头文件
#include <NXOpen/Features_CylinderBuilder.hxx> //有关特征头文件
#include <NXOpen/Features_FeatureCollection.hxx>//有关特征头文件
#include <NXOpen/Features_FeatureBuilder.hxx> //有关特征头文件
#include <NXOpen/Features_AssociativeLine.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/DisplayableObject.hxx>
#include <NXOpen/DisplayManager.hxx>
#include <NXOpen/Expression.hxx> //表达式
#include <NXOpen/ExpressionCollection.hxx> //表达式
#include <NXOpen/Measure.hxx> //measrue测量
#include <NXOpen/MeasureDistance.hxx>
#include <NXOpen/MeasureDistanceBuilder.hxx>
#include <NXOpen/MeasureAngle.hxx> //measrue测量
#include <NXOpen/MeasureAngleBuilder.hxx> //measrue测量
#include <NXOpen/MeasureManager.hxx> //measrue测量
#include <NXOpen/SelectDisplayableObject.hxx> //measrue测量
#include <NXOpen/SelectObject.hxx> //measrue测量
#include <NXOpen/TaggedObject.hxx> //measrue测量
#include <NXOpen/Unit.hxx> //measrue测量
#include <NXOpen/UnitCollection.hxx> //measrue测量
#include <NXOpen/DirectionCollection.hxx>
#include <NXOpen/BlockStyler_Wizard.hxx>
#include <sstream>
#include <uf_defs.h>
#include <uf_ui_types.h>
#include <iostream>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
#include <NXOpen/BlockStyler_Wizard.hxx>
#include <uf.h>
#include <uf_ui.h>
#include <uf_part.h>
#include <string.h>
#include <uf_modl.h>
#include <uf_modl_primitives.h>
#include <uf_curve.h>
#include <uf_obj.h>
#include <uf_attr.h>
#include <uf_assem.h>
#include <sstream>

void initialize_cb();
void dialogShown_cb();
int apply_cb();
int ok_cb();
int update_cb(NXOpen::BlockStyler::UIBlock* block);
void cycleLine();
void assemPart();
void zdy_step1();
void zdy_step2();
void ziLj();
void FaceAppend(int n);
void MeasueDisance(std::vector<Face*> vecFaceA,std::vector<Face*> vecFaceB);
void ZhiGuan();
void ZGCreat(double l1,string zght1_s);
void ZGAssemble(int i,int j,std::vector<Face*> vecFace);
void ZGArray(int i,int n,double l1);

posted @ 2022-03-13 09:03  光量子  阅读(261)  评论(0编辑  收藏  举报