Insert/Delete Vertex programatically

4437
7
08-08-2011 03:36 AM
New Contributor II
I'd like to implement function to insert vertex on polyline just like in arcmap.
When mouse cursor is located on boundary snap on polyline, clicking right mouse button, select insert vertex on popup menu.

My code to insert vetex is something like this.

==============================================
Dim pSelected As IEnumFeature = m_Map.FeatureSelection
Dim pFeature As IFeature = pSelected.Next()
Dim pGeom As IGeometry = pFeature.Shape

......

Dim pGeomColn As IGeometryCollection = pGeom
Dim pPath As IPath = pGeomColn.Geometry(partIndex)
Dim pPoints As IPointCollection = pPath

Dim Missing As Object = Type.Missing
Dim val As Object
If vertexIndex = 0 Then
       val = 1
       pPoints.AddPoint(pHitPoint, val, Missing)
Else
        val = vertexIndex
        pPoints.AddPoint(pHitPoint, Missing, val)
End If

Dim pGeom As IGeometry = pPoints
pFeature.Shape = pGeom
pFeature.Store()
==========================================

At line pFeature.Shape = pGeom
Error occures like below.

"The feature object does not have a valid shaple."

Do I missed something else in code?
Any advice is OK.
Reply
0 Kudos
7 Replies
New Contributor III
try
Dim pGeom As IGeometry = pFeature.ShapeCopy // does cloning of the geometry

After you finish editing if you got error use
ITopologicaOperator2 IsknowSimple2 = false;
and call
IToplogicalOperator.Simplify();
Reply
0 Kudos
New Contributor II
Thanks for your reply.
I changed  pFeature.Shape to  pFeature.ShapeCopy, but error occures as same.

About ITopologicaOperator2, actually I cann't understand what you mean because of my short knowledge...
If you write the code in detail, it'll be very thankful.
Reply
0 Kudos
New Contributor III
No problem at all. I'm not native Visual Basic or .Net Basic but will try to answer in VB.

I'm not sure where did pHitPoint came from, is it maybe part of some other geometry. If this is the case you should Clone geometry. Cloning Geometry is important because of reusing same reference and there fore using object from someone else geometry and two parent objects are referencing one object but only one is really using it as valid geometry element.

How to clone?
       

public static Function Clone(IPoint point) as IPoint[INDENT]Dim IClone cloner
cloner = point As IClone;
Set Clone = cloner.Clone as IPoint;
[/INDENT]Function End
More of cloning from SDK
http://resources.esri.com/help/9.3/arcgisengine/dotnet/9915ff7d-f119-47ed-b381-cf7dded5cd93.htm


Dim ITopologicaOperator2 pTopOp
Set pTopOp = pGeom as ITopologicaOperator2
pTopOp.IsknowSimple2 = false
pTopOp.Simplify

pFeature.Shape = pTopOp as IGeometry
pFeature.Store
Reply
0 Kudos
New Contributor II
Thanks again for your kind explanation.
But I still have problem.
The pHitPoint is a boundary snapped point.

I called HitTest function to find out snapped point.

Dim pGeom As IGeometry = pFeature.Shape
Dim pHitTest As IHitTest = pGeom
Dim pHitPoint As IPoint = New Point
pHitTest.HitTest(pPoint, tolerance, esriGeometryHitPartType.esriGeometryPartBoundary, pHitPoint, hitDist, partIndex, vertexIndex, bTrue)

And at the code "Set pTopOp = pGeom as ITopologicaOperator2",
error occures because the type of pGeom is IPointCollection not IPoint, I think.
Do you have any idea for resolve this problem?
Reply
0 Kudos
Regular Contributor III
According to your original code, you are getting a point collection from a path object, updating that point collection, then trying to assign the path object as the feature's shape.  You can't do that.  A path is not a high level geometry and cannot be used to assign a feature's shape.  You need to assign the shape using the original geometry reference you set at the beginning of the code.
Reply
0 Kudos
New Contributor III
Neil is right.

Your solution is there.
IPolyline.SplitAtPoint
http://resources.esri.com/help/9.3/ArcGISDesktop/ArcObjects/esrigeometry/IPolycurve_SplitAtPoint.htm

There is some info and example at the bottom.
It is useful.

Just some hint.
Create small static functions that does one thing and reuse them. with time you have your pretty powerful class.
That way your code will evolve become more readable.
Reply
0 Kudos
New Contributor II
Thanks you, guys
I resolved this problem for your helps.

My code was changed something like below.

Dim pSelected As IEnumFeature = m_Map.FeatureSelection
Dim pFeature As IFeature = pSelected.Next()
Dim pGeom As IGeometry = pFeature.Shape

Dim pHitTest As IHitTest = pGeom
Dim pHitPoint As IPoint = New Point
pHitTest.HitTest(pPoint, tolerance, esriGeometryHitPartType.esriGeometryPartBoundary, pHitPoint, hitDist, partIndex, vertexIndex, bTrue)

......

Dim pPoints As IPointCollection = pGeom
Dim Missing As Object = Type.Missing
Dim val As Object

If vertexIndex = 0 Then
    val = 1
    pPoints.AddPoint(pHitPoint, val, Missing)
Else
    val = vertexIndex
    pPoints.AddPoint(pHitPoint, Missing, val)
End If

pFeature.Shape = CType(pPoints, IGeometry)
pFeature.Store()


Of course, as dubravko's comment, I created function in my own code.
Any Comment is OK.
Thanks.