Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Cut Geometry via the API

Anonymous

Cut Geometry via the API

Anonymous
Not applicable

I have a series of FamilyInstances based on the Generic Model template, positioned in the drawing. Currently they overlap / clash (deliberately). 

 

If I select Cut Geometry from the ribbon, select one (say, the sloping rail) and then the other (an upright), it correctly cuts the upright clean through the rail, so they no longer clash. So it seems my elements as they stand qualify for this type of operation. 

 

However, I am unable to get this to work through code.

 

I tried SolidSolidCutUtils but it threw an exception, stating that they didn't qualify (possibly because I did not extract solids from the geometry?). I passed the Element representing the FamilyInstance as a whole. 

 

I also tried using InstanceVoidCutUtils. Although that didn't throw an exception, it didn't do anything either. My family is not marked as 'Cut with voids on load' - but if that were necessary, surely Cut Geometry from the ribbon would not work? 

 

Any suggestions on how to do this? BooleanOperationUtils does not seem to be of any use because one method returns an in-memory solid and I have no known way of setting that result as the new solid for the element (it specifically says it cannot be added to the database), and the other that modifies directly says it can't be a solid directly from an element. 

 

If extracting the solids from the geometry (I saw a sample on how) is the right way to use SolidSolidCutUtils, how can I get the resulting solids back into the element to update it to the 'cut' version? 

 

The attached image shows the arrangement of elements, and the one on the right is one cut by me via Revit's interface. That's what I want to do through code. 

 

0 Likes
Reply
5,158 Views
5 Replies
Replies (5)

Aaron.Lu
Autodesk
Autodesk
Dear, would you please upload a simple rvt file for me to play with?
and you want to do what UI provides? i.e. cut one element with another?


Aaron Lu
Developer Technical Services
Autodesk Developer Network
0 Likes

Anonymous
Not applicable

Attached is a .rvt file with the generic model solids in place, roughly as shown in the image (but with nothing cut yet). 

 

Also attached is the .rfa file from which those solids were generated - it is loaded under Generic Models in the Families tree. 

 

If you select Cut Geometry and select the sloping solid, then one of the vertical solids that currently clash with it, the sloping rail will cut around the vertical solid as expected. 

 

I would like to be able to achieve this same cut in code. Assume I have no problem accessing the solids as Element objects. 

 

(If you can figure out a way to actually chop the vertical solids to the underside of the rail - i.e. cut short rather than cut through - that would be a bonus!) 

 

Thank you. 

 

Aaron.Lu
Autodesk
Autodesk

Dear, thanks for providing the sample files, and appologize to not respond in time.

 

I tried the following code, it work, would you like to check it again? thanks.

 

var eleToBeCut = doc.GetElement(new ElementId(218107));
var ele2 = doc.GetElement(new ElementId(218142));
using (Transaction transaction = new Transaction(doc))
{
    transaction.Start("Try cut via API");
    SolidSolidCutUtils.AddCutBetweenSolids(doc, eleToBeCut, ele2);
    transaction.Commit();
}


Aaron Lu
Developer Technical Services
Autodesk Developer Network

Moustafa_K
Collaborator
Collaborator

Thanks for this valid question as well as the answer from Autodesk. I would like to point another issue under the same subject. I will explain in steps as follow:

 

  1. I have created a StructureConnection category void cuted family. 
  2. loaded into projec.
  3. performed a UI test to cut with any other object in the project and every thing was fine.
  4. I have undone step 3 and started an API lines as follow
  5. Reference r1 = m_uidoc.Selection.PickObject(ObjectType.Element);
                Element movmentjoint = m_doc.GetElement(r1.ElementId);
    
                Options op = new Options();
                op.ComputeReferences = true;
                GeometryElement ge = movmentjoint.get_Geometry(op);
                Solid sol = null;
                foreach (GeometryObject geo in ge)
                {
                    if (geo == null) continue;
                    if (geo is Solid)
                    {
                        sol = geo as Solid;
                        break;
                    }
                }
                ElementIntersectsSolidFilter solins = new ElementIntersectsSolidFilter(sol);
    
                FilteredElementCollector filcol = new FilteredElementCollector(m_doc, m_doc.ActiveView.Id).WherePasses(solins);
    
                foreach (Element e in filcol)
                {
                 
                    
                    if (t.HasEnded()) t.Start();
    
                    InstanceVoidCutUtils.AddInstanceVoidCut(m_doc, e, m_doc.GetElement(r1));
    
                    t.Commit();
                }
    4. Every thing is fine and the result is as excpected.
  6. now when i try to rerun the application again, so as to cut new objects in addition what is already cut. i got this exception
  7. Solid Function Exception.PNG

 

i think "may be i am wrong" when the cutting instance is already cutting objects, i will not be able to capture its solid geometry. is this true? and how i may be able to solve this.

also to mention i have tried to uncut all and recut again, the result was nothing and the cutting instance was removed from the project.

using these statments

 foreach (ElementId cutted in InstanceVoidCutUtils.GetElementsBeingCut(movmentjoint))
            {
                if (t.HasEnded()) t.Start();
                InstanceVoidCutUtils.RemoveInstanceVoidCut(m_doc, m_doc.GetElement(cutted), movmentjoint);
                t.Commit()
            }

Thanks

 -----------------------------------------------------

 

Edited01 22 oct

--------------------

it is may be also that cutting family instance is subjected to x number of objects to allow cutting? i have tested again the working part of the above but on multible elements but again stopped by this exception:

multiple cut exception.PNG

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1

NickBroadbent
Participant
Participant

I'm not getting this issue in Revit 2021.
I think this issue has been fixed.

To be clear this is what I'm doing;

In a Revit Project (Not family) I'm running the "AddInstanceVoidCut" command multiple times and it is working to cut the additional elements each time I run it.


InstanceVoidCutUtils.AddInstanceVoidCut(doc, toBeCutEle, cuttingEle);

0 Likes