选择集使用方法介绍
获得选择集中的元素
基于MicroStation中强大的选择集功能,可以直接使用选择集中图层、颜色、线形、元素类型、元素模板扥刚对元素进行过滤,该方法介绍了如果读取到选择集中的元素。通过对于在选择集中获取到元素我们还可以根据业务的实际需求进一步的筛选元素,已达到获取目标元素的目的
public static void QueryElemsInSelection(string unparsed)//Case:DistinguishNestingElement
{
ElementAgenda agenda = new ElementAgenda();//声明元素容器
SelectionSetManager.BuildAgenda(ref agenda);//从当前选择集中获得元素
string result = "The selected elements:\n";//声明字符串
for (uint i = 0; i < agenda.GetCount(); i++)//遍历选择集中的所有元素
{
result = result + i + 1 + ": ElementId = " + agenda.GetEntry(i).ElementId + ", Element type = " + agenda.GetEntry(i).ElementType + "\n";//将元素ID与元素类型信息写入字符串中
}
MessageBox.Show(result);//输出字符串结果
}
移除选择集中的元素
当我们在选择集中获得到一些不符合最终需求的元素时,我们可以首先将不符合需求的元素筛选出来,而后再将元素移出选择集
public static void ModifyElemsInSelection1(string unparsed)//Case:DistinguishNestingElement
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得当前激活的模型控件
ElementAgenda agenda = new ElementAgenda();//声明元素容器
SelectionSetManager.BuildAgenda(ref agenda);//从当前选择集中获得元素
for (uint i = 0; i < agenda.GetCount(); i++)//遍历选择集中的所有元素
{
if (agenda.GetEntry(i).ElementType == MSElementType.Solid)//通过元素类型为Solid筛选出对应元素
{
SelectionSetManager.RemoveElement(agenda.GetEntry(i), dgnModel);//移除选择集中元素类型为Solid的元素
}
}
}
在本案例中,我们首先使用框选的方式选中了参数化实体与普通实体,在代码中对元素类型为普通实体的元素进行过滤,对齐进行移除选择集的操作,所得结果如下
倒置选择集中的元素
当我们需要选择大部分而排除小部分相同属性的元素时,若选择大部分元素时,则十分浪费时间,那么如果我们可以选择小部分元素,对该属性元素进行反选,那么则会大幅度提高效率
SelectionSetManager.InvertElement()用于对相同属性类型的元素执行反向选择的操作,即当模型中相同属性的元素为未被选中状态,则执行该方法后该元素变为选中状态,反之则反
public static void ModifyElemsInSelection2(string unparsed)//Case:DistinguishNestingElement
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得当前激活的模型控件
ModelElementsCollection elems = dgnModel.GetGraphicElements();//获得当前模型中的所有图形元素
foreach (Element elem in elems)//遍历选择集中的所有元素
{
if (elem.ElementType == MSElementType.Solid)//通过元素类型为Solid筛选出对应元素
{
SelectionSetManager.InvertElement(elem, dgnModel);//倒置元素类型为Solid的元素
}
}
}
在本案例中,首先在模型中选择了部分参数化实体与普通实体,在执行代码后,对于条件过滤中的普通实体执行选择集倒置操作,因此原来被选中的普通实体变为未选中状态,而参数化实体保持不变
替换选择集中的元素
当我们在做管线设计时,如果发现管线某个位置与结构或建筑设计的构件存在碰撞,需要高亮由多个元素构成的整条管线,那么就有可能涉及到将选择的部分构件替换为其他构件
SlelctionSetManager.ReplaceWithElement()方法主要用于选择集中的元素集替换为其他元素,其表现于剔除选择集中的元素并添加用于替换的元素到选择集中
public static void ModifyElemsInSelection3(string unparsed)//Case:DistinguishNestingElement
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得当前激活的模型控件
double uorPerMas = Session.Instance.GetActiveDgnModel().GetModelInfo().UorPerMaster;//分辨率单位转换为主单位
ElementAgenda agenda = new ElementAgenda();//声明元素容器
SelectionSetManager.BuildAgenda(ref agenda);//从当前选择集中获得元素
if (agenda.GetCount() > 0)
{
DisplayableElement displayableElem = agenda.GetEntry(0) as DisplayableElement;//获得选择集元素对应的父类元素
displayableElem.GetTransformOrigin(out DPoint3d elemLoc);//从该父类中获得选中元素的定位坐标
EllipseElement ellipse = new EllipseElement(dgnModel, null, elemLoc, 10 * uorPerMas, 5 * uorPerMas, DMatrix3d.Identity);//声明椭圆元素
ellipse.AddToModel();//将椭圆元素添加到模型中
BentleyStatus status = SelectionSetManager.ReplaceWithElement(ellipse, dgnModel);//将该元素的选中状态替换为椭圆选中
}
}
在本案例中,首先创建一个椭圆元素,然后将椭圆元素放置在选择集中索引为0的插入点处,并且将选择集中的元素替换为新创建的椭圆元素
添加选择集中的元素
若我们需要通过代码的方式获得模型中指定属性的元素集,除了使用MicroStation内置的选择集,我们也可以根据过滤条件筛选出符合要求的元素集,并添加到选择集中供下一步操作
SelectionSetManager.AddElement()的功能为将元素添加到选择集中
public static void AddElemsInSelection(string unparsed)//Case:ElementType
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();//获得当前激活的模型控件
ElementAgenda agenda = new ElementAgenda();//声明元素容器
SelectionSetManager.EmptyAll();//清空选择集(主要为了防止因前期选择部分元素造成的选择集项目混乱)
ModelElementsCollection elems = dgnModel.GetGraphicElements();//获得当前模型中的所有图形元素
foreach (Element elem in elems)//遍历模型中的所有元素
{
if (elem.ElementType == MSElementType.Solid)//通过元素类型为Solid筛选出对应元素
{
SelectionSetManager.AddElement(elem, dgnModel);//将筛选后的元素写入模型
}
}
}
在本案例中,首先对选择集进行清空操作,以防若选择集中存在元素造成误导,然后对模型中的元素进行遍历,将元素属性行为普遍实体的元素添加到选择集中