using System; using System.Collections; using System.IO; using System.Windows.Forms; using System.Xml; using DevComponents.DotNetBar; using GeoScene.Data; using GeoScene.Engine; using GeoScene.Globe; namespace Cyberpipe.Forms { public partial class FrmShRK : Office2007Form { private GSOGlobeControl globecontrol; public static GSODataSource dssource; public static GSODataSource dsdes; private Hashtable en_cns = new Hashtable(); private Hashtable fields_types = new Hashtable(); string desIp, desDb, desUser, desPwd; string sourIp, sourDb, sourUser, sourPwd; public FrmShRK(GSOGlobeControl _globecontrol) { InitializeComponent(); globecontrol = _globecontrol; } private void FrmShRK_Load(object sender, EventArgs e) { string pipelinetype = Utility.pipelinetype; string[] pipelinetypes = pipelinetype.Split(','); for (int i = 0; i < pipelinetypes.Length; i++) { comboBox1.Items.Add(pipelinetypes[i]); } string filename = Application.StartupPath + "\\FormText.xml"; if (File.Exists(filename)) { XmlTextReader XmlReader = new XmlTextReader(filename); try { while (XmlReader.Read()) { if (XmlReader.Name == "Field") { string str1 = XmlReader["label"]; string str3 = XmlReader["type"]; string str2 = XmlReader.ReadElementString(); en_cns.Add(str1, str2); fields_types.Add(str1, str3); } } } catch (Exception ex) { LogError.PublishError(ex); MessageBox.Show(ex.Message); } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string pipelinetype = comboBox1.Text; listBox1.Items.Clear(); sourIp = Utility.sgdbip; sourDb = Utility.sgdbname; sourUser = Utility.sgdbuser; sourPwd = Utility.sgdbpwd; dssource = globecontrol.Globe.DataManager.OpenOracleDataSource(sourIp + "/" + sourDb, "", "", sourUser, sourPwd); if (dssource == null) { MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (pipelinetype != "全部") { for (int i = 0; i < dssource.DatasetCount; i++) { GSODataset dataset = dssource.GetDatasetAt(i); if (dataset.Name.ToLower().Contains("network")) { continue; } if (!dataset.Name.Contains(pipelinetype)) { continue; } if (dataset.Name.Contains("SH")) { listBox1.Items.Add(dataset.Name); } } } else { for (int i = 0; i < dssource.DatasetCount; i++) { GSODataset dataset = dssource.GetDatasetAt(i); if (dataset.Name.ToLower().Contains("network")) { continue; } listBox1.Items.Add(dataset.Name); } } buttonX1.Enabled = true; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { listBox2.Items.Clear(); desIp = Utility.sgdbip; desDb = Utility.sgdbname; desUser = Utility.sgdbuser; desPwd = Utility.sgdbpwd; dsdes = globecontrol.Globe.DataManager.OpenOracleDataSource(desIp + "/" + desDb, "", "", desUser, desPwd); if (dsdes == null) { MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } for (int i = 0; i < dsdes.DatasetCount; i++) { GSODataset dataset = dsdes.GetDatasetAt(i); if (dataset.Name.ToLower().Contains("network")) { continue; } if (!dataset.Name.ToLower().EndsWith("管线")) { continue; } listBox2.Items.Add(dataset.Name); } //listBox2.Items.Add("污水管线new"); } /// <summary> /// 将选中的图层复制到已有图层 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonX1_Click(object sender, EventArgs e) { //目标库已连接 if (dsdes == null) { MessageBox.Show("未连接数据库,请先连接数据库!", "提示"); return; } if (dssource == null) { MessageBox.Show("未连接数据库,请先连接数据库!", "提示"); return; } /* if (!Utility.isNetworkConnectionSuccess(Utility.sgdbip)) { MessageBox.Show("网络连接失败!", "提示"); return; } */ if (listBox1.Text == "") { MessageBox.Show("请选择要复制的数据源图层!", "提示"); return; } if (listBox2.Text == "") { MessageBox.Show("请选择要复制到的目标图层!", "提示"); return; } string layersource = listBox1.Text; string layerdes = listBox2.Text; //目标库 GSODataset desdataset = dsdes.GetDatasetByName(layerdes); GSOFeatureDataset deslayer; if (desdataset != null) { DialogResult result = MessageBox.Show("管线图层名称在数据库中已存在!是否向该表追加", "提示", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { deslayer = desdataset as GSOFeatureDataset; } else { return; } } else { deslayer = CreateDBFeatureDataset(layerdes); } if (deslayer == null) { return; } //元图层 GSODataset sourcefeat = dssource.GetDatasetByName(layersource); //MessageBox.Show(sourcefeat.DataSource); GSOFeatureDataset sourcefeatds=null; if (sourcefeat != null) { sourcefeatds = sourcefeat as GSOFeatureDataset; } sourcefeatds.Open(); GSOFeatures sourcefeatures = sourcefeatds.GetAllFeatures(); if (desdataset != null) { //根据编号删除数据库图层中已经存在的记录即要素 string sql = "delete from " + layerdes + " where 编号 in ("; for (int j = 0; j < sourcefeatures.Length; j++) { GSOFeature f = sourcefeatures[j]; if (f != null) { if (f.IsFieldValueNull("编号") == false) { string fieldID = f.GetFieldAsString("编号").Trim(); if (fieldID != "") { sql += "'" + fieldID + "',"; } } } } sql = sql.Substring(0, sql.Length - 1); sql += ")"; OledbHelper.sqlExecuteNonQuery(sql); } sourcefeatds.Close(); //向目标中拷贝 GSODataset desfeat = dsdes.GetDatasetByName(layerdes); GSOFeatureDataset desfeatds = null; if (desfeat != null) { desfeatds = desfeat as GSOFeatureDataset; } desfeatds.Open(); //向新数据集写入字段 for (int i = 0; i < sourcefeatds.FieldCount; i++) { GSOFieldAttr fieldFromDs = sourcefeatds.GetField(i); desfeatds.AddField(fieldFromDs); } //向新的要素集写入数据 for (int i = 0; i < sourcefeatures.Length; i++) { GSOFeature featureInDs = sourcefeatures[i];//sourcefeatds.GetFeatureByName();//.GetFeatureAt(i); GSOFeature featureInTargetDs = featureInDs.Clone(); featureInDs.Dispose(); desfeatds.AddFeature(featureInTargetDs); } desfeatds.Save(); desfeatds.Close(); sourcefeatds.Close(); MessageBox.Show("复制成功!", "提示"); //重新连接数据库,添加图层 dsdes.IsCloseSaved = true; dsdes = globecontrol.Globe.DataManager.OpenOracleDataSource(Utility.sgdbip.Trim() + "/" + Utility.sgdbname.Trim(), "", "", Utility.sgdbuser, Utility.sgdbpwd); GSOLayer sourceLayer2 = globecontrol.Globe.Layers.GetLayerByCaption(listBox2.Text.Trim()); if (sourceLayer2 != null) { globecontrol.Globe.Layers.Remove(sourceLayer2); } desfeatds.Caption = desfeatds.Name; GSOLayer layerRuku = globecontrol.Globe.Layers.Add(desfeatds); // layer.Caption = layer.Name; //GSOLayer layerRuku = globecontrol.Globe.Layers.Add(layer); //rukuLayer = layerRuku; } /// <summary> /// 创建数据库表对象 /// </summary> /// <param name="name"></param> /// <returns></returns> private GSOFeatureDataset CreateDBFeatureDataset(string name) { GSOLayer layer = globecontrol.Globe.Layers.GetLayerByCaption(""); if (layer == null) { return null; } GSOFeatures features = layer.GetAllFeatures(); if (features.Length == 0) { MessageBox.Show("图层要素个数为0!", "提示"); return null; } GSOFeatureDataset featDs = dsdes.CreateFeatureDataset(name); if (featDs == null) { MessageBox.Show("图层名称不符合要求!", "提示"); return null; } for (int i = 0; i < features[0].GetFieldCount(); i++) { GSOFieldDefn fielddef = features[0].GetFieldDefn(i); if (fielddef == null) { continue; } GSOFieldAttr field = new GSOFieldAttr(); field.Name = en_cns.ContainsKey(fielddef.Name) ? en_cns[fielddef.Name].ToString() : fielddef.Name; switch (fielddef.Type) { case EnumFieldType.Text: field.Type = EnumFieldType.Text; field.Width = 8000; break; case EnumFieldType.INT32: field.Type = EnumFieldType.INT32; field.Width = 4; break; case EnumFieldType.Double: field.Type = EnumFieldType.Double; field.Width = 8; break; case EnumFieldType.Date: field.Type = EnumFieldType.Date; field.Width = 10; break; } bool res = featDs.AddField(field); } featDs.Save(); return featDs; } } }