/// <summary> /// Read all figures from XML file with StreamReader /// </summary> /// <param name="path"> File path</param> /// <returns></returns> public static Box StreamReadFromXml(string path) { Figure figure = null; Сoloring color = Сoloring.None; bool isColored = false; Box box = new Box(); StreamReader stream = new StreamReader(path); XDocument xmlDocument = XDocument.Load(stream); XElement xmlRoot = xmlDocument.Element("Figures"); foreach (XElement xmlElem in xmlRoot.Elements("Figure").ToList()) { string shape = xmlElem.Attribute("shape").Value; // Colored figure (third attribute - color) if (xmlElem.Attributes().Count() == 2) { color = (Сoloring)int.Parse(xmlElem.Attribute("color").Value); isColored = true; } // Get colorless figure figure = LoadFigure(xmlElem, shape); // Paint if it is a paper figure if (isColored) { Painter.Colorize(figure, color); isColored = false; } box.Add(figure); } stream.Close(); return(box); }
/// <summary> /// Read all figures from XML file with XmlReader /// </summary> /// <param name="path"> File path</param> /// <returns> Figures collection</returns> public static Box ReadFromXml(string path) { Figure figure = null; Box box = new Box(); Сoloring color = Сoloring.None; bool isColored = false; using (XmlReader reader = XmlReader.Create(path)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Figure") { string shape = reader.GetAttribute("shape"); // Colored figure (third attribute - color) if (reader.AttributeCount == 2) { color = (Сoloring)int.Parse(reader.GetAttribute("color")); isColored = true; } // Get colorless figure figure = LoadFigure(reader, shape); // Paint if it is a paper figure if (isColored) { Painter.Colorize(figure, color); isColored = false; } box.Add(figure); } } } return(box); }
/// <summary> /// Initializes a new instance of the RectanglePaper class, cutting from another figure /// </summary> /// <param name="figure"> Figure for cutting</param> /// <param name="width"> Width of a rectangle</param> /// <param name="height"> Height of a rectangle</param> public RectanglePaper(Figure figure, float width, float height) : base(figure, width, height) { if (!(figure is IPaperFigure)) { throw new Exception("The figure cannot be created."); } color = ((IPaperFigure)figure).Сolor; }
/// <summary> /// Initializes a new instance of the TrianglePaper class, cutting from another figure /// </summary> /// <param name="figure"> Figure for cutting</param> /// <param name="a"> First side of a triangle</param> /// <param name="b"> Second side of a triangle</param> /// <param name="c"> Third side of a triangle</param> public TrianglePaper(Figure figure, float a, float b, float c) : base(figure, a, b, c) { if (!(figure is IPaperFigure)) { throw new Exception("The figure cannot be created."); } color = ((IPaperFigure)figure).Сolor; }
/// <summary> /// Initializes a new instance of the CirclePaper class, cutting from another figure /// </summary> /// <param name="figure"> Figure for cutting</param> /// <param name="radius"> Circle radius</param> public CirclePaper(Figure figure, float radius) : base(figure, radius) { if (!(figure is IPaperFigure)) { throw new Exception("The figure cannot be created."); } color = ((IPaperFigure)figure).Сolor; }
/// <summary> /// Paints a figure in a given color /// </summary> /// <param name="figure"> Figure to paint</param> /// <param name="color"> Color</param> public static void Colorize(Figure figure, Сoloring color) { if (figure is IPaperFigure) { IPaperFigure fig = (IPaperFigure)figure; fig.Сolor = color; } else { throw new Exception("This figure cannot be painted."); } }
/// <summary> /// Initializes a new instance of the RectanglePaper class /// </summary> /// <param name="width"> Width of a rectangle</param> /// <param name="height"> Height of a rectangle</param> public RectanglePaper(float width, float height) : base(width, height) { color = Сoloring.None; }
/// <summary> /// Initializes a new instance of the TrianglePaper class /// </summary> /// <param name="a"> First side of a triangle</param> /// <param name="b"> Second side of a triangle</param> /// <param name="c"> Third side of a triangle</param> public TrianglePaper(float a, float b, float c) : base(a, b, c) { color = Сoloring.None; }
/// <summary> /// Initializes a new instance of the CirclePaper class /// </summary> /// <param name="radius"> Circle radius</param> public CirclePaper(float radius) : base(radius) { color = Сoloring.None; }