Siemens官方NXOPEN实例解析:BlockStyler/HoleCoordinates


一、UI样式文件分析

1. 样式文件目录

目录位置:UGOPEN\SampleNXOpenApplications\C++\BlockStyler\HoleCoordinates\HoleCoordinates.dlx

2. 样式文件导入预览


3. 样式文件解析

本实例主要用到了一下控件:指定点、指定矢量、面收集器、操作按钮、列表框

二、源码文件解析

1. 主程序分析

extern "C" DllExport void ufusr(char *param, int *retcod, int param_len){    try    {        theHoleCoordinates = new HoleCoordinates();        // The following method shows the dialog immediately        theHoleCoordinates->Show();    }    catch(exception& ex)    {        //---- Enter your exception handling code here -----        HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());    }    delete theHoleCoordinates;} HoleCoordinates::HoleCoordinates(){    try    {        // Initialize the NX Open C++ API environment        HoleCoordinates::theSession = NXOpen::Session::GetSession();        HoleCoordinates::theUI = UI::GetUI();        theDialogName = "HoleCoordinates.dlx";        theDialog = HoleCoordinates::theUI->CreateDialog(theDialogName.c_str());        // Registration of callback functions        theDialog->AddApplyHandler(make_callback(this, &HoleCoordinates::apply_cb));        theDialog->AddUpdateHandler(make_callback(this, &HoleCoordinates::update_cb));        theDialog->AddInitializeHandler(make_callback(this, &HoleCoordinates::initialize_cb));        theDialog->AddDialogShownHandler(make_callback(this, &HoleCoordinates::dialogShown_cb));    }    catch(exception& ex)    {        std::string exceptionText = ex.what();        throw;    }}
构造函数里,我们需要关注dlx文件加载,回调方法里面主要关注:提交方法apply_cb、更新方法update_cb、初始化方法initialize_cb。

2. 处理模块分析


2.1 初始化方法

void HoleCoordinates::initialize_cb(){    try    {        workPart = HoleCoordinates::theSession->Parts()->Work();        displayPart = HoleCoordinates::theSession->Parts()->Display();        //ufsess = NXOpen::UF::UFSession->GetUFSession();        groupPoint = theDialog->TopBlock()->FindBlock("groupPoint");        origin = theDialog->TopBlock()->FindBlock("origin");        groupXdir = theDialog->TopBlock()->FindBlock("groupXdir");        xDir = theDialog->TopBlock()->FindBlock("xDir");        groupYdir = theDialog->TopBlock()->FindBlock("groupYdir");        yDir = theDialog->TopBlock()->FindBlock("yDir");        groupFace = theDialog->TopBlock()->FindBlock("groupFace");        face = theDialog->TopBlock()->FindBlock("face");        groupInfo = theDialog->TopBlock()->FindBlock("groupInfo");        buttonInfo = theDialog->TopBlock()->FindBlock("buttonInfo");        listBoxInfo = dynamic_cast<NXOpen::BlockStyler::ListBox* >(theDialog->TopBlock()->FindBlock("listBoxInfo"));         // User defined code        setEnablePropForBlock(buttonInfo, false);        setEnablePropForBlock(xDir, false);        setEnablePropForBlock(yDir, false);        setEnablePropForBlock(face, false);         refaxis1 = NULL;        refaxis2 = NULL;        highlightedFeature = NULL;     }    catch(exception& ex)    {        //---- Enter your exception handling code here -----        HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());    }}
初始化方法initialize_cb() 获取控件句柄:origin、xDir、yDir、face、buttonInfo、listBoxInfo


2.2 控件更新/点击/激活方法

int HoleCoordinates::update_cb(NXOpen::BlockStyler::UIBlock* block){    try    {        if(block == origin)        {            NXOpen::BlockStyler::PropertyList* originProperties = origin->GetProperties();            pointCol = originProperties->GetTaggedObjectVector("SelectedObjects");            delete originProperties;             axispoint = dynamic_cast<NXOpen::Point* >(pointCol[0]);            setEnablePropForBlock(xDir, true);            if (refaxis1 != NULL)             {                refaxis1 = editdatumaxis(refaxis1, dynamic_cast<NXOpen::Direction* >(vectCol[0]), axispoint);            }            if (refaxis2 != NULL)            {                refaxis2 = editdatumaxis(refaxis2, dynamic_cast<NXOpen::Direction* >(vectCol1[0]), axispoint);            }        }        else if(block == xDir)        {            NXOpen::BlockStyler::PropertyList* xDirProperties = xDir->GetProperties();            vectCol = xDirProperties->GetTaggedObjectVector("SelectedObjects");            delete xDirProperties;             if (refaxis1 == NULL)            {                refaxis1 = datumaxiscreater(axispoint, dynamic_cast<NXOpen::Direction* >(vectCol[0]));            }            else            {                refaxis1 = editdatumaxis(refaxis1, dynamic_cast<NXOpen::Direction* >(vectCol[0]), axispoint);            }            setEnablePropForBlock(yDir, true);        }        else if(block == yDir)        {            NXOpen::BlockStyler::PropertyList* yDirProperties = yDir->GetProperties();            vectCol1 = yDirProperties->GetTaggedObjectVector("SelectedObjects");            delete yDirProperties;             if (refaxis2 == NULL)            {                refaxis2 = datumaxiscreater(axispoint, dynamic_cast<NXOpen::Direction* >(vectCol1[0]));            }            else            {                refaxis2 = editdatumaxis(refaxis2, dynamic_cast<NXOpen::Direction* >(vectCol1[0]), axispoint);            }            setEnablePropForBlock(face, true);        }        else if(block == face)        {            NXOpen::BlockStyler::PropertyList* faceProperties = face->GetProperties();            faceCol = faceProperties->GetTaggedObjectVector("SelectedObjects");            delete faceProperties;             if (faceCol.size() > 0)            {                holeface = dynamic_cast<NXOpen::Face* >(faceCol[0]);                 setEnablePropForBlock(buttonInfo, true);            }            else if (faceCol.size() == 0)             {                holeface = NULL;                setEnablePropForBlock(buttonInfo, false);            }        }        else if(block == listBoxInfo)        {            NXOpen::BlockStyler::PropertyList* listBoxInfoProperties = listBoxInfo->GetProperties();            std::vector<NXOpen::NXString> str = listBoxInfoProperties->GetStrings("ListItems");            std::vector<int> index = listBoxInfoProperties->GetIntegerVector("SelectedItems");            delete listBoxInfoProperties;             if (index.size() > 0)            {                std::string selectedText(str[index[0]].GetUTF8Text());                if (selectedText.find("HOLE") != string::npos)                {                    NXOpen::Features::Feature* selectedFeature = dynamic_cast<NXOpen::Features::Feature* >(workPart->Features()->FindObject(str[index[0]]));                    if (selectedFeature != NULL)                    {                        if (index.size() > 0)                        {                            holeface->Unhighlight();                            // Comment out the following command because it is causing                             // excessive flickering                            // workPart->ModelingViews()->WorkView()->Regenerate();                        }                        if (highlightedFeature != NULL)                         {                            highlightedFeature->Unhighlight();                        }                        selectedFeature->Highlight();                        highlightedFeature = selectedFeature;                    }                }                else                {                    if (highlightedFeature != NULL)                     {                        highlightedFeature->Unhighlight();                    }                    // Comment out the following command because it is causing                     // excessive flickering                    // workPart->ModelingViews()->WorkView()->Regenerate();                }            }        }        else if(block == buttonInfo)        {            NXOpen::BlockStyler::PropertyList* listBoxInfoProperties = listBoxInfo->GetProperties();            std::vector<NXOpen::NXString> liststrings1 = listBoxInfoProperties->GetStrings("ListItems");            if (liststrings1.size() > 0)             {                liststrings1.clear();                //ReDim liststrings1(-1)                listBoxInfoProperties->SetStrings("ListItems", liststrings1);            }             bool foundholes = true;            NXOpen::Direction* dir1 = NULL;            NXOpen::Direction* dir2 = NULL;            try            {                if (vectCol.size() > 0 && vectCol1.size() > 0)                 {                    dir1 = dynamic_cast<NXOpen::Direction* >(vectCol[0]);                    dir2 = dynamic_cast<NXOpen::Direction* >(vectCol1[0]);                     // Checking for perpendicularity between vectors                    double doublearray1[3] = {dir1->Vector().X, dir1->Vector().Y, dir1->Vector().Z};                    double doublearray2[3] = {dir2->Vector().X, dir2->Vector().Y, dir2->Vector().Z};                    int result1;                    //ufsess.Vec3.IsPerpendicular(doublearray1, doublearray2, 0.000001, result1)                    UF_VEC3_is_perpendicular(doublearray1, doublearray2, 0.000001, &result1);                    if (result1 == 0)                    {                        HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, "Selected Vectors should be perpendicular");                        return 0;                    }                     UF_VEC3_is_parallel(doublearray1, doublearray2, 0.000001, &result1);                    if (result1 == 1)                     {                        HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, "Selected Vectors are parallel");                    }                    else                    {                        std::vector<NXOpen::Edge* > selectedFaceEdges = holeface->GetEdges();                         if (allholesNX5.size() > 0)                        {                            allholesNX5.clear();                            //ReDim allholesNX5(-1)                        }                        if (allholesNX6.size() > 0)                        {                            allholesNX6.clear();                            //ReDim allholesNX6(-1)                        }                         for (unsigned int ii = 0; ii < selectedFaceEdges.size(); ++ii)                         {                            NXOpen::Edge* seledge = selectedFaceEdges[ii];                            NXOpen::Features::Feature* assocFeature = workPart->Features()->GetAssociatedFeature(seledge);                            std::string featureString = std::string(assocFeature->FeatureType().GetUTF8Text());                            if (featureString.find("HOLE") != std::string::npos)                            {                                if (featureString.find("SIMPLE") != std::string::npos)                                {                                    // Found NX5 type hole feature, add to NX 5 list                                    allholesNX5.push_back(assocFeature->JournalIdentifier());                                    //allholesNX5.push_back(featureString);                                }                                else                                {                                    // Found NX6 type hole feature, add to NX 6 list                                    allholesNX6.push_back(assocFeature->JournalIdentifier());                                    //allholesNX6.push_back(featureString);                                }                            }#if 0                            NXString journalstring = seledge->JournalIdentifier();                            std::vector<std::string> individualstring = splitStrings(std::string(journalstring.GetUTF8Text()));                            for (unsigned int jj = 0; jj < individualstring.size(); ++jj)                            {                                std::string featurestring = individualstring[jj];                                if (featurestring.find("HOLE") != std::string::npos)                                {                                    if (featurestring.find("SIMPLE") != std::string::npos)                                    {                                        allholesNX5.push_back(featurestring);                                        //Array.Resize(allholesNX5, allholesNX5.Length + 1)                                        //allholesNX5.SetValue(featurestring, allholesNX5.Length - 1)                                    }                                    else                                    {                                        std::string tempString("SIMPLE ");                                        tempString.append(featurestring);                                        featurestring.clear();                                        featurestring.append(tempString);                                        allholesNX6.push_back(featurestring);                                        //Array.Resize(allholesNX6, allholesNX6.Length + 1)                                        //allholesNX6.SetValue(featurestring, allholesNX6.Length - 1)                                    }                                }                            }#endif                        }                        if (allholesNX5.size() == 0 && allholesNX6.size() == 0)                        {                            foundholes = false;                            HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, "No hole features found on this face");                            std::vector<NXOpen::TaggedObject* > tags1;                            NXOpen::BlockStyler::PropertyList* faceProperties = face->GetProperties();                            faceProperties->SetTaggedObjectVector("SelectedObjects", tags1);                            delete faceProperties;                            std::vector<NXOpen::NXString> liststrings = listBoxInfoProperties->GetStrings("ListItems");                            liststrings.clear();                            //ReDim liststrings(-1)                            listBoxInfoProperties->SetStrings("ListItems", liststrings);                             setEnablePropForBlock(buttonInfo, false);                        }                        else                        {                            if (allholesNX5.size() > 0)                            {                                for (unsigned int ii = 0; ii < allholesNX5.size(); ++ii)                                {                                    NXOpen::NXString nx5holes = allholesNX5[ii];                                    NXOpen::Features::Feature* myFeature = workPart->Features()->FindObject(nx5holes);                                    NXOpen::Features::Hole* myHoleFeature = dynamic_cast<NXOpen::Features::Hole* >(workPart->Features()->FindObject(nx5holes));                                    holelocation = myFeature->Location();                                    NXOpen::Point* point1 = workPart->Points()->CreatePoint(holelocation);                                    double xcoord = holedimension(refaxis1, refaxis2, point1);                                    double ycoord = holedimension(refaxis2, refaxis1, point1);                                    std::vector<NXOpen::NXString> holedata;                                    holedata.push_back(myFeature->JournalIdentifier());                                    std::stringstream xCoordStream;                                    xCoordStream << "X Coordinate = " << setprecision(16) << xcoord;                                    holedata.push_back(NXString(xCoordStream.str().c_str()));                                    std::stringstream yCoordStream;                                    yCoordStream << "Y Coordinate = " << setprecision(16) << ycoord;                                    holedata.push_back(NXString(yCoordStream.str().c_str()));                                    std::vector<NXOpen::NXString> liststrings = listBoxInfoProperties->GetStrings("ListItems");                                    liststrings.push_back(holedata[0]);                                    liststrings.push_back(holedata[1]);                                    liststrings.push_back(holedata[2]);                                    listBoxInfoProperties->SetStrings("ListItems", liststrings);                                }                            }                            if (allholesNX6.size() > 0)                            {                                for (unsigned int ii = 0; ii < allholesNX6.size(); ++ii)                                {                                    NXOpen::Features::BodyFeature* myFeature = dynamic_cast<NXOpen::Features::BodyFeature* >(workPart->Features()->FindObject(allholesNX6[ii]));                                    NXOpen::Features::HolePackage* myHolePackage = dynamic_cast<NXOpen::Features::HolePackage* >(myFeature);                                    //Point3d holeorigins() As = Nothing                                    NXOpen::Point* point1 = workPart->Points()->CreatePoint(myFeature->Location());                                    double xcoord = holedimension(refaxis1, refaxis2, point1);                                    double ycoord = holedimension(refaxis2, refaxis1, point1);                                    std::vector<NXOpen::NXString> holedata1;                                    holedata1.push_back(myFeature->JournalIdentifier());                                    std::stringstream xCoordStream;                                    xCoordStream << setprecision(16) << "X Coordinate = " << xcoord;                                    holedata1.push_back(NXString(xCoordStream.str().c_str()));                                    std::stringstream yCoordStream;                                    yCoordStream << setprecision(16) << "Y Coordinate = " << ycoord;                                    holedata1.push_back(NXString(yCoordStream.str().c_str()));                                    std::vector<NXOpen::NXString> liststrings = listBoxInfoProperties->GetStrings("ListItems");                                    liststrings.push_back(holedata1[0]);                                    liststrings.push_back(holedata1[1]);                                    liststrings.push_back(holedata1[2]);                                    listBoxInfoProperties->SetStrings("ListItems", liststrings);                                }                            }                        }                    }                }                else                {                    HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError,                         "Vectors not defined, please define the vectors for X and Y direction");                }                delete listBoxInfoProperties;            }            catch(exception& ex)            {                //---- Enter your exception handling code here -----                HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());             }        }    }    catch(exception& ex)    {        //---- Enter your exception handling code here -----        HoleCoordinates::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());    }    return 0;}
这里涉及到origin、xDir、yDir、face、buttonInfo、listBoxInfo控件的Update方法 1、origin 获取指定点坐标 pointCol = originProperties->GetTaggedObjectVector("SelectedObjects"); axispoint = dynamic_cast<NXOpen::Point* >(pointCol[0]); 2、xDir、yDir 分别设置x和y的矢量方向,同时创建对应的基准轴 vectCol = xDirProperties->GetTaggedObjectVector("SelectedObjects"); refaxis1 = datumaxiscreater(axispoint, dynamic_cast<NXOpen::Direction* >(vectCol[0])); vectCol1 = yDirProperties->GetTaggedObjectVector("SelectedObjects"); refaxis2 = datumaxiscreater(axispoint, dynamic_cast<NXOpen::Direction* >(vectCol1[0])); 3、face 用来指定选着面 faceCol = faceProperties->GetTaggedObjectVector("SelectedObjects"); holeface = dynamic_cast<NXOpen::Face* >(faceCol[0]); 4、listBoxInfo 主要用来显示孔信息,并且在选着列表中的对象的时候,ug会激活选中状态 std::vector<NXOpen::NXString> str = listBoxInfoProperties->GetStrings("ListItems"); std::vector<int> index = listBoxInfoProperties->GetIntegerVector("SelectedItems"); 5、buttonInfo 用来获取孔的信息,并显示在listBoxInfo列表,获取孔信息的主要方法还是遍历特征,如下: NXOpen::Edge* seledge = selectedFaceEdges[ii]; NXOpen::Features::Feature* assocFeature = workPart->Features()->GetAssociatedFeature(seledge); std::string featureString = std::string(assocFeature->FeatureType().GetUTF8Text()); if (featureString.find("HOLE") != std::string::npos) { if (featureString.find("SIMPLE") != std::string::npos) { // Found NX5 type hole feature, add to NX 5 list allholesNX5.push_back(assocFeature->JournalIdentifier()); //allholesNX5.push_back(featureString); } else { // Found NX6 type hole feature, add to NX 6 list allholesNX6.push_back(assocFeature->JournalIdentifier()); //allholesNX6.push_back(featureString); } }

3. 运行结果截图

总结


1、前端BlockUI使用控件:指定点、指定矢量、面收集器、操作按钮、列表框,主要知识点如下:

1)指定点、面收集器的使用及获取点坐标和面信息的方法

2)指定矢量的使用,初始化和获取选择的矢量方向

3)列表框的初始化和赋值方法


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空