UG/NX二次开发Siemens官方NXOPEN实例解析—1.1 BlockStyler/ColoredBlock

列文章目录

UG/NX二次开发Siemens官方NXOPEN实例解析—1.1 BlockStyler/ColoredBlock
UG/NX二次开发Siemens官方NXOPEN实例解析—1.2 BlockStyler/EditExpression
UG/NX二次开发Siemens官方NXOPEN实例解析—1.3 BlockStyler/ExtrudewithPreview
UG/NX二次开发Siemens官方NXOPEN实例解析—1.4 BlockStyler/HoleCoordinates
UG/NX二次开发Siemens官方NXOPEN实例解析—1.5 BlockStyler/MatrixOperations
UG/NX二次开发Siemens官方NXOPEN实例解析—1.6 BlockStyler/SelectionExample
UG/NX二次开发Siemens官方NXOPEN实例解析—1.7 BlockStyler/TreeListDemo
UG/NX二次开发Siemens官方NXOPEN实例解析—1.8 BlockStyler/UDB_CreateCylinder
UG/NX二次开发Siemens官方NXOPEN实例解析—1.9 BlockStyler/UpdateExample

前言

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

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


一、UI样式文件分析

1. 样式文件目录

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

2. 样式文件导入预览 

 3. 样式文件解析

本实例主要用到了双精度、指定点、对象颜色拾取器三个控件

二、源码文件解析

1. 主程序分析

    public static void Main()
    {
        try
        {
            theColoredBlock = new ColoredBlock();
            // The following method shows the dialog immediately
            theColoredBlock.Show();
        }
        catch (Exception ex)
        {
            //---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.Message);
        }
        finally
        {
            theColoredBlock.Dispose();
        }
    }
    public ColoredBlock()
    {
        try
        {
            theSession = Session.GetSession();
            theUI = UI.GetUI();
            theDialogName = "D:\\Program Files\\Siemens\\NX 8.0\\UGOPEN\\SampleNXOpenApplications\\C++\\BlockStyler\\ColoredBlock\\ColoredBlock.dlx";
            theDialog = theUI.CreateDialog(theDialogName);
            theDialog.AddApplyHandler(new NXOpen.BlockStyler.BlockDialog.Apply(apply_cb));
            theDialog.AddInitializeHandler(new NXOpen.BlockStyler.BlockDialog.Initialize(initialize_cb));
            theDialog.AddDialogShownHandler(new NXOpen.BlockStyler.BlockDialog.DialogShown(dialogShown_cb));
        }
        catch (Exception ex)
        {
            //---- Enter your exception handling code here -----
            throw ex;
        }
    }

        Main函数里,我们只需要关注ColoredBlock()方法;

        在ColoredBlock()方法里,主要加载了dlx文件路径,提交方法apply_cb,控件初始化方法initialize_cb,回调方法dialogShown_cb。下面,我们重点讲一下提交方法和控件初始化方法。

2. 处理模块分析

    public void initialize_cb()
    {
        try
        {
            group0 = theDialog.TopBlock.FindBlock("group0");
            blockHeight = theDialog.TopBlock.FindBlock("blockHeight");
            blockWidth = theDialog.TopBlock.FindBlock("blockWidth");
            blockLength = theDialog.TopBlock.FindBlock("blockLength");
            blockOrigin = theDialog.TopBlock.FindBlock("blockOrigin");
            blockColor = theDialog.TopBlock.FindBlock("blockColor");

            // Set the upper-limits and lower-limits
            PropertyList blockHeightProps = blockHeight.GetProperties();
            blockHeightProps.SetDouble("MaximumValue", 100000.0);
            blockHeightProps.SetDouble("MinimumValue", 0.001);
            blockHeightProps.SetDouble("Value", 100.0);
            blockHeightProps.Dispose();
            blockHeightProps = null;

            PropertyList blockWidthProps = blockWidth.GetProperties();
            blockWidthProps.SetDouble("MaximumValue", 100000.0);
            blockWidthProps.SetDouble("MinimumValue", 0.001);
            blockWidthProps.SetDouble("Value", 100.0);
            blockWidthProps.Dispose();
            blockWidthProps = null;

            PropertyList blockLengthProps = blockLength.GetProperties();
            blockLengthProps.SetDouble("MaximumValue", 100000.0);
            blockLengthProps.SetDouble("MinimumValue", 0.001);
            blockLengthProps.SetDouble("Value", 100.0);
            blockLengthProps.Dispose();
            blockLengthProps = null;
        }
        catch (Exception ex)
        {
            //---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.Message);
        }
    }

初始化方法initialize_cb()

1、获取控件方法:

     group0 = theDialog.TopBlock.FindBlock("group0");
     blockHeight = theDialog.TopBlock.FindBlock("blockHeight");
     blockWidth = theDialog.TopBlock.FindBlock("blockWidth");
     blockLength = theDialog.TopBlock.FindBlock("blockLength");
     blockOrigin = theDialog.TopBlock.FindBlock("blockOrigin");
     blockColor = theDialog.TopBlock.FindBlock("blockColor");

2、PropertyList在BlockUI里面是最常用的对象:

     PropertyList blockHeightProps = blockHeight.GetProperties();

3、分别设置双精度输入框的最大值、最小值、默认值:

     blockHeightProps.SetDouble("MaximumValue", 100000.0);
     blockHeightProps.SetDouble("MinimumValue", 0.001);
     blockHeightProps.SetDouble("Value", 100.0);

    public int apply_cb()
    {
        int errorCode = 0;
        NXOpen.Features.BlockFeatureBuilder blockFeatureBuilder1 = null;

        try
        {
            Part workPart = theSession.Parts.Work;

            //Get the values from UI Blocks
            PropertyList blockHeightProps = blockHeight.GetProperties();
            double theHeight = blockHeightProps.GetDouble("Value");
            blockHeightProps.Dispose();
            blockHeightProps = null;

            PropertyList blockWidthProps = blockWidth.GetProperties();
            double theWidth = blockWidthProps.GetDouble("Value");
            blockWidthProps.Dispose();
            blockWidthProps = null;

            PropertyList blockLengthProps = blockLength.GetProperties();
            double theLength = blockLengthProps.GetDouble("Value");
            blockLengthProps.Dispose();
            blockLengthProps = null;

            PropertyList blockOriginProps = blockOrigin.GetProperties();
            Point3d originPoint = blockOriginProps.GetPoint("Point");
            blockOriginProps.Dispose();
            blockOriginProps = null;

            PropertyList blockColorProps = blockColor.GetProperties();
            int[] color = blockColorProps.GetIntegerVector("Value");
            blockColorProps.Dispose();
            blockColorProps = null;

            //Create the NX block
            NXOpen.Features.Feature nullFeatures_Feature = null;
            blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature);

            blockFeatureBuilder1.SetOriginAndLengths(originPoint, theLength.ToString(), theHeight.ToString(), theWidth.ToString());

            NXOpen.Features.Feature feature1;
            feature1 = blockFeatureBuilder1.CommitFeature();


            // Get the body from Feature
            NXOpen.Features.BodyFeature bodyFeat = (NXOpen.Features.BodyFeature)feature1;
            Body[] bodies = new Body[1];
            bodies = bodyFeat.GetBodies();
            // Apply the color to feature body
            DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification();
            displayModification1.ApplyToAllFaces = true;
            displayModification1.NewColor = color[0];
            DisplayableObject[] objects1 = new DisplayableObject[1];
            objects1[0] = bodies[0];
            displayModification1.Apply(objects1);
            displayModification1.Dispose();
        }
        catch (Exception ex)
        {
            //---- Enter your exception handling code here -----
            errorCode = 1;
            theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.Message);
        }
        finally
        {
            if (blockFeatureBuilder1 != null)
            {
                blockFeatureBuilder1.Destroy();
                blockFeatureBuilder1 = null;
            }
        }
        return errorCode;
    }

 提交方法apply_cb()

1、获取输入框的值:

     double theHeight = blockHeightProps.GetDouble("Value");

2、获取选择点的坐标:

     Point3d originPoint = blockOriginProps.GetPoint("Point");

3、获取颜色选择器的颜色:

     int[] color = blockColorProps.GetIntegerVector("Value");

4、使用BlockFeatureBuilder对象,创建块:

     NXOpen.Features.Feature nullFeatures_Feature = null;
     blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature);
     blockFeatureBuilder1.SetOriginAndLengths(originPoint, theLength.ToString(), theHeight.ToString(), theWidth.ToString());
     NXOpen.Features.Feature feature1;
     feature1 = blockFeatureBuilder1.CommitFeature();

5、根据创建块的feature1,获取到块,然后通过DisplayModification设置颜色:

     NXOpen.Features.BodyFeature bodyFeat = (NXOpen.Features.BodyFeature)feature1;
     Body[] bodies = new Body[1];
     bodies = bodyFeat.GetBodies();
     DisplayModification displayModification1 = theSession.DisplayManager.NewDisplayModification();
     displayModification1.ApplyToAllFaces = true;
     displayModification1.NewColor = color[0];
     DisplayableObject[] objects1 = new DisplayableObject[1];
     objects1[0] = bodies[0];
     displayModification1.Apply(objects1);

3. 运行结果截图

总结

官方实例ColoredBlock

1、前端BlockUI主要用到了的3种控件:双精度、指定点、颜色拾取器。这里介绍了如何获取控件,初始化控件,获取指定点坐标,获取颜色信息。

2、后台通过前端获取信息,讲述了如何通过BlockFeatureBuilder创建块,通过DisplayModification设置块的颜色。

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarcoPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值