void axRenderControl1_RcMouseClickSelect_CreateFeature(IPickResult PickResult, IPoint IntersectPoint, gviModKeyMask Mask, gviMouseSelectMode EventSender) { _geoEditor.FinishEdit(); //用于当拾取到基准面时,或者没有正常右键结束连续调用Start时 this.axRenderControl1.FeatureManager.UnhighlightAll(); if (PickResult != null) { switch (PickResult.Type) { case gviObjectType.gviObjectFeatureLayer: { IFeatureLayerPickResult flpr = PickResult as IFeatureLayerPickResult; int fid = flpr.FeatureId; //加载多FeatureClass时要每次重新获取 _featureClass = (IFeatureClass)fcGUIDMap[flpr.FeatureLayer.FeatureClassId]; _featureLayer = flpr.FeatureLayer; IFdeCursor cursor = null; try { _buffer = _featureClass.CreateRowBuffer(); IQueryFilter filter = new QueryFilter(); //filter.AddSubField("oid"); //注意:StartEditFeatureGeometry里必须传入一个完整的rowbuffer,所以这里不能限定字段 filter.WhereClause = "oid =" + fid; cursor = _featureClass.Search(filter, false); IRowBuffer row = null; if ((row = cursor.NextRow()) != null) { _buffer = row as IRowBuffer; int pos = _buffer.FieldIndex("Geometry"); oldfdeGeometry = _buffer.GetValue(pos) as IGeometry; _buffer.SetValue(0, _featureClass.GetCount(null)); //修改fid为不同值,否则不是创建而是编辑 } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { } this.axRenderControl1.FeatureManager.HighlightFeature(_featureClass, fid, System.Drawing.Color.Yellow); resultCode = _geoEditor.StartEditFeatureGeometry(_buffer, _featureLayer, gviGeoEditType.gviGeoEditCreator); if (!resultCode) { MessageBox.Show(this.axRenderControl1.GetLastError().ToString()); } } break; } } }
void axRenderControl1_RcMouseClickSelect(IPickResult PickResult, IPoint IntersectPoint, gviModKeyMask Mask, gviMouseSelectMode EventSender) { this.axRenderControl1.FeatureManager.UnhighlightAll(); // 置空 _buffer = null; _featureLayer = null; if (PickResult != null) { switch (PickResult.Type) { case gviObjectType.gviObjectFeatureLayer: { IFeatureLayerPickResult flpr = PickResult as IFeatureLayerPickResult; int fid = flpr.FeatureId; //加载多FeatureClass时要每次重新获取 _featureClass = (IFeatureClass)fcGUIDMap[flpr.FeatureLayer.FeatureClassId]; _featureLayer = flpr.FeatureLayer; IFdeCursor cursor = null; try { _buffer = _featureClass.CreateRowBuffer(); QueryFilter filter = new QueryFilter(); //filter.AddSubField("oid"); //注意:StartEditFeatureGeometry里必须传入一个完整的rowbuffer,所以这里不能限定字段 filter.WhereClause = "oid =" + fid; cursor = _featureClass.Search(filter, false); IRowBuffer row = null; if ((row = cursor.NextRow()) != null) { _buffer = row as IRowBuffer; int pos = _buffer.FieldIndex("Geometry"); oldfdeGeometry = _buffer.GetValue(pos) as IGeometry; } } catch (COMException ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } finally { } this.axRenderControl1.FeatureManager.HighlightFeature(_featureClass, fid, System.Drawing.Color.Yellow); EditGeometry(); } break; } } }
public void ImportModelMdb(IFeatureClass fc, string mdbFile, int groupId) { try { //2、获取基本信息 FileInfo fInfo = new FileInfo(mdbFile); string strFilePath = fInfo.DirectoryName; Gvitech.CityMaker.Resource.IResourceFactory rf = new Gvitech.CityMaker.Resource.ResourceFactory(); IResourceManager rm = fc.FeatureDataSet as IResourceManager; Dictionary <string, IEnvelope> cacheModeInfos = new Dictionary <string, IEnvelope>(); List <string> cacheImageNames = new List <string>(); IGeometryFactory geoFactory = new GeometryFactory(); IResourceFactory symbolFac = new ResourceFactory(); IRowBufferCollection fcRows = new RowBufferCollection(); IRowBuffer fcRow = null; //4、解析mdb string strConn = _ConnStr + mdbFile + "\'"; int index = 0; int totalCount = 0; using (OleDbConnection oleConn = new OleDbConnection(strConn)) { oleConn.Open(); string strSql = "select count(*) from Attribute_Table_CityManager"; OleDbCommand countCmd = new OleDbCommand(strSql, oleConn); OleDbDataReader countReader = countCmd.ExecuteReader(); countReader.Read(); totalCount = countReader.GetInt32(0); countReader.Close(); OleDbCommand command = new OleDbCommand(_QueryModelStr, oleConn); OleDbDataReader reader = command.ExecuteReader(); IModelPoint modePoint = null; while (reader.Read()) { int percent = ++index * 100 / totalCount; string toolTip = string.Format("已完成{0}条/总共{1}条 {2}%", index, totalCount, percent); CommonEntity.FormEntity.Text = toolTip; double dScaleX = 0.0; double dScaleY = 0.0; double dScaleZ = 0.0; double.TryParse(reader.GetString(9), out dScaleX); double.TryParse(reader.GetString(10), out dScaleY); double.TryParse(reader.GetString(11), out dScaleZ); //if (dScaleX < 0 || dScaleY < 0 || dScaleZ < 0) // continue; //fc fcRow = fc.CreateRowBuffer(); int nPose = fcRow.FieldIndex("name"); if (nPose == -1) { continue; } string modelName = reader.GetString(0); fcRow.SetValue(nPose, modelName); nPose = fcRow.FieldIndex("groupId"); if (nPose == -1) { continue; } fcRow.SetValue(nPose, groupId); modePoint = (IModelPoint)geoFactory.CreateGeometry( gviGeometryType.gviGeometryModelPoint, gviVertexAttribute.gviVertexAttributeZ); modePoint.ModelName = modelName; double dModePointX = double.Parse(reader.GetString(2)) + 0; // CoorX = 0 double dModePointY = double.Parse(reader.GetString(3)) + 0; // CoorY = 0 double dModePointZ = double.Parse(reader.GetString(4)) + 0; // CoorZ = 0 double dRotationAngle = double.Parse(reader.GetString(5)); double dAxisX = double.Parse(reader.GetString(6)); double dAxisY = double.Parse(reader.GetString(7)); double dAxisZ = double.Parse(reader.GetString(8)); double dUTransAngel = double.Parse(reader.GetString(12)); double dUTransX = double.Parse(reader.GetString(13)); double dUTransY = double.Parse(reader.GetString(14)); double dUTransZ = double.Parse(reader.GetString(15)); modePoint.X = dModePointX; modePoint.Y = dModePointY; modePoint.Z = dModePointZ; IMatrix matrix = new Matrix(); IVector3 pointVec = new Vector3(); pointVec.X = dModePointX; pointVec.Y = dModePointY; pointVec.Z = dModePointZ; IVector3 Scale = new Vector3(); Scale.X = dScaleX; Scale.Y = dScaleY; Scale.Z = dScaleZ; IVector3 Rotation = new Vector3(); Rotation.X = dAxisX; Rotation.Y = dAxisY; Rotation.Z = dAxisZ; IVector3 Shear = new Vector3(); Shear.X = dUTransX; Shear.Y = dUTransY; Shear.Z = dUTransZ; matrix.Compose2(pointVec, Scale, dRotationAngle, Rotation, dUTransAngel, Shear); modePoint.FromMatrix(matrix); //mc IPropertySet Images = null; //如果内存中不包含 if (!cacheModeInfos.Keys.Contains(modelName)) { //数据库中包含 if (rm.ModelExist(modelName)) { //重名即跳过 IEnvelope ev = rm.GetModel(modelName).Envelope; modePoint.ModelEnvelope = ev; } else { IModel simpleModel = null; string osgFilePath = strFilePath + "\\" + modelName + ".osg"; IModel fineModel = null; IMatrix m = null; symbolFac.CreateModelAndImageFromFileEx(osgFilePath, out Images, out simpleModel, out fineModel, out m); if (fineModel == null || fineModel.GroupCount == 0) { continue; } cacheModeInfos[modelName] = modePoint.ModelEnvelope = Clone(fineModel.Envelope); rm.AddModel(modelName, fineModel, simpleModel); } } else { modePoint.ModelEnvelope = cacheModeInfos[modelName]; } nPose = fcRow.FieldIndex("Geometry"); fcRow.SetValue(nPose, modePoint); fcRows.Add(fcRow); //tc if (Images != null) { int nCount = Images.Count; if (nCount > 0) { Hashtable htImages = Images.AsHashtable(); foreach (DictionaryEntry item in htImages) { string imgName = item.Key.ToString(); IImage img = item.Value as IImage; if (img == null) { continue; } if (string.IsNullOrEmpty(imgName)) { continue; } //如果内存中不包含 if (!cacheImageNames.Contains(imgName)) { //数据库中包含 if (rm.ImageExist(imgName)) { //重名即跳过 } } //如果内存中不包含,数据库中也不包含 if (!rm.ImageExist(imgName) && !cacheImageNames.Contains(imgName)) { cacheImageNames.Add(imgName); rm.AddImage(imgName, img); } } } } //end if (fcRows.Count >= 10) { InsertFeatures(fc as IObjectClass, fcRows); fcRows.Clear(); } } reader.Close(); oleConn.Close(); } if (fcRows.Count > 0) { InsertFeatures(fc as IObjectClass, fcRows); fcRows.Clear(); } } catch (System.Exception ex) { if (ex.GetType().Name.Equals("UnauthorizedAccessException")) { MessageBox.Show("需要标准runtime授权"); } else { MessageBox.Show(ex.Message); } } }
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(); //保留表结构,清空表 }
public void ImportModelXml(IFeatureClass fc, string mdbFile, int groupId, int _nCount) { try { //2、获取基本信息 FileInfo fInfo = new FileInfo(mdbFile); string strFilePath = fInfo.DirectoryName; IResourceManager rm = fc.FeatureDataSet as IResourceManager; Dictionary <string, IEnvelope> cacheModeInfos = new Dictionary <string, IEnvelope>(); List <string> cacheImageNames = new List <string>(); IGeometryFactory geoFactory = new GeometryFactory(); IResourceFactory symbolFac = new ResourceFactory(); IRowBufferCollection fcRows = new RowBufferCollection(); IRowBuffer fcRow = null; //4、解析xml string strConn = "" + mdbFile + "\'"; int index = 0; int totalCount = _nCount; IModelPoint modePoint = null; for (int i = 0; i < _nCount; ++i) { int percent = ++index * 100 / totalCount; string toolTip = string.Format("已完成{0}条/总共{1}条 {2}%", index, totalCount, percent); CommonEntity.FormEntity.Text = toolTip; //fc fcRow = fc.CreateRowBuffer(); int nPose = fcRow.FieldIndex("name"); if (nPose == -1) { continue; } string modelName = _dataTable.Rows[i]["ModelName"].ToString(); fcRow.SetValue(nPose, modelName); nPose = fcRow.FieldIndex("groupId"); if (nPose == -1) { continue; } fcRow.SetValue(nPose, groupId); modePoint = (IModelPoint)geoFactory.CreateGeometry( gviGeometryType.gviGeometryModelPoint, gviVertexAttribute.gviVertexAttributeZ); modePoint.ModelName = modelName; double dModePointX = double.Parse(_dataTable.Rows[i]["LocationX"].ToString()) + 0; // CoorX = 0 double dModePointY = double.Parse(_dataTable.Rows[i]["LocationY"].ToString()) + 0; // CoorY = 0 double dModePointZ = double.Parse(_dataTable.Rows[i]["LocationZ"].ToString()) + 0; // CoorZ = 0 string strMatrix = _dataTable.Rows[i]["Matrix3"].ToString(); float[] Matrix = null; if (!string.IsNullOrEmpty(strMatrix)) { string[] strArray = strMatrix.Split(','); if (strArray.Length == 9) { Matrix = new float[9]; Matrix[0] = float.Parse(strArray[0]); Matrix[1] = float.Parse(strArray[1]); Matrix[2] = float.Parse(strArray[2]); Matrix[3] = float.Parse(strArray[3]); Matrix[4] = float.Parse(strArray[4]); Matrix[5] = float.Parse(strArray[5]); Matrix[6] = float.Parse(strArray[6]); Matrix[7] = float.Parse(strArray[7]); Matrix[8] = float.Parse(strArray[8]); } } modePoint.X = dModePointX; modePoint.Y = dModePointY; modePoint.Z = dModePointZ; modePoint.Matrix33 = Matrix; //mc IPropertySet Images = null; //如果内存中不包含 if (!cacheModeInfos.Keys.Contains(modelName)) { //数据库中包含 if (rm.ModelExist(modelName)) { //重名即跳过 IEnvelope ev = rm.GetModel(modelName).Envelope; modePoint.ModelEnvelope = ev; } else { IModel simpleModel = null; string osgFilePath = strFilePath + "\\" + modelName + ".osg"; IModel fineModel = null; IMatrix matrix = null; symbolFac.CreateModelAndImageFromFileEx(osgFilePath, out Images, out simpleModel, out fineModel, out matrix); if (fineModel == null || fineModel.GroupCount == 0) { continue; } cacheModeInfos[modelName] = modePoint.ModelEnvelope = Clone(fineModel.Envelope); rm.AddModel(modelName, fineModel, simpleModel); //Marshal.ReleaseComObject(simpleModel); //Marshal.ReleaseComObject(fineModel); } } else { modePoint.ModelEnvelope = cacheModeInfos[modelName]; } nPose = fcRow.FieldIndex("Geometry"); fcRow.SetValue(nPose, modePoint); fcRows.Add(fcRow); //tc int nCount = Images.Count; if (Images != null && nCount > 0) { Hashtable htImages = Images.AsHashtable(); foreach (DictionaryEntry item in htImages) { string imgName = item.Key.ToString(); IImage img = item.Value as IImage; if (img == null) { continue; } if (string.IsNullOrEmpty(imgName)) { continue; } //如果内存中不包含 if (!cacheImageNames.Contains(imgName)) { //数据库中包含 if (rm.ImageExist(imgName)) { //重名即跳过 } } //如果内存中不包含,数据库中也不包含 if (!rm.ImageExist(imgName) && !cacheImageNames.Contains(imgName)) { cacheImageNames.Add(imgName); rm.AddImage(imgName, img); } } } //end if (fcRows.Count >= 10) { InsertFeatures(fc as IObjectClass, fcRows); fcRows.Clear(); } } if (fcRows.Count > 0) { InsertFeatures(fc as IObjectClass, fcRows); fcRows.Clear(); } } catch (System.Exception ex) { if (ex.GetType().Name.Equals("UnauthorizedAccessException")) { MessageBox.Show("需要标准runtime授权"); } else { MessageBox.Show(ex.Message); } } }
public void ImportModelOsg(IFeatureClass fc, string osgFile, int groupId) { try { FileInfo fInfo = new FileInfo(osgFile); string strFilePath = fInfo.DirectoryName; IResourceManager rm = fc.FeatureDataSet as IResourceManager; IGeometryFactory geoFactory = new GeometryFactory(); IResourceFactory symbolFac = new ResourceFactory(); IRowBuffer fcRow = null; //fc string modelName = fInfo.Name.Split('.')[0]; fcRow = fc.CreateRowBuffer(); int nPose = fcRow.FieldIndex("name"); if (nPose != -1) { fcRow.SetValue(nPose, modelName); } nPose = fcRow.FieldIndex("groupId"); if (nPose != -1) { fcRow.SetValue(nPose, groupId); } //mc IPropertySet Images = null; IModel simpleModel = null; string osgFilePath = strFilePath + "\\" + modelName + ".osg"; IModel fineModel = null; IMatrix matrix = null; symbolFac.CreateModelAndImageFromFileEx(osgFilePath, out Images, out simpleModel, out fineModel, out matrix); if (fineModel == null || fineModel.GroupCount == 0) { return; } rm.AddModel(modelName, fineModel, simpleModel); //Marshal.ReleaseComObject(simpleModel); //Marshal.ReleaseComObject(fineModel); //tc int nCount = Images.Count; if (Images != null && nCount > 0) { Hashtable htImages = Images.AsHashtable(); foreach (DictionaryEntry item in htImages) { String imgName = item.Key.ToString(); IImage img = item.Value as IImage; rm.AddImage(imgName, img); } } //modelpoint IModelPoint modePoint = null; modePoint = (IModelPoint)geoFactory.CreateGeometry( gviGeometryType.gviGeometryModelPoint, gviVertexAttribute.gviVertexAttributeZ); modePoint.FromMatrix(matrix); modePoint.ModelName = modelName; double dModePointX = 0.0; double dModePointY = 0.0; double dModePointZ = 0.0; modePoint.X += dModePointX; modePoint.Y += dModePointY; modePoint.Z += dModePointZ; nPose = fcRow.FieldIndex("Geometry"); fcRow.SetValue(nPose, modePoint); if (fc.HasTemporal()) { ITemporalManager mgr = fc.TemporalManager; mgr.Insert(DateTime.Now, fcRow); //Marshal.ReleaseComObject(mgr); } else { IRowBufferCollection fcRows = new RowBufferCollection(); fcRows.Add(fcRow); InsertFeatures(fc as IObjectClass, fcRows); fcRows.Clear(); } } catch (System.Exception ex) { if (ex.GetType().Name.Equals("UnauthorizedAccessException")) { MessageBox.Show("需要标准runtime授权"); } else { MessageBox.Show(ex.Message); } } }