I have used the GP Model builder to build a model that accepts 3 variables (Input Layer of type layer file, Clip Feature of tyoe feature class and Output dataset). This model clips the input layer by the clip feature and exports the result. The model works fine in ArcGIS Desktop.
In the meantime I am using arcobjects to consume my GP model from .NET and I am finding it a bit hard. I want to create the Clip feature in memory and pass it to the Model. When I pass a null the model works fine but when I pass a feature class an exception is being returned:ex = {"Error HRESULT E_FAIL has been returned from a call to a COM component."} If I omit the Clip Feature from the model, the model works fine
This is how I am creating the feature class in memory and passing it to the GP Model:
GP.AddToolbox(@"C:\Users\xxxx\AppData\Roaming\ESRI\ArcToolbox\My Toolboxes\Export.tbx");
IVariantArray parameters = new VarArrayClass();
parameters.Add("C:\\temp\\export.lyr");
parameters.Add("SHAPE, C:\\temp\\exportshape\\");
/*CREATE IN MEMORY WORKSPACE*/
IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.InMemoryWorkspaceFactory();
IWorkspaceName workspaceName = workspaceFactory.Create("", "MyWorkspace", null, 0); // Create a new InMemory geodatabase.
IName name = (IName)workspaceName; // Cast for IName.
IWorkspace inmemWor = (IWorkspace)name.Open();; // Open a reference to the access workspace through the name object.
IFeatureWorkspace featWork = (IFeatureWorkspace)inmemWor;
// Create the fields collection and fields for the feature class.
IFieldsEdit pFldsEdt = new FieldsClass();
IFieldEdit pFldEdt = new FieldClass();
ISpatialReferenceFactory pSpatialRefFactory = new SpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem pGeographicCoordSys = pSpatialRefFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
ISpatialReference pSpaRef = pGeographicCoordSys;
pSpaRef.SetDomain(-180, 180, -90, 90);
pFldEdt = new FieldClass();
pFldEdt.Type_2 = esriFieldType.esriFieldTypeOID;
pFldEdt.Name_2 = "OBJECTID";
pFldEdt.AliasName_2 = "OBJECTID";
pFldsEdt.AddField(pFldEdt);
IGeometryDefEdit pGeoDef;
pGeoDef = new GeometryDefClass();
pGeoDef.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
pGeoDef.SpatialReference_2 = pSpaRef;
pGeoDef.GridCount_2 = 1;
pGeoDef.set_GridSize(0, 1000);
pGeoDef.AvgNumPoints_2 = 0;
pGeoDef.HasM_2 = false;
pGeoDef.HasZ_2 = false;
pFldEdt = new FieldClass();
pFldEdt.Name_2 = "SHAPE";
pFldEdt.AliasName_2 = "SHAPE";
pFldEdt.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFldEdt.GeometryDef_2 = pGeoDef;
pFldsEdt.AddField(pFldEdt);
/*CREATE FEATURE CLASS*/
IFeatureClass pFClass = featWork.CreateFeatureClass("Clip FC", pFldsEdt, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
/*CREATE FEATURE */
IFeature Ftr = pFClass.CreateFeature();
ESRI.ArcGIS.Geometry.IPolygon polygon = new ESRI.ArcGIS.Geometry.PolygonClass();
IPointCollection pointCollection;
pointCollection = polygon as IPointCollection;
object Missing = Type.Missing;
IPoint point = new PointClass();
point.PutCoords(14.291, 35.913);
pointCollection.AddPoint(point, ref Missing, ref Missing);
point.PutCoords(14.545, 35.913);
pointCollection.AddPoint(point, ref Missing, ref Missing);
point.PutCoords(14.545, 35.719);
pointCollection.AddPoint(point, ref Missing, ref Missing);
point.PutCoords(14.291, 35.719);
pointCollection.AddPoint(point, ref Missing, ref Missing);
polygon.Close();
Ftr.Shape = polygon;
Ftr.Store();
parameters.Add(pFClass );
GP.OverwriteOutput = true;
GP.Execute("ClipandExport", parameters, null);