Question

How can I get all selected objects on the active Autocad drawing in my c# Autocad plug-in application?

I have tried to get a selection set as follows:

SelectionSet Selection = AcadApp.DocumentManager.MdiActiveDocument.Editor.SelectImplied().Value;

foreach (SelectedObject Instance in Selection) ...

It seems that I can get selected objects if I have such selection set. Te problem is I get null reference exception in line:

AcadApp.DocumentManager.MdiActiveDocument.Editor.SelectImplied().Value
Was it helpful?

Solution

I got he solution.

AcadApp.DocumentManager.MdiActiveDocument.Editor.SelectImplied().Value

This code gives the selected objects but as I noted in the question I get null reference exception. This was because I was trying o get objects in a background thread. http://adndevblog.typepad.com/autocad/2012/06/use-thread-for-background-processing.html mentions this problem.

Problem solved when I call MdiActiveDocument in the main thread and then I send the result o my background thread for processing.

OTHER TIPS

I think This is what your looking for. I taped this out without an IDE so check it.

using AcApp = Autodesk.Autocad.ApplicationServices.Application;


public class yourclass 
{
  public Document AcDoc {
        get { return AcApp.DocumentManager.MdiActiveDocument;} 
    }     
  public static void getSelectionSet()
  {
    var _editor = AcDoc.Editor;
    var _selAll = ed.SelectAll();
    var _SelectionSet = _selAll.Value;

    using(var trans = AcDoc.TransactionManager.StartTransaction()){
      foreach(var ObjId in _SelectionSet.GetObjectIds()){
        // apply logic
      }
      trans.Commit();
    } 
  }

or if you want to return a SelectionSet

    public class yourclass
{
public Document AcDoc {
    get { return AcApp.DocumentManager.MdiActiveDocument;}
}

    public static SelectionSet getSelectionSet()
    {
        var _editor = AcDoc.Editor;
        var _selAll = ed.SelectAll();
        return _selAll.Value;
    }
}

forgive the formatting, i cant get it to work right on stack

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top