namespace CreateBlock
{
    public partial class CreateBlockForm : Form
    {
        private Session theSession = Session.GetSession();
        private NXOpen.UF.UFSession theUFSession = NXOpen.UF.UFSession.GetUFSession();

        //UndoMark
        NXOpen.Session.UndoMarkId createBlockUndoMark;

        //所创建长方体的特征
        Feature blk_feat = null;

        public CreateBlockForm()
        {
            InitializeComponent();
        }

        private void CreateBlockForm_Load(object sender, EventArgs e)
        {
            //设定UndoMark
            createBlockUndoMark = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "CreateBlock");
        }

        private void button1_Click(object sender, EventArgs e)  //预览
        {           
            if (this.button1.Text == "预览")
            {
                //改变按钮的显示
                this.button1.Text = "取消预览";              

                //创建长方体
                double l = Double.Parse(textBox1.Text);
                double w = Double.Parse(textBox2.Text);
                double h = Double.Parse(textBox3.Text);
                NXHelp.CreateBlock(l, w, h, out blk_feat);            
            }
            else
            {
                //改变按钮的显示
                this.button1.Text = "预览";

                //长方体特征置空
                blk_feat = null;

                //回退到UndoMark
                theSession.UndoToMark(createBlockUndoMark, "CreateBlock");
            }
        }

        private void button2_Click(object sender, EventArgs e)  //确定
        {
            if (blk_feat == null)
            {
                //创建长方体
                double l = Double.Parse(textBox1.Text);
                double w = Double.Parse(textBox2.Text);
                double h = Double.Parse(textBox3.Text);
                NXHelp.CreateBlock(l, w, h, out blk_feat);  
            }

            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)  //取消
        {
            theSession.DeleteUndoMark(createBlockUndoMark, "CreateBlock");

            if (blk_feat != null)
            {
                theUFSession.Obj.DeleteObject(blk_feat.Tag);
            }

            this.Close();
        }     
    }
}

 

namespace CreateBlock
{
    class NXHelp
    {
        /// <summary>
        /// 创建长方体
        /// </summary>
        /// <param name="l">长</param>
        /// <param name="w">宽</param>
        /// <param name="h">高</param>
        /// <param name="blk_feat">输出的特征</param>
        public static void CreateBlock(double l, double w, double h, out Feature blk_feat)
        {
            try
            {
                Session theSession = Session.GetSession();
                Part workPart = theSession.Parts.Work;
                Part displayPart = theSession.Parts.Display;

                NXOpen.Features.Feature nullFeatures_Feature = null;

                NXOpen.Features.BlockFeatureBuilder blockFeatureBuilder1;
                blockFeatureBuilder1 = workPart.Features.CreateBlockFeatureBuilder(nullFeatures_Feature);

                blockFeatureBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create;

                Body[] targetBodies1 = new Body[1];
                Body nullBody = null;
                targetBodies1[0] = nullBody;
                blockFeatureBuilder1.BooleanOption.SetTargetBodies(targetBodies1);

                blockFeatureBuilder1.Type = NXOpen.Features.BlockFeatureBuilder.Types.OriginAndEdgeLengths;

                Point point1;
                point1 = blockFeatureBuilder1.OriginPoint;

                blockFeatureBuilder1.OriginPoint = point1;

                Point3d originPoint1 = new Point3d(0.0, 0.0, 0.0);
                blockFeatureBuilder1.SetOriginAndLengths(originPoint1, l.ToString(), w.ToString(), h.ToString());

                blockFeatureBuilder1.SetBooleanOperationAndTarget(NXOpen.Features.Feature.BooleanType.Create, nullBody);

                blk_feat = blockFeatureBuilder1.CommitFeature();

                blockFeatureBuilder1.Destroy();
            }
            catch (NXException ex)
            {
                blk_feat = null;
                MessageBox.Show(ex.ToString());
            }
        }

    }
}