private bool CreateLCRecordOfFDS(IDataSource ds, String fdsname) { ITable table = null; IFdeCursor cursor = null; try { String lcguid = System.Guid.NewGuid().ToString(); byte[] lcbuf = CreateLogicTree("LogicTree", lcguid); if (!GetQueryResults(String.Format("groupid='{0}'", lcguid), "cm_logictree", ds)) { table = ds.OpenTable("cm_logictree"); cursor = table.Insert(); RowBufferFactory rbf = new RowBufferFactory(); IRowBuffer rb = rbf.CreateRowBuffer(table.GetFields()); rb.SetValue(1, lcguid); //groupid rb.SetValue(2, fdsname); //name rb.SetValue(3, fdsname); //founder IBinaryBuffer bb = new BinaryBuffer(); bb.FromByteArray(lcbuf); rb.SetValue(4, bb); //content cursor.InsertRow(rb); } table = ds.OpenTable("cm_group"); cursor = table.Insert(); RowBufferFactory rbf1 = new RowBufferFactory(); IRowBuffer rb1 = rbf1.CreateRowBuffer(table.GetFields()); rb1.SetValue(1, lcguid); //groupuid rb1.SetValue(2, fdsname); //DataSet cursor.InsertRow(rb1); return(true); } catch (COMException comEx) { System.Diagnostics.Trace.WriteLine(comEx.Message); return(false); } catch (System.Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); return(false); } finally { if (cursor != null) { //Marshal.ReleaseComObject(cursor); cursor = null; } if (table != null) { //Marshal.ReleaseComObject(table); table = null; } } }
public void InsertFeatures(IObjectClass oc, IRowBufferCollection rows) { if (oc == null || rows == null || rows.Count == 0) { return; } IFdeCursor cursor = null; try { oc.FeatureDataSet.DataSource.StartEditing(); cursor = oc.Insert(); for (int i = 0; i < rows.Count; ++i) { IRowBuffer row = rows.Get(i); cursor.InsertRow(row); int oid = cursor.LastInsertId; row.SetValue(0, oid); } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { if (cursor != null) { //Marshal.ReleaseComObject(cursor); oc.FeatureDataSet.DataSource.StopEditing(true); } //Marshal.ReleaseComObject(cursor); } }
public bool InsertRow(IDataSource ds, string tname, object[] values) { ITable table = ds.OpenTable(tname); IFdeCursor fdeCursor = table.Insert(); RowBufferFactory rowBufferFactory = new RowBufferFactoryClass(); IRowBuffer rowBuffer = rowBufferFactory.CreateRowBuffer(table.GetFields()); for (int i = 1; i <= values.Length; i++) { rowBuffer.SetValue(i, values[i]); } fdeCursor.InsertRow(rowBuffer); return(true); }
private bool InsertFacStyleClass(FacStyleClass style) { IFdeCursor cursor = null; IRowBuffer row = null; try { IFeatureDataSet fds = this._ds.OpenFeatureDataset("DataSet_BIZ"); if (fds == null) { return(false); } IObjectClass oc = fds.OpenObjectClass("OC_FacilityStyle"); if (oc == null) { return(false); } row = oc.CreateRowBuffer(); cursor = oc.Insert(); row.SetValue(row.FieldIndex("Name"), style.Name); row.SetValue(row.FieldIndex("FacClassCode"), style.FacClassCode); row.SetValue(row.FieldIndex("ObjectId"), style.ObjectId); row.SetValue(row.FieldIndex("StyleType"), style.Type.ToString()); row.SetValue(row.FieldIndex("StyleInfo"), style.ObjectToJson()); if (style.Thumbnail != null) { try { IBinaryBuffer bb = new BinaryBufferClass(); MemoryStream stream = new MemoryStream(); style.Thumbnail.Save(stream, ImageFormat.Png); bb.FromByteArray(stream.ToArray()); row.SetValue(row.FieldIndex("Thumbnail"), bb); } catch (Exception exception) { } } cursor.InsertRow(row); style.Id = cursor.LastInsertId; return(true); } catch (Exception ex) { return(false); } finally { if (cursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); cursor = null; } if (row != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(row); row = null; } } }
private bool CreateFacClassReg(FacClass fc) { if (fc == null || fc.FacilityType == null) { return(false); } IFdeCursor cursor = null; IRowBuffer row = null; try { IFeatureDataSet fds = this._dsPipe.OpenFeatureDataset("DataSet_BIZ"); if (fds == null) { return(false); } IFeatureDataSet fdsActuality = this._dsPipe.OpenFeatureDataset("DataSet_GEO_Actuality"); if (fdsActuality == null) { return(false); } IObjectClass oc = fds.OpenObjectClass("OC_FacilityClass"); if (oc == null) { return(false); } row = oc.CreateRowBuffer(); cursor = oc.Insert(); if (row != null) { row.SetValue(row.FieldIndex("FacClassCode"), fc.Code); row.SetValue(row.FieldIndex("Name"), fc.Name); row.SetValue(row.FieldIndex("FacilityType"), fc.FacilityType.Name); row.SetValue(row.FieldIndex("LocationType"), fc.LocationType.ToString()); row.SetValue(row.FieldIndex("TurnerStyle"), fc.TurnerStyle.ToString()); row.SetValue(row.FieldIndex("Comment"), fc.Comment); string fcName = string.Format("FC_{0}_{1}", (int)DataLifeCyle.Actuality, fc.Code); List <CMFieldConfig> fieldConfig = GetFieldsConfig(fc.Code); IFieldInfoCollection fielInfoCol = CreateFieldInfoCollection(fieldConfig, fc.FacilityType.Name); IFeatureClass featureClass = fdsActuality.CreateFeatureClass(fcName, fielInfoCol); if (featureClass == null) { return(false); } featureClass.AliasName = fc.Name; featureClass.LockType = gviLockType.gviLockExclusiveSchema; IGridIndexInfo indexInfo = new GridIndexInfoClass { L1 = 500.0, L2 = 2000.0, L3 = 10000.0, GeoColumnName = "Geometry" }; featureClass.AddSpatialIndex(indexInfo); indexInfo.GeoColumnName = "Shape"; featureClass.AddSpatialIndex(indexInfo); indexInfo.GeoColumnName = "FootPrint"; featureClass.AddSpatialIndex(indexInfo); IRenderIndexInfo info2 = new RenderIndexInfoClass { L1 = 500.0, GeoColumnName = "Geometry" }; featureClass.AddRenderIndex(info2); info2.GeoColumnName = "Shape"; featureClass.AddRenderIndex(info2); info2.GeoColumnName = "FootPrint"; featureClass.AddRenderIndex(info2); featureClass.LockType = gviLockType.gviLockSharedSchema; row.SetValue(row.FieldIndex("FeatureClassId"), featureClass.Guid.ToString()); row.SetValue(row.FieldIndex("DataSetName"), "DataSet_GEO_Actuality"); row.SetValue(row.FieldIndex("FcName"), featureClass.Name); row.SetValue(row.FieldIndex("DataType"), DataLifeCyle.Actuality.ToString()); cursor.InsertRow(row); return(true); } else { return(false); } } catch (Exception ex) { WaitForm.SetCaption("创建管线库中的设施要素类【" + fc.Name + "】失败!"); return(false); } finally { if (cursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); cursor = null; } if (row != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(row); row = null; } } }
private bool InsertColor() { IFdeCursor cursor = null; IRowBuffer row = null; try { IFeatureDataSet fds = this._ds.OpenFeatureDataset("DataSet_BIZ"); if (fds == null) { return(false); } IObjectClass oc = fds.OpenObjectClass("OC_ColorInfo"); if (oc == null) { return(false); } row = oc.CreateRowBuffer(); cursor = oc.Insert(); row.SetValue(row.FieldIndex("Name"), this._color.Name); row.SetValue(row.FieldIndex("ObjectId"), this._color.ObjectId); row.SetValue(row.FieldIndex("GroupId"), this._color.Group); row.SetValue(row.FieldIndex("Code"), this._color.Code); row.SetValue(row.FieldIndex("Type"), this._color.Type); if (this._color.Thumbnail != null) { try { IBinaryBuffer bb = new BinaryBufferClass(); MemoryStream stream = new MemoryStream(); this._color.Thumbnail.Save(stream, ImageFormat.Png); bb.FromByteArray(stream.ToArray()); row.SetValue(row.FieldIndex("Thumbnail"), bb); } catch (Exception exception) { } } row.SetValue(row.FieldIndex("Comment"), this._color.Comment); cursor.InsertRow(row); this._color.Id = cursor.LastInsertId; return(true); } catch (Exception ex) { return(false); } finally { if (cursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); cursor = null; } if (row != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(row); row = null; } } }
static void Main(string[] args) { //创建要素类要先定义属性字段 IFieldInfoCollection fields = new FieldInfoCollection(); { //名称列 IFieldInfo field = new FieldInfo(); field.Name = "Name"; field.FieldType = gviFieldType.gviFieldString; field.Length = 255; fields.Add(field); //空间列 field = new FieldInfo(); field.Name = "Geometry"; field.FieldType = gviFieldType.gviFieldGeometry; //列类型为空间类型 field.GeometryDef = new GeometryDef(); //需要实例化一个几何定义对象,里面有大量与空间有关的内容,比如: field.GeometryDef.HasZ = true; //是否在Z值,默认为false,只能存XY field.GeometryDef.GeometryColumnType = gviGeometryColumnType.gviGeometryColumnPoint; //空间列的几何类型 field.RegisteredRenderIndex = true; //注册渲染索引,允许在renderControl中显示 fields.Add(field); //多空间列 field = new FieldInfo(); field.Name = "Model"; field.FieldType = gviFieldType.gviFieldGeometry; //列类型为空间类型 field.GeometryDef = new GeometryDef(); //需要实例化一个几何定义对象,里面有大量与空间有关的内容,比如: field.GeometryDef.HasZ = true; //是否在Z值,默认为false,只能存XY field.GeometryDef.GeometryColumnType = gviGeometryColumnType.gviGeometryColumnModelPoint; //空间列的几何类型 field.RegisteredRenderIndex = true; //注册渲染索引,允许在renderControl中显示 fields.Add(field); //... 其它列 } //创建要素类 IFeatureClass FC = FDS.CreateFeatureClass("FC", fields); int nameID = FC.GetFields().IndexOf("Name"); //获取对应列的索引, int geomID = FC.GetFields().IndexOf("Geometry"); int fidIndex = FC.GetFields().IndexOf(FC.FidFieldName); //增加单条数据,并同步到FeatureLayer IFdeCursor fcu = FC.Insert(); //通过FC创建一个插入游标,用来操作rowBuffer IRowBuffer rb = FC.CreateRowBuffer(); //通过FC创建一条空要素实例,用来设置数据,设置完成后将其塞入FC IPoint p = new GeometryFactory().CreatePoint(gviVertexAttribute.gviVertexAttributeZ); // 将要塞入空间列的几何对象,类型要对应 FC的field.GeometryDef.GeometryColumnType ,否则塞不进 rb.SetValue(nameID, "testPointA"); //塞名称 rb.SetValue(geomID, p); //塞几何 fcu.InsertRow(rb); //塞进FC int fid = fcu.LastInsertId; //成功后反回这条buffer的主键ID rb.SetValue(fidIndex, fid); //Cursor(FC)增加数据后会产生一个对应的FID,要把这个FID赋值给row才能在FLayer中显示 ax.FeatureManager.CreateFeature(FC, rb); //同步到renderControl //增加很大量数据时,参考以下方法 IFdeCursor cu = FC.Insert(); IRowBufferCollection rows = new RowBufferCollection(); for (int i = 0; i < 100000; i++) { IRowBuffer row = FC.CreateRowBuffer(); // do something ... row.SetValue(geomID, p); cu.InsertRow(row); row.SetValue(fidIndex, cu.LastInsertId); rows.Add(row); } ax.FeatureManager.CreateFeatures(FC, rows); //或直接整体更新 ax.FeatureManager.RefreshFeatureClass(FC); //查 string tagStr = "tagName"; IQueryFilter qf = new QueryFilter(); //过滤器 qf.WhereClause = $"Name = '{tagStr}'"; IFdeCursor res = FC.Search(qf, true); //条件查 IRowBuffer aBuffer = res.NextRow(); //遍历查询结果 //改 IFdeCursor upCu = FC.Update(qf); IRowBuffer upBuffer = upCu.NextRow(); upCu.UpdateRow(upBuffer); //删 FC.Delete(qf); FC.DeleteRow(0); FC.Truncate(); //保留表结构,清空表 }
/// <summary> /// 在要素类里创建要素 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void toolStripButtonInsertFeature_Click(object sender, EventArgs e) { if (dsFactory == null) { dsFactory = new DataSourceFactory(); } IDataSource ds = null; IFeatureDataSet dataset = null; IFeatureClass fc = null; IFdeCursor cursor = null; try { ds = dsFactory.OpenDataSource(Info.ci); dataset = ds.OpenFeatureDataset(Info.datasetName); fc = dataset.OpenFeatureClass(Info.featureclassName); DataRow dr = AttriTable.NewRow();; RowBufferFactory rbf = new RowBufferFactory(); IRowBuffer rb = rbf.CreateRowBuffer(fc.GetFields()); // 构造待插入的数据:暂时用最后一行的数据进行插入 int lastOid = int.Parse(dataGridView1.Rows[AttriTable.Rows.Count - 1].Cells["oid"].Value.ToString()); IRowBuffer lastRow = fc.GetRow(lastOid); for (int i = 0; i < fc.GetFields().Count; ++i) { string strColName = fc.GetFields().Get(i).Name; int nPos = rb.FieldIndex(strColName); if (fc.GetFields().Get(i).Name != "oid") { rb.SetValue(nPos, lastRow.GetValue(nPos)); //插入字段值 } } // 构造界面显示数据 for (int j = 0; j < AttriTable.Columns.Count; ++j) { string strColName = AttriTable.Columns[j].ColumnName; int nPos = rb.FieldIndex(strColName); if (nPos != -1 && strColName != "oid") { if (lastRow.GetValue(nPos) != null) { dr[j] = lastRow.GetValue(nPos).ToString(); } } if ("GroupName" == strColName) { dr[j] = this.dataGridView1.Rows[AttriTable.Rows.Count - 1].Cells["GroupName"].Value.ToString(); } } // 往数据库中增加 cursor = fc.Insert(); cursor.InsertRow(rb); // 往界面DataTable上增加 dr["oid"] = cursor.LastInsertId; //获取插入后oid值在界面显示 AttriTable.Rows.Add(dr); this.dataGridView1.Refresh(); this.dataGridView1.Rows[AttriTable.Rows.Count - 1].Selected = true; this.dataGridView1.FirstDisplayedScrollingRowIndex = AttriTable.Rows.Count - 1; MessageBox.Show("增加记录成功"); // 更新表头文字 if (FilterWhereClause.Equals("")) { this.Text = "Attributes of " + Info.featureclassName + " [Total records: " + fc.GetCount(null).ToString() + "]"; } else { this.Text = "Attributes of " + Info.featureclassName + " [Total records: " + AttriTable.Rows.Count.ToString() + "]" + " Filter: " + FilterWhereClause; } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { if (ds != null) { //Marshal.ReleaseComObject(ds); ds = null; } if (dataset != null) { //Marshal.ReleaseComObject(dataset); dataset = null; } if (fc != null) { //Marshal.ReleaseComObject(fc); fc = null; } if (cursor != null) { //Marshal.ReleaseComObject(cursor); cursor = null; } } }
private void sbtn_Split_Click(object sender, System.EventArgs e) { DF3DApplication app = DF3DApplication.Application; if (app == null || app.Current3DMapControl == null) { return; } if (System.Windows.Forms.DialogResult.No != XtraMessageBox.Show("模型拆分不支持撤销操作,是否继续?", "询问", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Exclamation)) { IFeatureClass featureClass = null; IFdeCursor fdeCursor = null; app.Current3DMapControl.PauseRendering(false); WaitDialogForm waitDialogForm = null; try { int count = SelectCollection.Instance().GetCount(true); if (count <= 0) { XtraMessageBox.Show("未选中要拆分的模型!"); } else { if (this._renderGeo == null || this._renderGeo.GetFdeGeometry() == null) { XtraMessageBox.Show("请先绘制或选择拆分多边形!"); } else { IMultiPolygon multiPolygon = null; if (this.radioGroup1.SelectedIndex == 1) { IGeometryFactory geometryFactory = new GeometryFactoryClass(); multiPolygon = (geometryFactory.CreateGeometry(gviGeometryType.gviGeometryMultiPolygon, gviVertexAttribute.gviVertexAttributeZ) as IMultiPolygon); multiPolygon.AddPolygon(this._renderGeo.GetFdeGeometry() as IPolygon); } else { if (this.radioGroup1.SelectedIndex == 0) { multiPolygon = (this._renderGeo.GetFdeGeometry() as IMultiPolygon); } } if (multiPolygon == null) { XtraMessageBox.Show("获取裁剪多边形失败!"); } else { waitDialogForm = new WaitDialogForm(string.Empty, "正在拆分模型,请稍后..."); HashMap featureClassInfoMap = SelectCollection.Instance().FeatureClassInfoMap; System.Collections.Hashtable hashtable = new System.Collections.Hashtable(); foreach (DF3DFeatureClass featureClassInfo in featureClassInfoMap.Keys) { if (featureClassInfo.GetFeatureLayer().GeometryType == gviGeometryColumnType.gviGeometryColumnModelPoint) { System.Collections.Generic.List <int> list = new System.Collections.Generic.List <int>(); System.Collections.Generic.List <int> list2 = new System.Collections.Generic.List <int>(); IRowBufferCollection rowBufferCollection = new RowBufferCollectionClass(); IRowBufferCollection rowBufferCollection2 = new RowBufferCollectionClass(); ResultSetInfo resultSetInfo = featureClassInfoMap[featureClassInfo] as ResultSetInfo; DataTable resultSetTable = resultSetInfo.ResultSetTable; if (resultSetTable.Rows.Count >= 1) { featureClass = featureClassInfo.GetFeatureClass(); IResourceManager resourceManager = featureClass.FeatureDataSet as IResourceManager; string fidFieldName = featureClass.FidFieldName; int num = featureClass.GetFields().IndexOf(fidFieldName); foreach (DataRow dataRow in resultSetTable.Rows) { int num2 = int.Parse(dataRow[fidFieldName].ToString()); IRowBuffer row = featureClass.GetRow(num2); int position = row.FieldIndex(featureClassInfo.GetFeatureLayer().GeometryFieldName); IModelPoint modelPoint = row.GetValue(position) as IModelPoint; if (modelPoint != null) { Gvitech.CityMaker.Resource.IModel model = resourceManager.GetModel(modelPoint.ModelName); IGeometryConvertor geometryConvertor = new GeometryConvertorClass(); Gvitech.CityMaker.Resource.IModel model2 = null; Gvitech.CityMaker.Resource.IModel model3 = null; IModelPoint modelPoint2 = null; IModelPoint modelPoint3 = null; bool flag = geometryConvertor.SplitModelPointByPolygon2D(multiPolygon, model, modelPoint, out model2, out modelPoint2, out model3, out modelPoint3); if (flag && model2 != null && !model2.IsEmpty && modelPoint2 != null && model3 != null && !model3.IsEmpty && modelPoint3 != null) { System.Guid guid = System.Guid.NewGuid(); string text = guid.ToString(); resourceManager.AddModel(text, model2, null); resourceManager.RebuildSimplifiedModel(text); guid = System.Guid.NewGuid(); string text2 = guid.ToString(); resourceManager.AddModel(text2, model3, null); resourceManager.RebuildSimplifiedModel(text2); modelPoint3.ModelName = text2; row.SetValue(position, modelPoint3); rowBufferCollection.Add(row); list.Add(num2); modelPoint2.ModelName = text; IRowBuffer rowBuffer = row.Clone(false); rowBuffer.SetNull(num); rowBuffer.SetValue(position, modelPoint2); fdeCursor = featureClass.Insert(); fdeCursor.InsertRow(rowBuffer); int lastInsertId = fdeCursor.LastInsertId; rowBuffer.SetValue(num, lastInsertId); rowBufferCollection2.Add(rowBuffer); list2.Add(lastInsertId); System.Runtime.InteropServices.Marshal.ReleaseComObject(fdeCursor); fdeCursor = null; } } } featureClass.UpdateRows(rowBufferCollection, false); app.Current3DMapControl.FeatureManager.EditFeatures(featureClass, rowBufferCollection); app.Current3DMapControl.FeatureManager.CreateFeatures(featureClass, rowBufferCollection2); hashtable[featureClassInfo] = list2; //System.Runtime.InteropServices.Marshal.ReleaseComObject(featureClass); featureClass = null; System.Runtime.InteropServices.Marshal.ReleaseComObject(resourceManager); resourceManager = null; } } } SelectCollection.Instance().UpdateSelection(hashtable); base.Close(); base.DialogResult = System.Windows.Forms.DialogResult.OK; } } } } catch (System.Runtime.InteropServices.COMException ex) { XtraMessageBox.Show(ex.Message); } catch (System.UnauthorizedAccessException var_35_4EC) { XtraMessageBox.Show("拒绝访问"); } catch (System.Exception ex2) { XtraMessageBox.Show(ex2.Message); } finally { if (waitDialogForm != null) { waitDialogForm.Close(); } if (fdeCursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(fdeCursor); fdeCursor = null; } //if (featureClass != null && System.Runtime.InteropServices.Marshal.IsComObject(featureClass)) //{ // System.Runtime.InteropServices.Marshal.ReleaseComObject(featureClass); // featureClass = null; //} app.Current3DMapControl.ResumeRendering(); } } }