How to add line shape to line shapefile interactively?

Hello. Can anyone help me? Thanks.

I have a line shapefile. I want to insert linear shapes into this shapefile interactively. It’s as if you inserted a line on a layer in AutoCAD. How can this be achieved?

The EditAddShape() function in the Shapefile class can insert linear shapes into the shapefile, but not interactively. The ShapeEditor class seems to be able to modify the shapefile interactively, but there are no examples, and I do n’t know how to use it after reading the documentation.

In addition, I also want to allow the user to see the edited line in real time during the mouse movement, just like a rubber band. (Similar to the effect of drawing a polyline in AutoCAD software.) How can this be done? Do I still need to use the DrawLine () function in the Drawing layer?

Is anyone willing to help me write a code example? Thanks.

1 Like

Hello Henry.

I’ve started to explore using the Measuring tool to draw geometry (lines or polygons), then extract the geometry from the Measuring class when done. The measuring tool effectively allows interactive drawing, showing lengths, then double-clicking when done. See the help here.

    private void buttonTest_Click(object sender, EventArgs e)
    {
        axMap1.CursorMode = tkCursorMode.cmMeasure;
        // MeasureDistance draws lines, Measure Area draws polygons
        axMap1.Measuring.MeasuringType = tkMeasuringType.MeasureDistance;
    }

    private void axMap1_MeasuringChanged(object sender, AxMapWinGIS._DMapEvents_MeasuringChangedEvent e)
    {
        // when the user double-clicks, we get the MeasuringStopped event
        if (e.action == tkMeasuringAction.MeasuringStopped)
        {
            // extact the points clicked to build a geometry
            for (int i = 0; i < axMap1.Measuring.PointCount; i++)
            {
                double x, y;
                axMap1.Measuring.get_PointXY(i, out x, out y);

                // add point to a Shape
            }
        }
    }

This is what it looks like while drawing. It’s intended for measuring, but can be used simply for drawing.

And here’s what it looks like when drawing (Measuring) an Area.

Hello,

This idea is very great.

But the newly entered points by default can not snapped to the vertices of exiting line shapes. Do you know how to capture the end point of a line when using a measuring tool?

Regards,
Henry.

Are you double-clicking to stop the measuring operation? When I double-click, I get the MeasuringStopped event, and my point count is correct.

Jerry.

Hello.
Thanks very much. I’m very sorry I didn’t make myself clear. What I want is to be able to capture the vertices of existing lines when adding line shapes. For example, I want to extend it manually on the basis of existing lines. Or, I want to connect the two broken polylines manually. And these operations need to capture the vertices of the existing polyline. Could you please help me to see what this should do?

Regards,
Henry.

For example, when I draw the diagonal line on the right, I want to capture the vertices of line A and line B. That is, vertice 1 and vertice 2.

Hello.

Sorry I misunderstood. Yes, you can hold down the Shift key while moving the mouse, and the cursor should ‘snap’ to points and endpoints. And you can still double-click while snapping to the second vertex.

Addendum: You can snap to the first vertex, even though you may not get the visual indication that the point is going to snap.

Hello.

Press and hold shift can solve the problem. But such operation is a little troublesome. Because the user is not very professional. Can you provide another method to automatically snap to vertices without pressing shift?

I tried to use the ShapeEditor class. There are three snacks. (1) I can snap the vertices of the line without pressing the shift key. (2) When editing the shapefile, no rubber band line is drawn between the last vertex and the current position of the mouse. (3) I don’t know how to stop editing.

In addition, if you know how to directly snap vertices using the “measurement tool” (no need to hold down the shift key, and the vertices can be automatically displayed), I also hope you can tell me the solution. Thanks.

Attach my code.

    private void btnDrawLine_Click(object sender, EventArgs e)
    {
        
        Shapefile sf = axMap1.get_Shapefile(m_layerHandle);
        sf.InteractiveEditing = true;
        axMap1.CursorMode = tkCursorMode.cmAddShape;
        axMap1.ChooseLayer += AxMapChooseLayer;

        int index = sf.NumShapes;
        sf.EditInsertShape(m_shp, ref index);
    }

    private void AxMapChooseLayer(object sender, _DMapEvents_ChooseLayerEvent e)
    {
        MapWinGIS.Point pt = new MapWinGIS.Point();
        pt.x = e.xProj;
        pt.y = e.yProj;

        int index = m_shp.numPoints;
        m_shp.InsertPoint(pt, ref index);
        axMap1.Redraw();
    }

Hello. Can you help me see this little problem again? Thank you.

Hello Henry.

As far as your questions:

  1. Can you provide another method to automatically snap to vertices without pressing shift?
  2. and to have the vertices be automatically displayed?

Not without editing the OCX. The behavior of the “measuring” tool is what it is built-in to the OCX.

Similarly with the behavior of the “edit” tools. There are ways to change the behavior of the tools if you want to look into the source code and modify the current behavior.

Regarding your code sample:
I’m sorry but I do not understand what you are trying to do here. It looks like you want to add a point shape based on the mouse position, but this is not the best way to do that.

  1. You should consider using the MouseDown event rather than the ChooseLayer event.
  2. It is not a good idea to add the map event handler on every button click. That should be done as a one-time setup, for example, in the Form_Load event.

Does that help?

Hello again.

Ok. After my previous comments, I thought more about what you’re doing. And I did not provide accurate information.

Using the AddShape tool, you DO want to trap the ChooseLayer event (as you are doing), but that is not for the mouse point, but to specify to the Editor which layer you want to Add a shape. You return the Layer Handle in the ChooseLayerEvent parameter. The tool, I think, will then capture your mouse point. To stop editing? The help file says “to finish the creation of shape Ctrl + left mouse click is used”

Hello.

Thank you.

Do you mean: If I use ShapeEditor, I need to use two events. The ChooseLayer event is used to get the layer handle of the shape I want to add, and the MouseDown event is used to record the position of the mouse click.

“That should be done as a one-time setup, for example, in the Form_Load event.”

Very nice. Thank you.

Hello Henry.

I have not used the ShapeEditor very much. The customers I code for have had specific editing requirements, and I have wrapped those around the Shapefile editing API.

I just started to play with the cmAddShape option of the ShapeEditor. You must set the InteractiveEditing flag on the layer, and specify the layer handle in the ChooseLayer event, as we have discussed.

You do not need the MouseDown event, since the tool handles your mouse clicks, for instance, to draw a polyline shape. I was able to see the line being drawn, and was able to snap to other lines or vertices. I then used the Ctrl-click on the last point, and I was able to see my final polyline geometry. Unfortunately, at that point, I wasn’t sure how to save the edits.

I need to spend more time with it, but what I think you want to do is to set up a handler for the AfterShapeEdit event. Within that event, you can prompt the user whether or not to save the shape. And if so, you can hopefully use the Shapefile.StopEditingShapes command to save the changes.

Hope that helps.
Jerry.

Hello.

You are very caring and responsible. Thank you for your help.

I am now trying to use the measure tool to extract coordinate points. At the same time, this method gives me some inspiration. When I wanted to add a rectangle, I chose to use the SelectBoxFinal method instead of the measure tool.

Best wishes,
Henry.