Revit API封装一个通用函数“过名称找元素”
感觉这个函数不错。通过这种方式寻找元素经常需要用到。
url:http://greatverve.cnblogs.com/p/FindElementByName.html
//封装一个通用函数“过名称找元素”
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdFindElementByName : IExternalCommand
{
//通过类型与名称找Element
Element findElement(Document _rvtDoc, Type targetType, string targetName)
{
// get the elements of the given type
//
FilteredElementCollector collector = new FilteredElementCollector(_rvtDoc);
collector.WherePasses(new ElementClassFilter(targetType));
// parse the collection for the given name
// using LINQ query here.
//
var targetElems = from element in collector where element.Name.Equals(targetName) select element;
List<Element> elems = targetElems.ToList<Element>();
if (elems.Count > 0)
{ // we should have only one with the given name.
return elems[0];
}
// cannot find it.
return null;
}
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Selection sel = app.ActiveUIDocument.Selection;
View pViewPlan = (View)findElement(doc,typeof(ViewPlan), "Lower Ref. Level");
ReferencePlane refFront = (ReferencePlane)findElement(doc, typeof(ReferencePlane), "Front");
Level lowerLevel = findElement(doc,typeof(Level), "Lower Ref. Level") as Level;
Material pMat = findElement(doc, typeof(Material), "Glass") as Material;
if (refFront != null)
TaskDialog.Show("info", refFront.Name.ToString());
return Result.Succeeded;
}
}
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdFindElementByName : IExternalCommand
{
//通过类型与名称找Element
Element findElement(Document _rvtDoc, Type targetType, string targetName)
{
// get the elements of the given type
//
FilteredElementCollector collector = new FilteredElementCollector(_rvtDoc);
collector.WherePasses(new ElementClassFilter(targetType));
// parse the collection for the given name
// using LINQ query here.
//
var targetElems = from element in collector where element.Name.Equals(targetName) select element;
List<Element> elems = targetElems.ToList<Element>();
if (elems.Count > 0)
{ // we should have only one with the given name.
return elems[0];
}
// cannot find it.
return null;
}
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
Selection sel = app.ActiveUIDocument.Selection;
View pViewPlan = (View)findElement(doc,typeof(ViewPlan), "Lower Ref. Level");
ReferencePlane refFront = (ReferencePlane)findElement(doc, typeof(ReferencePlane), "Front");
Level lowerLevel = findElement(doc,typeof(Level), "Lower Ref. Level") as Level;
Material pMat = findElement(doc, typeof(Material), "Glass") as Material;
if (refFront != null)
TaskDialog.Show("info", refFront.Name.ToString());
return Result.Succeeded;
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。