frmInitializeImageboundary.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. using OTSMeasureApp._0_OTSModel.OTSDataType;
  2. using OTSModelSharp.ServiceCenter;
  3. using System;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. namespace OTSMeasureApp._4_OTSSamplespaceGraphicsPanel
  9. {
  10. public partial class frmInitializeImageboundary : Form
  11. {
  12. ISemController cfun;
  13. public PointF FirstPoint;
  14. public PointF SecondPoint;
  15. public bool IsInclinedPlaneFocus = false;
  16. OTSCommon.Language lan;
  17. Hashtable table;
  18. public PointF ThirdPoint;
  19. CSlopFocusParam slopFocusParam;
  20. public frmInitializeImageboundary()
  21. {
  22. InitializeComponent();
  23. lan = new OTSCommon.Language(this);
  24. table = lan.GetNameTable(this.Name);
  25. }
  26. private void bn_FirstPoint_Click(object sender, EventArgs e)
  27. {
  28. double Px = 0;
  29. double Py = 0;
  30. double Pr = 0;
  31. if (cfun.GetSemPositionXY(ref Px, ref Py, ref Pr))
  32. {
  33. tB_FirstPointX.Text = Math.Round(Px,2).ToString();
  34. tB_FirstPointY.Text = Math.Round(Py, 2).ToString();
  35. }
  36. double WD = 0;
  37. if (cfun.GetWorkingDistance(ref WD))
  38. {
  39. tB_FirstPointD.Text = WD.ToString();
  40. }
  41. if (panel1 != null) panel1.Invalidate();
  42. }
  43. private void bn_SecondPoint_Click(object sender, EventArgs e)
  44. {
  45. double Px = 0;
  46. double Py = 0;
  47. double Pr = 0;
  48. if (cfun.GetSemPositionXY(ref Px, ref Py, ref Pr))
  49. {
  50. tB_SecondPointX.Text = Math.Round(Px, 2).ToString();
  51. tB_SecondPointY.Text = Math.Round(Py, 2).ToString();
  52. }
  53. double WD = 0;
  54. if (cfun.GetWorkingDistance(ref WD))
  55. {
  56. tB_SecondPointD.Text = WD.ToString();
  57. }
  58. if (panel1 != null) panel1.Invalidate();
  59. }
  60. private void frmInitializeImageboundary_Load(object sender, EventArgs e)
  61. {
  62. if (cfun == null) { cfun = SemController.GetSEMController(); }
  63. cB_Inclinedplanefocus.Checked = true; double val = 0;
  64. //if (cfun.GetMagnification(ref val)) tBx_Magnification.Text = val.ToString();
  65. if (panel1 != null)
  66. {
  67. panel1.Paint += panel1_Paint;
  68. panel1.MouseMove += panel1_MouseMove;
  69. panel1.MouseLeave += panel1_MouseLeave;
  70. panel1.Resize += (s, args) => {
  71. InitializeImageBoundaryDrawingAreas();
  72. panel1.Invalidate();
  73. };
  74. }
  75. InitializeImageBoundaryDrawingAreas();
  76. panel1.Invalidate();
  77. }
  78. private void bn_ThirdPoint_Click(object sender, EventArgs e)
  79. {
  80. double Px = 0;
  81. double Py = 0;
  82. double Pr = 0;
  83. if (cfun.GetSemPositionXY(ref Px, ref Py, ref Pr))
  84. {
  85. tB_ThirdPointX.Text = Math.Round(Px, 2).ToString();
  86. tB_ThirdPointY.Text = Math.Round(Py, 2).ToString();
  87. }
  88. double WD = 0;
  89. if (cfun.GetWorkingDistance(ref WD))
  90. {
  91. tB_ThirdPointD.Text = WD.ToString();
  92. }
  93. if (panel1 != null) panel1.Invalidate();
  94. }
  95. private void bn_gtfirst_Click(object sender, EventArgs e)
  96. {
  97. if (tB_FirstPointD.Text == "")
  98. {
  99. MessageBox.Show("Please read first!");
  100. return;
  101. }
  102. double Px = Convert.ToDouble(tB_FirstPointX.Text);
  103. double Py = Convert.ToDouble(tB_FirstPointY.Text);
  104. double wd = Convert.ToDouble(tB_FirstPointD.Text);
  105. if (cfun.MoveSEMToPoint(Px, Py))
  106. {
  107. cfun.SetWorkingDistance(wd);
  108. }
  109. }
  110. private void bn_gtsecond_Click(object sender, EventArgs e)
  111. {
  112. if (tB_SecondPointD.Text == "")
  113. {
  114. MessageBox.Show("Please read first!");
  115. return;
  116. }
  117. double Px = Convert.ToDouble(tB_SecondPointX.Text);
  118. double Py = Convert.ToDouble(tB_SecondPointY.Text);
  119. double wd = Convert.ToDouble(tB_SecondPointD.Text);
  120. if (cfun.MoveSEMToPoint(Px, Py))
  121. {
  122. cfun.SetWorkingDistance(wd);
  123. }
  124. }
  125. private void bn_gtthird_Click(object sender, EventArgs e)
  126. {
  127. if (tB_ThirdPointD.Text == "")
  128. {
  129. MessageBox.Show("Please read first!");
  130. return;
  131. }
  132. double Px = Convert.ToDouble(tB_ThirdPointX.Text);
  133. double Py = Convert.ToDouble(tB_ThirdPointY.Text);
  134. double wd = Convert.ToDouble(tB_ThirdPointD.Text);
  135. if (cfun.MoveSEMToPoint(Px, Py))
  136. {
  137. cfun.SetWorkingDistance(wd);
  138. }
  139. }
  140. private void bn_OK_Click(object sender, EventArgs e)
  141. {
  142. double iPX1, iPY1, iPX2, iPY2, iPX3, iPY3;
  143. iPX1 = iPY1 = iPX2 = iPY2 = iPX3 = iPY3 = 0;
  144. double dPW1, dPW2, dPW3;
  145. dPW1 = dPW2 = dPW3 = 0;
  146. if (!double.TryParse(tB_FirstPointX.Text, out iPX1) || !double.TryParse(tB_FirstPointY.Text, out iPY1) || !double.TryParse(tB_SecondPointX.Text, out iPX2) || !double.TryParse(tB_SecondPointY.Text, out iPY2) || !double.TryParse(tB_FirstPointD.Text, out dPW1) || !double.TryParse(tB_SecondPointD.Text, out dPW2))
  147. {
  148. DialogResult result = MessageBox.Show("Please check Params!");
  149. this.DialogResult = DialogResult.None;
  150. return;
  151. }
  152. if (tB_FirstPointX.Text == tB_SecondPointX.Text && tB_FirstPointY.Text == tB_SecondPointY.Text)
  153. {
  154. DialogResult result = MessageBox.Show("Please select different points!");
  155. this.DialogResult = DialogResult.None;
  156. return;
  157. }
  158. if (!double.TryParse(tB_ThirdPointX.Text, out iPX3) || !double.TryParse(tB_ThirdPointY.Text, out iPY3) || !double.TryParse(tB_ThirdPointD.Text, out dPW3))
  159. {
  160. MessageBox.Show("Please check Params!");
  161. this.DialogResult = DialogResult.None;
  162. return;
  163. }
  164. if ((tB_SecondPointX.Text == tB_ThirdPointX.Text && tB_FirstPointY.Text == tB_ThirdPointY.Text) || (tB_SecondPointX.Text == tB_ThirdPointX.Text && tB_SecondPointY.Text == tB_ThirdPointY.Text))
  165. {
  166. MessageBox.Show("Please check Params!");
  167. this.DialogResult = DialogResult.None;
  168. return;
  169. }
  170. FirstPoint = new PointF((float)iPX1, (float)iPY1);
  171. SecondPoint = new PointF((float)iPX2, (float)iPY2);
  172. ThirdPoint = new PointF((float)iPX3, (float)iPY3);
  173. if (slopFocusParam == null)
  174. {
  175. slopFocusParam = new CSlopFocusParam();
  176. }
  177. slopFocusParam.IsUsingSlopParam = cB_Inclinedplanefocus.Checked;
  178. slopFocusParam.FirstPoint = new Point((int)iPX1, (int)iPY1);
  179. slopFocusParam.FirstWD = dPW1;
  180. slopFocusParam.SecondPoint = new Point((int)iPX2, (int)iPY2);
  181. slopFocusParam.SecondWD = dPW2;
  182. slopFocusParam.ThirdPoint = new Point((int)iPX3, (int)iPY3);
  183. slopFocusParam.ThirdWD = dPW3;
  184. this.DialogResult=DialogResult.OK;
  185. this.Close();
  186. }
  187. private void bn_Cancel_Click(object sender, EventArgs e)
  188. {
  189. this.Close();
  190. }
  191. public CSlopFocusParam GetCSlopFocusParam()
  192. {
  193. return slopFocusParam;
  194. }
  195. public double GetMagnification()
  196. {
  197. double dMagnification = 0;
  198. double.TryParse(tBx_Magnification.Text, out dMagnification);
  199. return dMagnification;
  200. }
  201. private void cB_Inclinedplanefocus_CheckedChanged(object sender, EventArgs e)
  202. {
  203. IsInclinedPlaneFocus = cB_Inclinedplanefocus.Checked;
  204. }
  205. // 主圆形区域
  206. private RectangleF mainCircleArea;
  207. // 三个等距分布在圆边界上的小圆
  208. private RectangleF firstPointCircle;
  209. private RectangleF secondPointCircle;
  210. private RectangleF thirdPointCircle;
  211. // 鼠标悬停状态
  212. private bool isMouseOverFirstPoint = false;
  213. private bool isMouseOverSecondPoint = false;
  214. private bool isMouseOverThirdPoint = false;
  215. private void InitializeImageBoundaryDrawingAreas()
  216. {
  217. if (panel1 == null) return;
  218. // 定义主圆形区域(居中显示)
  219. int margin = 50;
  220. float diameter = Math.Min(panel1.Width, panel1.Height) - 2 * margin;
  221. mainCircleArea = new RectangleF(
  222. (panel1.Width - diameter) / 2,
  223. (panel1.Height - diameter) / 2,
  224. diameter,
  225. diameter);
  226. // 在主圆边界上等距分布3个小圆(120度间隔)
  227. float smallCircleDiameter = 18f;
  228. float smallCircleRadius = smallCircleDiameter / 2;
  229. float mainRadius = diameter / 2;
  230. PointF center = new PointF(mainCircleArea.X + mainRadius, mainCircleArea.Y + mainRadius);
  231. float circlePlacementRadius = mainRadius; // 小圆放置在主圆边界上
  232. // 计算三个等距点位置(分布在圆周上)
  233. // 第一个点放在顶部(90度)
  234. firstPointCircle = new RectangleF(
  235. center.X + circlePlacementRadius * (float)Math.Cos(Math.PI / 2) - smallCircleRadius,
  236. center.Y - circlePlacementRadius * (float)Math.Sin(Math.PI / 2) - smallCircleRadius,
  237. smallCircleDiameter, smallCircleDiameter);
  238. // 第二个点放在左下(210度)
  239. secondPointCircle = new RectangleF(
  240. center.X + circlePlacementRadius * (float)Math.Cos(Math.PI / 2 + 2 * Math.PI / 3) - smallCircleRadius,
  241. center.Y - circlePlacementRadius * (float)Math.Sin(Math.PI / 2 + 2 * Math.PI / 3) - smallCircleRadius,
  242. smallCircleDiameter, smallCircleDiameter);
  243. // 第三个点放在右下(330度)
  244. thirdPointCircle = new RectangleF(
  245. center.X + circlePlacementRadius * (float)Math.Cos(Math.PI / 2 + 4 * Math.PI / 3) - smallCircleRadius,
  246. center.Y - circlePlacementRadius * (float)Math.Sin(Math.PI / 2 + 4 * Math.PI / 3) - smallCircleRadius,
  247. smallCircleDiameter, smallCircleDiameter);
  248. }
  249. // 绘制主圆形(绿色)
  250. private void DrawMainCircle(Graphics g, RectangleF circleRect)
  251. {
  252. using (GraphicsPath path = new GraphicsPath())
  253. {
  254. path.AddEllipse(circleRect);
  255. // 创建绿色渐变画刷(简单线性渐变)
  256. using (LinearGradientBrush brush = new LinearGradientBrush(
  257. circleRect,
  258. Color.FromArgb(144, 238, 144), // 浅绿色
  259. Color.FromArgb(34, 139, 34), // 较深绿色
  260. 90f))
  261. {
  262. g.FillPath(brush, path);
  263. }
  264. // 添加外边框
  265. using (Pen pen = new Pen(Color.FromArgb(0, 100, 0), 2)) // 深绿色边框
  266. {
  267. g.DrawEllipse(pen, circleRect);
  268. }
  269. // 添加内边框(浅色)
  270. using (Pen innerPen = new Pen(Color.FromArgb(144, 255, 144), 1)) // 浅绿色内边框
  271. {
  272. RectangleF innerRect = new RectangleF(
  273. circleRect.X + 1,
  274. circleRect.Y + 1,
  275. circleRect.Width - 2,
  276. circleRect.Height - 2);
  277. g.DrawEllipse(innerPen, innerRect);
  278. }
  279. }
  280. }
  281. // 绘制小圆(黄色,高端效果)
  282. private void DrawPointCircle(Graphics g, RectangleF circleRect, bool isHovered)
  283. {
  284. using (GraphicsPath path = new GraphicsPath())
  285. {
  286. path.AddEllipse(circleRect);
  287. // 根据悬停状态选择颜色
  288. Color centerColor = isHovered ? Color.Gold : Color.Yellow;
  289. Color edgeColor = isHovered ? Color.OrangeRed : Color.Goldenrod;
  290. // 创建径向渐变画刷
  291. using (PathGradientBrush brush = new PathGradientBrush(path))
  292. {
  293. brush.CenterColor = centerColor;
  294. brush.SurroundColors = new Color[] { edgeColor };
  295. brush.CenterPoint = new PointF(
  296. circleRect.X + circleRect.Width / 2,
  297. circleRect.Y + circleRect.Height / 2);
  298. brush.FocusScales = new PointF(0.5f, 0.5f);
  299. g.FillPath(brush, path);
  300. }
  301. // 添加高光效果
  302. RectangleF highlightRect = new RectangleF(
  303. circleRect.X + circleRect.Width * 0.15f,
  304. circleRect.Y + circleRect.Height * 0.15f,
  305. circleRect.Width * 0.3f,
  306. circleRect.Height * 0.3f);
  307. using (SolidBrush highlightBrush = new SolidBrush(Color.FromArgb(200, Color.White)))
  308. {
  309. g.FillEllipse(highlightBrush, highlightRect);
  310. }
  311. // 添加边框
  312. using (Pen pen = new Pen(Color.SaddleBrown, isHovered ? 2 : 1))
  313. {
  314. g.DrawEllipse(pen, circleRect);
  315. }
  316. // 鼠标悬停时添加发光效果
  317. if (isHovered)
  318. {
  319. using (Pen glowPen = new Pen(Color.FromArgb(100, Color.Orange), 3))
  320. {
  321. glowPen.Alignment = PenAlignment.Outset;
  322. g.DrawEllipse(glowPen, circleRect);
  323. }
  324. }
  325. }
  326. }
  327. // 绘制坐标值
  328. private void DrawCoordinateText(Graphics g, RectangleF circleRect, string xValue, string yValue)
  329. {
  330. string coordinates = $"({xValue}, {yValue})";
  331. // 使用高质量文本渲染
  332. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  333. using (Font font = new Font("微软雅黑", 9, FontStyle.Bold))
  334. {
  335. SizeF textSize = g.MeasureString(coordinates, font);
  336. // 计算文字位置(放在圆的外侧)
  337. float textX = circleRect.X + circleRect.Width / 2 - textSize.Width / 2;
  338. float textY = circleRect.Y - textSize.Height - 10;
  339. // 绘制坐标文字
  340. using (SolidBrush textBrush = new SolidBrush(Color.Black))
  341. {
  342. g.DrawString(coordinates, font, textBrush, textX, textY);
  343. }
  344. }
  345. }
  346. private void panel1_Paint(object sender, PaintEventArgs e)
  347. {
  348. Graphics g = e.Graphics;
  349. g.SmoothingMode = SmoothingMode.AntiAlias;
  350. g.CompositingQuality = CompositingQuality.HighQuality;
  351. // 确保绘图区域已初始化
  352. if (mainCircleArea.IsEmpty)
  353. {
  354. InitializeImageBoundaryDrawingAreas();
  355. }
  356. DrawMainCircle(g, mainCircleArea);
  357. // 绘制三个等距分布的小圆(黄色)
  358. DrawPointCircle(g, firstPointCircle, isMouseOverFirstPoint);
  359. DrawPointCircle(g, secondPointCircle, isMouseOverSecondPoint);
  360. DrawPointCircle(g, thirdPointCircle, isMouseOverThirdPoint);
  361. // 显示各点坐标值
  362. if (!string.IsNullOrEmpty(tB_FirstPointX.Text) && !string.IsNullOrEmpty(tB_FirstPointY.Text))
  363. {
  364. DrawCoordinateText(g, firstPointCircle, tB_FirstPointX.Text, tB_FirstPointY.Text);
  365. }
  366. if (!string.IsNullOrEmpty(tB_SecondPointX.Text) && !string.IsNullOrEmpty(tB_SecondPointY.Text))
  367. {
  368. DrawCoordinateText(g, secondPointCircle, tB_SecondPointX.Text, tB_SecondPointY.Text);
  369. }
  370. if (!string.IsNullOrEmpty(tB_ThirdPointX.Text) && !string.IsNullOrEmpty(tB_ThirdPointY.Text))
  371. {
  372. DrawCoordinateText(g, thirdPointCircle, tB_ThirdPointX.Text, tB_ThirdPointY.Text);
  373. }
  374. }
  375. private void panel1_MouseMove(object sender, MouseEventArgs e)
  376. {
  377. bool wasFirstOver = isMouseOverFirstPoint;
  378. bool wasSecondOver = isMouseOverSecondPoint;
  379. bool wasThirdOver = isMouseOverThirdPoint;
  380. // 检测鼠标是否在各个小圆内
  381. isMouseOverFirstPoint = IsPointInCircle(e.Location, firstPointCircle);
  382. isMouseOverSecondPoint = IsPointInCircle(e.Location, secondPointCircle);
  383. isMouseOverThirdPoint = IsPointInCircle(e.Location, thirdPointCircle);
  384. // 如果状态发生变化,刷新显示并更新GroupBox高亮
  385. if (wasFirstOver != isMouseOverFirstPoint ||wasSecondOver != isMouseOverSecondPoint ||wasThirdOver != isMouseOverThirdPoint)
  386. {
  387. panel1.Invalidate();
  388. if (wasFirstOver != isMouseOverFirstPoint)
  389. UpdateGroupBoxHighlight(groupBox1, isMouseOverFirstPoint);
  390. if (wasSecondOver != isMouseOverSecondPoint)
  391. UpdateGroupBoxHighlight(groupBox2, isMouseOverSecondPoint);
  392. if (wasThirdOver != isMouseOverThirdPoint)
  393. UpdateGroupBoxHighlight(groupBox3, isMouseOverThirdPoint);
  394. }
  395. }
  396. private void panel1_MouseLeave(object sender, EventArgs e)
  397. {
  398. bool wasFirstOver = isMouseOverFirstPoint;
  399. bool wasSecondOver = isMouseOverSecondPoint;
  400. bool wasThirdOver = isMouseOverThirdPoint;
  401. isMouseOverFirstPoint = false;
  402. isMouseOverSecondPoint = false;
  403. isMouseOverThirdPoint = false;
  404. panel1.Invalidate();
  405. if (wasFirstOver)
  406. UpdateGroupBoxHighlight(groupBox1, false);
  407. if (wasSecondOver)
  408. UpdateGroupBoxHighlight(groupBox2, false);
  409. if (wasThirdOver)
  410. UpdateGroupBoxHighlight(groupBox3, false);
  411. }
  412. private bool IsPointInCircle(Point point, RectangleF circle)
  413. {
  414. if (circle.IsEmpty) return false;
  415. float centerX = circle.X + circle.Width / 2;
  416. float centerY = circle.Y + circle.Height / 2;
  417. float radius = circle.Width / 2;
  418. float distance = (float)Math.Sqrt(Math.Pow(point.X - centerX, 2) +Math.Pow(point.Y - centerY, 2));
  419. return distance <= radius;
  420. }
  421. private void UpdateGroupBoxHighlight(GroupBox groupBox, bool isHighlighted)
  422. {
  423. if (groupBox == null) return;
  424. if (isHighlighted)
  425. {
  426. groupBox.BackColor = Color.FromArgb(240, 255, 240);
  427. groupBox.ForeColor = Color.DarkGreen;
  428. }
  429. else
  430. {
  431. groupBox.BackColor = SystemColors.Control;
  432. groupBox.ForeColor = SystemColors.ControlText;
  433. }
  434. groupBox.Invalidate();
  435. }
  436. }
  437. }