Esempio n. 1
0
        public KeyValuePair <List <ElementId>, List <double> > TravelDis(Document doc, ICollection <ElementId> selectedIds, List <ElementId> RoomsForbid) //distances of all rooms on current level to nearest exit
        {
            View currentView = doc.ActiveView;

            //door location
            var doors = new List <ElementId>();

            doors = GetExits(doc);
            var doors_loc = new List <XYZ>();

            foreach (ElementId id in doors)
            {
                Element       door = doc.GetElement(id);
                LocationPoint loc  = door.Location as LocationPoint;
                XYZ           xyz  = loc.Point;
                doors_loc.Add(xyz);
            }
            //room location
            var levelid   = ViewLevel(doc);
            var rooms     = GetRoomsOnLevel(doc, levelid);
            var final_rel = new List <double>();
            var rooms_loc = CenterOfRoom(doc, rooms);

            //TaskDialog.Show("Revit", doors_loc.Count.ToString());
            //TaskDialog.Show("Revit", rooms_loc.Count.ToString());
            var Exit2Door = new List <XYZ>();

            using (TransactionGroup transGroup = new TransactionGroup(doc))
            {
                transGroup.Start("group start");
                using (Transaction trans_del = new Transaction(doc))
                {
                    trans_del.Start("Del");
                    foreach (ElementId id in RoomsForbid)
                    {
                        Element temp = doc.GetElement(id);
                        DeleteDoorsOfRoom(doc, id);
                    }
                    trans_del.Commit();
                }
                using (Transaction trans = new Transaction(doc))
                {
                    if (trans.Start("Path") == TransactionStatus.Started)
                    {
                        //PathOfTravel.CreateMapped(currentView, rooms_loc, doors_loc);

                        //try to find the shortest path to the exits(one of)
                        //var ig = new List<ElementId>();
                        var settings = RouteAnalysisSettings.GetRouteAnalysisSettings(doc);
                        //foreach (ElementId id in selectedIds)
                        //{
                        //    Element temp = doc.GetElement(id);
                        //    ig.Add(temp.Category.Id);
                        //}
                        settings.SetIgnoredCategoryIds(selectedIds);
                        foreach (XYZ r in rooms_loc)
                        {
                            double temp_len = 10000000;
                            XYZ    temp_loc = null;
                            int    cnt      = 0;
                            foreach (XYZ d in doors_loc)
                            {
                                PathOfTravel path = PathOfTravel.Create(currentView, r, d);
                                if (path == null)
                                {
                                    continue;
                                }
                                IList <Curve> p = path.GetCurves();
                                if (temp_len >= calDis(p))
                                {
                                    temp_loc = d;
                                    temp_len = calDis(p);
                                }
                            }
                            Exit2Door.Add(temp_loc);
                        }
                        trans.RollBack();

                        //TaskDialog taskdialog = new TaskDialog("Revit");
                        //taskdialog.MainContent = "Click [OK] to commot and click [cancel] to roll back";
                        //TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel;
                        //taskdialog.CommonButtons = buttons;
                        //if (TaskDialogResult.Ok == taskdialog.Show())
                        //{
                        //    if (TransactionStatus.Committed != trans.Commit()) {
                        //        TaskDialog.Show("Fail", "Trans can not be committed");
                        //    }
                        //}
                        //else {
                        //    trans.RollBack();
                        //}
                    }
                }

                var RoomsPoint = rooms_loc;

                using (Transaction trans2 = new Transaction(doc))
                {
                    if (trans2.Start("Path_final") == TransactionStatus.Started)
                    {
                        var settings = RouteAnalysisSettings.GetRouteAnalysisSettings(doc);

                        settings.SetIgnoredCategoryIds(selectedIds);
                        for (int i = 0; i < RoomsPoint.Count; i++)
                        {
                            XYZ    d         = Exit2Door[i];
                            XYZ    r         = RoomsPoint[i];
                            Room   temp_room = doc.GetRoomAtPoint(r);
                            double halfDia   = calHalfDia(temp_room);
                            if (r == null || d == null)
                            {
                                final_rel.Add(MAX_NUM);
                                continue;
                            }
                            ;
                            IList <Curve> path = PathOfTravel.Create(currentView, r, d).GetCurves();
                            final_rel.Add(calDis(path));
                        }
                        trans2.Commit();
                    }
                }
                transGroup.Assimilate();
            }
            var allRoomName = new List <ElementId>();

            foreach (Room r in rooms)
            {
                allRoomName.Add(r.Id);
            }
            return(new KeyValuePair <List <ElementId>, List <double> >(allRoomName, final_rel));
        }
Esempio n. 2
0
        public IList <XYZ> CalPointOfRooms(Document doc, IEnumerable <Room> rooms, List <XYZ> Exit2Door, ICollection <ElementId> eleIg)
        {
            var rel = new List <XYZ>();

            using (Transaction trans = new Transaction(doc))
            {
                if (trans.Start("Path") == TransactionStatus.Started)
                {
                    int count    = 0;
                    var settings = RouteAnalysisSettings.GetRouteAnalysisSettings(doc);
                    settings.SetIgnoredCategoryIds(eleIg);
                    foreach (Room room in rooms)
                    {
                        var            exit      = Exit2Door[count];
                        BoundingBoxXYZ box       = room.get_BoundingBox(null);
                        Transform      trf       = box.Transform;
                        XYZ            min_xyz   = box.Min;
                        XYZ            max_xyz   = box.Max;
                        XYZ            minInCoor = trf.OfPoint(min_xyz);
                        XYZ            maxInCoor = trf.OfPoint(max_xyz);
                        List <XYZ>     temp      = new List <XYZ>();
                        temp.Add(new XYZ(minInCoor.X, maxInCoor.Y, minInCoor.Z));
                        temp.Add(new XYZ(minInCoor.Y, maxInCoor.X, minInCoor.Z));
                        temp.Add(new XYZ(maxInCoor.X, minInCoor.Y, minInCoor.Z));
                        temp.Add(new XYZ(maxInCoor.Y, minInCoor.X, minInCoor.Z));

                        XYZ    final     = null;
                        double final_dis = MAX_NUM;
                        foreach (XYZ r in temp)
                        {
                            if (!room.IsPointInRoom(r))
                            {
                                continue;
                            }
                            PathOfTravel path = PathOfTravel.Create(doc.ActiveView, r, exit);
                            if (path == null)
                            {
                                continue;
                            }
                            double dis = calDis(path.GetCurves());
                            if (dis < final_dis)
                            {
                                final_dis = dis;
                                final     = r;
                            }
                        }
                        if (final == null)
                        {
                            LocationPoint loc = room.Location as LocationPoint;
                            XYZ           xyz = loc.Point;
                            rel.Add(xyz);
                        }
                        else
                        {
                            rel.Add(final);
                        }
                    }
                    trans.RollBack();
                }
            }

            //foreach (Room r in rooms)
            //{
            //    LocationPoint loc = r.Location as LocationPoint;
            //    XYZ xyz = loc.Point;
            //    rel.Add(xyz);
            //}
            return(rel);
        }