UG二次开发入门--一个简单的示例

时间:2024-04-15 09:03:08
确认配置好开发环境:
1)安装UG NX5;
2)安装VS 2005;
3)确认安装有DotNet Framework 2.0。

新建一个项目:
1)FileNewProject 新建一个类库(ClassLibrary);
2) 添加引用集:定位到 %UGII_ROOT_DIR%\managed目录添加需要的引用集。
一般有这几个NXOpen.dll  NXOpen.UF.dll  NXOpen.Utilities.dll  NXOpenUI.dll。
3) 添加命名空间:
using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;
using NXOpenUI;

4)添加两个静态方法:
public static void Main()和public static int GetUnloadOption(string dummy)

整体代码如下:
using System;
using System.Collections.Generic;
using System.Text;

using NXOpen;
using NXOpen.Utilities;
using NXOpen.UF;
using NXOpenUI;
using NXOpen.Features;

namespace HelloWorld
{
    
public class MyClass
    
{
        
public static void Main()
        
{
            Session theSession 
= Session.GetSession();
            Part workPart 
= theSession.Parts.Work;

            BlockFeatureBuilder blockFeatureBuilder 
= null;
            blockFeatureBuilder 
= workPart.Features.CreateBlockFeatureBuilder(null);
            Feature blockFeature 
= null;
            
try
            
{
                Point3d originPoint 
= new Point3d(0.0f0.0f0.0f);
                blockFeatureBuilder.SetOriginAndLengths(originPoint, 
"100""100""100");
                blockFeatureBuilder.SetBooleanOperationAndTarget(Feature.BooleanType.Create, 
null);

                blockFeature 
= blockFeatureBuilder.CommitFeature();
            }

            
catch (System.Exception e)
            
{
                theSession.ListingWindow.Open();
                theSession.ListingWindow.WriteLine(e.ToString());
            }


            blockFeatureBuilder.Destroy();
        }


        
public static int GetUnloadOption(string dumy)
        
{
            
return (int)Session.LibraryUnloadOption.Immediately;
        }

    }

}

编译运行程序:
1)编译程序;
2)按Ctrl+U,弹出运行对话框运行编译好的dll