Search code examples
c#autocad-plugin

Get all selected objects in acitve Autocad document


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

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.