Save a Geometry (draw using Rubberband) as a feature in Feature Class

775
6
04-20-2013 07:30 AM
Santosh_Pavan_KumarNukala
New Contributor III
Namaste, Hola, Hi

Using the Code below I have created a Line geometry using Rubberbanding

public void DrawPolyline(ESRI.ArcGIS.Carto.IActiveView activeView)
{
if(activeView == null)
{
return;
}
ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast
ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
rgbColor.Red = 255;

ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit Cast
ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
simpleLineSymbol.Color = color;

ESRI.ArcGIS.Display.ISymbol symbol = (ESRI.ArcGIS.Display.ISymbol)simpleLineSymbol; // Explicit Cast
ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberLineClass();
ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);
screenDisplay.SetSymbol(symbol);
screenDisplay.DrawPolyline(geometry);
screenDisplay.FinishDrawing();
}

After Drawing a Line geometry using Rubberband, I would like to save the "geometry" as a feature in a FeatureClass and create a Feature Layer and display the layer. I am facing trouble in creating feature class and saving a feature. Please help me with some sample Code .. I am badly waiting for some reply.

Thanks and Regards,
Pavan
0 Kudos
6 Replies
Santosh_Pavan_KumarNukala
New Contributor III
Below are the samples to create feature class and feature 

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//0049000000pz000000
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000049v000000



Thanks for the reply,
I have seen the above mentioned links earlier also and tried to create a new feature class and add a feature into it but was facing issues, Hence I asked for some sample working code if anyone already had. Currently I am getting "No Current Record." Exception when I call CreateFeatureClass method on IFeatureWorkSpace and am unable to resolve the issue. This issue has previously been addressed in the forum which is left Unanswered .. http://forums.esri.com/Thread.asp?c=159&f=1707&t=225079
Anyways I am pasting my code, Kindly help on this. I am Stuck up and badly in need of help 😞


public void DrawPolygon(ESRI.ArcGIS.Carto.IActiveView activeView)
{

if (activeView == null)
{
return;
}

ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

// Constant
screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast
ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
rgbColor.Red = 255;

ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit Cast
ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
simpleFillSymbol.Color = color;

ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast
ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberPolygonClass();
ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);
screenDisplay.SetSymbol(symbol);
screenDisplay.DrawPolygon(geometry);
screenDisplay.FinishDrawing();

IWorkspace pWorkSpace = CreateAccessWorkspace(@"J:\GIS\DGN\");
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)pWorkSpace;
// createFeatureDataset();
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)featureWorkspace;
workspaceEdit.StartEditing(false);
workspaceEdit.StartEditOperation();

IFields fields = new FieldsClass();
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

IField oidField = new FieldClass();
IFieldEdit oidFieldEdit = (IFieldEdit)oidField;
oidFieldEdit.Name_2 = "OID";
oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
fieldsEdit.AddField(oidField);

IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryAny;
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
ISpatialReference spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_17N);
ISpatialReferenceResolution spatialReferenceResolution = (ISpatialReferenceResolution)spatialReference;
spatialReferenceResolution.ConstructFromHorizon();
spatialReferenceResolution.SetDefaultXYResolution();
ISpatialReferenceTolerance spatialReferenceTolerance = (ISpatialReferenceTolerance)spatialReference;
spatialReferenceTolerance.SetDefaultXYTolerance();
geometryDefEdit.SpatialReference_2 = spatialReference;

IField geometryField = new FieldClass();
IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField;
geometryFieldEdit.Name_2 = "Shape";
geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
geometryFieldEdit.GeometryDef_2 = geometryDef;
fieldsEdit.AddField(geometryField);

IField nameField = new FieldClass();
IFieldEdit nameFieldEdit = (IFieldEdit)nameField;
nameFieldEdit.Name_2 = "Name";
nameFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
nameFieldEdit.Length_2 = 20;
fieldsEdit.AddField(nameField);

IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);


// "NO CURRENT RECORD" EXCEPTION HERE

IFeatureClass featureClass = featureWorkspace.CreateFeatureClass("FC007", validatedFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");


IFeatureClass pFeatureClass = featureClass;

featureWorkspace.OpenFeatureClass(pFeatureClass.AliasName);
IFeature feature = pFeatureClass.CreateFeature();
feature.Shape = geometry;
int contractorFieldIndex = pFeatureClass.FindField("Name");
feature.set_Value(contractorFieldIndex, "Krishna");
feature.Store();
workspaceEdit.StopEditOperation();
workspaceEdit.StopEditing(true);


IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureClass;
pFeatureLayer.Name = pFeatureClass.AliasName;

axMapControl1.AddLayer(pFeatureLayer as ILayer);
}

Thanks and Regards,
Pavan
0 Kudos
sameerpuppal
Occasional Contributor
Thanks for the reply, 
I have seen the above mentioned links earlier also and tried to create a new feature class and add a feature into it but was facing issues, Hence I asked for some sample working code if anyone already had. Currently I am getting "No Current Record." Exception when I call CreateFeatureClass method on IFeatureWorkSpace and am unable to resolve the issue. This issue has previously been addressed in the forum which is left Unanswered ..   http://forums.esri.com/Thread.asp?c=159&f=1707&t=225079
Anyways I am pasting my code, Kindly help on this. I am Stuck up and badly in need of help 😞 


public void DrawPolygon(ESRI.ArcGIS.Carto.IActiveView activeView) 


if (activeView == null) 

return; 


ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay; 

// Constant 
screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast 
ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass(); 
rgbColor.Red = 255; 

ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit Cast 
ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass(); 
simpleFillSymbol.Color = color; 

ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast 
ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberPolygonClass(); 
ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol); 
screenDisplay.SetSymbol(symbol); 
screenDisplay.DrawPolygon(geometry); 
screenDisplay.FinishDrawing(); 

IWorkspace pWorkSpace = CreateAccessWorkspace(@"J:\GIS\DGN\"); 
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)pWorkSpace; 
// createFeatureDataset(); 
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)featureWorkspace; 
workspaceEdit.StartEditing(false); 
workspaceEdit.StartEditOperation(); 

IFields fields = new FieldsClass(); 
IFieldsEdit fieldsEdit = (IFieldsEdit)fields; 

IField oidField = new FieldClass(); 
IFieldEdit oidFieldEdit = (IFieldEdit)oidField; 
oidFieldEdit.Name_2 = "OID"; 
oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; 
fieldsEdit.AddField(oidField); 

IGeometryDef geometryDef = new GeometryDefClass(); 
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; 
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryAny; 
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); 
ISpatialReference spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_17N); 
ISpatialReferenceResolution spatialReferenceResolution = (ISpatialReferenceResolution)spatialReference; 
spatialReferenceResolution.ConstructFromHorizon(); 
spatialReferenceResolution.SetDefaultXYResolution(); 
ISpatialReferenceTolerance spatialReferenceTolerance = (ISpatialReferenceTolerance)spatialReference; 
spatialReferenceTolerance.SetDefaultXYTolerance(); 
geometryDefEdit.SpatialReference_2 = spatialReference; 

IField geometryField = new FieldClass(); 
IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField; 
geometryFieldEdit.Name_2 = "Shape"; 
geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; 
geometryFieldEdit.GeometryDef_2 = geometryDef; 
fieldsEdit.AddField(geometryField); 

IField nameField = new FieldClass(); 
IFieldEdit nameFieldEdit = (IFieldEdit)nameField; 
nameFieldEdit.Name_2 = "Name"; 
nameFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; 
nameFieldEdit.Length_2 = 20; 
fieldsEdit.AddField(nameField); 

IFieldChecker fieldChecker = new FieldCheckerClass(); 
IEnumFieldError enumFieldError = null; 
IFields validatedFields = null; 
fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace; 
fieldChecker.Validate(fields, out enumFieldError, out validatedFields); 


   // "NO CURRENT RECORD" EXCEPTION HERE  

IFeatureClass featureClass = featureWorkspace.CreateFeatureClass("FC007", validatedFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); 


IFeatureClass pFeatureClass = featureClass; 
  
featureWorkspace.OpenFeatureClass(pFeatureClass.AliasName); 
IFeature feature = pFeatureClass.CreateFeature(); 
feature.Shape = geometry; 
int contractorFieldIndex = pFeatureClass.FindField("Name"); 
feature.set_Value(contractorFieldIndex, "Krishna"); 
feature.Store(); 
workspaceEdit.StopEditOperation(); 
workspaceEdit.StopEditing(true); 


IFeatureLayer pFeatureLayer = new FeatureLayerClass(); 
pFeatureLayer.FeatureClass = pFeatureClass; 
pFeatureLayer.Name = pFeatureClass.AliasName; 

axMapControl1.AddLayer(pFeatureLayer as ILayer); 


Thanks and Regards, 
Pavan



Hi Pavan,

Please check if start editing is creating issue. you dont need to start edit at the start, as you are doing it workspace is not able to create featureclass. if u check we cannot create fields or featureclass while editing is on in arcmap as there is lock. start editing, once featureclass has been created and you are just about to create feature.

About error many a times arcobjects gives misleading error message. try setting 'next statement' on the statement which is giving error (i.e. check error twice on the same line it will give different error) and F10 again. 1st time it will give error which you told and 2nd time it may say. "workspace is in use"

other work around is, save a empty featureclass with schema in installation path/bin and copy paste it on other location on the fly programatically and create feature which is rightly done in the code and add it on arcmap.

This will work.

Link which you shared is of engine. sometimes arcobjects give error which gives lead where we get seriously stuck up.

disclaimer: looking at the problem this is all i can guess!!! Happy hunting!


Regards,
Sameer
0 Kudos
ThavitinaiduGulivindala
Occasional Contributor
Replace the following line

geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryAny;

with

geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;

Also have a look at the following link
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/GeometryType_Property/00...
0 Kudos
sameerpuppal
Occasional Contributor
Replace the following line

geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryAny;

with

geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;

Also have a look at the following link
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/GeometryType_Property/00...


Ohh! i missed it!
0 Kudos
Santosh_Pavan_KumarNukala
New Contributor III
Replace the following line

geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryAny;

with

geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;

Also have a look at the following link
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/GeometryType_Property/00...


Thanks to all for the reply.. Yes the error is now gone, but I have a basic doubt. If I specify esriGeometryType to esriGeometryPolygon.. does that mean I cannot store Point and polyline feature in the same feature class. Do i need to create a separate feature classes for each geometry type. In order to create a FeatureLayer from a Feature Class, can I assign multiple featureclasses to same feature layer or is it one to one corresponce. Also let me know how can i change the symbology of a Feature and delete a feature interactively (i.e selecting a feature and deleting).
These might be basic questions but please do reply patiently.

Thanks and Regards,
Pavan
0 Kudos