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; } } }
private void AddGeometry() { try { this.beforeRowBufferMap.Clear(); SelectCollection.Instance().Clear(); DF3DFeatureClass featureClassInfo = CommonUtils.Instance().CurEditLayer; if (featureClassInfo == null) { return; } IFeatureClass fc = featureClassInfo.GetFeatureClass(); if (fc == null) { return; } IFieldInfoCollection fields = fc.GetFields(); int indexGeo = fields.IndexOf(this._strGeometryFieldName); if (indexGeo == -1) { return; } IGeometry geo = this._drawTool.GetGeo(); IGeometry geoOut = null; switch (geo.GeometryType) { case gviGeometryType.gviGeometryModelPoint: geoOut = geo; break; case gviGeometryType.gviGeometryPoint: IPoint pt = geo as IPoint; IPoint pt1 = pt.Clone2(gviVertexAttribute.gviVertexAttributeZM) as IPoint; pt1.SetCoords(pt.X, pt.Y, pt.Z, 0, 0); geoOut = pt1; break; case gviGeometryType.gviGeometryPolyline: IPolyline line = geo as IPolyline; IPolyline line1 = this._geoFact.CreateGeometry(gviGeometryType.gviGeometryPolyline, gviVertexAttribute.gviVertexAttributeZM) as IPolyline; for (int i = 0; i < line.PointCount; i++) { IPoint ptGet = line.GetPoint(i); IPoint pttemp = ptGet.Clone2(gviVertexAttribute.gviVertexAttributeZM) as IPoint; pttemp.SetCoords(ptGet.X, ptGet.Y, ptGet.Z, 0, 0); line1.AppendPoint(pttemp); } geoOut = line1; break; case gviGeometryType.gviGeometryPolygon: IPolygon polygon = geo as IPolygon; IPolygon polygon1 = this._geoFact.CreateGeometry(gviGeometryType.gviGeometryPolygon, gviVertexAttribute.gviVertexAttributeZM) as IPolygon; for (int i = 0; i < polygon.ExteriorRing.PointCount; i++) { IPoint ptGet = polygon.ExteriorRing.GetPoint(i); IPoint pttemp = ptGet.Clone2(gviVertexAttribute.gviVertexAttributeZM) as IPoint; pttemp.SetCoords(ptGet.X, ptGet.Y, ptGet.Z, 0, 0); polygon1.ExteriorRing.AppendPoint(pttemp); } geoOut = polygon1; break; } if (geoOut == null) { return; } if (geo.GeometryType == gviGeometryType.gviGeometryModelPoint) { //导入模型到数据库中 string mn = BitConverter.ToString(ObjectIdGenerator.Generate()).Replace("-", string.Empty).ToLowerInvariant(); IModelPoint mp = geoOut as IModelPoint; bool bRes = this.ImportOsg(mn, mp.ModelName); if (!bRes) { return; } mp.ModelName = mn; geoOut = mp; } IRowBufferCollection rowCol = new RowBufferCollection(); IRowBufferFactory fac = new RowBufferFactory(); IRowBuffer row = fac.CreateRowBuffer(fields); row.SetValue(indexGeo, geoOut); rowCol.Add(row); beforeRowBufferMap[featureClassInfo] = rowCol; } catch (Exception ex) { } }
private void AddRecord() { try { this.beforeRowBufferMap.Clear(); SelectCollection.Instance().Clear(); DF3DFeatureClass featureClassInfo = CommonUtils.Instance().CurEditLayer; if (featureClassInfo == null) { return; } IFeatureClass fc = featureClassInfo.GetFeatureClass(); if (fc == null) { return; } IFeatureLayer fl = featureClassInfo.GetFeatureLayer(); if (fl == null) { return; } IFieldInfoCollection fields = fc.GetFields(); int indexGeo = fields.IndexOf(fl.GeometryFieldName); if (indexGeo == -1) { return; } IFieldInfo fiGeo = fields.Get(indexGeo); if (fiGeo == null || fiGeo.GeometryDef == null) { return; } IGeometry geo = this._drawTool.GetGeo(); if (geo.GeometryType != gviGeometryType.gviGeometryPolygon) { return; } IPolygon polygon = geo as IPolygon; IPolygon geoOut = this._geoFact.CreateGeometry(gviGeometryType.gviGeometryPolygon, fiGeo.GeometryDef.VertexAttribute) as IPolygon; for (int i = 0; i < polygon.ExteriorRing.PointCount; i++) { IPoint ptGet = polygon.ExteriorRing.GetPoint(i); IPoint pttemp = ptGet.Clone2(fiGeo.GeometryDef.VertexAttribute) as IPoint; if (fiGeo.GeometryDef.HasZ) { pttemp.SetCoords(ptGet.X, ptGet.Y, ptGet.Z, 0, 0); } else { pttemp.SetCoords(ptGet.X, ptGet.Y, 0, 0, 0); } geoOut.ExteriorRing.AppendPoint(pttemp); } IRowBufferCollection rowCol = new RowBufferCollection(); IRowBufferFactory fac = new RowBufferFactory(); IRowBuffer row = fac.CreateRowBuffer(fields); row.SetValue(indexGeo, geoOut); foreach (DataRow dr in this._dt.Rows) { IFieldInfo fi = dr["F"] as IFieldInfo; if (fi == null) { continue; } string fn = fi.Name; int index = fields.IndexOf(fn); if (index != -1) { if (dr["FV"] == null) { row.SetNull(index); } else { string strobj = dr["FV"].ToString(); bool bRes = false; switch (fi.FieldType) { case gviFieldType.gviFieldBlob: case gviFieldType.gviFieldGeometry: case gviFieldType.gviFieldUnknown: break; case gviFieldType.gviFieldFloat: float f; bRes = float.TryParse(strobj, out f); if (bRes) { row.SetValue(index, f); } else { row.SetNull(index); } break; case gviFieldType.gviFieldDouble: double d; bRes = double.TryParse(strobj, out d); if (bRes) { row.SetValue(index, d); } else { row.SetNull(index); } break; case gviFieldType.gviFieldFID: case gviFieldType.gviFieldUUID: case gviFieldType.gviFieldInt16: Int16 i16; bRes = Int16.TryParse(strobj, out i16); if (bRes) { row.SetValue(index, i16); } else { row.SetNull(index); } break; case gviFieldType.gviFieldInt32: Int32 i32; bRes = Int32.TryParse(strobj, out i32); if (bRes) { row.SetValue(index, i32); } else { row.SetNull(index); } break; case gviFieldType.gviFieldInt64: Int64 i64; bRes = Int64.TryParse(strobj, out i64); if (bRes) { row.SetValue(index, i64); } else { row.SetNull(index); } break;; case gviFieldType.gviFieldString: row.SetValue(index, strobj); break; case gviFieldType.gviFieldDate: DateTime dt; bRes = DateTime.TryParse(strobj, out dt); if (bRes) { row.SetValue(index, dt); } else { row.SetNull(index); } break; default: break; } } } } rowCol.Add(row); beforeRowBufferMap[featureClassInfo] = rowCol; UpdateDatabase(); } catch (Exception ex) { } }
private void AddRecord() { try { this.beforeRowBufferMap.Clear(); SelectCollection.Instance().Clear(); if (reg == null || tc == null) { return; } FacStyleClass style = this.cmbStyle.EditValue as FacStyleClass; if (style == null) { return; } SubClass sc = this.cmbClassify.EditValue as SubClass; DF3DFeatureClass featureClassInfo = CommonUtils.Instance().CurEditLayer; if (featureClassInfo == null) { return; } IFeatureClass fc = featureClassInfo.GetFeatureClass(); if (fc == null) { return; } IResourceManager manager = fc.FeatureDataSet as IResourceManager; if (manager == null) { return; } IFeatureLayer fl = featureClassInfo.GetFeatureLayer(); if (fl == null) { return; } IFieldInfoCollection fields = fc.GetFields(); int indexOid = fields.IndexOf(fc.FidFieldName); if (indexOid == -1) { return; } int mpindex = fields.IndexOf(fl.GeometryFieldName); if (mpindex == -1) { return; } int indexShape = fields.IndexOf("Shape"); if (indexShape == -1) { return; } int indexFootPrint = fields.IndexOf("FootPrint"); if (indexFootPrint == -1) { return; } int indexStyleId = fields.IndexOf("StyleId"); if (indexStyleId == -1) { return; } int indexFacilityId = fields.IndexOf("FacilityId"); if (indexFacilityId == -1) { return; } IFieldInfo fiShape = fields.Get(indexShape); if (fiShape == null || fiShape.GeometryDef == null) { return; } IGeometry geo = this._drawTool.GetGeo(); if (geo.GeometryType != gviGeometryType.gviGeometryPoint) { return; } IPoint pt = geo as IPoint; IPoint geoOut = pt.Clone2(fiShape.GeometryDef.VertexAttribute) as IPoint; if (fiShape.GeometryDef.HasZ) { geoOut.SetCoords(pt.X, pt.Y, pt.Z, 0, 0); } else { geoOut.SetCoords(pt.X, pt.Y, 0, 0, 0); } IQueryFilter filter = new QueryFilter(); filter.WhereClause = "1=1"; filter.ResultBeginIndex = 0; filter.ResultLimit = 1; filter.PostfixClause = "ORDER BY " + fc.FidFieldName + " desc"; IFdeCursor cursor = null; cursor = fc.Search(filter, false); IRowBuffer rowtemp = cursor.NextRow(); int oid = 0; if (rowtemp != null) { oid = int.Parse(rowtemp.GetValue(indexOid).ToString()); } if (cursor != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); cursor = null; } IRowBufferFactory fac = new RowBufferFactory(); IRowBuffer row = fac.CreateRowBuffer(fields); row.SetValue(indexOid, oid + 1); row.SetValue(indexShape, geoOut); row.SetValue(indexFootPrint, geoOut.Clone2(gviVertexAttribute.gviVertexAttributeNone)); row.SetValue(indexStyleId, style.ObjectId); row.SetValue(indexFacilityId, BitConverter.ToString(ObjectIdGenerator.Generate()).Replace("-", string.Empty).ToLowerInvariant()); if (sc != null) { int indexClassify = fields.IndexOf(this._classifyName); int indexGroupId = fields.IndexOf("GroupId"); if (indexClassify != -1 && indexGroupId != -1) { row.SetValue(indexClassify, sc.Name); row.SetValue(indexGroupId, sc.GroupId); } } foreach (DataRow dr in this._dt.Rows) { IFieldInfo fi = dr["F"] as IFieldInfo; if (fi == null) { continue; } string fn = fi.Name; int index = fields.IndexOf(fn); if (index != -1) { if (dr["FV"] == null) { row.SetNull(index); } else { string strobj = dr["FV"].ToString(); bool bRes = false; switch (fi.FieldType) { case gviFieldType.gviFieldBlob: case gviFieldType.gviFieldGeometry: case gviFieldType.gviFieldUnknown: break; case gviFieldType.gviFieldFloat: float f; bRes = float.TryParse(strobj, out f); if (bRes) { row.SetValue(index, f); } else { row.SetNull(index); } break; case gviFieldType.gviFieldDouble: double d; bRes = double.TryParse(strobj, out d); if (bRes) { row.SetValue(index, d); } else { row.SetNull(index); } break; case gviFieldType.gviFieldFID: case gviFieldType.gviFieldUUID: case gviFieldType.gviFieldInt16: Int16 i16; bRes = Int16.TryParse(strobj, out i16); if (bRes) { row.SetValue(index, i16); } else { row.SetNull(index); } break; case gviFieldType.gviFieldInt32: Int32 i32; bRes = Int32.TryParse(strobj, out i32); if (bRes) { row.SetValue(index, i32); } else { row.SetNull(index); } break; case gviFieldType.gviFieldInt64: Int64 i64; bRes = Int64.TryParse(strobj, out i64); if (bRes) { row.SetValue(index, i64); } else { row.SetNull(index); } break;; case gviFieldType.gviFieldString: row.SetValue(index, strobj); break; case gviFieldType.gviFieldDate: DateTime dt; bRes = DateTime.TryParse(strobj, out dt); if (bRes) { row.SetValue(index, dt); } else { row.SetNull(index); } break; default: break; } } } } Fac plf = new PipeNodeFac(reg, style, row, tc); IModelPoint mp = null; IModel finemodel = null; IModel simplemodel = null; string name = ""; if (UCAuto3DCreate.RebuildModel(plf, style, out mp, out finemodel, out simplemodel, out name)) { if (finemodel == null || mp == null) { return; } mp.ModelEnvelope = finemodel.Envelope; row.SetValue(mpindex, mp); //if (mc != null) //{ // if (indexClassify != -1 && indexGroupid != -1) // { // } //} bool bRes = false; if (!string.IsNullOrEmpty(mp.ModelName)) { if (!manager.ModelExist(mp.ModelName)) { if (manager.AddModel(mp.ModelName, finemodel, simplemodel)) { bRes = true; } } else { if (manager.UpdateModel(mp.ModelName, finemodel) && manager.UpdateSimplifiedModel(mp.ModelName, simplemodel)) { bRes = true; } } } if (!bRes) { return; } IRowBufferCollection rowCol = new RowBufferCollection(); rowCol.Add(row); beforeRowBufferMap[featureClassInfo] = rowCol; UpdateDatabase(); app.Current3DMapControl.FeatureManager.RefreshFeatureClass(fc); } } catch (Exception ex) { } }
/// <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 AddRecord(IGeometry geo) { try { if (geo.GeometryType != gviGeometryType.gviGeometryModelPoint) { return; } this.beforeRowBufferMap.Clear(); SelectCollection.Instance().Clear(); DF3DFeatureClass featureClassInfo = CommonUtils.Instance().CurEditLayer; if (featureClassInfo == null) { return; } IFeatureClass fc = featureClassInfo.GetFeatureClass(); if (fc == null) { return; } IFeatureDataSet fds = fc.FeatureDataSet; if (fds == null) { return; } IFeatureLayer fl = featureClassInfo.GetFeatureLayer(); if (fl == null) { return; } IFieldInfoCollection fields = fc.GetFields(); int indexGeo = fields.IndexOf(fl.GeometryFieldName); if (indexGeo == -1) { return; } IFieldInfo fiGeo = fields.Get(indexGeo); if (fiGeo == null || fiGeo.GeometryDef == null) { return; } IModelPoint pt = geo as IModelPoint; string mname = Path.GetFileNameWithoutExtension(pt.ModelName); IEnvelope envelope = ImportOsg(fc, pt.ModelName); if (envelope == null) { return; } IModelPoint geoOut = pt.Clone2(fiGeo.GeometryDef.VertexAttribute) as IModelPoint; if (fiGeo.GeometryDef.HasZ) { geoOut.SetCoords(pt.X, pt.Y, pt.Z, 0, 0); } else { geoOut.SetCoords(pt.X, pt.Y, 0, 0, 0); } geoOut.ModelName = mname; geoOut.ModelEnvelope = new EnvelopeClass { MinX = envelope.MinX, MaxX = envelope.MaxX, MinY = envelope.MinY, MaxY = envelope.MaxY, MinZ = envelope.MinZ, MaxZ = envelope.MaxZ }; IRowBufferCollection rowCol = new RowBufferCollection(); IRowBufferFactory fac = new RowBufferFactory(); IRowBuffer row = fac.CreateRowBuffer(fields); row.SetValue(indexGeo, geoOut); foreach (DataRow dr in this._dt.Rows) { IFieldInfo fi = dr["F"] as IFieldInfo; if (fi == null) { continue; } string fn = fi.Name; int index = fields.IndexOf(fn); if (index != -1) { if (dr["FV"] == null) { row.SetNull(index); } else { string strobj = dr["FV"].ToString(); bool bRes = false; switch (fi.FieldType) { case gviFieldType.gviFieldBlob: case gviFieldType.gviFieldGeometry: case gviFieldType.gviFieldUnknown: break; case gviFieldType.gviFieldFloat: float f; bRes = float.TryParse(strobj, out f); if (bRes) { row.SetValue(index, f); } else { row.SetNull(index); } break; case gviFieldType.gviFieldDouble: double d; bRes = double.TryParse(strobj, out d); if (bRes) { row.SetValue(index, d); } else { row.SetNull(index); } break; case gviFieldType.gviFieldFID: case gviFieldType.gviFieldUUID: case gviFieldType.gviFieldInt16: Int16 i16; bRes = Int16.TryParse(strobj, out i16); if (bRes) { row.SetValue(index, i16); } else { row.SetNull(index); } break; case gviFieldType.gviFieldInt32: Int32 i32; bRes = Int32.TryParse(strobj, out i32); if (bRes) { row.SetValue(index, i32); } else { row.SetNull(index); } break; case gviFieldType.gviFieldInt64: Int64 i64; bRes = Int64.TryParse(strobj, out i64); if (bRes) { row.SetValue(index, i64); } else { row.SetNull(index); } break;; case gviFieldType.gviFieldString: row.SetValue(index, strobj); break; case gviFieldType.gviFieldDate: DateTime dt; bRes = DateTime.TryParse(strobj, out dt); if (bRes) { row.SetValue(index, dt); } else { row.SetNull(index); } break; default: break; } } } } rowCol.Add(row); beforeRowBufferMap[featureClassInfo] = rowCol; UpdateDatabase(); Clear(); (this._drawTool as Draw3DModel).Set3DModelFilePath(this.beFilePath.Text); } catch (Exception ex) { } }