I need to programmatically add a point to the map, and then build a polygon, starting from this point. That is, to build a polygon began from the second point
I tried to execute the code below:
Point point = new Point {x = xEnd, y = yEnd};
Shape shape = new Shape();
shape.Create(ShpfileType.SHP_POLYGON);
shape.InsertPoint(point, 0);
Shapefile shapefile = new Shapefile();
shapefile.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON);
shapefile.EditAddShape(shape); // !!!
shapefile.InteractiveEditing = true;
axMap.AddLayer(shapefile, true);
axMap.ShapeEditor.StartEdit(1, 0);
I see my point on map, but after this instantly I get System.AccessViolationException error. What am I doing wrong?
Which version of MapWinGIS are you using?
I’m using the latest development release and have no problems with your code.
This is my test:
[TestMethod]
public void EditAddShapeTest()
{
var pnt = new Point { x = 6.7369281, y = 53.1603648 };
var shp = new Shape();
shp.Create(ShpfileType.SHP_POLYGON);
shp.InsertPoint(pnt, 0);
var sf = new Shapefile();
var result = sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON);
Assert.IsTrue(result, "CreateNewWithShapeID failed: " + sf.ErrorMsg[sf.LastErrorCode]);
var shpIndex = sf.EditAddShape(shp);
Assert.IsTrue(shpIndex > -1, "EditAddShape failed: " + sf.ErrorMsg[sf.LastErrorCode]);
sf.InteractiveEditing = true;
Assert.IsTrue(sf.InteractiveEditing, "InteractiveEditing failed: " + sf.ErrorMsg[sf.LastErrorCode]);
var layerIndex = _axMap1.AddLayer(sf, true);
Assert.IsTrue(layerIndex > -1, "AddLayer failed: " + sf.ErrorMsg[sf.LastErrorCode]);
result = _axMap1.ShapeEditor.StartEdit(layerIndex, shpIndex);
Assert.IsTrue(result, "ShapeEditor.StartEdit failed: " + sf.ErrorMsg[sf.LastErrorCode]);
}
I used the same syntax as you and added some checks of the return values.
I did change the call of StartEdit I’m using the return values from EditAddShape and AddLayer