Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

using Rhino; using Rhino.Geometry; using Rhino.Input.Custom; using Rhino.Input; using…

  1. User

    using Rhino; using Rhino.Geometry; using Rhino.Input.Custom; using Rhino.Input; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks;

    namespace EmreIstikam001.component.TehranOffice.Components.ÇİZİM_YARDIMCILARI.DetailSymbol { public static class RhinoGetPointHelper { /// /// Allows the user to select two points to define a rectangle. /// /// The prompt for the first corner. /// The prompt for the opposite corner. /// A Polyline representing the rectangle, or null if cancelled. public static Polyline GetRectangleFromUser(string prompt1, string prompt2) { var doc = RhinoDoc.ActiveDoc; var points = new List(); Point3d pt0;

            // Get the first corner of the rectangle
            using (var getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt(prompt1);
                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No point was selected.");
                    return null;
                }
                pt0 = getPointAction.Point();
            }
    
            Point3d pt1;
    
            // Get the opposite corner with dynamic drawing
            using (var getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt(prompt2);
                getPointAction.SetBasePoint(pt0, true);
                getPointAction.DynamicDraw += (sender, e) =>
                {
                    Point3d currentPt = e.CurrentPoint;
    
                    // Define the rectangle corners based on pt0 and currentPt
                    var rectangle = new Polyline(new[] {
                    pt0,
                    new Point3d(currentPt.X, pt0.Y, pt0.Z),
                    currentPt,
                    new Point3d(pt0.X, currentPt.Y, pt0.Z),
                    pt0 // Close the rectangle
                });
    
                    // Define other three corners based on pt0 and currentPt
                    Point3d pt2 = new Point3d(currentPt.X, pt0.Y, pt0.Z);
                    Point3d pt3 = new Point3d(pt0.X, currentPt.Y, pt0.Z);
    
                    // Draw temporary rectangle
                    e.Display.DrawPatternedLine(pt0, pt2, Color.CornflowerBlue, 61680, 2);
                    e.Display.DrawPatternedLine(pt2, currentPt, Color.CornflowerBlue, 61680, 2);
                    e.Display.DrawPatternedLine(currentPt, pt3, Color.CornflowerBlue, 61680, 2);
                    e.Display.DrawPatternedLine(pt3, pt0, Color.CornflowerBlue, 61680, 2);
                };
    
                if (getPointAction.Get() != GetResult.Point)
                {
                    RhinoApp.WriteLine("No end point was selected.");
                    return null;
                }
                pt1 = getPointAction.Point();
            }
    
            // Create the rectangle polyline
            var rectanglePolyline = new Polyline(new[] {
            pt0,
            new Point3d(pt1.X, pt0.Y, pt0.Z),
            pt1,
            new Point3d(pt0.X, pt1.Y, pt0.Z),
            pt0 // Close the rectangle
        });
    
            return rectanglePolyline;
        }
    
        /// <summary>
        /// Allows the user to select points for a leader annotation.
        /// </summary>
        /// <param name="prompt1">The prompt for the first point.</param>
        /// <param name="prompt2">The prompt for the second point.</param>
        /// <returns>A list of Point3d representing the leader points, or null if cancelled.</returns>
        public static void GetLeaderPointsFromUser(string prompt, RhinoInteractionHandler interactionHandler)
        {
            Point3d startPoint = Point3d.Unset;
            using (var getPointAction = new GetPoint())
            {
                getPointAction.SetCommandPrompt(prompt);
                getPointAction.AcceptNothing(true); // Allow pressing Enter to finish
                getPointAction.DynamicDraw += (sender, e) =>
                {
                    if (startPoint.IsValid)
                    {
                        // Draw the line from startPoint to current point
                        e.Display.DrawLine(startPoint, e.CurrentPoint, Color.CornflowerBlue, 2);
                    }
                };
    
                while (true)
                {
                    var res = getPointAction.Get();
                    if (interactionHandler.IsCancelled)
                    {
                        RhinoApp.WriteLine("Operation cancelled by user.");
                        break;
                    }
    
                    if (res == GetResult.Point)
                    {
                        if (!startPoint.IsValid)
                        {
                            // First point of the pair
                            startPoint = getPointAction.Point();
                            getPointAction.SetCommandPrompt("Select next point:");
                            getPointAction.SetBasePoint(startPoint, true);
                        }
                        else
                        {
                            // Second point of the pair
                            Point3d endPoint = getPointAction.Point();
    
                            // Process the pair
                            interactionHandler.ProcessLeaderPointPair(startPoint, endPoint, interactionHandler.IsCancelled);
    
                            // Reset startPoint
                            startPoint = Point3d.Unset;
    
                            // Reset command prompt
                            getPointAction.SetCommandPrompt("Select new Location or press Enter to finish:");
                        }
                    }
                    else if (res == GetResult.Nothing)
                    {
                        interactionHandler.GetGeometryData().Bake = true;
                        // User pressed Enter
                        break;
                    }
                    else
                    {
                        // Cancelled or error
                        RhinoApp.WriteLine("Point selection cancelled or invalid.");
                        break;
                    }
                }
            }
        }
    }
    

    }

    private void OnBakeClicked(object sender, EventArgs e) { Bake = true; // Expire the solution to trigger SolveInstance rhinoInteractionHandler.CancelOperation(); //rhinoInteractionHandler.ResetState(); // Implement this method as needed ExpireSolution(true); formManager.Dispose(); conduit.Disable(); RhinoDoc.ActiveDoc.Views.Redraw(); }

    I want to exit an active command if the Bake Event is trigered

  2. Assistant

    To exit an active GetPoint command when the Bake event is triggered, you should use RhinoApp.CancelAllGet(). This method cancels all active "Get" operations, allowing you to exit the GetPoint loop gracefully. Here's how you can modify your OnBakeClicked method:

    private void OnBakeClicked(object sender, EventArgs e)
    {
        Bake = true;
        // Cancel any active Get operations
        RhinoApp.CancelAllGet();
        rhinoInteractionHandler.CancelOperation();
        // Expire the solution to trigger SolveInstance
        ExpireSolution(true);
        formManager.Dispose();
        conduit.Disable();
        RhinoDoc.ActiveDoc.Views.Redraw();
    }
    

    In your GetLeaderPointsFromUser method, ensure you handle the GetResult.Cancel result properly:

    while (true)
    {
        var res = getPointAction.Get();
        if (interactionHandler.IsCancelled)
        {
            RhinoApp.WriteLine("Operation cancelled by user.");
            break;
        }
    
        if (res == GetResult.Point)
        {
            // Your existing code for handling points
        }
        else if (res == GetResult.Nothing)
        {
            // User pressed Enter
            interactionHandler.GetGeometryData().Bake = true;
            break;
        }
        else if (res == GetResult.Cancel)
        {
            // Get operation was cancelled
            RhinoApp.WriteLine("Get operation cancelled.");
            break;
        }
        else
        {
            // Other cases
            RhinoApp.WriteLine("Point selection cancelled or invalid.");
            break;
        }
    }
    

    By calling RhinoApp.CancelAllGet(), any active GetPoint operations will return GetResult.Cancel, allowing your loop to exit and the Bake operation to proceed. This ensures that the user interface remains responsive and the Bake event is handled correctly.