I want to reCreate the Radial Dimension and Diameter Dimension of Rhino with C#
Assistant
User
these commands take all kind of objects that could have radius or diameter
Like Circle,Arc,Nurbes Curve, ...
Assistant
To recreate Radial and Diameter Dimensions in Rhino using C#, you'll typically use the RhinoCommon library, which is the .NET SDK for Rhino. Here's a concise overview of how you may go about doing this.
Setting Up
Create a New Plugin: If you haven’t already, you'll need to create a new Rhino plugin. You can do this using the RhinoCommon template in Visual Studio.
Reference necessary assemblies: Make sure to reference RhinoCommon.dll in your project.
Creating Radial and Diameter Dimensions
Below are sample snippets demonstrating how to create a radius dimension and a diameter dimension in Rhino using C#.
Example Code
Creating a Radial Dimension
csharp
using System;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Rhino.DocObjects;
public class CreateRadialDimensionCommand : Command
{
public override string EnglishName => "CreateRadialDimension";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Assuming you already have a circle with a certain radius in your document
// You may also want to add code to pick a circle or create one for this example
Circle circle = new Circle(Plane.WorldXY, 5.0); // Example circle with radius 5
// Add the circle to the document
doc.Objects.AddCircle(circle);
// Create a Radial Dimension
Point3d dimensionPoint = new Point3d(circle.Center.X, circle.Center.Y + 6, 0); // Above circle center
double radius = circle.Radius;
DimensionStyle dimensionStyle = doc.DimStyles.CurrentDimStyle;
var radialDim = new RadialDimension(circle.Center, radius, dimensionPoint);
radialDim.DimensionStyle = dimensionStyle;
// Add the radial dimension to the document
doc.Objects.AddRadialDimension(radialDim);
doc.Views.Redraw();
return Result.Success;
}
}
Creating a Diameter Dimension
csharp
using System;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
public class CreateDiameterDimensionCommand : Command
{
public override string EnglishName => "CreateDiameterDimension";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Assuming you already have a circle with a certain radius in your document
Circle circle = new Circle(Plane.WorldXY, 5.0); // Example circle with radius 5
// Add the circle to the document
doc.Objects.AddCircle(circle);
// Create a Diameter Dimension
Point3d dimensionPoint = new Point3d(circle.Center.X + 7, circle.Center.Y, 0); // Right of circle center
double radius = circle.Radius;
DimensionStyle dimensionStyle = doc.DimStyles.CurrentDimStyle;
var diameterDim = new DiameterDimension(circle.Center, dimensionPoint, radius);
diameterDim.DimensionStyle = dimensionStyle;
// Add the diameter dimension to the document
doc.Objects.AddDiameterDimension(diameterDim);
doc.Views.Redraw();
return Result.Success;
}
}
Notes:
Coordinate and Geometry: The circle's center and the points for dimensioning should be set according to your needs. You might want to implement user input to select existing circles instead of hard-coding values.
Using Dimension Styles: Ensure that you're using the appropriate dimension style for your dimensional annotations, which can be retrieved from the doc.DimStyles.
Testing Commands: To test these commands, compile your plugin and load it into Rhino. You can then type CreateRadialDimension or CreateDiameterDimension in the command line.
Make sure to build upon these examples and handle edge cases like user input and instances where circles do not exist as needed for your specific implementation.
To create radial and diameter dimensions in Rhino using C# that can work with various types of geometric objects (like circles, arcs, and NURBS curves), you will need to ensure that your command can correctly identify and handle these types. Below are modified versions of the previous examples that include object selection and checks for valid geometry.
Complete Example
The following command will prompt the user to select an object (Circle, Arc, or NURBS Curve) and will create the corresponding dimension based on the radius or diameter of the selected object.
Command: CreateRadialOrDiameterDimension
csharp
using System.Runtime.CompilerServices;
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Input;
using Rhino.Input.Custom;
public class CreateRadialOrDiameterDimensionCommand : Command
{
public override string EnglishName => "CreateRadialOrDiameterDimension";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Get the object from the user
var go = new GetObject();
go.SetCommandPrompt("Select a circle, arc, or NURBS curve");
go.SubObjectSelect = false;
go.EnableOnlyDrawnObjects(true);
go.EnableSelectOnly(false);
go.Get();
if (go.CommandResult() != Result.Success)
return go.CommandResult();
// Get the selected object
var objRef = go.Object(0);
if (objRef == null)
return Result.Nothing;
// Check the geometry type
var curve = objRef.Curve();
if (curve == null)
return Result.Failure;
// Attempt to get the radius/diameter
double radius;
Point3d center;
if (curve.IsCircle(out var circle))
{
center = circle.Center;
radius = circle.Radius;
}
else if (curve.IsArc(out var arc))
{
center = arc.Center;
radius = arc.Radius;
}
else if (curve.TryGetNurbsCurve(out var nurbsCurve))
{
// Assume a simple check for circles within NURBS
if (nurbsCurve.IsClosed && nurbsCurve.GetLength() == 2 * Math.PI * nurbsCurve.GetBoundingBox(True).Diagonal.Length / 2)
{
center = nurbsCurve.PointAtStart; // Simplistic method
radius = nurbsCurve.GetLength() / (2 * Math.PI); // Approximate interpretation
}
else
{
RhinoApp.WriteLine("Selected NURBS does not represent a valid circle or arc.");
return Result.Failure;
}
}
else
{
RhinoApp.WriteLine("Selected object is not a valid circle, arc, or NURBS curve.");
return Result.Failure;
}
// Select where to place the dimension (simple logic for demonstration)
var dimensionPoint = new Point3d(center.X + radius + 1, center.Y, center.Z);
DimensionStyle dimensionStyle = doc.DimStyles.CurrentDimStyle;
// Create a Radial Dimension
if (curve.IsCircle() || curve.IsArc())
{
var radialDim = new RadialDimension(center, radius, dimensionPoint);
radialDim.DimensionStyle = dimensionStyle;
doc.Objects.AddRadialDimension(radialDim);
}
// Create a Diameter Dimension
else if (curve.TryGetNurbsCurve(out _))
{
var diameterDim = new DiameterDimension(center, dimensionPoint, radius);
diameterDim.DimensionStyle = dimensionStyle;
doc.Objects.AddDiameterDimension(diameterDim);
}
// Finalize
doc.Views.Redraw();
return Result.Success;
}
}
Explanation
Selection of Object: This command uses the GetObject class to allow users to select any curve.
Type Checking: It checks if the selected object is a Circle, Arc, or NURBS curve using the IsCircle and IsArc methods.
Extracting Geometry: For circles and arcs, it retrieves the radius and center. For NURBS curves, a simple assumption is made that closed curves can represent circles, and a basic approximation is used to estimate the circle's properties.
Dimension Creation: Depending on whether a circle or arc is selected, a Radial Dimension is created. If the selected object is a valid NURBS curve, a Diameter Dimension can be created based on how you decide to interpret it.
Final Steps: The command then adds the dimension to the document and redraws the view to reflect changes.
Customizations
You may want to refine the logic for detecting valid circles within NURBS curves or analyze how you want to deal with constraints on where to place dimensions. Additionally, user interaction can be expanded to allow for more configuration, such as selecting where to position the dimension label or adjusting scale factors.
Testing
Compile your plugin and load it into Rhino. You can then run the command by typing CreateRadialOrDiameterDimension in the command line, and test it with different types of curves.
Feel free to extend functionality and improve user experience by allowing custom placements, enhanced prompts, and better initializations based on your plugin's use case!