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: 

Auto split Floor by intersect Grids

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
manhgt214
155 Views, 2 Replies

Auto split Floor by intersect Grids

I tried split 1 floor by 1 grid but stuck when auto split by multiple Grids intersect it.

How to good solution for it.
Thank!

I stuck with multiple grids cut floor.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Controls;
using System.Xml.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Grid = Autodesk.Revit.DB.Grid;

namespace TestProject
{
    [Transaction(TransactionMode.Manual)]
    public class SplitFloor : IExternalCommand
    {
        UIDocument _uidoc;
        Document _doc;

        public Result Execute(ExternalCommandData commdata, ref string msg, ElementSet elemset)
        {
            _uidoc = commdata.Application.ActiveUIDocument;
            _doc = _uidoc.Document;
            List<Line> floorboundary = new List<Line>();
            CurveLoop Floorpart1 = new CurveLoop();
            CurveLoop Floorpart2 = new CurveLoop();
            FloorFilter _onlyFloors = new FloorFilter();
            GridFilter _onlyGrids = new GridFilter();
            RefPlaneFilter _onlyRefs = new RefPlaneFilter();
            Reference floorref = _uidoc.Selection.PickObject(ObjectType.Element, _onlyFloors, "Select Floor:");
            //Reference refref = _uidoc.Selection.PickObject(ObjectType.Element, _onlyGrids, "Select Grid:");

            List<Grid> allgrids = new FilteredElementCollector(_doc).OfClass(typeof(Grid)).WhereElementIsNotElementType().Cast<Grid>().ToList();

            foreach(Grid g in allgrids)
            {
                ReferencePlane refplane;

                using (Transaction newplane = new Transaction(_doc, "newplane"))
                {
                    newplane.Start();
                    refplane = _doc.Create.NewReferencePlane(g.Curve.GetEndPoint(0), g.Curve.GetEndPoint(1), new XYZ(0, 0, 1), _doc.ActiveView);
                    newplane.Commit();
                }

                Floor testfloor = _doc.GetElement(floorref.ElementId) as Floor;
                //ReferencePlane refplane = _doc.GetElement(refref.ElementId) as ReferencePlane;
                Sketch floorsketch = _doc.GetElement(testfloor.SketchId) as Sketch;
                int placeholder = 0;
                foreach (CurveArray curvarr in floorsketch.Profile)
                {
                    foreach (Line test in curvarr)
                    {
                        floorboundary.Add(test);
                    }
                }
                //creating a line to instersect with from the ref plane
                XYZ refstart = new XYZ(refplane.BubbleEnd.X, refplane.BubbleEnd.Y, floorboundary.First().GetEndPoint(0).Z);
                XYZ refend = new XYZ(refplane.FreeEnd.X, refplane.FreeEnd.Y, floorboundary.First().GetEndPoint(0).Z);
                Line refline = Line.CreateBound(refstart, refend);
                XYZ firstintersection = null;
                IntersectionResultArray intarray = new IntersectionResultArray();
                for (int i = 0; i < floorboundary.Count(); i++)
                {
                    SetComparisonResult result = floorboundary[i].Intersect(refline, out intarray);
                    if (result.Equals(SetComparisonResult.Overlap))
                    {
                        foreach (IntersectionResult intpoints in intarray)
                        {
                            firstintersection = intpoints.XYZPoint;
                            Line newline1 = Line.CreateBound(floorboundary[i].GetEndPoint(0), intpoints.XYZPoint);
                            Line newline2 = Line.CreateBound(intpoints.XYZPoint, floorboundary[i].GetEndPoint(1));
                            Floorpart1.Append(newline1);
                            Floorpart2.Append(newline2);
                        }
                        placeholder = i + 1;
                        break;
                    }
                    else
                    { Floorpart1.Append(floorboundary[i]); }
                }
                intarray.Clear();
                for (int j = placeholder; j < floorboundary.Count(); j++)
                {
                    SetComparisonResult result = floorboundary[j].Intersect(refline, out intarray);
                    if (result.Equals(SetComparisonResult.Overlap))
                    {
                        foreach (IntersectionResult intpoints in intarray)
                        {
                            Line newline2 = Line.CreateBound(floorboundary[j].GetEndPoint(0), intpoints.XYZPoint);
                            Line newline3 = Line.CreateBound(intpoints.XYZPoint, floorboundary[j].GetEndPoint(1));
                            Line newline4 = Line.CreateBound(intpoints.XYZPoint, firstintersection);
                            Line newline5 = newline4.CreateReversed() as Line;
                            Floorpart2.Append(newline2);
                            Floorpart2.Append(newline4);
                            Floorpart1.Append(newline5);
                            Floorpart1.Append(newline3);
                        }
                        placeholder = j + 1;
                        break;
                    }
                    else
                    { Floorpart2.Append(floorboundary[j]); }
                }
                intarray.Clear();
                for (int k = placeholder; k < floorboundary.Count(); k++)
                {
                    Floorpart1.Append(floorboundary[k]);
                }
                List<CurveLoop> FirstSide = new List<CurveLoop>() { Floorpart1 };
                List<CurveLoop> LastSide = new List<CurveLoop>() { Floorpart2 };
                //should probably test if the curveloops are open before continueing
                bool test1 = Floorpart1.IsOpen();
                bool test2 = Floorpart2.IsOpen();
                using (Transaction tr = new Transaction(_doc))
                {
                    tr.Start("Create new floors");
                    Floor.Create(_doc, FirstSide, testfloor.FloorType.Id, testfloor.LevelId);
                    Floor.Create(_doc, LastSide, testfloor.FloorType.Id, testfloor.LevelId);
                    _doc.Delete(testfloor.Id);
                    tr.Commit();
                }
            }

            //Grid grid = _doc.GetElement(refref) as Grid;

            //Reference refref = _uidoc.Selection.PickObject(ObjectType.Element, _onlyRefs, "Select Reference:");

            
            return Result.Succeeded;
        }
    }

    public class FloorFilter : ISelectionFilter
    {
        //filter for modeled elements
        public bool AllowElement(Element elem)
        {
            if (elem.Category != null)
            {
                if (elem.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_Floors))
                {
                    return true;
                }
                else { return false; }
            }
            else { return false; }
        }
        public bool AllowReference(Reference refer, XYZ loc)
        {
            return true;
        }
    }
    public class RefPlaneFilter : ISelectionFilter
    {
        //filter for modeled elements
        public bool AllowElement(Element elem)
        {
            if (elem.Category != null)
            {
                if (elem.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_CLines))
                {
                    return true;
                }
                else { return false; }
            }
            else { return false; }
        }
        public bool AllowReference(Reference refer, XYZ loc)
        {
            return true;
        }
    }
    public class GridFilter : ISelectionFilter
    {
        //filter for modeled elements
        public bool AllowElement(Element elem)
        {
            if (elem.Category != null)
            {
                if (elem.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_Grids))
                {
                    return true;
                }
                else { return false; }
            }
            else { return false; }
        }
        public bool AllowReference(Reference refer, XYZ loc)
        {
            return true;
        }
    }
}

 

2 REPLIES 2
Message 2 of 3
scgq425
in reply to: manhgt214

Hi @manhgt214 :

the line 47 , the floor still the pick one , but when the loop run twice , this line cant pick , beacuse you first loop is split by grid so you cant spilt by other grid .

if you want to split again , i think you can collect the splited floor by grid , and caclute them not only on floor instance .

LanHui Xu
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature

Message 3 of 3
manhgt214
in reply to: scgq425

Options geometryOptions = app.NewGeometryOptions();
GeometryElement geometry = part.get_Geometry(geometryOptions);
// get solid of part then get loops of top face
IList<CurveLoop> topfaceCurveloop = topface.GetEdgesAsCurveLoops();
//then create new Floor by Curveloop.

I found solution by create part by floor intersect grids then create newFloor based part. It's work fine

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report