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.

1083 lines
40 KiB
C#

1 month ago
using DevExpress.CodeParser;
using DevExpress.Diagram.Core.Shapes;
using DevExpress.Xpo.DB;
using EmbedIO.WebSockets;
1 month ago
using GeoSigma.SigmaDrawerUtil;
1 month ago
1 month ago
using GeoSigmaDrawLib;
1 month ago
1 month ago
using KevDrawServer.Drawer;
1 month ago
using Swan;
1 month ago
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
1 month ago
using static System.Runtime.CompilerServices.RuntimeHelpers;
1 month ago
namespace KevDrawServer
{
/// <summary>
/// 图形操作类.
/// </summary>
public class EventResponse
{
/// <summary>
/// Initializes a new instance of the <see cref="EventResponse"/> class.
/// </summary>
/// <param name="drawer">The drawer.</param>
/// <param name="context">The context.</param>
public EventResponse(WebDrawer drawer, IWebSocketContext context)
{
1 month ago
this.Drawer = drawer;
this.Context = context;
1 month ago
}
/// <summary>
/// Gets or sets 图形操作实例.
/// </summary>
public WebDrawer Drawer { get; set; } = null;
/// <summary>
/// Gets or sets 连接上下文.
/// </summary>
public IWebSocketContext Context { get; set; } = null;
private string imageFile = "temp/K1.png";
public JsEvent ExcuteOperator(string evtentType, Dictionary<string, object> keyValues)
{
1 month ago
if (this.Drawer == null)
1 month ago
{
return null;
}
object objDrawerWidth = null;
object objDrawerHeight = null;
if (keyValues != null)
{
keyValues.TryGetValue("width", out objDrawerWidth);
keyValues.TryGetValue("height", out objDrawerHeight);
}
if (objDrawerWidth != null && objDrawerHeight != null)
{
double dDrawerWidth = 0;
double dDrawerHeight = 0;
double.TryParse($"{objDrawerWidth}", out dDrawerWidth);
double.TryParse($"{objDrawerHeight}", out dDrawerHeight);
1 month ago
if (dDrawerWidth > 0 && dDrawerHeight > 0)
1 month ago
{
this.Drawer.DrawerSize = new Size((int)dDrawerWidth, (int)dDrawerHeight);
}
}
JsEvent evtResult = null;
1 month ago
int nKeyCode = 0;
1 month ago
switch (evtentType)
{
case "OpenFile":
1 month ago
evtResult = this.ShowImg();
break;
case "EditWellGroup":
evtResult = this.ShowImg();
1 month ago
break;
case "ZoomIn":
1 month ago
evtResult = this.ZoomIn(keyValues);
1 month ago
break;
case "ZoomOut":
1 month ago
evtResult = this.ZoomOut(keyValues);
1 month ago
break;
case "ViewAll":
1 month ago
evtResult = this.ViewAll(keyValues);
1 month ago
break;
case "Refresh":
1 month ago
evtResult = this.ReFresh();
1 month ago
break;
case "Center":
1 month ago
evtResult = this.DrawCenter();
1 month ago
break;
case "ViewPan":
1 month ago
evtResult = this.ViewPan(keyValues);
1 month ago
break;
case "ViewWindow":
1 month ago
evtResult = this.ViewWindow(keyValues);
1 month ago
break;
case "Redraw":
1 month ago
evtResult = this.Redraw();
break;
case "SwitchToolType":
if (this.Drawer != null)
{
this.Drawer.ActiveTool = (UCDraw.DrawToolType)Convert.ToInt32(keyValues["toolType"]);
}
1 month ago
break;
case "MouseDown":
//double dX = Convert.ToDouble(dictionary["x"]);
//double dY = Convert.ToDouble(dictionary["y"]);
//await ClickImageAsync(dX, dY);
break;
case "LButtonUp":
double dUpX = Convert.ToDouble(keyValues["x"]);
double dUpY = Convert.ToDouble(keyValues["y"]);
1 month ago
if (keyValues["keyCode"] != null)
1 month ago
{
nKeyCode = Convert.ToInt32(keyValues["keyCode"]);
}
1 month ago
evtResult = this.LButtonUp(dUpX, dUpY, nKeyCode);
break;
case "SelectRegion": // 区域选择
double dStartX = Convert.ToDouble(keyValues["startX"]);
double dStartY = Convert.ToDouble(keyValues["startY"]);
double dEndX = Convert.ToDouble(keyValues["endX"]);
double dEndY = Convert.ToDouble(keyValues["endY"]);
bool bAdd = false;
if (keyValues["isAdd"] != null)
{
bAdd = Convert.ToBoolean(keyValues["isAdd"]);
}
evtResult = this.SelectRegion(dStartX, dStartY, dEndX, dEndY, bAdd);
break;
case "SelectDragStart": // 拖动选择操作开始
evtResult = this.SelectDragStart();
1 month ago
break;
1 month ago
case "SelectMouseMove":
1 month ago
double dMoveX = Convert.ToDouble(keyValues["x"]);
double dMoveY = Convert.ToDouble(keyValues["y"]);
int nHandle = Convert.ToInt32(keyValues["handleIndex"]);
1 month ago
evtResult = this.SelectMouseMove(dMoveX, dMoveY);
break;
case "SelectTransformElements":
double dDragStartX = Convert.ToDouble(keyValues["startX"]);
double dDragStartY = Convert.ToDouble(keyValues["startY"]);
double dDragEndX = Convert.ToDouble(keyValues["endX"]);
double dDragEndY = Convert.ToDouble(keyValues["endY"]);
if (this.Drawer != null)
{
try
{
Bitmap bmp = this.Drawer.SelectTransformElements(dDragStartX, dDragStartY, dDragEndX, dDragEndY);
evtResult = this.CreateImgEvent(bmp);
}
catch (EmbedIO.WebSockets.WebSocketException ex)
{
Terminal.WriteLine(ex.Message);
}
}
1 month ago
break;
case "DragElement":
double dDragX = Convert.ToDouble(keyValues["x"]);
double dDragY = Convert.ToDouble(keyValues["y"]);
evtResult = this.TranslateElements(dDragX, dDragY);
break;
case "SelectMouseDown":
double dX = Convert.ToDouble(keyValues["x"]);
double dY = Convert.ToDouble(keyValues["y"]);
//await ClickImageAsync(dX, dY);
1 month ago
evtResult = this.SelectLButtonDown(dX, dY);
1 month ago
break;
case "SelectMouseUp":
double dSelectUpX = Convert.ToDouble(keyValues["x"]);
double dSelectUpY = Convert.ToDouble(keyValues["y"]);
int nMouseUpKeyCode = 0;
if (keyValues["keyCode"] != null)
{
nMouseUpKeyCode = Convert.ToInt32(keyValues["keyCode"]);
}
1 month ago
evtResult = this.SelectMouseUp(dSelectUpX, dSelectUpY, nMouseUpKeyCode);
1 month ago
break;
case "GetElementProperty":
1 month ago
evtResult = this.GetSelectElementProperty();
1 month ago
break;
case "SetElementProperty":
long elementID = 0;
string strPropertyData = @"";
strPropertyData = string.Empty;
if (keyValues.ContainsKey("elementID"))
{
long.TryParse($"{keyValues["elementID"]}", out elementID);
}
if (keyValues.ContainsKey("data"))
{
strPropertyData = $"{keyValues["data"]}";
}
1 month ago
evtResult = this.SetSelectElementProperty(strPropertyData);
1 month ago
break;
case "WellGroupClone":
long wellGroupID = 0;
if (keyValues.ContainsKey("elementID"))
{
long.TryParse($"{keyValues["elementID"]}", out elementID);
}
1 month ago
evtResult = this.SelectWellGroupClone(wellGroupID);
1 month ago
break;
case "GetWellGroupData":
1 month ago
evtResult = this.SelectGetWellGroupData();
1 month ago
break;
case "SaveFile":
1 month ago
evtResult = this.SaveFile();
1 month ago
break;
case "GetLayers":
1 month ago
evtResult = this.GetLayersJson(); // GetLayers();
1 month ago
break;
case "GetLayersNorm":
1 month ago
evtResult = this.GetLayers(); // GetLayers();
1 month ago
break;
case "GetElementsCount":
1 month ago
evtResult = this.GetElementsCount(); // GetLayers();
1 month ago
break;
case "SetLayersStatus":
1 month ago
string strData = $"{keyValues["layerData"]}";
evtResult = this.SetLayersStatus(strData);
1 month ago
break;
case "DeleteLayers":
string strLayersData = $"{keyValues["layers"]}";
int nWithChilden = 1;
if (keyValues.ContainsKey("widthChild"))
{
Int32.TryParse($"{keyValues["widthChild"]}", out nWithChilden);
}
evtResult = this.DeleteLayers(strLayersData, nWithChilden == 1);
break;
case "ClearAll":
int needUndo = 0;
string strNeedUndo = $"{keyValues["needUndo"]}";
if (!string.IsNullOrEmpty(strNeedUndo))
{
int.TryParse(strNeedUndo, out needUndo);
}
evtResult = this.ClearAll(needUndo == 1 ? true : false);
break;
case "AddBufferData":
1 month ago
string strBuffer = $"{keyValues["bufferData"]}";
evtResult = this.AddBufferData(strBuffer);
1 month ago
break;
case "AddBasicDraw":
{
1 month ago
string strDrawBuffer = $"{keyValues["bufferData"]}";
string strLayer = $"{keyValues["layer"]}";
1 month ago
bool bCreateBlock = Convert.ToBoolean(keyValues["CreateBlock"]);
1 month ago
evtResult = this.InsertDataBottom(strDrawBuffer, strLayer, bCreateBlock);
1 month ago
}
break;
case "MergeFile": // 合并图件
1 month ago
string strFileName = $"{keyValues["fileName"]}";
evtResult = this.MergeFile(strFileName);
1 month ago
break;
case "QueryRangeScreen2Real":
double dScreenStartX = Convert.ToDouble(keyValues["startX"]);
double dScreenEndX = Convert.ToDouble(keyValues["endX"]);
double dScreenStartY = Convert.ToDouble(keyValues["startY"]);
double dScreenEndY = Convert.ToDouble(keyValues["endY"]);
1 month ago
evtResult = this.QueryRangeScreen2RealX(dScreenStartX, dScreenEndX, dScreenStartY, dScreenEndY);
1 month ago
break;
case "CreateFavorableArea":
string strToken = $"{keyValues["token"]}";
var drawer = GlobalSession.GetData(strToken);
string filename = Path.GetFileNameWithoutExtension(((WebDrawer)drawer).DrawFileName);
double min = Convert.ToDouble(keyValues["min"]);
double max = Convert.ToDouble(keyValues["max"]);
string layer = Convert.ToString(keyValues["layer"]);
string dir = keyValues["dir"].ToString();
1 month ago
evtResult = this.CreateFavorableArea(dir, filename, layer, min, max);
1 month ago
break;
case "MergeFavorableArea":
strToken = $"{keyValues["token"]}";
drawer = GlobalSession.GetData(strToken);
filename = Path.GetFileName(((WebDrawer)drawer).DrawFileName);
dir = keyValues["dir"].ToString();
1 month ago
strFileName = $"{keyValues["fileName"]}";
evtResult = this.MergeFavorableArea(dir, filename, strFileName);
1 month ago
break;
1 month ago
1 month ago
default:
break;
}
return evtResult;
}
public JsEvent ShowImg(string file = "", int w = 0, int h = 0, bool sendContent = false)
{
if (this.Drawer == null)
{
return null;
}
Bitmap bmp = null;
if (w != 0 && h != 0)
{
this.Drawer.DrawerSize = new Size(w, h);
}
1 month ago
1 month ago
bmp = new Bitmap(this.Drawer.DrawerSize.Width, this.Drawer.DrawerSize.Height);
bmp.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
using (Graphics g = Graphics.FromImage(bmp))
{
this.Drawer.OnPaint(g);
}
1 month ago
JsEvent jsEvent = this.CreateImgEvent(bmp, "", sendContent);
if (jsEvent != null)
{
jsEvent.Success = true;
}
1 month ago
return jsEvent;
}
public JsEvent ZoomIn(Dictionary<string, object> keyValues)
{
if (this.Drawer == null)
{
return null;
}
this.Drawer.ZoomIn();
1 month ago
return this.ShowImg();
1 month ago
}
public JsEvent ZoomOut(Dictionary<string, object> keyValues)
{
//this.drawer = GetCurrentDrawer();
if (this.Drawer == null)
{
return null;
}
this.Drawer.ZoomOut();
1 month ago
return this.ShowImg();
1 month ago
}
public JsEvent ViewAll(Dictionary<string, object> keyValues)
{
if (this.Drawer == null)
{
return null;
}
this.Drawer.ViewAll();
1 month ago
return this.ShowImg();
1 month ago
}
/// <summary>
/// 移动浏览
/// </summary>
/// <returns></returns>
public JsEvent ViewPan(Dictionary<string, object> keyValues)
{
if (this.Drawer == null)
{
return null;
}
double dStartX = Convert.ToDouble(keyValues["startX"]);
double dStartY = Convert.ToDouble(keyValues["startY"]);
double dEndX = Convert.ToDouble(keyValues["endX"]);
double dEndY = Convert.ToDouble(keyValues["endY"]);
Bitmap bmp = null;
bmp = this.Drawer.ViewOperatorPan(dStartX, dStartY, dEndX, dEndY);
1 month ago
return this.CreateImgEvent(bmp);
1 month ago
}
/// <summary>
/// 移动浏览
/// </summary>
/// <returns></returns>
public JsEvent ViewWindow(Dictionary<string, object> keyValues)
{
if (this.Drawer == null)
{
return null;
}
double dStartX = Convert.ToDouble(keyValues["startX"]);
double dStartY = Convert.ToDouble(keyValues["startY"]);
double dEndX = Convert.ToDouble(keyValues["endX"]);
double dEndY = Convert.ToDouble(keyValues["endY"]);
1 month ago
1 month ago
Bitmap bmp = this.Drawer.ViewOperatorWindow(dStartX, dStartY, dEndX, dEndY);
1 month ago
return this.CreateImgEvent(bmp);
1 month ago
}
public JsEvent LButtonUp(double x, double y, int keyCode)
{
if (this.Drawer == null)
{
1 month ago
return null;
1 month ago
}
Bitmap bmp = this.Drawer.LButtonUp(x, y, keyCode);
1 month ago
return this.CreateImgEvent(bmp);
}
/// <summary>
/// 选择区域内图元函数,根据起始点和结束点坐标执行区域选择操作
/// </summary>
/// <param name="startX">选择区域的起始点X坐标</param>
/// <param name="startY">选择区域的起始点Y坐标</param>
/// <param name="endX">选择区域的结束点X坐标</param>
/// <param name="endY">选择区域的结束点Y坐标</param>
/// <param name="add">是否是添加状态</param>
/// <returns>返回JsEvent对象表示选择区域操作产生的JavaScript事件</returns>
public JsEvent SelectRegion(double startX, double startY, double endX, double endY, bool add)
{
if (this.Drawer == null)
{
return null;
}
Bitmap bmp = this.Drawer.SelectByRegion(startX, startY, endX, endY, add);
return this.CreateImgEvent(bmp);
1 month ago
}
1 month ago
/// <summary>
/// 获取选择区域图元轮廓
/// </summary>
/// <returns>选择区域轮廓</returns>
public JsEvent SelectDragStart()
{
if (this.Drawer == null)
{
return null;
}
this.Drawer.ActiveTool = UCDraw.DrawToolType.Select;
using (Bitmap bmp = this.Drawer.SelectDragStart())
{
if (bmp == null)
{
return null;
}
bmp.MakeTransparent(Color.White);
Point pt = this.Drawer.CacheImgLocation;
string strFile = string.Empty;
try
{
strFile = Path.Combine(WebConfig.HtmlRoot, this.imageFile);
bmp.Save(strFile);
}
catch (Exception ex)
{
return null;
}
string strEventName = "DragElement";
var responseEvent = new JsEvent(strEventName);
Dictionary<string, object> keyValues = new Dictionary<string, object>();
keyValues.Add("Data", this.imageFile);
keyValues.Add("ImgLeft", pt.X);
keyValues.Add("ImgTop", pt.Y);
keyValues.Add("ImgWidth", bmp.Width);
keyValues.Add("ImgHeight", bmp.Height);
responseEvent.Data = keyValues;
return responseEvent;
}
}
public JsEvent SelectMouseMove(double x, double y)
1 month ago
{
if (this.Drawer == null)
{
return null;
}
int result = 0;
1 month ago
result = this.Drawer.SelectMouseMove(x, y);
1 month ago
1 month ago
// Console.WriteLine($"MouseMove hanle:{result}");
1 month ago
JsEvent responseEvent = new JsEvent("SelectHandle")
{
Data = result,
};
return responseEvent;
}
public JsEvent TranslateElements(double x, double y)
{
if (this.Drawer == null)
{
return null;
}
try
{
Bitmap bmp = this.Drawer.TranslateElements(x, y);
1 month ago
return this.CreateImgEvent(bmp);
1 month ago
}
catch (EmbedIO.WebSockets.WebSocketException ex)
{
//Console.WriteLine($"WebSocketException: {ex.Message}");
}
catch (Exception ex)
{
//Console.WriteLine($"Exception: {ex.Message}");
}
return null;
}
public JsEvent SelectLButtonDown(double x, double y)
{
if (this.Drawer == null)
{
return null;
}
1 month ago
using (Bitmap bmp = this.Drawer.LButtonDown(x, y))
1 month ago
{
1 month ago
if (bmp == null)
{ return null; }
1 month ago
bmp.MakeTransparent(Color.White);
Point pt = this.Drawer.CacheImgLocation;
string strFile = string.Empty;
try
{
1 month ago
strFile = Path.Combine(WebConfig.HtmlRoot, this.imageFile);
1 month ago
bmp.Save(strFile);
}
catch (Exception ex)
{
1 month ago
Terminal.WriteLine($"SendImg: {ex.Message}");
1 month ago
return null;
}
string strEventName = "DragElement";
var responseEvent = new JsEvent(strEventName);
Dictionary<string, object> keyValues = new Dictionary<string, object>();
1 month ago
keyValues.Add("Data", this.imageFile);
1 month ago
keyValues.Add("ImgLeft", pt.X);
keyValues.Add("ImgTop", pt.Y);
1 month ago
keyValues.Add("ImgWidth", bmp.Width);
keyValues.Add("ImgHeight", bmp.Height);
1 month ago
responseEvent.Data = keyValues;
return responseEvent;
}
}
/// <summary>
/// 选中对象
/// </summary>
/// <returns></returns>
public JsEvent SelectMouseUp(double endX, double endY, int keyCode)
{
Bitmap bmp = this.Drawer.LButtonUp(endX, endY, keyCode);
1 month ago
return this.CreateImgEvent(bmp);
1 month ago
}
/// <summary>
/// 井组克隆.
/// </summary>
/// <param name="elementID">The element i d.</param>
/// <returns>A JsEvent.</returns>
1 month ago
public JsEvent SelectWellGroupClone(long elementID = 0)
1 month ago
{
bool bSuccess = this.Drawer.SelectWellGroupClone(elementID);
if (bSuccess == true)
{
1 month ago
return this.Redraw();
1 month ago
}
return null;
}
/// <summary>
/// 获得选中的井组相关数据
/// </summary>
/// <returns></returns>
public JsEvent SelectGetWellGroupData()
{
string strWellGroupData = this.Drawer.SelectGetWellGroupData();
var responseEvent = new JsEvent("WellGroupData");
responseEvent.Data = strWellGroupData;
return responseEvent;
}
1 month ago
1 month ago
/// <summary>
/// 设置选中图元的属性
/// </summary>
/// <param name="propertyData"></param>
/// <returns></returns>
public JsEvent SetSelectElementProperty(string propertyData)
{
bool success = this.Drawer.SetElementProperty(propertyData);
1 month ago
if (success == false)
1 month ago
{
return null;
}
1 month ago
return this.Redraw();
1 month ago
}
public JsEvent GetSelectElementProperty()
{
string strElementData = string.Empty;
long lPosition = 0;
this.Drawer.GetSelectedElement(ref strElementData, ref lPosition);
var responseEvent = new JsEvent("ElementProperty");
Dictionary<string, object> keyValues = new Dictionary<string, object>();
keyValues.Add("ElementID", lPosition);
keyValues.Add("ElementData", strElementData);
responseEvent.Data = keyValues;
return responseEvent;
}
/// <summary>
/// 保存图形.
/// </summary>
/// <returns>A JsEvent.</returns>
public JsEvent SaveFile()
{
bool bSuccess = this.Drawer.SaveFile();
var responseEvent = new JsEvent("Save");
responseEvent.Data = bSuccess;
return responseEvent;
}
private JsEvent CreateImgEvent(Bitmap bmp, string eventName = "", bool sendContent = false)
{
1 month ago
if (bmp == null)
1 month ago
{
return null;
}
byte[] image = null;
string strFile = string.Empty;
try
{
if (sendContent == false)
{
1 month ago
strFile = Path.Combine(WebConfig.HtmlRoot, this.imageFile);
string strPath = Path.GetDirectoryName(strFile);
if (!Directory.Exists(strPath))
{
Directory.CreateDirectory(strPath);
}
1 month ago
bmp.Save(strFile);
}
else
{
image = ConvertBitmapToByteArray(bmp);
}
}
catch (Exception ex)
{
//Console.WriteLine($"SendImg: {ex.Message}");
return null;
}
string strEventName = eventName;
if (string.IsNullOrEmpty(eventName))
{
strEventName = "Redraw";
}
var responseEvent = new JsEvent(strEventName);
if (sendContent)
{
string base64Header = $"data:image/png;base64,";
responseEvent.Data = (base64Header + Convert.ToBase64String(image));
}
else
{
1 month ago
responseEvent.Data = this.imageFile;
1 month ago
}
return responseEvent;
}
public JsEvent Redraw()
{
return this.ShowImg();
}
/// <summary>
/// Refresh-刷新
/// </summary>
/// <returns></returns>
public JsEvent ReFresh()
{
this.Drawer.Geo.ReloadFile();
return this.ShowImg();
}
/// <summary>
/// ReCenter -全图居中
/// </summary>
/// <returns></returns>
public JsEvent DrawCenter()
{
this.Drawer.Geo.ReloadFile();
return this.ShowImg();
}
/// <summary>
/// Gets 所有图元的数量.
/// </summary>
/// <returns>A JsEvent.</returns>
public JsEvent GetElementsCount()
{
var responseEvent = new JsEvent("GetElementsCount");
int nCount = this.Drawer.Geo.GetElementCount();
responseEvent.Data = nCount;
return responseEvent;
}
#region 图层管理
/// <summary>
/// 获得图层数据.获取层位字符串(常规格式)
/// </summary>
/// <returns>A Task.</returns>
public JsEvent GetLayers()
{
var responseEvent = new JsEvent("ReloadLayer")
{
Data = this.Drawer.LayerTree.Layers
};
return responseEvent;
}
/// <summary>
/// 获取图层获得JSON结构化图层数据.
/// </summary>
/// <returns>A Task.</returns>
public JsEvent GetLayersJson()
{
var responseEvent = new JsEvent("ReloadLayer")
{
Data = this.Drawer.LayerTree.LayersJson
};
// responseEvent.Data = this.Drawer.LayerTree.LayersJson;
return responseEvent;
}
/// <summary>
/// Sets the layers status.
/// </summary>
/// <param name="layersStatus">The layers status.
/// VIEW_EDIT = 10,
/// VIEW_NOT_EDIT = 11,
/// NOT_VIEW_NOT_EDIT = 12</param>
/// <returns>A Task.</returns>
public JsEvent SetLayersStatus(string layersStatus)
{
GeoLayerTree layerTree = this.Drawer?.LayerTree;
if (layerTree.SetLayersStatus(layersStatus))
{
1 month ago
return this.ShowImg();
1 month ago
}
return null;
}
/// <summary>
/// 删除图层
/// </summary>
/// <param name="layers"></param>
/// <param name="withSubLayer"></param>
/// <returns></returns>
public JsEvent DeleteLayers(string layers, bool withSubLayer)
{
var layerArray = layers.Split(new string[] { Environment.NewLine, "," }, StringSplitOptions.RemoveEmptyEntries);
int nCount = layerArray.Length;
if (nCount == 0)
{
return null;
}
for (int i = 0; i < nCount; i++)
{
int nIndexFind = layerArray[i].IndexOf("Layer:\\", 0, StringComparison.CurrentCultureIgnoreCase);
if (nIndexFind >= 0)
{
1 month ago
layerArray[i] = layerArray[i].Remove(0, 7 + nIndexFind);
1 month ago
}
}
this.Drawer.DeleteLayers(layerArray, withSubLayer);
return this.ShowImg();
//var responseEvent = new JsEvent("DeleteLayers")
//{
// Data = string.Empty,
//};
//await SendTargetedEvent(socketContex, responseEvent).ConfigureAwait(true);
//await this.ShowImgAsync();
}
public JsEvent ClearAll(bool needUndo)
{
bool bSuccess = this.Drawer.ClearAll(needUndo);
var responseEvent = new JsEvent("ClearAll")
{
Data = bSuccess,
};
return responseEvent;
}
#endregion
#region 合并图件
/// <summary>
/// 合并图件
/// </summary>
/// <param name="fileName">图件地址</param>
/// <returns></returns>
public JsEvent MergeFile(string fileName)
{
DrawerConfig.StartupPath = WebConfig.StartupPath;
//string strServerPath = WebConfig.MapDirectory;// 获取配置文件中的MapDirectory目录
1 month ago
// string strMapFile = Path.Combine(strServerPath, fileName);
1 month ago
if (!File.Exists(fileName))
{
var responseEventError = new JsEvent("MergeFile")
{
Data = false,
};
return responseEventError;
}
bool isSuccess = this.Drawer.MergeFile(fileName);
var responseEvent = new JsEvent("MergeFile")
{
Data = isSuccess,
};
return responseEvent;
}
#endregion
#region 数据操作
/// <summary>
/// 添加数据.
/// </summary>
/// <param name="data">The data.</param>
/// <returns>A Task.</returns>
public JsEvent AddBufferData(string data)
{
int nResult = this.Drawer.AddBufferData(data);
var responseEvent = new JsEvent("AddBufferData")
{
Data = nResult,
};
return responseEvent;
}
public JsEvent InsertDataBottom(string data, string newLayer, bool createBlock)
{
bool nResult = false;
if (createBlock)
{
string strData = $"Layer M {newLayer}{Environment.NewLine}Block{Environment.NewLine}{data}{Environment.NewLine}EndBlock{Environment.NewLine}";
nResult = this.Drawer.InsertDataBottom(data, "");
}
else
{
nResult = this.Drawer.InsertDataBottom(data, newLayer);
}
var responseEvent = new JsEvent("AddBasicDraw")
{
Data = nResult,
};
return responseEvent;
}
#endregion
#region 坐标工具
/// <summary>
/// 屏幕坐标转换为实际坐标.
/// </summary>
/// <param name="startX">The start x.</param>
/// <param name="endX"></param>
/// <param name="startY">The start y.</param>
/// <param name="endY"></param>
/// <returns>A Task.</returns>
public JsEvent QueryRangeScreen2RealX(double startX, double endX, double startY, double endY)
{
double dRectLeft = 0, dRectRight = 0, dRectTop = 0, dRectBottom = 0;
this.Drawer.Geo.GetScreenReal((int)startX, (int)startY, (int)endX, (int)endY
, ref dRectLeft, ref dRectTop, ref dRectRight, ref dRectBottom);
Dictionary<string, object> keyValues = new Dictionary<string, object>();
keyValues.Add("left", dRectLeft);
keyValues.Add("bottom", dRectBottom);
keyValues.Add("right", dRectRight);
keyValues.Add("top", dRectTop);
var responseEvent = new JsEvent("MapRangeReal")
{
Data = keyValues,
};
return responseEvent;
}
/// <summary>
/// 创建有利区
/// </summary>
/// <param name="dir">图件存储目录</param>
/// <param name="layerName">将创建有利区的图层,如果不存在,则会自动创建图层</param>
/// <param name="sourceFileName">原文件名,不带路径</param>
/// <param name="zMin">最小 z 值</param>
/// <param name="zMax">最大 z 值</param>
/// <returns></returns>
1 month ago
public JsEvent CreateFavorableArea(string dir, string sourceFileName, string layerName, double zMin, double zMax)
1 month ago
{
var drawerData = new DrawerData();
this.Drawer.Geo.CreateFavorableArea(drawerData.pXy, "有利区", zMin, zMax);
1 month ago
string filename = $"{sourceFileName}-有利区-{DateTime.Now:yyyyMMddHHmmssff}.kev";
1 month ago
string strMapFile = Path.Combine(dir, filename);
drawerData.SaveAs(strMapFile);
var keyValues = new Dictionary<string, object>()
{
{ "filename", filename },
};
return new JsEvent("CreateFavorableArea")
{
Data = keyValues,
};
}
/// <summary>
/// 合并有利区底图
/// </summary>
/// <param name="dir">目录</param>
/// <param name="sourceFileName">原图文件</param>
/// <param name="bottomFileName">底层</param>
/// <returns></returns>
public JsEvent MergeFavorableArea(string dir, string sourceFileName, string bottomFileName)
{
string filename = $"{Path.GetFileNameWithoutExtension(sourceFileName)}-有利区-{DateTime.Now:HHmmssff}-带底图.kev";
string sourcePath = Path.Combine(dir, sourceFileName);
string targetPath = Path.Combine(dir, filename);
string bottomPath = Path.Combine(dir, bottomFileName);
string type = "MergeFavorableArea";
var drawerData = new DrawerData();
if (!drawerData.OpenFile(sourcePath))
{
1 month ago
return this.ErrorEvent(type, $"打开原文件失败,原文件路径: {sourcePath}");
1 month ago
}
if (!drawerData.MergeFile(bottomPath))
{
1 month ago
return this.ErrorEvent(type, $"合并文件失败,合并文件路径: {bottomPath}");
1 month ago
}
if (!drawerData.SaveAs(targetPath))
{
1 month ago
return this.ErrorEvent(type, $"保存文件失败,保存文件路径: {targetPath}");
1 month ago
}
return new JsEvent(type)
{
Success = true,
Data = new Dictionary<string, object>()
{
{ "filename", $"{Path.GetFileName(targetPath)}" },
}
};
}
private JsEvent ErrorEvent(string type, string message)
{
return new JsEvent(type)
{
Success = false,
Data = new Dictionary<string, object>()
{
{ "error", message },
}
};
}
#endregion
1 month ago
/// <summary>
/// 创建井组编辑模型
/// </summary>
/// <param name="wellGroupData">井组数据</param>
/// <param name="w">默认显示宽度</param>
/// <param name="h">默认显示高度</param>
/// <param name="context">网络请求上下文</param>
/// <param name="token">模型的Token</param>
/// <param name="parentToken">父图形的Token</param>
/// <returns></returns>
public async static Task<string> CreateWellGroupDrawerAsync(string wellGroupData, int w, int h, IWebSocketContext context, string token, string parentToken)
{
WebDrawer drawer = (WebDrawer)GlobalSession.GetData(parentToken);
WellGroupDrawer wellGroupDrawer = new WellGroupDrawer(wellGroupData, w, h, drawer.Geo);
string strToken = wellGroupDrawer.Geo.GetDrawerXy().ToInt64().ToString();
if (!string.IsNullOrEmpty(strToken))
{
GlobalSession.StoreData(token, wellGroupDrawer);
GlobalSession.StoreData($"Lock_{token}", new SemaphoreSlim(1, 1));
}
Logger.Info("CreateWellGroupDrawerAsync:" + token);
await Task.CompletedTask;
return strToken;
}
1 month ago
/// <summary>
/// Creates the drawer.
/// </summary>
/// <param name="file">The file.</param>
/// <param name="w">The w.</param>
/// <param name="h">The h.</param>
/// <param name="context"></param>
public async static Task<string> CreateDrawerAsync(string file, int w, int h, IWebSocketContext context, string token)
{
DrawerConfig.StartupPath = WebConfig.StartupPath;
string strServerPath = WebConfig.MapDirectory;
WebDrawer drawer = new WebDrawer(w, h);
string strMapFile = Path.Combine(strServerPath, file);
//ImageFile = Path.Combine(strServerPath, "K1.png");
string strToken = string.Empty;
1 month ago
await Task.Run(() =>
{
GeoSigmaXY geo = drawer.OpenFile(strMapFile);
if (geo != null)
{
strToken = geo.GetDrawerXy().ToInt64().ToString();
}
});
1 month ago
if (!string.IsNullOrEmpty(strToken))
{
GlobalSession.StoreData(token, drawer);
//Context.Session["DrawerFile"] = file;
GlobalSession.StoreData($"Lock_{token}", new SemaphoreSlim(1, 1));
}
Logger.Info("CreateDrawerAsync:" + token);
return strToken;
}
/// <summary>
/// 资源销毁
/// </summary>
public static async Task CloseDrawerAsync(string token, IWebSocketContext context)
1 month ago
{
1 month ago
SemaphoreSlim drawerLock = (SemaphoreSlim)GlobalSession.GetData($"Lock_{token}");
1 month ago
if (drawerLock == null)
1 month ago
{
return;
}
// 确保在销毁前没有其他线程访问Drawer对象
await drawerLock.WaitAsync();
try
{
WebDrawer drawer = (WebDrawer)GlobalSession.GetData(token);
if (drawer != null)
{
drawer.Dispose(); // 释放资源
1 month ago
1 month ago
GlobalSession.TryRemove(token, out object objDrawer); // 从Session中移除Drawer对象
GlobalSession.TryRemove($"Lock_{token}", out object objLock); // 从Session中移除锁对象
drawer = null;
}
}
finally
{
drawerLock.Release();
drawerLock.Dispose(); // 释放 SemaphoreSlim 资源
}
}
/// <summary>
/// Converts the bitmap to byte array.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <returns>An array of byte.</returns>
public static byte[] ConvertBitmapToByteArray(Bitmap bitmap)
{
if (bitmap == null)
{
return null;
}
using (MemoryStream memoryStream = new MemoryStream())
{
// 选择图像格式可以是Png、Jpeg等
bitmap.Save(memoryStream, ImageFormat.Png);
return memoryStream.ToArray();
}
}
}
}