using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Identity
{
/// <summary>
/// FrmMain 的摘要说明。
/// </summary>
public class FrmMain : System.Windows.Forms.Form
{
//
// *=====================================SuperMap Objects示范工程说明=======================================
// *
// *功能简介:利用SuperwkspManager控件实现对工作空间的管理
// *所用控件:SuperMap控件、SuperWorkspace控件、SuperwkspManager控件、SuperLegend控件
// *所用数据:任意目录下的合法的*.smw文件
// *操作说明:
// * 1、单击“打开工作空间”,选择工作空间文件;
// * 2、实现对地图的基本操作;
// *
// *
// *===================================SuperMap Objects示范工程说明结束=====================================
[STAThread]
static void Main()
{
Application.Run(new FrmMain());
}
private System.Windows.Forms.Panel panelToolBar;
private System.Windows.Forms.Button buttonZoomIn;
private System.Windows.Forms.Button buttonZoomOut;
private System.Windows.Forms.Button buttonViewEntire;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Button buttonTemp;
private System.Windows.Forms.Button buttonPan;
private System.Windows.Forms.Button buttonZoomFree;
private System.Windows.Forms.Panel panelControl;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panelMap;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Splitter splitter2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button buttonOpen;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private AxSuperMapLib.AxSuperMap axSuperMap1;
private AxSuperMapLib.AxSuperWorkspace axSuperWorkspace1;
private AxSuperWkspManagerLib.AxSuperWkspManager axSuperWkspManager1;
private AxSuperLegendLib.AxSuperLegend axSuperLegend1;
private Button button2;
private Button button4;
private Button button3;
private Panel panel3;
private Button button5;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public FrmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain));
this.panelToolBar = new System.Windows.Forms.Panel();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.buttonOpen = new System.Windows.Forms.Button();
this.buttonTemp = new System.Windows.Forms.Button();
this.buttonClose = new System.Windows.Forms.Button();
this.buttonViewEntire = new System.Windows.Forms.Button();
this.buttonZoomFree = new System.Windows.Forms.Button();
this.buttonPan = new System.Windows.Forms.Button();
this.buttonZoomOut = new System.Windows.Forms.Button();
this.buttonZoomIn = new System.Windows.Forms.Button();
this.panelControl = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.axSuperWkspManager1 = new AxSuperWkspManagerLib.AxSuperWkspManager();
this.splitter2 = new System.Windows.Forms.Splitter();
this.panel2 = new System.Windows.Forms.Panel();
this.axSuperLegend1 = new AxSuperLegendLib.AxSuperLegend();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panelMap = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.axSuperWorkspace1 = new AxSuperMapLib.AxSuperWorkspace();
this.axSuperMap1 = new AxSuperMapLib.AxSuperMap();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.button5 = new System.Windows.Forms.Button();
this.panelToolBar.SuspendLayout();
this.panelControl.SuspendLayout();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axSuperWkspManager1)).BeginInit();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axSuperLegend1)).BeginInit();
this.panelMap.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axSuperWorkspace1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.axSuperMap1)).BeginInit();
this.SuspendLayout();
//
// panelToolBar
//
this.panelToolBar.Controls.Add(this.button5);
this.panelToolBar.Controls.Add(this.button4);
this.panelToolBar.Controls.Add(this.button3);
this.panelToolBar.Controls.Add(this.button2);
this.panelToolBar.Controls.Add(this.button1);
this.panelToolBar.Controls.Add(this.buttonOpen);
this.panelToolBar.Controls.Add(this.buttonTemp);
this.panelToolBar.Controls.Add(this.buttonClose);
this.panelToolBar.Controls.Add(this.buttonViewEntire);
this.panelToolBar.Controls.Add(this.buttonZoomFree);
this.panelToolBar.Controls.Add(this.buttonPan);
this.panelToolBar.Controls.Add(this.buttonZoomOut);
this.panelToolBar.Controls.Add(this.buttonZoomIn);
resources.ApplyResources(this.panelToolBar, "panelToolBar");
this.panelToolBar.Name = "panelToolBar";
//
// button4
//
resources.ApplyResources(this.button4, "button4");
this.button4.Name = "button4";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
resources.ApplyResources(this.button3, "button3");
this.button3.Name = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
resources.ApplyResources(this.button2, "button2");
this.button2.Name = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
resources.ApplyResources(this.button1, "button1");
this.button1.Name = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// buttonOpen
//
resources.ApplyResources(this.buttonOpen, "buttonOpen");
this.buttonOpen.Name = "buttonOpen";
this.buttonOpen.Click += new System.EventHandler(this.buttonOpen_Click);
//
// buttonTemp
//
resources.ApplyResources(this.buttonTemp, "buttonTemp");
this.buttonTemp.Name = "buttonTemp";
this.buttonTemp.TabStop = false;
//
// buttonClose
//
resources.ApplyResources(this.buttonClose, "buttonClose");
this.buttonClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonClose.Name = "buttonClose";
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
//
// buttonViewEntire
//
resources.ApplyResources(this.buttonViewEntire, "buttonViewEntire");
this.buttonViewEntire.Name = "buttonViewEntire";
this.buttonViewEntire.Click += new System.EventHandler(this.buttonViewEntire_Click);
//
// buttonZoomFree
//
resources.ApplyResources(this.buttonZoomFree, "buttonZoomFree");
this.buttonZoomFree.Name = "buttonZoomFree";
this.buttonZoomFree.Click += new System.EventHandler(this.buttonZoomFree_Click);
//
// buttonPan
//
resources.ApplyResources(this.buttonPan, "buttonPan");
this.buttonPan.Name = "buttonPan";
this.buttonPan.Click += new System.EventHandler(this.buttonPan_Click);
//
// buttonZoomOut
//
resources.ApplyResources(this.buttonZoomOut, "buttonZoomOut");
this.buttonZoomOut.Name = "buttonZoomOut";
this.buttonZoomOut.Click += new System.EventHandler(this.buttonZoomOut_Click);
//
// buttonZoomIn
//
resources.ApplyResources(this.buttonZoomIn, "buttonZoomIn");
this.buttonZoomIn.Name = "buttonZoomIn";
this.buttonZoomIn.Click += new System.EventHandler(this.buttonZoomIn_Click);
//
// panelControl
//
this.panelControl.Controls.Add(this.panel1);
this.panelControl.Controls.Add(this.splitter2);
this.panelControl.Controls.Add(this.panel2);
resources.ApplyResources(this.panelControl, "panelControl");
this.panelControl.Name = "panelControl";
//
// panel1
//
this.panel1.Controls.Add(this.axSuperWkspManager1);
resources.ApplyResources(this.panel1, "panel1");
this.panel1.Name = "panel1";
//
// axSuperWkspManager1
//
resources.ApplyResources(this.axSuperWkspManager1, "axSuperWkspManager1");
this.axSuperWkspManager1.Name = "axSuperWkspManager1";
this.axSuperWkspManager1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSuperWkspManager1.OcxState")));
this.axSuperWkspManager1.LDbClick += new AxSuperWkspManagerLib._DSuperWkspManagerEvents_LDbClickEventHandler(this.axSuperWkspManager1_LDbClick);
//
// splitter2
//
resources.ApplyResources(this.splitter2, "splitter2");
this.splitter2.Name = "splitter2";
this.splitter2.TabStop = false;
//
// panel2
//
this.panel2.Controls.Add(this.axSuperLegend1);
resources.ApplyResources(this.panel2, "panel2");
this.panel2.Name = "panel2";
//
// axSuperLegend1
//
resources.ApplyResources(this.axSuperLegend1, "axSuperLegend1");
this.axSuperLegend1.Name = "axSuperLegend1";
this.axSuperLegend1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSuperLegend1.OcxState")));
//
// splitter1
//
resources.ApplyResources(this.splitter1, "splitter1");
this.splitter1.Name = "splitter1";
this.splitter1.TabStop = false;
//
// panelMap
//
this.panelMap.Controls.Add(this.panel3);
this.panelMap.Controls.Add(this.axSuperWorkspace1);
this.panelMap.Controls.Add(this.axSuperMap1);
resources.ApplyResources(this.panelMap, "panelMap");
this.panelMap.Name = "panelMap";
//
// panel3
//
resources.ApplyResources(this.panel3, "panel3");
this.panel3.Name = "panel3";
//
// axSuperWorkspace1
//
resources.ApplyResources(this.axSuperWorkspace1, "axSuperWorkspace1");
this.axSuperWorkspace1.Name = "axSuperWorkspace1";
this.axSuperWorkspace1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSuperWorkspace1.OcxState")));
//
// axSuperMap1
//
resources.ApplyResources(this.axSuperMap1, "axSuperMap1");
this.axSuperMap1.Name = "axSuperMap1";
this.axSuperMap1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSuperMap1.OcxState")));
//
// button5
//
resources.ApplyResources(this.button5, "button5");
this.button5.Name = "button5";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// FrmMain
//
this.AcceptButton = this.buttonTemp;
resources.ApplyResources(this, "$this");
this.CancelButton = this.buttonClose;
this.Controls.Add(this.panelMap);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panelControl);
this.Controls.Add(this.panelToolBar);
this.Name = "FrmMain";
this.Load += new System.EventHandler(this.FrmMain_Load);
this.panelToolBar.ResumeLayout(false);
this.panelControl.ResumeLayout(false);
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axSuperWkspManager1)).EndInit();
this.panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axSuperLegend1)).EndInit();
this.panelMap.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axSuperWorkspace1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.axSuperMap1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void buttonOpen_Click(object sender, System.EventArgs e)
{
string strFileName;
openFileDialog1.Title = "打开工作空间";
openFileDialog1.Filter = "axSuperMap工作空间 (*.smw)|*.smw|工作空间文件 (*.sxw)|*.sxw,*.smw";
openFileDialog1.InitialDirectory = "..\\..\\..\\Data\\World";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName.ToString()!="")
{
strFileName=openFileDialog1.FileName;
}
else
{
return;
}
button1_Click(sender,e);
axSuperWorkspace1.Open(strFileName,"");
axSuperWkspManager1.Refresh();
}
private void OpenDT(string ds,string dt)
{
SuperMapLib.soDataSource objDs;
SuperMapLib.soDataset objDt;
SuperMapLib.soLayers objLayers;
objDs = axSuperWorkspace1.Datasources[ds];//.OpenDataSource(ds,"map",SuperMapLib.seEngineType.sceSDB,true);
objDt = objDs.Datasets[dt];
objLayers =axSuperMap1.Layers;
objLayers.RemoveAll();
objLayers.AddDataset(objDt,false);
axSuperMap1.Refresh();
axSuperLegend1.Refresh();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objLayers);
objLayers = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(objDs);
objDs = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(objDt);
objDt = null;
}
private void FrmMain_Load(object sender, System.EventArgs e)
{
object obj = this.axSuperWorkspace1.ObjectHandle;
axSuperWkspManager1.Connect(obj);
obj = this.axSuperWorkspace1.ObjectHandle;
axSuperMap1.Connect(obj);
obj = this.axSuperMap1.CtlHandle;
axSuperLegend1.Connect(obj);
axSuperMap1.Action = SuperMapLib.seAction.scaNull;
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
private void button1_Click(object sender, System.EventArgs e)
{
SuperMapLib.soLayers objLayers = axSuperMap1.Layers;
if(objLayers.Count>0)
{
objLayers.RemoveAll();
}
axSuperMap1.Refresh();
axSuperWorkspace1.Close();
axSuperWkspManager1.Refresh();
axSuperLegend1.Refresh();
System.Runtime.InteropServices.Marshal.ReleaseComObject(objLayers);
objLayers = null;
}
private void axSuperWkspManager1_LDbClick(object sender, AxSuperWkspManagerLib._DSuperWkspManagerEvents_LDbClickEvent e)
{
switch(e.nFlag)
{
case SuperMapLib.seSelectedItemFlag.scsDataset:
OpenDT(e.strParent,e.strSelected);
break;
case SuperMapLib.seSelectedItemFlag.scsMap:
axSuperMap1.OpenMap(e.strSelected);
axSuperMap1.ViewEntire();
axSuperMap1.Refresh();
axSuperLegend1.Refresh();
break;
case SuperMapLib.seSelectedItemFlag.scsSymbolLib:
axSuperWorkspace1.Resources.SymbolLib.ShowEditor();
break;
case SuperMapLib.seSelectedItemFlag.scsLineStyleLib:
axSuperWorkspace1.Resources.LineStyleLib.ShowEditor();
break;
case SuperMapLib.seSelectedItemFlag.scsFillStyleLib:
axSuperWorkspace1.Resources.FillStyleLib.ShowEditor();
break;
}
}
private void buttonZoomIn_Click(object sender, System.EventArgs e)
{
this.axSuperMap1.Action = SuperMapLib.seAction.scaZoomIn;
}
private void buttonZoomOut_Click(object sender, System.EventArgs e)
{
this.axSuperMap1.Action = SuperMapLib.seAction.scaZoomOut;
}
private void buttonZoomFree_Click(object sender, System.EventArgs e)
{
this.axSuperMap1.Action = SuperMapLib.seAction.scaZoomFree;
}
private void buttonViewEntire_Click(object sender, System.EventArgs e)
{
this.axSuperMap1.ViewEntire();
}
private void buttonPan_Click(object sender, System.EventArgs e)
{
this.axSuperMap1.Action = SuperMapLib.seAction.scaPan;
}
private void buttonClose_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void axSuperLegend1_Modified(object sender, System.EventArgs e)
{
this.axSuperMap1.Refresh();
}
/// <summary>
/// 修正直角
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
SuperMapLib.soDataSource objDataSource = this.axSuperWorkspace1.Datasources[1];
if (objDataSource == null)
{
MessageBox.Show("数据源为空,不能继续进行!");
return;
}
SuperMapLib.soDataset objDataset = objDataSource.Datasets[1];
if (objDataset == null)
{
MessageBox.Show("数据集为空,不能继续进行!");
return;
}
SuperMapLib.soRecordset objRecordset = null;
if (this.axSuperMap1.selection.Count == 0)
{
objRecordset = ((SuperMapLib.soDatasetVector)objDataset).Query("", true, null, null);
}
else
{
objRecordset = this.axSuperMap1.selection.ToRecordset(false);
}
if (objRecordset == null || objRecordset.RecordCount == 0)
{
MessageBox.Show("数据记录为空,或者没有记录,不能继续进行!");
return;
}
SuperMapLib.soGeometrist objGeometrist=new SuperMapLib.soGeometrist();
int iCount = objRecordset.RecordCount;
for (int i = 1; i <= iCount; i++)
{
SuperMapLib.soGeometry objGeometry = objRecordset.GetGeometry();
SuperMapLib.soGeoRegion objRegion=(SuperMapLib.soGeoRegion)objGeometry;
SuperMapLib.soPoints objPoints = new SuperMapLib.soPoints();
objPoints = objRegion.GetPartAt(1);
SuperMapLib.soPoint objPointStart, objPointEnd, objPointTarget, objPointNew;
int n = objPoints.Count;
//下面代码作用是调整建筑节点位置,以便摆正建筑方向(以最长边的方向确定建筑主朝向)
SuperMapLib.soPoints objPointsSort = new SuperMapLib.soPoints();
double maxL = Math.Sqrt((objPoints[1].x - objPoints[2].x) * (objPoints[1].x - objPoints[2].x) + (objPoints[1].y - objPoints[2].y) * (objPoints[1].y - objPoints[2].y));
int maxID = 1;
for (int s = 1; s < n; s++)
{
double len = Math.Sqrt((objPoints[s].x - objPoints[s + 1].x) * (objPoints[s].x - objPoints[s + 1].x) + (objPoints[s].y - objPoints[s + 1].y) * (objPoints[s].y - objPoints[s + 1].y));
if (len > maxL)
{
maxL = len;
maxID = s;
}
}
if (maxID > 1)
{
for (int s = maxID; s < n; s++)
{
objPointsSort.Add(objPoints[s]);
}
for (int s = 1; s < maxID; s++)
{
objPointsSort.Add(objPoints[s]);
}
objPointsSort.Add(objPoints[maxID - 1]);
objPoints = objPointsSort;
}
//下面实现找垂足点,并重新生成多边形
for (int j = 1; j < n - 1; j++)
{
objPointStart = objPoints[j];
objPointEnd = objPoints[j + 1];
objPointTarget = objPoints[j + 2];
objPointNew = objGeometrist.GetPerpendicularPosition(objPointTarget, objPointStart, objPointEnd);
objPoints[j + 1].x = objPointNew.x;//把终点的X,Y设置成垂足的X,Y
objPoints[j + 1].y = objPointNew.y;
}
SuperMapLib.soGeoRegion objRegionNew = new SuperMapLib.soGeoRegion();
objRegionNew.AddPart(objPoints);
objRecordset.Edit();
bool b = objRecordset.SetGeometry((SuperMapLib.soGeometry)objRegionNew);
objRecordset.Update();
objRecordset.MoveNext();
}
this.axSuperMap1.Refresh();
}
/// <summary>
/// 删除锐角和钝角
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
SuperMapLib.soDataSource objDataSource = this.axSuperWorkspace1.Datasources[1];
if (objDataSource == null)
{
MessageBox.Show("数据源为空,不能继续进行!");
return;
}
SuperMapLib.soDataset objDataset = objDataSource.Datasets[1];
if (objDataset == null)
{
MessageBox.Show("数据集为空,不能继续进行!");
return;
}
SuperMapLib.soRecordset objRecordset = null;
if (this.axSuperMap1.selection.Count == 0)
{
objRecordset = ((SuperMapLib.soDatasetVector)objDataset).Query("", true, null, null);
}
else
{
objRecordset = this.axSuperMap1.selection.ToRecordset(false);
}
if (objRecordset == null || objRecordset.RecordCount == 0)
{
MessageBox.Show("数据记录为空,或者没有记录,不能继续进行!");
return;
}
SuperMapLib.soGeometrist objGeometrist=new SuperMapLib.soGeometrist();
int iCount = objRecordset.RecordCount;
for (int i = 1; i <= iCount; i++)
{
SuperMapLib.soGeometry objGeometry = objRecordset.GetGeometry();
SuperMapLib.soGeoRegion objRegion=(SuperMapLib.soGeoRegion)objGeometry;
SuperMapLib.soPoints objPoints = new SuperMapLib.soPoints();
objPoints = objRegion.GetPartAt(1);
int n = objPoints.Count;
//代码将所有对象中角度小于某个数的锐角或角度大于某个数的钝角删除
double da, db, dc, L, ang;
SuperMapLib.soPoint objPt1, objPt2, objPt3;
ArrayList delList = new ArrayList();
objPt1 = objPoints[n - 1];
objPt2 = objPoints[1];
objPt3 = objPoints[2];
da = Math.Sqrt((objPt1.x - objPt2.x) * (objPt1.x - objPt2.x) + (objPt1.y - objPt2.y) * (objPt1.y - objPt2.y));
db = Math.Sqrt((objPt2.x - objPt3.x) * (objPt2.x - objPt3.x) + (objPt2.y - objPt3.y) * (objPt2.y - objPt3.y));
dc = Math.Sqrt((objPt1.x - objPt3.x) * (objPt1.x - objPt3.x) + (objPt1.y - objPt3.y) * (objPt1.y - objPt3.y));
L = (da * da + db * db - dc * dc) / (2 * da * db);
ang = Math.Acos(L);
if (ang > Math.PI / 2)
{
ang = Math.PI - ang;
}
if (ang < Math.PI / 5)//36度
{
delList.Add(1);
}//上面代码处理第一个角
//下面代码处理从 2,3,4....n个角
for (int k = 2; k < n; k++)
{
objPt1 = objPoints[k - 1];
objPt2 = objPoints[k];
objPt3 = objPoints[k + 1];
da = Math.Sqrt((objPt1.x - objPt2.x) * (objPt1.x - objPt2.x) + (objPt1.y - objPt2.y) * (objPt1.y - objPt2.y));
db = Math.Sqrt((objPt2.x - objPt3.x) * (objPt2.x - objPt3.x) + (objPt2.y - objPt3.y) * (objPt2.y - objPt3.y));
dc = Math.Sqrt((objPt1.x - objPt3.x) * (objPt1.x - objPt3.x) + (objPt1.y - objPt3.y) * (objPt1.y - objPt3.y));
L = (da * da + db * db - dc * dc) / (2 * da * db);
ang = Math.Acos(L);
if (ang > Math.PI / 2)
{
ang = Math.PI - ang;
}
if (ang < Math.PI / 7.2)//25度
{
delList.Add(k);
}
}
if (delList.IndexOf(1) > -1) delList.Add(n);
for (int k = delList.Count - 1; k > -1; k--)
{
objPoints.Remove((int)delList[k], 1);
}
SuperMapLib.soGeoRegion objRegionNew = new SuperMapLib.soGeoRegion();
objRegionNew.AddPart(objPoints);
objRecordset.Edit();
bool b = objRecordset.SetGeometry((SuperMapLib.soGeometry)objRegionNew);
objRecordset.Update();
objRecordset.MoveNext();
}
this.axSuperMap1.Refresh();
}
/// <summary>
/// 删除三角形和多余点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
SuperMapLib.soDataSource objDataSource = this.axSuperWorkspace1.Datasources[1];
if (objDataSource == null)
{
MessageBox.Show("数据源为空,不能继续进行!");
return;
}
SuperMapLib.soDataset objDataset = objDataSource.Datasets[1];
if (objDataset == null)
{
MessageBox.Show("数据集为空,不能继续进行!");
return;
}
SuperMapLib.soRecordset objRecordset = null;
if (this.axSuperMap1.selection.Count == 0)
{
objRecordset = ((SuperMapLib.soDatasetVector)objDataset).Query("", true, null, null);
}
else
{
objRecordset = this.axSuperMap1.selection.ToRecordset(false);
}
if (objRecordset == null || objRecordset.RecordCount == 0)
{
MessageBox.Show("数据记录为空,或者没有记录,不能继续进行!");
return;
}
SuperMapLib.soGeometrist objGeometrist=new SuperMapLib.soGeometrist();
int iCount = objRecordset.RecordCount;
for (int i = 1; i <= iCount; i++)
{
SuperMapLib.soGeometry objGeometry = objRecordset.GetGeometry();
SuperMapLib.soGeoRegion objRegion=(SuperMapLib.soGeoRegion)objGeometry;
SuperMapLib.soPoints objPoints = new SuperMapLib.soPoints();
objPoints = objRegion.GetPartAt(1);
int n = objPoints.Count;
if (n <= 4)//实际上是三角形,将此图形删除
{
bool aa = objRecordset.Delete();
objRecordset.MoveNext();
continue;
}
//下面代码实现删除奇数点多边形中的一个.
double minAngle = 3.0;//只要比Pi/2大就可以了
int minID = 0;
n = objPoints.Count;//因为删除了点,所以重新计算
if (n % 2 == 0)//如果能被2整除,实际上只有n-1条边,那么应该将角度最小或者最大的一个角点删除
{
double dA, dB, dC, l, Ang;
SuperMapLib.soPoint objPoint1, objPoint2, objPoint3;
for (int k = 2; k < n; k++)
{
objPoint1 = objPoints[k - 1];
objPoint2 = objPoints[k];
objPoint3 = objPoints[k + 1];
dA = Math.Sqrt((objPoint1.x - objPoint2.x) * (objPoint1.x - objPoint2.x) + (objPoint1.y - objPoint2.y) * (objPoint1.y - objPoint2.y));
dB = Math.Sqrt((objPoint2.x - objPoint3.x) * (objPoint2.x - objPoint3.x) + (objPoint2.y - objPoint3.y) * (objPoint2.y - objPoint3.y));
dC = Math.Sqrt((objPoint1.x - objPoint3.x) * (objPoint1.x - objPoint3.x) + (objPoint1.y - objPoint3.y) * (objPoint1.y - objPoint3.y));
l = (dA * dA + dB * dB - dC * dC) / (2 * dA * dB);
Ang = Math.Acos(l);
if (Ang > Math.PI / 2)
{
Ang = Math.PI - Ang;
}
if (Ang < minAngle)
{
minAngle = Ang;
minID = k;
}
}
objPoint1 = objPoints[n - 1];
objPoint2 = objPoints[n];
objPoint3 = objPoints[2];
dA = Math.Sqrt((objPoint1.x - objPoint2.x) * (objPoint1.x - objPoint2.x) + (objPoint1.y - objPoint2.y) * (objPoint1.y - objPoint2.y));
dB = Math.Sqrt((objPoint2.x - objPoint3.x) * (objPoint2.x - objPoint3.x) + (objPoint2.y - objPoint3.y) * (objPoint2.y - objPoint3.y));
dC = Math.Sqrt((objPoint1.x - objPoint3.x) * (objPoint1.x - objPoint3.x) + (objPoint1.y - objPoint3.y) * (objPoint1.y - objPoint3.y));
l = (dA * dA + dB * dB - dC * dC) / (2 * dA * dB);
Ang = Math.Acos(l);
if (Ang > Math.PI / 2)
{
Ang = Math.PI - Ang;
}
if (Ang < minAngle)
{
minAngle = Ang;
minID = 1;
}
if (minID == 1)//删除第一点和最后一点,并将第二点添加到最后。
{
objPoints.Remove(1, 1);
objPoints.Remove(n - 1, 1);//不是第n 个,因为前面己移去了一个,所有点向前移了一位
}
else
{
objPoints.Remove(minID, 1);
}
}
SuperMapLib.soGeoRegion objRegionNew = new SuperMapLib.soGeoRegion();
objRegionNew.AddPart(objPoints);
objRecordset.Edit();
bool b = objRecordset.SetGeometry((SuperMapLib.soGeometry)objRegionNew);
objRecordset.Update();
objRecordset.MoveNext();
}
this.axSuperMap1.Refresh();
}
private void button5_Click(object sender, EventArgs e)
{
this.axSuperMap1.Action = SuperMapLib.seAction.scaRectSelect;
}
}
}