Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get Solid from FamilyInstance - keeps returning null

1 REPLY 1
SOLVED
Reply
Message 1 of 2
DanielKP2Z9V
206 Views, 1 Reply

Get Solid from FamilyInstance - keeps returning null

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;
      }
    }
}
1 REPLY 1
Message 2 of 2
DanielKP2Z9V
in reply to: DanielKP2Z9V

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.

Post to forums  

Rail Community


Autodesk Design & Make Report