You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

512 lines
20 KiB
C#

1 month ago
using GeoSigma.SigmaDrawerUtil;
using GeoSigmaDrawLib;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using UCDraw;
namespace KevServer
{
public class WebDrawer
{
public Size DrawerSize;
public GeoSigmaXY Geo { get; set; }
public DrawerConfig Configuration
{
get => DrawerConfig.Instance;
}
public GeoLayerTree LayerTree
{
get; set;
}
public string DrawFileName { get; set; }
private Color viewBackColor = Color.White;
public Color ViewBackColor
{
get
{
return viewBackColor;
}
set
{
viewBackColor = value;
Geo?.SetViewBackcolor(viewBackColor.R, viewBackColor.G, viewBackColor.B);
}
}
public Point CacheImgLocation { get; set; } = new Point(0, 0);
public bool EnableRedraw { get; set; } = false;
private WebDrawViewOperator viewOperator;
private bool isNewFile = true;
private WebDrawTool[] tools; // array of tools
private WebDrawer()
{
tools = new WebDrawTool[(int)DrawToolType.NumberOfDrawTools];
//tools[(int)DrawToolType.Default] = new DrawToolDefault();
tools[(int)DrawToolType.Select] = new WebDrawToolSelect();
//tools[(int)DrawToolType.Pointer] = new DrawToolPointer();
//tools[(int)DrawToolType.ViewWindow] = new DrawToolViewWindow();
//tools[(int)DrawToolType.Point] = new DrawToolPoint();
//tools[(int)DrawToolType.CrossPoint] = new DrawToolCrossPoint();
//tools[(int)DrawToolType.Curve] = new DrawToolCurve();
//tools[(int)DrawToolType.Proportion] = new DrawToolProportion();
//tools[(int)DrawToolType.RectangularCSGrid] = new DrawToolRectangularCSGrid();
//tools[(int)DrawToolType.Rectangle] = new DrawToolRectangle();
//tools[(int)DrawToolType.Ellipse] = new DrawToolEllipse();
//tools[(int)DrawToolType.Arc] = new DrawToolArc();
//tools[(int)DrawToolType.Chord] = new DrawToolChord();
//tools[(int)DrawToolType.Pie] = new DrawToolPie();
//tools[(int)DrawToolType.Equilateral] = new DrawToolEquilateral();
//tools[(int)DrawToolType.Text] = new DrawToolText();
//tools[(int)DrawToolType.BreakDirect] = new DrawToolBreakDirect();
//tools[(int)DrawToolType.Break] = new DrawToolBreak();
//tools[(int)DrawToolType.Delete] = new DrawToolDelete();
//tools[(int)DrawToolType.DeleteIn] = new DrawToolDeleteIn();
//tools[(int)DrawToolType.DeleteOut] = new DrawToolDeleteOut();
//tools[(int)DrawToolType.ExtendCurve] = new DrawToolExtendCurve();
//tools[(int)DrawToolType.TrimCurve] = new DrawToolTrimCurve();
//tools[(int)DrawToolType.CloseCurve] = new DrawToolCloseCurve();
//tools[(int)DrawToolType.MoveCurveName] = new DrawToolMoveCurveName();
//tools[(int)DrawToolType.LinkCurve] = new DrawToolLinkCurve();
//tools[(int)DrawToolType.ReverseCurve] = new DrawToolReverseCurve();
//tools[(int)DrawToolType.NameToZ] = new DrawToolNameToZ();
//tools[(int)DrawToolType.ZToName] = new DrawToolZToName();
//tools[(int)DrawToolType.FindCurveOfEmptyName] = new DrawToolFindCurveOfEmptyName();
//tools[(int)DrawToolType.RangeToImage] = new DrawToolCopyRangeAsImage();
//tools[(int)DrawToolType.LeftAlignment] = new DrawToolLeftAlignment();
//tools[(int)DrawToolType.RightAlignment] = new DrawToolRightAlignment();
//tools[(int)DrawToolType.TopAlignment] = new DrawToolTopAlignment();
//tools[(int)DrawToolType.BottomAlignment] = new DrawToolBottomAlignment();
//tools[(int)DrawToolType.HCenterAlignment] = new DrawToolHCenterAlignment();
//tools[(int)DrawToolType.VCenterAlignment] = new DrawToolVCenterAlignment();
//tools[(int)DrawToolType.DisplayOrderTop] = new DrawToolDisplayOrderTop();
//tools[(int)DrawToolType.DisplayOrderBottom] = new DrawToolDisplayOrderBottom();
//tools[(int)DrawToolType.DisplayOrderFront] = new DrawToolDisplayOrderFront();
//tools[(int)DrawToolType.DisplayOrderBack] = new DrawToolDisplayOrderBack();
//tools[(int)DrawToolType.HorizontalMirror] = new DrawToolHorizontalMirror();
//tools[(int)DrawToolType.VerticalMirror] = new DrawToolVerticalMirror();
//tools[(int)DrawToolType.EditCurveNode] = new DrawToolEditCurveNode();
//tools[(int)DrawToolType.CurveSmooth] = new DrawToolCurveSmooth();
//tools[(int)DrawToolType.CutOutByRect] = new DrawToolCutOutByRect();
//tools[(int)DrawToolType.CutOutByPickup] = new DrawToolCutOutByPickup();
//tools[(int)DrawToolType.CutInByRect] = new DrawToolCutInByRect();
//tools[(int)DrawToolType.CutInByPickup] = new DrawToolCutInByPickup();
//tools[(int)DrawToolType.CalculateVolume] = new DrawToolCalculateSurfaceVolume();
//tools[(int)DrawToolType.SetZValueOfSurface] = new DrawToolSurfaceSetZValue();
//tools[(int)DrawToolType.EditGridPoint] = new DrawToolEditGridPoint();
//tools[(int)DrawToolType.PointPropertyClear] = new DrawToolPointPropertyClear();
//tools[(int)DrawToolType.PointPropertySet] = new DrawToolPointPropertySet();
//tools[(int)DrawToolType.CurvePropertyClear] = new DrawToolCurvePropertyClear();
//tools[(int)DrawToolType.CurvePropertySet] = new DrawToolCurvePropertySet();
//tools[(int)DrawToolType.HighLightTip] = new DrawToolHighLightTip();
//tools[(int)DrawToolType.ArcToCurve] = new DrawToolArcToCurve();
//tools[(int)DrawToolType.CurveZFromSurface] = new DrawToolCurveZFromSurface();
//tools[(int)DrawToolType.Calibration2] = new DrawToolCalibrationTwo();
//tools[(int)DrawToolType.Calibration4] = new DrawToolCalibrationFour();
//tools[(int)DrawToolType.ConnectCurveFilling] = new DrawToolFilling();
//tools[(int)DrawToolType.SolidLink] = new DrawToolSolidLink();
//tools[(int)DrawToolType.SolidAuto] = new DrawToolSolidAuto();
//tools[(int)DrawToolType.PositiveNegativeSign] = new DrawToolPositiveNegativeSign();
//tools[(int)DrawToolType.DeleteCondition] = new DrawToolDeletionCondition();
//tools[(int)DrawToolType.SaveAsSymbol] = new DrawToolSaveAsSymbol();
//tools[(int)DrawToolType.FractionPoint] = new DrawToolFractionPoint();
//tools[(int)DrawToolType.CurveNameSetting] = new DrawToolCurveNameSetting();
//tools[(int)DrawToolType.CurveBroaden] = new DrawToolCurveBroaden();
//tools[(int)DrawToolType.FaultSymply] = new DrawToolFaultSymply();
//tools[(int)DrawToolType.SetContourName] = new DrawToolContourName();
//tools[(int)DrawToolType.RangeToImageEx] = new DrawToolCopyRangeAsImageEx();
//tools[(int)DrawToolType.PolygonToImage] = new DrawToolCopyPolygonAsImage();
//tools[(int)DrawToolType.WellGroup] = new DrawToolWellGroup();
//tools[(int)DrawToolType.WellGroupEdit] = new DrawToolWellGroupEdit();
//tools[(int)DrawToolType.EditWellGroupBranchCurveNode] = new DrawToolEditWellBranchCurve();
}
private WebDrawer(int width, int height)
: this()
{
if (width <= 0 || height <= 0)
{
DrawerSize = new Size(1024, 768);
}
else
{
DrawerSize = new Size(width, height);
}
}
public void Dispose()
{
if (Geo != null)
{
Geo.Dispose();
Geo = null;
}
}
private void InitGlobaVariable()
{
// 5000 如果是 double 必须加 D如果类型不匹配会转换失败
double extensionLength = Configuration.FindConfigValue("Extension", "Length", 5000D);
Geo.Global_SetExtendLength(extensionLength);
double xStep = Configuration.FindConfigValue("GridSet", "X", 20D);
double yStep = Configuration.FindConfigValue("GridSet", "Y", 20D);
Geo.SetGridStep(xStep, yStep);
}
private DrawToolType activeTool; // active drawing tool
public DrawToolType ActiveTool
{
get
{
return activeTool;
}
set
{
if (tools == null || activeTool == value)
{
return;
}
//ReDraw();
DrawToolType oldTool = activeTool;
activeTool = value;
// FIXME: 这个对象需要传递一个多边形,暂时没想到好办法,在这里做下特殊处理
if (activeTool == DrawToolType.PolygonToImage)
{
long pos = Geo.SelectGetPolygon();
//(tools[(int)activeTool] as DrawToolCopyPolygonAsImage).PolygonPosition = pos;
}
tools[(int)oldTool]?.End();
if (oldTool != DrawToolType.HighLightTip)
{
// this.tools[(int)DrawToolType.HighLightTip].End();
}
// 默认命令是Default或Select 这取决于上一次选中的是Default还是Select
if (value == DrawToolType.Select
|| value == DrawToolType.Default
|| value == DrawToolType.WellGroupEdit)
{
//defaultTool = value;
}
if (value == DrawToolType.Finsih)
{
//activeTool = defaultTool;
}
//activeTool = value;
DrawItemType itemType = tools[(int)activeTool].ItemType;
// ChangeContextMenu(activeTool);
if (Geo == null)
{
return;
}
if (itemType == DrawItemType.ITEM_VIEW_PAN || itemType == DrawItemType.ITEM_VIEW_WINDOW)
{
// Geo.SetViewPan();
// this.Invalidate();
}
else
{
//Geo.SetItem((int)itemType);
//tools[(int)activeTool].SigmaDrawer = this;
//tools[(int)activeTool].Start();
if (oldTool == DrawToolType.Select && activeTool != DrawToolType.RangeToImage)
{
}
}
}
}
public static WebDrawer OpenFile(string fileName)
{
var webDrawer = new WebDrawer();
if (webDrawer.OpenFileImpl(fileName) != null)
{
return webDrawer;
}
return null;
}
private GeoSigmaXY OpenFileImpl(string fileName)
{
DrawFileName = fileName;
Console.WriteLine(DrawFileName);
Geo?.Dispose();
Geo = new GeoSigmaXY();
Geo.EnableRotateEdit(false);
foreach (WebDrawTool t in tools)
{
if (t != null)
{
t.Geo = Geo;
//t.Init();
}
}
LayerTree = new GeoLayerTree(this.Geo);
bool bSuccess = Geo.OpenDocument(DrawFileName);
if (!bSuccess)
{
Geo?.Dispose();
Geo = null;
return null;
}
Geo.SetAntiAlias(false);
Geo.SetViewBackcolor(ViewBackColor.R, ViewBackColor.G, ViewBackColor.B);
isNewFile = true;
EnableRedraw = true;
InitGlobaVariable();
viewOperator = new WebDrawViewOperator(Geo);
return Geo;
}
/// <summary>
/// 保存文件.
/// </summary>
/// <returns>A bool.</returns>
public bool SaveFile()
{
if (this.Geo == null)
{
return false;
}
return this.Geo.SaveFile();
}
public void OnPaint(Graphics g)
{
try
{
Graphics gMain = g;
if (Geo == null)
{
ClearGraphics(gMain);
return;
}
Rectangle rect = new Rectangle(0, 0, this.DrawerSize.Width, this.DrawerSize.Height);
if (isNewFile)
{
IntPtr pBmpDC = g.GetHdc();
Geo.InitializeImg(pBmpDC, rect.X, rect.Y, rect.Right, rect.Bottom);
Geo.DrawImg(false);
g.ReleaseHdc();
isNewFile = false;
}
else
{
Geo.SetViewRect(rect.Left, rect.Top, rect.Right, rect.Bottom);
IntPtr pBmpDC = g.GetHdc();
Geo.SetImgDC(pBmpDC);
Geo.DrawImg(false);
g.ReleaseHdc(pBmpDC);
EnableRedraw = false;
}
//if (PaintingImage != null)
//{
// DrawPaintingImage(gMain);
//}
//if (!(viewOperation != null && viewOperation.IsOnMoving))
//{
// DrawAssistant(gMain);
//}
//DrawerEnd?.Invoke(gMain);
//OnDrawOther(gMain);
}
catch (Exception)
{
//e.Graphics.Clear(ViewBackColor);
Trace.WriteLine("绘制错误,请刷新!");
}
}
private void ClearGraphics(Graphics g)
{
g.Clear(ViewBackColor);
}
public void ZoomIn()
{
EnableRedraw = true;
int zoomMode = Configuration.FindConfigValue("Display", "ZoomMode", 0);
//int zoomMode = int.Parse(config.ReadString("Display", "ZoomMode", "0"));
Geo.SetZoomMode(zoomMode);
Geo.ViewEnlarge();
}
public void ZoomOut()
{
EnableRedraw = true;
int zoomMode = Configuration.FindConfigValue("Display", "ZoomMode", 0);
Geo.SetZoomMode(zoomMode);
Geo.ViewReduce();
// 判断真实坐标范围,超出有效范围就回退,当真实地图缩到非常小时,计算会出问题,主要和浮点精度有关
if (RealViewRectOutOfRange())
{
Geo.Sigma_UndoView();
return;
}
}
public void ViewAll()
{
EnableRedraw = true;
Geo.ViewAll();
}
private bool OutOfRange(double value)
{
const double equatorialCircumference = 40009 * 1000; // 有效范围40009千米赤道周长
return Math.Abs(value) > equatorialCircumference * 100;
}
private bool RealViewRectOutOfRange()
{
double left = 0.0;
double top = 0.0;
double right = 0.0;
double bottom = 0.0;
Geo.GetRealViewRect(ref left, ref top, ref right, ref bottom);
return OutOfRange(left) || OutOfRange(top) || OutOfRange(right) || OutOfRange(bottom);
}
public Bitmap OnLButtonDown(double x, double y)
{
if (this.activeTool != DrawToolType.Select)
{
this.ActiveTool = DrawToolType.Select;
(tools[(int)ActiveTool]).Start();
}
WebDrawToolSelect tool = (tools[(int)DrawToolType.Select]) as WebDrawToolSelect;
Bitmap bmp = tool.OnLButtonDown(new MouseEventArgs(MouseButtons.Left, 1, (int)x, (int)y, 0));
CacheImgLocation = tool.CacheImgLocation;
return bmp;
}
public Bitmap LButtonUp(double x, double y)
{
if (this.activeTool == DrawToolType.Select)
{
WebDrawToolSelect drawToolSelect = (tools[(int)DrawToolType.Select]) as WebDrawToolSelect;
if (drawToolSelect.OnLButtonUp(new MouseEventArgs(MouseButtons.Left, 1, (int)x, (int)y, 0)))
{
Bitmap bmp = new Bitmap(this.DrawerSize.Width, this.DrawerSize.Height);
Graphics g = Graphics.FromImage(bmp);
this.OnPaint(g);
return bmp;
}
}
return null;
}
/// <summary>
/// 移动选择的图元到指定位置.
/// </summary>
/// <param name="x">The x像素坐标.</param>
/// <param name="y">The y像素坐标.</param>
/// <returns>A Bitmap.</returns>
public Bitmap TranslateElements(double x, double y)
{
if (this.activeTool != DrawToolType.Select)
{
return null;
}
WebDrawToolSelect drawToolSelect = (tools[(int)DrawToolType.Select]) as WebDrawToolSelect;
drawToolSelect.MoveElementsTo((int)x, (int)y);
Bitmap bmp = new Bitmap(this.DrawerSize.Width, this.DrawerSize.Height);
Graphics g = Graphics.FromImage(bmp);
this.OnPaint(g);
return bmp;
}
/// <summary>
/// 鼠标移动事件.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>An int.</returns>
public int MouseMove(double x, double y)
{
if (this.activeTool != DrawToolType.Select)
{
return -2;
}
WebDrawToolSelect drawToolSelect = (tools[(int)DrawToolType.Select]) as WebDrawToolSelect;
return drawToolSelect.OnMouseMove(new MouseEventArgs(MouseButtons.Left, 1, (int)x, (int)y, 0));
}
public Bitmap ViewOperator(double startX, double startY, double endX, double endY)
{
SetViewOperationKind(ViewOperationKind.ViewPan);
viewOperator.OnLButtonDown(new MouseEventArgs(MouseButtons.Left, 1, (int)startX, (int)startY, 0));
bool bSuccess = viewOperator.OnLButtonUp(new MouseEventArgs(MouseButtons.Left, 1, (int)endX, (int)endY, 0));
if (bSuccess)
{
Bitmap bmp = new Bitmap(this.DrawerSize.Width, this.DrawerSize.Height);
Graphics g = Graphics.FromImage(bmp);
this.OnPaint(g);
//bmp.Save("C:\\temp\\aa.png");
return bmp;
}
return null;
}
public void SetViewOperationKind(ViewOperationKind kind)
{
if (viewOperator.Kind == kind)
{
return;
}
viewOperator.Kind = kind;
if (kind == ViewOperationKind.Invalid)
{
if (tools[(int)activeTool] == null)
{
//RestoreTool();
}
}
}
public int AddBufferData(string data)
{
return this.Geo.AddBufferData(data);
}
public void DeleteLayers(string[] layers, bool withSublayer = false)
{
this.Geo.DeleteLayer(layers, withSublayer);
}
public static byte[] ConvertBitmapToByteArray(Bitmap bitmap)
{
using (MemoryStream memoryStream = new MemoryStream())
{
// 选择图像格式可以是Png、Jpeg等
bitmap.Save(memoryStream, ImageFormat.Png);
return memoryStream.ToArray();
}
}
}
}