Exemple #1
0
        /// <summary>
        /// Counts the number of datums in each drawing
        /// </summary>
        /// <param name="sheets">sheets to check</param>
        /// <param name="category">category of elements to count</param>
        /// <returns name="results">count of datums in each sheet</returns>
        /// <search>
        /// mace, structural, general, arrangement, drawing, datum, count, number
        /// </search>
        public static string NumberOfElementsInEachDrawing(Revit.Elements.Views.Sheet sheets, Revit.Elements.Category category)
        {
            Document doc = RevitServices.Persistence.DocumentManager.Instance.CurrentDBDocument;

            List <List <Revit.Elements.Element> > elements = Collector.ElementsInSheetByCategory(sheets, category);

            List <int> counts = new List <int>();

            foreach (List <Revit.Elements.Element> e in elements)
            {
                counts.Add(e.Count);
            }

            string output = sheets.SheetNumber.ToString() + " - " + sheets.SheetName.ToString() + " = " + counts[0].ToString();

            return(output);
        }
Exemple #2
0
        private static List <Autodesk.Revit.DB.Element> DuplicateViewport(Sheet oldSheet, Sheet newSheet, ViewDuplicateOption viewDuplicateOption, string prefix, string suffix)
        {
            var Viewports = oldSheet.Viewports.ToList();
            List <Revit.Elements.Views.View>            viewList     = new List <View>();
            List <Autodesk.Revit.DB.Element>            elements     = new List <Autodesk.Revit.DB.Element>();
            List <Autodesk.DesignScript.Geometry.Point> locationList = new List <Autodesk.DesignScript.Geometry.Point>();

            foreach (var viewport in Viewports)
            {
                if (viewport.View.InternalView.CanViewBeDuplicated(viewDuplicateOption))
                {
                    var newViewName = prefix + viewport.View.Name + suffix;
                    if (!CheckUniqueViewName(newViewName))
                    {
                        throw new ArgumentException(String.Format(Properties.Resources.ViewNameExists, newViewName));
                    }
                    var newViewId = viewport.View.InternalView.Duplicate(viewDuplicateOption);
                    var newView   = (Document.GetElement(newViewId) as Autodesk.Revit.DB.View).ToDSType(false) as View;
                    var param     = newView.InternalView.get_Parameter(BuiltInParameter.VIEW_NAME);
                    param.Set(prefix + viewport.View.Name + suffix);
                    viewList.Add(newView);
                    elements.Add(newView.InternalElement);
                }
                else
                {
                    throw new Exception(String.Format(Properties.Resources.ViewCantBeDuplicated, viewport.View.Name));
                }

                locationList.Add(viewport.BoxCenter);
            }

            newSheet.DuplicateViews = viewList;

            if (viewList.Count() == locationList.Count() && viewList.Any())
            {
                for (int i = 0; i < viewList.Count(); i++)
                {
                    ElementId sheetId      = newSheet.InternalView.Id;
                    ElementId viewId       = viewList[i].InternalView.Id;
                    XYZ       viewLocation = Revit.GeometryConversion.GeometryPrimitiveConverter.ToRevitType(locationList[i]);

                    if (!Autodesk.Revit.DB.Viewport.CanAddViewToSheet(Document, sheetId, viewId))
                    {
                        throw new InvalidOperationException(Properties.Resources.ViewAlreadyPlacedOnSheet);
                    }
                    Autodesk.Revit.DB.Viewport.Create(Document, sheetId, viewId, viewLocation);
                }
            }

            return(elements);
        }
Exemple #3
0
        /// <summary>
        /// Returns the percentage number of gridlines dimensioned in the drawing. If all gridlines are dimensioned the value should be 100%.
        /// </summary>
        /// <param name="sheets">sheets to check</param>
        /// <returns name="results">count of datums in each sheet</returns>
        /// <search>
        /// mace, structural, general, arrangement, drawing, datum, count, number
        /// </search>
        public static object PercentageOfDimensionedGridlines(Revit.Elements.Views.Sheet sheets)
        {
            Document doc = RevitServices.Persistence.DocumentManager.Instance.CurrentDBDocument;

            Autodesk.Revit.DB.Element unwrappedSheet = sheets.InternalElement;

            BuiltInCategory dimCat  = BuiltInCategory.OST_Dimensions;
            BuiltInCategory gridCat = BuiltInCategory.OST_Grids;

            //Get views on sheet
            Autodesk.Revit.DB.ViewSheet viewSheet = unwrappedSheet as Autodesk.Revit.DB.ViewSheet;
            List <ElementId>            viewIds   = viewSheet.GetAllPlacedViews().ToList();
            ElementId viewId = viewIds[0];

            FilteredElementCollector dimElements  = new FilteredElementCollector(doc, viewId).OfCategory(dimCat).WhereElementIsNotElementType();
            FilteredElementCollector gridElements = new FilteredElementCollector(doc, viewId).OfCategory(gridCat).WhereElementIsNotElementType();

            List <Autodesk.Revit.DB.Dimension> dimList = new List <Autodesk.Revit.DB.Dimension>();

            foreach (Autodesk.Revit.DB.Dimension dim in dimElements)
            {
                dimList.Add(dim as Autodesk.Revit.DB.Dimension);
            }

            List <int> gridList = new List <int>();

            foreach (Autodesk.Revit.DB.Element grid in gridElements)
            {
                gridList.Add(grid.Id.IntegerValue);
            }

            List <int> dimRefs = new List <int>();

            foreach (Autodesk.Revit.DB.Dimension d in dimList)
            {
                ReferenceArray refArray = d.References;
                int            size     = refArray.Size;
                RevitServices.Transactions.TransactionManager.Instance.EnsureInTransaction(doc);

                for (int i = 0; i < size; i++)
                {
                    Reference _ref = refArray.get_Item(i);
                    ElementId id   = _ref.ElementId;
                    //Autodesk.Revit.DB.Element revitElement = doc.GetElement(id);
                    dimRefs.Add(id.IntegerValue);
                }
            }

            List <int> uniqueIds = new HashSet <int>(dimRefs).ToList();

            List <bool> bools = new List <bool>();

            foreach (int id in gridList)
            {
                if (uniqueIds.Contains(id))
                {
                    bools.Add(true);
                }
                else
                {
                    bools.Add(false);
                }
            }

            double countTrue  = bools.Where(c => c).Count();
            double countTotal = bools.Count();

            double  num   = (countTrue / countTotal) * 100;
            decimal dec   = System.Convert.ToDecimal(num);
            decimal round = System.Math.Round(dec, 1);

            string output = sheets.SheetNumber.ToString() + " - " + sheets.SheetName.ToString() + " = " + round.ToString() + "%";

            return(output);
        }
Exemple #4
0
        /// <summary>
        /// Duplicates A Sheet.
        /// </summary>
        /// <param name="sheet">The Sheet to be Duplicated.</param>
        /// <param name="duplicateWithContents">Set to true that Duplicate sheet with contents</param>
        /// <param name="duplicateWithView">Set to true that Duplicate sheet with views.</param>
        /// <param name="viewDuplicateOption">Enter View Duplicate Option: Duplicate, AsDependent or WithDetailing.</param>
        /// <param name="prefix"></param>
        /// <param name="suffix">When prefix and suffix are both empty, suffix will set a default value - " - Copy".</param>
        /// <returns></returns>
        public static Sheet DuplicateSheet(Sheet sheet, bool duplicateWithContents = false, bool duplicateWithView = false, string viewDuplicateOption = "Duplicate", string prefix = "", string suffix = "")
        {
            if (sheet == null)
            {
                throw new ArgumentNullException(nameof(sheet));
            }

            ViewDuplicateOption Option = (ViewDuplicateOption)Enum.Parse(typeof(ViewDuplicateOption), viewDuplicateOption);

            if (String.IsNullOrEmpty(prefix) && String.IsNullOrEmpty(suffix))
            {
                suffix = " - Copy";
            }

            Sheet newSheet = null;

            try
            {
                RevitServices.Transactions.TransactionManager.Instance.EnsureInTransaction(Application.Document.Current.InternalDocument);

                var oldElements             = ElementBinder.GetElementsFromTrace <Autodesk.Revit.DB.Element>(Document);
                List <ElementId> elementIds = new List <ElementId>();
                var newSheetNumber          = prefix + sheet.SheetNumber + suffix;
                var newSheetName            = sheet.SheetName;
                List <Autodesk.Revit.DB.Element> TraceElements = new List <Autodesk.Revit.DB.Element>();

                if (oldElements != null)
                {
                    foreach (var element in oldElements)
                    {
                        elementIds.Add(element.Id);
                        if (element is ViewSheet)
                        {
                            var oldSheet = (element as ViewSheet).ToDSType(false) as Sheet;
                            if (oldSheet.SheetNumber.Equals(newSheetNumber))
                            {
                                if ((duplicateWithView && oldElements.Count() > 1) || (!duplicateWithView && oldElements.Count() == 1))
                                {
                                    newSheet = oldSheet;
                                    TraceElements.AddRange(oldElements);
                                }
                                if (newSheet != null)
                                {
                                    if (duplicateWithContents)
                                    {
                                        DuplicateSheetAnnotations(sheet, newSheet);
                                    }
                                    else
                                    {
                                        DeleteSheetAnnotations(newSheet);
                                    }
                                }
                            }
                        }
                    }
                    if (newSheet == null)
                    {
                        Autodesk.Revit.UI.UIDocument uIDocument = new Autodesk.Revit.UI.UIDocument(Document);
                        var openedViews       = uIDocument.GetOpenUIViews().ToList();
                        var shouldClosedViews = openedViews.FindAll(x => elementIds.Contains(x.ViewId));
                        if (shouldClosedViews.Count > 0)
                        {
                            foreach (var v in shouldClosedViews)
                            {
                                if (uIDocument.GetOpenUIViews().ToList().Count() > 1)
                                {
                                    v.Close();
                                }
                                else
                                {
                                    throw new InvalidOperationException(string.Format(Properties.Resources.CantCloseLastOpenView, v.ToString()));
                                }
                            }
                        }
                        Document.Delete(elementIds);
                    }
                }

                if (newSheet == null && TraceElements.Count == 0)
                {
                    // Create a new Sheet with different SheetNumber
                    var          titleBlockElement    = sheet.TitleBlock.First() as FamilyInstance;
                    FamilySymbol TitleBlock           = titleBlockElement.InternalFamilyInstance.Symbol;
                    FamilyType   titleBlockFamilyType = ElementWrapper.Wrap(TitleBlock, true);

                    if (!CheckUniqueSheetNumber(newSheetNumber))
                    {
                        throw new ArgumentException(String.Format(Properties.Resources.SheetNumberExists, newSheetNumber));
                    }

                    var viewSheet = Autodesk.Revit.DB.ViewSheet.Create(Document, TitleBlock.Id);
                    newSheet = new Sheet(viewSheet);
                    newSheet.InternalSetSheetName(newSheetName);
                    newSheet.InternalSetSheetNumber(newSheetNumber);
                    SetSheetInformation(sheet, newSheet);

                    TraceElements.Add(newSheet.InternalElement);

                    // Copy Annotation Elements from sheet to new sheet by ElementTransformUtils.CopyElements
                    if (duplicateWithContents)
                    {
                        DuplicateSheetAnnotations(sheet, newSheet);
                    }

                    if (duplicateWithView)
                    {
                        // Copy ScheduleSheetInstance except RevisionSchedule from sheet to new sheet by ElementTransformUtils.CopyElements
                        DuplicateScheduleSheetInstance(sheet, newSheet);

                        // Duplicate Viewport in sheet and place on new sheet
                        TraceElements.AddRange(DuplicateViewport(sheet, newSheet, Option, prefix, suffix));
                    }
                }

                ElementBinder.SetElementsForTrace(TraceElements);
                RevitServices.Transactions.TransactionManager.Instance.TransactionTaskDone();
            }
            catch (Exception e)
            {
                if (newSheet != null)
                {
                    newSheet.Dispose();
                }
                throw e;
            }

            return(newSheet);
        }