How to use the SelectByPolygon to select shapes from a layer

Hi,

I’m currently trying to achieve that i can select shapes from a layer within a polygon. The problem is that i’m not sure how to get the selected polygon corner points correctly. I saw a forum post here (Selection by polygon) and also checked out the MapWindow implementation (https://github.com/MapWindow/MapWindow5/blob/aa545a06ad5e730947374d2c828a95f3ddab1dfb/src/Plugins/MW5.ShapeEditor/MapListener.cs#L101) but i don’t get how to get the points of the polygon which i am drawing from those examples. On the map i select the points with left click and end the selection with CTRL + left click. The CTRL + left click invokes the ChooseLayerEvent but from there on i haven’t a good clue how to continue.

I hope you can help me, thanks in advance.

Hello @tilomi

Are you capturing the SelectionChanged event? That may be what you’re looking for.

You are provided with the Layer Handle of the layer, and you can then iterate the ‘selected’ shapes, something like:

Shapefile sf = axMap1.get_Shapefile(e.LayerHandle);
for(int i = 0; i < sf.numShapes; i++)
{
    if(sf.get_ShapeSelected(i))
    {
        // do something
    }
}

Hope that helps.
Jerry.

1 Like

Thanks for the response @jerryfaust
I now understood how to select the shapes with the tkCursorMode.cmSelectByPolygon Mode.
There were also two other important steps i was missing out, they were already mentioned in the forum post in the link above:

  1. To be able to select shapes in a layer the shapefile’s Selectable property has to be set to true before using the selection

  2. The CTRL + left click triggers the ChooseLayer Event (this is stated in the documentation). Since i wanted to select all shapes in the polygon area of all my layers on the map i had to set the e.layerHandle to -1 (this wasn’t stated explicitly in the documentation):

      private void MapOnChooseLayer(object sender, _DMapEvents_ChooseLayerEvent e)
     {
         e.layerHandle = -1;
     }
    

However i’m also trying to achieve a second goal which i might not stated clearly in my first post:
When finishing the polygon selection i want to get the polygon coordinates as e. g. a list of points.
I thought about capturing the MouseUpEvent and then adding the points to the list when i’m in the SelectByPolygon Mode. However this might be a bit error prone and if there is a way to retrieve the polygon directly after finishing the selection from another event please let me know.

Thanks for the help and best regards.

1 Like

Hello @tilomi

Glad you got that going. Just FYI, the OCX initializes the LayerHandle to -1 before raising the ChooseLayer event, so it will automatically be -1 unless you change it. In your case, you can simply omit the handler and it will work fine.

Regarding the points of the user-drawn polygon; those are not available publically. You can get them if you customize the OCX code. I once did this for a customer to expose the click points of the measuring tool.

Regards,
Jerry.

Hi again,

through other requirements i came across the Map.ShapeEditor which seems to be the location for the selected polygon shape. When listening to the AxMap.DblClick event the selected polygon can be read by using the Map.ShapeEditor.ValidatedShape Property. Also if there are errors (self-intersection or only two points but the polygon shape requires three points) the Map.ShapeValidationFailed event is fired which can be handled in user code. Unfortunately when the OnChooseLayer Event is fired, the ValidatedShape Property doesn’t contain the polygon shape anymore. So this workaround did avoid changing the OCX code for me just for your interest.

Best regards
Tilo

Thank you, Tilo, for the feedback.

I had overlooked that function, and I’m glad you found it.

Regards,
Jerry.

Hi. Good day. This was a great solution, but what do you suggest for vb6?

Hello @Sadjad_Dellon, and welcome.

I’m sorry, but I’m not sure what you’re asking. Are you asking for a VB6 code example of the above snippet? All of this can be done in VB6, but the syntax is a little different. For instance, many of the functions that are prefixed with get_ will not have the prefix in VB 6. So something like:

Dim sf As Shapefile
Set sf = axMap1.Shapefile(e.LayerHandle)

Dim i As Integer
For i = 0 To sf.numShapes - 1
    If sf.ShapeSelected(i) Then
        '' do something
    End If
Next i

I hope that helps.

Regards,
Jerry.

1 Like