-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
493 lines (453 loc) · 28.6 KB
/
Copy pathMainForm.cs
File metadata and controls
493 lines (453 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Windows.Forms;
namespace PerfoSimulateur
{
public sealed class MainForm : Form
{
// Palette TolTem (identique aux autres outils : DeveloppeTube / Tronconique / CoudeDev / Profil)
private static readonly Color BG = Color.FromArgb(0x1B, 0x20, 0x27); // fond general / panneau gauche
private static readonly Color PANEL = Color.FromArgb(0x1B, 0x20, 0x27); // panneau gauche
private static readonly Color BAR = Color.FromArgb(0x20, 0x24, 0x2B); // barres haut/bas
private static readonly Color INPUT = Color.FromArgb(0x26, 0x2C, 0x35); // champs (num / combo)
private static readonly Color CANVAS = Color.FromArgb(0x14, 0x18, 0x1F); // zone de dessin
private static readonly Color FG = Color.FromArgb(0xE6, 0xEB, 0xF0); // texte
private static readonly Color MUTED = Color.FromArgb(0x8A, 0x95, 0xA2); // texte discret
private static readonly Color ACCENT = Color.FromArgb(0xFF, 0x7A, 0x1A); // orange TolTem
private static readonly Color BTN = Color.FromArgb(0x2B, 0x31, 0x3B); // bouton secondaire
private static readonly Color BTN_BORDER = Color.FromArgb(0x3A, 0x42, 0x4C);
private static readonly Color INKBTN = Color.FromArgb(0x1A, 0x0C, 0x00); // texte sur bouton orange
private const float COTE_OFF = 26f;
private sealed class DrawPanel : Panel { public DrawPanel() { DoubleBuffered = true; ResizeRedraw = true; } }
private NumericUpDown nLarg, nHaut, nEp, nRayon, nK, nA, nB, nEx, nEy, nMarge, nPli1, nPli2, nAng;
private ComboBox cbOutil, cbDispo, cbSens1, cbSens2;
private CheckBox chkCentrer, chkCoter;
private Label lblB, lblEy, lblDesign, lblStats, lblAng;
private DrawPanel canvas;
private Button btn2D, btn3D;
private GrilleResult _result;
private Geom3D _g3;
private bool _is3D;
private double _az = -0.65, _el = 1.05;
private bool _drag; private Point _lastMouse;
public MainForm()
{
// --- Base d'autoscale DPI ---
// Cette form ne fixait PAS sa police -> elle heritait du defaut WinForms
// (Microsoft Sans Serif 8.25). On la fixe sur Segoe UI 9pt (cohérent avec les
// controles) et on declare la base (7,15) correspondante. AutoScaleMode.Font
// scale alors ENSEMBLE tous les Location/Size absolus + la police vers 80/125/150%.
// A poser AVANT toute creation de controle.
AutoScaleDimensions = new SizeF(7f, 15f);
AutoScaleMode = AutoScaleMode.Font;
SuspendLayout();
Text = "PerfoSimulateur — TolTem";
BackColor = BG; ForeColor = FG;
Font = new Font("Segoe UI", 9f);
Width = 1240; Height = 780; StartPosition = FormStartPosition.CenterScreen;
MinimumSize = new Size(1040, 660);
TryLoadIcon();
var right = new Panel { Dock = DockStyle.Fill, BackColor = CANVAS };
var toolbar = new Panel { Dock = DockStyle.Top, Height = 36, BackColor = BAR };
btn2D = ViewButton("A plat (2D)", 8); btn3D = ViewButton("3D", 120);
var btnReset = new Button { Text = "Reset vue", Location = new Point(236, 5), Size = new Size(90, 26), FlatStyle = FlatStyle.Flat, BackColor = BTN, ForeColor = FG };
btnReset.FlatAppearance.BorderColor = BTN_BORDER; btnReset.Click += (s, e) => { _az = -0.65; _el = 1.05; canvas.Invalidate(); };
btn2D.Click += (s, e) => SetView(false); btn3D.Click += (s, e) => SetView(true);
toolbar.Controls.Add(btn2D); toolbar.Controls.Add(btn3D); toolbar.Controls.Add(btnReset);
canvas = new DrawPanel { Dock = DockStyle.Fill, BackColor = CANVAS };
canvas.Paint += Canvas_Paint;
canvas.MouseDown += (s, e) => { _drag = true; _lastMouse = e.Location; };
canvas.MouseUp += (s, e) => _drag = false;
canvas.MouseMove += Canvas_MouseMove;
right.Controls.Add(canvas); right.Controls.Add(toolbar);
Controls.Add(right);
Controls.Add(BuildLeftPanel());
Controls.Add(BuildBottomBar());
SetView(false);
Recompute();
ResumeLayout(false);
}
private Button ViewButton(string t, int x)
{
var b = new Button { Text = t, Location = new Point(x, 5), Size = new Size(108, 26), FlatStyle = FlatStyle.Flat, BackColor = BTN, ForeColor = FG };
b.FlatAppearance.BorderColor = BTN_BORDER; return b;
}
private void SetView(bool is3d)
{
_is3D = is3d;
btn2D.BackColor = is3d ? BTN : ACCENT; btn2D.ForeColor = is3d ? FG : INKBTN;
btn3D.BackColor = is3d ? ACCENT : BTN; btn3D.ForeColor = is3d ? INKBTN : FG;
canvas.Invalidate();
}
private void TryLoadIcon()
{
try
{
var asm = Assembly.GetExecutingAssembly();
foreach (var name in asm.GetManifestResourceNames())
if (name.EndsWith("toltem.ico", StringComparison.OrdinalIgnoreCase))
{
using var s = asm.GetManifestResourceStream(name);
if (s != null) { Icon = new Icon(s); break; }
}
}
catch { }
}
private Panel BuildLeftPanel()
{
var p = new Panel { Dock = DockStyle.Left, Width = 348, BackColor = PANEL, AutoScroll = true, Padding = new Padding(10) };
int y = 8;
void Header(string t) { p.Controls.Add(new Label { Text = t, ForeColor = ACCENT, Font = new Font("Segoe UI Semibold", 9.5f), Location = new Point(8, y), AutoSize = true }); y += 22; }
Label Lab(string t, int x, int w) { var l = new Label { Text = t, ForeColor = FG, Location = new Point(x, y + 3), Size = new Size(w, 18), Font = new Font("Segoe UI", 8.5f) }; p.Controls.Add(l); return l; }
NumericUpDown Num(int x, int w, double val, double min, double max, int dec, double inc)
{
var n = new NumericUpDown { Location = new Point(x, y), Width = w, DecimalPlaces = dec, Minimum = (decimal)min, Maximum = (decimal)max, Value = (decimal)val, Increment = (decimal)inc, BackColor = INPUT, ForeColor = FG, BorderStyle = BorderStyle.FixedSingle };
n.ValueChanged += (s, e) => Recompute(); p.Controls.Add(n); return n;
}
ComboBox Combo(int x, int w, string[] items, int sel)
{
var c = new ComboBox { Location = new Point(x, y), Width = w, DropDownStyle = ComboBoxStyle.DropDownList, BackColor = INPUT, ForeColor = FG, FlatStyle = FlatStyle.Flat };
c.Items.AddRange(items); c.SelectedIndex = sel; c.SelectedIndexChanged += (s, e) => Recompute(); p.Controls.Add(c); return c;
}
Header("TOLE FINIE (face)");
Lab("Largeur", 8, 70); nLarg = Num(118, 90, 400, 1, 100000, 1, 10); Lab("mm", 214, 30); y += 28;
Lab("Hauteur", 8, 70); nHaut = Num(118, 90, 300, 1, 100000, 1, 10); Lab("mm", 214, 30); y += 28;
Lab("Epaisseur", 8, 70); nEp = Num(118, 90, 1.5, 0.1, 50, 2, 0.1); Lab("mm", 214, 30); y += 34;
Header("DEVELOPPE (pli 90deg)");
Lab("Rayon pli", 8, 70); nRayon = Num(118, 90, 1.5, 0, 50, 2, 0.5); Lab("mm", 214, 30); y += 28;
Lab("Coef. k", 8, 70); nK = Num(118, 90, 0.40, 0, 0.5, 2, 0.05); y += 34;
Header("RETOMBEES (pourtour, cote 0 = sans pli)");
string[] sens = { "Interieur", "Exterieur" };
Lab("Pli 1 cote", 8, 70); nPli1 = Num(118, 64, 0, 0, 5000, 1, 1); Lab("mm", 186, 26); cbSens1 = Combo(216, 110, sens, 0); y += 28;
Lab("Pli 2 cote", 8, 70); nPli2 = Num(118, 64, 0, 0, 5000, 1, 1); Lab("mm", 186, 26); cbSens2 = Combo(216, 110, sens, 0); y += 32;
Header("PERFORATION");
Lab("Outil", 8, 70); cbOutil = Combo(118, 130, new[] { "Rond", "Carre", "Oblong", "Hexagone" }, 0);
cbOutil.SelectedIndexChanged += (s, e) => ToggleFields(); y += 28;
Lab("Dim A", 8, 70); nA = Num(118, 90, 6, 0.1, 5000, 2, 1); Lab("Ø/cote/L/ep", 214, 110); y += 28;
lblB = Lab("Larg. B", 8, 70); nB = Num(118, 90, 6, 0.1, 5000, 2, 1); Lab("oblong", 214, 60); y += 28;
lblAng = Lab("Angle poincon", 8, 105); nAng = Num(118, 90, 0, -180, 180, 1, 5); Lab("deg", 214, 30); y += 28;
Lab("Dispo.", 8, 70); cbDispo = Combo(118, 150, new[] { "Aligne (droit)", "Quinconce 60 (T)", "Quinconce 45" }, 1);
cbDispo.SelectedIndexChanged += (s, e) => ToggleFields(); y += 28;
Lab("Entraxe X (pas)", 8, 105); nEx = Num(118, 90, 10, 0.5, 5000, 2, 1); Lab("mm", 214, 30); y += 28;
lblEy = Lab("Entraxe Y", 8, 70); nEy = Num(118, 90, 10, 0.5, 5000, 2, 1); Lab("mm", 214, 30); y += 28;
Lab("Marge bord", 8, 70); nMarge = Num(118, 90, 20, 0, 5000, 1, 1); Lab("mm", 214, 30); y += 30;
chkCentrer = new CheckBox { Text = "Centrer le motif", Checked = true, ForeColor = FG, Location = new Point(10, y), AutoSize = true };
chkCentrer.CheckedChanged += (s, e) => Recompute(); p.Controls.Add(chkCentrer); y += 24;
chkCoter = new CheckBox { Text = "Coter le DXF (calque COTATION)", Checked = true, ForeColor = FG, Location = new Point(10, y), AutoSize = true };
chkCoter.CheckedChanged += (s, e) => Recompute(); p.Controls.Add(chkCoter); y += 30;
lblDesign = new Label { Text = "", ForeColor = ACCENT, Font = new Font("Consolas", 10f, FontStyle.Bold), Location = new Point(8, y), AutoSize = true };
p.Controls.Add(lblDesign); y += 24;
lblStats = new Label { Text = "", ForeColor = Color.Gainsboro, Font = new Font("Consolas", 9f), Location = new Point(8, y), AutoSize = true };
p.Controls.Add(lblStats); y += 110;
ToggleFields();
return p;
}
private void ToggleFields()
{
bool ob = cbOutil != null && cbOutil.SelectedIndex == 2;
if (lblB != null) lblB.Enabled = ob; if (nB != null) nB.Enabled = ob;
bool round = cbOutil != null && cbOutil.SelectedIndex == 0;
if (lblAng != null) lblAng.Enabled = !round; if (nAng != null) nAng.Enabled = !round;
bool aligned = cbDispo != null && cbDispo.SelectedIndex == 0;
if (lblEy != null) lblEy.Enabled = aligned; if (nEy != null) nEy.Enabled = aligned;
Recompute();
}
private Panel BuildBottomBar()
{
var bar = new Panel { Dock = DockStyle.Bottom, Height = 46, BackColor = PANEL, Padding = new Padding(8, 6, 8, 6) };
// Bouton DXF principal : orange plein, identique aux autres outils TolTem
var btnDxf = new Button { Text = "\u2913 Exporter le DXF", Location = new Point(8, 8), Size = new Size(168, 30), FlatStyle = FlatStyle.Flat, BackColor = ACCENT, ForeColor = INKBTN, Font = new Font("Segoe UI", 10.5f, FontStyle.Bold) };
btnDxf.FlatAppearance.BorderSize = 0; btnDxf.Click += (s, e) => ExportDxf(); bar.Controls.Add(btnDxf);
Button Btn(string t, int x, int w, EventHandler h)
{
var b = new Button { Text = t, Location = new Point(x, 8), Size = new Size(w, 30), FlatStyle = FlatStyle.Flat, BackColor = BTN, ForeColor = FG };
b.FlatAppearance.BorderColor = BTN_BORDER; b.Click += h; bar.Controls.Add(b); return b;
}
Btn("Enregistrer reglage", 184, 150, (s, e) => SavePreset());
Btn("Charger reglage", 342, 140, (s, e) => LoadPreset());
var credit = new Label { Text = "Made with love by weapon pour TolTem", ForeColor = MUTED, Font = new Font("Segoe UI", 8.5f), AutoSize = true, Anchor = AnchorStyles.Top | AnchorStyles.Right, Location = new Point(bar.Width - 240, 14) };
bar.Controls.Add(credit);
return bar;
}
private GrilleConfig BuildConfig()
{
double D(NumericUpDown n) => (double)n.Value;
return new GrilleConfig
{
LargeurFace = D(nLarg), HauteurFace = D(nHaut), Epaisseur = D(nEp),
RayonPli = D(nRayon), KFactor = D(nK),
Pli1Cote = D(nPli1), Pli1Sens = (FoldReturn)cbSens1.SelectedIndex,
Pli2Cote = D(nPli2), Pli2Sens = (FoldReturn)cbSens2.SelectedIndex,
Outil = (ToolType)cbOutil.SelectedIndex,
DimA = D(nA), DimB = D(nB), AnglePoincon = D(nAng), EntraxeX = D(nEx), EntraxeY = D(nEy),
Disposition = (LayoutType)cbDispo.SelectedIndex,
MargeBord = D(nMarge), Centrer = chkCentrer.Checked, CoterDxf = chkCoter.Checked
};
}
private void ApplyConfig(GrilleConfig c)
{
void S(NumericUpDown n, double v) { n.Value = (decimal)Math.Min((double)n.Maximum, Math.Max((double)n.Minimum, v)); }
S(nLarg, c.LargeurFace); S(nHaut, c.HauteurFace); S(nEp, c.Epaisseur);
S(nRayon, c.RayonPli); S(nK, c.KFactor);
S(nPli1, c.Pli1Cote); cbSens1.SelectedIndex = (int)c.Pli1Sens;
S(nPli2, c.Pli2Cote); cbSens2.SelectedIndex = (int)c.Pli2Sens;
cbOutil.SelectedIndex = (int)c.Outil; S(nA, c.DimA); S(nB, c.DimB); S(nAng, c.AnglePoincon);
S(nEx, c.EntraxeX); S(nEy, c.EntraxeY); cbDispo.SelectedIndex = (int)c.Disposition;
S(nMarge, c.MargeBord); chkCentrer.Checked = c.Centrer; chkCoter.Checked = c.CoterDxf;
ToggleFields();
}
private void Recompute()
{
try
{
_result = GrilleModel.Build(BuildConfig());
_g3 = GrilleModel.Build3D(_result.Cfg, _result);
var r = _result;
if (lblDesign != null) lblDesign.Text = $"{r.Designation} vide {r.VidePct:0.0}%";
if (lblStats != null) lblStats.Text =
$"Trous : {r.NbTrous}\n" +
$"% de vide : {r.VidePct.ToString("0.0", CultureInfo.CurrentCulture)} %\n" +
$"Taux zone : {r.TauxZone.ToString("0.0", CultureInfo.CurrentCulture)} %\n" +
$"Flan a plat : {r.DevLargeur.ToString("0.#", CultureInfo.CurrentCulture)} x {r.DevHauteur.ToString("0.#", CultureInfo.CurrentCulture)}\n" +
$"Entraxe : {r.EffEntraxeX.ToString("0.#", CultureInfo.CurrentCulture)} (pas) / {r.EffEntraxeY.ToString("0.#", CultureInfo.CurrentCulture)} rangee\n" +
$"Perte au pli: {r.BendDeduction.ToString("0.00", CultureInfo.CurrentCulture)} mm";
}
catch { _result = null; _g3 = null; if (lblDesign != null) lblDesign.Text = ""; if (lblStats != null) lblStats.Text = ""; }
canvas?.Invalidate();
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (!_drag || !_is3D) return;
_az += (e.X - _lastMouse.X) * 0.01;
_el += (e.Y - _lastMouse.Y) * 0.01;
_el = Math.Max(0.05, Math.Min(Math.PI - 0.05, _el));
_lastMouse = e.Location;
canvas.Invalidate();
}
private static List<PointF> ExpandPoly(Geom.Poly p)
{
var outp = new List<PointF>(); int n = p.Pts.Count;
for (int i = 0; i < n; i++)
{
var a = p.Pts[i]; outp.Add(a);
if (i == n - 1 && !p.Closed) break;
var b = p.Pts[(i + 1) % n];
double bulge = (p.Bulges != null && i < p.Bulges.Length) ? p.Bulges[i] : 0.0;
if (bulge != 0.0)
{
double theta = 4.0 * Math.Atan(bulge);
double chord = Math.Sqrt((b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y));
if (chord > 1e-6)
{
double radius = chord / (2.0 * Math.Sin(theta / 2.0));
double mx = (a.X + b.X) / 2.0, my = (a.Y + b.Y) / 2.0;
double apo = radius * Math.Cos(theta / 2.0);
double nx = -(b.Y - a.Y) / chord, ny = (b.X - a.X) / chord;
double cx = mx + nx * apo, cy = my + ny * apo;
double a0 = Math.Atan2(a.Y - cy, a.X - cx);
for (int s = 1; s < 16; s++)
{
double ang = a0 + theta * s / 16;
outp.Add(new PointF((float)(cx + Math.Abs(radius) * Math.Cos(ang)), (float)(cy + Math.Abs(radius) * Math.Sin(ang))));
}
}
}
}
return outp;
}
private void Canvas_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias;
if (_result == null) { TextRenderer.DrawText(g, "Parametres invalides", Font, canvas.ClientRectangle, Color.IndianRed, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter); return; }
if (_is3D) Paint3D(g); else Paint2D(g);
}
private void Paint2D(Graphics g)
{
var r = _result;
var b = r.Geom.Bounds(false); // ignore cotation world entities (cotes ecran a la place)
if (b.Width <= 0 || b.Height <= 0) return;
float W = canvas.ClientSize.Width, H = canvas.ClientSize.Height;
// marges asymetriques : place pour cotes (gauche/bas) + entraxes (haut/droite)
float mL = 66, mT = 48, mR = 72, mB = 74;
float availW = W - mL - mR, availH = H - mT - mB;
if (availW < 40 || availH < 40) return;
float scale = Math.Min(availW / b.Width, availH / b.Height);
if (scale <= 0 || float.IsInfinity(scale)) return;
float offX = (mL + availW / 2f) - (b.Left + b.Right) / 2f * scale;
float offY = (mT + availH / 2f) + (b.Top + b.Bottom) / 2f * scale;
PointF SX(double x, double y) => new PointF((float)(x * scale + offX), (float)(offY - y * scale));
using var penCut = new Pen(Color.FromArgb(235, 235, 235), 1.6f);
using var penFold = new Pen(Color.FromArgb(120, 200, 120), 1.1f) { DashPattern = new float[] { 6, 4 } };
using var penRep = new Pen(Color.FromArgb(230, 90, 90), 1.4f);
using var penCote = new Pen(Color.FromArgb(90, 200, 210), 1f);
using var fCote = new Font("Segoe UI", 8.25f);
using var brCote = new SolidBrush(Color.FromArgb(150, 220, 230));
using var brWhite = new SolidBrush(Color.FromArgb(235, 235, 235));
using var brGreen = new SolidBrush(Color.FromArgb(120, 200, 120));
using var brRed = new SolidBrush(Color.FromArgb(230, 90, 90));
foreach (var p in r.Geom.Polys)
{
if (p.Layer == GrilleModel.L_COTATION) continue;
var pts = ExpandPoly(p); if (pts.Count < 2) continue;
var sp = new PointF[pts.Count];
for (int i = 0; i < pts.Count; i++) sp[i] = SX(pts[i].X, pts[i].Y);
Pen pen = p.Layer == GrilleModel.L_REPERE ? penRep : penCut;
if (p.Closed) g.DrawPolygon(pen, sp); else g.DrawLines(pen, sp);
}
foreach (var c in r.Geom.Circles)
{
var ctr = SX(c.Cx, c.Cy); float rad = (float)(c.R * scale);
g.DrawEllipse(penCut, ctr.X - rad, ctr.Y - rad, rad * 2, rad * 2);
}
foreach (var l in r.Geom.Lines)
{
if (l.Layer == GrilleModel.L_COTATION) continue;
var a = SX(l.X1, l.Y1); var bb = SX(l.X2, l.Y2);
g.DrawLine(l.Layer == GrilleModel.L_TRACAGE ? penFold : penCut, a, bb);
}
// ---------- COTES en taille ecran (lisibles a tout zoom) ----------
void Tick(float x, float y) { g.DrawLine(penCote, x - 3, y - 3, x + 3, y + 3); }
void DimH(double wx1, double wx2, double wy, float dir, string label)
{
PointF a = SX(wx1, wy), bb = SX(wx2, wy);
float dimY = (dir > 0 ? Math.Max(a.Y, bb.Y) : Math.Min(a.Y, bb.Y)) + dir * COTE_OFF;
g.DrawLine(penCote, a.X, a.Y, a.X, dimY); g.DrawLine(penCote, bb.X, bb.Y, bb.X, dimY);
g.DrawLine(penCote, a.X, dimY, bb.X, dimY); Tick(a.X, dimY); Tick(bb.X, dimY);
var sz = g.MeasureString(label, fCote);
g.DrawString(label, fCote, brCote, (a.X + bb.X) / 2 - sz.Width / 2, dir > 0 ? dimY + 1 : dimY - sz.Height - 1);
}
void DimV(double wx, double wy1, double wy2, float dir, string label)
{
PointF a = SX(wx, wy1), bb = SX(wx, wy2);
float dimX = (dir > 0 ? Math.Max(a.X, bb.X) : Math.Min(a.X, bb.X)) + dir * COTE_OFF;
g.DrawLine(penCote, a.X, a.Y, dimX, a.Y); g.DrawLine(penCote, bb.X, bb.Y, dimX, bb.Y);
g.DrawLine(penCote, dimX, a.Y, dimX, bb.Y); Tick(dimX, a.Y); Tick(dimX, bb.Y);
var sz = g.MeasureString(label, fCote);
g.DrawString(label, fCote, brCote, dir > 0 ? dimX + 2 : dimX - sz.Width - 2, (a.Y + bb.Y) / 2 - sz.Height / 2);
}
double Wf = r.DevLargeur, Hf = r.DevHauteur;
DimH(0, Wf, 0, +1, $"{Wf:0.#}"); // largeur flan (dessous)
DimV(0, 0, Hf, -1, $"{Hf:0.#}"); // hauteur flan (gauche)
bool flanges = r.FaceX0 > 0 || r.FaceY0 > 0 || (r.FaceX0 + r.FaceW) < Wf || (r.FaceY0 + r.FaceH) < Hf;
if (flanges)
{
DimH(r.FaceX0, r.FaceX0 + r.FaceW, Hf, -1, $"face {r.FaceW:0.#}"); // largeur face (dessus)
DimV(Wf, r.FaceY0, r.FaceY0 + r.FaceH, +1, $"face {r.FaceH:0.#}"); // hauteur face (droite)
}
// ---- entraxes places HORS du champ perfore (X au-dessus, Y a droite) ----
if (r.Centers.Count >= 2)
{
float topLineY = SX(0, Hf).Y - (flanges ? 40 : 16);
double yTop = r.Centers.Max(c => c.Y);
var row = r.Centers.Where(c => Math.Abs(c.Y - yTop) < 0.5).OrderBy(c => c.X).ToList();
if (row.Count >= 2)
{
PointF h1 = SX(row[0].X, yTop), h2 = SX(row[1].X, yTop);
g.DrawLine(penCote, h1.X, h1.Y, h1.X, topLineY); g.DrawLine(penCote, h2.X, h2.Y, h2.X, topLineY);
g.DrawLine(penCote, h1.X, topLineY, h2.X, topLineY); Tick(h1.X, topLineY); Tick(h2.X, topLineY);
string lbX = $"pas {r.EffEntraxeX:0.#}";
var sz = g.MeasureString(lbX, fCote);
g.DrawString(lbX, fCote, brCote, (h1.X + h2.X) / 2 - sz.Width / 2, topLineY - sz.Height - 1);
}
var ys = r.Centers.Select(c => c.Y).Distinct().OrderBy(v => v).ToList();
if (ys.Count >= 2)
{
double repX = r.Centers.Max(c => c.X);
float rightLineX = SX(Wf, 0).X + (flanges ? 40 : 16);
PointF k1 = SX(repX, ys[0]), k2 = SX(repX, ys[1]);
g.DrawLine(penCote, k1.X, k1.Y, rightLineX, k1.Y); g.DrawLine(penCote, k2.X, k2.Y, rightLineX, k2.Y);
g.DrawLine(penCote, rightLineX, k1.Y, rightLineX, k2.Y); Tick(rightLineX, k1.Y); Tick(rightLineX, k2.Y);
string lbY = $"{r.EffEntraxeY:0.#}";
g.DrawString(lbY, fCote, brCote, rightLineX + 2, (k1.Y + k2.Y) / 2 - g.MeasureString(lbY, fCote).Height / 2);
}
}
// legende (haut-droite, hors dessin)
using var fs = new Font("Segoe UI", 8.5f);
float lx = W - 148, ly = 12;
g.DrawString("DECOUPE", fs, brWhite, lx, ly);
g.DrawString("TRACAGE (plis)", fs, brGreen, lx, ly + 18);
g.DrawString("REPERE", fs, brRed, lx, ly + 36);
g.DrawString("COTES", fs, brCote, lx, ly + 54);
}
private void Paint3D(Graphics g)
{
var g3 = _g3; if (g3 == null) return;
var r = _result;
// centre monde
double cx = r.FaceX0 + r.FaceW / 2, cy = r.FaceY0 + r.FaceH / 2, cz = -Math.Max(0.1, (r.DevLargeur + r.DevHauteur)) * 0.05;
double ca = Math.Cos(_az), sa = Math.Sin(_az), ce = Math.Cos(_el), se = Math.Sin(_el);
(double rx, double ry) Raw(Vec3 v)
{
double x = v.X - cx, y = v.Y - cy, z = v.Z - cz;
double x1 = x * ca - y * sa, y1 = x * sa + y * ca;
double y2 = y1 * ce - z * se;
return (x1, y2);
}
double minx = double.MaxValue, miny = double.MaxValue, maxx = double.MinValue, maxy = double.MinValue;
void acc(double x, double y) { if (x < minx) minx = x; if (y < miny) miny = y; if (x > maxx) maxx = x; if (y > maxy) maxy = y; }
foreach (var s in g3.Segs) { var a = Raw(s.A); var b = Raw(s.B); acc(a.rx, a.ry); acc(b.rx, b.ry); }
foreach (var h in g3.Holes) { var c = Raw(h.C); acc(c.rx, c.ry); }
if (minx > maxx) return;
float pad = 60, W = canvas.ClientSize.Width, Hh = canvas.ClientSize.Height;
double bw = Math.Max(1e-6, maxx - minx), bh = Math.Max(1e-6, maxy - miny);
float scale = (float)Math.Min((W - 2 * pad) / bw, (Hh - 2 * pad) / bh);
float offX = (float)(W / 2 - (minx + maxx) / 2 * scale);
float offY = (float)(Hh / 2 + (miny + maxy) / 2 * scale);
PointF P(Vec3 v) { var rr = Raw(v); return new PointF((float)(rr.rx * scale + offX), (float)(offY - rr.ry * scale)); }
using var penShell = new Pen(Color.FromArgb(235, 235, 235), 1.4f);
using var penHole = new Pen(Color.FromArgb(120, 200, 210), 1f);
foreach (var s in g3.Segs) g.DrawLine(penShell, P(s.A), P(s.B));
foreach (var h in g3.Holes)
{
if (h.R > 0)
{
float rad = (float)(h.R * scale);
var c = P(h.C);
g.DrawEllipse(penHole, c.X - rad, c.Y - rad, rad * 2, rad * 2);
}
else if (h.Poly != null)
{
var sp = new PointF[h.Poly.Count];
for (int i = 0; i < h.Poly.Count; i++) sp[i] = P(h.Poly[i]);
g.DrawPolygon(penHole, sp);
}
}
using var fs = new Font("Segoe UI", 8.5f);
using var brInfo = new SolidBrush(Color.FromArgb(150, 220, 230));
using var brWarn = new SolidBrush(Color.FromArgb(200, 170, 110));
g.DrawString("Vue 3D — glisser pour pivoter", fs, brInfo, 14, canvas.ClientSize.Height - 26);
if (r.Holes.Count > 2500)
g.DrawString("(trous masques en 3D au-dela de 2500 pour la fluidite)", fs, brWarn, 14, canvas.ClientSize.Height - 44);
}
private void ExportDxf()
{
if (_result == null) { MessageBox.Show("Parametres invalides.", "PerfoSimulateur"); return; }
var cfg = BuildConfig();
using var sfd = new SaveFileDialog { Filter = "DXF (*.dxf)|*.dxf", FileName = $"Grille_{cfg.LargeurFace:0}x{cfg.HauteurFace:0}_{_result.Designation.Replace(" ", "")}.dxf" };
if (sfd.ShowDialog() != DialogResult.OK) return;
try { DxfWriter.Save(sfd.FileName, _result.Geom); MessageBox.Show($"DXF exporte ({_result.NbTrous} trous, {_result.Designation}).\nCalques : DECOUPE / TRACAGE / REPERE / INFOS / COTATION.", "PerfoSimulateur"); }
catch (Exception ex) { MessageBox.Show("Echec export : " + ex.Message, "PerfoSimulateur"); }
}
private void SavePreset()
{
using var sfd = new SaveFileDialog { Filter = "Reglage JSON (*.json)|*.json", FileName = "grille.json" };
if (sfd.ShowDialog() != DialogResult.OK) return;
try { File.WriteAllText(sfd.FileName, JsonSerializer.Serialize(BuildConfig(), new JsonSerializerOptions { WriteIndented = true })); }
catch (Exception ex) { MessageBox.Show("Echec : " + ex.Message, "PerfoSimulateur"); }
}
private void LoadPreset()
{
using var ofd = new OpenFileDialog { Filter = "Reglage JSON (*.json)|*.json" };
if (ofd.ShowDialog() != DialogResult.OK) return;
try { var cfg = JsonSerializer.Deserialize<GrilleConfig>(File.ReadAllText(ofd.FileName)); if (cfg != null) ApplyConfig(cfg); }
catch (Exception ex) { MessageBox.Show("Echec : " + ex.Message, "PerfoSimulateur"); }
}
}
}