This document is archived and information here might be outdated.  Recommended version.


Create Test Polyline from Geometry Environment Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Create Test Polyline from Geometry Environment Snippet

Create a sample polyline geometry using the GeometryBridge singleton object.

[C#]
///<summary>
///Create a sample polyline geometry using the GeometryBridge singleton object.
///</summary>
///<param name="spatialReference">An ESRI.ArcGIS.Geometry.ISpatialReference that will define where the newly created polyline object will exist in space.</param>
///<returns>An ESRI.ArcGIS.Geometry.IPolyline interface that is a sample polyline in the shape of a backwards C.</returns>
///<remarks></remarks>
public ESRI.ArcGIS.Geometry.IPolyline CreateTestPolylineFromGeometryEnvironment(ESRI.ArcGIS.Geometry.ISpatialReference spatialReference)
{

  if (spatialReference.Name == "Unknown")
  {
    System.Windows.Forms.MessageBox.Show("An Unknown SpatialReference was supplied.", System.Reflection.MethodBase.GetCurrentMethod().Name);
    return null;
  }

  //Create an array of WKSPoint structures starting in the middle of the x,y domain of the 
  //projected coordinate system.

  System.Double xMin=0;
  System.Double xMax=0;
  System.Double yMin=0;
  System.Double yMax=0;
  spatialReference.GetDomain(out xMin, out xMax, out yMin, out yMax);

  System.Double xFactor=(System.Math.Abs(xMax) / 2);
  System.Double yFactor=(System.Math.Abs(yMax) / 2);

  //Define what the shape looks like (a backwards C)
  ESRI.ArcGIS.esriSystem.WKSPoint[] wksPoint=new ESRI.ArcGIS.esriSystem.WKSPoint[4];

  wksPoint[0].X=xMin + xFactor;
  wksPoint[0].Y=yMin + yFactor;

  wksPoint[1].X=xMax - xFactor;
  wksPoint[1].Y=yMin + yFactor;

  wksPoint[2].X=xMax - xFactor;
  wksPoint[2].Y=yMax - yFactor;

  wksPoint[3].X=xMin + xFactor;
  wksPoint[3].Y=yMax - yFactor;

  ESRI.ArcGIS.Geometry.IPointCollection4 pointCollection4=new ESRI.ArcGIS.Geometry.PolylineClass();

  // The GeometryEnvironmentClass is a singleton object.
  ESRI.ArcGIS.Geometry.IGeometryBridge2 geometryBridge2=new ESRI.ArcGIS.Geometry.GeometryEnvironmentClass();
  geometryBridge2.AddWKSPoints(pointCollection4, ref wksPoint);

  ESRI.ArcGIS.Geometry.IPolyline polyline=pointCollection4 as ESRI.ArcGIS.Geometry.IPolyline;
  polyline.SpatialReference=spatialReference;

  return polyline;

}
[Visual Basic .NET]
'''<summary>
'''Create a sample polyline geometry using the GeometryBridge singleton object.
'''</summary>
'''<param name="spatialReference">An ESRI.ArcGIS.Geometry.ISpatialReference that will define where the newly created polyline object will exist in space.</param>
'''<returns>An ESRI.ArcGIS.Geometry.IPolyline interface that is a sample polyline in the shape of a backwards C.</returns>
'''<remarks></remarks>
Public Function CreateTestPolylineFromGeometryEnvironment(ByVal spatialReference As ESRI.ArcGIS.Geometry.ISpatialReference) As ESRI.ArcGIS.Geometry.IPolyline

  If spatialReference.Name="Unknown" Then
    System.Windows.Forms.MessageBox.Show("An Unknown SpatialReference was supplied.", System.Reflection.MethodBase.GetCurrentMethod.Name)
    Return Nothing
  End If

  'Create an array of WKSPoint structures starting in the middle of the x,y domain of the 
  'projected coordinate system.

  Dim xMin As System.Double
  Dim xMax As System.Double
  Dim yMin As System.Double
  Dim yMax As System.Double
  spatialReference.GetDomain(xMin, xMax, yMin, yMax)

  Dim xFactor As System.Double=(System.Math.Abs(xMax) / 2)
  Dim yFactor As System.Double=(System.Math.Abs(yMax) / 2)

  'Define what the shape looks like (a backwards C)
  Dim wksPoint As ESRI.ArcGIS.esriSystem.WKSPoint()=New ESRI.ArcGIS.esriSystem.WKSPoint(3) {}

  wksPoint(0).X=xMin + xFactor
  wksPoint(0).Y=yMin + yFactor

  wksPoint(1).X=xMax - xFactor
  wksPoint(1).Y=yMin + yFactor

  wksPoint(2).X=xMax - xFactor
  wksPoint(2).Y=yMax - yFactor

  wksPoint(3).X=xMin + xFactor
  wksPoint(3).Y=yMax - yFactor

  Dim pointCollection4 As ESRI.ArcGIS.Geometry.IPointCollection4=New ESRI.ArcGIS.Geometry.PolylineClass()

  ' The GeometryEnvironmentClass is a singleton object.
  Dim geometryBridge2 As ESRI.ArcGIS.Geometry.IGeometryBridge2=New ESRI.ArcGIS.Geometry.GeometryEnvironmentClass()
  geometryBridge2.AddWKSPoints(pointCollection4, wksPoint)

  Dim polyline As ESRI.ArcGIS.Geometry.IPolyline=TryCast(pointCollection4, ESRI.ArcGIS.Geometry.IPolyline)
  polyline.SpatialReference=spatialReference

  Return polyline

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.3DAnalyst
  • ESRI.ArcGIS.Animation
  • ESRI.ArcGIS.AnimationUI
  • ESRI.ArcGIS.ArcWeb
  • ESRI.ArcGIS.CatalogUI
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System
  • System
  • System.Windows.Forms