src/ExternalPrograms.cc

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

mercurial