前言

        随着工业智能化的不断发展,UG二次开发的需求越来越多,也吸引了大批的二开从业人员,本人作为一名资深IT从业者(10年+)也毅然加入二次开发大军。

        然而,和流行IT行业(互联网、金融、医疗等)相比,工业智能化的门槛显得更高一点,专业的工业软件,相对封闭的开发理念和更小的开发圈子,让刚进入二开的从业者有点举步维艰。边学边整理,希望通过这系列文章的整理能给二开的生态增添一叶绿。


一、知识点提取

本案例主要实现了UG环境打开文本文件,及读取文件内容创建点,主要知识点如下:

1)UG环境打开文件方法

2)逐行读取文本文件内容的实现

3)创建点的实现

二、案例需求分析

1、效果图

用Java二次开发UG NX ug二次开发实例_学习

2、需求分解

1)UG环境打开文件

2)逐行读取文本文件内容

3)创建点

三、程序分析

1、源码所在目录

UGOPEN\SampleNXOpenApplications\C++\File2Points

2、主要功能分析  

1)UG环境打开文件方法

UF_UI_create_filebox( prompt,
                          "Select point data file",
                          filter, NULL,
                          filename, &response );

2) 逐行读取文本文件内容实现

if ( create_filebox( "Select file that contains the point coordinate data", fileName ) == UF_UI_OK )
{ 
ifstream file1;
        
file1.open(fileName);

if ( !file1.is_open() )
    PrintErrorMessage( "Could not open file\n" );
else
{
    double x,y,z;
    char c;
    while(!file1.eof())
    {
        file1 >> x >> c >> y >> c >> z;
        if(!file1)
        {
            PrintErrorMessage("File format error:\n   The input file does not have the expected format\n");
            break;
        }                
        NXOpen::Point3d point3d11(x,y,z);
        Point *point1;
        point1 = workPart->Points()->CreatePoint(point3d11);
        // The point is invisible when initially created
        point1->SetVisibility(NXOpen::SmartObject::VisibilityOptionVisible);

        // Check for end of file
        file1 >> c;
        if ( !file1.eof() )
            file1.unget();
    }
    file1.close();
    file1.clear();
}

3) 创建点的实现

point1 = workPart->Points()->CreatePoint(point3d11);