Quantcast
Channel: All Revit API Forum posts
Viewing all articles
Browse latest Browse all 67020

Automatic space creation problem

$
0
0

Hello again!

This time my problem is as follows.

 

I wrote an addin that batch creates spaces on user selected floorplans in places that do not already contain them [spaces]. The algorithm there is to create spaces at all possible locations for the level and then delete those which overlap with existing ones.

It utilizes "Doc.NewSpaces2 method" which unfortunately will "regenerate the document even in manual regeneration mode" (see API ref.).

This causes unacceptable long execution time on my subject project file.

 

But there is also another method called NewSpace(Level level, Phase phase, UV point) which I hope will not regenerate the document at least after each creation unlike NewSpaces2. This method however must be supplied with a UV point at which the space is to be created.

Back to NewSpaces2 method.. if spaces are created manually via UI in Revit it happens rather fast and I guess its done with this method. Description states that NewSpaces2 creates spaces for every "circuit" (a closed loop I guess) of the given floor and I think it includes the procedure of finding them.

 

So my question is how one can obtain those closed loops that will serve as spaces boundaries?

Especially taking into account that space bounding geometry is in linked fileand there might not be created rooms at every "cavity" there.

I tried to create a grid of UV points varying its scale (i.e. distance between points) and create a space at each of them but I believe its rather complicated and performance of this approach isn't much greater. So i need your help!

 

The code for the slow version is here

#region Namespaces
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion

namespace SpaceManager
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public static String strNewModelFullName = "";
        public static List<ViewPlan> AllFloorPlanViews = new List<ViewPlan>();
        public static List<int> SelectedFloorPlansIndicies = new List<int>();
        public static double Delta = 5; // 0.5=~150 mm, 5=~1.5m
        //public static double DeltaY = 0.5;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document doc = uidoc.Document;
            Autodesk.Revit.ApplicationServices.Application appsrv = commandData.Application.Application;

            Autodesk.Revit.Creation.Document docCreation = doc.Create;

            ElementCategoryFilter PhasesFilter = new ElementCategoryFilter(BuiltInCategory.OST_Phases);
            FilteredElementCollector AllPhasesFilterCollector = new FilteredElementCollector(doc);
            Phase Fase = new List<Phase>(AllPhasesFilterCollector.WherePasses(PhasesFilter).ToElements().Where(x=>x.Name=="New Construction").Cast<Phase>()).First();

            BuiltInCategory bic = BuiltInCategory.OST_Views;
            BuiltInParameter bip = BuiltInParameter.VIEW_TYPE;

            ParameterValueProvider provider = new ParameterValueProvider(new ElementId(bip));
            FilterStringRuleEvaluator evaluator = new FilterStringContains();
            FilterRule rule = new FilterStringRule(provider, evaluator, "Floor Plan", true);
            ElementParameterFilter name_filter = new ElementParameterFilter(rule);
            AllFloorPlanViews = new List<ViewPlan>(new FilteredElementCollector(doc)
                                                       .OfCategory(bic)
                                                       .WherePasses(name_filter)
                                                       .ToElements().Cast<ViewPlan>());

            List<Space> MEPSpaces = new List<Space>(new FilteredElementCollector(doc)
                                                       .OfCategory(BuiltInCategory.OST_MEPSpaces)                                                       
                                                       .ToElements().Cast<Space>());

            List<Level> Levels = new List<Level>(new FilteredElementCollector(doc)
                                                       .OfCategory(BuiltInCategory.OST_Levels)
                                                       .WhereElementIsNotElementType()
                                                       .ToElements().Cast<Level>());

            Dictionary<ElementId, List<Space>> LevelLandedSpacesDict = new Dictionary<ElementId, List<Space>>();
            Dictionary<ElementId, List<ElementId>> NewLevelLandedSpacesDict = new Dictionary<ElementId, List<ElementId>>();

            foreach (Space MEPSpace in MEPSpaces)
            {
                if (!Levels.Contains(MEPSpace.Level))
                {
                    Levels.Add(MEPSpace.Level);
                }
                if (!LevelLandedSpacesDict.ContainsKey(MEPSpace.Level.Id))
                {
                    LevelLandedSpacesDict.Add(MEPSpace.Level.Id, new List<Space>());
                }
                LevelLandedSpacesDict[MEPSpace.Level.Id].Add(MEPSpace);
            }
            ListBoxForm ListBoxFloors = new ListBoxForm();
            ListBoxFloors.ShowDialog();
            List<ElementId> ElsIDsToDel = new List <ElementId>();
            List<ElementId> NewSpIds = new List<ElementId>();

//SelectedFloorPlansIndicies now contains indicies for selected floorplans in AllFloorPlanViews List

            using (Transaction t = new Transaction(doc))
            {
                t.Start("Creating Spaces");
                foreach (int FPind in SelectedFloorPlansIndicies)
                {                    
                    Level L = AllFloorPlanViews[FPind].GenLevel;
                    if (!LevelLandedSpacesDict.ContainsKey(L.Id))
                    {
                        LevelLandedSpacesDict.Add(L.Id, new List<Space>());
                    }
                    NewLevelLandedSpacesDict.Add(L.Id, new List<ElementId>());
                    NewSpIds = docCreation.NewSpaces2(L, Fase, AllFloorPlanViews[FPind]).ToList<ElementId>();
                        NewLevelLandedSpacesDict[L.Id].AddRange(NewSpIds);

                        foreach (Space OldSp in LevelLandedSpacesDict[L.Id])
                        {
                            foreach (ElementId NewSp in NewLevelLandedSpacesDict[L.Id])
                            {
                                XYZ NewSpXYZ = (doc.GetElement(NewSp).Location as LocationPoint).Point;
                                //XYZ NewSpXYZ = NewSpPt.Point;
                                if (OldSp.IsPointInSpace(NewSpXYZ))
                                {
                                    ElsIDsToDel.Add(NewSp);
                                    NewLevelLandedSpacesDict[L.Id].Remove(NewSp);
                                    break;
                                }
                            }
                        }
               }
                if (ElsIDsToDel.Count > 0)
                {
                    doc.Delete(ElsIDsToDel);
                }
                t.Commit();
            }
            return Result.Succeeded;
        }
}

Viewing all articles
Browse latest Browse all 67020

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>