Wednesday 16 November 2016

DiscoBall - Poking a stick at Rhino3D RhinoCommons






using System;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
using Rhino.DocObjects;

namespace CeeBeeTest
{
    [System.Runtime.InteropServices.Guid("845BC3BC-02FF-4ED8-A116-2B7E3FEB2293")]

    public class CeeBeeTest : Command
    {
        private bool m_escape_key_pressed;
        private int loop = 1;
        public CeeBeeTest()
        {Instance = this;}
        public static CeeBeeTest Instance
        {get; private set;}
        public override string EnglishName
        {get { return "DiscoBall"; }}

        void RhinoApp_EscapeKeyPressed(object sender, EventArgs e)
        {m_escape_key_pressed = true;}
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            RhinoApp.EscapeKeyPressed += RhinoApp_EscapeKeyPressed;
            RhinoApp.WriteLine("The {0} creates a sphere with a random colour.", EnglishName);
            m_escape_key_pressed = false;

            Point3d pt0;
            using (GetPoint getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt("Click starting point.");
                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No start point was selected.");
                    return getPointAction.CommandResult();
                }
                pt0 = getPointAction.Point();              
            }

            Point3d pt1;
            using (GetPoint getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt("Click end point.");
                getPointAction.SetBasePoint(pt0, true);
                getPointAction.DynamicDraw +=
                  (sender, e) => e.Display.DrawLine(pt0, e.CurrentPoint, System.Drawing.Color.DarkRed);
                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No end point was selected.");
                    return getPointAction.CommandResult();
                }
                pt1 = getPointAction.Point();
            }

            Sphere sphere1 = new Sphere(pt0, pt0.DistanceTo(pt1));
            Random rnd = new Random();
            var attributes = new ObjectAttributes();
            int cnt = 0;
            while (loop == 1) 
            {
                cnt++;             
                if (m_escape_key_pressed) loop = 2;
                int RED = rnd.Next(1, 255);
                int BLUE = rnd.Next(1, 255);
                int GREEN = rnd.Next(1, 255);
                RhinoApp.SetCommandPrompt("Running colour #" + cnt + " press [ESC] to exit");                
                attributes.ObjectColor = System.Drawing.Color.FromArgb(RED,BLUE,GREEN);
                attributes.ColorSource = ObjectColorSource.ColorFromObject;
                Guid thesphere = doc.Objects.AddSphere(sphere1, attributes);
                doc.Views.Redraw();            
                doc.Objects.Delete(thesphere, true);             
            }

            loop = 1;
            doc.Objects.AddSphere(sphere1, attributes);
            doc.Views.Redraw();
            RhinoApp.WriteLine("Congrats, you just made a disco ball! Radius {0} {1}.", pt0.DistanceTo(pt1), doc.ModelUnitSystem.ToString().ToLower());
            RhinoApp.EscapeKeyPressed -= RhinoApp_EscapeKeyPressed;
            return Result.Success;
        }
    }
}

No comments:

Post a Comment