RevitLookup.Test.TestElements.SimpleTag C# (CSharp) Method

SimpleTag() public method

Add a simple tag to selected elements.
public SimpleTag ( ) : void
return void
        public void SimpleTag()
        {
            Revit.Document docu = m_revitApp.ActiveUIDocument.Document;

              //Autodesk.Revit.UI.Selection.SelElementSet elemSet = m_revitApp.ActiveUIDocument.Selection.Elements; // 2015, jeremy: 'Autodesk.Revit.UI.Selection.SelElementSet' is obsolete: 'This class is deprecated in Revit 2015. Use Selection.SetElementIds() and Selection.GetElementIds() instead.'
              ICollection<ElementId> ids = m_revitApp.ActiveUIDocument.Selection.GetElementIds();  // 2016, jeremy

              Autodesk.Revit.DB.View view = m_revitApp.ActiveUIDocument.Document.ActiveView;

              XYZ tagLocation = new XYZ();

              //Revit.ElementSetIterator elemIter = elemSet.ForwardIterator();
              //while( elemIter.MoveNext() )

              foreach(ElementId id in ids )
              {
            //Revit.Element elem = elemIter.Current as Revit.Element;
            Revit.Element elem = docu.GetElement( id );

            Revit.Location location = elem.Location;

            // for elements like walls
            Revit.LocationCurve locCurve = location as Revit.LocationCurve;
            if( null != locCurve )
            {
              XYZ startPt = locCurve.Curve.GetEndPoint( 0 );
              XYZ endPt = locCurve.Curve.GetEndPoint( 1 );
              double x = ( endPt.X + startPt.X ) / 2;
              double y = ( endPt.Y + startPt.Y ) / 2;
              tagLocation = new XYZ( x, y, 0 );
            }

            // for familyinstances
            Revit.LocationPoint locPt = location as Revit.LocationPoint;
            if( null != locPt )
            {
              tagLocation = locPt.Point;
            }

            // Add the tag.
            if( null != elem )
            {
              m_revitApp.ActiveUIDocument.Document.Create.NewTag( view, elem, true, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, tagLocation );
            }
              }
        }