UG二次开发之快速重量计算

开发思路:


1  首先在本地文件夹下建一个文本,格式.dat; 存放金属密度


格式:

灰口铸铁,7.4
可锻铸铁,7.4
铸钢,7.8
碳素钢,7.85
锰钢,7.81
15CrA铬钢,7.74
38CrA铬钢,7.80
高强度合金钢,7.82
轴承钢,7.81
Gr14,7.70
黄铜,8.7
超硬铝,2.85
硬铝,2.76

2 UI设计: 两个字符串控件,分别是材料和密度,代码实现 材料读取 左边的汉字,密度读取右边的数字。

3 通过用户指定相应的材料,通过函数获得 所选择体的tag,获得体的体积,通过计算输出信息窗口

难点在于: 怎样读取文本中的数据,在一个就是更新问题

代码:

void zljs::dialogShown_cb()
{
    try
    {
        //---- Enter your callback code here -----

UF_initialize ();    
    //获取NX安装路径
        char *translation;
        UF_translate_variable("UGII_BASE_DIR", &translation);
    //电子表格模板路径
        string BaseDIR1 = translation;
        string BaseDIR2 = "\\SYTool\\data\\volume.dat";
        string basePath = BaseDIR1+BaseDIR2;
    
        //打开数据库
        int weinjian = uc4504( basePath.c_str(), 1, 87);

        //读数据
        char *outdata;
        int data1 = uc4514a(weinjian, &outdata);
        std:vector<NXString> cailiao,midu;
        while (data1>0)
        {
            string s1 = outdata;
            cailiao.push_back(s1.substr(0,s1.find(",")));
            midu.push_back(s1.substr(s1.find(",")+1, s1.find(" ")));
            data1 = uc4514a(weinjian, &outdata);
        }

        //设置默认
        string01->SetValue(cailiao[0]);
        string01->SetListItems(cailiao);
        string0->SetValue(midu[0]);
        string0->SetListItems(midu);

        //关闭文件
        uc4540(weinjian, 0);
        UF_free(outdata);

UF_terminate ();


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

//------------------------------------------------------------------------------
//Callback Name: apply_cb
//------------------------------------------------------------------------------
int zljs::apply_cb()
{
    int errorCode = 0;
    try
    {
        //---- Enter your callback code here -----

    //选择获取body的tag
        PropertyList *bodySelectoprops =  bodySelect0->GetProperties();
        std::vector<NXOpen::TaggedObject *>body = bodySelectoprops->GetTaggedObjectVector("SelectedObjects");  
        delete bodySelectoprops;
        bodySelectoprops =NULL ;
    //获取密度
        NXString str_1 = string0->Value();
        char *str_2 = (char*)(str_1.GetLocaleText());
UF_initialize ();
    //转换 char 转 double
        double as=atof(str_2);

    //获取体的体积
        tag_t body_tag[1] = {body[0]->Tag()};
        double acc_value [ 11 ] ={.01,0,0,0,0,0,0,0,0,0,0};
        double mass_props [ 47 ];
        double statistics [ 13 ];
        UF_MODL_ask_mass_props_3d( body_tag,1,1,3,NULL ,1,acc_value,mass_props,statistics);
    //计算重量
        double tj = mass_props[1];                    //体积
        double zl = as * tj;                        //重量
    //打开信息窗口
          UF_UI_open_listing_window();
    //输出
          char msg0[256];                //体积
          char msg1[256];                //重量
          char msg2[256];                //表面积
          char msg3[256];                //质心
          sprintf (msg0,"体积:%f立方厘米.",mass_props[1]);
          sprintf (msg1,"重量:%f克.",zl);
          sprintf(msg2,"表面积:%f平方厘米",mass_props[0]);
          sprintf(msg3,"质心坐标:X:%f.Y:%f.Z:%f.",mass_props[6],mass_props[7],mass_props[8]);
          UF_UI_write_listing_window(msg0);
          UF_UI_write_listing_window("\n");
          UF_UI_write_listing_window(msg1);
          UF_UI_write_listing_window("\n");
          UF_UI_write_listing_window(msg2);
          UF_UI_write_listing_window("\n");
          UF_UI_write_listing_window(msg3);
          UF_UI_write_listing_window("\n");
UF_terminate();


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

//------------------------------------------------------------------------------
//Callback Name: update_cb
//------------------------------------------------------------------------------
int zljs::update_cb(NXOpen::BlockStyler::UIBlock* block)
{
    try
    {
        if(block == bodySelect0)
        {
        //---------Enter your code here-----------

            UF_initialize ();    
    //获取NX安装路径
        char *translation;
        UF_translate_variable("UGII_BASE_DIR", &translation);
    //电子表格模板路径
        string BaseDIR1 = translation;
        string BaseDIR2 = "\\SYTool\\data\\volume.dat";
        string basePath = BaseDIR1+BaseDIR2;
    
        //打开数据库
        int weinjian = uc4504( basePath.c_str(), 1, 87);

        //读数据
        char *outdata;
        int data1 = uc4514a(weinjian, &outdata);
        std:vector<NXString> cailiao,midu;
        while (data1>0)
        {
            string s1 = outdata;
            cailiao.push_back(s1.substr(0,s1.find(",")));
            midu.push_back(s1.substr(s1.find(",")+1, s1.find(" ")));
            data1 = uc4514a(weinjian, &outdata);
        }
        //设置默认
        string0->SetValue(midu[0]);
        string0->SetListItems(midu);
        string01->SetValue(cailiao[0]);
        string01->SetListItems(cailiao);

        //关闭文件
        uc4540(weinjian, 0);
        UF_free(outdata);

UF_terminate ();

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

                std::vector<NXString> thestring01= string01->GetListItems();
                std::vector<NXString> thestring0= string0->GetListItems();
                NXString str1 = string01->Value();
    
                for (int i = 0; i < thestring01.size(); i++)
                {    
                    if (!strcmp(thestring01[i].GetLocaleText(),str1.GetLocaleText()))
                    {
                        string0->SetValue(thestring0[i]);
                    }
                }

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


                std::vector<NXString> thestring00 = string0->GetListItems();
                std::vector<NXString> thestring11 = string01->GetListItems();
                NXString str2 = string0->Value();
    
                for (int i = 0; i < thestring00.size(); i++)
                {    
                    if (!strcmp(thestring00[i].GetLocaleText(),str2.GetLocaleText()))
                    {
                        string01->SetValue(thestring11[i]);
                    }
                }


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



  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值