/// <summary> /// Create a Revit Tag for a Revit Element at an offset location /// from the element's view extents /// </summary> /// <param name="view">View to tag in</param> /// <param name="element">Element to tag</param> /// <param name="horizontal">Optional: Place tag horizontal, /// defaults to true</param> /// <param name="addLeader">Optional: Add a leader, defaults to false</param> /// <param name="offset">Optional: Offset Vector, defaults to 0,0,0</param> /// <param name="horizontalAlignment">Optional: Horizontal Alignment /// within the element's extents, defaults to Center</param> /// <param name="verticalAlignment">Optional: Vertical Alignment /// within the element's extents, defaults to Middle</param> /// <returns></returns> /// <search> /// tagelement,annotate,documentation,tagoffset,movetag /// </search> public static Tag ByElementAndOffset(Revit.Elements.Views.View view, Element element, [DefaultArgument("Autodesk.DesignScript.Geometry.Vector.ByCoordinates(0,0,0)")] Autodesk.DesignScript.Geometry.Vector offset, string horizontalAlignment = "Center", string verticalAlignment = "Middle", bool horizontal = true, bool addLeader = false) { Autodesk.Revit.DB.HorizontalAlignmentStyle horizontalAlignmentStyle = HorizontalAlignmentStyle.Center; if (!Enum.TryParse <Autodesk.Revit.DB.HorizontalAlignmentStyle>(horizontalAlignment, out horizontalAlignmentStyle)) { horizontalAlignmentStyle = HorizontalAlignmentStyle.Center; } Autodesk.Revit.DB.VerticalAlignmentStyle verticalAlignmentStyle = VerticalAlignmentStyle.Middle; if (!Enum.TryParse <Autodesk.Revit.DB.VerticalAlignmentStyle>(verticalAlignment, out verticalAlignmentStyle)) { verticalAlignmentStyle = VerticalAlignmentStyle.Middle; } Autodesk.Revit.DB.View revitView = (Autodesk.Revit.DB.View)view.InternalElement; // Tagging elements by element category Autodesk.Revit.DB.TagMode tagMode = TagMode.TM_ADDBY_CATEGORY; Autodesk.Revit.DB.TagOrientation orientation = (horizontal) ? TagOrientation.Horizontal : TagOrientation.Vertical; if (!view.IsAnnotationView()) { throw new Exception(Properties.Resources.ViewDoesNotSupportAnnotations); } XYZ location = GetExtentsWithOffset(element, revitView, offset.ToRevitType(true), verticalAlignmentStyle, horizontalAlignmentStyle); return(new Tag(revitView, element.InternalElement, orientation, tagMode, addLeader, location)); }
/// <summary> /// Create a Revit Tag for a Revit Element /// </summary> /// <param name="view">View to Tag</param> /// <param name="element">Element to tag</param> /// <param name="horizontal">Horizontal alignment</param> /// <param name="addLeader">Add a leader</param> /// <param name="offset">Offset Vector or Tag Location</param> /// <param name="isOffset">Specifies if the point is being used as an offset vector or if it specifies the tags location</param> /// <param name="horizontalAlignment">Horizontal Alignment</param> /// <param name="verticalAlignment">Vertical Alignment</param> /// <returns></returns> public static Tag ByElement(Revit.Elements.Views.View view, Element element, bool horizontal, bool addLeader, string horizontalAlignment, string verticalAlignment, [DefaultArgument("Autodesk.DesignScript.Geometry.Vector.ByCoordinates(0,0,0)")] Autodesk.DesignScript.Geometry.Vector offset, bool isOffset = true) { Autodesk.Revit.DB.HorizontalAlignmentStyle horizontalAlignmentStyle = HorizontalAlignmentStyle.Center; if (!Enum.TryParse <Autodesk.Revit.DB.HorizontalAlignmentStyle>(horizontalAlignment, out horizontalAlignmentStyle)) { horizontalAlignmentStyle = HorizontalAlignmentStyle.Center; } Autodesk.Revit.DB.VerticalAlignmentStyle verticalAlignmentStyle = VerticalAlignmentStyle.Middle; if (!Enum.TryParse <Autodesk.Revit.DB.VerticalAlignmentStyle>(verticalAlignment, out verticalAlignmentStyle)) { verticalAlignmentStyle = VerticalAlignmentStyle.Middle; } Autodesk.Revit.DB.View revitView = (Autodesk.Revit.DB.View)view.InternalElement; Autodesk.Revit.DB.XYZ point = offset.ToRevitType(true); Autodesk.Revit.DB.TagMode tagMode = TagMode.TM_ADDBY_CATEGORY; Autodesk.Revit.DB.TagOrientation orientation = (horizontal) ? TagOrientation.Horizontal : TagOrientation.Vertical; if (!view.IsAnnotationView()) { throw new Exception(Properties.Resources.ViewDoesNotSupportAnnotations); } if (isOffset) { BoundingBoxXYZ box = element.InternalElement.get_BoundingBox(revitView); if (box == null) { box = element.InternalElement.get_BoundingBox(null); } if (box != null) { double Y, X = 0; switch (verticalAlignmentStyle) { case VerticalAlignmentStyle.Bottom: Y = box.Min.Y; break; case VerticalAlignmentStyle.Top: Y = box.Max.Y; break; default: Y = box.Min.Y + ((box.Max.Y - box.Min.Y) / 2); break; } switch (horizontalAlignmentStyle) { case HorizontalAlignmentStyle.Left: X = box.Min.X; break; case HorizontalAlignmentStyle.Right: X = box.Max.X; break; default: X = box.Min.X + ((box.Max.X - box.Min.X) / 2); break; } point = new XYZ(X + point.X, Y + point.Y, 0 + point.Z); } else { if (element.InternalElement.Location != null) { if (element.InternalElement.Location.GetType() == typeof(LocationCurve)) { LocationCurve lc = (LocationCurve)element.InternalElement.Location; XYZ midpoint = lc.Curve.Evaluate(0.5, true); point = new XYZ(midpoint.X + point.X, midpoint.Y + point.Y, 0 + point.Z); } else if (element.InternalElement.Location.GetType() == typeof(LocationPoint)) { LocationPoint lp = (LocationPoint)element.InternalElement.Location; point = new XYZ(lp.Point.X + point.X, lp.Point.Y + point.Y, 0 + point.Z); } else { throw new Exception(Properties.Resources.InvalidElementLocation); } } else { throw new Exception(Properties.Resources.InvalidElementLocation); } } } return(new Tag(revitView, element.InternalElement, orientation, tagMode, addLeader, point)); }
/// <summary> /// Create an element based Tag /// </summary> /// <param name="view">View to Tag</param> /// <param name="element">Element to tag</param> /// <param name="horizontal">Horizontal alignment</param> /// <param name="addLeader">Add a leader</param> /// <param name="offset">Offset Vector or Tag Location</param> /// <param name="isOffset">Specifies if the point is being used as an offset vector or if it specifies the tags location</param> /// <param name="horizontalAlignment">Horizontal Alignment</param> /// <param name="verticalAlignment">Vertical Alignment</param> /// <returns></returns> public static Tag ByElement(Revit.Elements.Views.View view, Element element, bool horizontal, bool addLeader, Autodesk.DesignScript.Geometry.Point offset = null, bool isOffset = true, string horizontalAlignment = "Center", string verticalAlignment = "Middle") { if (offset == null) { offset = Autodesk.DesignScript.Geometry.Point.ByCoordinates(0, 0, 0); } Autodesk.Revit.DB.HorizontalAlignmentStyle alignHorizontal = Autodesk.Revit.DB.HorizontalAlignmentStyle.Center; Enum.TryParse <Autodesk.Revit.DB.HorizontalAlignmentStyle>(horizontalAlignment, out alignHorizontal); Autodesk.Revit.DB.VerticalAlignmentStyle alignVertical = Autodesk.Revit.DB.VerticalAlignmentStyle.Middle; Enum.TryParse <Autodesk.Revit.DB.VerticalAlignmentStyle>(verticalAlignment, out alignVertical); //Autodesk.Revit.DB.Document document = DocumentManager.Instance.CurrentDBDocument; Autodesk.Revit.DB.View revitView = (Autodesk.Revit.DB.View)view.InternalElement; Autodesk.Revit.DB.XYZ point = offset.ToRevitType(true); Autodesk.Revit.DB.TagMode tagMode = TagMode.TM_ADDBY_CATEGORY; Autodesk.Revit.DB.TagOrientation orientation = (horizontal) ? TagOrientation.Horizontal : TagOrientation.Vertical; if (revitView.ViewType != ViewType.FloorPlan && revitView.ViewType != ViewType.EngineeringPlan && revitView.ViewType != ViewType.Detail && revitView.ViewType != ViewType.Section && revitView.ViewType != ViewType.Elevation && revitView.ViewType != ViewType.CeilingPlan) { throw new ArgumentException("Cannot place a Tag on active View"); } if (isOffset) { BoundingBoxXYZ box = element.InternalElement.get_BoundingBox(revitView); if (box == null) { box = element.InternalElement.get_BoundingBox(null); } if (box != null) { double Y, X = 0; switch (alignVertical) { case VerticalAlignmentStyle.Bottom: Y = box.Min.Y; break; case VerticalAlignmentStyle.Top: Y = box.Max.Y; break; default: Y = box.Min.Y + ((box.Max.Y - box.Min.Y) / 2); break; } switch (alignHorizontal) { case HorizontalAlignmentStyle.Left: X = box.Min.X; break; case HorizontalAlignmentStyle.Right: X = box.Max.X; break; default: X = box.Min.X + ((box.Max.X - box.Min.X) / 2); break; } point = new XYZ(X + point.X, Y + point.Y, 0 + point.Z); } else { if (element.InternalElement.Location != null) { if (element.InternalElement.Location.GetType() == typeof(LocationCurve)) { LocationCurve lc = (LocationCurve)element.InternalElement.Location; XYZ midpoint = lc.Curve.Evaluate(0.5, true); point = new XYZ(midpoint.X + point.X, midpoint.Y + point.Y, 0 + point.Z); } else if (element.InternalElement.Location.GetType() == typeof(LocationPoint)) { LocationPoint lp = (LocationPoint)element.InternalElement.Location; point = new XYZ(lp.Point.X + point.X, lp.Point.Y + point.Y, 0 + point.Z); } else { throw new ArgumentNullException("Cannot determine location"); } } else { throw new ArgumentNullException("Cannot determine location"); } } } return(new Tag(revitView, element.InternalElement, orientation, tagMode, addLeader, point)); }