private string GetRoomParameterValue(Room room, string parameterName)
        {
            string parameterValue = parameterName;

            try
            {
                Parameter param = room.LookupParameter(parameterName);

                if (null != param)
                {
                    if (param.StorageType == StorageType.String)
                    {
                        parameterValue = param.AsString();
                    }
                    else
                    {
                        parameterValue = param.AsValueString();
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + ": failed to get room parameter values for the creation of view names.\n" + ex.Message);
                //MessageBox.Show("Failed to get the room parameter value for the view name.\n"+ex.Message, "Elevation Creator: GetRoomParameterValue", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(parameterValue);
        }
        private string GetViewName(string prefix, string intermediateText, int elevationIndex, string indexText, string suffix)
        {
            string viewName = "";

            try
            {
                if (toolSettings.PrefixSelected)
                {
                    viewName = prefix;
                }
                if (toolSettings.IntermediateSelected)
                {
                    viewName += intermediateText;
                }

                if (toolSettings.ElevationSelected)
                {
                    if (!string.IsNullOrEmpty(viewName))
                    {
                        viewName += "-Elevation" + elevationIndex;
                    }
                    else
                    {
                        viewName += "Elevation" + elevationIndex;
                    }
                }

                if (toolSettings.ABCDSelected)
                {
                    if (!string.IsNullOrEmpty(viewName))
                    {
                        viewName += "-" + indexText;
                    }
                    else
                    {
                        viewName += indexText;
                    }
                }

                if (toolSettings.SuffixSelected)
                {
                    if (!string.IsNullOrEmpty(viewName))
                    {
                        viewName += " (" + suffix + ")";
                    }
                    else
                    {
                        viewName += "(" + suffix + ")";
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + " : failed to get view names.\n" + ex.Message);
                //MessageBox.Show("Failed to create a view name.\n"+ex.Message, "Elevation Creator : GetViewName", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(viewName);
        }
        public bool CheckExisting()
        {
            bool result = false;

            try
            {
                //delete existing elevation views created by List
                if (roomProperties.KeyMarkId != -1)
                {
                    int markId = roomProperties.KeyMarkId;
                    if (roomProperties.ElevationViews.ContainsKey(markId))
                    {
                        using (Transaction trans = new Transaction(m_doc, "Delete Elevation Mark"))
                        {
                            trans.Start();
                            FailureHandlingOptions failureOptions = trans.GetFailureHandlingOptions();
                            failureOptions.SetFailuresPreprocessor(new DeleteViewsPreprocessor());
                            try
                            {
                                m_doc.Delete(new ElementId(markId));
                                trans.Commit(failureOptions);
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                string message = ex.Message;
                            }
                        }
                        roomProperties.KeyMarkId = -1;
                        roomProperties.ElevationViews.Remove(markId);
                    }
                }
                result = true;
            }
            catch (Exception ex)
            {
                LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + " : failed to check existing views.");
                LogMessageBuilder.AddLogMessage(ex.Message);
                //MessageBox.Show("Failed to check existing views.\n"+ex.Message , "Elevation Creator: CheckExisting", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(result);
        }
        private int GetElevationMarkIndex()
        {
            int elevationIndex = 1;

            try
            {
                if (roomProperties.ElevationViews.Count > 0)
                {
                    foreach (int markId in roomProperties.ElevationViews.Keys)
                    {
                        if (roomProperties.ElevationViews[markId].Count > 0)
                        {
                            ElevationViewProperties evp = roomProperties.ElevationViews[markId].Values.First();
                            string[] splitNames         = evp.ViewName.Split('-');
                            foreach (string name in splitNames)
                            {
                                if (name.Contains("Elevation"))
                                {
                                    string indexStr = name.Replace("Elevation", "");
                                    int    index    = 1;
                                    if (int.TryParse(indexStr, out index))
                                    {
                                        if (elevationIndex == index)
                                        {
                                            elevationIndex++;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + " : failed to get elevation index.\n" + ex.Message);
                string message = ex.Message;
            }
            return(elevationIndex);
        }
        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);
            }
        }
Beispiel #7
0
 public LogMessageBox()
 {
     InitializeComponent();
     textBoxLog.Text = LogMessageBuilder.GetLogMessages();
 }
        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);
        }
        public bool CreateElevationByList()
        {
            bool result = false;

            try
            {
                ElevationMarker marker         = null;
                XYZ             markerLocation = null;


                ApplyTemplateSettings();

                using (Transaction trans = new Transaction(m_doc, "Create Elevation Marker"))
                {
                    trans.Start();
                    try
                    {
                        BoundingBoxXYZ bbBox = m_room.get_BoundingBox(null);
                        markerLocation = new XYZ((bbBox.Max.X + bbBox.Min.X) / 2, (bbBox.Max.Y + bbBox.Min.Y) / 2, bbBox.Min.Z);

                        //LocationPoint locationPoint = m_room.Location as LocationPoint;
                        //markerLocation = locationPoint.Point;
                        if (m_room.Document.IsLinked)
                        {
                            var documents = from doc in linkedDocuments.Values where doc.DocumentTitle == m_room.Document.Title select doc;
                            if (documents.Any())
                            {
                                LinkedInstanceProperties lip = documents.First();
                                roomLink       = lip;
                                markerLocation = lip.TransformValue.OfPoint(markerLocation);
                            }
                        }

                        marker = ElevationMarker.CreateElevationMarker(m_doc, m_viewFamilyTypeId, markerLocation, toolSettings.CustomScale);
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.RollBack();
                        LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + " : failed to create an elevation marker.");
                        LogMessageBuilder.AddLogMessage(ex.Message);
                        //MessageBox.Show("Failed to create an elevation marker.\n" + ex.Message, "Elevation Creator: Create Elevation Marker", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }

                if (null != marker)
                {
                    using (Transaction trans = new Transaction(m_doc, "Create Elevation Views"))
                    {
                        trans.Start();
                        try
                        {
                            string prefix           = toolSettings.PrefixText;
                            string intermediateText = GetRoomParameterValue(m_room, toolSettings.IntermediateText);
                            int    elevationIndex   = GetElevationMarkIndex();
                            string suffix           = GetRoomParameterValue(m_room, toolSettings.SuffixText);

                            int viewCount = marker.MaximumViewCount < 4 ? marker.MaximumViewCount : 4;

                            Dictionary <int, Dictionary <int, ElevationViewProperties> > elevationDictionary = new Dictionary <int, Dictionary <int, ElevationViewProperties> >();
                            Dictionary <int, ElevationViewProperties> elevationViews = new Dictionary <int, ElevationViewProperties>();

                            for (int i = 0; i < viewCount; i++)
                            {
                                string indexText = "";
                                if (i == 0 && toolSettings.DIsSelected)
                                {
                                    indexText = "D";
                                }
                                else if (i == 1 && toolSettings.AIsSelected)
                                {
                                    indexText = "A";
                                }
                                else if (i == 2 && toolSettings.BIsSelected)
                                {
                                    indexText = "B";
                                }
                                else if (i == 3 && toolSettings.CIsSelected)
                                {
                                    indexText = "C";
                                }
                                else
                                {
                                    continue;
                                }

                                ViewSection viewElevation = marker.CreateElevation(m_doc, m_viewPlan.Id, i);
                                viewElevation.Name = GetViewName(prefix, intermediateText, elevationIndex, indexText, suffix);
                                if (toolSettings.ViewTemplateId != -1)
                                {
                                    viewElevation.ViewTemplateId = new ElementId(toolSettings.ViewTemplateId);
                                }

                                Parameter param = viewElevation.LookupParameter("Title on Sheet");

                                if (null != param)
                                {
                                    param.Set(m_room.Name);
                                }

                                ElevationViewProperties viewProperties = new ElevationViewProperties(viewElevation);
                                if (!elevationViews.ContainsKey(viewProperties.ViewId))
                                {
                                    elevationViews.Add(viewProperties.ViewId, viewProperties);
                                }
                            }

                            if (elevationViews.Count > 0 && !roomProperties.ElevationViews.ContainsKey(marker.Id.IntegerValue))
                            {
                                roomProperties.KeyMarkId = marker.Id.IntegerValue;
                                roomProperties.ElevationViews.Add(marker.Id.IntegerValue, elevationViews);
                            }

                            trans.Commit();
                        }
                        catch (Exception ex)
                        {
                            trans.RollBack();
                            LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + " : failed to create elevation views.");
                            LogMessageBuilder.AddLogMessage(ex.Message);
                            //MessageBox.Show("Failed to create elevation views.\n" + ex.Message, "Elevation Creator: Create Elevation Views", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                    }
                }

                if (null != marker && null != markerLocation)
                {
                    if (ModifyCropBox(roomProperties, marker.Id.IntegerValue))
                    {
                        if (ElevationCreatorDataStorageUtil.StoreRoomElevationProperties(m_doc, roomProperties))
                        {
                            //update extensible storage
                            result = true;
                        }
                    }
                }
                result = true;
            }
            catch (Exception ex)
            {
                LogMessageBuilder.AddLogMessage(roomProperties.RoomNumber + " - " + roomProperties.RoomName + " : failed to create elevations by room lists.\n" + ex.Message);
                //MessageBox.Show("Failed to create elevation by room lists.\n"+ex.Message, "Elevation Creator: CreateElevationByList", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(result);
        }