src/extprogs.cc

changeset 557
04e140bdeb0b
child 560
39085791128f
equal deleted inserted replaced
556:5f4395ec5db0 557:04e140bdeb0b
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <QProcess>
20 #include <QTemporaryFile>
21 #include <QDialog>
22 #include <QDialogButtonBox>
23 #include <QSpinBox>
24 #include <QCheckBox>
25 #include <QComboBox>
26 #include <QGridLayout>
27 #include "main.h"
28 #include "config.h"
29 #include "misc.h"
30 #include "gui.h"
31 #include "document.h"
32 #include "widgets.h"
33 #include "history.h"
34 #include "ui_ytruder.h"
35 #include "ui_intersector.h"
36 #include "ui_rectifier.h"
37 #include "ui_coverer.h"
38 #include "ui_isecalc.h"
39 #include "ui_edger2.h"
40 #include "dialogs.h"
41
42 enum extprog
43 { Isecalc,
44 Intersector,
45 Coverer,
46 Ytruder,
47 Rectifier,
48 Edger2,
49 };
50
51 // =============================================================================
52 // -----------------------------------------------------------------------------
53 cfg (String, prog_isecalc, "");
54 cfg (String, prog_intersector, "");
55 cfg (String, prog_coverer, "");
56 cfg (String, prog_ytruder, "");
57 cfg (String, prog_rectifier, "");
58 cfg (String, prog_edger2, "");
59
60 StringConfig* const g_extProgPaths[] =
61 { &prog_isecalc,
62 &prog_intersector,
63 &prog_coverer,
64 &prog_ytruder,
65 &prog_rectifier,
66 &prog_edger2,
67 };
68
69 #ifndef _WIN32
70 cfg (Bool, prog_isecalc_wine, false);
71 cfg (Bool, prog_intersector_wine, false);
72 cfg (Bool, prog_coverer_wine, false);
73 cfg (Bool, prog_ytruder_wine, false);
74 cfg (Bool, prog_rectifier_wine, false);
75 cfg (Bool, prog_edger2_wine, false);
76
77 BoolConfig* const g_extProgWine[] =
78 { &prog_isecalc_wine,
79 &prog_intersector_wine,
80 &prog_coverer_wine,
81 &prog_ytruder_wine,
82 &prog_rectifier_wine,
83 &prog_edger2_wine,
84 };
85 #endif // _WIN32
86
87 const char* g_extProgNames[] =
88 { "Isecalc",
89 "Intersector",
90 "Coverer",
91 "Ytruder",
92 "Rectifier",
93 "Edger2"
94 };
95
96 // =============================================================================
97 // -----------------------------------------------------------------------------
98 static bool checkProgPath (const extprog prog)
99 { alias path = g_extProgPaths[prog]->value;
100
101 if (path.length() > 0)
102 return true;
103
104 ExtProgPathPrompt* dlg = new ExtProgPathPrompt (g_extProgNames[prog]);
105
106 if (dlg->exec() && !dlg->getPath().isEmpty())
107 { path = dlg->getPath();
108 return true;
109 }
110
111 return false;
112 }
113
114 // =============================================================================
115 // -----------------------------------------------------------------------------
116 static str processErrorString (QProcess& proc)
117 { switch (proc.error())
118 { case QProcess::FailedToStart:
119 return "Failed to start (check your permissions)";
120
121 case QProcess::Crashed:
122 return "Crashed.";
123
124 case QProcess::WriteError:
125 case QProcess::ReadError:
126 return "I/O error.";
127
128 case QProcess::UnknownError:
129 return "Unknown error";
130
131 case QProcess::Timedout:
132 return fmt ("Timed out (30 seconds)");
133 }
134
135 return "";
136 }
137
138 // =============================================================================
139 // -----------------------------------------------------------------------------
140 static bool mkTempFile (QTemporaryFile& tmp, str& fname)
141 { if (!tmp.open())
142 return false;
143
144 fname = tmp.fileName();
145 tmp.close();
146 return true;
147 }
148
149 // =============================================================================
150 // -----------------------------------------------------------------------------
151 static void writeObjects (const QList<LDObject*>& objects, File& f)
152 { for (LDObject* obj : objects)
153 { if (obj->getType() == LDObject::Subfile)
154 { LDSubfile* ref = static_cast<LDSubfile*> (obj);
155 QList<LDObject*> objs = ref->inlineContents (LDSubfile::DeepInline);
156
157 writeObjects (objs, f);
158
159 for (LDObject* obj : objs)
160 delete obj;
161 }
162 else
163 f.write (obj->raw() + "\r\n");
164 }
165 }
166
167 // =============================================================================
168 // -----------------------------------------------------------------------------
169 static void writeObjects (const QList<LDObject*>& objects, str fname)
170 { // Write the input file
171 File f (fname, File::Write);
172
173 if (!f)
174 { critical (fmt ("Couldn't open temporary file %1 for writing.\n", fname));
175 return;
176 }
177
178 writeObjects (objects, f);
179 f.close();
180 }
181
182 // =============================================================================
183 // -----------------------------------------------------------------------------
184 void writeSelection (str fname)
185 { writeObjects (selection(), fname);
186 }
187
188 // =============================================================================
189 // -----------------------------------------------------------------------------
190 void writeColorGroup (const int colnum, str fname)
191 { QList<LDObject*> objects;
192
193 for (LDObject* obj : getCurrentDocument()->getObjects())
194 { if (obj->isColored() == false || obj->getColor() != colnum)
195 continue;
196
197 objects << obj;
198 }
199
200 writeObjects (objects, fname);
201 }
202
203 // =============================================================================
204 // -----------------------------------------------------------------------------
205 bool runUtilityProcess (extprog prog, str path, str argvstr)
206 { QTemporaryFile input, output;
207 str inputname, outputname;
208 QStringList argv = argvstr.split (" ", QString::SkipEmptyParts);
209
210 #ifndef _WIN32
211 if (*g_extProgWine[prog])
212 { argv.insert (0, path);
213 path = "wine";
214 }
215 #endif // _WIN32
216
217 log ("cmdline: %1 %2\n", path, argv.join (" "));
218
219 // Temporary files for stdin and stdout
220 if (!mkTempFile (input, inputname) || !mkTempFile (output, outputname))
221 return false;
222
223 QProcess proc;
224
225 // Init stdin
226 File stdinfp (inputname, File::Write);
227
228 // Begin!
229 proc.setStandardInputFile (inputname);
230 proc.start (path, argv);
231
232 // Write an enter, the utility tools all expect one
233 stdinfp.write ("\n");
234
235 // Wait while it runs
236 proc.waitForFinished();
237
238 str err = "";
239
240 if (proc.exitStatus() != QProcess::NormalExit)
241 err = processErrorString (proc);
242
243 // Check the return code
244 if (proc.exitCode() != 0)
245 err = fmt ("Program exited abnormally (return code %1).", proc.exitCode());
246
247 if (err.length() > 0)
248 { critical (fmt ("%1 failed: %2\n", g_extProgNames[prog], err));
249 return false;
250 }
251
252 return true;
253 }
254
255 // =============================================================================
256 // -----------------------------------------------------------------------------
257 static void insertOutput (str fname, bool replace, QList<int> colorsToReplace)
258 {
259 #ifndef RELEASE
260 QFile::copy (fname, "./debug_lastOutput");
261 #endif // RELEASE
262
263 // Read the output file
264 File f (fname, File::Read);
265
266 if (!f)
267 { critical (fmt ("Couldn't open temporary file %1 for reading.\n", fname));
268 return;
269 }
270
271 QList<LDObject*> objs = loadFileContents (&f, null);
272
273 // If we replace the objects, delete the selection now.
274 if (replace)
275 g_win->deleteSelection();
276
277 for (const int colnum : colorsToReplace)
278 g_win->deleteByColor (colnum);
279
280 // Insert the new objects
281 getCurrentDocument()->clearSelection();
282
283 for (LDObject * obj : objs)
284 { if (!obj->isScemantic())
285 { delete obj;
286 continue;
287 }
288
289 getCurrentDocument()->addObject (obj);
290 obj->select();
291 }
292
293 g_win->doFullRefresh();
294 }
295
296 // =============================================================================
297 // Interface for Ytruder
298 // -----------------------------------------------------------------------------
299 DEFINE_ACTION (Ytruder, 0)
300 { setlocale (LC_ALL, "C");
301
302 if (!checkProgPath (Ytruder))
303 return;
304
305 QDialog* dlg = new QDialog;
306 Ui::YtruderUI ui;
307 ui.setupUi (dlg);
308
309 if (!dlg->exec())
310 return;
311
312 // Read the user's choices
313 const enum { Distance, Symmetry, Projection, Radial } mode =
314 ui.mode_distance->isChecked() ? Distance :
315 ui.mode_symmetry->isChecked() ? Symmetry :
316 ui.mode_projection->isChecked() ? Projection : Radial;
317
318 const Axis axis =
319 ui.axis_x->isChecked() ? X :
320 ui.axis_y->isChecked() ? Y : Z;
321
322 const double depth = ui.planeDepth->value(),
323 condAngle = ui.condAngle->value();
324
325 QTemporaryFile indat, outdat;
326 str inDATName, outDATName;
327
328 // Make temp files for the input and output files
329 if (!mkTempFile (indat, inDATName) || !mkTempFile (outdat, outDATName))
330 return;
331
332 // Compose the command-line arguments
333 str argv = join (
334 { (axis == X) ? "-x" : (axis == Y) ? "-y" : "-z",
335 (mode == Distance) ? "-d" : (mode == Symmetry) ? "-s" : (mode == Projection) ? "-p" : "-r",
336 depth,
337 "-a",
338 condAngle,
339 inDATName,
340 outDATName
341 });
342
343 writeSelection (inDATName);
344
345 if (!runUtilityProcess (Ytruder, prog_ytruder, argv))
346 return;
347
348 insertOutput (outDATName, false, {});
349 }
350
351 // =============================================================================
352 // Rectifier interface
353 // -----------------------------------------------------------------------------
354 DEFINE_ACTION (Rectifier, 0)
355 { setlocale (LC_ALL, "C");
356
357 if (!checkProgPath (Rectifier))
358 return;
359
360 QDialog* dlg = new QDialog;
361 Ui::RectifierUI ui;
362 ui.setupUi (dlg);
363
364 if (!dlg->exec())
365 return;
366
367 QTemporaryFile indat, outdat;
368 str inDATName, outDATName;
369
370 // Make temp files for the input and output files
371 if (!mkTempFile (indat, inDATName) || !mkTempFile (outdat, outDATName))
372 return;
373
374 // Compose arguments
375 str argv = join (
376 { (!ui.cb_condense->isChecked()) ? "-q" : "",
377 (!ui.cb_subst->isChecked()) ? "-r" : "",
378 (ui.cb_condlineCheck->isChecked()) ? "-a" : "",
379 (ui.cb_colorize->isChecked()) ? "-c" : "",
380 "-t",
381 ui.dsb_coplthres->value(),
382 inDATName,
383 outDATName
384 });
385
386 writeSelection (inDATName);
387
388 if (!runUtilityProcess (Rectifier, prog_rectifier, argv))
389 return;
390
391 insertOutput (outDATName, true, {});
392 }
393
394 // =============================================================================
395 // Intersector interface
396 // -----------------------------------------------------------------------------
397 DEFINE_ACTION (Intersector, 0)
398 { setlocale (LC_ALL, "C");
399
400 if (!checkProgPath (Intersector))
401 return;
402
403 QDialog* dlg = new QDialog;
404 Ui::IntersectorUI ui;
405 ui.setupUi (dlg);
406
407 makeColorComboBox (ui.cmb_incol);
408 makeColorComboBox (ui.cmb_cutcol);
409 ui.cb_repeat->setWhatsThis ("If this is set, " APPNAME " runs Intersector a second time with inverse files to cut the "
410 " cutter group with the input group. Both groups are cut by the intersection.");
411 ui.cb_edges->setWhatsThis ("Makes " APPNAME " try run Isecalc to create edgelines for the intersection.");
412
413 int inCol, cutCol;
414 const bool repeatInverse = ui.cb_repeat->isChecked();
415
416 forever
417 { if (!dlg->exec())
418 return;
419
420 inCol = ui.cmb_incol->itemData (ui.cmb_incol->currentIndex()).toInt();
421 cutCol = ui.cmb_cutcol->itemData (ui.cmb_cutcol->currentIndex()).toInt();
422
423 if (inCol == cutCol)
424 { critical ("Cannot use the same color group for both input and cutter!");
425 continue;
426 }
427
428 break;
429 }
430
431 // Five temporary files!
432 // indat = input group file
433 // cutdat = cutter group file
434 // outdat = primary output
435 // outdat2 = inverse output
436 // edgesdat = edges output (isecalc)
437 QTemporaryFile indat, cutdat, outdat, outdat2, edgesdat;
438 str inDATName, cutDATName, outDATName, outDAT2Name, edgesDATName;
439
440 if (!mkTempFile (indat, inDATName) || !mkTempFile (cutdat, cutDATName) ||
441 !mkTempFile (outdat, outDATName) || !mkTempFile (outdat2, outDAT2Name) ||
442 !mkTempFile (edgesdat, edgesDATName))
443 { return;
444 }
445
446 str parms = join (
447 { (ui.cb_colorize->isChecked()) ? "-c" : "",
448 (ui.cb_nocondense->isChecked()) ? "-t" : "",
449 "-s",
450 ui.dsb_prescale->value()
451 });
452
453 str argv_normal = join (
454 { parms,
455 inDATName,
456 cutDATName,
457 outDATName
458 });
459
460 str argv_inverse = join (
461 { parms,
462 cutDATName,
463 inDATName,
464 outDAT2Name
465 });
466
467 writeColorGroup (inCol, inDATName);
468 writeColorGroup (cutCol, cutDATName);
469
470 if (!runUtilityProcess (Intersector, prog_intersector, argv_normal))
471 return;
472
473 insertOutput (outDATName, false, {inCol});
474
475 if (repeatInverse && runUtilityProcess (Intersector, prog_intersector, argv_inverse))
476 insertOutput (outDAT2Name, false, {cutCol});
477
478 if (
479 ui.cb_edges->isChecked() &&
480 checkProgPath (Isecalc) &&
481 runUtilityProcess (Isecalc, prog_isecalc, join ( {inDATName, cutDATName, edgesDATName}))
482 )
483 insertOutput (edgesDATName, false, {});
484 }
485
486 // =============================================================================
487 // -----------------------------------------------------------------------------
488 DEFINE_ACTION (Coverer, 0)
489 { setlocale (LC_ALL, "C");
490
491 if (!checkProgPath (Coverer))
492 return;
493
494 QDialog* dlg = new QDialog;
495 Ui::CovererUI ui;
496 ui.setupUi (dlg);
497 makeColorComboBox (ui.cmb_col1);
498 makeColorComboBox (ui.cmb_col2);
499
500 int in1Col, in2Col;
501
502 forever
503 { if (!dlg->exec())
504 return;
505
506 in1Col = ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt();
507 in2Col = ui.cmb_col2->itemData (ui.cmb_col2->currentIndex()).toInt();
508
509 if (in1Col == in2Col)
510 { critical ("Cannot use the same color group for both input and cutter!");
511 continue;
512 }
513
514 break;
515 }
516
517 QTemporaryFile in1dat, in2dat, outdat;
518 str in1DATName, in2DATName, outDATName;
519
520 if (!mkTempFile (in1dat, in1DATName) || !mkTempFile (in2dat, in2DATName) || !mkTempFile (outdat, outDATName))
521 return;
522
523 str argv = join (
524 { (ui.cb_oldsweep->isChecked() ? "-s" : ""),
525 (ui.cb_reverse->isChecked() ? "-r" : ""),
526 (ui.dsb_segsplit->value() != 0 ? fmt ("-l %1", ui.dsb_segsplit->value()) : ""),
527 (ui.sb_bias->value() != 0 ? fmt ("-s %1", ui.sb_bias->value()) : ""),
528 in1DATName,
529 in2DATName,
530 outDATName
531 });
532
533 writeColorGroup (in1Col, in1DATName);
534 writeColorGroup (in2Col, in2DATName);
535
536 if (!runUtilityProcess (Coverer, prog_coverer, argv))
537 return;
538
539 insertOutput (outDATName, false, {});
540 }
541
542 // =============================================================================
543 // -----------------------------------------------------------------------------
544 DEFINE_ACTION (Isecalc, 0)
545 { setlocale (LC_ALL, "C");
546
547 if (!checkProgPath (Isecalc))
548 return;
549
550 Ui::IsecalcUI ui;
551 QDialog* dlg = new QDialog;
552 ui.setupUi (dlg);
553
554 makeColorComboBox (ui.cmb_col1);
555 makeColorComboBox (ui.cmb_col2);
556
557 int in1Col, in2Col;
558
559 // Run the dialog and validate input
560 forever
561 { if (!dlg->exec())
562 return;
563
564 in1Col = ui.cmb_col1->itemData (ui.cmb_col1->currentIndex()).toInt(),
565 in2Col = ui.cmb_col1->itemData (ui.cmb_col2->currentIndex()).toInt();
566
567 if (in1Col == in2Col)
568 { critical ("Cannot use the same color group for both input and cutter!");
569 continue;
570 }
571
572 break;
573 }
574
575 QTemporaryFile in1dat, in2dat, outdat;
576 str in1DATName, in2DATName, outDATName;
577
578 if (!mkTempFile (in1dat, in1DATName) || !mkTempFile (in2dat, in2DATName) || !mkTempFile (outdat, outDATName))
579 return;
580
581 str argv = join (
582 { in1DATName,
583 in2DATName,
584 outDATName
585 });
586
587 writeColorGroup (in1Col, in1DATName);
588 writeColorGroup (in2Col, in2DATName);
589 runUtilityProcess (Isecalc, prog_isecalc, argv);
590 insertOutput (outDATName, false, {});
591 }
592
593 // =============================================================================
594 // -----------------------------------------------------------------------------
595 DEFINE_ACTION (Edger2, 0)
596 { setlocale (LC_ALL, "C");
597
598 if (!checkProgPath (Edger2))
599 return;
600
601 QDialog* dlg = new QDialog;
602 Ui::Edger2Dialog ui;
603 ui.setupUi (dlg);
604
605 if (!dlg->exec())
606 return;
607
608 QTemporaryFile in, out;
609 str inName, outName;
610
611 if (!mkTempFile (in, inName) || !mkTempFile (out, outName))
612 return;
613
614 int unmatched = ui.unmatched->currentIndex();
615
616 str argv = join (
617 { fmt ("-p %1", ui.precision->value()),
618 fmt ("-af %1", ui.flatAngle->value()),
619 fmt ("-ac %1", ui.condAngle->value()),
620 fmt ("-ae %1", ui.edgeAngle->value()),
621 ui.delLines->isChecked() ? "-de" : "",
622 ui.delCondLines->isChecked() ? "-dc" : "",
623 ui.colored->isChecked() ? "-c" : "",
624 ui.bfc->isChecked() ? "-b" : "",
625 ui.convex->isChecked() ? "-cx" : "",
626 ui.concave->isChecked() ? "-cv" : "",
627 unmatched == 0 ? "-u+" : (unmatched == 2 ? "-u-" : ""),
628 inName,
629 outName,
630 });
631
632 writeSelection (inName);
633
634 if (!runUtilityProcess (Edger2, prog_edger2, argv))
635 return;
636
637 insertOutput (outName, true, {});
638 }

mercurial