// check intersection by box
public static void chkInterBound( ExternalCommandData commandData)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
var app = uiApp.Application;
Document doc = uiDoc.Document;
// Select something to use as base bounding box.
Reference r = uiDoc.Selection.PickObject( Autodesk.Revit.UI.Selection.ObjectType.Element);
// Find the bounding box from the selected
// object and convert to outline.
Element Er = doc.GetElement(r.ElementId);
BoundingBoxXYZ bb = Er.get_BoundingBox( doc.ActiveView);
Outline outline = new Outline(bb.Min, bb.Max);
// Create a BoundingBoxIntersectsFilter to
// find everything intersecting the bounding
// box of the selected element.
BoundingBoxIntersectsFilter bbfilter
= new BoundingBoxIntersectsFilter(outline);
// Use a view to construct the filter so we
// get only visible elements. For example,
// the analytical model will be found otherwise.
FilteredElementCollector collector
= new FilteredElementCollector(
doc, doc.ActiveView.Id);
// Lets also exclude the view itself (which
// often will have an intersecting bounding box),
// and also the element selected.
ICollection<ElementId> idsExclude
= new List<ElementId>();
idsExclude.Add(r.ElementId);
// No need for this, BoundingBoxIntersectsFilter
// excludes all objects derived from View, as
// pointed out by Jim Jia below:
//
//idsExclude.Add( doc.ActiveView.Id );
// Get the set of elements that pass the
// criteria. Note both filters are quick,
// so order is not so important.
collector.Excluding(idsExclude)
.WherePasses(bbfilter);
// Generate a report to display in the dialog.
int nCount = 0;
string report = string.Empty;
foreach (Element e in collector)
{
string name = e.Name;
report += "\nName = " + name
+ " Element Id: " + e.Id.ToString();
nCount++;
}
TaskDialog.Show(
"Bounding Box + View + Exclusion Filter",
"Found " + nCount.ToString()
+ " elements whose bounding box intersects\n"+
report);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น