Create Polygons Help

1583
6
03-04-2013 06:33 AM
Corbinde_Bruin
Occasional Contributor II
Hello all,

This seems like such a simple task. I've been unable to create polygons programmatically.
My basic goals are:

  • Draw a polygon with a visual representation as it's being drawn

  • Be able to pan/zoom with mouse center button is clicked or scrolled

  • Be able to programmatically add/edit/delete vertices


The ESRI Help doesn't seem to work I've tried multiple other ways of doing it:
I can successfully make polygons from Rubberband.TrackNew, but I have no control with this method. I need to be able to pan the screen in between plunking down vertices. Rubberband.TrackNew doesn't allow for any kind of clicking outside of creating new vertices or double clicking to finish the sketch. You can't even click a new tool in ArcMap. It just treats it as a new click. The Draw Polygon ArcGIS Snippet has the same behavior.
The ArcGIS Snippet Create Polygon from Points that uses a GeometryBridge and a PointCollection doesn't show any visible representation of the polygon as it's being drawn.
I haven't gotten as far as Add/Edit/Delete vertices yet. If you have a resource for these types of operations, it'd be helpful.

I'm lost in the woods. I must be missing something about how to interrupt the drawing/tracking functions to pan/zoom the map. I appreciate any help. Has anyone dealt with these kinds of operations before? Are there some API Objects I should take a second look at?

Thank you for your time,
Corbin de Bruin
0 Kudos
6 Replies
AlexanderGray
Occasional Contributor III
I have done it with lines and at 9.3 and migrated to 10.0.  I have done it with polygons at 8.3.  In 9.3, I created a new edit task.  In the task activate you can set the sketch geometry to polygon.  You then can use the sketch tools like any other polygon, pan, zoom, etc.  On finish sketch, you can do whatever you need to do.  In ArcGIS 10.0 and up, you should look at IShapeConstructorTool and ISketchTool.
You can similarly create a task to edit a polygon using the edit sketch ArcMap GUI goodness. 

Even if the end goal is not to store the polygon in a featureclass, the types of functionality you describe are achieved via the edit sketch.  The edit sketch is created or modified via sketchtools or edittasks. There are many topics associated with customizing the edits in the help.
0 Kudos
Corbinde_Bruin
Occasional Contributor II
Thank you Alexander,

I had yet to come across the ISketchTool interface. It looks like it's going to help greatly. I'm going to do some testing then, I'll get back to this post to mark as answer.

Thank you,
Corbin de Bruin
0 Kudos
Corbinde_Bruin
Occasional Contributor II
I'm having an issue trying to add a point to an EditSketch object. The IEditSketch3.AddPoint method is giving me an error I don't understand. It's seems to me I have my parameters right, but I'm hoping I'm wrong.

Dim addPoint as IPoint = pActiveview.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y)
pEditSketch.AddPoint(addPoint, True)


System.ArgumentException occurred
  Message=Value does not fall within the expected range.
  Source=ESRI.ArcGIS.Editor
  StackTrace:
       at ESRI.ArcGIS.Editor.IEditSketch3.AddPoint(IPoint point, Boolean allowUndo)
       at SanitarySewerAddin.ToolSanSheetMake.OnMouseDown(MouseEventArgs arg) in (...)ToolSanSheetMake.vb:line 110
  InnerException:

With minor syntax and casting changes I can change the exception detail, but it's still always an Argument Exception.
0 Kudos
MichaelVolz
Esteemed Contributor
Have you tried logged a support incident with ESRI concenring this issue?
0 Kudos
AlexanderGray
Occasional Contributor III
values not in range for add point often mean a mismatch of the geometry type or the coordinate system domain.  If the points coordinates are outside of the domain of the target geometry (editsketch) you would get this problem.  There is also the possibility of z and m values mismatch between geometries.
0 Kudos
Corbinde_Bruin
Occasional Contributor II
Hello again,

I've been messing around a bit with EditSketch and I've managed to successfully draw a polygon with it, but I still don't know how to get a graphical representation while I'm dropping down verticies. I want the rubber banding effect so I can see where my last vertices are and how my polygon is going to look, however, using the rubberpolygon class doesn't work because I can't pan the screen in between plunking polygon verticies.

I feel like there has to be another method out there that accomplishes my needs.
Public Class ToolEditSketch
    Inherits ESRI.ArcGIS.Desktop.AddIns.Tool

    Dim featurelayer As IFeatureLayer
    Dim featureClass As IFeatureClass
    Dim feature As IFeature

    Dim pGeometry As IGeometry5
    Dim pActiveview As IActiveView

    Dim projCoordSys As IProjectedCoordinateSystem5
    
    Dim pEditSketch As IEditSketch3
    Dim sketchOp As ISketchOperation2

    Public Sub New()

    End Sub


    Protected Overrides Sub OnActivate()

        'Define Spatial Reference and Coordinate System
        Dim spatialReferenceFactory As ISpatialReferenceFactory = New SpatialReferenceEnvironment
        projCoordSys = spatialReferenceFactory.CreateProjectedCoordinateSystem(102718) ' ESRI spatial reference code for Long Island Zone is 102718

        'Public Method Call to fill global applicationa and document variables
        InitializeApplicationVariables()

        'Public Method Call to get the target layer and featureclass
        featurelayer = GetLayer("Sewer Sheet Outlines")
        featureClass = featurelayer.FeatureClass

    End Sub


    Protected Overrides Sub OnMouseDown(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)
        'Get the Activeview from the map document
        pActiveview = pMxDoc.FocusMap

        'If statement to Start Edit Session if not already Editing

        'set the Edit Sketch properties
        pEditSketch = editor
        pEditSketch.GeometryType = esriGeometryType.esriGeometryPolygon
        pEditSketch.Geometry = New Polygon


        Dim point1 As IPoint = New ESRI.ArcGIS.Geometry.Point
        point1 = pActiveview.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y)

      pEditSketch.AddPoint(point1, False)

    End Sub


    Protected Overrides Sub OnDoubleClick()
        pGeometry = pEditSketch.Geometry
        feature = featureClass.CreateFeature()
        feature.Shape = pGeometry

        feature.Store()
        ModuleGdBFunctions.EndEditSession()
        pActiveview = pMxDoc.FocusMap
        pActiveview.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewAll, Nothing, Nothing)
       
    End Sub


    Protected Overrides Sub OnUpdate()

    End Sub


End Class
0 Kudos