上一篇介绍了Tag,TaggedObject,Handle在装配前后的差别,这一篇来介绍一下这三个对象在制图模式和建模模式的应用:UG/NX 二开开发 Tag,TaggedObject,Handle在不同场景下的分析(一)
在制图模式下,目前我还没有发现直接获取组件和部件的方法,后续会持续更新,目前是用了唐康林老师的通过手动命名,然后根据name通过ufun来获取tag,theUFSession.Obj.CycleByName。
直接看源码:
public static void Main(string[] args)
{
try
{
theSession = Session.GetSession();
displayPart = theSession.Parts.Display;
theUFSession = UFSession.GetUFSession();
workPart = theSession.Parts.Work;
theUI = UI.GetUI();
List<Component> compList = new List<Component>();
List<Body> bodyList = new List<Body>();
Dictionary<string, Component> dicComp = new Dictionary<string, Component>();
Dictionary<string, Body> dicBody = new Dictionary<string, Body>();
ComponentTool.GetBodyListFromComponet(ref compList, ref bodyList);
Body bodyblock = bodyList[0];
Auto_Init.BodyInit init = new Auto_Init.BodyInit(bodyblock);
BodyModel model = init.ProcBody();
FaceModel topFace = model.DicFace[model.faceFlag.faceTop];
EdgeModel edge1 = topFace.EdgeList[0];
EdgeModel edge2 = topFace.EdgeList[2];
string a = theUFSession.Tag.AskHandleOfTag(edge1.Tag);
string b = theUFSession.Tag.AskHandleOfTag(edge2.Tag);
Debug.WriteLine(string.Format("edgeStart Name:{0}, edgeEnd Name:{1}", edge1.Name, edge2.Name));
Debug.WriteLine(string.Format("edgeStart Tag:{0}, edgeEnd Tag:{1}", edge1.Tag, edge2.Tag));
Debug.WriteLine(string.Format("edgeStart Hand:{0}, edgeEnd Hand:{1}", a, b));
Debug.WriteLine("-------");
Tag edgeStart = Tag.Null;
Tag edgeEnd = Tag.Null;
theUFSession.Obj.CycleByName("EDGESTART", ref edgeStart);
theUFSession.Obj.CycleByName("EDGEEND", ref edgeEnd);
a = theUFSession.Tag.AskHandleOfTag(edgeStart);
b = theUFSession.Tag.AskHandleOfTag(edgeEnd);
Debug.WriteLine(string.Format("edgeStart Tag:{0}, edgeEnd Tag:{1}", edgeStart, edgeEnd));
Debug.WriteLine(string.Format("edgeStart Hand:{0}, edgeEnd Hand:{1}", a, b));
}
catch (NXOpen.NXException ex)
{
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
}
建模模式下,运行结果如下:
从运行结果来看,同样的两条边:EDGESTART、EDGEEND
他们有2个Tag,2个Hand,个人验证过,通过theUFSession.Obj.CycleByName获取的tag在制图模式可以正常使用,相反另外一套tag在制图模式下无法正常使用。
结论: 在制图模块,暂时可以通过theUFSession.Obj.SetName的方式标记对象的name,然后在制图模式下通过CycleByName的方式获取对象。