Esempio n. 1
0
        GetSingleSelectedElementOrPrompt(
            UIDocument uidoc)
        {
            Element e = null;

            ICollection <ElementId> ids
                = uidoc.Selection.GetElementIds(); // 2015

            if (1 == ids.Count)
            {
                foreach (ElementId id in ids)
                {
                    e = uidoc.Document.GetElement(id);
                }
            }
            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
                        {
                            int id;
                            if (int.TryParse(sid, out id))
                            {
                                ElementId elementId = new ElementId(
                                    id);


                                e = uidoc.Document.GetElement(elementId);
                                if (null == e)
                                {
                                    ErrorMsg(string.Format(
                                                 "Invalid element id '{0}'.",
                                                 sid));
                                }
                            }
                            else
                            {
                                e = uidoc.Document.GetElement(sid);
                                if (null == e)
                                {
                                    ErrorMsg(string.Format(
                                                 "Invalid element id '{0}'.",
                                                 sid));
                                }
                            }
                        }
                    }
                }
            }
            return(e);
        }
Esempio n. 2
0
        public static Element GetSingleSelectedElementOrPrompt(
            UIDocument uidoc)
        {
            Element e = null;

              ICollection<ElementId> ids
            = uidoc.Selection.GetElementIds(); // 2015

              if( 1 == ids.Count )
              {
            foreach( ElementId id in ids )
            {
              e = uidoc.Document.GetElement( id );
            }
              }
              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
            {
              int id;
              if( int.TryParse( sid, out id ) )
              {
                ElementId elementId = new ElementId(
                    id );

                e = uidoc.Document.GetElement( elementId );
                if( null == e )
                {
                  ErrorMsg( string.Format(
                      "Invalid element id '{0}'.",
                      sid ) );
                }
              }
              else
              {
                e = uidoc.Document.GetElement( sid );
                if( null == e )
                {
                  ErrorMsg( string.Format(
                      "Invalid element id '{0}'.",
                      sid ) );
                }
              }
            }
              }
            }
              }
              return e;
        }