Esempio n. 1
0
        public static List <IndependentTag> TagElements(this View view, ElementId elementId_TagType, BuiltInCategory builtInCategory, bool addLeader = false, TagOrientation tagOrientation = TagOrientation.Horizontal, bool allowDuplicates = false)
        {
            if (view == null || elementId_TagType == null || elementId_TagType == ElementId.InvalidElementId)
            {
                return(null);
            }

            Document document = view.Document;

            FamilySymbol familySymbol = document.GetElement(elementId_TagType) as FamilySymbol;

            if (familySymbol == null)
            {
                return(null);
            }

            BuiltInCategory builtInCategory_Tag = (BuiltInCategory)familySymbol.Category.Id.IntegerValue;

            if (!builtInCategory_Tag.IsValidTagCategory(builtInCategory))
            {
                return(null);
            }

            IEnumerable <ElementId> elementIds = new FilteredElementCollector(document, view.Id).OfCategory(builtInCategory).ToElementIds();

            if (elementIds == null)
            {
                return(null);
            }

            return(TagElements(view, elementId_TagType, elementIds, addLeader, tagOrientation, allowDuplicates));
        }
Esempio n. 2
0
        public static List <IndependentTag> TagElements(this View view, ElementId elementId_TagType, IEnumerable <ElementId> elementIds, bool addLeader = false, TagOrientation tagOrientation = TagOrientation.Horizontal, bool allowDuplicates = false)
        {
            if (view == null || elementId_TagType == null || elementId_TagType == ElementId.InvalidElementId)
            {
                return(null);
            }

            Document document = view.Document;

            FamilySymbol familySymbol = document.GetElement(elementId_TagType) as FamilySymbol;

            if (familySymbol == null)
            {
                return(null);
            }

            BuiltInCategory builtInCategory_Tag = (BuiltInCategory)familySymbol.Category.Id.IntegerValue;

            IEnumerable <ElementId> elementIds_View = new FilteredElementCollector(document, view.Id).ToElementIds();

            if (elementIds_View == null)
            {
                return(null);
            }

            List <IndependentTag> result = new List <IndependentTag>();

            if (elementIds_View.Count() == 0)
            {
                return(result);
            }

            foreach (ElementId elementId in elementIds_View)
            {
                if (elementId == null || elementId == ElementId.InvalidElementId)
                {
                    continue;
                }

                if (!elementIds.Contains(elementId))
                {
                    continue;
                }

                Element element = document.GetElement(elementId);
                if (element == null)
                {
                    continue;
                }

                if (!allowDuplicates)
                {
#if Revit2017
                    IList <ElementId> elementIds_Tags = null;
#else
                    IList <ElementId> elementIds_Tags = element.GetDependentElements(new LogicalAndFilter(new ElementClassFilter(typeof(IndependentTag)), new ElementOwnerViewFilter(view.Id)));
#endif

                    if (elementIds_Tags != null && elementIds_Tags.Count != 0)
                    {
                        ElementId elementId_Tag = elementIds_Tags.ToList().Find(x => document.GetElement(x).GetTypeId() == elementId_TagType);
                        if (elementId_Tag != null)
                        {
                            continue;
                        }
                    }
                }

                if (!builtInCategory_Tag.IsValidTagCategory((BuiltInCategory)element.Category.Id.IntegerValue))
                {
                    continue;
                }

                Autodesk.Revit.DB.Location location = element.Location;
                if (location == null)
                {
                    continue;
                }

                XYZ xyz = null;
                if (location is LocationCurve)
                {
                    LocationCurve locationCurve = (LocationCurve)location;
                    Curve         curve         = locationCurve.Curve;
                    if (curve == null)
                    {
                        continue;
                    }

                    XYZ xyz_1 = curve.GetEndPoint(0);
                    XYZ xyz_2 = curve.GetEndPoint(1);
                    xyz = new XYZ((xyz_1.X + xyz_2.X) / 2, (xyz_1.Y + xyz_2.Y) / 2, (xyz_1.Z + xyz_2.Z) / 2);
                }
                else if (location is LocationPoint)
                {
                    xyz = ((LocationPoint)location).Point;
                }

                if (xyz == null)
                {
                    continue;
                }

#if Revit2017
                IndependentTag independentTag = document.Create.NewTag(view, element, addLeader, TagMode.TM_ADDBY_CATEGORY, tagOrientation, xyz);
                independentTag?.ChangeTypeId(elementId_TagType);
#elif Revit2018
                Reference      reference      = new Reference(element);
                IndependentTag independentTag = IndependentTag.Create(document, view.Id, reference, addLeader, TagMode.TM_ADDBY_CATEGORY, tagOrientation, xyz);
                independentTag?.ChangeTypeId(elementId_TagType);
#else
                Reference      reference      = new Reference(element);
                IndependentTag independentTag = IndependentTag.Create(document, elementId_TagType, view.Id, reference, addLeader, tagOrientation, xyz);
#endif

                if (independentTag != null)
                {
                    result.Add(independentTag);
                }
            }

            return(result);
        }