NavisWorks二次开发-获取当前选择集的属性值
下面给出一个例子:
把构件中所有含有元素特性的全部筛选出来。
废话不多说,直接上代码。
public void GetHandle(Document doc)//一个获取实体句柄的函数
{
string handlevalue=null;
string[] HandleValue=null;
string EntityHandle;
List<Model_Para> para = new List<Model_Para>();
if (Application.ActiveDocument != null &&
!Application.ActiveDocument.IsClear)
{
//documentControl1.Document.Tool.Value = Autodesk.Navisworks.Api.Tool.Select;
foreach (ModelItem item in Autodesk.Navisworks.Api.Application.ActiveDocument.CurrentSelection.SelectedItems)
{
Model_Para model_Para = new Model_Para();//放循环外面会造成添加到集合里的元素参数变成一样的,并且导致下面的集合add之后只有一个元素
//iterate the item's PropertyCategory entries lcldrevit_parameter_225862 225864 570150 570152
foreach (PropertyCategory category in item.PropertyCategories)
{
if (category.Name== "LcRevitData_Element")
{
foreach (DataProperty dataProperty in category.Properties)
{
string name = dataProperty.DisplayName;
if (name.EndsWith("编码"))//"570150 LcOaNat64AttributeValue"我个人理解是在系统中的名称
{
handlevalue = dataProperty.Value.ToString();//得到是字符串类似“displaystring:EB4”
HandleValue = handlevalue.Split(':');//按“:”分割
model_Para.Para = HandleValue[1];
model_Para.modelItem = item;
para.Add(model_Para);
}
if (name.EndsWith("编码描述"))
{
handlevalue = dataProperty.Value.ToString();
HandleValue = handlevalue.Split(':');
model_Para.Define = HandleValue[1];
}
if (dataProperty.CombinedName.Name.ToString()=="LcRevitPropertyElementId")
{
handlevalue = dataProperty.Value.ToString();
HandleValue = handlevalue.Split(':');
model_Para.Id = HandleValue[1];
}
}
}
}
}
}
}
现在的问题是如何根据自己设置的条件检索所有具有某种参数的构件集合呢?
具体的代码如下:
public void Search_Test()
{
Document doc = Application.ActiveDocument;
Search search = new Search();
SearchCondition searchCondition = SearchCondition.HasCategoryByDisplayName("元素");
//searchCondition = searchCondition.DisplayStringContains("项目编码");
//SearchCondition searchCondition = SearchCondition.HasPropertyByDisplayName("元素", "项目编码");
search.SearchConditions.Add(searchCondition);
search.Selection.SelectAll();
search.Locations = SearchLocations.DescendantsAndSelf;
if (Application.ActiveDocument!=null)
{
var selection = Application.ActiveDocument.CurrentSelection;
var root = doc.SelectionSets.RootItem;
Application.ActiveDocument.CurrentSelection.CopyFrom(search.FindAll(Application.ActiveDocument, false));
ModelItemCollection modelCollec = new ModelItemCollection();
foreach (var item in selection.SelectedItems)
{
modelCollec.Add(item);
}
SelectionSet selectionSet = new SelectionSet(modelCollec);
selectionSet.DisplayName = "筛选的含有编码选择集";
//doc.SelectionSets.AddCopy((root.Children[0] as GroupItem).Children[0] as GroupItem,selectionSet);
}
}