private void SampleCsObjectManagerControl_Load(object sender, EventArgs e)
        {
            m_events.Register();
            m_events.Enable(true);

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

            it.IncludeLights(true);
            it.IncludeGrips(false);

            m_listbox.BeginUpdate();
            for (MRhinoObject obj = it.First(); obj != null; obj = it.Next())
            {
                Guid guid  = obj.Attributes().m_uuid;
                int  index = m_listbox.Items.Add(guid.ToString());
                if (obj.IsSelected() > 0)
                {
                    m_listbox.SetSelected(index, true);
                }
            }
            m_listbox.EndUpdate();
            SaveSelectedIndices();
        }
 public override void OnReplaceObject(ref MRhinoDoc doc, 
              ref MRhinoObject old, ref MRhinoObject obj, 
              Splash splashScreen) 
 { 
    // to access the instance of the class from here, 
    // pass in the variable that holds a reference to the instance
    splashScreen.[Whatever];
Example #3
0
 public override void OnReplaceObject(ref MRhinoDoc doc, ref MRhinoObject old, ref MRhinoObject obj)
 {
     if (_mainUI != null)
     {
         _mainUI.Whatever();
     }
 }
Example #4
0
        /// <summary>
        /// Add an array of 3-D points to the document
        /// </summary>
        public object AddPoints(object pointsObj)
        {
            On3dPointArray points = new On3dPointArray();

            if (SampleCsRhinoScriptUtils.ConvertToOn3dPointArray(pointsObj, ref points))
            {
                MRhinoDoc doc = RhUtil.RhinoApp().ActiveDoc();
                if (null != doc)
                {
                    ArrayList objectIds = new ArrayList();
                    for (int i = 0; i < points.Count(); i++)
                    {
                        MRhinoObject rhinoObj = doc.AddPointObject(points[i]);
                        if (null != rhinoObj)
                        {
                            objectIds.Add(rhinoObj.ModelObjectId().ToString());
                        }
                    }
                    if (objectIds.Count > 0)
                    {
                        doc.Redraw();
                        return(objectIds.ToArray());
                    }
                }
            }
            return(null);
        }
Example #5
0
 public override void OnDeleteObject(ref MRhinoDoc doc, ref MRhinoObject @object)
 {
     if (true == m_control.Visible)
     {
         m_control.OnDeleteObject(ref doc, ref @object);
     }
 }
        private void m_listbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_bInEvent)
            {
                return;
            }

            m_bInSelect = true;
            MRhinoDoc doc = RhUtil.RhinoApp().ActiveDoc();

            // Select what got selected
            ListBox.SelectedIndexCollection selected = m_listbox.SelectedIndices;

            // Select what got selected
            int i = 0;

            for (i = 0; i < selected.Count; i++)
            {
                int index = selected[i];
                if (!m_selected.Contains(index))
                {
                    Guid         guid = new Guid(m_listbox.Items[index].ToString());
                    MRhinoObject obj  = doc.LookupObject(guid);
                    if (obj != null && obj.IsSelectable())
                    {
                        obj.Select(true);
                    }
                }
            }

            // Unselect what got unselected
            for (i = 0; i < m_selected.Count; i++)
            {
                int index = m_selected[i];
                if (!selected.Contains(index))
                {
                    Guid         guid = new Guid(m_listbox.Items[index].ToString());
                    MRhinoObject obj  = doc.LookupObject(guid);
                    if (obj != null)
                    {
                        obj.Select(false);
                    }
                }
            }

            SaveSelectedIndices();
            doc.Redraw();

            m_bInSelect = false;
        }
        public void OnDeleteObject(ref MRhinoDoc doc, ref MRhinoObject obj)
        {
            if (m_bInSelect)
            return;

              m_bInEvent = true;
              Guid guid = obj.Attributes().m_uuid;
              string str = guid.ToString();
              int index = m_listbox.FindStringExact(str);
              if (index != ListBox.NoMatches)
            m_listbox.Items.RemoveAt(index);
              SaveSelectedIndices();
              m_bInEvent = false;
        }
        public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
        {
            MRhinoObjectIterator it = new MRhinoObjectIterator(
                IRhinoObjectIterator.object_state.undeleted_objects,
                IRhinoObjectIterator.object_category.active_and_reference_objects
                );

            for (MRhinoObject obj = it.First(); null != obj; obj = it.Next())
            {
                obj.EnableAnalysisMode(ZAnalysisVAM.ZANALYSIS_VAM_ID, false);
            }

            context.m_doc.Redraw();
            RhUtil.RhinoApp().Print("Z-Analysis is off.\n");

            return(IRhinoCommand.result.success);
        }
        public void OnDeleteObject(ref MRhinoDoc doc, ref MRhinoObject obj)
        {
            if (m_bInSelect)
            {
                return;
            }

            m_bInEvent = true;
            Guid   guid  = obj.Attributes().m_uuid;
            string str   = guid.ToString();
            int    index = m_listbox.FindStringExact(str);

            if (index != ListBox.NoMatches)
            {
                m_listbox.Items.RemoveAt(index);
            }
            SaveSelectedIndices();
            m_bInEvent = false;
        }
Example #10
0
        /// <summary>
        /// Add a 3-D point to the document
        /// </summary>
        public object AddPoint(object pointObj)
        {
            On3dPoint point = new On3dPoint();

            if (SampleCsRhinoScriptUtils.ConvertToOn3dPoint(pointObj, ref point))
            {
                MRhinoDoc doc = RhUtil.RhinoApp().ActiveDoc();
                if (null != doc)
                {
                    MRhinoObject rhinoObj = doc.AddPointObject(point);
                    if (null != rhinoObj)
                    {
                        doc.Redraw();
                        return(rhinoObj.ModelObjectId().ToString());
                    }
                }
            }
            return(null);
        }
        ///<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);
        }
 public override void OnDeleteObject(ref MRhinoDoc doc, ref MRhinoObject @object)
 {
     if (true == m_control.Visible)
     m_control.OnDeleteObject(ref doc, ref @object);
 }
Example #13
0
        /// <summary>
        /// Rhino calls WriteFile() to write document geometry to an external file.
        /// </summary>
        public override int WriteFile(string filename, int index, ref MRhinoDoc doc, ref IRhinoFileWriteOptions options)
        {
            int rc = 0; // false

            // Are we saving or exporting?
            bool bExport = options.Mode(IRhinoFileWriteOptions.ModeFlag.SelectedMode);
            // Are we in interactive or scripted mode?
            bool bScript = options.Mode(IRhinoFileWriteOptions.ModeFlag.BatchMode);

            List <IRhinoObject> objects = new List <IRhinoObject>();

            // Get objects to save/export
            MRhinoObjectIterator it = new MRhinoObjectIterator(doc, IRhinoObjectIterator.object_state.undeleted_objects);

            if (bExport)
            {
                it.EnableSelectedFilter();
                it.EnableVisibleFilter();
            }

            // Do the iteration...
            MRhinoObject obj = null;

            for (obj = it.First(); null != obj; obj = it.Next())
            {
                objects.Add(obj);
            }

            ArrayMRhinoObjectMesh meshes          = new ArrayMRhinoObjectMesh(objects.Count);
            OnMeshParameters      mesh_parameters = _mesh_parameters;
            int mesh_ui_style = (bScript) ? 2 : _mesh_ui_style;

            // Get the meshes to save/export
            IRhinoCommand.result res = RhUtil.RhinoMeshObjects(objects.ToArray(), ref mesh_parameters, ref mesh_ui_style, ref meshes);
            if (res == IRhinoCommand.result.success)
            {
                if (mesh_ui_style >= 0 && mesh_ui_style <= 1)
                {
                    _mesh_ui_style = mesh_ui_style;
                }
                _mesh_parameters = mesh_parameters;
            }
            else
            {
                if (bExport)
                {
                    RhUtil.RhinoApp().Print("No meshes to export.\n");
                }
                else
                {
                    RhUtil.RhinoApp().Print("No meshes to save.\n");
                }
                return(rc);
            }

            try
            {
                // Open the file
                System.IO.StreamWriter file = new System.IO.StreamWriter(filename);

                // Write mesh count
                file.WriteLine(string.Format("meshcount={0}\n", meshes.Count()));

                // Write each mesh
                for (int i = 0; i < meshes.Count(); i++)
                {
                    MRhinoObjectMesh obj_mesh = meshes[i];
                    OnMesh           mesh     = obj_mesh.GetMesh();
                    if (null != mesh)
                    {
                        // Write mesh number
                        file.WriteLine(string.Format("mesh={0}\n", i));

                        // Write mesh vertex count
                        file.WriteLine(string.Format("vertexcount={0}\n", mesh.m_V.Count()));

                        // Write mesh face count
                        file.WriteLine(string.Format("facecount={0}\n", mesh.m_F.Count()));

                        // Write mesh vertices
                        for (int vi = 0; vi < mesh.m_V.Count(); vi++)
                        {
                            On3fPoint p = mesh.m_V[vi];
                            file.WriteLine(string.Format("vertex=({0},{1},{2})\n", p.x, p.y, p.z));
                        }

                        // Write mesh faces
                        for (int fi = 0; fi < mesh.m_F.Count(); fi++)
                        {
                            OnMeshFace f = mesh.m_F[fi];
                            file.WriteLine(string.Format("face=({0},{1},{2},{3})\n", f.get_vi(0), f.get_vi(1), f.get_vi(2), f.get_vi(3)));
                        }
                    }
                }

                file.Close();

                rc = 1; // true
            }
            catch (Exception e)
            {
                RhUtil.RhinoApp().Print(string.Format("{0}\n", e.Message));
            }

            return(rc);
        }
Example #14
0
        int CreateInstanceDefinition(MRhinoDoc doc, IOnInstanceDefinition idef, IOn3dPoint point, List <IRhinoObject> objects, bool bQuietly)
        {
            int found_index = doc.m_instance_definition_table.FindInstanceDefinition(idef.Name(), true);
            List <IRhinoObject> idef_objects = new List <IRhinoObject>();

            OnXform xform = new OnXform();

            xform.Translation(new On3dPoint(OnUtil.On_origin) - point);

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

                if (obj.ObjectType() == IOn.object_type.light_object ||
                    obj.ObjectType() == IOn.object_type.grip_object ||
                    obj.ObjectType() == IOn.object_type.phantom_object
                    )
                {
                    continue;
                }

                // Transform geometry and attributes, but do not add to the document
                MRhinoObject dupe = doc.TransformObject(obj, xform, false, false, false);
                if (dupe != null)
                {
                    if (doc.AddObject(dupe, false, true))
                    {
                        idef_objects.Add(dupe);
                    }
                }
            }

            if (idef_objects.Count < 1)
            {
                return(-1);
            }

            int idef_index = -1;

            if (found_index < 0)
            {
                idef_index = doc.m_instance_definition_table.AddInstanceDefinition(idef, idef_objects.ToArray(), false, bQuietly);
            }
            else if (doc.m_instance_definition_table.ModifyInstanceDefinitionGeometry(found_index, idef_objects.ToArray(), bQuietly))
            {
                idef_index = found_index;
                doc.m_instance_definition_table.ModifyInstanceDefinition(idef, idef_index, (uint)IDEF_SETTINGS.all_idef_settings, bQuietly);

                if (!bQuietly)
                {
                    IRhinoInstanceObject[] iref_object_list = null;
                    int iref_count = doc.m_instance_definition_table[found_index].GetReferences(out iref_object_list);
                    if (iref_count > 0)
                    {
                        RhUtil.RhinoApp().Print(string.Format("{0} Instances of block \"{1}\" have been updated.\n", iref_count, idef.Name()));
                    }
                }
            }

            if (idef_index < 0 && !bQuietly)
            {
                string message = (found_index < 0) ? "Error creating block.\n" : "Error modifying block.\n";
                RhUtil.RhinoApp().Print(message);
            }

            return(idef_index);
        }