public Dictionary <Room, List <string> > GetRoomInfo(Document document, UIDocument uidoc)
        {
            using (Transaction t = new Transaction(document, "Turn on volume calculation"))
            {
                t.Start();
                AreaVolumeSettings settings = AreaVolumeSettings.GetAreaVolumeSettings(document);
                settings.ComputeVolumes = true;
                t.Commit();
            }

            Dictionary <Room, List <string> > dimensions = new Dictionary <Room, List <string> >();
            List <string>            info      = new List <string>();
            RoomFilter               filter    = new RoomFilter();
            FilteredElementCollector collector = new FilteredElementCollector(document);

            collector.WherePasses(filter);

            FilteredElementIdIterator roomIdItr = collector.GetElementIdIterator();

            roomIdItr.Reset();
            while (roomIdItr.MoveNext())
            {
                ElementId roomId = roomIdItr.Current;
                Room      room   = document.GetElement(roomId) as Room;

                //Make sure room is valid
                if (room != null)
                {
                    //Wall wall = document.GetReference(room) as Wall;

                    //Fill out dictionary with room attributes
                    info.Add(room.Name);
                    info.Add(room.Volume.ToString());
                    info.Add(room.Area.ToString());
                    info.Add(room.Perimeter.ToString());
                    info.Add(room.UnboundedHeight.ToString());

                    dimensions.Add(room, info);
                }
            }

            return(dimensions);
        }
        public Dictionary <ElementId, List <ElementId> > getElementsIdsByStyleType()
        {
            Dictionary <ElementId, List <ElementId> > elemToStyleIds = new Dictionary <ElementId, List <ElementId> >();

            Autodesk.Revit.DB.Options geoOptions = uiapp.Application.Create.NewGeometryOptions();
            geoOptions.ComputeReferences = true;
            FilteredElementCollector curElementsDocFilter = new FilteredElementCollector(doc);

            if (curElementsDocFilter != null)
            {
                FilteredElementIdIterator curElementsIdsIterator = curElementsDocFilter.WhereElementIsNotElementType().GetElementIdIterator();
                while (curElementsIdsIterator.MoveNext())
                {
                    ElementId curElementId = curElementsIdsIterator.Current;
                    if (curElementId != null)
                    {
                        Element curElement = doc.GetElement(curElementId);
                        if (curElement != null)
                        {
                            GeometryElement curGeomElement     = curElement.get_Geometry(geoOptions);
                            ElementId       curGraphicsStyleId = curGeomElement.GraphicsStyleId;
                            if (elemToStyleIds.ContainsKey(curGraphicsStyleId))
                            {
                                elemToStyleIds[curGraphicsStyleId].Add(curElementId);
                            }
                            else
                            {
                                List <ElementId> elemsList = new List <ElementId>();
                                elemsList.Add(curElementId);
                                elemToStyleIds.Add(curGraphicsStyleId, elemsList);
                            }
                        }
                    }
                }
            }

            return(elemToStyleIds);
        }
        private void export(Document doc)
        {
            elementsByCategory.Clear();

            FilteredElementCollector docFilter = new FilteredElementCollector(doc, doc.ActiveView.Id).WhereElementIsNotElementType();

            if (docFilter != null)
            {
                try
                {
                    Stopwatch sw = Stopwatch.StartNew();

                    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                    if (excelApp != null)
                    {
                        FilteredElementIdIterator docFilterIterator = docFilter.GetElementIdIterator();
                        while (docFilterIterator.MoveNext())
                        {
                            Element elem = doc.GetElement(docFilterIterator.Current);
                            if (elem != null)
                            {
                                Category elemCategory = elem.Category;
                                if (elemCategory != null)
                                {
                                    string elemCatName = elemCategory.Name;
                                    if (elementsByCategory.ContainsKey(elemCatName))
                                    {
                                        elementsByCategory[elemCatName].Add(elem);
                                    }
                                    else
                                    {
                                        List <Element> norElementsList = new List <Element>();
                                        norElementsList.Add(elem);
                                        elementsByCategory.Add(elemCatName, norElementsList);
                                    }
                                }
                            }
                        }

                        excelApp.Visible = true;

                        List <string> keys = new List <string>(elementsByCategory.Keys);

                        keys.Sort();
                        keys.Reverse();

                        bool first = true;

                        int nElements   = 0;
                        int nCategories = keys.Count;

                        Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Add(Missing.Value);
                        if (excelWorkbook != null)
                        {
                            foreach (string categoryName in keys)
                            {
                                List <Element> elementSet = elementsByCategory[categoryName];

                                Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = excelWorkbook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Microsoft.Office.Interop.Excel.Worksheet;
                                if (excelWorksheet != null)
                                {
                                    string name = "";

                                    name = (31 < categoryName.Length) ? categoryName.Substring(0, 31) : categoryName;
                                    name = name.Replace(':', '_').Replace('/', '_');

                                    excelWorksheet.Name = name;

                                    List <string> paramNames = new List <string>();
                                    foreach (Element elem in elementSet)
                                    {
                                        ParameterSet parameters = elem.Parameters;

                                        foreach (Parameter parameter in parameters)
                                        {
                                            name = parameter.Definition.Name;

                                            if (!paramNames.Contains(name))
                                            {
                                                paramNames.Add(name);
                                            }
                                        }
                                    }
                                    paramNames.Sort();

                                    excelWorksheet.Cells[1, 1] = "ID";
                                    excelWorksheet.Cells[1, 2] = "IsType";

                                    int column = 3;

                                    foreach (string paramName in paramNames)
                                    {
                                        excelWorksheet.Cells[1, column] = paramName;
                                        ++column;
                                    }
                                    var range = excelWorksheet.get_Range("A1", "Z1");

                                    range.Font.Bold = true;
                                    range.EntireColumn.AutoFit();

                                    int row = 2;

                                    foreach (Element elem in elementSet)
                                    {
                                        excelWorksheet.Cells[row, 1] = elem.Id.IntegerValue;

                                        excelWorksheet.Cells[row, 2] = (elem is ElementType) ? 1 : 0;
                                        column = 3;

                                        string paramValue;

                                        foreach (string paramName in paramNames)
                                        {
                                            paramValue = getParameterValue(elem, paramName);
                                            if (paramValue != null)
                                            {
                                                excelWorksheet.Cells[row, column++] = paramValue;
                                            }
                                            else
                                            {
                                                excelWorksheet.Cells[row, column++] = "*NA*";
                                            }
                                        }

                                        ++nElements;
                                        ++row;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        TaskDialog.Show("Error", "Please start Excel before exporting");
                    }

                    sw.Stop();
                }
                catch (Exception ex)
                {
                }
            }
        }
        private void updateTreeView(Document doc, string groupName)
        {
            if (this.almEnabled)
            {
                if (doc != null)
                {
                    try
                    {
                        string[] keywords;
                        if (groupName == "mech")
                        {
                            keywords = this.mechanicalKeywords;

                            this.buttonMechanical.IsEnabled = false;
                            this.buttonPlumbing.IsEnabled   = true;
                            this.buttonElectrical.IsEnabled = true;
                        }
                        else if (groupName == "plum")
                        {
                            keywords = this.plumbingKeywords;

                            this.buttonMechanical.IsEnabled = true;
                            this.buttonPlumbing.IsEnabled   = false;
                            this.buttonElectrical.IsEnabled = true;
                        }
                        else if (groupName == "elec")
                        {
                            keywords = this.electricalKeywords;

                            this.buttonMechanical.IsEnabled = true;
                            this.buttonPlumbing.IsEnabled   = true;
                            this.buttonElectrical.IsEnabled = false;
                        }
                        else
                        {
                            return;
                        }

                        this.categoryNodesDict.Clear();
                        this.familyNodesDict.Clear();
                        this.treeViewTool.Items.Clear();

                        FilteredElementCollector docFilter = new FilteredElementCollector(doc).OfClass(typeof(ElementType));
                        if (docFilter != null)
                        {
                            StringBuilder strBld = new StringBuilder();

                            FilteredElementIdIterator docFilterIterator = docFilter.GetElementIdIterator();
                            while (docFilterIterator.MoveNext())
                            {
                                Element curElement = doc.GetElement(docFilterIterator.Current);
                                if (curElement != null)
                                {
                                    if (curElement is ElementType)
                                    {
                                        ElementType curElementType = curElement as ElementType;
                                        if (curElementType != null)
                                        {
                                            if (this.uidoc.CanPlaceElementType(curElementType))
                                            {
                                                string curFamilyName = curElementType.FamilyName;

                                                Category curCategory = curElementType.Category;
                                                if (curCategory != null)
                                                {
                                                    string curCategoryName = curCategory.Name;
                                                    foreach (string curKeyword in keywords)
                                                    {
                                                        if (curCategoryName.IndexOf(curKeyword, StringComparison.OrdinalIgnoreCase) >= 0)
                                                        {
                                                            TreeViewItem curCategoryNode    = null;
                                                            TreeViewItem curFamilyNode      = null;
                                                            TreeViewItem curElementTypeNode = null;

                                                            if (this.categoryNodesDict.ContainsKey(curCategoryName))
                                                            {
                                                                curCategoryNode = categoryNodesDict[curCategoryName];
                                                            }
                                                            else
                                                            {
                                                                curCategoryNode        = new TreeViewItem();
                                                                curCategoryNode.Header = curCategoryName;
                                                                curCategoryNode.Tag    = null;

                                                                this.categoryNodesDict.Add(curCategoryName, curCategoryNode);
                                                                this.treeViewTool.Items.Add(curCategoryNode);
                                                            }

                                                            if (curCategoryNode != null)
                                                            {
                                                                Tuple <string, string> curFamCatTuple = new Tuple <string, string>(curCategoryName, curFamilyName);
                                                                if (this.familyNodesDict.ContainsKey(curFamCatTuple))
                                                                {
                                                                    curFamilyNode = this.familyNodesDict[curFamCatTuple];
                                                                }
                                                                else
                                                                {
                                                                    curFamilyNode        = new TreeViewItem();
                                                                    curFamilyNode.Header = curFamilyName;
                                                                    curFamilyNode.Tag    = null;

                                                                    this.familyNodesDict.Add(curFamCatTuple, curFamilyNode);
                                                                    curCategoryNode.Items.Add(curFamilyNode);
                                                                }
                                                            }

                                                            if (curFamilyNode != null)
                                                            {
                                                                curElementTypeNode        = new TreeViewItem();
                                                                curElementTypeNode.Header = curElementType.Name;
                                                                curElementTypeNode.Tag    = curElementType;

                                                                curFamilyNode.Items.Add(curElementTypeNode);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Autodesk.Revit.UI.TaskDialog.Show("Exception", ex.Message);
                    }
                }
            }
        }
        private void ImportAutoCADFile(int id)
        {
            if (uidoc.Document != null)
            {
                preCurveElementIds.Clear();
                FilteredElementCollector preElementDocFilter = new FilteredElementCollector(doc);
                if (preElementDocFilter != null)
                {
                    FilteredElementIdIterator preElementsIdsIterator = preElementDocFilter.OfClass(typeof(CurveElement)).GetElementIdIterator();
                    while (preElementsIdsIterator.MoveNext())
                    {
                        preCurveElementIds.Add(preElementsIdsIterator.Current);
                    }
                }

                preTextNotesIds.Clear();
                FilteredElementCollector preTextNoteDocFilter = new FilteredElementCollector(doc);
                if (preTextNoteDocFilter != null)
                {
                    FilteredElementIdIterator preElementsIdsIterator = preTextNoteDocFilter.OfClass(typeof(TextNote)).GetElementIdIterator();
                    while (preElementsIdsIterator.MoveNext())
                    {
                        preTextNotesIds.Add(preElementsIdsIterator.Current);
                    }
                }

                preTextNoteTypesIds.Clear();
                FilteredElementCollector preTextNoteTypeDocFilter = new FilteredElementCollector(doc);
                if (preTextNoteTypeDocFilter != null)
                {
                    FilteredElementIdIterator preElementsIdsIterator = preTextNoteTypeDocFilter.OfClass(typeof(TextNoteType)).GetElementIdIterator();
                    while (preElementsIdsIterator.MoveNext())
                    {
                        preTextNoteTypesIds.Add(preElementsIdsIterator.Current);

                        TextNoteType preTextNodeType = doc.GetElement(preElementsIdsIterator.Current) as TextNoteType;
                        if (preTextNodeType != null)
                        {
                            this.revitTextNoteTypesDict.Add(preTextNodeType.Name, preTextNodeType);
                        }
                    }
                }

                Category preLinesStyles = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
                foreach (Category preLineCategory in preLinesStyles.SubCategories)
                {
                    preLineCategoriesIds.Add(preLineCategory.Id);
                    this.revitCategoriesDict.Add(preLineCategory.Name, preLineCategory);
                }


                preGraphycStyleIds.Clear();
                FilteredElementCollector preGraphycsStyleDocFilter = new FilteredElementCollector(doc);
                if (preGraphycsStyleDocFilter != null)
                {
                    FilteredElementIdIterator preElementsIdsIterator = preGraphycsStyleDocFilter.OfClass(typeof(GraphicsStyle)).GetElementIdIterator();
                    while (preElementsIdsIterator.MoveNext())
                    {
                        GraphicsStyle preGraphicsStyle = doc.GetElement(preElementsIdsIterator.Current) as GraphicsStyle;
                        if (preGraphicsStyle != null)
                        {
                            if (preLineCategoriesIds.Contains(preGraphicsStyle.GraphicsStyleCategory.Id))
                            {
                                preGraphycStyleIds.Add(preElementsIdsIterator.Current);

                                revitLineStylesDict.Add(preGraphicsStyle.Name, preGraphicsStyle);
                            }
                        }
                    }
                }

                #region
                deleteImportInstances();
                DWGImportOptions importOptions = new DWGImportOptions();
                importOptions.OrientToView = true;
                importOptions.Placement    = ImportPlacement.Origin;
                doc.Import(fileName, importOptions, importView, out importInstanceId);
                if (importInstanceId != null)
                {
                    explodeImportInstance();
                }
                uidoc.RefreshActiveView();


                curCurveElementIds.Clear();
                FilteredElementCollector curElementDocFilter = new FilteredElementCollector(doc);
                if (curElementDocFilter != null)
                {
                    FilteredElementIdIterator curElementsIdsIterator = curElementDocFilter.OfClass(typeof(CurveElement)).GetElementIdIterator();
                    while (curElementsIdsIterator.MoveNext())
                    {
                        curCurveElementIds.Add(curElementsIdsIterator.Current);
                    }
                }

                curTextNotesIds.Clear();
                FilteredElementCollector curDocFilter = new FilteredElementCollector(doc);
                if (curDocFilter != null)
                {
                    FilteredElementIdIterator curElementsIdsIterator = curDocFilter.OfClass(typeof(TextNote)).GetElementIdIterator();
                    while (curElementsIdsIterator.MoveNext())
                    {
                        curTextNotesIds.Add(curElementsIdsIterator.Current);
                    }
                }

                curTextNoteTypesIds.Clear();
                FilteredElementCollector curTextNoteTypeDocFilter = new FilteredElementCollector(doc);
                if (curTextNoteTypeDocFilter != null)
                {
                    FilteredElementIdIterator curElementsIdsIterator = curTextNoteTypeDocFilter.OfClass(typeof(TextNoteType)).GetElementIdIterator();
                    while (curElementsIdsIterator.MoveNext())
                    {
                        curTextNoteTypesIds.Add(curElementsIdsIterator.Current);
                    }
                }

                Category curLinesStyles = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines);
                foreach (Category curLineCategory in curLinesStyles.SubCategories)
                {
                    curLineCategoriesIds.Add(curLineCategory.Id);
                }


                curGraphycStyleIds.Clear();
                FilteredElementCollector curGraphycsStyleDocFilter = new FilteredElementCollector(doc);
                if (curGraphycsStyleDocFilter != null)
                {
                    FilteredElementIdIterator curElementsIdsIterator = curGraphycsStyleDocFilter.OfClass(typeof(GraphicsStyle)).GetElementIdIterator();
                    while (curElementsIdsIterator.MoveNext())
                    {
                        GraphicsStyle curGraphicsStyle = doc.GetElement(curElementsIdsIterator.Current) as GraphicsStyle;
                        if (curGraphicsStyle != null)
                        {
                            if (curLineCategoriesIds.Contains(curGraphicsStyle.GraphicsStyleCategory.Id))
                            {
                                curGraphycStyleIds.Add(curElementsIdsIterator.Current);
                            }
                        }
                    }
                }
                #endregion

                difCurveElementIds.Clear();
                difCurveElementIds.UnionWith(curCurveElementIds);
                difCurveElementIds.ExceptWith(preCurveElementIds);

                difTextNotesIds.Clear();
                difTextNotesIds.UnionWith(curTextNotesIds);
                difTextNotesIds.ExceptWith(preTextNotesIds);

                difTextNoteTypesIds.Clear();
                difTextNoteTypesIds.UnionWith(curTextNoteTypesIds);
                difTextNoteTypesIds.ExceptWith(preTextNoteTypesIds);

                difLineCategoriesIds.Clear();
                difLineCategoriesIds.UnionWith(curLineCategoriesIds);
                difLineCategoriesIds.ExceptWith(preLineCategoriesIds);

                difGraphycStyleIds.Clear();
                difGraphycStyleIds.UnionWith(curGraphycStyleIds);
                difGraphycStyleIds.ExceptWith(preGraphycStyleIds);


                foreach (Category curLineCategory in curLinesStyles.SubCategories)
                {
                    if (difLineCategoriesIds.Contains(curLineCategory.Id))
                    {
                        Color curColor = curLineCategory.LineColor;
                        Tuple <byte, byte, byte> curColorTuple = new Tuple <byte, byte, byte>(curColor.Red, curColor.Green, curColor.Blue);

                        if (autocadColorLineStylesDict.ContainsKey(curColorTuple))
                        {
                            autocadColorLineStylesDict[curColorTuple].Add(curLineCategory);
                        }
                        else
                        {
                            List <Category> norList = new List <Category>();
                            norList.Add(curLineCategory);
                            autocadColorLineStylesDict.Add(curColorTuple, norList);
                        }
                    }
                }

                #region
                Autodesk.Revit.DB.Options geoOptions = uiapp.Application.Create.NewGeometryOptions();
                geoOptions.ComputeReferences = true;
                FilteredElementCollector curElementsDocFilter = new FilteredElementCollector(doc);
                if (curElementsDocFilter != null)
                {
                    FilteredElementIdIterator curElementsIdsIterator = curElementsDocFilter.WhereElementIsNotElementType().GetElementIdIterator();
                    while (curElementsIdsIterator.MoveNext())
                    {
                        ElementId curElementId = curElementsIdsIterator.Current;
                        if (curElementId != null)
                        {
                            Element curElement = doc.GetElement(curElementId);
                            if (curElement != null)
                            {
                                GeometryElement curGeometryElement = curElement.get_Geometry(geoOptions);
                                if (curGeometryElement != null)
                                {
                                    foreach (GeometryObject curGeometryObject in curGeometryElement)
                                    {
                                        ElementId curGraphicsStyleId = curGeometryObject.GraphicsStyleId;
                                        if (curGraphicsStyleId != null)
                                        {
                                            GraphicsStyle curGraphicsStyle = doc.GetElement(curGraphicsStyleId) as GraphicsStyle;
                                            if (curGraphicsStyle != null)
                                            {
                                                string curGraphicsStyleName = curGraphicsStyle.Name;

                                                Category curGraphicsStyleCategory = curGraphicsStyle.GraphicsStyleCategory;
                                                if (curGraphicsStyleCategory != null)
                                                {
                                                    string curGraphicsStyleCategoryName = curGraphicsStyleCategory.Name;

                                                    if (this.curGraphicsStylesNamesCount.ContainsKey(curGraphicsStyleCategoryName))
                                                    {
                                                        this.curGraphicsStylesNamesCount[curGraphicsStyleCategoryName]++;
                                                    }
                                                    else
                                                    {
                                                        this.curGraphicsStylesNamesCount.Add(curGraphicsStyleCategoryName, 1);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion


                CadDetailConverterOutputForm outputForm = new CadDetailConverterOutputForm(this);
                outputForm.ShowDialog();

                uiapp.Idling += uiapp_Idling;
            }
        }
        public void init()
        {
            try
            {
                FilteredElementCollector docFilter = new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(typeof(FamilyInstance));
                if (docFilter != null)
                {
                    FilteredElementIdIterator docFilterIterator = docFilter.GetElementIdIterator();
                    while (docFilterIterator.MoveNext())
                    {
                        ElementId selectedElementId = docFilterIterator.Current;
                        if (selectedElementId != null)
                        {
                            Element selectedElement = this.doc.GetElement(selectedElementId);
                            if (selectedElement != null)
                            {
                                if (selectedElement.Category.Name == "Doors")
                                {
                                    if (selectedElement is FamilyInstance)
                                    {
                                        FamilyInstance selectedDoor = selectedElement as FamilyInstance;
                                        if (selectedDoor != null)
                                        {
                                            this.selectedDoors.Add(selectedDoor);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                #region

                /*
                 * while (true)
                 * {
                 *  Reference selectedReference = uidoc.Selection.PickObject(ObjectType.Element, "Select doors in order to be renumbered. Press ESC key when finished.");
                 *  if (selectedReference != null)
                 *  {
                 *      ElementId selectedElementId = selectedReference.ElementId;
                 *      if (selectedElementId != null)
                 *      {
                 *              Element selectedElement = this.doc.GetElement(selectedElementId);
                 *              if (selectedElement != null)
                 *              {
                 *                  if (selectedElement.Category.Name == "Doors")
                 *                  {
                 *                      if (selectedElement is FamilyInstance)
                 *                      {
                 *                          FamilyInstance selectedDoor = selectedElement as FamilyInstance;
                 *                          if (selectedDoor != null)
                 *                          {
                 *                              this.selectedDoors.Add(selectedDoor);
                 *                          }
                 *                      }
                 *                  }
                 *                  else
                 *                  {
                 *                      TaskDialog.Show("Info", "Your selection is not a door, it will not be counted.");
                 *                  }
                 *              }
                 *      }
                 *  }
                 * }
                 */
                #endregion
            }
            catch (Exception ex)
            {
            }

            if (this.selectedDoors.Count > 0)
            {
                AutomaticDoorRenumberToFromForm form = new AutomaticDoorRenumberToFromForm();
                DialogResult result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    int selectedDirection = form.getSelectedDirection();
                    AutomaticDoorRenumberToFromHandler handler = new AutomaticDoorRenumberToFromHandler(this.selectedDoors, selectedDirection);
                    handler.Execute(uiapp);
                }
            }
            else
            {
                TaskDialog.Show("Info", "You did not select any door to renumber.");
            }
        }