HWndCtrl.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. using System;
  2. using ViewWindow;
  3. using System.Collections;
  4. using HalconDotNet;
  5. using ViewROI.Config;
  6. using System.Collections.Generic;
  7. using System.Windows.Forms;
  8. namespace ViewWindow.Model
  9. {
  10. public delegate void IconicDelegate(int val);
  11. public delegate void FuncDelegate();
  12. /// <summary>
  13. /// This class works as a wrapper class for the HALCON window
  14. /// HWindow. HWndCtrl is in charge of the visualization.
  15. /// You can move and zoom the visible image part by using GUI component
  16. /// inputs or with the mouse. The class HWndCtrl uses a graphics stack
  17. /// to manage the iconic objects for the display. Each object is linked
  18. /// to a graphical context, which determines how the object is to be drawn.
  19. /// The context can be changed by calling changeGraphicSettings().
  20. /// The graphical "modes" are defined by the class GraphicsContext and
  21. /// map most of the dev_set_* operators provided in HDevelop.
  22. /// </summary>
  23. public class HWndCtrl
  24. {
  25. /// <summary>No action is performed on mouse events</summary>
  26. public const int MODE_VIEW_NONE = 10;
  27. /// <summary>Zoom is performed on mouse events</summary>
  28. public const int MODE_VIEW_ZOOM = 11;
  29. /// <summary>Move is performed on mouse events</summary>
  30. public const int MODE_VIEW_MOVE = 12;
  31. /// <summary>Magnification is performed on mouse events</summary>
  32. public const int MODE_VIEW_ZOOMWINDOW = 13;
  33. public const int MODE_INCLUDE_ROI = 1;
  34. public const int MODE_EXCLUDE_ROI = 2;
  35. /// <summary>
  36. /// Constant describes delegate message to signal new image
  37. /// </summary>
  38. public const int EVENT_UPDATE_IMAGE = 31;
  39. /// <summary>
  40. /// Constant describes delegate message to signal error
  41. /// when reading an image from file
  42. /// </summary>
  43. public const int ERR_READING_IMG = 32;
  44. /// <summary>
  45. /// Constant describes delegate message to signal error
  46. /// when defining a graphical context
  47. /// </summary>
  48. public const int ERR_DEFINING_GC = 33;
  49. /// <summary>
  50. /// Maximum number of HALCON objects that can be put on the graphics
  51. /// stack without loss. For each additional object, the first entry
  52. /// is removed from the stack again.
  53. /// </summary>
  54. private const int MAXNUMOBJLIST = 2;//原始值为50 实际上2都可以,因这里只是存储背景图片
  55. private int stateView;
  56. private bool mousePressed = false;
  57. private double startX,startY;
  58. /// <summary>HALCON window</summary>
  59. private HWindowControl viewPort;
  60. /// <summary>
  61. /// Instance of ROIController, which manages ROI interaction
  62. /// </summary>
  63. public ROIController roiManager;
  64. /* dispROI is a flag to know when to add the ROI models to the
  65. paint routine and whether or not to respond to mouse events for
  66. ROI objects */
  67. private int dispROI;
  68. /// <summary>
  69. /// 缩放事件开关
  70. /// </summary>
  71. public bool drawModel = false;
  72. //开启编辑模式
  73. public bool EditModel = true;
  74. /* Basic parameters, like dimension of window and displayed image part */
  75. private int windowWidth;
  76. private int windowHeight;
  77. internal int imageWidth;
  78. internal int imageHeight;
  79. private int[] CompRangeX;
  80. private int[] CompRangeY;
  81. private int prevCompX, prevCompY;
  82. private double stepSizeX, stepSizeY;
  83. /* Image coordinates, which describe the image part that is displayed
  84. in the HALCON window */
  85. public double ImgRow1, ImgCol1, ImgRow2, ImgCol2;
  86. /// <summary>Error message when an exception is thrown</summary>
  87. public string exceptionText = "";
  88. /* Delegates to send notification messages to other classes */
  89. /// <summary>
  90. /// Delegate to add information to the HALCON window after
  91. /// the paint routine has finished
  92. /// </summary>
  93. public FuncDelegate addInfoDelegate;
  94. /// <summary>
  95. /// Delegate to notify about failed tasks of the HWndCtrl instance
  96. /// </summary>
  97. public IconicDelegate NotifyIconObserver;
  98. private HWindow ZoomWindow;
  99. private double zoomWndFactor;
  100. private double zoomAddOn;
  101. private int zoomWndSize;
  102. /// <summary>
  103. /// List of HALCON objects to be drawn into the HALCON window.
  104. /// The list shouldn't contain more than MAXNUMOBJLIST objects,
  105. /// otherwise the first entry is removed from the list.
  106. /// </summary>
  107. private ArrayList HObjImageList;
  108. /// <summary>
  109. /// Instance that describes the graphical context for the
  110. /// HALCON window. According on the graphical settings
  111. /// attached to each HALCON object, this graphical context list
  112. /// is updated constantly.
  113. /// </summary>
  114. private GraphicsContext mGC;
  115. /// <summary>
  116. /// Initializes the image dimension, mouse delegation, and the
  117. /// graphical context setup of the instance.
  118. /// </summary>
  119. /// <param name="view"> HALCON window </param>
  120. protected internal HWndCtrl(HWindowControl view)
  121. {
  122. viewPort = view;
  123. stateView = MODE_VIEW_NONE;
  124. windowWidth = viewPort.Size.Width;
  125. windowHeight = viewPort.Size.Height;
  126. zoomWndFactor = (double)imageWidth / viewPort.Width;
  127. zoomAddOn = Math.Pow(0.9, 5);
  128. zoomWndSize = 150;
  129. /*default*/
  130. CompRangeX = new int[] { 0, 100 };
  131. CompRangeY = new int[] { 0, 100 };
  132. prevCompX = prevCompY = 0;
  133. dispROI = MODE_INCLUDE_ROI;//1;
  134. viewPort.HMouseUp += new HalconDotNet.HMouseEventHandler(this.mouseUp);
  135. viewPort.HMouseDown += new HalconDotNet.HMouseEventHandler(this.mouseDown);
  136. viewPort.HMouseWheel += new HalconDotNet.HMouseEventHandler(this.HMouseWheel);
  137. viewPort.HMouseMove += new HalconDotNet.HMouseEventHandler(this.mouseMoved);
  138. addInfoDelegate = new FuncDelegate(dummyV);
  139. NotifyIconObserver = new IconicDelegate(dummy);
  140. // graphical stack
  141. HObjImageList = new ArrayList(20);
  142. mGC = new GraphicsContext();
  143. mGC.gcNotification = new GCDelegate(exceptionGC);
  144. }
  145. private void HMouseWheel(object sender, HMouseEventArgs e)
  146. {
  147. //关闭缩放事件
  148. if (drawModel)
  149. {
  150. return;
  151. }
  152. double scale;
  153. if (e.Delta > 0)
  154. scale = 0.9;
  155. else
  156. scale = 1 / 0.9;
  157. zoomImage(e.X, e.Y, scale);
  158. }
  159. /// <summary>
  160. /// Read dimensions of the image to adjust own window settings
  161. /// </summary>
  162. /// <param name="image">HALCON image</param>
  163. private void setImagePart(HImage image)
  164. {
  165. string s;
  166. int w,h;
  167. image.GetImagePointer1(out s, out w, out h);
  168. setImagePart(0, 0, h, w);
  169. }
  170. /// <summary>
  171. /// Adjust window settings by the values supplied for the left
  172. /// upper corner and the right lower corner
  173. /// </summary>
  174. /// <param name="r1">y coordinate of left upper corner</param>
  175. /// <param name="c1">x coordinate of left upper corner</param>
  176. /// <param name="r2">y coordinate of right lower corner</param>
  177. /// <param name="c2">x coordinate of right lower corner</param>
  178. private void setImagePart(int r1, int c1, int r2, int c2)
  179. {
  180. ImgRow1 = r1;
  181. ImgCol1 = c1;
  182. ImgRow2 = imageHeight = r2;
  183. ImgCol2 = imageWidth = c2;
  184. System.Drawing.Rectangle rect = viewPort.ImagePart;
  185. rect.X = (int)ImgCol1;
  186. rect.Y = (int)ImgRow1;
  187. rect.Height = (int)imageHeight;
  188. rect.Width = (int)imageWidth;
  189. viewPort.ImagePart = rect;
  190. }
  191. /// <summary>
  192. /// Sets the view mode for mouse events in the HALCON window
  193. /// (zoom, move, magnify or none).
  194. /// </summary>
  195. /// <param name="mode">One of the MODE_VIEW_* constants</param>
  196. protected internal void setViewState(int mode)
  197. {
  198. stateView = mode;
  199. if (roiManager != null)
  200. roiManager.resetROI();
  201. }
  202. /********************************************************************/
  203. private void dummy(int val)
  204. {
  205. }
  206. private void dummyV()
  207. {
  208. }
  209. /*******************************************************************/
  210. private void exceptionGC(string message)
  211. {
  212. exceptionText = message;
  213. NotifyIconObserver(ERR_DEFINING_GC);
  214. }
  215. /// <summary>
  216. /// Paint or don't paint the ROIs into the HALCON window by
  217. /// defining the parameter to be equal to 1 or not equal to 1.
  218. /// </summary>
  219. public void setDispLevel(int mode)
  220. {
  221. dispROI = mode;
  222. }
  223. /****************************************************************************/
  224. /* graphical element */
  225. /****************************************************************************/
  226. private void zoomImage(double x, double y, double scale)
  227. {
  228. //关闭缩放事件
  229. if (drawModel)
  230. {
  231. return;
  232. }
  233. double lengthC, lengthR;
  234. double percentC, percentR;
  235. int lenC, lenR;
  236. percentC = (x - ImgCol1) / (ImgCol2 - ImgCol1);
  237. percentR = (y - ImgRow1) / (ImgRow2 - ImgRow1);
  238. lengthC = (ImgCol2 - ImgCol1) * scale;
  239. lengthR = (ImgRow2 - ImgRow1) * scale;
  240. ImgCol1 = x - lengthC * percentC;
  241. ImgCol2 = x + lengthC * (1 - percentC);
  242. ImgRow1 = y - lengthR * percentR;
  243. ImgRow2 = y + lengthR * (1 - percentR);
  244. lenC = (int)Math.Round(lengthC);
  245. lenR = (int)Math.Round(lengthR);
  246. System.Drawing.Rectangle rect = viewPort.ImagePart;
  247. rect.X = (int)Math.Round(ImgCol1);
  248. rect.Y = (int)Math.Round(ImgRow1);
  249. rect.Width = (lenC > 0) ? lenC : 1;
  250. rect.Height = (lenR > 0) ? lenR : 1;
  251. viewPort.ImagePart = rect;
  252. double _zoomWndFactor = 1;
  253. _zoomWndFactor = scale * zoomWndFactor;
  254. if (zoomWndFactor < 0.01 && _zoomWndFactor < zoomWndFactor)
  255. {
  256. //超过一定缩放比例就不在缩放
  257. resetWindow();
  258. return;
  259. }
  260. if (zoomWndFactor > 100 && _zoomWndFactor > zoomWndFactor)
  261. {
  262. //超过一定缩放比例就不在缩放
  263. resetWindow();
  264. return;
  265. }
  266. zoomWndFactor = _zoomWndFactor;
  267. repaint();
  268. }
  269. /// <summary>
  270. /// Scales the image in the HALCON window according to the
  271. /// value scaleFactor
  272. /// </summary>
  273. public void zoomImage(double scaleFactor)
  274. {
  275. double midPointX, midPointY;
  276. if (((ImgRow2 - ImgRow1) == scaleFactor * imageHeight) &&
  277. ((ImgCol2 - ImgCol1) == scaleFactor * imageWidth))
  278. {
  279. repaint();
  280. return;
  281. }
  282. ImgRow2 = ImgRow1 + imageHeight;
  283. ImgCol2 = ImgCol1 + imageWidth;
  284. midPointX = ImgCol1;
  285. midPointY = ImgRow1;
  286. zoomWndFactor = (double)imageWidth / viewPort.Width;
  287. zoomImage(midPointX, midPointY, scaleFactor);
  288. }
  289. /// <summary>
  290. /// Scales the HALCON window according to the value scale
  291. /// </summary>
  292. public void scaleWindow(double scale)
  293. {
  294. ImgRow1 = 0;
  295. ImgCol1 = 0;
  296. ImgRow2 = imageHeight;
  297. ImgCol2 = imageWidth;
  298. viewPort.Width = (int)(ImgCol2 * scale);
  299. viewPort.Height = (int)(ImgRow2 * scale);
  300. zoomWndFactor = ((double)imageWidth / viewPort.Width);
  301. }
  302. /// <summary>
  303. /// Recalculates the image-window-factor, which needs to be added to
  304. /// the scale factor for zooming an image. This way the zoom gets
  305. /// adjusted to the window-image relation, expressed by the equation
  306. /// imageWidth/viewPort.Width.
  307. /// </summary>
  308. public void setZoomWndFactor()
  309. {
  310. zoomWndFactor = ((double)imageWidth / viewPort.Width);
  311. }
  312. /// <summary>
  313. /// Sets the image-window-factor to the value zoomF
  314. /// </summary>
  315. public void setZoomWndFactor(double zoomF)
  316. {
  317. zoomWndFactor = zoomF;
  318. }
  319. /*******************************************************************/
  320. private void moveImage(double motionX, double motionY)
  321. {
  322. ImgRow1 += -motionY;
  323. ImgRow2 += -motionY;
  324. ImgCol1 += -motionX;
  325. ImgCol2 += -motionX;
  326. System.Drawing.Rectangle rect = viewPort.ImagePart;
  327. rect.X = (int)Math.Round(ImgCol1);
  328. rect.Y = (int)Math.Round(ImgRow1);
  329. viewPort.ImagePart = rect;
  330. repaint();
  331. }
  332. /// <summary>
  333. /// Resets all parameters that concern the HALCON window display
  334. /// setup to their initial values and clears the ROI list.
  335. /// </summary>
  336. protected internal void resetAll()
  337. {
  338. ImgRow1 = 0;
  339. ImgCol1 = 0;
  340. ImgRow2 = imageHeight;
  341. ImgCol2 = imageWidth;
  342. zoomWndFactor = (double)imageWidth / viewPort.Width;
  343. System.Drawing.Rectangle rect = viewPort.ImagePart;
  344. rect.X = (int)ImgCol1;
  345. rect.Y = (int)ImgRow1;
  346. rect.Width = (int)imageWidth;
  347. rect.Height = (int)imageHeight;
  348. viewPort.ImagePart = rect;
  349. if (roiManager != null)
  350. roiManager.reset();
  351. }
  352. protected internal void resetWindow()
  353. {
  354. ImgRow1 = 0;
  355. ImgCol1 = 0;
  356. ImgRow2 = imageHeight;
  357. ImgCol2 = imageWidth;
  358. zoomWndFactor = (double)imageWidth / viewPort.Width;
  359. System.Drawing.Rectangle rect = viewPort.ImagePart;
  360. rect.X = (int)ImgCol1;
  361. rect.Y = (int)ImgRow1;
  362. rect.Width = (int)imageWidth;
  363. rect.Height = (int)imageHeight;
  364. viewPort.ImagePart = rect;
  365. }
  366. /*************************************************************************/
  367. /* Event handling for mouse */
  368. /*************************************************************************/
  369. private void mouseDown(object sender, HalconDotNet.HMouseEventArgs e)
  370. {
  371. //关闭缩放事件
  372. if (drawModel)
  373. {
  374. return;
  375. }
  376. stateView = MODE_VIEW_MOVE;
  377. mousePressed = true;
  378. int activeROIidx = -1;
  379. if (roiManager != null && (dispROI == MODE_INCLUDE_ROI))
  380. {
  381. activeROIidx = roiManager.mouseDownAction(e.X, e.Y);
  382. }
  383. if (activeROIidx == -1)
  384. {
  385. switch (stateView)
  386. {
  387. case MODE_VIEW_MOVE:
  388. startX = e.X;
  389. startY = e.Y;
  390. break;
  391. case MODE_VIEW_NONE:
  392. break;
  393. case MODE_VIEW_ZOOMWINDOW:
  394. activateZoomWindow((int)e.X, (int)e.Y);
  395. break;
  396. default:
  397. break;
  398. }
  399. }
  400. //end of if
  401. }
  402. /*******************************************************************/
  403. private void activateZoomWindow(int X, int Y)
  404. {
  405. double posX, posY;
  406. int zoomZone;
  407. if (ZoomWindow != null)
  408. ZoomWindow.Dispose();
  409. HOperatorSet.SetSystem("border_width", 10);
  410. ZoomWindow = new HWindow();
  411. posX = ((X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width;
  412. posY = ((Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height;
  413. zoomZone = (int)((zoomWndSize / 2) * zoomWndFactor * zoomAddOn);
  414. ZoomWindow.OpenWindow((int)posY - (zoomWndSize / 2), (int)posX - (zoomWndSize / 2),
  415. zoomWndSize, zoomWndSize,
  416. viewPort.HalconID, "visible", "");
  417. ZoomWindow.SetPart(Y - zoomZone, X - zoomZone, Y + zoomZone, X + zoomZone);
  418. repaint(ZoomWindow);
  419. ZoomWindow.SetColor("black");
  420. }
  421. public void raiseMouseup()
  422. {
  423. mousePressed = false;
  424. if (roiManager != null
  425. && (roiManager.activeROIidx != -1)
  426. && (dispROI == MODE_INCLUDE_ROI))
  427. {
  428. roiManager.NotifyRCObserver(ROIController.EVENT_UPDATE_ROI);
  429. }
  430. else if (stateView == MODE_VIEW_ZOOMWINDOW)
  431. {
  432. ZoomWindow.Dispose();
  433. }
  434. }
  435. /*******************************************************************/
  436. private void mouseUp(object sender, HalconDotNet.HMouseEventArgs e)
  437. {
  438. //关闭缩放事件
  439. if (drawModel)
  440. {
  441. return;
  442. }
  443. mousePressed = false;
  444. if (roiManager != null
  445. && (roiManager.activeROIidx != -1)
  446. && (dispROI == MODE_INCLUDE_ROI))
  447. {
  448. roiManager.NotifyRCObserver(ROIController.EVENT_UPDATE_ROI);
  449. }
  450. else if (stateView == MODE_VIEW_ZOOMWINDOW)
  451. {
  452. ZoomWindow.Dispose();
  453. }
  454. }
  455. /*******************************************************************/
  456. private void mouseMoved(object sender, HalconDotNet.HMouseEventArgs e)
  457. {
  458. //关闭缩放事件
  459. if (drawModel)
  460. {
  461. return;
  462. }
  463. double motionX, motionY;
  464. double posX, posY;
  465. double zoomZone;
  466. if (!mousePressed)
  467. return;
  468. if (roiManager != null && (roiManager.activeROIidx != -1) && (dispROI == MODE_INCLUDE_ROI))
  469. {
  470. roiManager.mouseMoveAction(e.X, e.Y,viewPort );
  471. }
  472. else if (stateView == MODE_VIEW_MOVE)
  473. {
  474. viewPort.Cursor = System.Windows.Forms.Cursors.Hand;
  475. motionX = ((e.X - startX));
  476. motionY = ((e.Y - startY));
  477. if (((int)motionX != 0) || ((int)motionY != 0))
  478. {
  479. moveImage(motionX, motionY);
  480. startX = e.X - motionX;
  481. startY = e.Y - motionY;
  482. }
  483. }
  484. else if (stateView == MODE_VIEW_ZOOMWINDOW)
  485. {
  486. HSystem.SetSystem("flush_graphic", "false");
  487. ZoomWindow.ClearWindow();
  488. posX = ((e.X - ImgCol1) / (ImgCol2 - ImgCol1)) * viewPort.Width;
  489. posY = ((e.Y - ImgRow1) / (ImgRow2 - ImgRow1)) * viewPort.Height;
  490. zoomZone = (zoomWndSize / 2) * zoomWndFactor * zoomAddOn;
  491. ZoomWindow.SetWindowExtents((int)posY - (zoomWndSize / 2),
  492. (int)posX - (zoomWndSize / 2),
  493. zoomWndSize, zoomWndSize);
  494. ZoomWindow.SetPart((int)(e.Y - zoomZone), (int)(e.X - zoomZone),
  495. (int)(e.Y + zoomZone), (int)(e.X + zoomZone));
  496. repaint(ZoomWindow);
  497. HSystem.SetSystem("flush_graphic", "true");
  498. ZoomWindow.DispLine(-100.0, -100.0, -100.0, -100.0);
  499. }
  500. }
  501. /// <summary>
  502. /// To initialize the move function using a GUI component, the HWndCtrl
  503. /// first needs to know the range supplied by the GUI component.
  504. /// For the x direction it is specified by xRange, which is
  505. /// calculated as follows: GuiComponentX.Max()-GuiComponentX.Min().
  506. /// The starting value of the GUI component has to be supplied
  507. /// by the parameter Init
  508. /// </summary>
  509. public void setGUICompRangeX(int[] xRange, int Init)
  510. {
  511. int cRangeX;
  512. CompRangeX = xRange;
  513. cRangeX = xRange[1] - xRange[0];
  514. prevCompX = Init;
  515. stepSizeX = ((double)imageWidth / cRangeX) * (imageWidth / windowWidth);
  516. }
  517. /// <summary>
  518. /// To initialize the move function using a GUI component, the HWndCtrl
  519. /// first needs to know the range supplied by the GUI component.
  520. /// For the y direction it is specified by yRange, which is
  521. /// calculated as follows: GuiComponentY.Max()-GuiComponentY.Min().
  522. /// The starting value of the GUI component has to be supplied
  523. /// by the parameter Init
  524. /// </summary>
  525. public void setGUICompRangeY(int[] yRange, int Init)
  526. {
  527. int cRangeY;
  528. CompRangeY = yRange;
  529. cRangeY = yRange[1] - yRange[0];
  530. prevCompY = Init;
  531. stepSizeY = ((double)imageHeight / cRangeY) * (imageHeight / windowHeight);
  532. }
  533. /// <summary>
  534. /// Resets to the starting value of the GUI component.
  535. /// </summary>
  536. public void resetGUIInitValues(int xVal, int yVal)
  537. {
  538. prevCompX = xVal;
  539. prevCompY = yVal;
  540. }
  541. /// <summary>
  542. /// Moves the image by the value valX supplied by the GUI component
  543. /// </summary>
  544. public void moveXByGUIHandle(int valX)
  545. {
  546. double motionX;
  547. motionX = (valX - prevCompX) * stepSizeX;
  548. if (motionX == 0)
  549. return;
  550. moveImage(motionX, 0.0);
  551. prevCompX = valX;
  552. }
  553. /// <summary>
  554. /// Moves the image by the value valY supplied by the GUI component
  555. /// </summary>
  556. public void moveYByGUIHandle(int valY)
  557. {
  558. double motionY;
  559. motionY = (valY - prevCompY) * stepSizeY;
  560. if (motionY == 0)
  561. return;
  562. moveImage(0.0, motionY);
  563. prevCompY = valY;
  564. }
  565. /// <summary>
  566. /// Zooms the image by the value valF supplied by the GUI component
  567. /// </summary>
  568. public void zoomByGUIHandle(double valF)
  569. {
  570. double x, y, scale;
  571. double prevScaleC;
  572. x = (ImgCol1 + (ImgCol2 - ImgCol1) / 2);
  573. y = (ImgRow1 + (ImgRow2 - ImgRow1) / 2);
  574. prevScaleC = (double)((ImgCol2 - ImgCol1) / imageWidth);
  575. scale = ((double)1.0 / prevScaleC * (100.0 / valF));
  576. zoomImage(x, y, scale);
  577. }
  578. /// <summary>
  579. /// Triggers a repaint of the HALCON window
  580. /// </summary>
  581. public void repaint()
  582. {
  583. repaint(viewPort.HalconWindow);
  584. }
  585. /// <summary>
  586. /// Repaints the HALCON window 'window'
  587. /// </summary>
  588. public void repaint(HalconDotNet.HWindow window)
  589. {
  590. try
  591. {
  592. int count = HObjImageList.Count;
  593. HObjectEntry entry;
  594. HSystem.SetSystem("flush_graphic", "false");
  595. window.ClearWindow();
  596. mGC.stateOfSettings.Clear();
  597. //显示图片
  598. for (int i=0; i < count; i++)
  599. {
  600. entry = ((HObjectEntry)HObjImageList[i]);
  601. mGC.applyContext(window, entry.gContext);
  602. window.DispObj(entry.HObj);
  603. }
  604. //显示region
  605. showHObjectList();
  606. addInfoDelegate();
  607. if (roiManager != null && (dispROI == MODE_INCLUDE_ROI))
  608. roiManager.paintData(window,viewPort );
  609. HSystem.SetSystem("flush_graphic", "true");
  610. //注释了下面语句,会导致窗口无法实现缩放和拖动
  611. window.SetColor("black");
  612. window.DispLine(-100.0, -100.0, -101.0, -101.0);
  613. }
  614. catch (Exception)
  615. {
  616. }
  617. }
  618. /********************************************************************/
  619. /* GRAPHICSSTACK */
  620. /********************************************************************/
  621. /// <summary>
  622. /// Adds an iconic object to the graphics stack similar to the way
  623. /// it is defined for the HDevelop graphics stack.
  624. /// </summary>
  625. /// <param name="obj">Iconic object</param>
  626. public void addIconicVar(HObject img)
  627. {
  628. //先把HObjImageList给全部释放了,源代码 会出现内存泄漏问题
  629. for (int i = 0; i < HObjImageList.Count; i++)
  630. {
  631. ((HObjectEntry)HObjImageList[i]).clear();
  632. }
  633. HObjectEntry entry;
  634. if (img == null)
  635. return;
  636. HTuple classValue=null;
  637. HOperatorSet.GetObjClass(img, out classValue);
  638. if (!classValue.S.Equals("image"))
  639. {
  640. return;
  641. }
  642. HImage obj = new HImage(img);
  643. if (obj is HImage)
  644. {
  645. double r, c;
  646. int h, w, area;
  647. string s;
  648. area = ((HImage)obj).GetDomain().AreaCenter(out r, out c);
  649. ((HImage)obj).GetImagePointer1(out s, out w, out h);
  650. if (area == (w * h))
  651. {
  652. clearList();
  653. if ((h != imageHeight) || (w != imageWidth))
  654. {
  655. imageHeight = h;
  656. imageWidth = w;
  657. zoomWndFactor = (double)imageWidth / viewPort.Width;
  658. setImagePart(0, 0, h, w);
  659. }
  660. }//if
  661. }//if
  662. entry = new HObjectEntry(obj, mGC.copyContextList());
  663. HObjImageList.Add(entry);
  664. //每当传入背景图的时候 都清空HObjectList
  665. clearHObjectList();
  666. if (HObjImageList.Count > MAXNUMOBJLIST)
  667. {
  668. //需要自己手动释放
  669. ((HObjectEntry)HObjImageList[0]).clear();
  670. HObjImageList.RemoveAt(1);
  671. }
  672. }
  673. /// <summary>
  674. /// Clears all entries from the graphics stack
  675. /// </summary>
  676. public void clearList()
  677. {
  678. HObjImageList.Clear();
  679. }
  680. /// <summary>
  681. /// Returns the number of items on the graphics stack
  682. /// </summary>
  683. public int getListCount()
  684. {
  685. return HObjImageList.Count;
  686. }
  687. /// <summary>
  688. /// Changes the current graphical context by setting the specified mode
  689. /// (constant starting by GC_*) to the specified value.
  690. /// </summary>
  691. /// <param name="mode">
  692. /// Constant that is provided by the class GraphicsContext
  693. /// and describes the mode that has to be changed,
  694. /// e.g., GraphicsContext.GC_COLOR
  695. /// </param>
  696. /// <param name="val">
  697. /// Value, provided as a string,
  698. /// the mode is to be changed to, e.g., "blue"
  699. /// </param>
  700. public void changeGraphicSettings(string mode, string val)
  701. {
  702. switch (mode)
  703. {
  704. case GraphicsContext.GC_COLOR:
  705. mGC.setColorAttribute(val);
  706. break;
  707. case GraphicsContext.GC_DRAWMODE:
  708. mGC.setDrawModeAttribute(val);
  709. break;
  710. case GraphicsContext.GC_LUT:
  711. mGC.setLutAttribute(val);
  712. break;
  713. case GraphicsContext.GC_PAINT:
  714. mGC.setPaintAttribute(val);
  715. break;
  716. case GraphicsContext.GC_SHAPE:
  717. mGC.setShapeAttribute(val);
  718. break;
  719. default:
  720. break;
  721. }
  722. }
  723. /// <summary>
  724. /// Changes the current graphical context by setting the specified mode
  725. /// (constant starting by GC_*) to the specified value.
  726. /// </summary>
  727. /// <param name="mode">
  728. /// Constant that is provided by the class GraphicsContext
  729. /// and describes the mode that has to be changed,
  730. /// e.g., GraphicsContext.GC_LINEWIDTH
  731. /// </param>
  732. /// <param name="val">
  733. /// Value, provided as an integer, the mode is to be changed to,
  734. /// e.g., 5
  735. /// </param>
  736. public void changeGraphicSettings(string mode, int val)
  737. {
  738. switch (mode)
  739. {
  740. case GraphicsContext.GC_COLORED:
  741. mGC.setColoredAttribute(val);
  742. break;
  743. case GraphicsContext.GC_LINEWIDTH:
  744. mGC.setLineWidthAttribute(val);
  745. break;
  746. default:
  747. break;
  748. }
  749. }
  750. /// <summary>
  751. /// Changes the current graphical context by setting the specified mode
  752. /// (constant starting by GC_*) to the specified value.
  753. /// </summary>
  754. /// <param name="mode">
  755. /// Constant that is provided by the class GraphicsContext
  756. /// and describes the mode that has to be changed,
  757. /// e.g., GraphicsContext.GC_LINESTYLE
  758. /// </param>
  759. /// <param name="val">
  760. /// Value, provided as an HTuple instance, the mode is
  761. /// to be changed to, e.g., new HTuple(new int[]{2,2})
  762. /// </param>
  763. public void changeGraphicSettings(string mode, HTuple val)
  764. {
  765. switch (mode)
  766. {
  767. case GraphicsContext.GC_LINESTYLE:
  768. mGC.setLineStyleAttribute(val);
  769. break;
  770. default:
  771. break;
  772. }
  773. }
  774. /// <summary>
  775. /// Clears all entries from the graphical context list
  776. /// </summary>
  777. public void clearGraphicContext()
  778. {
  779. mGC.clear();
  780. }
  781. /// <summary>
  782. /// Returns a clone of the graphical context list (hashtable)
  783. /// </summary>
  784. public Hashtable getGraphicContext()
  785. {
  786. return mGC.copyContextList();
  787. }
  788. /// <summary>
  789. /// Registers an instance of an ROIController with this window
  790. /// controller (and vice versa).
  791. /// </summary>
  792. /// <param name="rC">
  793. /// Controller that manages interactive ROIs for the HALCON window
  794. /// </param>
  795. protected internal void setROIController(ROIController rC)
  796. {
  797. roiManager = rC;
  798. rC.setViewController(this);
  799. this.setViewState(HWndCtrl.MODE_VIEW_NONE);
  800. }
  801. /// <summary>
  802. /// 添加设定显示的图像
  803. /// </summary>
  804. /// <param name="image"></param>
  805. protected internal void addImageShow(HObject image)
  806. {
  807. addIconicVar(image);
  808. }
  809. #region 再次显示region和 xld
  810. /// <summary>
  811. /// hObjectList用来存储存入的HObject
  812. /// </summary>
  813. private List<HObjectWithColor> hObjectList = new List<HObjectWithColor>();
  814. /// <summary>
  815. /// 默认红颜色显示
  816. /// </summary>
  817. /// <param name="hObj">传入的region.xld,image</param>
  818. public void DispObj(HObject hObj)
  819. {
  820. DispObj(hObj, null);
  821. }
  822. /// <summary>
  823. /// 重新开辟内存保存 防止被传入的HObject在其他地方dispose后,不能重现
  824. /// </summary>
  825. /// <param name="hObj">传入的region.xld,image</param>
  826. /// <param name="color">颜色</param>
  827. public void DispObj(HObject hObj, string color)
  828. {
  829. lock (this)
  830. {
  831. //显示指定的颜色
  832. if (color != null)
  833. {
  834. HOperatorSet.SetColor(viewPort.HalconWindow, color);
  835. }
  836. else
  837. {
  838. HOperatorSet.SetColor(viewPort.HalconWindow, "red");
  839. }
  840. if (hObj != null && hObj.IsInitialized())
  841. {
  842. //
  843. HObject temp = new HObject(hObj);
  844. //
  845. hObjectList.Add(new HObjectWithColor(temp, color));
  846. viewPort.HalconWindow.DispObj(temp);
  847. }
  848. //恢复默认的红色
  849. HOperatorSet.SetColor(viewPort.HalconWindow, "red");
  850. }
  851. }
  852. /// <summary>
  853. /// 每次传入新的背景Image时,清空hObjectList,避免内存没有被释放
  854. /// </summary>
  855. public void clearHObjectList()
  856. {
  857. foreach (HObjectWithColor hObjectWithColor in hObjectList)
  858. {
  859. hObjectWithColor.HObject.Dispose();
  860. }
  861. hObjectList.Clear();
  862. }
  863. /// <summary>
  864. /// 将hObjectList中的HObject,按照先后顺序显示出来
  865. /// </summary>
  866. private void showHObjectList()
  867. {
  868. try
  869. {
  870. foreach (HObjectWithColor hObjectWithColor in hObjectList)
  871. {
  872. if (hObjectWithColor.Color != null)
  873. {
  874. HOperatorSet.SetColor(viewPort.HalconWindow, hObjectWithColor.Color);
  875. }
  876. else
  877. {
  878. HOperatorSet.SetColor(viewPort.HalconWindow, "red");
  879. }
  880. if (hObjectWithColor != null && hObjectWithColor.HObject.IsInitialized())
  881. {
  882. viewPort.HalconWindow.DispObj(hObjectWithColor.HObject);
  883. //恢复默认的红色
  884. HOperatorSet.SetColor(viewPort.HalconWindow, "red");
  885. }
  886. }
  887. }
  888. catch (Exception e)
  889. {
  890. //有时候hobj被dispose了,但是其本身不为null,此时则报错. 已经使用IsInitialized解决了
  891. }
  892. }
  893. #endregion
  894. }//end of class
  895. }//end of namespace