-
Notifications
You must be signed in to change notification settings - Fork 9
/
SW2GDMLconverter.cpp
4600 lines (4215 loc) · 172 KB
/
SW2GDMLconverter.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
// SW2GDMLconverter.cpp : Console application that converts a SolidWorks design into GDML for Geant4
//
// Outstanding issues:
// Support for multiple holes in a part is missing
//
// Rotation of parts with angular size (like disks) may be off when the
// part is composed of two faces with different sizes because output
// uses the largest sizes/angle but the z rotation may be based on the
// smaller face
#include "stdafx.h"
//Import the SolidWorks type library
#import "sldworks.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
//Import the SolidWorks constant type library
#import "swconst.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids
#import "swcommands.tlb"
#include "atlbase.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
enum surfTypeIDs {
PLANE_ID = 4001, CYLINDER_ID, CONE_ID, SPHERE_ID, TORUS_ID, BSURF_ID, BLEND_ID,
OFFSET_ID, EXTRUSION_ID, S_REVOLVE_ID, VOLUME_ID, POSITION_ID, ROTATION_ID, SUBTRACTION_ID, DISK_ID, BOARD_ID
};
const char *const surftypes[] = { "plane", "cylinder", "cone", "sphere", "torus", "bsurf", "blend", "offset", "extrusion",
"s-revolve", "vol", "pos", "rot", "subt", "disk", "board" };
// The select types must be in correct order
const char *const selectTypes[] = { "swSelNOTHING", "swSelEDGES", "swSelFACES", "swSelVERTICES", "swSelDATUMPLANES", "swSelDATUMAXES",
"swSelDATUMPOINTS", "swSelOLEITEMS", "swSelATTRIBUTES", "swSelSKETCHES", "swSelSKETCHSEGS", "swSelSKETCHPOINTS" };
static const int MAX_NUM_SEL_TYPES = 12;
static const float WORLD_BOX_SIZE = 100.0;
static const float PART_BOX_SIZE = 99.0;
static const char *const selectTypeStr(long selTypeID) {
if (selTypeID < 0 || selTypeID >= MAX_NUM_SEL_TYPES)
selTypeID = 0;
return (selectTypes[selTypeID]);
}
static const double MIN_THICKNESS = 0.0005;
static const double MIN_ABS_DIFF = 0.0005;
static const double MIN_PART_SIZE = 0.0001; // Smallest size is 0.1 mm, ignore smaller
typedef struct funcpair {
HRESULT(*boolFunc)(VARIANT_BOOL *arg);
HRESULT(*getParams)(VARIANT *arg);
} surfFuncs;
static bool approxEqual(double val1, double val2) {
double diff = fabs(val1 - val2);
double avg = (fabs(val1) + fabs(val2)) / 2.0;
// Allow relative differences of 9%.
return (diff < MIN_ABS_DIFF || (diff < 0.4 && (avg == 0 || diff / avg < 0.09)));
}
static double checkZero(double val, double precision = 0.0000001) {
if (val != 0.0 && fabs(val) < precision)
val = 0.0;
return (val);
}
// Greater than & less than for doubles that are close
static bool dblgt(double val1, double val2) {
if (checkZero(val1 - val2) == 0.0)
return (false);
return (val1 > val2);
}
static bool dbllt(double val1, double val2) {
if (checkZero(val1 - val2) == 0.0)
return (false);
return (val1 < val2);
}
class coords {
public:
coords(double xval = 0.0, double yval = 0.0, double zval = 0.0, bool doNormalize = false) :
x(xval), y(yval), z(zval)
{
x = checkZero(x);
y = checkZero(y);
z = checkZero(z);
if (doNormalize)
normalize();
}
coords(const coords &operand) {
x = checkZero(operand.x);
y = checkZero(operand.y);
z = checkZero(operand.z);
}
double x, y, z;
double length() const
{
return (sqrt(x*x + y*y + z*z));
}
void normalize() {
double chkLen = length();
if (chkLen != 1.0 && chkLen != 0) {
x = (x / chkLen);
y = (y / chkLen);
z = (z / chkLen);
}
}
coords operator-(const coords &operand) const
{
coords difference;
difference.x = x - operand.x;
if (fabs(difference.x) < MIN_ABS_DIFF)
difference.x = 0.0;
difference.y = y - operand.y;
if (fabs(difference.y) < MIN_ABS_DIFF)
difference.y = 0.0;
difference.z = z - operand.z;
if (fabs(difference.z) < MIN_ABS_DIFF)
difference.z = 0.0;
return (difference);
}
coords operator+(const coords &operand) const
{
coords sum;
sum.x = x + operand.x;
if (fabs(sum.x) < MIN_ABS_DIFF)
sum.x = 0.0;
sum.y = y + operand.y;
if (fabs(sum.y) < MIN_ABS_DIFF)
sum.y = 0.0;
sum.z = z + operand.z;
if (fabs(sum.z) < MIN_ABS_DIFF)
sum.z = 0.0;
return (sum);
}
bool operator==(const coords &operand) const
{
return (approxEqual(x, operand.x) &&
approxEqual(y, operand.y) &&
approxEqual(z, operand.z));
}
bool operator!=(const coords &operand) const
{
return ((*this == operand) == false);
}
coords operator*(double factor) {
coords product;
product.x = x * factor;
product.y = y * factor;
product.z = z * factor;
return (product);
}
coords& operator=(const coords &operand) {
x = checkZero(operand.x);
y = checkZero(operand.y);
z = checkZero(operand.z);
return (*this);
}
};
static const coords zaxis(0.0, 0.0, 1.0),
yaxis(0.0, 1.0, 0.0), xaxis(1.0, 0.0, 0.0),
xaxisminus(-1.0, 0.0, 0.0), zaxisminus(0.0, 0.0, -1.0),
origin(0, 0, 0);
ostream & operator<<(ostream &os, const coords &coordval) {
os << "(" << coordval.x << ", " << coordval.y << ", " << coordval.z << ") ";
return (os);
}
struct coordHash {
size_t operator() (const coords ¶m) const {
std::stringstream strval;
strval << param;
std::hash<string> hashfn;
size_t retval = hashfn(strval.str());
// cout << "Coords " << param << " hash " << retval << endl;
return (retval);
}
};
struct coordCmp {
bool operator() (const coords &first, const coords &second) const {
return (first == second);
}
};
typedef unordered_map<coords, int, coordHash, coordCmp> coordList;
double dotProd(const coords &fac1, const coords &fac2) {
double xpd = fac1.x * fac2.x;
double ypd = fac1.y * fac2.y;
double zpd = fac1.z * fac2.z;
return (xpd + ypd + zpd);
}
coords crossProd(const coords &fac1, const coords &fac2) {
coords prod;
prod.x = (fac1.y * fac2.z) - (fac1.z * fac2.y);
prod.y = (fac1.z * fac2.x) - (fac1.x * fac2.z);
prod.z = (fac1.x * fac2.y) - (fac1.y * fac2.x);
return (prod);
}
class matrix3x3 {
public:
matrix3x3() : goodValues(false)
{}
void setCol(const coords &colVal, unsigned index) {
if (index < 3) {
goodValues = true;
matrix2d[0][index] = colVal.x;
matrix2d[1][index] = colVal.y;
matrix2d[2][index] = colVal.z;
}
else cout << "Index too large " << index << endl;
}
coords operator*(const coords &val) {
coords product;
if (goodValues) {
coords row(matrix2d[0][0], matrix2d[0][1], matrix2d[0][2]);
product.x = dotProd(row, val);
row.x = matrix2d[1][0];
row.y = matrix2d[1][1];
row.z = matrix2d[1][2];
product.y = dotProd(row, val);
row.x = matrix2d[2][0];
row.y = matrix2d[2][1];
row.z = matrix2d[2][2];
product.z = dotProd(row, val);
}
return (product);
}
int empty() {
return (goodValues == false);
}
void clear() {
goodValues = false;
}
bool isIdentity() {
if (goodValues == false)
return (false);
bool isID = true;
for (unsigned col = 0; isID && (col < 3); ++col) {
for (unsigned row = 0; isID && (row < 3); ++row) {
if (row == col )
isID = (isID && matrix2d[row][col] == 1.0);
else isID = (isID && matrix2d[row][col] == 0.0);
}
}
return (isID);
}
double matrix2d[3][3];
bool goodValues;
};
ostream & operator<<(ostream &os, const matrix3x3 &mat) {
for (unsigned row = 0; row < 3; ++row) {
os << "[ ";
for (unsigned col = 0; col < 3; ++col) {
os << mat.matrix2d[row][col];
}
os << " ]" << endl;
}
return (os);
}
typedef struct boxSide {
coords point;
coords lineDir;
double length;
} sideInfo;
typedef struct parts {
int surfInd;
string nameID, volumeName;
bool createVol;
string matNameStr, compName;
bool inBoolSolid;
} partDesc;
class AssemblyInfo;
class CylSurf;
class ConeSurf;
class DiskSurf;
class BoardSurf;
class TorusSurf;
class EllipsoidSurf;
class GenericSurf {
public:
GenericSurf(long surfIDVal, ISurface *swSurf) :
radiusTmp(0), areaTmp(0), diameterTmp(0), perimeterTmp(0),
angleTmp(2.0 * M_PI), lengthTmp(0), surfID(surfIDVal), surfPtr(swSurf),
wasOutput(false), valid(true), subType(BOARD_ID) // Not used for non-plane surfaces
{}
virtual HRESULT boolFunc(VARIANT_BOOL *arg, ISurface *surfPtr) = 0;
virtual HRESULT getParams(VARIANT *arg, ISurface *surfPtr) = 0;
virtual double size() = 0;
virtual CylSurf *cylPtr() {
cout << "Bad call -- CylSurf should override\n";
return (NULL);
}
virtual ConeSurf *conePtr() {
return (NULL);
}
virtual TorusSurf *torusPtr() {
return (NULL);
}
virtual DiskSurf *diskPtr() {
return (NULL);
}
virtual BoardSurf *boardPtr() {
return (NULL);
}
virtual EllipsoidSurf *ellipsoidPtr() {
return (NULL);
}
virtual bool setSize(double radius, double area, double diameter, double perimeter, double angle, double length) {
radiusTmp = radius; // Save these values just in case
areaTmp = area;
diameterTmp = diameter;
perimeterTmp = perimeter;
angleTmp = angle;
lengthTmp = length;
return (false);
}
virtual double getAngle()
{
return (angleTmp);
}
virtual double holeSize() // If surface is a hole
{
return (0.0);
}
double radiusTmp, areaTmp, diameterTmp, perimeterTmp, angleTmp, lengthTmp; // Just for temporary holding of values.
const char *surfName();
double showSurfParams(AssemblyInfo *assembly);
long surfID;
bool wasOutput, valid;
long partInd;
vector<sideInfo> sideList;
string matNameStr, compNameStr, pathNameStr, featureTypeStr;
virtual bool sameBody(GenericSurf *second, bool unused = false)
{
// Check same position, same axis, and same component
if (second != NULL && position == second->position && axis == second->axis &&
compNameStr.compare(second->compNameStr) == 0)
return (true);
return (false);
}
void setSubType(const surfTypeIDs newType) {
subType = newType;
}
surfTypeIDs getSubType() const {
return (subType);
}
coords rotation, position, axis, startAxis, endAxis;
// startAxis is where object starts, corresponding to the x-axis
vector<coords> axisPts, axisList;
// axisPts are points used to help find the axis
// axisList area axis values used to help find the axis
coords displace;
coords overallRot;
matrix3x3 overallRotMatrix;
IFace2 *facePtr;
protected:
ISurface *surfPtr;
surfTypeIDs subType;
};
class ConeSurf : public GenericSurf {
public:
ConeSurf(long surfIDVal, ISurface *swSurf) : GenericSurf(surfIDVal, swSurf), smRadius(0.0), lgRadius(0.0), height(0.0)
{}
virtual HRESULT boolFunc(VARIANT_BOOL *arg, ISurface *surfPtr) {
return (surfPtr->IsCone(arg));
}
virtual HRESULT getParams(VARIANT *arg, ISurface *surfPtr) {
return (surfPtr->get_ConeParams(arg));
}
virtual bool setSize(double radius, double area, double diam, double perimeter, double unused2, double heightVal)
{
// lgRadius = smRadius = height = 0.0;
if (radius <= 0 && diam > 0)
radius = diam / 2.0;
cout << "Cone set size radius smRadius " << radius << " " << smRadius << endl;
if (radius >= 0) {
if (smRadius <= 0)
smRadius = checkZero(radius, MIN_PART_SIZE);
if (perimeter > 0) {
double radTmp = (perimeter / (2.0 * M_PI)) - smRadius;
if (radTmp > 0.0) {
lgRadius = checkZero(radTmp, MIN_PART_SIZE);
if (lgRadius < smRadius) {
lgRadius = checkZero(smRadius, MIN_PART_SIZE);
smRadius = checkZero(radTmp, MIN_PART_SIZE); // Make sure "smRadius" is the smaller value
}
if (height <= 0 && lgRadius > 0 && area > 0 && smRadius >= 0) {
double ht = pow(area / (M_PI * (lgRadius + smRadius)), 2.0) - pow(lgRadius - smRadius, 2.0);
if (ht >= 0.0) {
height = checkZero(sqrt(ht), MIN_PART_SIZE);
}
}
}
}
}
if (radius == -1.0 && heightVal > 0 && area <= 0 && perimeter <= 0 && diam <= 0) // Override height
height = checkZero(heightVal, MIN_PART_SIZE);
if (height > 0)
cout << "Cone height is currently = " << height << endl;
return (lgRadius > 0 && height > 0);
}
virtual double size()
{
return (height);
}
virtual ConeSurf *conePtr() {
return (this);
}
virtual bool sameBody(GenericSurf *second, bool unused = false) {
if (GenericSurf::sameBody(second)) {
ConeSurf *secondCone = second->conePtr();
if (secondCone != NULL) {
cout << "smRadii " << smRadius << " " << secondCone->smRadius << endl;
cout << "lgRadii " << lgRadius << " " << secondCone->lgRadius << endl;
cout << "height " << height << " " << secondCone->height << endl;
return (approxEqual(smRadius, secondCone->smRadius) &&
approxEqual(lgRadius, secondCone->lgRadius) &&
approxEqual(height, secondCone->height));
}
else cout << "cone ptr null\n";
}
return (false);
}
double smRadius, lgRadius, height;
};
class CylSurf : public GenericSurf {
public:
CylSurf(long surfIDVal, ISurface *swSurf) : GenericSurf(surfIDVal, swSurf), radius(0.0), length(0.0), angle(2.0 * M_PI)
{}
virtual HRESULT boolFunc(VARIANT_BOOL *arg, ISurface *surfPtr) {
return (surfPtr->IsCylinder(arg));
}
virtual HRESULT getParams(VARIANT *arg, ISurface *surfPtr) {
return (surfPtr->get_CylinderParams(arg));
}
virtual bool setSize(double rad, double area, double diameter, double perimeter, double angleVal, double lengthVal)
{
if (angleVal > 0.0) {
if (angle != 2.0 * M_PI) {
if (angleVal != angle)
cout << "ERROR: inconsistent angle values! Old = " << angle << " new = " << angleVal << endl;
}
else angle = checkZero(angleVal, MIN_PART_SIZE);
}
if (rad > radius)
radius = checkZero(rad, MIN_PART_SIZE);
// Don't use lengthVal if it is circumference
if (lengthVal > length && approxEqual(lengthVal, diameter / M_PI) == false)
length = checkZero(lengthVal, MIN_PART_SIZE);
else if (area > 0 && diameter > 0.0) {
double newLength = area / (M_PI * diameter);
if (newLength > 0.0)
length = checkZero(newLength, MIN_PART_SIZE);
}
return (radius > 0 && length >= 0.001 && angle > 0); // Omit tiny cylinders
}
virtual double getAngle()
{
return (angle);
}
virtual double size()
{
return (length);
}
virtual double holeSize() // If cylinder is a hole, use diameter
{
return (2.0 * radius);
}
double radius, length, angle;
// friend const char *outputCylDesc(const CylSurf *const surf1, const CylSurf *const surf2);
virtual CylSurf *cylPtr() {
return (this);
}
virtual bool sameBodyLoose(CylSurf *secondCyl) {
if (secondCyl != NULL)
return (approxEqual(angle, secondCyl->angle) &&
approxEqual(length, secondCyl->length)); // Allow differing radii
return (false);
}
virtual bool sameBody(GenericSurf *second, bool loose = false) {
if (GenericSurf::sameBody(second, loose)) {
CylSurf *secondCyl = second->cylPtr();
if (secondCyl != NULL)
return (sameBodyLoose(secondCyl) && (loose || approxEqual(radius, secondCyl->radius) &&
approxEqual(angle * radius, secondCyl->angle * secondCyl->radius)));
}
return (false);
}
};
class TorusSurf : public GenericSurf {
public:
TorusSurf(long surfIDVal, ISurface *swSurf) : GenericSurf(surfIDVal, swSurf),
smRadius(0.0), lgRadius(0.0), majorRadius(0.0), angle(2.0 * M_PI)
{}
virtual HRESULT boolFunc(VARIANT_BOOL *arg, ISurface *surfPtr) {
return (surfPtr->IsTorus(arg));
}
virtual HRESULT getParams(VARIANT *arg, ISurface *surfPtr) {
return (surfPtr->get_TorusParams(arg));
}
virtual bool setSize(double majorRadiusVal, double area, double minorDiam, double perimeter, double angleVal, double heightVal)
{
// lgRadius = smRadius = height = 0.0;
// cout << "Torus set size radius smRadius " << radius << " " << smRadius << endl;
if (majorRadiusVal > 0 && minorDiam > 0) {
double minorRadius = minorDiam / 2.0;
majorRadius = majorRadiusVal;
lgRadius = minorRadius;
}
double calcArea = 4.0 * M_PI * majorRadius * M_PI * lgRadius;
if (calcArea > 0) {
cout << "torus calc area " << calcArea << endl;
double angleVal = 2.0 * M_PI * area / calcArea;
if (angleVal > 0) {
angle = angleVal;
cout << "angle is " << angle << endl;
}
}
return (lgRadius > 0 && majorRadius > 0);
}
virtual double size()
{
return (majorRadius);
}
virtual TorusSurf *torusPtr() {
return (this);
}
virtual double getAngle()
{
return (angle);
}
virtual bool sameBody(GenericSurf *second, bool unused = false) {
if (GenericSurf::sameBody(second)) {
TorusSurf *secondTorus = second->torusPtr();
if (secondTorus != NULL) {
cout << "smRadii " << smRadius << " " << secondTorus->smRadius << endl;
cout << "lgRadii " << lgRadius << " " << secondTorus->lgRadius << endl;
cout << "majorRadius " << majorRadius << " " << secondTorus->majorRadius << endl;
return (approxEqual(smRadius, secondTorus->smRadius) &&
approxEqual(lgRadius, secondTorus->lgRadius) &&
approxEqual(majorRadius, secondTorus->majorRadius) &&
approxEqual(angle * majorRadius, secondTorus->angle * secondTorus->majorRadius));
}
else cout << "torus ptr null\n";
}
return (false);
}
double smRadius, lgRadius, majorRadius, angle;
};
class PlaneSurf : public GenericSurf {
public:
PlaneSurf(long surfIDVal, ISurface *swSurf) : GenericSurf(surfIDVal, swSurf)
{}
PlaneSurf(const GenericSurf &tmp) : GenericSurf(tmp)
{}
virtual HRESULT boolFunc(VARIANT_BOOL *arg, ISurface *surfPtr) {
return (surfPtr->IsPlane(arg));
}
virtual HRESULT getParams(VARIANT *arg, ISurface *surfPtr) {
return (surfPtr->get_PlaneParams(arg));
}
virtual double size() // Default to no size (depth), but disks use diameter for size
{
return (0.0);
}
};
class DiskSurf : public PlaneSurf {
public:
DiskSurf(long surfIDVal, ISurface *swSurf) : PlaneSurf(surfIDVal, swSurf), inRadius(0.0), outRadius(0.0), angle(2.0 * M_PI)
{
subType = DISK_ID;
}
DiskSurf(const GenericSurf &tmp) : PlaneSurf(tmp), inRadius(0.0),
outRadius(tmp.radiusTmp), angle(tmp.angleTmp)
{
subType = DISK_ID;
}
virtual bool setSize(double radius, double unused1, double unused4, double unused2, double angleVal, double unused3)
{
if (radius > 0) { // Take the two biggest radii encountered for in and out radii
if (dbllt(radius, outRadius)) { // Smaller radii are probably holes
if (dblgt(radius, inRadius)) {
inRadius = radius;
}
}
else {
if (dblgt(radius, outRadius)) {
inRadius = outRadius;
}
outRadius = radius;
}
}
if (angleVal > 0.0) {
if (angle != 2.0 * M_PI) {
if (radius != 0) {
if (approxEqual(radius * angleVal, radius * angle)) {
if (angleVal != angle) {
angle = (angleVal + angle) / 2.0;
}
}
else cout << "ERROR: inconsistent angle values! Old = " << angle << " new = " << angleVal << endl;
}
}
else angle = angleVal;
}
return (outRadius > 0 && angle > 0);
}
virtual double getAngle()
{
return (angle);
}
double inRadius, outRadius, angle;
// friend const char *outputCylDesc(const CylSurf *const surf1, const CylSurf *const surf2);
virtual DiskSurf *diskPtr() {
return (this);
}
virtual bool sameBody(GenericSurf *second, bool unused = false) {
if (GenericSurf::sameBody(second)) {
DiskSurf *secondDisk = second->diskPtr();
if (secondDisk != NULL)
return (approxEqual(inRadius, secondDisk->inRadius) &&
approxEqual(outRadius, secondDisk->outRadius) && approxEqual(angle * inRadius, secondDisk->angle * secondDisk->inRadius));
}
return (false);
}
virtual double size() // Use diameter for size
{
return (2.0 * outRadius);
}
};
class BoardSurf : public PlaneSurf {
public:
BoardSurf(long surfIDVal, ISurface *swSurf) : PlaneSurf(surfIDVal, swSurf), length(0.0), width(0.0), consistentVals(true)
{}
BoardSurf(const GenericSurf &tmp) : PlaneSurf(tmp), length(tmp.lengthTmp), width(0.0), consistentVals(true)
{}
virtual bool setSize(double unused5, double unused1, double unused4, double unused2, double unused3, double lengthVal)
{
if (lengthVal > 0) {
if (length == 0) {
length = lengthVal;
}
else if (width == 0) {
width = lengthVal;
}
else if (lengthVal != length && lengthVal != width) {
cout << "Attempt to change length/width. New = " << lengthVal << " old l w = " << length << " " << width << endl;
consistentVals = false;
return (false);
}
}
if (length < width) { // Switch length/width to make length the longer one
lengthVal = length;
length = width;
width = lengthVal;
}
return (consistentVals && length > 0.001 && width > 0.001); // Eliminate mysterious tiny boards
}
double length, width;
bool consistentVals; // Flag to show whether setting inconsistent values was attempted
// friend const char *outputCylDesc(const CylSurf *const surf1, const CylSurf *const surf2);
virtual BoardSurf *boardPtr() {
return (this);
}
virtual bool sameBody(GenericSurf *second, bool unused = false) {
if (GenericSurf::sameBody(second)) {
BoardSurf *secondBoard = second->boardPtr();
if (secondBoard != NULL)
return (approxEqual(length, secondBoard->length) && approxEqual(width, secondBoard->width));
}
return (false);
}
};
// test comment
class EllipsoidSurf : public GenericSurf {
// Currently only implementing half-ellipsoid with circular face
public:
EllipsoidSurf(long surfIDVal, ISurface *swSurf) : GenericSurf(surfIDVal, swSurf),
ax(0.0), by(0.0), cz(0.0), zcutLow(0.0), zcutHi(0.0)
{}
virtual HRESULT boolFunc(VARIANT_BOOL *arg, ISurface *surfPtr) {
return (surfPtr->IsRevolved(arg));
}
virtual HRESULT getParams(VARIANT *arg, ISurface *surfPtr) {
return (surfPtr->GetRevsurfParams(arg));
}
virtual bool setSize(double rad, double area, double diameter, double perimeter, double unused1, double unused2)
{
if (rad > ax) {
ax = by = rad;
}
else if (diameter > (ax * 2.0)) {
ax = by = diameter / 2.0;
}
if (area > 0 && ax > 0 && by > 0) {
// Use area of an ellipsoid, but assume input area is 1/2 full area
cz = (3.0 * pow(area / (2.0 * M_PI), 1.6)) - pow(ax * by, 1.6);
cz /= pow(ax, 1.6) + pow(by, 1.6);
if (cz > 0.0)
cz = pow(cz, 5.0 / 8.0);
else cz = 0;
}
return (ax > 0 && by > 0 && cz > 0);
}
virtual double size()
{
return (2.0 * M_PI * ax);
}
double ax, by, cz, zcutLow, zcutHi;
virtual EllipsoidSurf *ellipsoidPtr() {
return (this);
}
virtual bool sameBody(GenericSurf *second, bool unused = false) {
if (GenericSurf::sameBody(second)) {
EllipsoidSurf *secondEll = second->ellipsoidPtr();
if (secondEll != NULL)
return (approxEqual(ax, secondEll->ax) &&
approxEqual(by, secondEll->by) && approxEqual(cz, secondEll->cz) &&
approxEqual(zcutLow, secondEll->zcutLow) && approxEqual(zcutHi, secondEll->zcutHi));
}
return (false);
}
};
class shapeList {
public:
vector<int> shapeInds;
virtual string outputShapeDesc(const int ind1, const int ind2, AssemblyInfo *assembly) = 0;
};
class cylinderList : public shapeList {
public:
virtual string outputShapeDesc(const int ind1, const int ind2, AssemblyInfo *assembly);
};
class conesList : public shapeList {
public:
virtual string outputShapeDesc(const int ind1, const int ind2, AssemblyInfo *assembly);
};
class torusesList : public shapeList {
public:
virtual string outputShapeDesc(const int ind1, const int ind2, AssemblyInfo *assembly);
};
class disksList : public shapeList {
public:
virtual string outputShapeDesc(const int ind1, const int ind2, AssemblyInfo *assembly);
};
class boardsList : public shapeList {
public:
virtual string outputShapeDesc(const int ind1, const int ind2, AssemblyInfo *assembly);
};
class ellipList : public shapeList {
public:
virtual string outputShapeDesc(const int ind1, const int ind2, AssemblyInfo *assembly);
};
typedef vector < pair<coords, int> > centerSurfIndArray;
template<class T>
class PatternFuncs {
public:
virtual double getSpacing(T *patternType, AssemblyInfo &assembly) = 0;
};
template<class T>
class LinPattFuncs : public PatternFuncs<T> {
public:
virtual double getSpacing(T *patternType, AssemblyInfo &assembly) override;
};
template<class T>
class CircPattFuncs : public PatternFuncs<T> {
public:
virtual double getSpacing(T *patternType, AssemblyInfo &assembly) override;
};
class AssemblyInfo {
public:
AssemblyInfo() :
surfArrayInd(-1), after1stComp(false)
{}
void calcmeasure(CComPtr<IMeasure> &mymeasure, VARIANT &oneface, double radius, long surfID, double &sideLen);
void outputParts();
void outputPartBoxes();
void outputSolids(shapeList *sList, bool looseMatch = false, bool singleSolids = false);
void centerPosition();
void getEdgeDist(IFace2 *const faceptr, CComPtr<IMeasure> &mymeasure, long surfID, double radius);
void showFaceDetails(LPDISPATCH *srcptr, int srcindex, double *radius, long *surfID, CComPtr<IMeasure> &mymeasure);
void outputShapeSetPart(shapeList *sList, const int ind1, const int ind2, bool averagePos = true);
void outputHole(const long baseInd, const long holeInd);
void breakUpFaces(shapeList *sList);
void findHoles(shapeList *sList);
bool getLineInfo(CComPtr<ICurve> curveptr, sideInfo &thisSide);
void calcBoard();
void getInitRot(const int index);
coords getStartAxis(IEdge *const edgeptr, const coords ¢er, const coords &axis, double circRad, coords &endAxis);
coords getbcurve(CComPtr<ICurve> curveptr);
void getMates(IComponent2 *swSelectedComponent);
// void showMateEntity(CComPtr<IFeature> swFeature);
void showMate(IMate2 *matePtr, coords &antiAlignTot, int &alignCnt, coordList &displaceList,
int mateInd, bool &adjustDisplace, int mateCnt, bool &meRadiiSame);
void resetAxis(const coords ¢er, const coords &startVertex, const coords &endVertex);
coords choseStartAxis(const coords ¢er, const coords &radVec, const coords &normal,
const coords &startVertex, const coords &endVertex, coords &endAxis);
void getSeedComps(ILocalLinearPatternFeatureData *const linpattern);
void getRelatedComps(IComponent2 *baseComp);
void getTransfDisplace(ILocalLinearPatternFeatureData *linpattern, double xspacing, double zspacing);
void findTorusCenter();
void chkEllipAxis();
void chkPatterns(CComPtr<IFeature> swFeature, IModelDoc2* swModel, const CComBSTR &sTypeName);
template<typename T>
void procPattern(CComPtr<IFeature> swFeature, IModelDoc2* swModel, PatternFuncs<T> *pattfuncs);
void doClosestAlign(const long index, coords &newAxis, const coords &direction);
void doAligned(int &alignCnt, bool &adjustDisplace, coords &location, const long index,
const long mateRefType, const int mateCnt, const long mateType, const coords &direction);
void doAntiAligned(bool &adjustDisplace, coords &location, const long index,
const long mateRefType, const int mateCnt, const coords &direction, const coords &direction1);
void getMateFaces(IMate2 *const matePtr) const;
void calcMateDisplace(const coords &posNoDispl);
void checkMateRot(const coords &normal);
void outputPhysVols();
void getMateEdge(IEntity *const mateRefPtr, const coords &location, const coords &direction, bool &adjustDisplace);
void getTransf(IComponent2 *swSelectedComponent);
vector<GenericSurf *> surfArray;
int surfArrayInd;
bool after1stComp;
char matNameStr[80];
string compNameStr, pathNameStr, featureTypeStr;
vector<coords> pattDisplaceList;
protected:
cylinderList cylList;
conesList coneList;
torusesList torusList;
disksList diskList;
boardsList boardList;
ellipList ellipsoidList;
vector<partDesc> partArray;
centerSurfIndArray edgeList;
vector<pair<int, int>> holeList;
unordered_set<string> compList;
class mateInfoStruc {
public:
void clear() {
displace = origin; // Clear values from previous component
overallRot = origin;
rotMatrix.clear();
overallRotCmpNmStr.clear();
origpos = origin;
origdir = origin;
displpos = origin;
displdir = origin;
axis = origin;
edgeSet = false;
faceRot = false;
pendingDisplace = false;
faceptr = NULL;
}
coords displace;
coords overallRot;