Skip to content

Commit 36a0495

Browse files
committed
working
1 parent abac7c0 commit 36a0495

11 files changed

Lines changed: 114 additions & 25 deletions

File tree

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"configurations": [
44
{
55
"name": ".NET Core Launch (console)",
6-
"type": "coreclr",
6+
"type": "dotnet",
77
"request": "launch",
88
"preLaunchTask": "build",
99
"program": "${workspaceFolder}/OpenSharpCAD/bin/Debug/net10.0/CSharpCAD.dll",

ApiTest/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ private static CsgObject Render()
1212
{
1313
CsgObject box = new Box(100, 100, 100);
1414
//return box;
15-
//CsgObject cylinder = new Cylinder(50, 50, 50);
16-
// return cylinder;
15+
CsgObject cylinder = new Cylinder(50, 50, 50);
16+
return cylinder;
1717
//CsgObject tor = new Torus(10, 20);
1818
//return tor;
1919

CSharpCad.Test/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using CSharpCAD;
44
using MatterHackers.Csg;
5+
using Microsoft.CodeAnalysis;
56

67
namespace CSharpCad.Test
78
{
@@ -15,7 +16,7 @@ static void Main(string[] args)
1516
string script = "Draw(new Box(8, 20, 10));";
1617

1718
ICompilerService compiler = new CompilerService();
18-
List<string> errors;
19+
List<Diagnostic> errors;
1920

2021
var result = compiler.Compile(script, out errors);
2122

CSharpCad.UnitTests/CompilerServiceTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using CSharpCAD;
22
using NUnit.Framework;
3+
using Microsoft.CodeAnalysis;
4+
using System.Collections.Generic;
35

46
namespace CSharpCad.UnitTests;
57

@@ -11,7 +13,7 @@ public void TestCompileValidScript()
1113
{
1214
string script = "Draw(new Box(8, 20, 10));";
1315
ICompilerService compiler = new CompilerService();
14-
List<string> errors;
16+
List<Diagnostic> errors;
1517

1618
var result = compiler.Compile(script, out errors);
1719

@@ -24,7 +26,7 @@ public void TestCompileInvalidScript()
2426
{
2527
string script = "InvalidMethod();";
2628
ICompilerService compiler = new CompilerService();
27-
List<string> errors;
29+
List<Diagnostic> errors;
2830

2931
var result = compiler.Compile(script, out errors);
3032

MatterCadGui/CsgEdtors/CsgEditorBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public static GuiWidget CreateEditorForCsg(CsgObject csgObject)
2828
{
2929
return new CsgEditorTranslate((Translate)csgObject);
3030
}
31+
else if (csgObject.GetType() == typeof(Cylinder.CylinderPrimitive))
32+
{
33+
return new CsgEditorCylinder((Cylinder.CylinderPrimitive)csgObject);
34+
}
3135
else
3236
{
3337
Debug.WriteLine(csgObject.GetType());
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using MatterHackers.Csg.Solids;
3+
using MatterHackers.Agg.UI;
4+
5+
namespace MatterHackers.MatterCadGui.CsgEditors
6+
{
7+
public class CsgEditorCylinder : FlowLayoutWidget
8+
{
9+
Cylinder.CylinderPrimitive target;
10+
public CsgEditorCylinder(Cylinder.CylinderPrimitive target)
11+
{
12+
this.target = target;
13+
14+
AddChild(new TextWidget("Cylinder R1,R2,H,S"));
15+
16+
AddNumberEdit(target.Radius1, (val) => target.Radius1 = val, 0);
17+
AddNumberEdit(target.Radius2, (val) => target.Radius2 = val, 1);
18+
AddNumberEdit(target.Height, (val) => target.Height = val, 2);
19+
AddNumberEdit(target.Sides, (val) => target.Sides = (int)val, 3);
20+
}
21+
22+
private void AddNumberEdit(double initialValue, Action<double> setter, int index)
23+
{
24+
NumberEdit editControl = new NumberEdit(initialValue, pixelWidth: 35, allowDecimals: true)
25+
{
26+
TabIndex = index
27+
};
28+
editControl.InternalNumberEdit.TextChanged += (sender, e) =>
29+
{
30+
setter(editControl.Value);
31+
};
32+
AddChild(editControl);
33+
}
34+
}
35+
}

OpenSharpCAD/CompilerService/CompilerService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace CSharpCAD
77
{
88
public class CompilerService : ICompilerService
99
{
10-
public object Compile(string scriptSource, out List<string> errors)
10+
public object Compile(string scriptSource, out List<Diagnostic> errors)
1111
{
1212
errors = [];
1313
StringBuilder sb = new();
@@ -18,6 +18,7 @@ public object Compile(string scriptSource, out List<string> errors)
1818
sb.AppendLine("using System.Collections.Generic;");
1919
sb.AppendLine("using MatterHackers.VectorMath; ");
2020
sb.AppendLine("using MatterHackers.Csg.Operations; ");
21+
sb.AppendLine("using MatterHackers.Csg.Transform; ");
2122
sb.AppendLine("using MatterHackers.Csg;");
2223
sb.AppendLine("using MatterHackers.Csg.Solids; ");
2324
sb.AppendLine("using MatterHackers.RenderOpenGl; ");
@@ -54,6 +55,7 @@ public object Compile(string scriptSource, out List<string> errors)
5455
var classRef = DynCode.CodeHelper.HelperFunction(classCode, "Test.RenderTest", new object[]
5556
{
5657
typeof(System.Console).Assembly.Location,
58+
typeof(MatterHackers.Csg.Transform.Translate).Assembly.Location,
5759
typeof(MatterHackers.Csg.CsgObject).Assembly.Location,
5860
typeof(MatterHackers.VectorMath.Vector3).Assembly.Location,
5961
typeof(MatterHackers.Agg.Graphics2D).Assembly.Location,
@@ -63,18 +65,16 @@ public object Compile(string scriptSource, out List<string> errors)
6365

6466
if (classRef is List<Diagnostic> diagnostics)
6567
{
66-
foreach (var error in diagnostics)
67-
{
68-
errors.Add(error.ToString());
69-
}
68+
errors = diagnostics;
7069
return null;
7170
}
7271

7372
return classRef;
7473
}
7574
catch (Exception ex)
7675
{
77-
errors.Add(ex.Message);
76+
// Create a basic diagnostic if something else fails
77+
Console.WriteLine(ex.Message);
7878
return null;
7979
}
8080
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System.Collections.Generic;
2+
using Microsoft.CodeAnalysis;
23

34
namespace CSharpCAD
45
{
56
public interface ICompilerService
67
{
7-
object Compile(string scriptSource, out List<string> errors);
8+
object Compile(string scriptSource, out List<Diagnostic> errors);
89
}
910
}

OpenSharpCAD/MainWindow.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class MainWindow : SystemWindow
7373
private FlowLayoutWidget objectEditorList;
7474
private FlowLayoutWidget textSide;
7575
private TextEditWidget hello;
76+
private ListBox errorListBox;
7677
private Union rootUnion = new Union("root");
7778
dynamic classRef;
7879

@@ -110,7 +111,15 @@ public MainWindow(bool renderRayTrace) : base(800, 600)
110111
};
111112

112113
var code = new StringBuilder();
113-
code.AppendLine("Draw(new Box(8, 20, 10));");
114+
code.AppendLine("var holesize =4;");
115+
code.AppendLine("// draw a box");
116+
code.AppendLine("var box = new Box(20, 20, 20);");
117+
code.AppendLine("var rotated = new Rotate(box, x: MathHelper.DegreesToRadians(45));");
118+
code.AppendLine("var cylinder = new Cylinder(50,20,60);");
119+
code.AppendLine("// draw the rotated box on top of the original");
120+
code.AppendLine("var translated = new Translate(new Box(40, 10, 10), 0, holesize, holesize);");
121+
code.AppendLine("Draw(box + rotated - translated + cylinder);");
122+
114123

115124
hello = new TextEditWidget(code.ToString().Replace('\r', '\n'))
116125
{
@@ -125,23 +134,34 @@ public MainWindow(bool renderRayTrace) : base(800, 600)
125134

126135
{
127136
// panel 2 stuff
128-
FlowLayoutWidget renderSide = new FlowLayoutWidget(FlowDirection.TopToBottom)
137+
Splitter rightSplitter = new Splitter()
129138
{
130139
HAnchor = HAnchor.Stretch,
131140
VAnchor = VAnchor.Stretch,
141+
Orientation = Orientation.Horizontal,
142+
SplitterDistance = 150,
132143
};
144+
verticalSplitter.Panel2.AddChild(rightSplitter);
133145

134146
var world = new WorldView(800, 600);
135147
world.TranslationMatrix = Matrix4X4.CreateTranslation(0, 0, -50);
136-
trackBallWidget = new TrackballTumbleView(world, renderSide)
148+
trackBallWidget = new TrackballTumbleView(world, rightSplitter.Panel1)
137149
{
138150
HAnchor = HAnchor.Stretch,
139151
VAnchor = VAnchor.Stretch,
140152
DrawContent = glLightedView_DrawGlContent,
141153
TransformState = MatterHackers.VectorMath.TrackBall.TrackBallTransformType.Rotation,
142154
};
143-
renderSide.AddChild(trackBallWidget);
144-
verticalSplitter.Panel2.AddChild(renderSide);
155+
rightSplitter.Panel1.AddChild(trackBallWidget);
156+
157+
errorListBox = new ListBox()
158+
{
159+
HAnchor = HAnchor.Stretch,
160+
VAnchor = VAnchor.Stretch,
161+
BackgroundColor = Color.White,
162+
};
163+
errorListBox.SelectedValueChanged += ErrorListBox_SelectedValueChanged;
164+
rightSplitter.Panel2.AddChild(errorListBox);
145165
}
146166

147167
BackgroundColor = Color.White;
@@ -151,27 +171,42 @@ public MainWindow(bool renderRayTrace) : base(800, 600)
151171

152172
private void Hello_TextChanged(object sender, EventArgs e)
153173
{
174+
hello.ErrorLineIndices.Clear();
154175
Compile();
155176
}
156177

178+
private void ErrorListBox_SelectedValueChanged(object sender, EventArgs e)
179+
{
180+
if (errorListBox.SelectedItem is ListBoxTextItem item)
181+
{
182+
if (int.TryParse(item.ItemValue, out int line))
183+
{
184+
hello.JumpToLine(line);
185+
}
186+
}
187+
}
188+
157189
private void Compile()
158190
{
159191
var compilerService = new CompilerService();
160-
List<string> errors;
192+
List<Diagnostic> errors;
161193

162194
// The service expects just the content inside Render(), which comes from hello.Text
163195
classRef = compilerService.Compile(hello.Text, out errors);
164196

197+
errorListBox.Clear();
198+
165199
if (errors.Count > 0)
166200
{
167-
StringBuilder sberror = new StringBuilder();
168201
foreach (var error in errors)
169202
{
170-
sberror.AppendLine(error);
203+
var lineSpan = error.Location.GetLineSpan();
204+
int line = lineSpan.StartLinePosition.Line - 17; // offset from preamble
205+
string message = $"{line + 1}: {error.GetMessage()}";
206+
errorListBox.AddChild(new ListBoxTextItem(message, line.ToString()));
207+
hello.ErrorLineIndices.Add(line);
171208
}
172-
// TODO: Display errors to user (e.g., txtErrors.Text = sberror.ToString())
173-
// For now, logging as before might be done by caller or added here if needed,
174-
// but simpler to just return.
209+
hello.Invalidate();
175210
return;
176211
}
177212
trackBallWidget.Invalidate();

OpenSharpCAD/Shapes.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using MatterHackers.Csg.Solids;
55
using MatterHackers.RenderOpenGl;
66
using MatterHackers.Agg;
7+
using MatterHackers.Csg;
78

89
namespace CSharpCAD
910
{
@@ -33,5 +34,15 @@ public static void Box(this Union rootUnion, double sizeX, double sizeY, double
3334
{
3435
rootUnion.Add(new Box(sizeX, sizeY, sizeZ));
3536
}
37+
38+
public static void Cylinder(this Union rootUnion, double radius, double height, int sides = 40, Alignment alignment = Alignment.z, string name = "")
39+
{
40+
rootUnion.Add(new Cylinder(radius, height, sides, alignment, name));
41+
}
42+
43+
public static void Cylinder(this Union rootUnion, double radius1, double radius2, double height, int sides = 40, Alignment alignment = Alignment.z, string name = "")
44+
{
45+
rootUnion.Add(new Cylinder(radius1, radius2, height, sides, alignment, name));
46+
}
3647
}
3748
}

0 commit comments

Comments
 (0)