protected override void WriteDocument(RhinoDoc doc, BinaryArchiveWriter archive, FileWriteOptions options)
        {
            var dict = new Rhino.Collections.ArchivableDictionary(1, "RigiBodyData");

            //If user save during animation, save the shapes at the time when start was pressed
            if (!TimePanel.Instance.Restarted || TimePanel.Instance.TrackbarValue != 0)
            {
                List <RigidBody> copyToAdd = new List <RigidBody>();;
                for (int i = 0; i < RigidBodyManager.World.RigidBodies.Count; i++)
                {
                    copyToAdd.Add(RigidBodyManager.DuplicateRigidBody(RigidBodyManager.FramesRigidBodies[0][i], RigidBodyManager.FramesRigidBodies[0][i].IsStatic));
                }
                dict.Set("rigidBodies", RigidBodyManager.ObjectToByteArray(copyToAdd));
            }
            else
            {
                dict.Set("rigidBodies", RigidBodyManager.ObjectToByteArray(RigidBodyManager.RigidBodies));
            }

            dict.Set("guidList", RigidBodyManager.ObjectToByteArray(RigidBodyManager.GuidList));
            dict.Set("geometryList", RigidBodyManager.ObjectToByteArray(RigidBodyManager.GeometryList));
            dict.Set("MaxFrameBoxValue", (int)TimePanel.Instance.MaxFrameBoxValue);
            dict.Set("worldCount", RigidBodyManager.World.RigidBodies.Count);
            archive.WriteDictionary(dict);
        }
 protected override void ReadDocument(RhinoDoc doc, BinaryArchiveReader archive, FileReadOptions options)
 {
     RigidBodyManager.World = new World(new CollisionSystemSAP());
     Rhino.Collections.ArchivableDictionary dict = archive.ReadDictionary();
     RigidBodyManager.GuidList           = (List <Guid>)RigidBodyManager.ByteArrayToObject(dict["guidList"] as byte[]);
     RigidBodyManager.RigidBodies        = (List <RigidBody>)RigidBodyManager.ByteArrayToObject(dict["rigidBodies"] as byte[]);
     RigidBodyManager.GeometryList       = (List <Brep>)RigidBodyManager.ByteArrayToObject(dict["geometryList"] as byte[]);
     TimePanel.Instance.MaxFrameBoxValue = (int)dict["MaxFrameBoxValue"];
     //Reset
     TimePanel.Instance.ResetNotSavable();
     WorldCount = (int)dict["worldCount"];
 }
Exemplo n.º 3
0
        public void DrawGeometry(int i, RigidBody body)
        {
            RhinoDoc.ActiveDoc.Objects.Unlock(RigidBodyManager.GuidList[i], true);
            Brep currentBrep = RigidBodyManager.GeometryList[i];

            //Create copy of Brep
            Brep copyToAdd = currentBrep.DuplicateBrep();
            //Rotate the rhino object
            Transform trafo = RigidBodyManager.MatrixTransfRotation(body);

            trafo = trafo.Transpose();
            copyToAdd.Transform(trafo);

            //Translate and so now the center of the bounding boxes are the same
            copyToAdd.Translate(body.BoundingBox.Center.X, body.BoundingBox.Center.Y, body.BoundingBox.Center.Z);

            RhinoDoc.ActiveDoc.Objects.Replace(RigidBodyManager.GuidList[i], copyToAdd);
            RhinoDoc.ActiveDoc.Objects.Lock(RigidBodyManager.GuidList[i], true);
        }
Exemplo n.º 4
0
 private void Restart_Click(object sender, EventArgs e)
 {
     restarted             = true;
     Restart.Enabled       = !pause;
     Pause.Enabled         = !pause;
     Play.Enabled          = pause;
     FrameTrackBar.Value   = 0;
     FrameTrackBar.Enabled = false;
     MaxFrameBox.Enabled   = true;
     ActualFrame.Text      = "Frame 0";
     for (int i = 0; i < RigidBodyManager.World.RigidBodies.Count; i++)
     {
         //I remove the actual and I insert another
         RigidBodyManager.World.RemoveBody(RigidBodyManager.RigidBodies[i]);
         RigidBody rigidCopyToAdd = RigidBodyManager.DuplicateRigidBody(RigidBodyManager.FramesRigidBodies[0][i], RigidBodyManager.FramesRigidBodies[0][i].IsStatic);
         RigidBodyManager.World.AddBody(rigidCopyToAdd);
         RigidBodyManager.RigidBodies[i] = rigidCopyToAdd;
         DrawGeometry(i, rigidCopyToAdd);
         RhinoDoc.ActiveDoc.Objects.Unlock(RigidBodyManager.GuidList[i], true);
     }
     RigidBodyManager.FramesRigidBodies.Clear();
 }
Exemplo n.º 5
0
        private void RhinoDocOnBeforeTransformObjects(object sender, RhinoTransformObjectsEventArgs ea)
        {
            RhinoObject[] rhinoObjects = ea.Objects;
            Vector3d      traslation;
            Transform     transform;

            ea.Transform.DecomposeAffine(out transform, out traslation);
            transform = transform.Transpose();

            for (int i = 0; i < rhinoObjects.Length; i++)
            {
                //Se il guid é presente nella lista allora fa parte delle jitter forme
                if (RigidBodyManager.GuidList.Contains(rhinoObjects[i].Id))
                {
                    int index = RigidBodyManager.GuidList.IndexOf(rhinoObjects[i].Id);
                    //If it is a rigid transformation (so translation and rotation)
                    if (ea.Transform.RigidType == TransformRigidType.Rigid)
                    {   //Rotate the body
                        JMatrix rotation = RigidBodyManager.RigidBodies[index].Orientation * new JMatrix((float)transform.M00, (float)transform.M01, (float)transform.M02, (float)transform.M10, (float)transform.M11, (float)transform.M12, (float)transform.M20, (float)transform.M21, (float)transform.M22);
                        RigidBodyManager.RigidBodies[index].Orientation = rotation;
                        //Move the center of mass of Jitter shape on the center of the BBox of rhino shape
                        Brep rhinoobj = (Brep)(ea.Objects[i].Geometry).Duplicate();
                        rhinoobj.Transform(ea.Transform);
                        Point3d centerBbox = rhinoobj.GetBoundingBox(true).Center;
                        RigidBodyManager.RigidBodies[index].Position = RigidBodyManager.Point3dtoJVector(centerBbox);
                        //Find the difference between rhino bbx center and jitter bbox center
                        JVector bboxjitter = RigidBodyManager.RigidBodies[index].BoundingBox.Center;
                        JVector diff       = bboxjitter - RigidBodyManager.Point3dtoJVector(centerBbox);
                        //Align the center of both bboxes
                        RigidBodyManager.RigidBodies[index].Position -= diff;
                    }
                    else
                    {
                        RhinoApp.WriteLine("You can't apply a non rigid transformation to a shape that has a rigid body property");
                        rigidTransf = false;
                    }
                }
            }
        }
Exemplo n.º 6
0
        private void Play_Click(object sender, EventArgs e)
        {
            //Get max frames from panel label
            maxFrame = (int)MaxFrameBox.Value;
            FrameTrackBar.Maximum    = (int)MaxFrameBox.Value - 1;
            FrameProgressBar.Maximum = FrameTrackBar.Maximum;
            MaxFrameBox.Enabled      = false;
            //I focuse on pause beacuse when I run Play the button Play remain focused.
            //With this command I remove the focus on the Play button
            Pause.Focus();
            pause                 = false;
            Restart.Enabled       = pause;
            Play.Enabled          = pause;
            Pause.Enabled         = !pause;
            FrameTrackBar.Enabled = false;

            if (restarted)
            {
                //If user delete some obects, remove them from lists
                for (int i = 0; i < RigidBodyManager.World.RigidBodies.Count; i++)
                {
                    Guid        currentGuid        = RigidBodyManager.GuidList[i];
                    RhinoObject currentRhinoObject = RhinoDoc.ActiveDoc.Objects.Find(currentGuid);
                    if (currentRhinoObject == null)
                    {
                        DeleteJitterObject(i);
                        i--;
                    }
                }
            }

            if (RigidBodyManager.World.RigidBodies.Count != 0)
            {
                for (int f = FrameTrackBar.Value; f < maxFrame && !pause; f++)
                {
                    FrameTrackBar.Value = f;
                    ActualFrame.Text    = "Frame " + f;
                    // If it is the first time, it saves the frames otherwise read them
                    if (restarted)
                    {
                        //Array of rigid bodies in this single frame
                        List <RigidBody> rigidBodyFrame = new List <RigidBody>(RigidBodyManager.World.RigidBodies.Count);
                        for (int i = 0; i < RigidBodyManager.World.RigidBodies.Count; i++)//for every rigid body in the world
                        {
                            RigidBody body = RigidBodyManager.RigidBodies[i];
                            DrawGeometry(i, body);

                            //Add the rigidBody form into the array that contains every rigidBody of a frame
                            rigidBodyFrame.Add(RigidBodyManager.DuplicateRigidBody(body, body.IsStatic));
                        }
                        //you can interact with the doc during the simulation
                        RhinoApp.Wait();
                        RhinoDoc.ActiveDoc.Views.Redraw();
                        RigidBodyManager.World.Step(1.0f / 25.0f, true);

                        //Add the RigidBody array of this single frame to the list that contains all frames
                        RigidBodyManager.FramesRigidBodies.Add(rigidBodyFrame);
                    }
                    else
                    {                                                                      //Only read frames and not save them
                        for (int i = 0; i < RigidBodyManager.World.RigidBodies.Count; i++) //The number of rigid bodies in the world is equal to the lenght of the array in every frame (frames are rappresented by framesRigidBodies)
                        {
                            RigidBody bodyOfFrame = RigidBodyManager.FramesRigidBodies[f][i];
                            DrawGeometry(i, bodyOfFrame);
                        }
                        RhinoApp.Wait();
                        RhinoDoc.ActiveDoc.Views.Redraw();
                    }
                }

                if (Pause.Enabled)
                {
                    Pause_Click(null, null);
                }

                if (FrameTrackBar.Value == maxFrame - 1)
                {
                    Play.Enabled          = false;
                    restarted             = false;
                    FrameTrackBar.Enabled = true;
                }
            }
        }
Exemplo n.º 7
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            if (TimePanel.Instance.Restarted && TimePanel.Instance.TrackbarValue == 0)
            {
                ObjRef[] objrefs;
                Result   rc = Rhino.Input.RhinoGet.GetMultipleObjects("Select the polysurfaces for the union",
                                                                      false, ObjectType.Brep, out objrefs);
                if (rc != Result.Success)
                {
                    return(rc);
                }
                if (objrefs == null || objrefs.Length <= 1)
                {
                    Dialogs.ShowMessage("You have to select 2 or more shapes.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                    return(Result.Failure);
                }


                List <Brep> in_breps0 = new List <Brep>();
                for (int i = 0; i < objrefs.Length; i++)
                {
                    int index = RigidBodyManager.GuidList.IndexOf(objrefs[i].ObjectId);
                    //Avoid to create a compound from another compound
                    if (RigidBodyManager.RigidBodies[index].Shape is CompoundShape)
                    {
                        Dialogs.ShowMessage("You cannot create compound shape from another compound shape. Try to create it at once.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                        return(Result.Failure);
                    }
                    //Accept shapes only if they intersect to each other
                    Brep brep = objrefs[i].Brep();
                    if (brep != null)
                    {
                        in_breps0.Add(brep);
                    }
                    RhinoDoc.ActiveDoc.Objects.Delete(objrefs[i], true);
                }

                //Create the rhino compound shape
                double tolerance = doc.ModelAbsoluteTolerance;
                Brep[] breps     = Brep.CreateBooleanUnion(in_breps0, tolerance);
                if (breps.Length > 1)
                {
                    Dialogs.ShowMessage("You cannot create more than a compound shape in once time.", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                    return(Result.Failure);
                }

                Brep rhinoCompound = breps[0];
                // If the user create zero or more than 1 compound the command fails
                if (breps.Length != 1)
                {
                    return(Rhino.Commands.Result.Nothing);
                }

                Brep copyToAdd = rhinoCompound.DuplicateBrep();

                //Create the rigid compound shape
                CompoundShape.TransformedShape[] transformedShapes = new CompoundShape.TransformedShape[in_breps0.Count];

                for (int i = 0; i < in_breps0.Count; i++)
                {
                    Guid guid           = objrefs[i].ObjectId;
                    int  indexRigidBody = RigidBodyManager.GuidList.IndexOf(guid);
                    transformedShapes[i] = new CompoundShape.TransformedShape(RigidBodyManager.RigidBodies[indexRigidBody].Shape, RigidBodyManager.RigidBodies[indexRigidBody].Orientation, RigidBodyManager.RigidBodies[indexRigidBody].Position);
                }
                CompoundShape jCompound     = new CompoundShape(transformedShapes);
                RigidBody     jCompoundBody = new RigidBody(jCompound);

                //Move the center of mass of Jitter shape on the center of the BBox of rhino shape
                Point3d centerBbox = rhinoCompound.GetBoundingBox(true).Center;
                jCompoundBody.Position = RigidBodyManager.Point3dtoJVector(centerBbox);
                //Find the difference between rhino bbx center and jitter bbox center
                JVector bboxjitter = jCompoundBody.BoundingBox.Center;
                JVector diff       = bboxjitter - RigidBodyManager.Point3dtoJVector(centerBbox);
                //Align the center of both bboxes
                jCompoundBody.Position -= diff;

                //Translate the center of the Bbox to 0,0,0 and save it to geometry list
                Point3d bboxDoc = rhinoCompound.GetBoundingBox(true).Center;
                rhinoCompound.Translate(new Vector3d(-bboxDoc.X, -bboxDoc.Y, -bboxDoc.Z));

                RigidBodyManager.RigidBodies.Add(jCompoundBody);
                RigidBodyManager.GeometryList.Add(rhinoCompound);
                Guid guidToAdd = doc.Objects.Add(copyToAdd);
                RigidBodyManager.GuidList.Add(guidToAdd);

                doc.Views.Redraw();

                return(Result.Success);
            }
            else
            {
                Dialogs.ShowMessage("Press Restart before use other commands", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                return(Result.Success);
            }
        }
Exemplo n.º 8
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            if (TimePanel.Instance.Restarted && TimePanel.Instance.TrackbarValue == 0)
            {
                RhinoApp.WriteLine("Now it's time to draw a box");

                Point3d pt0;
                using (GetPoint getPointAction = new GetPoint())
                {
                    getPointAction.SetCommandPrompt("First corner of the base");
                    if (getPointAction.Get() != GetResult.Point) //getPointAction.Get() rimane in attesa del punto
                    {
                        RhinoApp.WriteLine("No corner point was selected.");
                        return(getPointAction.CommandResult());
                    }
                    pt0 = getPointAction.Point();
                    if (pt0.Z != 0)
                    {
                        RhinoApp.WriteLine("The base of the square is not on the plane XY");
                        return(getPointAction.CommandResult());
                    }
                }

                Point3d pt1;
                using (GetPoint getPointAction = new GetPoint())
                {
                    getPointAction.SetCommandPrompt("Other corner of the base");

                    getPointAction.SetBasePoint(pt0, true);

                    getPointAction.DynamicDraw +=
                        (sender, e) =>
                    {
                        e.Display.DrawLine(pt0, new Point3d(pt0.X, e.CurrentPoint.Y, 0), Color.Black);
                        e.Display.DrawLine(pt0, new Point3d(e.CurrentPoint.X, pt0.Y, 0), Color.Black);
                        e.Display.DrawLine(new Point3d(pt0.X, e.CurrentPoint.Y, 0), e.CurrentPoint, Color.Black);
                        e.Display.DrawLine(new Point3d(e.CurrentPoint.X, pt0.Y, 0), e.CurrentPoint, Color.Black);
                    };

                    if (getPointAction.Get() != GetResult.Point) //getPointAction.Get() rimane in attesa del punto
                    {
                        RhinoApp.WriteLine("No corner point was selected.");
                        return(getPointAction.CommandResult());
                    }
                    pt1 = getPointAction.Point();
                    if (pt1.Z != 0)
                    {
                        RhinoApp.WriteLine("The base of the square is not on the plane XY");
                        return(getPointAction.CommandResult());
                    }
                    if (pt1.Equals(pt0))
                    {
                        RhinoApp.WriteLine("The second point is the same of the first");
                        return(getPointAction.CommandResult());
                    }
                }

                Point3d pt2;
                using (GetPoint getPointAction = new GetPoint())
                {
                    getPointAction.SetCommandPrompt("Height");

                    getPointAction.SetBasePoint(pt1, true);
                    var line = new Rhino.Geometry.Line(pt1, new Point3d(pt1.X, pt1.Y, 1));
                    getPointAction.Constrain(line);

                    getPointAction.DynamicDraw +=
                        (sender, e) =>
                    {
                        e.Display.DrawLine(pt0, new Point3d(pt0.X, pt1.Y, 0), Color.Black);
                        e.Display.DrawLine(pt0, new Point3d(pt1.X, pt0.Y, 0), Color.Black);
                        e.Display.DrawLine(new Point3d(pt0.X, pt1.Y, 0), pt1, Color.Black);
                        e.Display.DrawLine(new Point3d(pt1.X, pt0.Y, 0), pt1, Color.Black);

                        e.Display.DrawLine(new Point3d(pt0.X, pt0.Y, e.CurrentPoint.Z), new Point3d(pt0.X, pt1.Y, e.CurrentPoint.Z), Color.Black);
                        e.Display.DrawLine(new Point3d(pt0.X, pt0.Y, e.CurrentPoint.Z), new Point3d(pt1.X, pt0.Y, e.CurrentPoint.Z), Color.Black);
                        e.Display.DrawLine(new Point3d(pt0.X, pt1.Y, e.CurrentPoint.Z), e.CurrentPoint, Color.Black);
                        e.Display.DrawLine(new Point3d(pt1.X, pt0.Y, e.CurrentPoint.Z), e.CurrentPoint, Color.Black);

                        e.Display.DrawLine(pt0, new Point3d(pt0.X, pt0.Y, e.CurrentPoint.Z), Color.Black);
                        e.Display.DrawLine(pt1, new Point3d(pt1.X, pt1.Y, e.CurrentPoint.Z), Color.Black);
                        e.Display.DrawLine(new Point3d(pt0.X, pt1.Y, e.CurrentPoint.Z), new Point3d(pt0.X, pt1.Y, 0), Color.Black);
                        e.Display.DrawLine(new Point3d(pt1.X, pt0.Y, e.CurrentPoint.Z), new Point3d(pt1.X, pt0.Y, 0), Color.Black);
                    };
                    if (getPointAction.Get() != GetResult.Point) //getPointAction.Get() rimane in attesa del punto
                    {
                        RhinoApp.WriteLine("No Height point was selected.");
                        return(getPointAction.CommandResult());
                    }
                    pt2 = getPointAction.Point();
                    if (pt2.Z == 0)
                    {
                        RhinoApp.WriteLine("The height of the box must be different of 0");
                        return(getPointAction.CommandResult());
                    }
                }

                //Find center of the box
                Point3d middleDiagonal = new Point3d((pt0.X + pt1.X) / 2, (pt0.Y + pt1.Y) / 2, 0);
                Point3d middleHeight   = new Point3d(0, 0, (pt1.Z + pt2.Z) / 2);
                Point3d centerBox      = new Point3d(middleDiagonal.X, middleDiagonal.Y, middleHeight.Z);
                //Find dimension of the box
                Shape     boxShape = new BoxShape((float)Math.Abs(pt1.X - pt0.X), (float)Math.Abs(pt1.Y - pt0.Y), (float)Math.Abs(pt2.Z));
                RigidBody rigidBox = new RigidBody(boxShape);
                rigidBox.Position = new JVector((float)centerBox.X, (float)centerBox.Y, (float)centerBox.Z);

                Box box = new Box(new BoundingBox(RigidBodyManager.JVectorToPoint3d(boxShape.BoundingBox.Min), RigidBodyManager.JVectorToPoint3d(boxShape.BoundingBox.Max)));


                //Original one with the center in 0,0,0
                Brep brepBox = box.ToBrep();
                //Copy to translate and rotate
                Brep copyToAdd = brepBox.DuplicateBrep();
                //Move the box to the correct position
                copyToAdd.Translate(centerBox.X, centerBox.Y, centerBox.Z);

                RigidBodyManager.RigidBodies.Add(rigidBox);
                RigidBodyManager.GeometryList.Add(brepBox);
                RigidBodyManager.GuidList.Add(doc.Objects.Add(copyToAdd));

                doc.Views.Redraw();

                return(Result.Success);
            }
            else
            {
                Dialogs.ShowMessage("Press Restart before use other commands", "Warning", ShowMessageButton.OK, ShowMessageIcon.Warning);
                return(Result.Success);
            }
        }