bhavanisankarnimmala

Some of the Common Methods which will help in codification using arcobjects

Blog Post created by bhavanisankarnimmala on May 14, 2018

public static DataTable ReadAccessDataTable(string connString)

{

DataTable results = new DataTable();

using (OleDbConnection conn = new OleDbConnection(connString))

{

string sqlQuery = "SELECT * from Table order by Ref_Id";

OleDbCommand cmd = new OleDbCommand(sqlQuery, conn);

conn.Open();

OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

adapter.Fill(results);

}

return results;

}

public static DataTable ReadSQLDataTable(string connectionStr)

{

DataTable dt = new DataTable();

try

{

string connstr = connectionStr;

using (SqlConnection sqlConn = new SqlConnection(connstr))

{

string sqlQuery = "SELECT * from Table order by Ref_Id";

using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))

{

SqlDataAdapter ds = new SqlDataAdapter(cmd);

ds.Fill(dt);

}

}

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

}

finally

{

GC.Collect();

}

return dt;

}

public static IRgbColor GetRGBColor(int red, int green, int blue)

{

IRgbColor rGB = new RgbColor();

try

{

rGB.Red = red;

rGB.Green = green;

rGB.Blue = blue;

rGB.UseWindowsDithering = true;

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

}

finally

{

GC.Collect();

}

return rGB;

}

public static ISpatialReference GetSpatialReference()

{

ISpatialReference spatialRef = null;

try

{

IMxDocument mdoc = ArcMap.Application.Document as IMxDocument;

IMap map = mdoc.FocusMap;

Globals.pmap = map;

if (map.LayerCount > 0)

{

IActiveView act = mdoc.ActivatedView;

Globals.actView = act;

mdoc.UpdateContents();

ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass();

uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}"; // Example: IFeatureLayer

ESRI.ArcGIS.Carto.IEnumLayer enumLayer = map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true); // Explicit Cast

enumLayer.Reset();

ESRI.ArcGIS.Carto.ILayer layer = null;

while ((layer = enumLayer.Next()) != null)

{

IFeatureLayer flayer = layer as IFeatureLayer;

IFeatureClass fclass = flayer.FeatureClass;

IDataset dataset = fclass as IDataset;

ESRI.ArcGIS.Geodatabase.IGeoDataset geoDS = dataset as IGeoDataset;

spatialRef = geoDS.SpatialReference;

break;

}

}

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

}

finally

{

GC.Collect();

}

return spatialRef;

}

public static IWorkspace GetWorkspace()

{

IWorkspace workspace = null;

try

{

IMxDocument mdoc = ArcMap.Application.Document as IMxDocument;

IMap map = mdoc.FocusMap;

Globals.pmap = map;

if (map.LayerCount > 0)

{

IActiveView act = mdoc.ActivatedView;

Globals.actView = act;

mdoc.UpdateContents();

ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass();

uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}"; // Example: IFeatureLayer

ESRI.ArcGIS.Carto.IEnumLayer enumLayer = map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true); // Explicit Cast

enumLayer.Reset();

ESRI.ArcGIS.Carto.ILayer layer = null;

while ((layer = enumLayer.Next()) != null)

{

IFeatureLayer flayer = layer as IFeatureLayer;

IFeatureClass fclass = flayer.FeatureClass;

IDataset dataset = fclass as IDataset;

workspace = dataset.Workspace;

break;

}

}

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

}

finally

{

GC.Collect();

}

return workspace;

}

private static IFeatureClass GetFclassFromWS(string fclassName)

{

IFeatureClass fclass = null;

try

{

IWorkspace ws = GetWorkspace();

IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)ws;

fclass = featureWorkspace.OpenFeatureClass(fclassName);

}

catch

{

fclass = null;

}

finally

{

GC.Collect();

}

return fclass;

}

public static IFeatureClass GetFclassFromMap(string annoFClass)

{

try

{

IMxDocument mdoc = ArcMap.Application.Document as IMxDocument;

IMap map = mdoc.FocusMap;

Globals.pmap = map;

if (map.LayerCount > 0)

{

IActiveView act = mdoc.ActivatedView;

Globals.actView = act;

mdoc.UpdateContents();

ESRI.ArcGIS.esriSystem.IUID uid = new ESRI.ArcGIS.esriSystem.UIDClass();

uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}"; // Example: IFeatureLayer

ESRI.ArcGIS.Carto.IEnumLayer enumLayer = map.get_Layers(((ESRI.ArcGIS.esriSystem.UID)(uid)), true); // Explicit Cast

enumLayer.Reset();

ESRI.ArcGIS.Carto.ILayer layer = null;

while ((layer = enumLayer.Next()) != null)

{

IFeatureLayer flayer = layer as IFeatureLayer;

IFeatureClass fclass = flayer.FeatureClass;

IDataset dataset = fclass as IDataset;

string fclassName = dataset.BrowseName;

if (annoFClass.ToUpper() == fclassName.ToUpper())

{

return fclass;

}

}

}

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

}

finally

{

GC.Collect();

}

return null;

}

public static ITable GetITable(string tableName, IWorkspace workspace)

{

ITable sysTable = null;

try

{

IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;

sysTable = featureWorkspace.OpenTable(tableName);

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

}

finally

{

GC.Collect();

}

return sysTable;

}

public static bool ZoomTo(IGeometry geometry)

{

bool flag = false;

try

{

IMxDocument doc = (IMxDocument)ArcMap.Application.Document;

IMap map = (IMap)doc.FocusMap;

IActiveView pActiveView = (IActiveView)map;

IEnvelope pEnvelope;

if (geometry is IPoint)

{

IEnvelope currentEnv = pActiveView.Extent;

IPoint point = (IPoint)geometry;

currentEnv.CenterAt(point);

pActiveView.Extent = currentEnv;

map.MapScale = 100; //to set the scale to 1:100

}

else

{

pEnvelope = geometry.Envelope;

pEnvelope.Expand(1.2, 1.2, true);

pActiveView.Extent = pEnvelope;

}

pActiveView.Refresh();

flag = true;

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

flag = false;

}

finally

{

GC.Collect();

}

return flag;

}

public static IFeature GetFeatFromFeatureClass(string fieldName, string fieldValue, IFeatureClass fclass)

{

IFeature feat = null;

try

{

IQueryFilter fqfilt = new QueryFilter();

if (fieldName != "")

{

fqfilt.WhereClause = fieldName + " = '" + fieldValue + "'";

}

else

{

fqfilt.WhereClause = null;

}

IFeatureCursor fcur = fclass.Search(fqfilt, true);

int count = fclass.FeatureCount(fqfilt);

while ((feat = fcur.NextFeature()) != null)

{

break;

}

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

feat = null;

}

finally

{

GC.Collect();

}

return feat;

}

public static IPoint GetCentrePoint(IFeature secFeat)

{

IPoint pt = new Point();

try

{

IGeometry geo = secFeat.ShapeCopy;

IEnvelope env = geo.Envelope;

double x = env.XMin + (env.XMax - env.XMin) / 2;

double y = env.YMin + (env.YMax - env.YMin) / 2;

pt.X = x;

pt.Y = y;

}

catch (Exception ex)

{

Globals.logging(ex.Message, false, "FATAL");

Globals.logging(ex.StackTrace, false, "FATAL");

Globals.logging("ERROR in " + System.Reflection.MethodBase.GetCurrentMethod().Name + " Method.", false, "FATAL");

pt = null;

}

finally

{

GC.Collect();

}

return pt;

}

private static IPoint BuildPolygonAndGetCentrePoint(IPoint pt1, IPoint pt2, IPoint pt3, IPoint pt4)

{

IPoint centrePoint = new Point();

IPolygon poly = null;

try

{

IPointCollection pPntColl = new Polygon();

pPntColl.AddPoint(pt1);

pPntColl.AddPoint(pt2);

pPntColl.AddPoint(pt3);

pPntColl.AddPoint(pt4);

poly = pPntColl as IPolygon;

poly.Close();

IArea area = poly as IArea;

area.QueryCentroid(centrePoint);

}

catch

{

}

finally

{

GC.Collect();

}

return centrePoint;

}

Outcomes