Removing overlapped Family with a link Model elements
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hallo everyone, i am trying to remove the overlapping or intersected imported family with the linked elements, i tried to the bounding box method but it didnt work, both overlapps as well in the 3d is there any method that could help to get that. thank you so much.
this was my try of finding the intersected grid with linkedElements :
"
private bool FamilyInstanceOverlaps(FamilyInstance instance, List<Element> linkedElements)
{
BoundingBoxXYZ bb = instance.get_BoundingBox(null);
if (bb == null)
{
return false;
}
Outline outline = new Outline(bb.Min, bb.Max);
BoundingBoxIntersectsFilter intersectsFilter = new BoundingBoxIntersectsFilter(outline);
foreach (var element in linkedElements)
{
BoundingBoxXYZ elementBox = element.get_BoundingBox(null);
if (elementBox != null && BoundingBoxIntersects(bb, elementBox))
{
return true;
}
}
return false;
}
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
uiapp = commandData.Application;
uidoc = uiapp.ActiveUIDocument;
doc = uidoc.Document;
FilteredElementCollector RVT_Links = new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(RevitLinkInstance));
List<Element> Links_List = new List<Element>();
List<XYZ> centers = new List<XYZ>();
List<string> exe = new List<string>();
BoundingBoxXYZ bb = new BoundingBoxXYZ();
double largestVolume = 0;
BoundingBoxXYZ largestBoundingBox = null;
RevitLinkInstance largestElement = null;
foreach (RevitLinkInstance element in RVT_Links)
{
Document linkedDoc = element.GetLinkDocument();
BoundingBoxXYZ linkbounding = element.get_BoundingBox(doc.ActiveView);
if (linkbounding != null)
{
double volume = (linkbounding.Max.X - linkbounding.Min.X) *
(linkbounding.Max.Y - linkbounding.Min.Y) *
(linkbounding.Max.Z - linkbounding.Min.Z);
if (volume > largestVolume)
{
largestVolume = volume;
largestBoundingBox = linkbounding;
largestElement = element;
}
}
}
if (largestElement == null)
{
return Result.Failed;
}
XYZ MinboundingBoxPoint = new XYZ(largestBoundingBox.Min.X, largestBoundingBox.Min.Y, largestBoundingBox.Min.Z);
XYZ MaxboundingBoxPoint = new XYZ(largestBoundingBox.Max.X, largestBoundingBox.Max.Y, largestBoundingBox.Max.Z);
double cellWidth = 1;
double cellHeight = 1;
// Number of grid cells in each direction
int numCellsX = (int)Math.Ceiling((MaxboundingBoxPoint.X - MinboundingBoxPoint.X) / cellWidth);
int numCellsY = (int)Math.Ceiling((MaxboundingBoxPoint.Y - MinboundingBoxPoint.Y) / cellHeight);
View activeView = doc.ActiveView;
string path = @"\.....\Grid.rfa";
List<Solid> linkedSolids = GetAllSolidsFromLinkedElements(largestElement);
using (Transaction trans = new Transaction(doc, "CreateSurfaceGrid"))
{
trans.Start();
Family family;
string familyName = "Grid";
family = new FilteredElementCollector(doc)
.OfClass(typeof(Family))
.Cast<Family>()
.FirstOrDefault(fam => fam.Name == familyName);
if (family == null)
{
if (!doc.LoadFamily(path, out family))
{
TaskDialog.Show("Error", "The family file could not be loaded.");
return Result.Failed;
}
}
// Get the first available family symbol (if there is one)
FamilySymbol symbol = family.GetFamilySymbolIds().Select(id => doc.GetElement(id)).Cast<FamilySymbol>().FirstOrDefault();
if (symbol == null)
{
TaskDialog.Show("Error", "The family does not contain any symbols.");
return Result.Failed;
}
// Activate the family symbol (this is necessary before it can be used)
if (!symbol.IsActive)
symbol.Activate();
// Create a list to store all grid instances
List<FamilyInstance> allGridInstances = new List<FamilyInstance>();
for (int i = 0; i < numCellsX; i++)
{
for (int j = 0; j < numCellsY; j++)
{
// Define the position of the family instance
XYZ origin = new XYZ(MinboundingBoxPoint.X + i * cellWidth, MinboundingBoxPoint.Y + j * cellHeight, largestBoundingBox.Min.Z);
// Create a new family instance and add to the list
FamilyInstance instance = doc.Create.NewFamilyInstance(origin, symbol, doc.ActiveView.GenLevel, StructuralType.NonStructural);
allGridInstances.Add(instance);
}
}
// Collect elements in the linked document for collision detection
Document linkedDoc = largestElement.GetLinkDocument();
List<Element> linkedElements = null;
if (linkedDoc != null)
{
linkedElements = new FilteredElementCollector(linkedDoc).WhereElementIsNotElementType().ToElements().ToList();
}
if (linkedElements != null)
{
foreach (var instance in allGridInstances)
{
if (FamilyInstanceOverlaps(instance, linkedElements))
{
doc.Delete(instance.Id);
}
}
}
// Re-collect the remaining grid instances
allGridInstances = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).OfCategoryId(symbol.Category.Id).Cast<FamilyInstance>().ToList();
if (linkedElements != null)
{
foreach (var instance in allGridInstances)
{
if (FamilyInstanceOverlaps(instance, linkedElements))
{
doc.Delete(instance.Id);
//TaskDialog.Show("Info", "Instance deleted due to overlap");
}
}
}
trans.Commit();
}
this is what i am willing to get
3d view