//** 功能描述: 把EnumIDs中的要素插入到目标要素类中 //** 参数列表: pDesFeatCls (IFeatureClass) // pEnumIDs (IEnumIDs) // pOriFeatCls "原要素类(把该要素类中的要素插入到目标要素类中) public static long InsertFeatIntoFeatClsByEnumIDs(ref IFeatureClass pDesFeatCls, ref IFeatureClass pOriFeatCls, ref IEnumIDs pEnumIDs, bool bIsCut, IGeometry pDomainGeometry, ref ISpatialReference pSpatialReference, ref Dictionary <string, string> pDicField, ref ProgressBar vProgressBar) { long functionReturnValue = 0; IFeature pFeat = default(IFeature); //要素类,用于循环 IFeatureCursor pDesFeatCursor = default(IFeatureCursor); //用新建要素类的游标,使用该游标向新要素类中插入要素,并写入数据库 IFeatureBuffer pDesFeatBuffer = default(IFeatureBuffer); //新建要素类创建的要素缓冲区, int iFeatCount = 0; IGeometry pGeometry = default(IGeometry); long lID = 0; IGeometry pDesGeometry = default(IGeometry); esriGeometryDimension EnumGeometryDimension = default(esriGeometryDimension); if (pDomainGeometry == null) { bIsCut = false; } if (pDesFeatCls.ShapeType == esriGeometryType.esriGeometryPolyline) { EnumGeometryDimension = esriGeometryDimension.esriGeometry1Dimension; } else if (pDesFeatCls.ShapeType == esriGeometryType.esriGeometryPolygon) { EnumGeometryDimension = esriGeometryDimension.esriGeometry2Dimension; } else { bIsCut = false; } pEnumIDs.Reset(); lID = pEnumIDs.Next(); if (lID != -1) { //从新建要素类中获得新游标,用于插入新的要素 pDesFeatCursor = pDesFeatCls.Insert(true); //创建要素缓冲区,和insert cursor 配合,创建新要素 pDesFeatBuffer = pDesFeatCls.CreateFeatureBuffer(); } else { return(functionReturnValue); } if (lID > -1) { while (lID > -1) { pFeat = pOriFeatCls.GetFeature((int)lID); pGeometry = pFeat.ShapeCopy; if ((pGeometry != null)) { if (pGeometry.IsEmpty == false) { //进行剪切或不剪切 if (bIsCut) { pDesGeometry = ClsGeometryOperator.CutFeatByGeometry(ref pGeometry, ref pDomainGeometry, ref EnumGeometryDimension); } else { pDesGeometry = pGeometry; } InsertOneFeatIntoCursor(ref pFeat, pDesGeometry, ref pDesFeatCursor, ref pDesFeatBuffer, pSpatialReference, ref pDicField); } } lID = pEnumIDs.Next(); //每一千个要素就把缓冲区内的要素类写入数据库 if (iFeatCount >= 1000) { pDesFeatCursor.Flush(); System.Windows.Forms.Application.DoEvents(); iFeatCount = 0; } if ((vProgressBar != null)) { vProgressBar.Value = vProgressBar.Value + 1; } } if (iFeatCount % 1000 != 0) { pDesFeatCursor.Flush(); } } if (vProgressBar != null) { vProgressBar.Value = 0; vProgressBar.Visible = false; } pDesFeatCursor = null; pDesFeatBuffer = null; return(functionReturnValue); }
//** 功能描述: 通过FeatureCursor向目标要素类中插入要素 //** 参数列表: pDesFeatCls (IFeatureClass) // pFeatCursor (IFeatureCursor) public static bool InsertFeatIntoFeatClsByCursor(ref IFeatureClass pDesFeatCls, ref IFeatureCursor pFeatCursor, bool bIsCut, IGeometry pDomainGeometry, ref ISpatialReference pSpatialReference, Dictionary <string, string> pDicField, ref ProgressBarX vProgressBar) { bool functionReturnValue = false; //要素类,用于循环 IFeature pFeat = default(IFeature); //用新建要素类的游标,使用该游标向新要素类中插入要素,并写入数据库 IFeatureCursor pDesFeatCursor = default(IFeatureCursor); //新建要素类创建的要素缓冲区 IFeatureBuffer pDesFeatBuffer = default(IFeatureBuffer); int iFeatCount = 0; IGeometry pGeometry = default(IGeometry); esriGeometryDimension EnumGeometryDimension = default(esriGeometryDimension); IGeometry pDesGeometry = default(IGeometry); bool bTransferRight = true; //记录InsertOneFeatIntoCursor的返回值 //On Error Resume Next if (pDomainGeometry == null) { bIsCut = false; } if (pDesFeatCls.ShapeType == esriGeometryType.esriGeometryPolyline) { EnumGeometryDimension = esriGeometryDimension.esriGeometry1Dimension; } else if (pDesFeatCls.ShapeType == esriGeometryType.esriGeometryPolygon) { EnumGeometryDimension = esriGeometryDimension.esriGeometry2Dimension; } else { bIsCut = false; } try { pFeat = pFeatCursor.NextFeature(); if (pFeat != null) { //从新建要素类中获得新游标,用于插入新的要素 pDesFeatCursor = pDesFeatCls.Insert(true); //创建要素缓冲区,和insert cursor 配合,创建新要素 pDesFeatBuffer = pDesFeatCls.CreateFeatureBuffer(); while (pFeat != null) { pGeometry = pFeat.ShapeCopy; if (pGeometry != null) { if (pGeometry.IsEmpty == false) { iFeatCount = iFeatCount + 1; //进行剪切或不剪切 if (bIsCut) { pDesGeometry = ClsGeometryOperator.CutFeatByGeometry(ref pGeometry, ref pDomainGeometry, ref EnumGeometryDimension); } else { pDesGeometry = pGeometry; } bTransferRight = InsertOneFeatIntoCursor(ref pFeat, pDesGeometry, ref pDesFeatCursor, ref pDesFeatBuffer, pSpatialReference, ref pDicField); //印骅 20081202 如果投影失败 则跳出函数 if (bTransferRight == false) { return(functionReturnValue); } //每一千个要素就把缓冲区内的要素类写入数据库 if (iFeatCount >= 1000) { pDesFeatCursor.Flush(); iFeatCount = 0; System.Windows.Forms.Application.DoEvents(); } if ((vProgressBar != null)) { vProgressBar.Value = vProgressBar.Value + 1; } } } pFeat = pFeatCursor.NextFeature(); } if (iFeatCount % 1000 != 0) { pDesFeatCursor.Flush(); } pDesFeatCursor = null; pDesFeatBuffer = null; } } catch (System.Runtime.InteropServices.COMException ex) { if (ex.ErrorCode == -2147216556) { functionReturnValue = false; //g_clsErrorHandle.DisplayInformation(string.Format("目标数据{0}正被其他程序使用,请检查", ((IDataset)pDesFeatCls).Name), false); MessageBoxEx.Show(string.Format("目标数据{0}正被其他程序使用,请检查", ((IDataset)pDesFeatCls).Name), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } functionReturnValue = true; return(functionReturnValue); //正常退出,返回true }