初学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、运行结果如下图文章来源:https://www.toymoban.com/news/detail-597569.html
文章来源地址https://www.toymoban.com/news/detail-597569.html
到了这里,关于UG/NX二次开发(C#) 一个方法遍历部件的体、面、边属性的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!