Community
I'm trying to convert a Beam family instance to a solid for BooleanOperationsUtils.ExecuteBooleanOperation - however this keeps returning null:
Solid depSolid = depElem.get_Geometry(new Options()).OfType<Solid>().FirstOrDefault();
full code:
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
namespace scripts
{
[Transaction(TransactionMode.Manual)]
public class CutGroup : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
// Get element to cut
Reference pickedRef = uidoc.Selection.PickObject(ObjectType.Element, "Select an element to cut");
Element selectedElement = doc.GetElement(pickedRef);
GeometryElement selectedGeometry = selectedElement.get_Geometry(new Options());
if (selectedElement == null)
{
message = "Please select an element.";
return Result.Failed;
}
// Get group
Reference pickedGroupRef = uidoc.Selection.PickObject(ObjectType.Element, "Select a model group");
if (pickedGroupRef == null)
{
message = "Please select a model group.";
return Result.Failed;
}
Group pickedGroup = doc.GetElement(pickedGroupRef.ElementId) as Group;
ElementClassFilter filter = new ElementClassFilter(typeof(FamilyInstance));
IList<ElementId> dependentIds = pickedGroup.GetDependentElements(filter);
// Use a transaction to group cutting operations
Transaction tx = new Transaction(doc);
tx.Start("Cut with Group");
try
{
foreach (GeometryObject selectedGeomObj in selectedGeometry)
{
Solid selectedSolid = selectedGeomObj as Solid;
foreach (ElementId id in dependentIds)
{
Element depElem = doc.GetElement(id);
Solid depSolid = depElem.get_Geometry(new Options()).OfType<Solid>().FirstOrDefault();
if (depSolid != null)
{
Solid cutter = BooleanOperationsUtils.ExecuteBooleanOperation(selectedSolid, depSolid, BooleanOperationsType.Difference);
}
}
}
tx.Commit();
message = "Element cut successfully with group members.";
}
catch (System.Exception ex)
{
tx.RollBack();
message = $"An error occurred: {ex.Message}";
return Result.Failed;
}
return Result.Succeeded;
}
}
}
Solved! Go to Solution.
Solved by DanielKP2Z9V. Go to Solution.
Turned out that I was using wrong function to replicate the "Cut geometry" button. The correct one is SolidSolidCutUtils.AddCutBetweenSolids. Thanks to Aaron.Lu's answer .
Updated code:
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
namespace scripts
{
[Transaction(TransactionMode.Manual)]
public class CutGroup : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
// Get element to cut
Reference pickedRef = uidoc.Selection.PickObject(ObjectType.Element, "Select an element to cut");
Element selectedElement = doc.GetElement(pickedRef);
if (selectedElement == null)
{
message = "Please select an element.";
return Result.Failed;
}
// Get group
Reference pickedGroupRef = uidoc.Selection.PickObject(ObjectType.Element, "Select a model group");
if (pickedGroupRef == null)
{
message = "Please select a model group.";
return Result.Failed;
}
Group pickedGroup = doc.GetElement(pickedGroupRef.ElementId) as Group;
ElementClassFilter filter = new ElementClassFilter(typeof(FamilyInstance));
IList<ElementId> dependentIds = pickedGroup.GetDependentElements(filter);
// Use a transaction to group cutting operations
Transaction tx = new Transaction(doc);
tx.Start("Cut with Group");
try
{
foreach (ElementId id in dependentIds)
{
Element depElem = doc.GetElement(id);
SolidSolidCutUtils.AddCutBetweenSolids(doc, selectedElement, depElem);
}
tx.Commit();
message = "Element cut successfully with group members.";
}
catch (System.Exception ex)
{
tx.RollBack();
message = $"An error occurred: {ex.Message}";
return Result.Failed;
}
return Result.Succeeded;
}
}
}
Can't find what you're looking for? Ask the community or share your knowledge.