?初學(xué)UG/NX, 對體的各種tag還不熟悉,更別說各種面、邊、點(diǎn)的操作,感覺就像一口鍋里面的餃子,根本分不清哪個是哪個。
所以,這里對體的面、邊、點(diǎn)以及各對象進(jìn)行了一次整理,廢話不說,直接上代碼:
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; } //返回當(dāng)前對象所在層
public bool IsOccurrence { get; set; } //返回此對象是否為實(shí)例
public bool IsBlanked { get; set; } //返回此對象是否為空白狀態(tài)
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. (可能是中心點(diǎn)坐標(biāo))
public string JournalIdentifier { get; set; } //返回此對象的日志中的標(biāo)識符
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; } //是否為實(shí)體
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; } //返回面的實(shí)體類型
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; } //返回曲線的參考狀態(tài)
public Edge.EdgeType SolidEdgeType { get; set; } //返回邊的實(shí)體類型
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、調(diào)用方法
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、運(yùn)行結(jié)果如下圖文章來源:http://www.zghlxwxcb.cn/news/detail-597569.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-597569.html
到了這里,關(guān)于UG/NX二次開發(fā)(C#) 一個方法遍歷部件的體、面、邊屬性的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!