Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
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: 

Can't understand how to obtain solids out of a group

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

Can't understand how to obtain solids out of a group

Does anyone have any guides to understand how to get geometry to perform BooleanOperations ?

I have a group of structural framing elements and trying to cut them out of a toposolid. I'm new to Revit API and can't "unpack" the combo: Group -> Geometry -> GeometryInstance/SymbolGeometry/Solid...

 

Code so far (the confusing bit is in the foreach loop):

 

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace scripts
{
    [Transaction(TransactionMode.Manual)]
    public class CutWithGroup : IExternalCommand
    {
      public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
      {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;

            // Get the currently selected element
            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;
            }

            // Prompt user to select a group
            Reference pickedGroupRef = uidoc.Selection.PickObject(ObjectType.Element, "Select a model group");
            Group pickedGroup = doc.GetElement(pickedGroupRef.ElementId) as Group;
                Options options = new Options();
                options.DetailLevel = ViewDetailLevel.Coarse;
                options.IncludeNonVisibleObjects = true;
            GeometryElement pickedGroupGeometry = pickedGroup.get_Geometry(options);
            if (pickedGroup == null)
            {
              message = "Please select a model group.";
              return Result.Failed;
            }

            // 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 (GeometryInstance pickedGroupGeoInst in pickedGroupGeometry)
                        {
                            GeometryElement geometryElement = pickedGroupGeoInst.GetSymbolGeometry();

                            foreach (GeometryObject geomObj in geometryElement)
                            {
                            Solid cutter = BooleanOperationsUtils.ExecuteBooleanOperation(selectedSolid, geomObj as Solid, 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

Sorry for duplicate question. The solution turned out to be using SolidSolidCutUtils.AddCutBetweenSolids instead of BooleanOperationsUtils.ExecuteBooleanOperation.

 

Full 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 CutGeometryWithGroup : 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