NXの二次開発では、ListBox(列とテーブルボックス)は非常に便利なブロックです。この記事では、主にListBoxの追加機能と削除機能について説明します。
次の図に示すように、リストボックスの右側で「+」(追加)および「X」(削除)操作を実行するための2つのブロック、オブジェクト選択とリストボックスが含まれています。
1.以下に示すようにUIStylerを作成します。次に、.dlxおよび.csとして保存します
2.VS2019でリストボックスデモプロジェクトをビルドします
3.リンクリストはprivatestatic List <NXObject> nXObjectsList = new List <NXObject>();;を使用します。初期化するときは、リンクリストとリストボックスをクリアします。
nXObjectsList.Clear();
string[] listBoxStrings = new string[0];
_ListBoxUI_ObjectsListBox.SetListItems(listBoxStrings);
4.初期化中に、変更します
_ListBoxUI_ObjectsListBox.SetAddHandler(new NXOpen.BlockStyler.ListBox.AddCallback(AddCallback));
_ListBoxUI_ObjectsListBox.SetDeleteHandler(new NXOpen.BlockStyler.ListBox.DeleteCallback(DeleteCallback));
コメントを外します。
public void initialize_cb()
{
try
{
group = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group");
_ListBoxUI_ObjectsSelect = (NXOpen.BlockStyler.SelectObject)theDialog.TopBlock.FindBlock("_ListBoxUI_ObjectsSelect");
group1 = (NXOpen.BlockStyler.Group)theDialog.TopBlock.FindBlock("group1");
_ListBoxUI_ObjectsListBox = (NXOpen.BlockStyler.ListBox)theDialog.TopBlock.FindBlock("_ListBoxUI_ObjectsListBox");
//------------------------------------------------------------------------------
//Registration of ListBox specific callbacks
//------------------------------------------------------------------------------
_ListBoxUI_ObjectsListBox.SetAddHandler(new NXOpen.BlockStyler.ListBox.AddCallback(AddCallback));
_ListBoxUI_ObjectsListBox.SetDeleteHandler(new NXOpen.BlockStyler.ListBox.DeleteCallback(DeleteCallback));
//------------------------------------------------------------------------------
}
catch (Exception ex)
{
//---- Enter your exception handling code here -----
theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
}
}
5.背後にあるコード
public int AddCallback(NXOpen.BlockStyler.ListBox list_box)
public int DeleteCallback(NXOpen.BlockStyler.ListBox list_box)
また、コメントを外して、次のようにコードを追加します。
//------------------------------------------------------------------------------
//ListBox specific callbacks
//------------------------------------------------------------------------------
public int AddCallback(NXOpen.BlockStyler.ListBox list_box)
{
if(list_box==_ListBoxUI_ObjectsListBox)
{
TaggedObject[] taggedObjects = _ListBoxUI_ObjectsSelect.GetSelectedObjects();
TaggedObject[] restTaggenObjects = new TaggedObject[0];
foreach(var to in taggedObjects)
{
NXObject nXObject = (NXObject)to;
bool isExsit = false;
nXObjectsList.ForEach(a =>
{
if (a.Tag == nXObject.Tag)
isExsit = true;
});
if(isExsit==false)
{
nXObjectsList.Add(nXObject);
}
}
string[] listBoxStrings = new string[0];
foreach(var st in nXObjectsList)
{
Array.Resize(ref listBoxStrings, listBoxStrings.Length + 1);
listBoxStrings[listBoxStrings.Length - 1] = st.JournalIdentifier;
}
_ListBoxUI_ObjectsListBox.SetListItems(listBoxStrings);
TaggedObject[] taggedObjects1 = new TaggedObject[0];
_ListBoxUI_ObjectsSelect.SetSelectedObjects(taggedObjects1);
return 1;
}
else
{
return 0;
}
}
public int DeleteCallback(NXOpen.BlockStyler.ListBox list_box)
{
if(list_box==_ListBoxUI_ObjectsListBox)
{
string []listBoxStrings = _ListBoxUI_ObjectsListBox.GetSelectedItemStrings();
foreach(var lv in listBoxStrings)
{
nXObjectsList.ForEach(a =>
{
if (a.JournalIdentifier == lv)
nXObjectsList.Remove(a);
});
}
listBoxStrings = new string[0];
foreach (var st in nXObjectsList)
{
Array.Resize(ref listBoxStrings, listBoxStrings.Length + 1);
listBoxStrings[listBoxStrings.Length - 1] = st.JournalIdentifier;
}
_ListBoxUI_ObjectsListBox.SetListItems(listBoxStrings);
return 1;
}
else
{
return 0;
}
}
生成が完了したら、呼び出すことができます。
デモンストレーションは次のとおりです。