I need a form to control the paramethers with sliders for this code. The form has not to block rhino, so i must be able to use it
using System;
using System.Collections.Generic;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
namespace TestingVisualTech
{
public class Lamp : Command
{
public Lamp()
{
Instance = this;
}
code
///<summary>The only instance of this command.</summary>
public static Lamp Instance { get; private set; }
public override string EnglishName => "SemplifiedTexturedLamp";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// --- Parametri Generali ---
double height = 320;
double baseRadius = 100;
double neckRadius = 80;
double twistAmount = 2 * Math.PI * 0.5;
// --- Parametri Onde Principali ---
double mainWaveDepth = 0.15;
int mainWaveCount = 15;
// --- Parametri Texture Verticale ---
double textureDepth = 0.28;
// RIDOTTO textureCount per linee verticali meno fitte
int textureCount = 60;
// --- Risoluzione (RIDOTTA per semplicità) ---
// RIDOTTO sectionCount e pointsPerSection
int sectionCount = 30;
int pointsPerSection = 150;
var sections = new List<Curve>();
for (int i = 0; i < sectionCount; i++)
{
double t = (double)i / (sectionCount - 1);
double z = t * height;
double profileFactor = Math.Sin(Math.PI * t);
double radius = baseRadius + (neckRadius - baseRadius) * profileFactor;
double twist = twistAmount * t;
var pts = new List<Point3d>();
for (int j = 0; j < pointsPerSection; j++)
{
double angle = 2 * Math.PI * j / pointsPerSection;
double twistedAngle = angle + twist;
double mainWaveDelta = mainWaveDepth * Math.Sin(mainWaveCount * twistedAngle);
double textureDelta = textureDepth * Math.Sin(textureCount * twistedAngle);
double combinedModulation = 1.0 + mainWaveDelta + textureDelta;
double r = radius * combinedModulation;
double x = r * Math.Cos(angle);
double y = r * Math.Sin(angle);
pts.Add(new Point3d(x, y, z));
}
pts.Add(pts[0]);
var curve = Curve.CreateInterpolatedCurve(pts, 3, CurveKnotStyle.Uniform);
if (curve != null && curve.IsValid)
sections.Add(curve);
}
var loft = Brep.CreateFromLoft(sections, Point3d.Unset, Point3d.Unset, LoftType.Tight, false);
if (loft != null && loft.Length > 0)
{
Brep resultBrep = loft[0];
resultBrep.CapPlanarHoles(doc.ModelAbsoluteTolerance);
doc.Objects.AddBrep(resultBrep);
doc.Views.Redraw();
RhinoApp.WriteLine("Lampada semplificata con trama verticale generata.");
return Result.Success;
}
RhinoApp.WriteLine("Errore nella generazione del Loft.");
return Result.Failure;
}
}
}
Assistant
Of course! This is a classic and very useful pattern for Rhino plugin development. To achieve a non-blocking form (also known as a modeless form) with sliders, you need to separate your UI logic from your geometry generation logic.
Here's a complete, step-by-step guide and the refactored code to achieve this.
Core Concepts
Modeless Form: Instead of using form.ShowDialog(), which blocks Rhino, we will use form.Show(). This lets the user interact with both the form and the Rhino viewport simultaneously.
Decoupling Logic: The geometry creation code will be moved out of the RunCommand method into its own public method. The RunCommand method will only be responsible for creating and showing the form.
Communication: The form will hold a reference to the command instance. When a slider is moved, the form will call the public geometry creation method on the command instance, passing the new parameter values.
Live Update: To make the update "live," we'll delete the previously generated object and add the new one each time a slider changes. We'll keep track of the object's ID (Guid) to do this.
Step 1: Create the Windows Form
In your Visual Studio project:
Right-click on your project in the Solution Explorer.
Go to Add > Form (Windows Forms...).
Name it LampControlForm.cs and click Add.
If you don't have a reference to System.Windows.Forms, you'll need to add it. Right-click References > Add Reference... and check System.Windows.Forms and System.Drawing.
Now, design the form. Drag Label and TrackBar controls from the Toolbox onto your form for each parameter. It might look something like this:
Crucially, give each control a meaningful name in the Properties window:
Configure the TrackBars:
For each TrackBar, set the Minimum and Maximum properties. Since TrackBar only works with integers, we'll use a trick: for decimal values, we'll use a large range (like 0-100) and divide by 100.0 in the code.
tbHeight: Min: 50, Max: 500
tbTwist: Min: 0, Max: 628 (we'll divide by 100 to get a range of 0 to 6.28, i.e., 2*PI)
tbWaveDepth: Min: 0, Max: 50 (we'll divide by 100)
Step 2: Refactor Your Code
We will now split the original code into two files: the command that launches the form, and the form's code-behind that handles the UI events.
File 1: LampCommand.cs (The Main Command)
This class is now much simpler. Its only jobs are to manage the form instance and contain the geometry generation logic that the form can call.
csharp
using System;
using System.Collections.Generic;
using Rhino;
using Rhino.Commands;
using Rhino.Geometry;
using Rhino.UI; // Required for RhinoWin.MainWindow
namespace TestingVisualTech
{
public class InteractiveLampCommand : Command
{
// --- Fields for managing the form and the lamp object ---
private LampControlForm _form;
private Guid _lastLampId = Guid.Empty;
public InteractiveLampCommand()
{
Instance = this;
}
public static InteractiveLampCommand Instance { get; private set; }
public override string EnglishName => "InteractiveLamp";
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Check if the form is already open
if (_form != null && _form.Visible)
{
_form.Focus();
return Result.Success;
}
// Create and show a new modeless form
// We pass 'this' so the form can call back to our RegenerateLamp method
_form = new LampControlForm(this);
// The magic line: Show() is modeless, ShowDialog() is modal (blocking)
// We pass the main Rhino window as the owner
_form.Show(RhinoWin.MainWindow);
return Result.Success;
}
/// <summary>
/// This method contains the core geometry generation logic.
/// It's now public so the form can call it.
/// </summary>
public void RegenerateLamp(
double height, double baseRadius, double neckRadius, double twistAmount,
double mainWaveDepth, int mainWaveCount,
double textureDepth, int textureCount)
{
var doc = RhinoDoc.ActiveDoc;
if (doc == null) return;
// --- Delete the previous lamp object before creating a new one ---
if (_lastLampId != Guid.Empty)
{
doc.Objects.Delete(_lastLampId, true);
_lastLampId = Guid.Empty;
}
// --- GEOMETRY GENERATION LOGIC (Copied from your original command) ---
int sectionCount = 30;
int pointsPerSection = 150;
var sections = new List<Curve>();
for (int i = 0; i < sectionCount; i++)
{
double t = (double)i / (sectionCount - 1);
double z = t * height;
double profileFactor = Math.Sin(Math.PI * t);
double radius = baseRadius + (neckRadius - baseRadius) * profileFactor;
double twist = twistAmount * t;
var pts = new List<Point3d>();
for (int j = 0; j < pointsPerSection; j++)
{
double angle = 2 * Math.PI * j / pointsPerSection;
double twistedAngle = angle + twist;
double mainWaveDelta = mainWaveDepth * Math.Sin(mainWaveCount * twistedAngle);
double textureDelta = textureDepth * Math.Sin(textureCount * twistedAngle);
double combinedModulation = 1.0 + mainWaveDelta + textureDelta;
double r = radius * combinedModulation;
double x = r * Math.Cos(angle);
double y = r * Math.Sin(angle);
pts.Add(new Point3d(x, y, z));
}
pts.Add(pts[0]);
var curve = Curve.CreateInterpolatedCurve(pts, 3, CurveKnotStyle.Uniform);
if (curve != null && curve.IsValid)
sections.Add(curve);
}
var loft = Brep.CreateFromLoft(sections, Point3d.Unset, Point3d.Unset, LoftType.Tight, false);
if (loft != null && loft.Length > 0)
{
Brep resultBrep = loft[0];
resultBrep.CapPlanarHoles(doc.ModelAbsoluteTolerance);
// --- Add the new object and store its ID ---
_lastLampId = doc.Objects.AddBrep(resultBrep);
doc.Views.Redraw();
}
}
}
}
File 2: LampControlForm.cs (The Form's Code)
This is the code-behind for your form. It handles reading slider values and telling the main command to update the geometry.
csharp
using System;
using System.Windows.Forms;
namespace TestingVisualTech
{
// Make sure your form's class name matches what you created in the designer
public partial class LampControlForm : Form
{
// A reference to our command, so we can call its RegenerateLamp method
private readonly InteractiveLampCommand _command;
// Constructor that accepts the command instance
public LampControlForm(InteractiveLampCommand command)
{
_command = command;
// This is a standard method call for WinForms initialization
InitializeComponent();
// Hook up all the TrackBar scroll events to a single handler
// Do this for every TrackBar you have on the form
tbHeight.Scroll += Parameter_Changed;
tbBaseRadius.Scroll += Parameter_Changed;
tbNeckRadius.Scroll += Parameter_Changed;
tbTwist.Scroll += Parameter_Changed;
tbWaveDepth.Scroll += Parameter_Changed;
tbWaveCount.Scroll += Parameter_Changed;
tbTextureDepth.Scroll += Parameter_Changed;
tbTextureCount.Scroll += Parameter_Changed;
// Set the initial values for the sliders when the form opens
InitializeSliders();
// Trigger the first lamp generation
UpdateRhino();
}
private void InitializeSliders()
{
// Set initial values based on your defaults
// Note: For decimals, we multiply by our factor (e.g., 100)
tbHeight.Value = 320;
tbBaseRadius.Value = 100;
tbNeckRadius.Value = 80;
tbTwist.Value = (int)(Math.PI * 100); // 0.5 * 2 * PI = PI. PI * 100 ~ 314
tbWaveDepth.Value = 15; // 0.15 * 100
tbWaveCount.Value = 15;
tbTextureDepth.Value = 28; // 0.28 * 100
tbTextureCount.Value = 60;
}
/// <summary>
* This single event handler is called whenever any slider is moved.
* </summary>
private void Parameter_Changed(object sender, EventArgs e)
{
UpdateRhino();
}
/// <summary>
* Reads all slider values, updates labels, and calls the command to regenerate the lamp.
* </summary>
private void UpdateRhino()
{
// --- Read values from sliders and convert them to the correct type (double/int) ---
// For doubles, we divide by the factor we used earlier
double height = tbHeight.Value;
double baseRadius = tbBaseRadius.Value;
double neckRadius = tbNeckRadius.Value;
double twistAmount = tbTwist.Value / 100.0;
double mainWaveDepth = tbWaveDepth.Value / 100.0;
int mainWaveCount = tbWaveCount.Value;
double textureDepth = tbTextureDepth.Value / 100.0;
int textureCount = tbTextureCount.Value;
// --- Update the labels on the form to show the current value ---
lblHeightValue.Text = height.ToString("F0");
lblBaseRadiusValue.Text = baseRadius.ToString("F0");
lblNeckRadiusValue.Text = neckRadius.ToString("F0");
lblTwistValue.Text = twistAmount.ToString("F2");
lblWaveDepthValue.Text = mainWaveDepth.ToString("F2");
lblWaveCountValue.Text = mainWaveCount.ToString();
lblTextureDepthValue.Text = textureDepth.ToString("F2");
lblTextureCountValue.Text = textureCount.ToString();
// --- Tell the command to regenerate the geometry with the new values ---
_command.RegenerateLamp(
height, baseRadius, neckRadius, twistAmount,
mainWaveDepth, mainWaveCount,
textureDepth, textureCount
);
}
}
}
How to Use
Build your plugin in Visual Studio.
Run Rhino and load your plugin (.rhp file).
Type the command InteractiveLamp in the Rhino command line.
The form will appear, and you can now move the sliders. The lamp in the viewport will update in real-time without blocking Rhino.