采用UIStyler,开发人员可以设计出与NX风格一样的操作界面,在“开发人员”中,打开块UI样式编辑器。
添加UIStyler中的一个按钮和一个tree(树),实现模型中体、面、边的树结构
在生成的cs中,去除.SetOnSelectHandler()的行注释。
//------------------------------------------------------------------------------
//Registration of Treelist specific callbacks
//------------------------------------------------------------------------------
//_ObjectsTree.SetOnExpandHandler(new NXOpen.BlockStyler.Tree.OnExpandCallback(OnExpandCallback));
//_ObjectsTree.SetOnInsertColumnHandler(new NXOpen.BlockStyler.Tree.OnInsertColumnCallback(OnInsertColumnCallback));
//_ObjectsTree.SetOnInsertNodeHandler(new NXOpen.BlockStyler.Tree.OnInsertNodeCallback(OnInsertNodecallback));
//_ObjectsTree.SetOnDeleteNodeHandler(new NXOpen.BlockStyler.Tree.OnDeleteNodeCallback(OnDeleteNodecallback));
//_ObjectsTree.SetOnPreSelectHandler(new NXOpen.BlockStyler.Tree.OnPreSelectCallback(OnPreSelectcallback));
_ObjectsTree.SetOnSelectHandler(new NXOpen.BlockStyler.Tree.OnSelectCallback(OnSelectcallback));
//_ObjectsTree.SetOnStateChangeHandler(new NXOpen.BlockStyler.Tree.OnStateChangeCallback(OnStateChangecallback));
对应的在回调函数onSelectcallback()中去除掉行注释,实现树的选择操作。
//------------------------------------------------------------------------------
//Treelist specific callbacks
//------------------------------------------------------------------------------
//public void OnExpandCallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node)
//{
//}
//public void OnInsertColumnCallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node, int columnID)
//{
//}
//public void OnInsertNodecallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node)
//{
//}
//public void OnDeleteNodecallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node)
//{
//}
//public void OnPreSelectcallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node, int columnID, bool Selected)
//{
//}
public void OnSelectcallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node, int columnID, bool Selected)
然后,在树中插入行:
public void dialogShown_cb()
{
try
{
//---- Enter your callback code here -----
NXUtilityTools.AllObjectsUnhightlight();
_ObjectsTree.InsertColumn(1,"模型对象",240);
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
}
在按钮中,依次遍历对象,并按照体-->面-->顺序形成对象树:
if (block == _TravelAllObjectsButton)
{
//---------Enter your code here-----------
//将所有的对象存储在tree中
BodyCollection bodyCollection = workPart.Bodies;
Body[] bodies = bodyCollection.ToArray();
foreach(var bd in bodies)
{
Node rootNode = _ObjectsTree.CreateNode("体(" + bd.Tag.ToString() + ")");
_ObjectsTree.InsertNode(rootNode,null,null,Tree.NodeInsertOption.Sort);
Face[] faces = bd.GetFaces();
foreach(var fc in faces)
{
Node faceRootNode = _ObjectsTree.CreateNode("面("+fc.Tag.ToString()+")");
_ObjectsTree.InsertNode(faceRootNode, rootNode, null, Tree.NodeInsertOption.Sort);
Edge[] edges = fc.GetEdges();
foreach(var eg in edges)
{
Node edgeRootNode = _ObjectsTree.CreateNode("边("+eg.Tag.ToString()+")");
_ObjectsTree.InsertNode(edgeRootNode,faceRootNode,null,Tree.NodeInsertOption.Sort);
}
}
}
}
得到如下的结果:
选择任意对象,则高亮显示这个对象,则在选择回调函数中,添加如下代码:
public void OnSelectcallback(NXOpen.BlockStyler.Tree tree, NXOpen.BlockStyler.Node node, int columnID, bool Selected)
{
if(tree==_ObjectsTree)
{
if(Selected==true)
{
string tagString = node.DisplayText.Split('(')[1].Trim(')');
uint tagInt = (uint)int.Parse(tagString);
NXObjectManager nXObjectManager = new NXObjectManager();
TaggedObject taggedObject = NXObjectManager.GetObjectFromUInt(tagInt);
if(node.DisplayText.Contains("体"))
{
Body body = (Body)taggedObject;
body.Highlight();
}
else if(node.DisplayText.Contains("面"))
{
Face face = (Face)taggedObject;
face.Highlight();
}
else if (node.DisplayText.Contains("边"))
{
Edge edge = (Edge)taggedObject;
edge.Highlight();
}
}
else
{
NXOPENTools.General.UtilityTools.NXUtilityTools.AllObjectsUnhightlight();
}
}
}