public void Export(string path, IList <IShape> shapes) { this.shapes = shapes; ShapesDto dtos = GetDtos(); ExportToXml(path, dtos); }
//Uses XmlSerializer to export shapeDtos to xml file private static void ExportToXml(string path, ShapesDto dtos) { XmlSerializer serializer = new XmlSerializer(typeof(ShapesDto), new XmlRootAttribute("Shapes")); StreamWriter streamWriter = new StreamWriter(path); serializer.Serialize(streamWriter, dtos); streamWriter.Dispose(); }
public IList <IShape> Import(string path, out int currentMaxLayer) { ShapesDto shapesDto = ImportShapesDtoFromXml(path); IList <IShape> shapes = new List <IShape>(); GetShapesFromDto(shapesDto, shapes); //Set the maxLayer to the highest number. currentMaxLayer = shapes.OrderByDescending(s => s.CurrentLayer).ToArray()[0].CurrentLayer; return(shapes); }
//Uses XmlSerializer to import shapes from Xml file private static ShapesDto ImportShapesDtoFromXml(string path) { XmlRootAttribute rootAttribute = new XmlRootAttribute("Shapes"); XmlSerializer serializer = new XmlSerializer(typeof(ShapesDto), rootAttribute); StreamReader streamReader = new StreamReader(path); ShapesDto shapesDto = (ShapesDto)serializer.Deserialize(streamReader); streamReader.Dispose(); return(shapesDto); }
//Creates shapes from ShapesDtos and adds them to shapes list private static void GetShapesFromDto(ShapesDto shapesDto, IList <IShape> shapes) { foreach (CircleDto circleDto in shapesDto.Circles) { PointF center = new PointF(circleDto.CenterX, circleDto.CenterY); float radius = circleDto.Radius; Color color = GetColor(circleDto); int currentLayer = circleDto.CurrentLayer; IShape circle = new Circle(center, radius, color, currentLayer); shapes.Add(circle); } foreach (TriangleDto triangleDto in shapesDto.Triangles) { PointF pointA = new PointF(triangleDto.PointAX, triangleDto.PointAY); PointF pointB = new PointF(triangleDto.PointBX, triangleDto.PointBY); PointF pointC = new PointF(triangleDto.PointCX, triangleDto.PointCY); Color color = GetColor(triangleDto); int currentLayer = triangleDto.CurrentLayer; IShape triangle = new Triangle(pointA, pointB, pointC, color, currentLayer); shapes.Add(triangle); } foreach (RectangleDto rectangleDto in shapesDto.Rectangles) { PointF point = new PointF(rectangleDto.PointX, rectangleDto.PointY); float height = rectangleDto.Height; float width = rectangleDto.Width; Color color = GetColor(rectangleDto); int currentLayer = rectangleDto.CurrentLayer; IShape rectangle = new ShapeMaker.Models.Rectangle(point, width, height, color, currentLayer); shapes.Add(rectangle); } }