using System;
using System.Windows.Forms;
using Rhino;
namespace TestingVisualTech
{
public partial class LampParametersForm : Form
{
// Declare all the parameters as properties
public double Height { get; private set; } = 320;
public double BaseRadius { get; private set; } = 100;
public double NeckRadius { get; private set; } = 80;
public double TwistAmount { get; private set; } = 2 * Math.PI * 0.5;
public double MainWaveDepth { get; private set; } = 0.15;
public int MainWaveCount { get; private set; } = 15;
public double TextureDepth { get; private set; } = 0.28;
public int TextureCount { get; private set; } = 60;
public int SectionCount { get; private set; } = 30;
public int PointsPerSection { get; private set; } = 150;
public LampParametersForm()
{
InitializeComponent();
SetupForm();
}
private void SetupForm()
{
this.Text = "Lamp Parameters";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.Size = new System.Drawing.Size(400, 600);
// Create controls
CreateSlider("Height", 100, 500, 320, 10, UpdateHeight);
CreateSlider("Base Radius", 50, 200, 100, 5, UpdateBaseRadius);
CreateSlider("Neck Radius", 30, 150, 80, 5, UpdateNeckRadius);
CreateSlider("Twist Amount", 0, Math.PI * 2, Math.PI * 0.5, 0.1, UpdateTwistAmount);
CreateSlider("Main Wave Depth", 0.05, 0.5, 0.15, 0.01, UpdateMainWaveDepth);
CreateSlider("Main Wave Count", 5, 30, 15, 1, UpdateMainWaveCount);
CreateSlider("Texture Depth", 0.05, 0.5, 0.28, 0.01, UpdateTextureDepth);
CreateSlider("Texture Count", 20, 100, 60, 1, UpdateTextureCount);
CreateSlider("Section Count", 10, 50, 30, 1, UpdateSectionCount);
CreateSlider("Points Per Section", 50, 300, 150, 5, UpdatePointsPerSection);
// Add generate button
var generateButton = new Button
{
Text = "Generate Lamp",
Size = new System.Drawing.Size(150, 40),
Location = new System.Drawing.Point(125, 520)
};
generateButton.Click += GenerateButton_Click;
this.Controls.Add(generateButton);
}
private void CreateSlider(string label, double min, double max, double value, double increment, Action<double> updateAction)
{
var yPos = 20 + (this.Controls.Count * 50);
var lbl = new Label
{
Text = label,
Location = new System.Drawing.Point(20, yPos),
Size = new System.Drawing.Size(150, 20)
};
this.Controls.Add(lbl);
var trackBar = new TrackBar
{
Minimum = 0,
Maximum = 100,
Value = (int)((value - min) / (max - min) * 100),
TickFrequency = 10,
Location = new System.Drawing.Point(20, yPos + 20),
Size = new System.Drawing.Size(300, 45)
};
this.Controls.Add(trackBar);
var valueLabel = new Label
{
Text = value.ToString(),
Location = new System.Drawing.Point(330, yPos + 20),
Size = new System.Drawing.Size(50, 20)
};
this.Controls.Add(valueLabel);
trackBar.Scroll += (sender, e) =>
{
double newValue = min + (max - min) * trackBar.Value / 100;
newValue = Math.Round(newValue / increment) * increment;
valueLabel.Text = newValue.ToString();
updateAction(newValue);
};
}
private void UpdateHeight(double value) => Height = value;
private void UpdateBaseRadius(double value) => BaseRadius = value;
private void UpdateNeckRadius(double value) => NeckRadius = value;
private void UpdateTwistAmount(double value) => TwistAmount = value;
private void UpdateMainWaveDepth(double value) => MainWaveDepth = value;
private void UpdateMainWaveCount(double value) => MainWaveCount = (int)value;
private void UpdateTextureDepth(double value) => TextureDepth = value;
private void UpdateTextureCount(double value) => TextureCount = (int)value;
private void UpdateSectionCount(double value) => SectionCount = (int)value;
private void UpdatePointsPerSection(double value) => PointsPerSection = (int)value;
private void GenerateButton_Click(object sender, EventArgs e)
{
// Run the command with current parameters
RhinoApp.RunScript($"_-RunPythonScript \"{GetPythonScript()}\"", false);
}
private string GetPythonScript()
{
return $@"
import Rhino
import System
import math
# Get the current document
doc = Rhino.RhinoDoc.ActiveDoc
# Parameters from form
height = {Height}
baseRadius = {BaseRadius}
neckRadius = {NeckRadius}
twistAmount = {TwistAmount}
mainWaveDepth = {MainWaveDepth}
mainWaveCount = {MainWaveCount}
textureDepth = {TextureDepth}
textureCount = {TextureCount}
sectionCount = {SectionCount}
pointsPerSection = {PointsPerSection}
sections = []
for i in range(sectionCount):
t = float(i) / (sectionCount - 1)
z = t * height
profileFactor = math.sin(math.pi * t)
radius = baseRadius + (neckRadius - baseRadius) * profileFactor
twist = twistAmount * t
pts = []
for j in range(pointsPerSection):
angle = 2 * math.pi * j / pointsPerSection
twistedAngle = angle + twist
mainWaveDelta = mainWaveDepth * math.sin(mainWaveCount * twistedAngle)
textureDelta = textureDepth * math.sin(textureCount * twistedAngle)
combinedModulation = 1.0 + mainWaveDelta + textureDelta
r = radius * combinedModulation
x = r * math.cos(angle)
y = r * math.sin(angle)
pts.append(Rhino.Geometry.Point3d(x, y, z))
pts.append(pts[0])
curve = Rhino.Geometry.Curve.CreateInterpolatedCurve(pts, 3)
if curve and curve.IsValid:
sections.append(curve)
loft = Rhino.Geometry.Brep.CreateFromLoft(sections, Rhino.Geometry.Point3d.Unset, Rhino.Geometry.Point3d.Unset, Rhino.Geometry.LoftType.Tight, False)
if loft and len(loft) > 0:
resultBrep = loft[0]
resultBrep.CapPlanarHoles(doc.ModelAbsoluteTolerance)
doc.Objects.AddBrep(resultBrep)
doc.Views.Redraw()
print("Lamp generated successfully")
else:
print("Error generating loft")
";
}
}
}