Пример #1
0
    // This method reads a config.xml file at the given path and fills the
    // ConfigData object with the data.
    public bool structToFile(string configXMLPath, ConfigData configData)
    {
        XmlWriterSettings configWriterSettings = new XmlWriterSettings();
        configWriterSettings.Indent = true;
        using (XmlWriter configWriter =
            XmlWriter.Create(configXMLPath, configWriterSettings))
        {
            configWriter.WriteStartDocument();

            configWriter.WriteStartElement("QCARConfig");
            configWriter.WriteAttributeString("xmlns", "xsi", null,
                "http://www.w3.org/2001/XMLSchema-instance");
            configWriter.WriteAttributeString("xsi",
                "noNamespaceSchemaLocation", null, "qcar_config.xsd");

            configWriter.WriteStartElement("Tracking");

            // Writing Image Target elements into config.xml file.
            string[] imageTargetNames = new string[configData.NumImageTargets];
            configData.CopyImageTargetNames(imageTargetNames, 0);
            for (int i = 0; i < imageTargetNames.Length; ++i)
            {
                ConfigData.ImageTarget it;

                configData.GetImageTarget(imageTargetNames[i], out it);

                configWriter.WriteStartElement("ImageTarget");
                string imageTargetSize = it.size.x.ToString() + " " +
                                         it.size.y.ToString();
                configWriter.WriteAttributeString("size", imageTargetSize);
                configWriter.WriteAttributeString("name", imageTargetNames[i]);

                // Writing Virtual Button elements into config.xml file per
                // Image Target.
                List<ConfigData.VirtualButton> vbs = it.virtualButtons;
                for (int j = 0; j < vbs.Count; j++)
                {
                    configWriter.WriteStartElement("VirtualButton");
                    configWriter.WriteAttributeString("name", vbs[j].name);
                    string virtualButtonRectangle =
                        vbs[j].rectangle.x.ToString() + " " +
                        vbs[j].rectangle.y.ToString() + " " +
                        vbs[j].rectangle.z.ToString() + " " +
                        vbs[j].rectangle.w.ToString();
                    configWriter.WriteAttributeString("rectangle",
                        virtualButtonRectangle);
                    if (vbs[j].enabled)
                        configWriter.WriteAttributeString("enabled",
                                                          "true");
                    else
                        configWriter.WriteAttributeString("enabled",
                                                          "false");
                    if (vbs[j].sensitivity ==
                        VirtualButtonBehaviour.Sensitivity.LOW)
                        configWriter.WriteAttributeString("sensitivity",
                                                          "low");
                    else if (vbs[j].sensitivity ==
                        VirtualButtonBehaviour.Sensitivity.MEDIUM)
                        configWriter.WriteAttributeString("sensitivity",
                                                          "medium");
                    else if (vbs[j].sensitivity ==
                        VirtualButtonBehaviour.Sensitivity.HIGH)
                        configWriter.WriteAttributeString("sensitivity",
                                                          "high");
                    configWriter.WriteEndElement(); // VirtualButton
                }
                configWriter.WriteEndElement(); // ImageTarget
            }

            // Writing Multi Target elements into config.xml file
            string[] multiTargetNames = new string[configData.NumMultiTargets];
            configData.CopyMultiTargetNames(multiTargetNames, 0);
            for (int i = 0; i < multiTargetNames.Length; i++)
            {
                ConfigData.MultiTarget mt;

                configData.GetMultiTarget(multiTargetNames[i], out mt);

                configWriter.WriteStartElement("MultiTarget");
                configWriter.WriteAttributeString("name", multiTargetNames[i]);

                // Writing Multi Target Part elements into config.xml file
                List<ConfigData.MultiTargetPart> prts = mt.parts;
                for (int j = 0; j < prts.Count; j++)
                {
                    configWriter.WriteStartElement("Part");
                    configWriter.WriteAttributeString("name", prts[j].name);
                    string multiTargetTranslation =
                        prts[j].translation.x.ToString() + " " +
                        prts[j].translation.z.ToString() + " " +
                        prts[j].translation.y.ToString();
                    configWriter.WriteAttributeString("translation",
                                                      multiTargetTranslation);
                    float rotationAngle;
                    Vector3 rotationAxis;
                    prts[j].rotation.ToAngleAxis(out rotationAngle,
                                                 out rotationAxis);
                    string multiTargetRotation =
                        "AD: " +
                        (-rotationAxis.x).ToString() + " " +
                        (-rotationAxis.z).ToString() + " " +
                        rotationAxis.y.ToString() + " " +
                        rotationAngle.ToString();
                    configWriter.WriteAttributeString("rotation",
                        multiTargetRotation);
                    configWriter.WriteEndElement(); // Part
                }

                configWriter.WriteEndElement(); // MultiTarget
            }

            // Writing Frame Marker elements into config.xml file
            for (int i = 0; i < configData.NumFrameMarkers; i++)
            {
                ConfigData.FrameMarker fmConfig;
                if (configData.GetFrameMarker(i, out fmConfig))
                {
                    configWriter.WriteStartElement("Marker");
                    configWriter.WriteAttributeString("type", "id-frame");
                    string frameMarkerSize = fmConfig.size.x.ToString() + " " +
                                             fmConfig.size.y.ToString();
                    configWriter.WriteAttributeString("size", frameMarkerSize);
                    configWriter.WriteAttributeString("name", fmConfig.name);
                    configWriter.WriteAttributeString("id", i.ToString());
                    configWriter.WriteEndElement(); // Marker
                }
            }

            configWriter.WriteEndElement(); // Tracking

            configWriter.WriteEndElement(); // QCARConfig

            configWriter.WriteEndDocument();
        }

        return true;
    }