UG/NX二次开发(C#) 一个方法遍历部件的体、面、边属性

 初学UG/NX, 对体的各种tag还不熟悉,更别说各种面、边、点的操作,感觉就像一口锅里面的饺子,根本分不清哪个是哪个。

所以,这里对体的面、边、点以及各对象进行了一次整理,废话不说,直接上代码:

1、先定义体、面、边模型

using NXOpen;
using NXOpen.Assemblies;
using NXOpen.Facet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace Auto_Init.Model
{
    /// <summary>
    /// 基本属性
    /// </summary>
    public class Base
    {
        public string StrTag { get; set; }                                   //返回对象的tag
        public Tag Tag { get; set; }
        public int Layer { get; set; }                               //返回当前对象所在层
        public bool IsOccurrence { get; set; }                       //返回此对象是否为实例
        public bool IsBlanked { get; set; }                          //返回此对象是否为空白状态
        public string Name { get; set; }                                //Returns the custom name of the object.
        public Point3d NameLocation { get; set; }                    //Returns the location of the object's name. (可能是中心点坐标)
        public string JournalIdentifier { get; set; }                //返回此对象的日志中的标识符
        public int Color { get; set; }                               //返回颜色
        public DisplayableObject.ObjectFont LineFont { get; set; }   //Returns or sets the line font of the object
        public DisplayableObject.ObjectWidth LineWidth { get; set; } //Returns or sets the line width of the object. 
        public Component OwningComponent { get; set; }               //如果此对象是引用,则返回所属组件
        public BasePart OwningPart { get; set; }                     //Returns the owning part of this object 
        public INXObject Prototype { get; set; }                     //Returns the prototype of this object if it is an occurrence. 
    }

    /// <summary>
    /// 块/体
    /// </summary>
    public class BodyModel : Base
    {
        public List<FaceModel> FaceList { get; set; }
        public FaceFlag faceFlag { get; set; }
        public double Density { get; set; }                          //密度
        public bool IsSheetBody { get; set; }                        //是否为sheet
        public bool IsSolidBody { get; set; }                        //是否为实体
        public IMessageSink NextSink { get; set; }                   //Gets the next message sink in the sink chain. 
        public Type Type { get; set; }                               //类型
        public FacetedBody facetBody { get; set; }                   //镶嵌体
        public bool upToDate { get; set; }                           //过期时间
        public Dictionary<string, FaceModel> DicFace { get; set; }   //面字典,key为面的tag
        public Dictionary<string, EdgeModel> DicEdge { get; set; }   //边字典,key为边的tag
    }

    public class FaceFlag
    { 
        public string faceTop { get;set;}
        public string faceBotton { get;set;}
        public string face2 {get;set;}
        public string face3 { get; set; }
        public string face4 { get; set; }
        public string face5 { get; set; }
    }

    /// <summary>
    /// 面
    /// </summary>
    public class FaceModel : Base
    {
        public Face.FaceType SolidFaceType { get; set; }             //返回面的实体类型
        public List<EdgeModel> EdgeList { get; set; }
        public List<Point3d> PointList { get; set; }
        public Body fatherBody { get; set; }
        public NXObject.AttributeInformation[] Attribute { get; set; }  //返回对象上的所有属性
    }

    /// <summary>
    /// 边
    /// </summary>
    public class EdgeModel : Base
    {
        public double Len { get; set; }                              //边长
        public bool IsReference { get; set; }                        //返回曲线的参考状态
        public Edge.EdgeType SolidEdgeType { get; set; }             //返回边的实体类型
        public Face[] FatherFaces { get; set; }                      //边所在的面
        public Body FatherBody { get; set; }                         //边所在的体
        public Type Type { get; set; }                               //类型
        public Point3d Vertex1 { get; set; }
        public Point3d Vertex2 { get; set; }
    }
}

2、加载体的方法如下

public class BodyInit
    {
        public Body body;
        public BodyModel bodyModel;
        public UI theUI;
        public NXOpen.UF.UFSession theUFSession;

        public BodyInit(Body body)
        {
            this.body = body;
            this.theUI = UI.GetUI();
            theUFSession = UFSession.GetUFSession();
        }

        public FaceModel GetFaceByTag(string tag)
        {
            if (bodyModel.DicFace.ContainsKey(tag))
                return bodyModel.DicFace[tag];
            return null;
        }

        public EdgeModel GetEdgeModelByTag(string tag)
        {
            if (bodyModel.DicEdge.ContainsKey(tag))
                return bodyModel.DicEdge[tag];
            return null;
        }

        /// <summary>
        /// 加载体
        /// </summary>
        /// <returns></returns>
        public BodyModel ProcBody()
        {
            bodyModel = new BodyModel();
            bodyModel.FaceList = new List<FaceModel>();
            bodyModel.DicFace = new Dictionary<string, FaceModel>();
            bodyModel.DicEdge = new Dictionary<string, EdgeModel>();

            try
            {
                foreach (Face faceObj in body.GetFaces())
                {
                    FaceModel face = ProcFace(faceObj);
                    bodyModel.FaceList.Add(face);
                    bodyModel.DicFace.Add(faceObj.Tag.ToString(), face);
                }

                foreach (Edge edgeObj in body.GetEdges())
                {
                    EdgeModel edge = ProcEdge(edgeObj);
                    bodyModel.DicEdge.Add(edge.StrTag.ToString(), edge);
                }

                bodyModel.JournalIdentifier = body.JournalIdentifier;
                bodyModel.Color = body.Color;
                bodyModel.Density = body.Density;
                bodyModel.IsBlanked = body.IsBlanked;
                bodyModel.IsOccurrence = body.IsOccurrence;
                bodyModel.IsSheetBody = body.IsSheetBody;
                bodyModel.IsSolidBody = body.IsSolidBody;
                bodyModel.Layer = body.Layer;
                bodyModel.LineFont = body.LineFont;
                bodyModel.LineWidth = body.LineWidth;
                bodyModel.Name = body.Name;
                try
                {
                    bodyModel.NameLocation = body.NameLocation;
                }
                catch (Exception)
                {

                }
                bodyModel.NextSink = body.NextSink;
                bodyModel.OwningComponent = body.OwningComponent;
                //bodyModel.OwningPart = body.OwningPart;
                bodyModel.Prototype = body.Prototype;
                bodyModel.StrTag = body.Tag.ToString();
                bodyModel.Tag = body.Tag;

                try
                {
                    FacetedBody facete;
                    bool uptodate;
                    body.GetFacetedBody(out facete, out uptodate);
                    bodyModel.facetBody = facete;
                    bodyModel.upToDate = uptodate;
                }
                catch (Exception)
                {

                }
                bodyModel.Type = body.GetType();

                bodyModel.faceFlag = new FaceFlag();
                string tagTop, tagBotton, tag2, tag3, tag4, tag5;
                InitBll.GetRightFace(bodyModel.FaceList, out tagTop, out tagBotton, out tag2, out tag3, out tag4, out tag5);
                bodyModel.faceFlag.faceTop = tagTop;
                bodyModel.faceFlag.faceBotton = tagBotton;
                bodyModel.faceFlag.face2 = tag2;
                bodyModel.faceFlag.face3 = tag3;
                bodyModel.faceFlag.face4 = tag4;
                bodyModel.faceFlag.face5 = tag5;

                int num_boundaries;
                int[] num_edges;
                Tag[] edge_tags;
                theUFSession.Modl.AskBodyBoundaries(body.Tag, out num_boundaries, out num_edges, out edge_tags);
            }
            catch (Exception ex)
            {
                theUI.NXMessageBox.Show("ProcBody fun", NXMessageBox.DialogType.Error, ex.ToString());
            }

            return bodyModel;
        }

        /// <summary>
        /// 加载面
        /// </summary>
        /// <param name="faceObj"></param>
        /// <returns></returns>
        public FaceModel ProcFace(Face faceObj)
        {
            FaceModel model = new FaceModel();
            model.EdgeList = new List<EdgeModel>();
            model.PointList = new List<Point3d>();
            try
            {
                foreach (Edge edgeObj in faceObj.GetEdges())
                {
                    EdgeModel item = ProcEdge(edgeObj);
                    model.EdgeList.Add(item);
                    if (!model.PointList.Contains(item.Vertex1))
                    {
                        model.PointList.Add(item.Vertex1);
                    }
                    if (!model.PointList.Contains(item.Vertex2))
                    {
                        model.PointList.Add(item.Vertex2);
                    }
                }
                model.Color = faceObj.Color;
                model.IsBlanked = faceObj.IsBlanked;
                model.IsOccurrence = faceObj.IsOccurrence;
                model.JournalIdentifier = faceObj.JournalIdentifier;
                model.Layer = faceObj.Layer;
                model.LineFont = faceObj.LineFont;
                model.LineWidth = faceObj.LineWidth;
                model.Name = faceObj.Name;
                try
                {
                    model.NameLocation = faceObj.NameLocation;
                }
                catch (Exception)
                {

                }
                model.OwningComponent = faceObj.OwningComponent;
                //model.OwningPart = faceObj.OwningPart;
                model.Prototype = faceObj.Prototype;
                model.SolidFaceType = faceObj.SolidFaceType;
                model.StrTag = faceObj.Tag.ToString();
                model.Tag = faceObj.Tag;
                model.fatherBody = faceObj.GetBody();
                model.Attribute = faceObj.GetUserAttributes();
            }
            catch (Exception ex)
            {
                theUI.NXMessageBox.Show("ProcFace fun", NXMessageBox.DialogType.Error, ex.ToString());
            }

            return model;
        }

        /// <summary>
        /// 加载边
        /// </summary>
        /// <param name="edgeObj"></param>
        /// <returns></returns>
        public EdgeModel ProcEdge(Edge edgeObj)
        {
            EdgeModel model = new EdgeModel();

            try
            {
                model.StrTag = edgeObj.Tag.ToString();
                model.Tag = edgeObj.Tag;
                model.Color = edgeObj.Color;
                model.FatherBody = edgeObj.GetBody();
                model.FatherFaces = edgeObj.GetFaces();
                model.IsBlanked = edgeObj.IsBlanked;
                model.IsOccurrence = edgeObj.IsOccurrence;
                model.IsReference = edgeObj.IsReference;
                model.JournalIdentifier = edgeObj.JournalIdentifier;
                model.Layer = edgeObj.Layer;
                model.Len = edgeObj.GetLength();
                model.LineFont = edgeObj.LineFont;
                model.LineWidth = edgeObj.LineWidth;
                model.Name = edgeObj.Name;
                try
                {
                    model.NameLocation = edgeObj.NameLocation;
                }
                catch (Exception)
                {

                }
                model.OwningComponent = edgeObj.OwningComponent;
                //model.OwningPart = edgeObj.OwningPart;
                model.Prototype = edgeObj.Prototype;
                model.SolidEdgeType = edgeObj.SolidEdgeType;
                model.Type = edgeObj.GetType();
                Point3d temp1, temp2;
                edgeObj.GetVertices(out temp1, out temp2);
                model.Vertex1 = temp1;
                model.Vertex2 = temp2;
            }
            catch (Exception ex)
            {
                theUI.NXMessageBox.Show("ProcEdge fun", NXMessageBox.DialogType.Error, ex.ToString());
            }


            return model;
        }
    }

3、调用方法

using System;
using NXOpen;
using Auto_Init.Model;
using NXOpen.UF;

public class Program
{
    public static Session theSession;
    public static Part workPart;
    public static Part displayPart;
    public static NXOpen.UF.UFSession theUFSession;
    private static UI theUI = null;
    public static AssembliesUtils assem;


    public static void Main(string[] args)
    {
        try
        {
            theSession = Session.GetSession();
            displayPart = theSession.Parts.Display;
            theUFSession = UFSession.GetUFSession();
            workPart = theSession.Parts.Work;

            Body bodyYZHU = (Body)workPart.Bodies.FindObject("BLOCK(1)");
            Auto_Init.BodyInit init = new Auto_Init.BodyInit(bodyYZHU);
            BodyModel model = init.ProcBody();
            Face topface = (Face)NXOpen.Utilities.NXObjectManager.GetObjectFromUInt(uint.Parse(model.faceFlag.faceTop));
        }
        catch (NXOpen.NXException ex)
        {
            theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
        }
    }
}

4、运行结果如下图

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
《基于UG NX系统的二次开发》和《深入阐述了UG二次开发工具、UG开发环境的设置、菜单及工具条的编写、UG对话框的制作、零件参数化系统的开发、MFC及数据库开发技术在UG开发中的融入,并综合应用前述开发方法及工具,以渐开线斜齿轮参数化设计、齿轮仿真加工系统和UG平台上模型文件信息管理系统为例,阐述了系统的开发过程,并公开了源代码,使读者能够快速掌握UG二次开发与数据库技术相结合的开发精髓,提高二次开发的能力,以满足工程实际开发的需要。》这两本书提供了关于UG/NX二次开发机械知识的详细介绍和实际应用。 这些书包含了UG/NX二次开发的基本知识、方法和技术,如二次开发的运行模式、项目的创建方法开发流程,编程接口的约定,对象、属性、表达式和链表操作,用户对话框、菜单和工具条的创建技术,日志录制和回放,外部数据源的访问方法,以及零件设计、装配设计和工程图导出等。这些书还提供了大量的开发实例,帮助读者解决实际问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【社区图书馆】UG\NX二次开发书籍推荐](https://blog.csdn.net/WangPaiFeiXingYuan/article/details/130296879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MarcoPro

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

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

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

打赏作者

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

抵扣说明:

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

余额充值