示例#1
0
文件: ToKML.cs 项目: BHoM/XML_Toolkit
        public static KML.KML ToKML(this KMLDocumentBuilder docBuilder, KMLSettings settings)
        {
            KML.KML kml = new KML.KML();
            kml.Document.Name = docBuilder.Name;
            List <Style> docStyles = new List <Style>();

            //set the default style
            docStyles.Add(new Style());
            List <Folder> folders = new List <Folder>();

            //each KMLGeometry is placed in a Folder
            foreach (KMLGeometry kMLGeometry in docBuilder.KMLGeometries)
            {
                //Add kmlGeo style

                List <BHG.Point> points = new List <BHG.Point>();
                List <BHG.Mesh>  meshes = new List <BHG.Mesh>();

                foreach (BHG.IGeometry igeo in kMLGeometry.Geometries)
                {
                    if (igeo is BHG.Point)
                    {
                        points.Add(igeo as BHG.Point);
                    }

                    if (igeo is BHG.Mesh)
                    {
                        meshes.Add(igeo as BHG.Mesh);
                    }
                }

                Folder           folder     = new Folder();
                List <Placemark> placemarks = new List <Placemark>();
                folder.Name = kMLGeometry.Name;

                List <Polygon> polygons      = new List <Polygon>();
                Placemark      placemark     = new Placemark();
                MultiGeometry  multiGeometry = new MultiGeometry();
                foreach (BHG.Mesh m in meshes)
                {
                    placemark          = new Placemark();
                    placemark.StyleURL = "#default";
                    multiGeometry      = new MultiGeometry();
                    polygons           = new List <Polygon>();
                    polygons           = m.ToKML(kMLGeometry.GeoReference);

                    placemark.MultiGeometry.Polygons = polygons.ToArray();
                    placemarks.Add(placemark);
                }
                folder.Placemarks = placemarks.ToArray();

                folders.Add(folder);
            }
            kml.Document.Folders = folders.ToArray();
            kml.Document.Styles  = docStyles.ToArray();
            return(kml);
        }
示例#2
0
        private bool CreateKML <T>(IEnumerable <T> objects, XMLConfig config)
        {
            bool success = true;

            if (config.Settings == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Please provide a suitable set of KMLSettings with the XMLConfig to determine how the KML file should be created");
                return(false);
            }

            KMLSettings settings = config.Settings as KMLSettings;

            if (settings == null)
            {
                BH.Engine.Reflection.Compute.RecordError("Please provide a suitable set of KMLSettings with the XMLConfig to determine how the KML file should be created");
                return(false);
            }

            KMLDocumentBuilder docBuilder = objects.ToList()[0] as KML.KMLDocumentBuilder;

            if (docBuilder == null)
            {
                BH.Engine.Reflection.Compute.RecordError("The KML schema requires a full model to be provided as a single push operation. For pushing to the KML version, you need to plug your objects into a KMLDocumentBuilder which collates the objects for pushing and push that to KML via this adapter.");
                return(false);
            }

            KMLSchema.KML kml = docBuilder.ToKML(settings);

            try
            {
                System.Reflection.PropertyInfo[] bhomProperties = typeof(BHoMObject).GetProperties();
                XmlAttributeOverrides            overrides      = new XmlAttributeOverrides();

                foreach (System.Reflection.PropertyInfo pi in bhomProperties)
                {
                    overrides.Add(typeof(BHoMObject), pi.Name, new XmlAttributes {
                        XmlIgnore = true
                    });
                }

                XmlSerializerNamespaces xns  = new XmlSerializerNamespaces();
                XmlSerializer           szer = new XmlSerializer(typeof(BH.oM.Adapters.XML.KMLSchema.KML), overrides);
                TextWriter ms = new StreamWriter(_fileSettings.GetFullFileName());
                szer.Serialize(ms, kml, xns);
                ms.Close();
            }
            catch (Exception e)
            {
                BH.Engine.Reflection.Compute.RecordError(e.ToString());
                success = false;
            }

            return(success);
        }