GraphicsContext.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.Collections;
  3. using HalconDotNet;
  4. namespace ViewWindow.Model
  5. {
  6. public delegate void GCDelegate(string val);
  7. /// <summary>
  8. /// This class contains the graphical context of an HALCON object. The
  9. /// set of graphical modes is defined by the hashlist 'graphicalSettings'.
  10. /// If the list is empty, then there is no difference to the graphical
  11. /// setting defined by the system by default. Otherwise, the provided
  12. /// HALCON window is adjusted according to the entries of the supplied
  13. /// graphical context (when calling applyContext())
  14. /// </summary>
  15. public class GraphicsContext
  16. {
  17. /// <summary>
  18. /// Graphical mode for the output color (see dev_set_color)
  19. /// </summary>
  20. public const string GC_COLOR = "Color";
  21. /// <summary>
  22. /// Graphical mode for the multi-color output (see dev_set_colored)
  23. /// </summary>
  24. public const string GC_COLORED = "Colored";
  25. /// <summary>
  26. /// Graphical mode for the line width (see set_line_width)
  27. /// </summary>
  28. public const string GC_LINEWIDTH = "LineWidth";
  29. /// <summary>
  30. /// Graphical mode for the drawing (see set_draw)
  31. /// </summary>
  32. public const string GC_DRAWMODE = "DrawMode";
  33. /// <summary>
  34. /// Graphical mode for the drawing shape (see set_shape)
  35. /// </summary>
  36. public const string GC_SHAPE = "Shape";
  37. /// <summary>
  38. /// Graphical mode for the LUT (lookup table) (see set_lut)
  39. /// </summary>
  40. public const string GC_LUT = "Lut";
  41. /// <summary>
  42. /// Graphical mode for the painting (see set_paint)
  43. /// </summary>
  44. public const string GC_PAINT = "Paint";
  45. /// <summary>
  46. /// Graphical mode for the line style (see set_line_style)
  47. /// </summary>
  48. public const string GC_LINESTYLE = "LineStyle";
  49. /// <summary>
  50. /// Hashlist containing entries for graphical modes (defined by GC_*),
  51. /// which is then linked to some HALCON object to describe its
  52. /// graphical context.
  53. /// </summary>
  54. private Hashtable graphicalSettings;
  55. /// <summary>
  56. /// Backup of the last graphical context applied to the window.
  57. /// </summary>
  58. public Hashtable stateOfSettings;
  59. private IEnumerator iterator;
  60. /// <summary>
  61. /// Option to delegate messages from the graphical context
  62. /// to some observer class
  63. /// </summary>
  64. public GCDelegate gcNotification;
  65. /// <summary>
  66. /// Creates a graphical context with no initial
  67. /// graphical modes
  68. /// </summary>
  69. public GraphicsContext()
  70. {
  71. graphicalSettings = new Hashtable(10, 0.2f);
  72. gcNotification = new GCDelegate(dummy);
  73. stateOfSettings = new Hashtable(10, 0.2f);
  74. }
  75. /// <summary>
  76. /// Creates an instance of the graphical context with
  77. /// the modes defined in the hashtable 'settings'
  78. /// </summary>
  79. /// <param name="settings">
  80. /// List of modes, which describes the graphical context
  81. /// </param>
  82. public GraphicsContext(Hashtable settings)
  83. {
  84. graphicalSettings = settings;
  85. gcNotification = new GCDelegate(dummy);
  86. stateOfSettings = new Hashtable(10, 0.2f);
  87. }
  88. /// <summary>Applies graphical context to the HALCON window</summary>
  89. /// <param name="window">Active HALCON window</param>
  90. /// <param name="cContext">
  91. /// List that contains graphical modes for window
  92. /// </param>
  93. public void applyContext(HWindow window, Hashtable cContext)
  94. {
  95. string key = "";
  96. string valS = "";
  97. int valI = -1;
  98. HTuple valH = null;
  99. iterator = cContext.Keys.GetEnumerator();
  100. try
  101. {
  102. while (iterator.MoveNext())
  103. {
  104. key = (string)iterator.Current;
  105. if (stateOfSettings.Contains(key) &&
  106. stateOfSettings[key] == cContext[key])
  107. continue;
  108. switch (key)
  109. {
  110. case GC_COLOR:
  111. valS = (string)cContext[key];
  112. window.SetColor(valS);
  113. if (stateOfSettings.Contains(GC_COLORED))
  114. stateOfSettings.Remove(GC_COLORED);
  115. break;
  116. case GC_COLORED:
  117. valI = (int)cContext[key];
  118. window.SetColored(valI);
  119. if (stateOfSettings.Contains(GC_COLOR))
  120. stateOfSettings.Remove(GC_COLOR);
  121. break;
  122. case GC_DRAWMODE:
  123. valS = (string)cContext[key];
  124. window.SetDraw(valS);
  125. break;
  126. case GC_LINEWIDTH:
  127. valI = (int)cContext[key];
  128. window.SetLineWidth(valI);
  129. break;
  130. case GC_LUT:
  131. valS = (string)cContext[key];
  132. window.SetLut(valS);
  133. break;
  134. case GC_PAINT:
  135. valS = (string)cContext[key];
  136. window.SetPaint(valS);
  137. break;
  138. case GC_SHAPE:
  139. valS = (string)cContext[key];
  140. window.SetShape(valS);
  141. break;
  142. case GC_LINESTYLE:
  143. valH = (HTuple)cContext[key];
  144. window.SetLineStyle(valH);
  145. break;
  146. default:
  147. break;
  148. }
  149. if (valI != -1)
  150. {
  151. if (stateOfSettings.Contains(key))
  152. stateOfSettings[key] = valI;
  153. else
  154. stateOfSettings.Add(key, valI);
  155. valI = -1;
  156. }
  157. else if (valS != "")
  158. {
  159. if (stateOfSettings.Contains(key))
  160. stateOfSettings[key] = valI;
  161. else
  162. stateOfSettings.Add(key, valI);
  163. valS = "";
  164. }
  165. else if (valH != null)
  166. {
  167. if (stateOfSettings.Contains(key))
  168. stateOfSettings[key] = valI;
  169. else
  170. stateOfSettings.Add(key, valI);
  171. valH = null;
  172. }
  173. }//while
  174. }
  175. catch (HOperatorException e)
  176. {
  177. gcNotification(e.Message);
  178. return;
  179. }
  180. }
  181. /// <summary>Sets a value for the graphical mode GC_COLOR</summary>
  182. /// <param name="val">
  183. /// A single color, e.g. "blue", "green" ...etc.
  184. /// </param>
  185. public void setColorAttribute(string val)
  186. {
  187. if (graphicalSettings.ContainsKey(GC_COLORED))
  188. graphicalSettings.Remove(GC_COLORED);
  189. addValue(GC_COLOR, val);
  190. }
  191. /// <summary>Sets a value for the graphical mode GC_COLORED</summary>
  192. /// <param name="val">
  193. /// The colored mode, which can be either "colored3" or "colored6"
  194. /// or "colored12"
  195. /// </param>
  196. public void setColoredAttribute(int val)
  197. {
  198. if (graphicalSettings.ContainsKey(GC_COLOR))
  199. graphicalSettings.Remove(GC_COLOR);
  200. addValue(GC_COLORED, val);
  201. }
  202. /// <summary>Sets a value for the graphical mode GC_DRAWMODE</summary>
  203. /// <param name="val">
  204. /// One of the possible draw modes: "margin" or "fill"
  205. /// </param>
  206. public void setDrawModeAttribute(string val)
  207. {
  208. addValue(GC_DRAWMODE, val);
  209. }
  210. /// <summary>Sets a value for the graphical mode GC_LINEWIDTH</summary>
  211. /// <param name="val">
  212. /// The line width, which can range from 1 to 50
  213. /// </param>
  214. public void setLineWidthAttribute(int val)
  215. {
  216. addValue(GC_LINEWIDTH, val);
  217. }
  218. /// <summary>Sets a value for the graphical mode GC_LUT</summary>
  219. /// <param name="val">
  220. /// One of the possible modes of look up tables. For
  221. /// further information on particular setups, please refer to the
  222. /// Reference Manual entry of the operator set_lut.
  223. /// </param>
  224. public void setLutAttribute(string val)
  225. {
  226. addValue(GC_LUT, val);
  227. }
  228. /// <summary>Sets a value for the graphical mode GC_PAINT</summary>
  229. /// <param name="val">
  230. /// One of the possible paint modes. For further
  231. /// information on particular setups, please refer refer to the
  232. /// Reference Manual entry of the operator set_paint.
  233. /// </param>
  234. public void setPaintAttribute(string val)
  235. {
  236. addValue(GC_PAINT, val);
  237. }
  238. /// <summary>Sets a value for the graphical mode GC_SHAPE</summary>
  239. /// <param name="val">
  240. /// One of the possible shape modes. For further
  241. /// information on particular setups, please refer refer to the
  242. /// Reference Manual entry of the operator set_shape.
  243. /// </param>
  244. public void setShapeAttribute(string val)
  245. {
  246. addValue(GC_SHAPE, val);
  247. }
  248. /// <summary>Sets a value for the graphical mode GC_LINESTYLE</summary>
  249. /// <param name="val">
  250. /// A line style mode, which works
  251. /// identical to the input for the HDevelop operator
  252. /// 'set_line_style'. For particular information on this
  253. /// topic, please refer to the Reference Manual entry of the operator
  254. /// set_line_style.
  255. /// </param>
  256. public void setLineStyleAttribute(HTuple val)
  257. {
  258. addValue(GC_LINESTYLE, val);
  259. }
  260. /// <summary>
  261. /// Adds a value to the hashlist 'graphicalSettings' for the
  262. /// graphical mode described by the parameter 'key'
  263. /// </summary>
  264. /// <param name="key">
  265. /// A graphical mode defined by the constant GC_*
  266. /// </param>
  267. /// <param name="val">
  268. /// Defines the value as an int for this graphical
  269. /// mode 'key'
  270. /// </param>
  271. private void addValue(string key, int val)
  272. {
  273. if (graphicalSettings.ContainsKey(key))
  274. graphicalSettings[key] = val;
  275. else
  276. graphicalSettings.Add(key, val);
  277. }
  278. /// <summary>
  279. /// Adds a value to the hashlist 'graphicalSettings' for the
  280. /// graphical mode, described by the parameter 'key'
  281. /// </summary>
  282. /// <param name="key">
  283. /// A graphical mode defined by the constant GC_*
  284. /// </param>
  285. /// <param name="val">
  286. /// Defines the value as a string for this
  287. /// graphical mode 'key'
  288. /// </param>
  289. private void addValue(string key, string val)
  290. {
  291. if (graphicalSettings.ContainsKey(key))
  292. graphicalSettings[key] = val;
  293. else
  294. graphicalSettings.Add(key, val);
  295. }
  296. /// <summary>
  297. /// Adds a value to the hashlist 'graphicalSettings' for the
  298. /// graphical mode, described by the parameter 'key'
  299. /// </summary>
  300. /// <param name="key">
  301. /// A graphical mode defined by the constant GC_*
  302. /// </param>
  303. /// <param name="val">
  304. /// Defines the value as a HTuple for this
  305. /// graphical mode 'key'
  306. /// </param>
  307. private void addValue(string key, HTuple val)
  308. {
  309. if (graphicalSettings.ContainsKey(key))
  310. graphicalSettings[key] = val;
  311. else
  312. graphicalSettings.Add(key, val);
  313. }
  314. /// <summary>
  315. /// Clears the list of graphical settings.
  316. /// There will be no graphical changes made prior
  317. /// before drawing objects, since there are no
  318. /// graphical entries to be applied to the window.
  319. /// </summary>
  320. public void clear()
  321. {
  322. graphicalSettings.Clear();
  323. }
  324. /// <summary>
  325. /// Returns an exact clone of this graphicsContext instance
  326. /// </summary>
  327. public GraphicsContext copy()
  328. {
  329. return new GraphicsContext((Hashtable)this.graphicalSettings.Clone());
  330. }
  331. /// <summary>
  332. /// If the hashtable contains the key, the corresponding
  333. /// hashtable value is returned
  334. /// </summary>
  335. /// <param name="key">
  336. /// One of the graphical keys starting with GC_*
  337. /// </param>
  338. public object getGraphicsAttribute(string key)
  339. {
  340. if (graphicalSettings.ContainsKey(key))
  341. return graphicalSettings[key];
  342. return null;
  343. }
  344. /// <summary>
  345. /// Returns a copy of the hashtable that carries the
  346. /// entries for the current graphical context
  347. /// </summary>
  348. /// <returns> current graphical context </returns>
  349. public Hashtable copyContextList()
  350. {
  351. return (Hashtable)graphicalSettings.Clone();
  352. }
  353. /********************************************************************/
  354. public void dummy(string val) { }
  355. }//end of class
  356. }//end of namespace