using System; using System.Collections; using HalconDotNet; namespace ViewWindow.Model { public delegate void GCDelegate(string val); /// /// This class contains the graphical context of an HALCON object. The /// set of graphical modes is defined by the hashlist 'graphicalSettings'. /// If the list is empty, then there is no difference to the graphical /// setting defined by the system by default. Otherwise, the provided /// HALCON window is adjusted according to the entries of the supplied /// graphical context (when calling applyContext()) /// public class GraphicsContext { /// /// Graphical mode for the output color (see dev_set_color) /// public const string GC_COLOR = "Color"; /// /// Graphical mode for the multi-color output (see dev_set_colored) /// public const string GC_COLORED = "Colored"; /// /// Graphical mode for the line width (see set_line_width) /// public const string GC_LINEWIDTH = "LineWidth"; /// /// Graphical mode for the drawing (see set_draw) /// public const string GC_DRAWMODE = "DrawMode"; /// /// Graphical mode for the drawing shape (see set_shape) /// public const string GC_SHAPE = "Shape"; /// /// Graphical mode for the LUT (lookup table) (see set_lut) /// public const string GC_LUT = "Lut"; /// /// Graphical mode for the painting (see set_paint) /// public const string GC_PAINT = "Paint"; /// /// Graphical mode for the line style (see set_line_style) /// public const string GC_LINESTYLE = "LineStyle"; /// /// Hashlist containing entries for graphical modes (defined by GC_*), /// which is then linked to some HALCON object to describe its /// graphical context. /// private Hashtable graphicalSettings; /// /// Backup of the last graphical context applied to the window. /// public Hashtable stateOfSettings; private IEnumerator iterator; /// /// Option to delegate messages from the graphical context /// to some observer class /// public GCDelegate gcNotification; /// /// Creates a graphical context with no initial /// graphical modes /// public GraphicsContext() { graphicalSettings = new Hashtable(10, 0.2f); gcNotification = new GCDelegate(dummy); stateOfSettings = new Hashtable(10, 0.2f); } /// /// Creates an instance of the graphical context with /// the modes defined in the hashtable 'settings' /// /// /// List of modes, which describes the graphical context /// public GraphicsContext(Hashtable settings) { graphicalSettings = settings; gcNotification = new GCDelegate(dummy); stateOfSettings = new Hashtable(10, 0.2f); } /// Applies graphical context to the HALCON window /// Active HALCON window /// /// List that contains graphical modes for window /// public void applyContext(HWindow window, Hashtable cContext) { string key = ""; string valS = ""; int valI = -1; HTuple valH = null; iterator = cContext.Keys.GetEnumerator(); try { while (iterator.MoveNext()) { key = (string)iterator.Current; if (stateOfSettings.Contains(key) && stateOfSettings[key] == cContext[key]) continue; switch (key) { case GC_COLOR: valS = (string)cContext[key]; window.SetColor(valS); if (stateOfSettings.Contains(GC_COLORED)) stateOfSettings.Remove(GC_COLORED); break; case GC_COLORED: valI = (int)cContext[key]; window.SetColored(valI); if (stateOfSettings.Contains(GC_COLOR)) stateOfSettings.Remove(GC_COLOR); break; case GC_DRAWMODE: valS = (string)cContext[key]; window.SetDraw(valS); break; case GC_LINEWIDTH: valI = (int)cContext[key]; window.SetLineWidth(valI); break; case GC_LUT: valS = (string)cContext[key]; window.SetLut(valS); break; case GC_PAINT: valS = (string)cContext[key]; window.SetPaint(valS); break; case GC_SHAPE: valS = (string)cContext[key]; window.SetShape(valS); break; case GC_LINESTYLE: valH = (HTuple)cContext[key]; window.SetLineStyle(valH); break; default: break; } if (valI != -1) { if (stateOfSettings.Contains(key)) stateOfSettings[key] = valI; else stateOfSettings.Add(key, valI); valI = -1; } else if (valS != "") { if (stateOfSettings.Contains(key)) stateOfSettings[key] = valI; else stateOfSettings.Add(key, valI); valS = ""; } else if (valH != null) { if (stateOfSettings.Contains(key)) stateOfSettings[key] = valI; else stateOfSettings.Add(key, valI); valH = null; } }//while } catch (HOperatorException e) { gcNotification(e.Message); return; } } /// Sets a value for the graphical mode GC_COLOR /// /// A single color, e.g. "blue", "green" ...etc. /// public void setColorAttribute(string val) { if (graphicalSettings.ContainsKey(GC_COLORED)) graphicalSettings.Remove(GC_COLORED); addValue(GC_COLOR, val); } /// Sets a value for the graphical mode GC_COLORED /// /// The colored mode, which can be either "colored3" or "colored6" /// or "colored12" /// public void setColoredAttribute(int val) { if (graphicalSettings.ContainsKey(GC_COLOR)) graphicalSettings.Remove(GC_COLOR); addValue(GC_COLORED, val); } /// Sets a value for the graphical mode GC_DRAWMODE /// /// One of the possible draw modes: "margin" or "fill" /// public void setDrawModeAttribute(string val) { addValue(GC_DRAWMODE, val); } /// Sets a value for the graphical mode GC_LINEWIDTH /// /// The line width, which can range from 1 to 50 /// public void setLineWidthAttribute(int val) { addValue(GC_LINEWIDTH, val); } /// Sets a value for the graphical mode GC_LUT /// /// One of the possible modes of look up tables. For /// further information on particular setups, please refer to the /// Reference Manual entry of the operator set_lut. /// public void setLutAttribute(string val) { addValue(GC_LUT, val); } /// Sets a value for the graphical mode GC_PAINT /// /// One of the possible paint modes. For further /// information on particular setups, please refer refer to the /// Reference Manual entry of the operator set_paint. /// public void setPaintAttribute(string val) { addValue(GC_PAINT, val); } /// Sets a value for the graphical mode GC_SHAPE /// /// One of the possible shape modes. For further /// information on particular setups, please refer refer to the /// Reference Manual entry of the operator set_shape. /// public void setShapeAttribute(string val) { addValue(GC_SHAPE, val); } /// Sets a value for the graphical mode GC_LINESTYLE /// /// A line style mode, which works /// identical to the input for the HDevelop operator /// 'set_line_style'. For particular information on this /// topic, please refer to the Reference Manual entry of the operator /// set_line_style. /// public void setLineStyleAttribute(HTuple val) { addValue(GC_LINESTYLE, val); } /// /// Adds a value to the hashlist 'graphicalSettings' for the /// graphical mode described by the parameter 'key' /// /// /// A graphical mode defined by the constant GC_* /// /// /// Defines the value as an int for this graphical /// mode 'key' /// private void addValue(string key, int val) { if (graphicalSettings.ContainsKey(key)) graphicalSettings[key] = val; else graphicalSettings.Add(key, val); } /// /// Adds a value to the hashlist 'graphicalSettings' for the /// graphical mode, described by the parameter 'key' /// /// /// A graphical mode defined by the constant GC_* /// /// /// Defines the value as a string for this /// graphical mode 'key' /// private void addValue(string key, string val) { if (graphicalSettings.ContainsKey(key)) graphicalSettings[key] = val; else graphicalSettings.Add(key, val); } /// /// Adds a value to the hashlist 'graphicalSettings' for the /// graphical mode, described by the parameter 'key' /// /// /// A graphical mode defined by the constant GC_* /// /// /// Defines the value as a HTuple for this /// graphical mode 'key' /// private void addValue(string key, HTuple val) { if (graphicalSettings.ContainsKey(key)) graphicalSettings[key] = val; else graphicalSettings.Add(key, val); } /// /// Clears the list of graphical settings. /// There will be no graphical changes made prior /// before drawing objects, since there are no /// graphical entries to be applied to the window. /// public void clear() { graphicalSettings.Clear(); } /// /// Returns an exact clone of this graphicsContext instance /// public GraphicsContext copy() { return new GraphicsContext((Hashtable)this.graphicalSettings.Clone()); } /// /// If the hashtable contains the key, the corresponding /// hashtable value is returned /// /// /// One of the graphical keys starting with GC_* /// public object getGraphicsAttribute(string key) { if (graphicalSettings.ContainsKey(key)) return graphicalSettings[key]; return null; } /// /// Returns a copy of the hashtable that carries the /// entries for the current graphical context /// /// current graphical context public Hashtable copyContextList() { return (Hashtable)graphicalSettings.Clone(); } /********************************************************************/ public void dummy(string val) { } }//end of class }//end of namespace