今天分享的是如何将实体part复制到一个固定的图层中。
逻辑如下:首先将无选择过滤器调成实体(这块不知道如何调整可以看我之前发的文章,具体讲了如何操作),然后用鼠标选中坐标系中显示的part(注意此处不要点击左侧部件导航器上的内容)。选中以后运行代码就可以实现将实体复制到指定图层的操作。
实现代码如下:
NXOpen.Session theSession = NXOpen.Session.GetSession();
NXOpen.Part workPart =theSession.Parts.Work;
NXOpen.Part displayPart =theSession.Parts.Display;
int selcount = 0;
Tag[] objs;
theUfSession.Ui.AskGlobalSelObjectList(out selcount, out objs);
if (selcount < 1)
{
theUI.NXMessageBox.Show("提示", NXMessageBox.DialogType.Information, "请选中体");
return retValue;
}
NXOpen.NXObject[] objectArray1 = newNXOpen.NXObject[selcount];
for(int i=0;i<selcount;i++)
{
NXOpen.NXObject q1 =(NXOpen.NXObject)NXObjectManager.Get(objs[i]);
objectArray1[i] = q1;
}
NXOpen.Features.FeatureCollectionfeatureCollection1 = workPart.Features;
NXOpen.Features.Feature[]baseParts1 = featureCollection1.ToArray();
int layerNum = 255;
workPart.Layers.CopyObjects(layerNum,objectArray1);
string layerName = "客户数模";
CategoryCollection category=workPart.LayerCategories;
Category cateG1=null;
Category [] cateG=category.ToArray();
bool a = false;
for (int i = 0; i < cateG.Length; i++)
{
string Name = null;
theUfSession.Obj.AskName(cateG[i].Tag, out Name);
if (Name == layerName)
{
a = true;
cateG1 = cateG[i];
}
}
if (a)
{
int[] b = cateG1.GetMemberLayers();
int[] layers1 = new int[b.Length + 1];
for (int i = 0; i < b.Length; i++)
{
layers1[i] = b[i];
}
layers1[b.Length] = layerNum;
cateG1.SetMemberLayers(layers1);
}
else
{
SetLayerName(layerNum, layerName);
}
NXOpen.Features.FeatureCollectionfeatureCollection2 = workPart.Features;
NXOpen.Features.Feature[]baseParts2 = featureCollection2.ToArray();
List<NXObject> obj3 = newList<NXObject>();
foreach (NXObject aitem in baseParts2)
{
if (!baseParts1.Contains(aitem))
{
obj3.Add(aitem);
}
}
for (int j = 0; j < obj3.Count; j++)
{
int u; int v;
theUfSession.Obj.AskTypeAndSubtype(obj3[j].Tag, out u, out v);
obj3[j].SetName("客户数模");
}
State state =workPart.Layers.GetState(layerNum);
if (state == State.Selectable)
{
workPart.Layers.SetState(layerNum, State.Hidden);
}
theUI.NXMessageBox.Show("提示", NXMessageBox.DialogType.Information, "复制成功");
运行代码效果之前如下图所示:
运行代码效果之后如下图所示:
我已经把原有体隐藏,把新复制的体放到了255图层。