public static Polyline Get2Point(string command, string command2)
{
var doc = RhinoDoc.ActiveDoc;
var points = new List();
Point3d pt0;
using (var getPointAction = new GetPoint())
{
getPointAction.SetCommandPrompt("Select the first corner of the rectangle:");
if (getPointAction.Get() != GetResult.Point)
{
RhinoApp.WriteLine("No point was selected.");
}
pt0 = getPointAction.Point();
}
using (var getPointAction = new GetPoint())
{
getPointAction.SetCommandPrompt("Select the opposite corner of the rectangle:");
getPointAction.SetBasePoint(pt0, true);
getPointAction.DynamicDraw += (sender, e) =>
{
Point3d pt1 = e.CurrentPoint;
code
// Assuming rectangle is aligned with the construction plane
Point3d pt2 = new Point3d(pt1.X, pt0.Y, pt0.Z);
Point3d pt3 = new Point3d(pt0.X, pt1.Y, pt1.Z);
// Draw the rectangle edges
e.Display.DrawPatternedLine(pt0, pt2, Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(pt2, pt1, Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(pt1, 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.");
}
Point3d pt1 = getPointAction.Point();
// (Optionally) Create the rectangle as a Polyline or Curve in Rhino
var rectangle = new Polyline(new[] { pt0, new Point3d(pt1.X, pt0.Y, pt0.Z), pt1, new Point3d(pt0.X, pt1.Y, pt1.Z), pt0 });
//doc.Objects.AddPolyline(rectangle);
doc.Views.Redraw();
return rectangle;
}
}
is it possible to get object snap on the rectangle i the 3rd point?
Assistant
Yes, it is possible to enable object snapping to the rectangle's corners or edges when selecting a third point in Rhino. However, achieving this requires making the rectangle geometry available for Rhino's object snapping mechanism. By default, dynamically drawn geometry (like your temporary rectangle) isn't recognized by Rhino's object snapping because it's not part of the document's object table.
Here are a few approaches to achieve object snapping to the rectangle:
1. Add the Rectangle as a Temporary Object
One straightforward method is to add the rectangle to the document as a temporary object. This allows Rhino's built-in object snapping to recognize and snap to its geometry. After the snapping operation, you can remove the temporary rectangle.
csharp
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Input.Custom;
using System.Collections.Generic;
public static Polyline Get2PointWithSnap(string command, string command2)
{
var doc = RhinoDoc.ActiveDoc;
var points = new List<Point3d>();
Point3d pt0;
// Get the first corner
using (var getPointAction = new GetPoint())
{
getPointAction.SetCommandPrompt("Select the first corner of the rectangle:");
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("Select the opposite corner of the rectangle:");
getPointAction.SetBasePoint(pt0, true);
getPointAction.DynamicDraw += (sender, e) =>
{
Point3d currentPt = e.CurrentPoint;
// 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, System.Drawing.Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(pt2, currentPt, System.Drawing.Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(currentPt, pt3, System.Drawing.Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(pt3, pt0, System.Drawing.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 rectangle = new Polyline(new[]
{
pt0,
new Point3d(pt1.X, pt0.Y, pt0.Z),
pt1,
new Point3d(pt0.X, pt1.Y, pt1.Z),
pt0
});
// Add the rectangle as a temporary object
var tempObjectId = doc.Objects.AddPolyline(rectangle);
doc.Views.Redraw();
// Now get the third point with object snaps enabled
Point3d pt3Snap;
using (var getPointSnap = new GetPoint())
{
getPointSnap.SetCommandPrompt("Select a point snapped to the rectangle:");
getPointSnap.EnableObjectSnap(true); // Enable standard object snaps
getPointSnap.GeometryFilter = ObjectType.Curve; // Optionally filter to curves
getPointSnap.SetBasePoint(pt1, true);
getPointSnap.DynamicDraw += (sender, e) =>
{
// Optional: provide custom dynamic feedback if needed
};
if (getPointSnap.Get() != GetResult.Point)
{
RhinoApp.WriteLine("No snapped point was selected.");
// Clean up the temporary object before exiting
doc.Objects.Delete(tempObjectId, false);
return null;
}
pt3Snap = getPointSnap.Point();
}
// Remove the temporary rectangle
doc.Objects.Delete(tempObjectId, false);
doc.Views.Redraw();
// Optionally, add the snapped point or use it as needed
// For example, add it to the points list
points.Add(pt3Snap);
// Return the rectangle polyline
return rectangle;
}
Explanation:
First Point Selection (pt0): The user selects the first corner of the rectangle.
Second Point Selection (pt1): The user selects the opposite corner with dynamic drawing of the rectangle.
Temporary Rectangle: After selecting pt1, the rectangle is added to the document as a temporary polyline (tempObjectId). This makes it recognizable by Rhino's object snapping.
Third Point Selection (pt3Snap): The user is prompted to select a third point, and object snapping is enabled. The user can snap to any part of the temporary rectangle.
Cleanup: The temporary rectangle is removed from the document after the snapping operation to keep the document clean.
2. Implement Custom Snapping Logic
If you prefer not to add the rectangle to the document, you can implement custom snapping by finding the nearest points on the rectangle geometry manually. Here's how you can achieve this:
csharp
Explanation:
First and Second Point Selection: Similar to the previous method, the user selects two points to define the rectangle with dynamic drawing.
Custom Snapping for Third Point: Instead of relying on Rhino's object snapping, this method manually calculates the closest point on the rectangle to where the user is pointing. It provides visual feedback by drawing the snapped point in red.
Finalizing the Snapped Point: After the user selects a point, the code snaps it to the nearest point on the rectangle and can further process it as needed.
3. Advanced: Custom Object Snapping with Gadgets
For more advanced snapping behavior, such as snapping to specific edges or implementing complex snapping rules, you might consider creating custom snapping gadgets. This approach involves more in-depth programming with Rhino's SDK and is generally more complex.
Note: Implementing custom snapping gadgets is beyond the scope of this answer, but it involves handling snapping events and possibly interacting with Rhino's lower-level APIs to create bespoke snapping behavior.
Summary
To enable object snapping to the rectangle's third point:
Temporary Object Addition: Add the rectangle to the document temporarily to leverage Rhino's built-in snapping.
Custom Snapping Logic: Implement manual snapping by calculating the nearest points on the rectangle.
Choose the method that best fits your application's requirements and complexity. The first method is generally the simplest and most effective for leveraging Rhino's existing capabilities.
Not yet included in a published release.
public static Polyline Get2PointWithCustomSnap(string command, string command2)
{
var doc = RhinoDoc.ActiveDoc;
var points = new List<Point3d>();
Point3d pt0;
// Get the first corner
using (var getPointAction = new GetPoint())
{
getPointAction.SetCommandPrompt("Select the first corner of the rectangle:");
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("Select the opposite corner of the rectangle:");
getPointAction.SetBasePoint(pt0, true);
getPointAction.DynamicDraw += (sender, e) =>
{
Point3d currentPt = e.CurrentPoint;
// 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, System.Drawing.Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(pt2, currentPt, System.Drawing.Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(currentPt, pt3, System.Drawing.Color.CornflowerBlue, 61680, 2);
e.Display.DrawPatternedLine(pt3, pt0, System.Drawing.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 rectangle = new Polyline(new[]
{
pt0,
new Point3d(pt1.X, pt0.Y, pt0.Z),
pt1,
new Point3d(pt0.X, pt1.Y, pt1.Z),
pt0
});
// Now get the third point with custom snapping to the rectangle
Point3d pt3Snap;
using (var getPointSnap = new GetPoint())
{
getPointSnap.SetCommandPrompt("Select a point snapped to the rectangle:");
getPointSnap.EnableObjectSnap(false); // Disable standard object snaps
getPointSnap.SetBasePoint(pt1, true);
getPointSnap.DynamicDraw += (sender, e) =>
{
// Find the closest point on the rectangle to the current cursor position
double t;
Point3d closestPt;
if (rectangle.ClosestPoint(e.CurrentPoint, out t, out closestPt))
{
// Draw the snapped point for visual feedback
e.Display.DrawPoint(closestPt, System.Drawing.Color.Red, 5);
// Optionally, you can display lines or other feedback
}
};
if (getPointSnap.Get() != GetResult.Point)
{
RhinoApp.WriteLine("No point was selected.");
return null;
}
// Snap the selected point to the closest point on the rectangle
if (rectangle.ClosestPoint(getPointSnap.Point(), out double t, out pt3Snap))
{
RhinoApp.WriteLine($"Point snapped to: {pt3Snap}");
// You can add further logic here, such as adding the point to the document
}
else
{
RhinoApp.WriteLine("Failed to snap to the rectangle.");
return null;
}
}
// Optionally, add the snapped point to the document or use it as needed
// For example:
// doc.Objects.AddPoint(pt3Snap);
// doc.Views.Redraw();
// Return the rectangle polyline
return rectangle;
}