Author Topic: Selection set woe  (Read 472 times)

0 Members and 1 Guest are viewing this topic.


  • Newt
  • Posts: 46
Selection set woe
« on: May 20, 2019, 11:00:21 AM »
I'm using the following function to give me a selection set of Block refs on a specific layer.  AutoCADDrawingObjectName.BlockReference returns the text "INSERT". 

But it's returning an error warning (no code that I can pick up) for the selectionSetResult.Status value and Null for the value, when I know that there are Block refs for the layer I'm asking for.

Code: [Select]
        public static SelectionSet GetBlockRefOnLayerSelectionSet(string LayerName)
            Document thisDrawing;
            SelectionSet selectionSet = null;
            PromptSelectionResult selectionSetResult;
            TypedValue[] typedArray = new TypedValue[2];
            SelectionFilter selectionFilter;


                thisDrawing = GetDocument();

                Editor editor = thisDrawing.Editor;

                typedArray.SetValue(new TypedValue((int)DxfCode.Start, AutoCADDrawingObjectName.BlockReference), 0);           
                typedArray.SetValue(new TypedValue((int)DxfCode.LayerName, LayerName), 1); 

                selectionFilter = new SelectionFilter(typedArray);

                selectionSetResult = editor.SelectAll(selectionFilter);

                if (selectionSetResult.Value != null)
                    selectionSet = selectionSetResult.Value;
            catch (Exception ex)
                MessageBox.Show("AutoCADUtility.GetBlockRefOnLayerSelectionSet : " + ex.Message, MessageHeadings.ExceptionError);

            return selectionSet;

I can't help feeling that the Editor.SelectAll instruction is wrong.  The COM AcadSelectionSet class allows us to create global selection sets with the .Select method, but I cannot find the equivalent.

Can anyone see what I'm doing wrong here?

Regards, Paul


  • Water Moccasin
  • Posts: 2235
  • Marseille, France
Re: Selection set woe
« Reply #1 on: May 20, 2019, 11:46:38 AM »

Please, don't make C# as verbose/noisy as VB(A), you can declare and initialize your variable in a single expression.
You say "I know that there are Block refs for the layer I'm asking for.", but the code does not...
With .NET, we check for the PromptResult.Satus. PromptStatus.OK means objects have been selected PromptStatus.Error means the selection is empty.
And  forget what COM does, this is .NET here.

Try like this:
Code - C#: [Select]
  1.         public static SelectionSet GetBlockRefOnLayerSelectionSet(string layerName)
  2.         {
  3.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  4.             TypedValue[] typedValues = { new TypedValue(0, "INSERT"), new TypedValue(8, layerName) };
  5.             PromptSelectionResult selectionResult = ed.GetSelection(new SelectionFilter(typedValues));
  6.             if (selectionResult.Status == PromptStatus.OK)
  7.                 return selectionResult.Value;
  8.             else
  9.                 return null;
  10.         }
« Last Edit: May 20, 2019, 11:58:56 AM by gile »
Speaking English as a French Frog


  • Newt
  • Posts: 46
Re: Selection set woe
« Reply #2 on: May 21, 2019, 04:12:48 AM »
As I had hoped it was my programming elsewhere.  I wasn't setting the layer of the block reference.


  • Newt
  • Posts: 46
Re: Selection set woe
« Reply #3 on: May 21, 2019, 04:22:52 AM »
Thanks for confirming the code was working.