Modify a polyline
arcgissamples\geometry\ModifyPolyline.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.geometry;

import java.io.*;

import com.esri.arcgis.datasourcesfile.*;
import com.esri.arcgis.geodatabase.*;
import com.esri.arcgis.geometry.*;
import com.esri.arcgis.system.*;

/**
 * The code in this class demonstrates how to modify the coordinates of a Polyline's geometry.
 */
public class ModifyPolyline
{
  public ModifyPolyline()
  {
    
  }
  
  public static void main(String[] args)
  {
    System.out.println("Starting ModifyPolyline - An ArcObjects Developer Sample");
    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);
      
      if (args.length != 1)
      {
        System.out.println("Usage: ModifyPolyline [Source ShapeFile]");
        System.exit(0);
      }  
      
      String inShape = args[0];

      String srcPath = inShape.substring(0, inShape.lastIndexOf("/"));
      String srcName = inShape.substring(inShape.lastIndexOf("/") + 1);

      // Open a shapefile to get a polygon...
      ShapefileWorkspaceFactory wsFactory = new ShapefileWorkspaceFactory();

      IWorkspace ws = wsFactory.openFromFile(srcPath, 0);
      IFeatureWorkspace fws = (IFeatureWorkspace) ws;

      FeatureClass fc = new FeatureClass(fws.openFeatureClass(srcName));

      IFeature feature = fc.getFeature(1);

      // If the geometry type is not of type Polygon, exit...
      if (feature.getShape().getGeometryType() != esriGeometryType.esriGeometryPolyline)
      {
        System.out.println("The input shapefile must be of type Polyline.");
        aoInit.shutdown();
        System.exit(0);
      }

      Polyline poly = (Polyline) feature.getShape();

      ModifyPolyline modify = new ModifyPolyline();
      modify.modifyOneSegmentOfAPolyline(poly);
      modify.modifyOneVertexOfAPolyline(poly);
      
      aoInit.shutdown();

      System.out.println("Done");
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
      System.exit(0);
    }
  }
      
  /**
   * Modifies one vertex of a polyline
   * @param pline
   * @throws Exception
   */
  private void modifyOneVertexOfAPolyline(Polyline pline) throws Exception
  {
    int[] lPart = new int[1];
    int[] lVertex = new int[1];
    boolean bright[] = new boolean[1];
    double[] dht = new double[1];

    // Here, getFromPoint returns an IPoint. You could alternatively
    // create a Point instance by passing "pline.getFromPoint()" into
    // the contructor of Point.
    IPoint pqpt = pline.getFromPoint();
    int dr = 1;

    // Create the output point
    Point pHit = new Point();
    pline.hitTest(pqpt, dr, esriGeometryHitPartType.esriGeometryPartVertex, pHit, dht, lPart, lVertex, bright);
    // Get the path where the point is
    Path pPathColl = (Path) pline.getGeometry(lPart[0]);

    // Get the vertex that got hit
    Point pTrans2D = (Point) pPathColl.getPoint(lVertex[0]);

    // Move the point
    pTrans2D.move(-10, 0);

    // Update the point via IPointCollection
    pPathColl.updatePoint(lVertex[0], pTrans2D);
    // Let the polyline know it's geometry has changed
    pline.geometriesChanged();
  }

  /**
   * Modifies one segment of a polyline
   * @param pline
   * @throws Exception
   */
  private void modifyOneSegmentOfAPolyline(Polyline pline) throws Exception
  {
    int[] lPart = new int[1];
    int[] lSeg = new int[1];
    boolean bright[] = new boolean[1];
    double[] dht = new double[1];

    // This could be a user input point (Ex: Mouse click etc.)
    IPoint pqpt = pline.getFromPoint();
    int dr = 1;
    Point pHit = new Point();
    pline.hitTest(pqpt, dr, esriGeometryHitPartType.esriGeometryPartBoundary, pHit, dht, lPart, lSeg, bright);
    // Get the path where the point is
    Path pPathColl = (Path) pline.getGeometry(lPart[0]);
    // Get the segment that got hit
    Line pTrans2D = (Line) pPathColl.getSegment(lSeg[0]);

    pTrans2D.move(-10, 0);

    // Let the Polyline know that it's geometry has changed
    pline.geometriesChanged();
  }

  /**
   * Initializes the lowest available ArcGIS License
   */
  private static void initializeArcGISLicenses(AoInitialize aoInit)
  {
    try
    {
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      }
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
      }
      else
      {
        System.err.println("Could not initialize an Engine or ArcView license. Exiting application.");
        System.exit(-1);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}