/
step.cpp
2054 lines (1805 loc) · 71.3 KB
/
step.cpp
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2007-2009 Kevin Clague. All rights reserved.
** Copyright (C) 2015 - 2018 Trevor SANDY. All rights reserved.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
/****************************************************************************
*
* This class represents one step including a step number, and assembly
* image, possibly a parts list image and zero or more callouts needed for
* the step.
*
* Please see lpub.h for an overall description of how the files in LPub
* make up the LPub program.
*
***************************************************************************/
#include <QFileInfo>
#include <QDir>
#include <QFile>
#include "lpub.h"
#include "step.h"
#include "range.h"
#include "ranges.h"
#include "ranges_element.h"
#include "render.h"
#include "callout.h"
#include "calloutbackgrounditem.h"
#include "pointer.h"
#include "calloutpointeritem.h"
#include "numberitem.h"
#include "csiitem.h"
#include "resolution.h"
#include "dependencies.h"
#include "paths.h"
#include "ldrawfiles.h"
#include <LDVQt/LDVImageMatte.h>
/*********************************************************************
*
* Create a new step and remember the meta-command state at the time
* that it was created.
*
********************************************************************/
Step::Step(
Where &topOfStep,
AbstractStepsElement *_parent,
int num, // step number as seen by the user
Meta &_meta, // the current state of the meta-commands
bool calledOut, // if we're a callout
bool multiStep)
: calledOut(calledOut)
, multiStep(multiStep)
{
top = topOfStep;
parent = _parent;
submodelLevel = _meta.submodelStack.size();
stepNumber.number = num; // record step number
csiItem = nullptr;
modelDisplayOnlyStep = false;
relativeType = StepType;
csiPlacement.relativeType = CsiType;
stepNumber.relativeType = StepNumberType;
rotateIcon.relativeType = RotateIconType;
subModel.relativeType = SubModelType;
float pW, hH, fH;
int which = _meta.LPub.page.orientation.value() == Landscape ? 1 : 0;
pW = _meta.LPub.page.size.value(which);
hH = _meta.LPub.page.pageHeader.size.valueInches(1);
fH = _meta.LPub.page.pageFooter.size.valueInches(1);
PageHeaderMeta headerMeta = _meta.LPub.page.pageHeader;
PageFooterMeta footerMeta = _meta.LPub.page.pageFooter;
headerMeta.size.setValuesInches(pW,hH);
footerMeta.size.setValuesInches(pW,fH);
plPageHeader.relativeType = PageHeaderType;
plPageHeader.placement = headerMeta.placement;
plPageHeader.size[XX] = headerMeta.size.valuePixels(XX);
plPageHeader.size[YY] = headerMeta.size.valuePixels(YY);
plPageFooter.relativeType = PageFooterType;
plPageFooter.placement = footerMeta.placement;
plPageFooter.size[XX] = footerMeta.size.valuePixels(XX);
plPageFooter.size[YY] = footerMeta.size.valuePixels(YY);
if (calledOut) {
csiPlacement.margin = _meta.LPub.callout.csi.margin;
csiPlacement.placement = _meta.LPub.callout.csi.placement;
pli.margin = _meta.LPub.callout.pli.margin;
pli.placement = _meta.LPub.callout.pli.placement;
rotateIcon.placement = _meta.LPub.callout.rotateIcon.placement;
rotateIcon.margin = _meta.LPub.callout.rotateIcon.margin;
rotateIconMeta = _meta.LPub.callout.rotateIcon;
numberPlacemetMeta = _meta.LPub.callout.stepNum;
stepNumber.placement = _meta.LPub.callout.stepNum.placement;
stepNumber.font = _meta.LPub.callout.stepNum.font.valueFoo();
stepNumber.color = _meta.LPub.callout.stepNum.color.value();
stepNumber.margin = _meta.LPub.callout.stepNum.margin;
subModel.margin = _meta.LPub.callout.subModel.margin;
subModel.placement = _meta.LPub.callout.subModel.placement;
pliPerStep = _meta.LPub.callout.pli.perStep.value();
} else if (multiStep) {
csiPlacement.margin = _meta.LPub.multiStep.csi.margin;
csiPlacement.placement = _meta.LPub.multiStep.csi.placement;
pli.margin = _meta.LPub.multiStep.pli.margin;
pli.placement = _meta.LPub.multiStep.pli.placement;
rotateIcon.placement = _meta.LPub.multiStep.rotateIcon.placement;
rotateIcon.margin = _meta.LPub.multiStep.rotateIcon.margin;
rotateIconMeta = _meta.LPub.multiStep.rotateIcon;
numberPlacemetMeta = _meta.LPub.multiStep.stepNum;
stepNumber.placement = _meta.LPub.multiStep.stepNum.placement;
stepNumber.font = _meta.LPub.multiStep.stepNum.font.valueFoo();
stepNumber.color = _meta.LPub.multiStep.stepNum.color.value();
stepNumber.margin = _meta.LPub.multiStep.stepNum.margin;
subModel.margin = _meta.LPub.multiStep.subModel.margin;
subModel.placement = _meta.LPub.multiStep.subModel.placement;
pliPerStep = _meta.LPub.multiStep.pli.perStep.value();
} else {
csiPlacement.margin = _meta.LPub.assem.margin;
csiPlacement.placement = _meta.LPub.assem.placement;
placement = _meta.LPub.assem.placement;
pli.margin = _meta.LPub.assem.margin;
pli.placement = _meta.LPub.pli.placement;
rotateIcon.placement = _meta.LPub.rotateIcon.placement;
rotateIcon.margin = _meta.LPub.rotateIcon.margin;
stepNumber.placement = _meta.LPub.stepNumber.placement;
stepNumber.font = _meta.LPub.stepNumber.font.valueFoo();
stepNumber.color = _meta.LPub.stepNumber.color.value();
stepNumber.margin = _meta.LPub.stepNumber.margin;
subModel.margin = _meta.LPub.subModel.margin;
subModel.placement = _meta.LPub.subModel.placement;
pliPerStep = false;
}
pli.steps = grandparent();
pli.step = this;
subModel.steps = grandparent();
subModel.step = this;
showStepNumber = _meta.LPub.assem.showStepNumber.value();
rotateIcon.setSize( _meta.LPub.rotateIcon.size,
_meta.LPub.rotateIcon.border.valuePixels().thickness);
placeSubModel = false;
placeRotateIcon = false;
}
/* step destructor destroys all callouts */
Step::~Step() {
for (int i = 0; i < list.size(); i++) {
Callout *callout = list[i];
delete callout;
}
list.clear();
pli.clear();
subModel.clear();
}
Step *Step::nextStep()
{
const AbstractRangeElement *re = dynamic_cast<const AbstractRangeElement *>(this);
return dynamic_cast<Step *>(nextElement(re));
}
Range *Step::range()
{
Range *range = dynamic_cast<Range *>(parent);
return range;
}
/*
* given a set of parts, generate a CSI
*/
int Step::createCsi(
QString const &addLine,
QStringList const &csiParts, // the partially assembles model
QPixmap *pixmap,
Meta &meta,
bool bfxLoad) // Bfx load special case (no parts added)
{
bool csiExist = false;
bool nativeRenderer = (Preferences::preferredRenderer == RENDERER_NATIVE ||
(Preferences::preferredRenderer == RENDERER_POVRAY &&
Preferences::povFileGenerator == RENDERER_NATIVE));
QString csi_Name = modelDisplayOnlyStep ? csiName()+"_fm" : bfxLoad ? csiName()+"_bfx" : csiName();
qreal modelScale = meta.LPub.assem.modelScale.value();
bool doFadeStep = meta.LPub.fadeStep.fadeStep.value();
bool doHighlightStep = meta.LPub.highlightStep.highlightStep.value() && !gui->suppressColourMeta();
bool invalidIMStep = ((modelDisplayOnlyStep) || (stepNumber.number == 1));
bool absRotstep = meta.rotStep.value().type == "ABS";
FloatPairMeta absCA;
if (absRotstep)
absCA.setValues(0.0f,0.0f);
ldrName.clear();
// 1 color x y z a b c d e f g h i foo.dat
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
QStringList tokens;
split(addLine,tokens);
QString orient;
if (tokens.size() == 15) {
for (int i = 5; i < 14; i++) {
orient += "_" + tokens[i];
}
}
// Define csi file paths
QString csiLdrFilePath = QString("%1/%2").arg(QDir::currentPath()).arg(Paths::tmpDir);
QString csiPngFilePath = QString("%1/%2").arg(QDir::currentPath()).arg(Paths::assemDir);
QString csiLdrFile = QString("%1/csi.ldr").arg(csiLdrFilePath);
QString key = QString("%1_%2_%3_%4_%5_%6_%7_%8_%9")
.arg(csi_Name+orient)
.arg(stepNumber.number)
.arg(gui->pageSize(meta.LPub.page, 0))
.arg(resolution())
.arg(resolutionType() == DPI ? "DPI" : "DPCM")
.arg(modelScale)
.arg(meta.LPub.assem.cameraFoV.value())
.arg(absRotstep ? absCA.value(0) : meta.LPub.assem.cameraAngles.value(0))
.arg(absRotstep ? absCA.value(1) : meta.LPub.assem.cameraAngles.value(1));
// populate png name
pngName = QString("%1/%2.png").arg(csiPngFilePath).arg(key);
// create ImageMatte csiKey
csiKey = QString("%1_%2").arg(csi_Name).arg(stepNumber.number);
// add csiKey and pngName to ImageMatte repository - exclude first step
if (Preferences::enableFadeSteps && Preferences::enableImageMatting && !invalidIMStep) {
if (!LDVImageMatte::validMatteCSIImage(csiKey))
LDVImageMatte::insertMatteCSIImage(csiKey, pngName);
}
// Check if png file date modified is older than model file (on the stack) date modified
csiOutOfDate = false;
QFile csi(pngName);
csiExist = csi.exists();
if (csiExist) {
QDateTime lastModified = QFileInfo(pngName).lastModified();
QStringList parsedStack = submodelStack();
parsedStack << parent->modelName();
if ( ! isOlder(parsedStack,lastModified)) {
csiOutOfDate = true;
if (! csi.remove()) {
emit gui->messageSig(LOG_ERROR,QString("Failed to remove out of date CSI PNG file."));
}
}
}
int rc;
// Populate viewerCsiKey variable
viewerCsiKey = QString("\"%1\"%2;%3%4")
.arg(top.modelName)
.arg(top.lineNumber)
.arg(stepNumber.number)
.arg(modelDisplayOnlyStep ? "_fm" : "");
// Viewer Csi does not yet exist in repository
bool addViewerStepContent = !gui->viewerStepContentExist(viewerCsiKey);
// We are processing again the current step so Csi must have been updated in the viewer
bool viewerUpdate = viewerCsiKey == gui->getViewerCsiKey();
// Generate 3DViewer CSI entry
if ( addViewerStepContent || csiOutOfDate || viewerUpdate ) {
// set rotated parts
QStringList rotatedParts = csiParts;
// rotate parts for 3DViewer display - do not apply camera angles
if ((rc = renderer->rotateParts(addLine,meta.rotStep,rotatedParts,absRotstep ? absCA : meta.LPub.assem.cameraAngles,false)) != 0)
emit gui->messageSig(LOG_ERROR,QString("Failed to rotate viewer CSI parts"));
// add ROTSTEP command
rotatedParts.prepend(renderer->getRotstepMeta(meta.rotStep));
// header and closing meta
QString modelName = QFileInfo(top.modelName).baseName().toLower();
modelName = QString("%1%2").arg(modelName.replace(modelName.at(0),modelName.at(0).toUpper()))
.arg(modelDisplayOnlyStep ? " - Final Model" : "");
rotatedParts.prepend(QString("0 !LEOCAD MODEL NAME %1").arg(modelName));
rotatedParts.prepend(QString("0 Name: %1").arg(top.modelName));
rotatedParts.prepend(QString("0 %1").arg(modelName));
rotatedParts.append("0 NOFILE");
// consolidate subfiles and parts into single file - I don't think this is still needed but I keep it anyway
createViewerCSI(rotatedParts, doFadeStep, doHighlightStep);
gui->insertViewerStep(viewerCsiKey,rotatedParts,csiLdrFile,multiStep,calledOut);
}
// Generate renderer CSI file
if ( ! csiExist || csiOutOfDate ) {
QElapsedTimer timer;
timer.start();
// populate ldr file name
ldrName = QString("%1/%2.ldr").arg(csiLdrFilePath).arg(key);
// create the CSI ldr file and rotate its parts for single call LDView and Native renderers
if (renderer->useLDViewSCall() || nativeRenderer) {
if (nativeRenderer)
ldrName = csiLdrFile;
if ((rc = renderer->rotateParts(addLine, meta.rotStep, csiParts, ldrName, top.modelName, absRotstep ? absCA : meta.LPub.assem.cameraAngles)) != 0) {
emit gui->messageSig(LOG_ERROR,QString("Failed to create and rotate CSI ldr file: %1.")
.arg(ldrName));
return rc;
}
}
if (!renderer->useLDViewSCall()) {
// render the partially assembled model
QStringList csiKeys;
if (nativeRenderer)
csiKeys = (QStringList() << viewerCsiKey);
else
csiKeys = (QStringList() << csiKey); // adding just a single key
if ((rc = renderer->renderCsi(addLine, csiParts, csiKeys, pngName, meta)) != 0) {
emit gui->messageSig(LOG_ERROR,QString("Render CSI part failed for %1.")
.arg(pngName));
return rc;
}
emit gui->messageSig(LOG_INFO,
QString("%1 CSI render call took %2 milliseconds "
"to render %3 for %4 %5 %6 on page %7.")
.arg(Render::getRenderer())
.arg(timer.elapsed())
.arg(pngName)
.arg(calledOut ? "called out," : "simple,")
.arg(multiStep ? "step group" : "single step")
.arg(stepNumber.number)
.arg(gui->stepPageNum));
}
}
// set viewer camera options
viewerOptions.ViewerCsiKey = viewerCsiKey;
viewerOptions.FoV = meta.LPub.assem.cameraFoV.value();
viewerOptions.Latitude = absRotstep ? absCA.value(0) : meta.LPub.assem.cameraAngles.value(0);
viewerOptions.Longitude = absRotstep ? absCA.value(1) : meta.LPub.assem.cameraAngles.value(1);
// Load the 3DViewer
loadTheViewer();
// If not using LDView SCall, populate pixmap
if (! renderer->useLDViewSCall()) {
pixmap->load(pngName);
csiPlacement.size[0] = pixmap->width();
csiPlacement.size[1] = pixmap->height();
}
return 0;
}
// create 3D Viewer version of the csi file
int Step::createViewerCSI(
QStringList &csiRotatedParts,
bool doFadeStep,
bool doHighlightStep)
{
QStringList csiSubModels;
QStringList csiSubModelParts;
QStringList csiParts = csiRotatedParts;
QStringList argv;
bool alreadyInserted;
int rc;
if (csiRotatedParts.size() > 0) {
for (int index = 0; index < csiRotatedParts.size(); index++) {
QString csiLine = csiRotatedParts[index];
split(csiLine, argv);
if (argv.size() == 15 && argv[0] == "1") {
/* process subfiles in csiRotatedParts */
QString type = argv[argv.size()-1];
bool isCustomSubModel = false;
bool isCustomPart = false;
QString customType;
// Custom part types
if (doFadeStep) {
bool isFadedItem = type.contains("-fade.");
// Fade file
if (isFadedItem) {
customType = type;
customType = customType.replace("-fade.",".");
isCustomSubModel = gui->isSubmodel(customType);
isCustomPart = gui->isUnofficialPart(customType);
}
}
else
if (doHighlightStep) {
bool isHighlightItem = type.contains("-highlight");
// Highlight file
if (isHighlightItem) {
customType = type;
customType = customType.replace("-highlight.",".");
isCustomSubModel = gui->isSubmodel(customType);
isCustomPart = gui->isUnofficialPart(customType);
}
}
if (gui->isSubmodel(type) || gui->isUnofficialPart(type) || isCustomSubModel || isCustomPart) {
/* capture subfiles (full string) to be processed when finished */
if (!csiSubModels.contains(type.toLower()))
csiSubModels << type.toLower();
}
}
} //end for
/* process extracted submodels and unofficial files */
if (csiSubModels.size() > 0){
if (csiSubModels.size() > 2)
csiSubModels.removeDuplicates();
if ((rc = mergeViewerCSISubModels(csiSubModels, csiSubModelParts, doFadeStep, doHighlightStep)) != 0){
emit gui->messageSig(LOG_ERROR,QString("Failed to process viewer CSI submodels"));
return rc;
}
}
/* add sub model content to csiRotatedParts file */
if (! csiSubModelParts.empty())
{
for (int i = 0; i < csiSubModelParts.size(); i++) {
QString smLine = csiSubModelParts[i];
csiParts << smLine;
}
}
csiRotatedParts = csiParts;
}
return 0;
}
int Step::mergeViewerCSISubModels(QStringList &subModels,
QStringList &subModelParts,
bool doFadeStep,
bool doHighlightStep)
{
QStringList csiSubModels = subModels;
QStringList csiSubModelParts = subModelParts;
QStringList newSubModels;
QStringList argv;
int rc;
if (csiSubModels.size() > 0) {
/* read in all detected sub model file content */
for (int index = 0; index < csiSubModels.size(); index++) {
QString ldrName(QDir::currentPath() + "/" +
Paths::tmpDir + "/" +
csiSubModels[index]);
/* initialize the working submodel file - define header. */
QString modelName = QFileInfo(csiSubModels[index]).baseName().toLower();
modelName = modelName.replace(modelName.at(0),modelName.at(0).toUpper());
csiSubModelParts << QString("0 FILE %1").arg(csiSubModels[index]);
csiSubModelParts << QString("0 %1").arg(modelName);
csiSubModelParts << QString("0 Name: %1").arg(csiSubModels[index]);
csiSubModelParts << QString("0 !LEOCAD MODEL NAME %1").arg(modelName);
/* read the actual submodel file */
QFile ldrfile(ldrName);
if ( ! ldrfile.open(QFile::ReadOnly | QFile::Text)) {
emit gui->messageSig(LOG_ERROR,QString("Could not read CSI submodel file %1: %2")
.arg(ldrName)
.arg(ldrfile.errorString()));
return -1;
}
/* populate file contents into working submodel csi parts */
QTextStream in(&ldrfile);
while ( ! in.atEnd()) {
QString csiLine = in.readLine(0);
split(csiLine, argv);
if (argv.size() == 15 && argv[0] == "1") {
/* check and process any subfiles in csiRotatedParts */
QString type = argv[argv.size()-1];
bool isCustomSubModel = false;
bool isCustomPart = false;
QString customType;
// Custom part types
if (doFadeStep) {
bool isFadedItem = type.contains("-fade.");
// Fade file
if (isFadedItem) {
customType = type;
customType = customType.replace("-fade.",".");
isCustomSubModel = gui->isSubmodel(customType);
isCustomPart = gui->isUnofficialPart(customType);
}
}
else
if (doHighlightStep) {
bool isHighlightItem = type.contains("-highlight");
// Highlight file
if (isHighlightItem) {
customType = type;
customType = customType.replace("-highlight.",".");
isCustomSubModel = gui->isSubmodel(customType);
isCustomPart = gui->isUnofficialPart(customType);
}
}
if (gui->isSubmodel(type) || gui->isUnofficialPart(type) || isCustomSubModel || isCustomPart) {
/* capture all subfiles (full string) to be processed when finished */
if (!newSubModels.contains(type.toLower()))
newSubModels << type.toLower();
}
}
csiLine = argv.join(" ");
csiSubModelParts << csiLine;
}
csiSubModelParts << "0 NOFILE";
}
/* recurse and process any identified submodel files */
if (newSubModels.size() > 0){
if (newSubModels.size() > 2)
newSubModels.removeDuplicates();
if ((rc = mergeViewerCSISubModels(newSubModels, csiSubModelParts, doFadeStep, doHighlightStep)) != 0){
emit gui->messageSig(LOG_ERROR,QString("Failed to recurse viewer CSI submodels"));
return rc;
}
}
subModelParts = csiSubModelParts;
}
return 0;
}
bool Step::loadTheViewer(){
// Load the 3DViewer
if (! gui->exporting() /* && !Preferences::preferredRenderer == RENDERER_NATIVE */) {
if (! renderer->LoadViewer(viewerOptions)) {
emit gui->messageSig(LOG_ERROR,QString("Could not load 3D Viewer with CSI key: %1")
.arg(viewerCsiKey));
return false;
}
}
return true;
}
/*
* LPub is able to pack steps together into multi-step pages or callouts.
*
* Multiple steps gathered on a page and callouts share a lot of
* commonality. They are organized into rows or columns of steps.
*
* From this springs two algorithms, the first algorithm is based on
* similarity between steps, in that across steps sub-components
* within steps are placed in sub-columns or sub-rows. This format
* is common these days in LEGO building instructions. For lack of
* a better name, I refer to this modern algorithm as tabular.
*
* The other algorithm, which is new to LPub 3, is that of a more
* free format.
*
* These concepts and algorithms are described below.
* 1. tabular format
* a) either Vertically broken down into sub-columns for
* csi, pli, stepNumber, rotateIcon and/or callouts.
* b) or Horizontally broken down into sub-rows for
* csi, pli, stepNumber, rotateIcon and/or callouts.
*
* 2. free form format
* a) either Vertically composed into columns of steps
* b) or rows of steps
*
* This format does not force PLI's or step numbers
* to be organized across steps, but it does force steps themselves
* to be organized into columns or rows.
*
* The default is tabular format because that is the first algorithm
* implemented. This is also the most common algorithm used by LEGO
* today (2007 AD).
*
* The free format is similar to the algorithms used early by LEGO
* and provides the maximum area compression of building instructions,
* even if they are possibly harder to follow.
*/
/*
* the algorithms below implement tabular placement.
*
* size - allocate step sub-components into sub-rows or sub-columns.
* place - determine the rectangle that is needed to totally contain
* the subcomponents (CSI, step number, PLI, rotateIcon, step-relative callouts.)
* Also place the CSI, step number, PLI, rotateIcon and step-relative callouts
* within the step's rectangle.
*
* making all this look nice takes a few passes:
* 1. determine the height and width of each step's sub-columns and
* sub-rows.
* 2. If we're creating a Vertically allocated multi-step or callout
* then make all the sub-columns line up.
*
* If we're creating a Horizontally allocated multi-step or callout
* them make all the sub-rows line up.
*
* from here we've sized each of the steps.
*
* From here, we sum up the the height of each column or row, depending on
* whether we're creating a Vertical or Horizontal multi-step/callout. We
* also keep track of the tallest (widest) column/row within the sets of rows,
* and how wide (tall) the multi-step/callout is.
*
* Now we know the enclosing rectangle for the entire multi-step or callout.
* Given this we can place the multi-step or callout conglomeration relative
* to the thing they are to be placed next to.
*
* Multi-steps can only be placed relative to the page.
*
* Callouts can be place relative to CSI, PLI, step-number, rotateIcon, multi-step, or
* page.
*/
/*
* Size the set of ranges by sizing each range
* and then placing them relative to each other
*/
/*
* Think of the possible placement as a two dimensional table, of
* places where something can be placed within a rectangle.
* -- see step.cpp for detail walkthrough --
*
* CCCCCCCCCCCCCCCCCCC
* CMMMMMMMMMMMMMMMMMC
* CMCCCCCCCCCCCCCCCMC
* CMCRRRRRRRRRRRRRCMC
* CMCRCCCCCCCCCCCRCMC
* CMCRCSSSSSSSSSCRCMC
* CMCRCSCCCCCCCSCRCMC
* CMCRCSCPPPPPCSCRCMC
* CMCRCSCPCCCPCSCRCMC
* CMCRCSCPCACPCSCRCMC
* CMCRCSCPCCCPCSCRCMC
* CMCRCSCPPPPPCSCRCMC
* CMCRCSCCCCCCCSCRCMC
* CMCRCSSSSSSSSSCRCMC
* CMCRCCCCCCCCCCCRCMC
* CMCRRRRRRRRRRRRRCMC
* CMCCCCCCCCCCCCCCCMC
* CMMMMMMMMMMMMMMMMMC
* CCCCCCCCCCCCCCCCCCC
*
* The table above represents either the Horizontal slice
* going through the CSI (represented by A for assembly),
* or the Vertical slice going through the CSI.
*
* C0 - Callout relative to Submodel
* M0 - Submodel relative to CSI
* C1 - Callout relative to RotateIcon
* R0 - RotateIcon relative to CSI
* C2 - Callout relative to StepNumber
* S0 - StepNumber relative to CSI
* C3 - Callout relative to PLI
* P0 - Pli relative to CSI
* C4 - Callout relative to CSI
* A - CSI
* C5 - Callout relative to CSI
* P1 - Pli relative to CSI
* C6 - Callout relative to PLI
* S1 - StepNumber relative to CSI
* C7 - Callout relative to StepNumber
* R1 - RotateIcon relative to CSI
* C8 - Callout relative to RotateIcon
* M1 - Submodel relative to CSI
* C9 - Callout relative to Submodel
*/
/*
* this tells us where to place the stepNumber when placing
* relative to csi
*/
const int stepNumberPlace[NumPlacements][2] =
{
{ TblSn0, TblSn0 }, // Top_Left
{ TblCsi, TblSn0 }, // Top
{ TblSn1, TblSn0 }, // Top_Right
{ TblSn1, TblCsi }, // Right
{ TblSn1, TblSn1 }, // BOTTOM_RIGHT
{ TblCsi, TblSn1 }, // BOTTOM
{ TblSn0, TblSn1 }, // BOTTOM_LEFT
{ TblSn0, TblCsi }, // LEFT
{ TblCsi, TblCsi },
};
/*
* this tells us where to place the pli when placing
* relative to csi
*/
const int pliPlace[NumPlacements][2] =
{
{ TblPli0, TblPli0 }, // Top_Left
{ TblCsi, TblPli0 }, // Top
{ TblPli1, TblPli0 }, // Top_Right
{ TblPli1, TblCsi }, // Right
{ TblPli1, TblPli1 }, // BOTTOM_RIGHT
{ TblCsi, TblPli1 }, // BOTTOM
{ TblPli0, TblPli1 }, // BOTTOM_LEFT
{ TblPli0, TblCsi }, // LEFT
{ TblCsi, TblCsi },
};
/*
* this tells us where to place the Submodel when placing
* relative to csi
*/
const int subModelPlace[NumPlacements][2] =
{
{ TblSm0, TblSm0 }, // Top_Left
{ TblCsi, TblSm0 }, // Top
{ TblSm1, TblSm0 }, // Top_Right
{ TblSm1, TblCsi }, // Right
{ TblSm1, TblSm1 }, // BOTTOM_RIGHT
{ TblCsi, TblSm1 }, // BOTTOM
{ TblSm0, TblSm1 }, // BOTTOM_LEFT
{ TblSm0, TblCsi }, // LEFT
{ TblCsi, TblCsi },
};
/*
* this tells us where to place a rotateIcon when placing
* relative to csi
*/
const int rotateIconPlace[NumPlacements][2] =
{
{ TblRi0, TblRi0 }, // Top_Left
{ TblCsi, TblRi0 }, // Top
{ TblRi1, TblRi0 }, // Top_Right
{ TblRi1, TblCsi }, // Right
{ TblRi1, TblRi1 }, // BOTTOM_RIGHT
{ TblCsi, TblRi1 }, // BOTTOM
{ TblRi0, TblRi1 }, // BOTTOM_LEFT
{ TblRi0, TblCsi }, // LEFT
{ TblCsi, TblCsi },
};
/*
* this tells us where to place a callout when placing
* relative to csi
*/
const int coPlace[NumPlacements][2] =
{
{ TblCo4, TblCo4 }, // Top_Left
{ TblCsi, TblCo4 }, // Top
{ TblCo5, TblCo4 }, // Top_Right
{ TblCo5, TblCsi }, // Right
{ TblCo5, TblCo5 }, // BOTTOM_RIGHT
{ TblCsi, TblCo5 }, // BOTTOM
{ TblCo4, TblCo5 }, // BOTTOM_LEFT
{ TblCo4, TblCsi }, // LEFT
{ TblCsi, TblCsi },
};
/*
* this tells us the row/col offset when placing
* relative to something other than csi
*/
const int relativePlace[NumPlacements][2] =
{
{ -1, -1 },
{ 0, -1 },
{ 1, -1 },
{ 1, 0 },
{ 1, 1 },
{ 0, 1 },
{ -1, 1 },
{ -1, 0 },
{ 0, 0 },
};
void Step::maxMargin(
MarginsMeta &margin,
int tbl[2],
int marginRows[][2],
int marginCols[][2])
{
if (margin.valuePixels(XX) > marginCols[tbl[XX]][0]) {
marginCols[tbl[XX]][0] = margin.valuePixels(XX);
}
if (margin.valuePixels(XX) > marginCols[tbl[XX]][1]) {
marginCols[tbl[XX]][1] = margin.valuePixels(XX);
}
if (margin.valuePixels(YY) > marginRows[tbl[YY]][0]) {
marginRows[tbl[YY]][0] = margin.valuePixels(YY);
}
if (margin.valuePixels(YY) > marginRows[tbl[YY]][1]) {
marginRows[tbl[YY]][1] = margin.valuePixels(YY);
}
}
/*
* This is the first pass of sizing a step.
*
* locate the proper row/col in the placement table (see above)
* for each component (csi, pli, subModel, stepNumber, rotateIcon, callout) in the step
*
* locate the proper row/col for those relative to CSI (absolute)
*
* locate the proper row/col for those relative to (pli, stepNumber)
*
* determine the largest dimensions for each row/col in the table
*
* record the height of this step
*
* determine the pixel offset for each row/col in the table
*
* place the components Vertically in pixel units using row
*/
int Step::sizeit(
int rows[], // accumulate sub-row heights here
int cols[], // accumulate sub-col widths here
int marginRows[][2],// accumulate sub-row margin heights here
int marginCols[][2],
int x,
int y)// accumulate sub-col margin widths here
{
// size up each callout
int numCallouts = list.size();
for (int i = 0; i < numCallouts; i++) {
list[i]->sizeIt();
}
// size up the step number
if (showStepNumber && ! onlyChild()) {
stepNumber.sizeit();
}
// size up the rotate icon
if (placeRotateIcon){
rotateIcon.sizeit();
}
/****************************************************/
/* figure out who is placed in which row and column */
/****************************************************/
csiPlacement.tbl[XX] = TblCsi;
csiPlacement.tbl[YY] = TblCsi;
/* Lets start with the absolutes (those relative to the CSI) */
// PLI relative to CSI
PlacementData pliPlacement = pli.placement.value();
if (pliPlacement.relativeTo == CsiType) {
if (pliPlacement.preposition == Outside) {
pli.tbl[XX] = pliPlace[pliPlacement.placement][XX];
pli.tbl[YY] = pliPlace[pliPlacement.placement][YY];
} else {
pli.tbl[XX] = TblCsi;
pli.tbl[YY] = TblCsi;
}
}
// Submodel relative to CSI
PlacementData subModelPlacement = subModel.placement.value();
if (placeSubModel){
if (subModelPlacement.relativeTo == CsiType) {
if (subModelPlacement.preposition == Outside) {
subModel.tbl[XX] = subModelPlace[subModelPlacement.placement][XX];
subModel.tbl[YY] = subModelPlace[subModelPlacement.placement][YY];
} else {
subModel.tbl[XX] = TblCsi;
subModel.tbl[YY] = TblCsi;
}
}
}
// Rotate Icon relative to CSI
PlacementData rotateIconPlacement = rotateIcon.placement.value();
if (placeRotateIcon){
if (rotateIconPlacement.relativeTo == CsiType){
if (rotateIconPlacement.preposition == Outside) {
rotateIcon.tbl[XX] = rotateIconPlace[rotateIconPlacement.placement][XX];
rotateIcon.tbl[YY] = rotateIconPlace[rotateIconPlacement.placement][YY];
} else {
rotateIcon.tbl[XX] = TblCsi;
rotateIcon.tbl[YY] = TblCsi;
}
}
}
PlacementData stepNumberPlacement = stepNumber.placement.value();
// if Step Number relative to PLI, but no PLI,
// Step Number is relative to CSI (Assem)
if (stepNumberPlacement.relativeTo == PartsListType && ! pliPerStep) {
stepNumberPlacement.relativeTo = CsiType;
}
if (stepNumberPlacement.relativeTo == CsiType) {
if (stepNumberPlacement.preposition == Outside) {
stepNumber.tbl[XX] = stepNumberPlace[stepNumberPlacement.placement][XX];
stepNumber.tbl[YY] = stepNumberPlace[stepNumberPlacement.placement][YY];
} else {
stepNumber.tbl[XX] = TblCsi;
stepNumber.tbl[YY] = TblCsi;
}
}
/* Now lets place things relative to others row/columns */
/* first the known entities (CSI, PLI, SM, SN, RI)*/
if (pliPlacement.relativeTo == StepNumberType) {
if (pliPerStep && pli.tsize()) {
pli.tbl[XX] = stepNumber.tbl[XX]+relativePlace[pliPlacement.placement][XX];
pli.tbl[YY] = stepNumber.tbl[YY]+relativePlace[pliPlacement.placement][YY];
} else {
stepNumber.tbl[XX] = stepNumberPlace[stepNumberPlacement.placement][XX];
stepNumber.tbl[YY] = stepNumberPlace[stepNumberPlacement.placement][YY];
}
}
if (pliPlacement.relativeTo == SubModelType) {
if (pliPerStep && pli.tsize()) {
pli.tbl[XX] = subModel.tbl[XX]+relativePlace[pliPlacement.placement][XX];
pli.tbl[YY] = subModel.tbl[YY]+relativePlace[pliPlacement.placement][YY];
} else {
subModel.tbl[XX] = subModelPlace[subModelPlacement.placement][XX];
subModel.tbl[YY] = subModelPlace[subModelPlacement.placement][YY];
}
}
if (pliPlacement.relativeTo == RotateIconType) {
if (pliPerStep && pli.tsize()) {
pli.tbl[XX] = rotateIcon.tbl[XX]+relativePlace[pliPlacement.placement][XX];
pli.tbl[YY] = rotateIcon.tbl[YY]+relativePlace[pliPlacement.placement][YY];
} else {
rotateIcon.tbl[XX] = rotateIconPlace[rotateIconPlacement.placement][XX];
rotateIcon.tbl[YY] = rotateIconPlace[rotateIconPlacement.placement][YY];
}
}
if (subModelPlacement.relativeTo == PartsListType) {
if (placeSubModel) {
subModel.tbl[XX] = pli.tbl[XX]+relativePlace[subModelPlacement.placement][XX];
subModel.tbl[YY] = pli.tbl[YY]+relativePlace[subModelPlacement.placement][YY];
} else {
pli.tbl[XX] = pliPlace[pliPlacement.placement][XX];
pli.tbl[YY] = pliPlace[pliPlacement.placement][YY];
}
}
if (subModelPlacement.relativeTo == RotateIconType) {
if (placeSubModel) {
subModel.tbl[XX] = rotateIcon.tbl[XX]+relativePlace[subModelPlacement.placement][XX];
subModel.tbl[YY] = rotateIcon.tbl[YY]+relativePlace[subModelPlacement.placement][YY];
} else {
rotateIcon.tbl[XX] = rotateIconPlace[rotateIconPlacement.placement][XX];
rotateIcon.tbl[YY] = rotateIconPlace[rotateIconPlacement.placement][YY];