private Dictionary <int, RoomElevationProperties> GetRoomsProperties(Dictionary <int, RoomElevationProperties> roomsStored)
        {
            Dictionary <int, RoomElevationProperties> dictionary = new Dictionary <int, RoomElevationProperties>();

            try
            {
                FilteredElementCollector collector = new FilteredElementCollector(m_doc, viewPlan.Id);
                List <Room> rooms = collector.OfCategory(BuiltInCategory.OST_Rooms).ToElements().Cast <Room>().ToList();
                foreach (Room room in rooms)
                {
                    int roomId = room.Id.IntegerValue;
                    if (room.Area > 0)
                    {
                        //skip unplaced rooms
                        RoomElevationProperties rep = new RoomElevationProperties(room);
                        if (roomsStored.ContainsKey(roomId))
                        {
                            rep = roomsStored[roomId];
                        }

                        if (!dictionary.ContainsKey(roomId))
                        {
                            dictionary.Add(roomId, rep);
                        }
                    }
                }

                foreach (LinkedInstanceProperties linkedInstance in linkedDocuments.Values)
                {
                    collector = new FilteredElementCollector(linkedInstance.LinkedDocument);
                    List <Room> linkedRooms = collector.OfCategory(BuiltInCategory.OST_Rooms).ToElements().Cast <Room>().ToList();
                    foreach (Room room in linkedRooms)
                    {
                        int roomId = room.Id.IntegerValue;
                        if (room.Area > 0)
                        {
                            RoomElevationProperties rep = new RoomElevationProperties(room, linkedInstance.InstanceId);
                            if (roomsStored.ContainsKey(roomId))
                            {
                                rep = roomsStored[roomId];
                            }

                            if (!dictionary.ContainsKey(roomId))
                            {
                                dictionary.Add(roomId, rep);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to get Room and Elevation properties.\n" + ex.Message, "Elevation Creator: GetRoomsProperties", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(dictionary);
        }
 public RoomElevationProperties(RoomElevationProperties rep)
 {
     m_room         = rep.RoomObj;
     roomName       = rep.RoomName;
     roomNumber     = rep.RoomNumber;
     roomId         = rep.RoomId;
     isLinked       = rep.IsLinked;
     documentTitle  = rep.DocumentTitle;
     rvtLinkId      = rep.RvtLinkId;
     keyMarkId      = rep.KeyMarkId;
     elevationViews = rep.ElevationViews;
 }
 public ElevationCreator(UIApplication uiapp, RoomElevationProperties rep, ElevationCreatorSettings settings, Dictionary <int, LinkedInstanceProperties> linkedInstances)
 {
     //create by room list
     m_app              = uiapp;
     m_doc              = uiapp.ActiveUIDocument.Document;
     roomProperties     = rep;
     m_room             = roomProperties.RoomObj;
     toolSettings       = settings;
     linkedDocuments    = linkedInstances;
     m_viewPlan         = toolSettings.ActiveViewPlan;
     m_viewFamilyTypeId = new ElementId(toolSettings.ViewFamilyId);
 }
Esempio n. 4
0
        public TreeviewModel(RoomElevationProperties rep)
        {
            RoomProperties = rep;
            ViewProperties = null;
            string roomNumber = rep.RoomNumber;
            string roomName   = rep.RoomName;

            Name    = roomNumber + " - " + roomName;
            Tag     = rep;
            ToolTip = "Element ID:" + rep.RoomId.ToString();

            if (rep.ElevationViews.Count > 0)
            {
                TextColor = new SolidColorBrush(Colors.Gray);
            }
            else
            {
                TextColor = new SolidColorBrush(Colors.Black);
            }
        }
        public static Dictionary <int, RoomElevationProperties> GetRoomElevationProperties(Document doc, Dictionary <int, LinkedInstanceProperties> linkedDocs)
        {
            Dictionary <int, RoomElevationProperties> pDictionary = new Dictionary <int, RoomElevationProperties>();

            try
            {
                if (null == m_schema)
                {
                    m_schema = CreateSchema();
                }
                if (null != m_schema)
                {
                    IList <DataStorage> savedStorage = GetRoomElevationStorage(doc, m_schema);
                    if (savedStorage.Count > 0)
                    {
                        foreach (DataStorage storage in savedStorage)
                        {
                            Entity    entity                  = storage.GetEntity(m_schema);
                            string    roomNumber              = entity.Get <string>(m_schema.GetField(s_RoomNumber));
                            string    roomName                = entity.Get <string>(m_schema.GetField(s_RoomName));
                            ElementId roomId                  = entity.Get <ElementId>(m_schema.GetField(s_RoomId));
                            bool      isLinked                = entity.Get <bool>(m_schema.GetField(s_IsLinked));
                            ElementId rvtInstanceId           = entity.Get <ElementId>(m_schema.GetField(s_RvtInstanceId));
                            ElementId keyMarkId               = entity.Get <ElementId>(m_schema.GetField(s_KeyMarkId));
                            IDictionary <int, int> elevations = entity.Get <IDictionary <int, int> >(s_Elevations);

                            //ElementId elevationMarkId = entity.Get<ElementId>(m_schema.GetField(s_elevationMarkId));
                            //IList<ElementId> elevationIds = entity.Get<IList<ElementId>>(m_schema.GetField(s_ElevationIds));

                            if (roomId != ElementId.InvalidElementId)
                            {
                                Room room = null;
                                RoomElevationProperties rep = null;
                                if (isLinked && linkedDocs.ContainsKey(rvtInstanceId.IntegerValue))
                                {
                                    LinkedInstanceProperties lip = linkedDocs[rvtInstanceId.IntegerValue];
                                    room = lip.LinkedDocument.GetElement(roomId) as Room;
                                    if (null != room)
                                    {
                                        rep = new RoomElevationProperties(room, rvtInstanceId.IntegerValue);
                                    }
                                }
                                else
                                {
                                    room = doc.GetElement(roomId) as Room;
                                    if (null != room)
                                    {
                                        rep = new RoomElevationProperties(room);
                                    }
                                }

                                if (null != rep)
                                {
                                    rep.KeyMarkId = keyMarkId.IntegerValue;
                                    Dictionary <int, Dictionary <int, ElevationViewProperties> > elevationDictionary = new Dictionary <int, Dictionary <int, ElevationViewProperties> >();
                                    foreach (int viewId in elevations.Keys)
                                    {
                                        int elevationViewId = viewId;
                                        int markId          = elevations[elevationViewId];

                                        ViewSection viewSection = doc.GetElement(new ElementId(elevationViewId)) as ViewSection;
                                        if (null != viewSection)
                                        {
                                            ElevationViewProperties evp = new ElevationViewProperties(viewSection);
                                            if (!rep.ElevationViews.ContainsKey(markId))
                                            {
                                                Dictionary <int, ElevationViewProperties> elevationViews = new Dictionary <int, ElevationViewProperties>();
                                                elevationViews.Add(evp.ViewId, evp);
                                                rep.ElevationViews.Add(markId, elevationViews);
                                            }
                                            else
                                            {
                                                if (!rep.ElevationViews[markId].ContainsKey(elevationViewId))
                                                {
                                                    rep.ElevationViews[markId].Add(evp.ViewId, evp);
                                                }
                                            }
                                        }
                                    }

                                    if (!pDictionary.ContainsKey(rep.RoomId))
                                    {
                                        pDictionary.Add(rep.RoomId, rep);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to get room elevation properties from data storage.\n" + ex.Message, "Elevation Creator : GetRoomElevationProperties", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(pDictionary);
        }
        public static bool StoreRoomElevationProperties(Document doc, RoomElevationProperties rep)
        {
            bool saved = false;

            try
            {
                if (null == m_schema)
                {
                    m_schema = CreateSchema();
                }

                if (null != m_schema)
                {
                    IList <DataStorage> savedStorage = GetRoomElevationStorage(doc, m_schema);
                    if (savedStorage.Count > 0)
                    {
                        using (Transaction trans = new Transaction(doc))
                        {
                            trans.Start("Delete Data Storage");
                            try
                            {
                                DataStorage storageToDelete = null;
                                foreach (DataStorage ds in savedStorage)
                                {
                                    Entity    entity = ds.GetEntity(m_schema);
                                    ElementId roomId = entity.Get <ElementId>(m_schema.GetField(s_RoomId));
                                    if (rep.RoomId == roomId.IntegerValue)
                                    {
                                        storageToDelete = ds;
                                        break;
                                    }
                                }
                                if (null != storageToDelete)
                                {
                                    doc.Delete(storageToDelete.Id);
                                }

                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                LogMessageBuilder.AddLogMessage(rep.RoomName + " : failed to delete data storage.");
                                LogMessageBuilder.AddLogMessage(ex.Message);
                                //MessageBox.Show("Failed to delete data storage.\n" + ex.Message, "Elevation Creator : Update Data Storage", MessageBoxButton.OK, MessageBoxImage.Warning);
                            }
                        }
                    }

                    using (Transaction trans = new Transaction(doc))
                    {
                        trans.Start("Add New Storage");
                        try
                        {
                            DataStorage storage = DataStorage.Create(doc);
                            Entity      entity  = new Entity(schemaId);
                            entity.Set <string>(s_RoomNumber, rep.RoomNumber);
                            entity.Set <string>(s_RoomName, rep.RoomName);
                            entity.Set <ElementId>(s_RoomId, new ElementId(rep.RoomId));
                            entity.Set <bool>(s_IsLinked, rep.IsLinked);
                            entity.Set <ElementId>(s_KeyMarkId, new ElementId(rep.KeyMarkId));
                            entity.Set <ElementId>(s_RvtInstanceId, new ElementId(rep.RvtLinkId));

                            IDictionary <int, int> elevationViews = new Dictionary <int, int>();
                            foreach (int markId in rep.ElevationViews.Keys)
                            {
                                foreach (int viewId in rep.ElevationViews[markId].Keys)
                                {
                                    if (!elevationViews.ContainsKey(viewId))
                                    {
                                        elevationViews.Add(viewId, markId);
                                    }
                                }
                            }
                            entity.Set(s_Elevations, elevationViews);
                            storage.SetEntity(entity);

                            trans.Commit();
                            saved = true;
                        }
                        catch (Exception ex)
                        {
                            trans.RollBack();
                            LogMessageBuilder.AddLogMessage(rep.RoomName + " : failed to add data storage.");
                            LogMessageBuilder.AddLogMessage(ex.Message);
                            //MessageBox.Show("Failed to add data storage.\n" + ex.Message, "Elevation Creator : Update Data Stroage", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessageBuilder.AddLogMessage(rep.RoomName + " : failed to save properties of room elevation views in data storage.");
                LogMessageBuilder.AddLogMessage(ex.Message);
                //MessageBox.Show("Failed to save room elevation properties in data storage.\n" + ex.Message, "Elevation Creator : StoreRoomeElevationProperties", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(saved);
        }
        private void buttonCreateByList_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (SetToolSettings())
                {
                    List <RoomElevationProperties> selectedRooms = new List <RoomElevationProperties>();

                    List <TreeviewModel> treeviewModels = treeViewRoom.ItemsSource as List <TreeviewModel>;
                    foreach (TreeviewModel roomNode in treeviewModels)
                    {
                        if (roomNode.IsChecked == true)
                        {
                            if (null != roomNode.RoomProperties)
                            {
                                RoomElevationProperties rep = roomNode.RoomProperties;
                                selectedRooms.Add(rep);
                            }
                        }
                    }

                    if (selectedRooms.Count > 0)
                    {
                        progressBar.Visibility = System.Windows.Visibility.Visible;
                        statusLable.Visibility = System.Windows.Visibility.Visible;
                        statusLable.Text       = "Creating Elevation Views . . .";

                        progressBar.Minimum = 0;
                        progressBar.Maximum = selectedRooms.Count;
                        progressBar.Value   = 0;

                        double value = 0;
                        UpdateProgressBarDelegate updatePdDelegate = new UpdateProgressBarDelegate(progressBar.SetValue);

                        foreach (RoomElevationProperties rep in selectedRooms)
                        {
                            ElevationCreator creator = new ElevationCreator(m_app, rep, toolSettings, linkedDocuments);
                            if (creator.CheckExisting())
                            {
                                if (creator.CreateElevationByList())
                                {
                                    RoomElevationProperties roomProperties = new RoomElevationProperties(creator.RoomProperties);
                                    if (roomDictionary.ContainsKey(roomProperties.RoomId))
                                    {
                                        roomDictionary.Remove(roomProperties.RoomId);
                                    }
                                    roomDictionary.Add(roomProperties.RoomId, roomProperties);
                                }
                            }

                            value += 1;
                            Dispatcher.Invoke(updatePdDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
                        }

                        statusLable.Text       = "Ready";
                        progressBar.Visibility = System.Windows.Visibility.Hidden;

                        treeViewRoom.ItemsSource = null;
                        treeViewRoom.ItemsSource = TreeviewModel.SetTreeView(roomDictionary, toolSettings.IsLinkedRoom);

                        if (LogMessageBuilder.GetLogMessages().Length > 0)
                        {
                            LogMessageBox logMessageBox = new LogMessageBox();
                            logMessageBox.Show();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to start creating elevation views by rooms lists.\n" + ex.Message, "Elevation Creator: CreateByRoomList", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
        public bool ModifyCropBox(RoomElevationProperties rep, int markerId)
        {
            bool result = false;

            try
            {
                List <XYZ>      vertices    = new List <XYZ>();
                GeometryElement geomElement = m_room.ClosedShell;
                if (null != geomElement)
                {
                    if (null != roomLink)
                    {
                        geomElement = geomElement.GetTransformed(roomLink.TransformValue);
                    }
                    foreach (GeometryObject geomObject in geomElement)
                    {
                        if (geomObject is Solid)
                        {
                            Solid solid = geomObject as Solid;
                            foreach (Edge edge in solid.Edges)
                            {
                                Curve curve = edge.AsCurve();
                                vertices.Add(curve.GetEndPoint(0));
                                vertices.Add(curve.GetEndPoint(1));
                            }
                        }
                    }
                }

                if (vertices.Count > 0)
                {
                    if (rep.ElevationViews.ContainsKey(markerId))
                    {
                        Dictionary <int, ElevationViewProperties> elevationViews = rep.ElevationViews[markerId];
                        foreach (ElevationViewProperties evp in elevationViews.Values)
                        {
                            ViewSection    elevationView  = evp.ViewObj;
                            List <XYZ>     verticesInView = new List <XYZ>();
                            BoundingBoxXYZ bb             = elevationView.CropBox;
                            if (null != bb)
                            {
                                Transform transform        = bb.Transform;
                                Transform transformInverse = transform.Inverse;

                                foreach (XYZ vertex in vertices)
                                {
                                    verticesInView.Add(transformInverse.OfPoint(vertex));
                                }

                                double xMin = 0, yMin = 0, xMax = 0, yMax = 0, zMin = 0, zMax = 0;
                                bool   first = true;
                                foreach (XYZ p in verticesInView)
                                {
                                    if (first)
                                    {
                                        xMin  = p.X;
                                        yMin  = p.Y;
                                        zMin  = p.Z;
                                        xMax  = p.X;
                                        yMax  = p.Y;
                                        zMax  = p.Z;
                                        first = false;
                                    }
                                    else
                                    {
                                        if (xMin > p.X)
                                        {
                                            xMin = p.X;
                                        }
                                        if (yMin > p.Y)
                                        {
                                            yMin = p.Y;
                                        }
                                        if (zMin > p.Z)
                                        {
                                            zMin = p.Z;
                                        }
                                        if (xMax < p.X)
                                        {
                                            xMax = p.X;
                                        }
                                        if (yMax < p.Y)
                                        {
                                            yMax = p.Y;
                                        }
                                        if (zMax < p.Z)
                                        {
                                            zMax = p.Z;
                                        }
                                    }
                                }

                                using (Transaction trans = new Transaction(m_doc, "Set Crop Box"))
                                {
                                    trans.Start();
                                    try
                                    {
                                        elevationView.CropBoxActive = false;
                                        int spacing = toolSettings.SpaceAround;

                                        bb.Max = new XYZ(xMax + spacing, yMax + spacing, -zMin);
                                        bb.Min = new XYZ(xMin - spacing, yMin - spacing, 0);
                                        elevationView.CropBox        = bb;
                                        elevationView.CropBoxActive  = true;
                                        elevationView.CropBoxVisible = true;

                                        trans.Commit();
                                        result = true;
                                    }
                                    catch (Exception ex)
                                    {
                                        trans.RollBack();
                                        string message = ex.Message;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + " : failed to modify crop box.");
                LogMessageBuilder.AddLogMessage(ex.Message);
                //MessageBox.Show("Failed to modify the crop region in this view.\n"+ex.Message, "Modify Crop Box", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(result);
        }
        private void SelectRoomAndWall(bool hostRoom, bool hostWall)
        {
            try
            {
                Reference selectedRoom = null;
                Room      roomElement  = null;
                XYZ       pickPoint    = null;
                if (hostRoom)
                {
                    selectedRoom = uidoc.Selection.PickObject(ObjectType.Element, new RoomElementFilter(), "Select a room from the host model to create an elevation view.");
                    if (null != selectedRoom)
                    {
                        roomElement = m_doc.GetElement(selectedRoom.ElementId) as Room;
                    }
                }
                else
                {
                    selectedRoom = uidoc.Selection.PickObject(ObjectType.LinkedElement, "Select a room from linked models to create an elevation view.");
                    if (null != selectedRoom)
                    {
                        foreach (LinkedInstanceProperties lip in linkedDocuments.Values)
                        {
                            Element element = lip.LinkedDocument.GetElement(selectedRoom.LinkedElementId);
                            if (null != element)
                            {
                                roomElement = element as Room; break;
                            }
                        }
                    }
                }

                if (null != selectedRoom)
                {
                    pickPoint = uidoc.Selection.PickPoint("Pick a point to locate the elevation mark.");
                    if (null != pickPoint)
                    {
                        Reference selectedWall = null;
                        Wall      wallElement  = null;
                        if (hostWall)
                        {
                            selectedWall = uidoc.Selection.PickObject(ObjectType.Element, new WallSelectionFilter(), "Select a wall from the host model to rotate an elevation view perpendicular to the wall.");
                            if (null != selectedWall)
                            {
                                wallElement = m_doc.GetElement(selectedWall.ElementId) as Wall;
                            }
                        }
                        else
                        {
                            selectedWall = uidoc.Selection.PickObject(ObjectType.LinkedElement, "Select a wall from linked models to rotate an elevation view perpendicular to the wall.");
                            if (null != selectedWall)
                            {
                                foreach (LinkedInstanceProperties lip in linkedDocuments.Values)
                                {
                                    Element element = lip.LinkedDocument.GetElement(selectedWall.LinkedElementId);
                                    if (null != element)
                                    {
                                        wallElement = element as Wall; break;
                                    }
                                }
                            }
                        }

                        if (null != roomElement && null != wallElement && null != pickPoint)
                        {
                            int roomId = roomElement.Id.IntegerValue;
                            RoomElevationProperties rep = null;
                            if (roomDictionary.ContainsKey(roomId))
                            {
                                rep = roomDictionary[roomId];
                            }
                            else
                            {
                                rep = new RoomElevationProperties(roomElement);
                            }

                            ElevationCreator elevationCreator = new ElevationCreator(m_app, rep, wallElement, toolSettings, linkedDocuments);
                            elevationCreator.PickPoint = pickPoint;

                            if (elevationCreator.CreateElevationByWall())
                            {
                                MessageBoxResult dr = MessageBox.Show("Would you like to continue to select a room and a wall for the elevation view?", "Select a Room and a Wall", MessageBoxButton.YesNo, MessageBoxImage.Question);
                                if (dr == MessageBoxResult.Yes)
                                {
                                    SelectRoomAndWall(hostRoom, hostWall);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Errors occured while selecting rooms and walls.\n" + ex.Message, "Select Rooms and Walls", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }