zoukankan      html  css  js  c++  java
  • NXOpen 重命名装配组件

    可在装配树选择部件改名,也可以直接选择部件改名。

    VS2015 NX12


    ======================================================================================

    HPP

    ======================================================================================

    #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_Group.hxx>
    #include <NXOpen/BlockStyler_SelectObject.hxx>
    #include <NXOpen/BlockStyler_Enumeration.hxx>
    #include <NXOpen/BlockStyler_StringBlock.hxx>
    #include <NXOpen/BlockStyler_Toggle.hxx>

    //用户头文件
    #include <NXOpen/Part.hxx>
    #include <NXOpen/BasePart.hxx>
    #include <NXOpen/PartCollection.hxx>
    #include <NXOpen/Assemblies_Component.hxx>
    #include <NXOpen/Assemblies_ComponentAssembly.hxx>
    #include <NXOpen/DisplayManager.hxx>
    #include <NXOpen/DisplayModification.hxx>
    #include <NXOpen/DisplayableObject.hxx>
    #include <NXOpen/PartCleanup.hxx>
    #include <NXOpen/NXObjectManager.hxx>
    #include <uf.h>
    #include <uf_ui.h>
    #include <string.h>
    #include <uf_modl.h>
    #include <uf_part.h>
    #include <uf_curve.h>
    #include <uf_assem.h>
    #include <uf_obj.h>
    #include <windows.h>
    #include <stdarg.h>
    #include <strstream>
    #include <sstream>
    #include <io.h>
    #undef CreateDialog
    #include <fstream>
    #include <NXOpen/ListingWindow.hxx>

    //用户代码(定义函数)
    void PartCleanup();// 执行部分清理并关闭高亮显示
    std::vector<NXOpen::TaggedObject *> theComponentSelect; //定义选择控件的容器
    NXOpen::Assemblies::Component *originalcomponent1;//定义选中的部件
    Part *originalpart;//定义选中的part
    NXString originalpartfullpath;//选中部件原来的全名(包括路径)
    string PartOriginalName;//选中部件原来的名字(不包括路径)
    string strDir;//选中部件所在的路径(不包括部件名)
    void setUI();//设置

    int DelectValue;//对话框返回值,决定是否删除原组件
    string NameOrPath(const char* Path, int Type);//返回.prt文件名字和所在文件夹路径

    bool isSameName; //是否同名
    string NewName;//新部件名字
    string partNewFullName;//部件新名(全路径)
    std::vector<string> samepartname1;
    void Cylmemory();//遍历内存并判断文件是否存在

    void Componentrename();//改名

    std::vector<string> NewNamePath1;//新部件全路径


    ======================================================================================

    CPP

    ======================================================================================

    void RenameComponent::initialize_cb()
    {
    try
    {
    group03 = dynamic_cast<NXOpen::BlockStyler::Group*>(theDialog->TopBlock()->FindBlock("group03"));
    selComponent = dynamic_cast<NXOpen::BlockStyler::SelectObject*>(theDialog->TopBlock()->FindBlock("selComponent"));
    enum0 = dynamic_cast<NXOpen::BlockStyler::Enumeration*>(theDialog->TopBlock()->FindBlock("enum0"));
    stringOriginal = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringOriginal"));
    stringPath = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringPath"));
    toggle0 = dynamic_cast<NXOpen::BlockStyler::Toggle*>(theDialog->TopBlock()->FindBlock("toggle0"));
    stringNew = dynamic_cast<NXOpen::BlockStyler::StringBlock*>(theDialog->TopBlock()->FindBlock("stringNew"));
    //------------------------------------------------------------------------------
    //Registration of StringBlock specific callbacks
    //------------------------------------------------------------------------------
    //stringOriginal->SetKeystrokeCallback(make_callback(this, &RenameComponent::KeystrokeCallback));

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

    //用户代码
    theSession->ListingWindow()->Open();

    //设置选择掩码,只选择组件
    NXOpen::Selection::SelectionAction action = Selection::SelectionActionClearAndEnableSpecific;
    std::vector<NXOpen::Selection::MaskTriple>selectionMask_array(2);
    selectionMask_array[0].Type = UF_component_type;
    selectionMask_array[0].Subtype = UF_component_subtype;
    selectionMask_array[1].Type = UF_component_type;
    selectionMask_array[1].Subtype = UF_part_occurrence_subtype;

    NXOpen::BlockStyler::PropertyList *selComponentProps = selComponent->GetProperties();
    selComponentProps->SetSelectionFilter("SelectionFilter", action, selectionMask_array);
    delete selComponentProps;
    selComponentProps = NULL;

    //设置enum0默认值
    //enum0->GetProperties()->SetEnum("Value", 0 );

    //由对枚举值判断是否删除原组件
    DelectValue = enum0->GetProperties()->GetEnum("Value");

    stringOriginal->GetProperties()->SetString("Label", "原名:"); //修改按钮显示的内容 联系QQ:3132070

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

    //------------------------------------------------------------------------------
    //Callback Name: apply_cb
    //------------------------------------------------------------------------------
    int RenameComponent::apply_cb()
    {
    int errorCode = 0;
    try
    {
    //---- Enter your callback code here -----
    Cylmemory();//遍历内存并判断文件是否存在
    Componentrename();//改名
    }
    catch(exception& ex)
    {
    //---- Enter your exception handling code here -----
    errorCode = 1;
    RenameComponent::theUI->NXMessageBox()->Show("Block Styler", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    return errorCode;
    }

    //------------------------------------------------------------------------------
    //Callback Name: update_cb
    //------------------------------------------------------------------------------
    int RenameComponent::update_cb(NXOpen::BlockStyler::UIBlock* block)
    {
    try
    {
    if (block == selComponent)
    {
    //---------Enter your code here-----------
    //用户代码

    setUI();

    }
    else if (block == enum0)
    {
    //---------Enter your code here-----------

    //由对枚举值判断是否删除原组件
    DelectValue = enum0->GetProperties()->GetEnum("Value");

    setUI();


    }
    else if (block == stringOriginal)
    {
    //---------Enter your code here-----------
    }
    else if (block == stringPath)
    {
    //---------Enter your code here-----------
    }
    else if (block == toggle0)
    {
    //---------Enter your code here-----------
    //用户代码
    //PartCleanup();
    setUI();

    }
    else if (block == stringNew)
    {
    //---------Enter your code here-----------
    //Cylmemory();//遍历内存并判断文件是否存在


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

    //执行部分清理并关闭高亮显示
    void RenameComponent::PartCleanup()
    {
    try
    {
    NXOpen::PartCleanup *partCleanup1;
    partCleanup1 = theSession->NewPartCleanup();
    partCleanup1->SetTurnOffHighlighting(true);
    partCleanup1->DoCleanup();
    delete partCleanup1;
    partCleanup1 = NULL;
    }
    catch (exception& ex)
    {
    //---- Enter your exception handling code here -----
    RenameComponent::theUI->NXMessageBox()->Show("执行部分清理并关闭高亮显示", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }
    }

    //获取文件名和路径
    string RenameComponent::NameOrPath(const char* Path, int Type)
    {
    try
    {
    //反向找位置,分割字符串(只读取文件夹路径)
    string strPath = Path;
    string strDir;
    int nPos = strPath.find_last_of('\');
    if (string::npos != nPos)
    {
    strDir = strPath.substr(0, nPos);
    }

    //分割字符串(只读取part名字+后缀)
    //方法2
    int pos = strPath.find_last_of('\');
    string s1(strPath.substr(pos + 1));

    //分割字符串(只读取part名)
    string PartName(s1.substr(0, s1.find(".prt")));

    if (Type == 1)
    {
    return PartName;//返回名字
    }
    else if (Type == 2)
    {
    return strDir;//返回文件夹路径
    }

    }
    catch (exception& ex)
    {
    //---- Enter your exception handling code here -----
    RenameComponent::theUI->NXMessageBox()->Show("获取文件名和路径", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }


    }


    //设置
    void RenameComponent::setUI()
    {
    try
    {
    //---- Enter your callback code here -----

    UF_initialize(); //入口

    //获取选择的组件

    theComponentSelect.clear();

    PropertyList* ComponentSelectProps = selComponent->GetProperties();
    theComponentSelect = ComponentSelectProps->GetTaggedObjectVector("SelectedObjects");
    delete ComponentSelectProps;
    ComponentSelectProps = NULL;

    //获取开关控件
    PropertyList* ToggleProps = toggle0->GetProperties();
    int ToggleValue = ToggleProps->GetLogical("Value");
    delete ToggleProps;
    ToggleProps = NULL;

    //设置原名/路径UI 和 获部选中部件的名和路径
    if (theComponentSelect.size() == 1)
    {
    originalcomponent1 = (dynamic_cast<NXOpen::Assemblies::Component *>(NXOpen::NXObjectManager::Get(theComponentSelect[0]->Tag())));
    originalpart = dynamic_cast<Part *>(originalcomponent1->Prototype()); //component强制转换为part

    originalpartfullpath = originalpart->FullPath();
    //theSession->ListingWindow()->WriteLine(originalpartfullpath);

    //转换
    char part_name_fullpath[133];
    sprintf(part_name_fullpath, "%s", originalpartfullpath.GetLocaleText());
    //分割字符串(只读取part名)
    PartOriginalName = NameOrPath(part_name_fullpath, 1);
    //截取路径
    strDir = NameOrPath(originalpartfullpath.GetLocaleText(), 2);
    stringOriginal->SetValue(PartOriginalName.c_str());
    stringPath->SetValue(strDir);
    //获取文件夹路径

    }

    else if (theComponentSelect.size() == 0)

    {
    stringOriginal->SetValue("");
    stringPath->SetValue("");
    //theSession->ListingWindow()->WriteLine("没有");
    }

    //设置新名输入框UI
    if (ToggleValue == 1 && theComponentSelect.size() == 1)
    {
    stringNew->SetValue(PartOriginalName.c_str());
    }

    else if (ToggleValue == 0 || theComponentSelect.size() == 0)

    {
    stringNew->SetValue("");
    }


    UF_terminate(); //终止

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

    //遍历内存并判断文件是否存在
    void RenameComponent::Cylmemory()
    {
    try
    {
    isSameName = false;

    //获取新名字
    PropertyList *stringNewProps = stringNew->GetProperties();
    NXString thestringNew = stringNewProps->GetString("Value");
    delete stringNewProps;
    stringNewProps = NULL;
    NewName = thestringNew.GetLocaleText();
    //theSession->ListingWindow()->WriteLine("部件新名:"+thestringNew);

    string f1 = "\";
    string f2 = ".prt";
    partNewFullName = strDir + f1 + NewName + f2;//部件新名(全路径)
    //theSession->ListingWindow()->WriteLine("部件新全路径:"+partNewFullName);

    //测试文件或目录是否存在
    UF_initialize();
    int partNewFullNametatus = 0;
    UF_CFI_ask_file_exist(partNewFullName.c_str(), &partNewFullNametatus); //partNewFullNametatus 检测文件是否存在,0为存在,1为不存在
    UF_terminate();

    //通过迭代器获取内存加载部件的名
    samepartname1.clear();
    PartCollection *Pc = theSession->Parts();
    for (PartCollection::iterator ite = Pc->begin(); ite != Pc->end(); ite++)
    {
    NXOpen::BasePart *part = (*ite);
    NXString partfullpath2 = part->FullPath();
    //theSession->ListingWindow()->WriteLine("部件路径:"+partfullpath2);
    //分割字符串得到内存加载的部件名字
    string currPartName1 = NameOrPath(partfullpath2.GetLocaleText(), 1);
    //theSession->ListingWindow()->WriteLine("部件名:"+currPartName1);

    //新建的部件名字和内存加载部件的名字对比,相同就把内存加载部件的名字塞到容器
    if (currPartName1 == NewName || partNewFullNametatus == 0) //partNewFullNametatus 检测文件是否存在,0为存在,1为不存在
    {
    isSameName = true;
    theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "部件名已存在:" + NewName);
    return;
    }

    }

    }
    catch (exception& ex)
    {
    //---- Enter your exception handling code here -----
    RenameComponent::theUI->NXMessageBox()->Show("遍历文件夹", NXOpen::NXMessageBox::DialogTypeError, ex.what());
    }

    }

    //改名
    void RenameComponent::Componentrename()
    {
    try
    {
    if (isSameName == false && theComponentSelect.size() == 1)
    {

    //找到原部件,另存为新名字
    Part *part1(dynamic_cast<Part *>(theSession->Parts()->FindObject(originalcomponent1->DisplayName())));
    PartSaveStatus *partSaveStatus1;
    partSaveStatus1 = part1->SaveAs(partNewFullName);
    delete partSaveStatus1;
    //theSession->ListingWindow()->WriteLine(partNewFullName);

    if (DelectValue == 0)
    {
    string oldfile = originalpartfullpath.GetLocaleText();
    //theSession->ListingWindow()->WriteLine(oldfile);
    //删除旧名字的.prt文件
    DeleteFile(oldfile.c_str());
    }
    else if (DelectValue == 1)
    {
    theUI->NXMessageBox()->Show("提示", NXOpen::NXMessageBox::DialogTypeInformation, "请注意原文件没有删除");

    }
    }
    else if (isSameName == true && theComponentSelect.size() == 0)
    {
    return;
    }

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

    }

     

    怡宁塑胶模具设计
  • 相关阅读:
    Go 模板
    使用Go开发web服务器
    CLI:使用Go开发命令行应用
    MyBatis 注解使用动态SQL
    Tomcat 使用Redis存储Session
    [翻译] java NIO Buffer
    [翻译] java NIO Channel
    [翻译]java nio 概述
    [翻译] java NIO 教程---介绍
    接口的定义常量与使用
  • 原文地址:https://www.cnblogs.com/hqsalanhuang/p/14260153.html
Copyright © 2011-2022 走看看