GetSingleSelectedElementOrPrompt( UIDocument uidoc) { Element e = null; //ElementSet ss = uidoc.Selection.Elements; // 2014 ICollection <ElementId> ids = uidoc.Selection.GetElementIds(); // 2015 if (1 == ids.Count) { //ElementSetIterator iter = ss.ForwardIterator(); //iter.MoveNext(); //e = iter.Current as Element; e = uidoc.Document.GetElement(ids.First()); } else { string sid; DialogResult result = DialogResult.OK; while (null == e && DialogResult.OK == result) { using (ElementIdForm form = new ElementIdForm()) { result = form.ShowDialog(); sid = form.ElementId; } if (DialogResult.OK == result) { if (0 == sid.Length) { try { Reference r = uidoc.Selection.PickObject( ObjectType.Element, "Please pick an element"); //e = r.Element; // 2011 e = uidoc.Document.GetElement(r); // 2012 } catch (OperationCanceledException) { } } else { ElementId id = new ElementId( int.Parse((sid))); e = uidoc.Document.GetElement(id); if (null == e) { ErrorMsg(string.Format( "Invalid element id '{0}'.", sid)); } } } } } return(e); }
public static Element GetSingleSelectedElementOrPrompt( UIDocument uidoc) { Element e = null; //ElementSet ss = uidoc.Selection.Elements; // 2014 ICollection<ElementId> ids = uidoc.Selection.GetElementIds(); // 2015 if( 1 == ids.Count ) { //ElementSetIterator iter = ss.ForwardIterator(); //iter.MoveNext(); //e = iter.Current as Element; e = uidoc.Document.GetElement( ids.First() ); } else { string sid; DialogResult result = DialogResult.OK; while( null == e && DialogResult.OK == result ) { using( ElementIdForm form = new ElementIdForm() ) { result = form.ShowDialog(); sid = form.ElementId; } if( DialogResult.OK == result ) { if( 0 == sid.Length ) { try { Reference r = uidoc.Selection.PickObject( ObjectType.Element, "Please pick an element" ); //e = r.Element; // 2011 e = uidoc.Document.GetElement( r ); // 2012 } catch( OperationCanceledException ) { } } else { ElementId id = new ElementId( int.Parse( ( sid ) ) ); e = uidoc.Document.GetElement( id ); if( null == e ) { ErrorMsg( string.Format( "Invalid element id '{0}'.", sid ) ); } } } } } return e; }