Пример #1
0
        /// <summary>
        /// Parse XML file and create 2d representation of given asset
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string rootDirPath         = $"{Environment.CurrentDirectory}/../../";
            string assetsPath          = $"{rootDirPath}/assets/";
            string representationsPath = $"{rootDirPath}/representations/";
            string assertFileName;

            double panelX, panelY, panelWidth, panelHeight, panelOffset;
            int    imgHeight, imgWidth, rotation = 0;

            Console.Write("Input name of XML file you need to process: ");
            assertFileName = Console.ReadLine();
            string assetFilePath = $"{assetsPath}/{assertFileName}.xml";

            //Check if file exists
            if (!File.Exists(assetFilePath))
            {
                Console.WriteLine("File doesn't exists");
                Console.Read();
                return;
            }

            XmlDocument xmlFile = new XmlDocument();

            xmlFile.Load(assetFilePath);

            //Set image size based on attributes of "folding" node
            XmlNode rootNode = xmlFile.SelectSingleNode(XmlNameConsts.FOLDING_NAME);

            imgHeight = XmlConverter.GetIntValueFromXmlAttr(rootNode, XmlNameConsts.IMG_HEIGHT_NAME);
            imgWidth  = XmlConverter.GetIntValueFromXmlAttr(rootNode, XmlNameConsts.IMG_WIDTH_NAME);

            using (Bitmap image = new Bitmap(imgWidth, imgHeight))
            {
                using (Graphics imgGraphics = Graphics.FromImage(image))
                {
                    //Fill image with default pixels
                    imgGraphics.FillRectangle(Brushes.White, 0, 0, imgWidth, imgHeight);

                    //Get values to draw root node
                    panelX = XmlConverter.GetDoubleValueFromXmlAttr(rootNode, XmlNameConsts.ROOT_X_COORDINATE_NAME);
                    panelY = XmlConverter.GetDoubleValueFromXmlAttr(rootNode, XmlNameConsts.ROOT_Y_COORDINATE_NAME);

                    rootNode    = rootNode.SelectSingleNode(XmlNameConsts.PANELS_NAME).SelectSingleNode(XmlNameConsts.ITEM_NAME);
                    panelHeight = XmlConverter.GetDoubleValueFromXmlAttr(rootNode, XmlNameConsts.PANEL_HEIGHT_NAME);
                    panelWidth  = XmlConverter.GetDoubleValueFromXmlAttr(rootNode, XmlNameConsts.PANEL_WIDTH_NAME);
                    panelOffset = XmlConverter.GetDoubleValueFromXmlAttr(rootNode, XmlNameConsts.HINGE_OFFSET_NAME);

                    //Draw root node
                    PanelManager.DrawPanel(imgGraphics, panelX + panelOffset, panelY, panelHeight, panelWidth);

                    //Create all other nodes recursively
                    PanelManager.CreateChildPanels(imgGraphics, rootNode, panelX, panelY, panelHeight, panelWidth, rotation);
                }

                //If image with asset name already exists - delete and create new
                if (File.Exists($"{representationsPath}/{assertFileName}.jpg"))
                {
                    File.Delete($"{representationsPath}/{assertFileName}.jpg");
                }
                image.Save($"{representationsPath}/{assertFileName}.jpg");
            }

            Console.WriteLine("Representation created");
            Console.Read();
        }
Пример #2
0
        /// <summary>
        /// Recursively creating all children of given parent node
        /// </summary>
        /// <param name="imgGraphics">Graphic tool used for drawing figures (rectangles)</param>
        /// <param name="parent">Parent node, whose children we'll draw</param>
        /// <param name="parentX">X coordinate of parent panel (Is used to calculate X coordinate of children)</param>
        /// <param name="parentY">Y coordinate of parent panel (Is used to calculate Y coordinate of children)</param>
        /// <param name="parentHeight">Height of parent panel (Is used to calculate Y coordinate of children)</param>
        /// <param name="parentWidth">Width of parent panel (Is used to calculate X coordinate of children)</param>
        /// <param name="rotation">Value, describes if parent panel is rotated</param>
        public static void CreateChildPanels(Graphics imgGraphics, XmlNode parent, double parentX, double parentY, double parentHeight, double parentWidth, int rotation)
        {
            double panelX, panelY, panelWidth, panelHeight, panelOffset;
            int    sideToAttach, unrotatedSide;

            foreach (XmlNode childNode in parent.SelectSingleNode(XmlNameConsts.ATTACHED_PANELS_NAME).ChildNodes)
            {
                //Side of parent panel, where child is attached
                sideToAttach = XmlConverter.GetIntValueFromXmlAttr(childNode, XmlNameConsts.ATTACHED_TO_SIDE_NAME);

                //Normalized side number name
                unrotatedSide = AddRotationToSide(sideToAttach, rotation);
                switch (unrotatedSide)
                {
                //Child is attached to the bottom of parent
                case 0:
                    panelHeight = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_HEIGHT_NAME);
                    panelWidth  = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_WIDTH_NAME);
                    panelOffset = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.HINGE_OFFSET_NAME);

                    panelX = parentX - panelOffset;
                    panelY = parentY + panelHeight;

                    DrawPanel(imgGraphics, panelX, panelY, panelHeight, panelWidth);
                    break;

                //Child is attached to the right side of parent
                case 1:
                    if (unrotatedSide == 2)
                    {
                        panelHeight = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_HEIGHT_NAME);
                        panelWidth  = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_WIDTH_NAME);
                    }
                    else
                    {
                        panelHeight = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_WIDTH_NAME);
                        panelWidth  = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_HEIGHT_NAME);
                    }
                    panelOffset = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.HINGE_OFFSET_NAME);

                    panelX = parentX + (parentWidth / 2) + (panelWidth / 2);
                    panelY = parentY - (parentHeight / 2) + (panelHeight / 2) + panelOffset;

                    DrawPanel(imgGraphics, panelX, panelY, panelHeight, panelWidth);
                    break;

                //Child is attached to the top of parent
                case 2:
                    panelHeight = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_HEIGHT_NAME);
                    panelWidth  = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_WIDTH_NAME);
                    panelOffset = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.HINGE_OFFSET_NAME);

                    panelX = parentX + panelOffset;
                    panelY = parentY - parentHeight;

                    DrawPanel(imgGraphics, panelX, panelY, panelHeight, panelWidth);
                    break;

                //Child is attached to the left side of parent
                default:
                    if (unrotatedSide == 2)
                    {
                        panelHeight = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_HEIGHT_NAME);
                        panelWidth  = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_WIDTH_NAME);
                    }
                    else
                    {
                        panelHeight = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_WIDTH_NAME);
                        panelWidth  = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.PANEL_HEIGHT_NAME);
                    }
                    panelOffset = XmlConverter.GetDoubleValueFromXmlAttr(childNode, XmlNameConsts.HINGE_OFFSET_NAME);

                    panelX = parentX - (parentWidth / 2) - (panelWidth / 2);
                    panelY = parentY - (parentHeight / 2) + (panelHeight / 2) - panelOffset;

                    DrawPanel(imgGraphics, panelX, panelY, panelHeight, panelWidth);
                    break;
                }

                //If current child has its own children, we recursively going through all of them
                //(according to this child rotation)
                if (childNode.SelectSingleNode(XmlNameConsts.ATTACHED_PANELS_NAME) != null)
                {
                    int newRotation = 0;
                    switch (sideToAttach)
                    {
                    case 0:
                        newRotation += 2;
                        break;

                    case 1:
                        newRotation++;
                        break;

                    case 2:
                        break;

                    default:
                        newRotation--;
                        break;
                    }

                    //(rotation + newRotation) is used to track rotated child of rotated parent
                    CreateChildPanels(imgGraphics, childNode, panelX, panelY, panelHeight, panelWidth, rotation + newRotation);
                }
            }
        }