Skip to content

Commit

Permalink
Simplified Unit Types
Browse files Browse the repository at this point in the history
Removed all unit options except inches and millimeters, made units consistent with stl loading and the entries in the property grid (speeds, etc.).
  • Loading branch information
xenovacivus committed Apr 17, 2023
1 parent 29e8f47 commit 5e52a58
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 57 deletions.
76 changes: 48 additions & 28 deletions GUI/PathCAM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public PathCAM()

settings = new Settings(robotControl.GetRobot(), router);
propertyGrid.SelectedObject = settings;
comboBox1.SelectedIndex = 0;
}

void Drawing3D_DragLeave(object sender, EventArgs e)
Expand Down Expand Up @@ -263,28 +264,28 @@ private void clearPathsButton_Click(object sender, EventArgs e)
}

private float loadObjectScale = 1.0f;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
float targetScale = 0.0f;
float sourceScale = 0.0f;

var match = new Regex(@"^(?<source>\S+):(?<target>\S+)").Match(comboBox1.Text);

if (match.Success
&& float.TryParse(match.Groups["source"].Value, out sourceScale)
&& float.TryParse(match.Groups["target"].Value, out targetScale)
&& targetScale != 0 && sourceScale != 0)
{
comboBox1.BackColor = SystemColors.Window;
openFileButton.Enabled = true;
loadObjectScale = targetScale / sourceScale;
}
else
{
comboBox1.BackColor = Color.LightPink;
openFileButton.Enabled = false;
}
}
//private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
//{
// float targetScale = 0.0f;
// float sourceScale = 0.0f;
//
// var match = new Regex(@"^(?<source>\S+):(?<target>\S+)").Match(comboBox1.Text);
//
// if (match.Success
// && float.TryParse(match.Groups["source"].Value, out sourceScale)
// && float.TryParse(match.Groups["target"].Value, out targetScale)
// && targetScale != 0 && sourceScale != 0)
// {
// comboBox1.BackColor = SystemColors.Window;
// openFileButton.Enabled = true;
// loadObjectScale = targetScale / sourceScale;
// }
// else
// {
// comboBox1.BackColor = Color.LightPink;
// openFileButton.Enabled = false;
// }
//}

private void InitializeComponent()
{
Expand Down Expand Up @@ -323,6 +324,7 @@ private void InitializeComponent()
this.propertyGrid.Size = new System.Drawing.Size(183, 205);
this.propertyGrid.TabIndex = 5;
this.propertyGrid.ToolbarVisible = false;
this.propertyGrid.Click += new System.EventHandler(this.propertyGrid_Click);
//
// saveGcodeButton
//
Expand Down Expand Up @@ -360,19 +362,17 @@ private void InitializeComponent()
// comboBox1
//
this.comboBox1.BackColor = System.Drawing.SystemColors.Window;
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"1:1 (inches)",
"25.4:1 (millimeters)",
".254:1 (meters)",
"1:12 (feet)"});
"inches",
"millimeters"});
this.comboBox1.Location = new System.Drawing.Point(87, 129);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(107, 21);
this.comboBox1.TabIndex = 7;
this.comboBox1.Text = "1:1 (inches)";
this.comboBox1.TextChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged_1);
//
// openFileButton
//
Expand Down Expand Up @@ -495,5 +495,25 @@ private void robotControl_Load(object sender, EventArgs e)
{

}

private void propertyGrid_Click(object sender, EventArgs e)
{

}

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString().ToLower().Contains("inches"))
{
settings.ChangeUnitType(Settings.MeasurementUnitTypes.Inches);
loadObjectScale = 1.0f;
}
if (comboBox1.SelectedItem.ToString().ToLower().Contains("millimeters"))
{
settings.ChangeUnitType(Settings.MeasurementUnitTypes.Millimeters);
loadObjectScale = 1.0f / 25.4f;
}
propertyGrid.Refresh();
}
}
}
138 changes: 109 additions & 29 deletions GUI/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Globalization;
using System.Linq;
using System.Text;

Expand All @@ -15,79 +16,158 @@ public class Settings
{
private Robot.Robot robot;
private Router.Router router;

// Internally all units are in inches.
public enum MeasurementUnitTypes
{
Millimeters,
Inches,
};
MeasurementUnitTypes units;

public void ChangeUnitType(MeasurementUnitTypes newType)
{
units = newType;
}

private float ToDisplayUnits(float inches)
{
switch (units)
{
case MeasurementUnitTypes.Inches:
return inches;
case MeasurementUnitTypes.Millimeters:
return inches * 25.4f;
default:
return float.NaN; // Should never get here.
}
}

private float FromDisplayUnits(float value)
{
switch(units)
{
case MeasurementUnitTypes.Inches:
return value;
case MeasurementUnitTypes.Millimeters:
return value / 25.4f;
default:
return float.NaN; // Should never get here.
}
}

public Settings(Robot.Robot robot, Router.Router router)
{
this.router = router;
this.robot = robot;
this.units = Settings.MeasurementUnitTypes.Millimeters;
}

///
/// Accessors for the property grid
///

//[DisplayName("Units")]
//[Description("Unit of Measurement (Millimeters or Inches)")]
//public string Units
//{
// get { return this.units.ToString(); }
// set {
// if (value.ToLower().StartsWith("i"))
// {
// this.units = MeasurementUnitTypes.Inches;
// }
// else if (value.ToLower().StartsWith("m"))
// {
// this.units = MeasurementUnitTypes.Millimeters;
// }
// }
//}

// https://learn.microsoft.com/en-us/answers/questions/1183869/when-displaying-float-or-double-values-in-property
public class FourDecimalPlaceConverter : SingleConverter
{
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is float)
{
//return ((float)value).ToString("N3");
return String.Format("{0:0.####}", (float)value);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}

[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Tab Height")]
[Description("Height of the tabs in inches")]
[Description("Height of the tabs")]
public float TabHeight
{
get { return router.TabHeight; }
set { router.TabHeight = value; }
get { return ToDisplayUnits(router.TabHeight); }
set { router.TabHeight = FromDisplayUnits(value); }
}

///
/// Accessors for the property grid
///
[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Last Pass Height")]
[Description("Height of the last pass in inches")]
[Description("Height of the last pass")]
public float LastPassHeight
{
get { return router.LastPassHeight; }
set { router.LastPassHeight = value; }
get { return ToDisplayUnits(router.LastPassHeight); }
set { router.LastPassHeight = FromDisplayUnits(value); }
}

[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Tool Diameter")]
[Description("Tool Diameter in inches")]
[Description("Tool Diameter")]
public float ToolDiameter
{
get { return router.ToolDiameter; }
set { router.ToolDiameter = value; }
get { return ToDisplayUnits(router.ToolDiameter); }
set { router.ToolDiameter = FromDisplayUnits(value); }
}

[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Move Height")]
[Description("Safe travel height")]
[Description("Tool height for rapid moves (set to a height above the workpiece and all clamps)")]
public float MoveHeight
{
get { return router.MoveHeight; }
set { router.MoveHeight = value; }
get { return ToDisplayUnits(router.MoveHeight); }
set { router.MoveHeight = FromDisplayUnits(value); }
}

[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Max Cut Depth")]
[Description("Maximum Cut Depth")]
[Description("All generated paths will be above this z-height.")]
public float MaxCutDepth
{
get { return router.MaxCutDepth; }
set { router.MaxCutDepth = value; }
get { return ToDisplayUnits(router.MaxCutDepth); }
set { router.MaxCutDepth = FromDisplayUnits(value); }
}



[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Cutting Speed")]
[Description("Cutting Speed (inches per minute)")]
[Description("Cutting Speed (units per minute)")]
public float RoutSpeed
{
get { return robot.MaxCutSpeed; }
set { robot.MaxCutSpeed = value; }
get { return ToDisplayUnits(robot.MaxCutSpeed); }
set { robot.MaxCutSpeed = FromDisplayUnits(value); }
}

[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Moving Speed")]
[Description("Rapid movement speed (inches per minute)")]
[Description("Rapid movement speed (units per minute)")]
public float MoveSpeed
{
get { return robot.MaxRapidSpeed; }
set { robot.MaxRapidSpeed = value; }
get { return ToDisplayUnits(robot.MaxRapidSpeed); }
set { robot.MaxRapidSpeed = FromDisplayUnits(value); }
}

[TypeConverter(typeof(FourDecimalPlaceConverter))]
[DisplayName("Max Z Speed")]
[Description("Maximum possible Z axis speed (inches per minute)")]
[Description("Maximum possible Z axis speed (units per minute)")]
public float MaxAxisSpeeds
{
get { return robot.MaxZSpeed; }
set { robot.MaxZSpeed = value; }
get { return ToDisplayUnits(robot.MaxZSpeed); }
set { robot.MaxZSpeed = FromDisplayUnits(value); }
}
}
}

0 comments on commit 5e52a58

Please sign in to comment.