title: c# Autocad Map 3D 2012 get user drawn object
body: <p>Is it possible to send a string to execute to Map and find out what objects has the user drawn in an easier way? I feel that like what I am doing is too much...</p>
  
  <p>This is the command:</p>
  
  <pre><code>    acad.DocumentManager.MdiActiveDocument.SendStringToExecute
  ("_MPOLYGON ", false, false, true);
  </code></pre>
  
  <p>As of now, what I do is I subscribe to the object appended event, and to the command ended event, then I call the sendStringToExecute and from there I check all the new objects coming in the DB and keep a reference to the ones I find the user has drawn.</p>
  
  <p>So first:</p>
  
  <pre><code>    HostApplicationServices.WorkingDatabase.ObjectAppended += 
  new ObjectEventHandler(activeDB_ObjectAppended);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    acad.DocumentManager.MdiActiveDocument.CommandEnded += 
  new CommandEventHandler(MdiActiveDocument_CommandEnded);
  </code></pre>
  
  <p>While the command is executing:</p>
  
  <pre><code>void activeDB_ObjectAppended(object sender, ObjectEventArgs e)
          {
              polyDessinIds.Add(e.DBObject.Id);
          }
  </code></pre>
  
  <p>Autocad adds objects to the database WHILE the _MPOLYGON command is being executed, so I keep a reference to all them. Autocad adds points and lines to the database while the user is drawing. For example, the first and second click that for a line seem to be put in the database. Also when a polygon is added to the working db it does not mean it is the last one. The last added object is not guaranteed to be the real object either.</p>
  
  <p>Then when the command is ended, I look for the correct objectId, even if there are many polygons that were drawn, there is only 1 valid objectId that references the one or the many polygons drawn.</p>
  
  <pre><code> foreach (ObjectId obj in polyDessinIds)
   {
              if (obj.IsErased == false &amp;&amp; obj.ObjectClass.Name == "AcDbMPolygon")
              {
               mPolygons = obj;
               //there should be only one valid at this point
               break;
              }
   }
  </code></pre>
  
  <p>I am wondering if there is a better and more reliable way to do this? Maybe something like when you prompt for selecting objects, this returns a selection set with all the objects. Is there a way to "promptForPolygon" or something similar that will just return me the new objects once the user has drawn them?</p>