Hi @julia_morseP8AHB ,
Based on my understanding, here’s what you need to do:
- Create and save a selection set by choosing the model items.
- You can retrieve the bounding box using
doc.CurrentSelection.SelectedItems.BoundingBox
.
- To expand the bounding box, you can use the following code snippet.
- You can check if two bounding boxes intersect or if one is contained within another by using the
Intersects
or Contains
methods. Please take a look at these links BoundingBox3D.Contains Method (BoundingBox3D) and BoundingBox3D.Intersects Method
- By following these steps, you can identify nearby items.
I’ve put together a rough code example (not entirely sure it will solve your issue, but it should give you a good starting point).
public class Class1 : AddInPlugin //Derives from AddInPlugin
{
public override int Execute(params string[] parameters)
{
MyNwCode();
return 0;
}
private void MyNwCode()
{
Document doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
//Step 1: Create Set from Collection
CreateSet(doc);
//Step 2: Get BoundingBox of items in set
BoundingBox3D boundingBox3D = doc.CurrentSelection.SelectedItems.BoundingBox(true);
//Step 3: Expand BoundingBox
int expansionFactor = 20;
BoundingBox3D expandBoundingBox = ExpandBoundingBox(boundingBox3D, expansionFactor);
//Step 4: Get Required BoundingBox intersecting Elements
IEnumerable<ModelItem> requiredElements = BoundingBoxIntersectingElements(doc, expandBoundingBox);
MessageBox.Show("Intersecting Elements = " + requiredElements.Count().ToString());
}
private IEnumerable<ModelItem> BoundingBoxIntersectingElements(Document doc, BoundingBox3D expandBoundingBox)
{
doc.CurrentSelection.SelectAll();
ModelItemCollection collection = doc.CurrentSelection.SelectedItems;
Search search = new Search();
search.Selection.SelectAll();
search.SearchConditions.Add(SearchCondition.HasPropertyByDisplayName("Element ID", "Value"));
ModelItemCollection allItems = search.FindAll(doc, false);
IEnumerable<ModelItem> items1 =allItems.Where(y => y.BoundingBox(true).Intersects(expandBoundingBox));
IEnumerable<ModelItem> items2 = allItems.Where(y => expandBoundingBox.Contains(y.BoundingBox(true)));
return items2;
}
private BoundingBox3D ExpandBoundingBox(BoundingBox3D box, int expansionAmount)
{
return new BoundingBox3D(
new Point3D(box.Min.X - expansionAmount, box.Min.Y - expansionAmount, box.Min.Z - expansionAmount),
new Point3D(box.Max.X + expansionAmount, box.Max.Y + expansionAmount, box.Max.Z + expansionAmount));
}
private void CreateSet(Document doc)
{
ModelItemCollection modelItemCollection = doc.CurrentSelection.SelectedItems;
SavedItem newSelectionSet = new SelectionSet(modelItemCollection);
newSelectionSet.DisplayName = "MySelectionSet";
doc.SelectionSets.InsertCopy(0, newSelectionSet);
}
}
I hope this helps!
Naveen Kumar T
Developer Technical Services
Autodesk Developer Network