Join Geometry is not working
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType
import itertools
from System import Math
def get_non_empty_solid(element):
# Function to retrieve a non-empty solid from the element's geometry
options = Options()
options.ComputeReferences = True
options.DetailLevel = ViewDetailLevel.Fine
geometry = element.get_Geometry(options)
for item in geometry:
if isinstance(item, Solid) and item.Volume > 0:
return item
def join_elements(element1, element2):
# Function to join two elements if they are not already joined
try:
JoinGeometryUtils.JoinGeometry(doc, element1, element2)
return True
except Exception as e:
# Add more specific exception handling if needed
print("Failed to join elements: {e}")
return False
def main():
# Get the active Revit document and active UI document
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
# Get the currently selected elements
selected_elements = [doc.GetElement(elId) for elId in uidoc.Selection.GetElementIds()]
# Generate combinations of two elements from the selection
element_combinations = list(itertools.combinations(selected_elements, 2))
# List to store results
results = []
# Iterate through pairs of elements
for element1, element2 in element_combinations:
# Check if elements are already joined
if not JoinGeometryUtils.AreElementsJoined(doc, element1, element2):
# Attempt to get non-empty solids from the elements
solid1 = get_non_empty_solid(element1)
solid2 = get_non_empty_solid(element2)
# Check if solids are valid and perform intersection
if solid1 and solid2:
intersection_solid = BooleanOperationsUtils.ExecuteBooleanOperation(solid1, solid2, BooleanOperationsType.Intersect)
# Check if the volume of the intersection is significant
if Math.Abs(intersection_solid.Volume) > 0.00000000001:
# Attempt to join the elements
if join_elements(element1, element2):
results.append("Elements {element1.Id} and {element2.Id} joined successfully")
else:
results.append("Failed to join elements {element1.Id} and {element2.Id}")
else:
results.append("Elements {element1.Id} and {element2.Id} do not intersect significantly")
else:
results.append("No valid solids found for elements {element1.Id} and {element2.Id}")
else:
results.append("Elements {element1.Id} and {element2.Id} are already joined")
# Display results
for result in results:
print(result)
if __name__ == "__main__":
main()