///<summary> This gets called when when the user runs this command.</summary>
        public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
        {
            List <MRhinoObject> object_list = new List <MRhinoObject>();

            MRhinoObjectIterator it = new MRhinoObjectIterator(
                IRhinoObjectIterator.object_state.undeleted_objects,
                IRhinoObjectIterator.object_category.active_and_reference_objects);

            foreach (MRhinoObject obj in it)
            {
                string data = null;
                if (EstimatorHelpers.GetData(obj, ref data) > 0)
                {
                    object_list.Add(obj);
                }
            }

            if (0 == object_list.Count)
            {
                RhUtil.RhinoApp().Print("No objects with Estimator tag data found.\n");
                return(IRhinoCommand.result.nothing);
            }

            string filename = null;

            SaveFileDialog sd = new SaveFileDialog();

            sd.DefaultExt       = "csv";
            sd.Filter           = "CSV file (*.csv)|*.csv|XML file (*.xml)|*.xml";
            sd.AddExtension     = true;
            sd.RestoreDirectory = true;
            sd.Title            = "Save";
            if (sd.ShowDialog() == DialogResult.OK)
            {
                filename = sd.FileName;
            }
            sd.Dispose();
            sd = null;

            if (null == filename)
            {
                return(IRhinoCommand.result.cancel);
            }

            bool bXml = false;

            if (Path.GetExtension(filename) == ".xml")
            {
                bXml = true;
            }

            if (bXml)
            {
                XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteComment("Saved on " + DateTime.Now);

                // Write root element
                writer.WriteStartElement("Estimator");
                writer.WriteAttributeString("Version", "1.0");

                // Write objects element
                writer.WriteStartElement("Objects");
                writer.WriteAttributeString("Count", object_list.Count.ToString());

                for (int i = 0; i < object_list.Count; i++)
                {
                    MRhinoObject obj = object_list[i];
                    if (null == obj)
                    {
                        continue;
                    }

                    IOnGeometry geo = obj.Geometry();
                    if (null == geo)
                    {
                        continue;
                    }

                    string[] string_array = null;
                    if (0 == EstimatorHelpers.GetData(obj, ref string_array))
                    {
                        continue;
                    }

                    // Write object
                    writer.WriteStartElement("Object");
                    writer.WriteAttributeString("Type", geo.ObjectType().ToString());
                    writer.WriteElementString("Uuid", obj.Attributes().m_uuid.ToString());
                    if (obj.Attributes().m_name.Length > 0)
                    {
                        writer.WriteElementString("Name", obj.Attributes().m_name);
                    }
                    else
                    {
                        writer.WriteElementString("Name", "(none)");
                    }

                    // Write object length
                    double length = EstimatorHelpers.GetLength(obj);
                    if (length > 0.0)
                    {
                        writer.WriteElementString("Length", length.ToString());
                    }
                    else
                    {
                        writer.WriteElementString("Length", "n/a");
                    }

                    double tol = context.m_doc.AbsoluteTolerance();

                    // Write object area
                    double area = EstimatorHelpers.GetArea(obj, tol);
                    if (area > 0.0)
                    {
                        writer.WriteElementString("Area", area.ToString());
                    }
                    else
                    {
                        writer.WriteElementString("Area", "n/a");
                    }

                    // Write object volume
                    double volume = EstimatorHelpers.GetVolume(obj);
                    if (volume > 0.0)
                    {
                        writer.WriteElementString("Volume", volume.ToString());
                    }
                    else
                    {
                        writer.WriteElementString("Volume", "n/a");
                    }

                    // Write object tags
                    writer.WriteStartElement("Tags");
                    for (int j = 0; j < string_array.Length; j++)
                    {
                        writer.WriteElementString("Tag", string_array[j]);
                    }

                    writer.WriteEndElement(); // Tags

                    writer.WriteEndElement(); // Object
                }

                writer.WriteEndElement(); // Objects

                writer.WriteEndElement(); // Estimator

                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }
            else
            {
                TextWriter writer = new StreamWriter(filename);

                for (int i = 0; i < object_list.Count; i++)
                {
                    MRhinoObject obj = object_list[i];
                    if (null == obj)
                    {
                        continue;
                    }

                    IOnGeometry geo = obj.Geometry();
                    if (null == geo)
                    {
                        continue;
                    }

                    string[] string_array = null;
                    if (0 == EstimatorHelpers.GetData(obj, ref string_array))
                    {
                        continue;
                    }

                    StringBuilder sb = new StringBuilder();
                    sb.Append(geo.ObjectType().ToString());
                    sb.Append(",");
                    sb.Append(obj.Attributes().m_uuid.ToString());
                    sb.Append(",");

                    double length = EstimatorHelpers.GetLength(obj);
                    if (length > 0.0)
                    {
                        sb.Append(length.ToString());
                    }
                    else
                    {
                        sb.Append("n/a");
                    }
                    sb.Append(",");

                    double tol  = context.m_doc.AbsoluteTolerance();
                    double area = EstimatorHelpers.GetArea(obj, tol);
                    if (area > 0.0)
                    {
                        sb.Append(area.ToString());
                    }
                    else
                    {
                        sb.Append("n/a");
                    }
                    sb.Append(",");

                    double volume = EstimatorHelpers.GetVolume(obj);
                    if (volume > 0.0)
                    {
                        sb.Append(volume.ToString());
                    }
                    else
                    {
                        sb.Append("n/a");
                    }

                    for (int j = 0; j < string_array.Length; j++)
                    {
                        sb.Append(",");
                        sb.Append(string_array[j]);
                    }

                    writer.WriteLine(sb.ToString());
                }

                // close the stream
                writer.Close();
            }

            return(IRhinoCommand.result.success);
        }
Beispiel #2
0
        ///<summary> This gets called when when the user runs this command.</summary>
        public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
        {
            MRhinoGetObject go = new MRhinoGetObject();

            go.SetCommandPrompt("Select tagged object to report");
            go.EnableSubObjectSelect(false);
            go.GetObjects(1, 1);
            if (go.CommandResult() != IRhinoCommand.result.success)
            {
                return(go.CommandResult());
            }

            IRhinoObject obj = go.Object(0).Object();

            if (null == obj)
            {
                return(IRhinoCommand.result.failure);
            }

            IOnGeometry geo = obj.Geometry();

            if (null == geo)
            {
                return(IRhinoCommand.result.failure);
            }

            string[] string_array = null;
            if (0 == EstimatorHelpers.GetData(obj, ref string_array))
            {
                RhUtil.RhinoApp().Print("No Estimator tag data found.\n");
                return(IRhinoCommand.result.nothing);
            }

            string filename = null;

            SaveFileDialog sd = new SaveFileDialog();

            sd.DefaultExt       = "xml";
            sd.Filter           = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
            sd.AddExtension     = true;
            sd.RestoreDirectory = true;
            sd.Title            = "Save";
            if (sd.ShowDialog() == DialogResult.OK)
            {
                filename = sd.FileName;
            }
            sd.Dispose();
            sd = null;

            if (null == filename)
            {
                return(IRhinoCommand.result.cancel);
            }

            XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8);

            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteComment("Saved on " + DateTime.Now);

            // Write root element
            writer.WriteStartElement("Estimator");
            writer.WriteAttributeString("Version", "1.0");

            // Write object
            writer.WriteStartElement("Object");
            writer.WriteAttributeString("Type", geo.ObjectType().ToString());
            writer.WriteElementString("Uuid", obj.Attributes().m_uuid.ToString());
            if (obj.Attributes().m_name.Length > 0)
            {
                writer.WriteElementString("Name", obj.Attributes().m_name);
            }
            else
            {
                writer.WriteElementString("Name", "(none)");
            }

            // Write object length
            double length = EstimatorHelpers.GetLength(obj);

            if (length > 0.0)
            {
                writer.WriteElementString("Length", length.ToString());
            }
            else
            {
                writer.WriteElementString("Length", "n/a");
            }

            double tol = context.m_doc.AbsoluteTolerance();

            // Write object area
            double area = EstimatorHelpers.GetArea(obj, tol);

            if (area > 0.0)
            {
                writer.WriteElementString("Area", area.ToString());
            }
            else
            {
                writer.WriteElementString("Area", "n/a");
            }

            // Write object volume
            double volume = EstimatorHelpers.GetVolume(obj);

            if (volume > 0.0)
            {
                writer.WriteElementString("Volume", volume.ToString());
            }
            else
            {
                writer.WriteElementString("Volume", "n/a");
            }

            // Write object tags
            writer.WriteStartElement("Tags");
            for (int i = 0; i < string_array.Length; i++)
            {
                writer.WriteElementString("Tag", string_array[i]);
            }

            writer.WriteEndElement(); // Tags

            writer.WriteEndElement(); // Object

            writer.WriteEndElement(); // Estimator

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();

            return(IRhinoCommand.result.success);
        }