-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemblyconstraint.cpp
2757 lines (2211 loc) · 88.3 KB
/
assemblyconstraint.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
/*
* assemblyconstraint.cpp
*
* AssemblyConstraintManager class: model the constraints which locate a subassembly
* Copyright (C) 2002 lignum Computing, Inc. <lignumcad@lignumcomputing.com>
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <qdom.h>
#include <gp_Ax2.hxx>
#include <gp_Pln.hxx>
//#include <Bnd_Box.hxx>
//#include <BRepBndLib.hxx>
#include <BRep_Tool.hxx>
#include <BRepTools.hxx>
#include <Geom_Plane.hxx>
#include <Geom_Surface.hxx>
#include <GeomTools.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shape.hxx>
#include <TopExp_Explorer.hxx>
#include "constants.h"
#include "dburl.h"
#include "model.h"
#include "assembly.h"
#include "subassembly.h"
#include "assemblyconstraint.h"
#include "lcdebug.h"
/*!
* Generic example of constraining two planes.
*/
struct PlnPln : public SurfacePair {
PlnPln ( const AssemblyConstraint* parent )
: SurfacePair( parent )
{}
PlnPln ( const QDomElement& xml_rep, const AssemblyConstraint* parent )
: SurfacePair( parent )
{
QString surface0_db_url = xml_rep.attribute( lC::STR::SURFACE0 );
if ( !surface0_db_url.isEmpty() ) {
DBURL surface0_db( surface0_db_url );
surface0_id_ = parent_->model()->pathID( DBURL( surface0_db_url ) );
face0_ = TopoDS::Face( parent_->model()->lookupShape( surface0_id_ ) );
plane0_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face0_ ) );
}
QString surface1_db_url = xml_rep.attribute( lC::STR::SURFACE1 );
if ( !surface1_db_url.isEmpty() ) {
DBURL surface1_db( surface1_db_url );
surface1_id_ = parent_->model()->pathID( DBURL( surface1_db_url ) );
face1_ = TopoDS::Face( parent_->model()->lookupShape( surface1_id_ ) );
plane1_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face1_ ) );
}
}
//! \return the id path to the first plane (and let the requestor
//! parse it out as desired).
QVector<uint> reference0 ( void ) const
{
return surface0_id_;
}
//! \return the id path to the second plane (and let the requestor
//! parse it out as desired).
QVector<uint> reference1 ( void ) const
{
return surface1_id_;
}
/*!
* Remove the reference to the second surface. Should also un-transform
* the subassembly back to where is was...
*/
void removeLastReference ( void )
{
surface1_id_.clear();
face1_.Nullify();
plane1_.Nullify();
}
/*!
* Recompute the constraint due to the subassembly having changed. But
* only if this constraint is complete!
*/
void recompute ( void )
{
if ( surface0_id_.empty() || surface1_id_.empty() ) return;
face0_ = TopoDS::Face( parent_->model()->lookupShape( surface0_id_ ) );
plane0_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face0_ ) );
face1_ = TopoDS::Face( parent_->model()->lookupShape( surface1_id_ ) );
plane1_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face1_ ) );
transform();
}
//! \return the offset (0 by default, though).
virtual double offset ( void ) const { return 0; }
/*!
* Store the plane's new offset (ignored by default, though).
* \param offset new offset.
*/
virtual void setOffset ( double /*offset*/ ) {}
/*!
* Update the plane's offset implies that the subassembly transformation
* is recomputed (ignored by default, though).
* \param offset new offset.
*/
virtual void updateOffset ( double /*offset*/ ) {}
//! \return the first end of a dimension representing the constraint's offset.
Space3D::Point end0 ( void ) const { return end0_; }
//! \return the second end of a dimension representing the constraint's offset.
Space3D::Point end1 ( void ) const { return end1_; }
//! \return the normal direction of a dimension representing the constraint's
//! offset.
Space3D::Vector normal ( void ) const { return normal_; }
/*!
* Serialize constraint to XML representation. (At least) the offset
* versions also have to append some extra data to the representation;
* so, this method is virtual.
* \param xml_rep Manager XML representation to append to.
*/
virtual void write ( QDomElement& xml_rep ) const
{
xml_rep.setAttribute( lC::STR::SURFACE, lC::STR::PLANE );
QString surface0_path = parent_->model()->idPath( surface0_id_ );
QUrl::toPercentEncoding( surface0_path );
DBURL surface0_db_url( lC::STR::DB_PREFIX, surface0_path );
xml_rep.setAttribute( lC::STR::SURFACE0, surface0_db_url.toString( ) );
QString surface1_path = parent_->model()->idPath( surface1_id_ );
QUrl::toPercentEncoding( surface1_path );
DBURL surface1_db_url( lC::STR::DB_PREFIX, surface1_path );
xml_rep.setAttribute( lC::STR::SURFACE1, surface1_db_url.toString( ) );
}
//! Compute the parameters of a dimension.
void updateDimension ( void )
{
// Recompute face0 and plane0.
face0_ = TopoDS::Face( parent_->model()->lookupShape( surface0_id_));
plane0_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face0_ ) );
// 1. Arbitrarily take the first vertex from face0 for end0.
TopExp_Explorer vertex( face0_, TopAbs_VERTEX );
gp_Pnt end0 = BRep_Tool::Pnt( TopoDS::Vertex( vertex.Current() ) );
end0_ = Space3D::Point( end0.X(), end0.Y(), end0.Z() );
// 2. Find the closest vertex in face1 for end1.
vertex.Init( face1_, TopAbs_VERTEX );
gp_Pnt end1 = BRep_Tool::Pnt( TopoDS::Vertex( vertex.Current() ) );
vertex.Next();
for ( ; vertex.More(); vertex.Next() ) {
gp_Pnt v = BRep_Tool::Pnt( TopoDS::Vertex( vertex.Current() ) );
if ( end0.SquareDistance( v ) < end0.SquareDistance( end1 ) )
end1 = v;
}
end1_ = Space3D::Point( end1.X(), end1.Y(), end1.Z() );
// 3. Compute a direction for the dimension extension lines (aka, the
// dimension normal). Start with the projection of the vector from
// end1 to end0 onto the plane of face1. (Note that face0 and face1
// are parallel at this point.)
gp_Vec d_ends( end1, end0 );
double nx = d_ends * plane1_->Pln().XAxis().Direction();
double ny = d_ends * plane1_->Pln().YAxis().Direction();
gp_Vec n = nx * plane1_->Pln().XAxis().Direction() +
ny * plane1_->Pln().YAxis().Direction();
// If this vector is not null (i.e., end0 and end1 don't line up along
// the face normal), use it as the dimension normal.
if ( n.SquareMagnitude() > Precision::Confusion() ) {
normal_ = Space3D::Vector( n.X(), n.Y(), n.Z() );
normal_.normalize();
}
// Otherwise, we just pick an arbitrary direction in the plane of face1.
else {
gp_Dir nnx = plane1_->Pln().XAxis().Direction();
normal_ = Space3D::Vector( nnx.X(), nnx.Y(), nnx.Z() );
}
}
QVector<uint> surface0_id_;
TopoDS_Face face0_;
Handle( Geom_Plane ) plane0_;
QVector<uint> surface1_id_;
TopoDS_Face face1_;
Handle( Geom_Plane ) plane1_;
// Optional, really...
Space3D::Point end0_;
Space3D::Point end1_;
Space3D::Vector normal_;
};
struct MatePlnPln0 : public PlnPln {
MatePlnPln0 ( const AssemblyConstraint* parent )
: PlnPln( parent )
{}
MatePlnPln0 ( const QDomElement& xml_rep, const AssemblyConstraint* parent )
: PlnPln( xml_rep, parent )
{
// I guess it's possible that the XML representation was not complete.
if ( ! ( face0_.IsNull() || face1_.IsNull() ) )
transform();
}
//! \return a (more or less) deep copy of this object.
MatePlnPln0* clone ( void ) const
{
MatePlnPln0* new_plnpln = new MatePlnPln0( parent_ );
new_plnpln->surface0_id_ = surface0_id_;
new_plnpln->face0_ = face0_;
new_plnpln->plane0_ = plane0_;
new_plnpln->surface1_id_ = surface1_id_;
new_plnpln->face1_ = face1_;
new_plnpln->plane1_ = plane1_;
return new_plnpln;
}
//! \return the coordinate system characteristic of this constraint.
gp_Ax2 characteristic ( void ) const
{
if ( plane1_.IsNull() )
return gp::XOY(); // Probably should be an error...
// OK. Have to construct an appropriate coordinate system. The
// Direction is the *face1's* outward normal, which may be reversed from
// from the surface's normal.
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
return gp_Ax2( plane1_->Pln().Axis().Location(), n1,
plane1_->Pln().Position().XDirection() );
}
AssemblyConstraintStatus validate0 ( const QVector<uint>& surface0_id )
{
surface0_id_ = surface0_id;
TopoDS_Shape shape0 = parent_->model()->lookupShape( surface0_id_ );
// We already know this is a plane of some sort.
face0_ = TopoDS::Face( shape0 );
Handle(Geom_Surface) surface0 = BRep_Tool::Surface( face0_ );
plane0_ = Handle(Geom_Plane)::DownCast( surface0 );
// To the best of my knowledge, nothing can go wrong here.
return OK;
}
AssemblyConstraintStatus validate1 ( const QVector<uint>& surface1_id )
{
Handle(Standard_Type) type = parent_->model()->lookupType(surface1_id);
if ( type != STANDARD_TYPE( Geom_Plane ) )
return Invalid;
surface1_id_ = surface1_id;
TopoDS_Shape shape1 = parent_->model()->lookupShape( surface1_id_ );
// We already know this is a plane of some sort.
face1_ = TopoDS::Face( shape1 );
Handle(Geom_Surface) surface1 = BRep_Tool::Surface( face1_ );
plane1_ = Handle(Geom_Plane)::DownCast( surface1 );
// The subassembly is completely free, so any surface is acceptable.
return ConstraintComplete;
}
void transform ( void )
{
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
// Twist the first subassembly around so that the normals point in
// opposite directions.
if ( !n0.IsOpposite( n1, Precision::Angular() ) ) {
if ( n0.IsEqual( n1, Precision::Angular() ) ) {
gp_Dir x_dir = plane0_->Pln().XAxis().Direction();
parent_->subassembly()->rotate( x_dir, M_PI );
}
else {
gp_Dir dir = n0 ^ n1;
double angle = n0.Angle( n1 ) - M_PI;
parent_->subassembly()->rotate( dir, angle );
}
// Need to reconstruct the transformation of the face.
face0_ = TopoDS::Face( parent_->model()->lookupShape( surface0_id_));
plane0_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face0_ ) );
}
// Translate the origin of plane0 so that it lies on plane1.
gp_Vec delta( plane0_->Pln().Axis().Location(),
plane1_->Pln().Axis().Location() );
double distance = delta * n1;
gp_Vec translation = distance * n1;
parent_->subassembly()->translate( translation );
}
};
struct MatePlnPln1 : public PlnPln {
MatePlnPln1 ( const AssemblyConstraint* parent )
: PlnPln( parent )
{}
MatePlnPln1 ( const QDomElement& xml_rep, const AssemblyConstraint* parent )
: PlnPln( xml_rep, parent )
{
// I guess it's possible that the XML representation was not complete.
if ( ! ( face0_.IsNull() || face1_.IsNull() ) )
transform();
}
//! \return a (more or less) deep copy of this object.
MatePlnPln1* clone ( void ) const
{
MatePlnPln1* new_plnpln = new MatePlnPln1( parent_ );
new_plnpln->surface0_id_ = surface0_id_;
new_plnpln->face0_ = face0_;
new_plnpln->plane0_ = plane0_;
new_plnpln->surface1_id_ = surface1_id_;
new_plnpln->face1_ = face1_;
new_plnpln->plane1_ = plane1_;
return new_plnpln;
}
//! \return the coordinate system characteristic of this constraint.
gp_Ax2 characteristic ( void ) const
{
if ( plane1_.IsNull() )
return gp::XOY(); // Probably should be an error...
// From this constraint, we report the remaining direction in which
// the subassembly can be moved. That is, the subassembly is already
// constrained in to two directions, so it can only move in the direction
// of their cross product (so, Ax2 is overkill for this constraint).
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
gp_Dir n2 = n1.Crossed( parent_->characteristic().Direction() );
return gp_Ax2( plane1_->Pln().Axis().Location(), n2 );
}
AssemblyConstraintStatus validate0 ( const QVector<uint>& surface0_id )
{
surface0_id_ = surface0_id;
TopoDS_Shape shape0 = parent_->model()->lookupShape( surface0_id_ );
// We already know this is a plane of some sort.
face0_ = TopoDS::Face( shape0 );
Handle(Geom_Surface) surface0 = BRep_Tool::Surface( face0_ );
plane0_ = Handle(Geom_Plane)::DownCast( surface0 );
if ( plane0_->Pln().Axis().Direction().
IsOpposite( parent_->characteristic().Direction(),
Precision::Angular() ) ||
plane0_->Pln().Axis().Direction().
IsEqual( parent_->characteristic().Direction(),
Precision::Angular() ) )
return Invalid;
return OK;
}
AssemblyConstraintStatus validate1 ( const QVector<uint>& surface1_id )
{
Handle(Standard_Type) type = parent_->model()->lookupType(surface1_id);
if ( type != STANDARD_TYPE( Geom_Plane ) )
return Invalid;
surface1_id_ = surface1_id;
TopoDS_Shape shape1 = parent_->model()->lookupShape( surface1_id_ );
// We already know this is a plane of some sort.
face1_ = TopoDS::Face( shape1 );
Handle(Geom_Surface) surface1 = BRep_Tool::Surface( face1_ );
plane1_ = Handle(Geom_Plane)::DownCast( surface1 );
// In the second phase, the mate can only be made if the plane normals
// make the same (polar) angle with the characteristic: That is, you must
// be able to rotate the subassembly about the characteristic to make
// the plane normals line up.
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
if ( fabs( parent_->characteristic().Direction().Angle( n0 ) -
parent_->characteristic().Direction().Angle( n1 ) )
> Precision::Angular() )
return Invalid;
return ConstraintComplete;
}
void transform ( void )
{
// Twist the first subassembly around so that the normals point in
// opposite directions.
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
if ( !n0.IsOpposite( n1, Precision::Angular() ) ) {
if ( n0.IsEqual( n1, Precision::Angular() ) ) {
// Roll around on the same azimuth(?)
gp_Dir meln = parent_->characteristic().Direction();
parent_->subassembly()->rotate( meln, M_PI );
}
else {
// If the two planes are not at least parallel, we have the
// added convolution that the direction of rotation is the
// characteristic. So, we have to find the azimuthal angle
// which separates n0 and n1 with respect to the
// characteristic. Works like this:
// 1. Project n0 onto the plane defined by the characteristic.
double n0x = n0 * parent_->characteristic().XDirection();
double n0y = n0 * parent_->characteristic().YDirection();
gp_Vec n0p = n0x * parent_->characteristic().XDirection() +
n0y * parent_->characteristic().YDirection();
// 2. Project n1 onto the plane defined by the characteristic.
double n1x = n1 * parent_->characteristic().XDirection();
double n1y = n1 * parent_->characteristic().YDirection();
gp_Vec n1p = n1x * parent_->characteristic().XDirection() +
n1y * parent_->characteristic().YDirection();
// 3. The angle between the projected directions is just the azimuthal
// angle.
double dphi = n0p.Angle( n1p );
gp_Dir meln = n0p ^ n1p;
parent_->subassembly()->rotate( meln, dphi - M_PI );
}
// Need to reconstruct the transformation of the face.
face0_ = TopoDS::Face( parent_->model()->lookupShape( surface0_id_));
plane0_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face0_ ) );
}
// Translate the origin of plane0 so that it lies on plane1 with the
// added requirement that the origin must move parallel to the
// characteristic plane.
// Compute a direction mostly normal to plane1 which also lies in the
// plane of the characteristic.
gp_Dir omega = parent_->characteristic().Direction().
CrossCrossed( n1, parent_->characteristic().Direction() );
// Compute the distance along omega from plane0's origin to plane1.
double distance = ( gp_Vec( plane0_->Pln().Axis().Location(),
plane1_->Pln().Axis().Location() ) * n1 ) /
( omega * n1 );
gp_Vec translation = distance * omega;
parent_->subassembly()->translate( translation );
}
};
struct MatePlnPln2 : public PlnPln {
MatePlnPln2 ( const AssemblyConstraint* parent )
: PlnPln( parent )
{}
MatePlnPln2 ( const QDomElement& xml_rep, const AssemblyConstraint* parent )
: PlnPln( xml_rep, parent )
{
// I guess it's possible that the XML representation was not complete.
if ( ! ( face0_.IsNull() || face1_.IsNull() ) )
transform();
}
//! \return a (more or less) deep copy of this object.
MatePlnPln2* clone ( void ) const
{
MatePlnPln2* new_plnpln = new MatePlnPln2( parent_ );
new_plnpln->surface0_id_ = surface0_id_;
new_plnpln->face0_ = face0_;
new_plnpln->plane0_ = plane0_;
new_plnpln->surface1_id_ = surface1_id_;
new_plnpln->face1_ = face1_;
new_plnpln->plane1_ = plane1_;
return new_plnpln;
}
//! \return the coordinate system characteristic of this constraint.
gp_Ax2 characteristic ( void ) const
{
// Nothing comes after this so this method should never be invoked...
return gp::XOY();
}
AssemblyConstraintStatus validate0 ( const QVector<uint>& surface0_id )
{
surface0_id_ = surface0_id;
TopoDS_Shape shape0 = parent_->model()->lookupShape( surface0_id_ );
// We already know this is a plane of some sort.
face0_ = TopoDS::Face( shape0 );
Handle(Geom_Surface) surface0 = BRep_Tool::Surface( face0_ );
plane0_ = Handle(Geom_Plane)::DownCast( surface0 );
// In the third phase, the selected subassembly surface must not be
// normal to the phase two characteristic.
if ( (plane0_->Pln().Axis().Direction().
IsNormal( parent_->characteristic().Direction(), Precision::Angular() ) ))
return Invalid;
return OK;
}
AssemblyConstraintStatus validate1 ( const QVector<uint>& surface1_id )
{
Handle(Standard_Type) type = parent_->model()->lookupType(surface1_id);
if ( type != STANDARD_TYPE( Geom_Plane ) )
return Invalid;
surface1_id_ = surface1_id;
TopoDS_Shape shape1 = parent_->model()->lookupShape( surface1_id_ );
// We already know this is a plane of some sort.
face1_ = TopoDS::Face( shape1 );
Handle(Geom_Surface) surface1 = BRep_Tool::Surface( face1_ );
plane1_ = Handle(Geom_Plane)::DownCast( surface1 );
// Since only a translation is possible in phase three, the surfaces
// must already have opposite normals.
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
if ( !n0.IsOpposite( n1, Precision::Angular() ) )
return Invalid;
return ConstraintComplete;
}
void transform ( void )
{
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
// Only a translation in the characteristic direction is
// possible at this point.
gp_Vec delta( plane0_->Pln().Axis().Location(),
plane1_->Pln().Axis().Location() );
double distance = ( delta * n1 ) / ( parent_->characteristic().Direction()*n1);
gp_Vec translation = distance * parent_->characteristic().Direction();
parent_->subassembly()->translate( translation );
}
};
struct AlignPlnPln0 : public PlnPln {
AlignPlnPln0 ( const AssemblyConstraint* parent )
: PlnPln( parent )
{}
AlignPlnPln0 ( const QDomElement& xml_rep, const AssemblyConstraint* parent )
: PlnPln( xml_rep, parent )
{
// I guess it's possible that the XML representation was not complete.
if ( ! ( face0_.IsNull() || face1_.IsNull() ) )
transform();
}
//! \return a (more or less) deep copy of this object.
AlignPlnPln0* clone ( void ) const
{
AlignPlnPln0* new_plnpln = new AlignPlnPln0( parent_ );
new_plnpln->surface0_id_ = surface0_id_;
new_plnpln->face0_ = face0_;
new_plnpln->plane0_ = plane0_;
new_plnpln->surface1_id_ = surface1_id_;
new_plnpln->face1_ = face1_;
new_plnpln->plane1_ = plane1_;
return new_plnpln;
}
//! \return the coordinate system characteristic of this constraint. For a
//! plane, it's the normal of the SECOND plane.
gp_Ax2 characteristic ( void ) const
{
if ( plane1_.IsNull() )
return gp::XOY();
// OK. Have to construct an appropriate coordinate system. The
// Direction is the *face1's* outward normal, which may be reversed from
// from the surface's normal.
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
return gp_Ax2( plane1_->Pln().Axis().Location(), n1,
plane1_->Pln().Position().XDirection() );
}
AssemblyConstraintStatus validate0 ( const QVector<uint>& surface0_id )
{
surface0_id_ = surface0_id;
TopoDS_Shape shape0 = parent_->model()->lookupShape( surface0_id_ );
// We already know this is a plane of some sort.
face0_ = TopoDS::Face( shape0 );
Handle(Geom_Surface) surface0 = BRep_Tool::Surface( face0_ );
plane0_ = Handle(Geom_Plane)::DownCast( surface0 );
// To the best of my knowledge, nothing can go wrong here.
return OK;
}
AssemblyConstraintStatus validate1 ( const QVector<uint>& surface1_id )
{
Handle(Standard_Type) type = parent_->model()->lookupType(surface1_id);
if ( type != STANDARD_TYPE( Geom_Plane ) )
return Invalid;
surface1_id_ = surface1_id;
TopoDS_Shape shape1 = parent_->model()->lookupShape( surface1_id_ );
// We already know this is a plane of some sort.
face1_ = TopoDS::Face( shape1 );
Handle(Geom_Surface) surface1 = BRep_Tool::Surface( face1_ );
plane1_ = Handle(Geom_Plane)::DownCast( surface1 );
// The subassembly is completely free, so any surface is acceptable.
return ConstraintComplete;
}
void transform ( void )
{
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
// Twist the first subassembly around so that the normals point in
// the same direction.
if ( !n0.IsEqual( n1, Precision::Angular() ) ) {
if ( n0.IsOpposite( n1, Precision::Angular() ) ) {
gp_Dir x_dir = plane0_->Pln().XAxis().Direction();
parent_->subassembly()->rotate( x_dir, M_PI );
}
else {
gp_Dir dir = n0 ^ n1;
double angle = n0.Angle( n1 );
parent_->subassembly()->rotate( dir, angle );
}
// Need to reconstruct the transformation of the face.
face0_ = TopoDS::Face( parent_->model()->lookupShape( surface0_id_));
plane0_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face0_ ) );
}
// Translate the origin of plane0 so that it lies on plane1. Note:
gp_Vec delta( plane0_->Pln().Axis().Location(),
plane1_->Pln().Axis().Location() );
double distance = delta * n1;
gp_Vec translation = distance * n1;
parent_->subassembly()->translate( translation );
}
};
struct AlignPlnPln1 : public PlnPln {
AlignPlnPln1 ( const AssemblyConstraint* parent )
: PlnPln( parent )
{}
AlignPlnPln1 ( const QDomElement& xml_rep, const AssemblyConstraint* parent )
: PlnPln( xml_rep, parent )
{
// I guess it's possible that the XML representation was not complete.
if ( ! ( face0_.IsNull() || face1_.IsNull() ) )
transform();
}
//! \return a (more or less) deep copy of this object.
AlignPlnPln1* clone ( void ) const
{
AlignPlnPln1* new_plnpln = new AlignPlnPln1( parent_ );
new_plnpln->surface0_id_ = surface0_id_;
new_plnpln->face0_ = face0_;
new_plnpln->plane0_ = plane0_;
new_plnpln->surface1_id_ = surface1_id_;
new_plnpln->face1_ = face1_;
new_plnpln->plane1_ = plane1_;
return new_plnpln;
}
//! \return the coordinate system characteristic of this constraint. For a
//! plane, it's the normal of the SECOND plane.
gp_Ax2 characteristic ( void ) const
{
if ( plane1_.IsNull() )
return gp::XOY(); // Probably should be an error...
// From this constraint, we report the remaining direction in which
// the subassembly can be moved. That is, the subassembly is already
// constrained in to two directions, so it can only move in the direction
// of their cross product (so, Ax2 is overkill for this constraint).
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
gp_Dir n2 = n1.Crossed( parent_->characteristic().Direction() );
return gp_Ax2( plane1_->Pln().Axis().Location(), n2 );
}
AssemblyConstraintStatus validate0 ( const QVector<uint>& surface0_id )
{
surface0_id_ = surface0_id;
TopoDS_Shape shape0 = parent_->model()->lookupShape( surface0_id_ );
// We already know this is a plane of some sort.
face0_ = TopoDS::Face( shape0 );
Handle(Geom_Surface) surface0 = BRep_Tool::Surface( face0_ );
plane0_ = Handle(Geom_Plane)::DownCast( surface0 );
// In phase two, the selected subassembly surface normal can point in
// any direction except exactly in the same direction as the phase one
// characteristic.
if ( plane0_->Pln().Axis().Direction().
IsOpposite( parent_->characteristic().Direction(),
Precision::Angular() ) ||
plane0_->Pln().Axis().Direction().
IsEqual( parent_->characteristic().Direction(),
Precision::Angular() ) )
return Invalid;
return OK;
}
AssemblyConstraintStatus validate1 ( const QVector<uint>& surface1_id )
{
Handle(Standard_Type) type = parent_->model()->lookupType(surface1_id);
if ( type != STANDARD_TYPE( Geom_Plane ) )
return Invalid;
surface1_id_ = surface1_id;
TopoDS_Shape shape1 = parent_->model()->lookupShape( surface1_id_ );
// We already know this is a plane of some sort.
face1_ = TopoDS::Face( shape1 );
Handle(Geom_Surface) surface1 = BRep_Tool::Surface( face1_ );
plane1_ = Handle(Geom_Plane)::DownCast( surface1 );
// In the second phase, the alignment can only be made if the plane normals
// make the same (polar) angle with the characteristic: That is, you must
// be able to rotate the subassembly about the characteristic to make
// the plane normals line up.
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
if ( fabs( parent_->characteristic().Direction().Angle( n0 ) -
parent_->characteristic().Direction().Angle( n1 ) )
> Precision::Angular() )
return Invalid;
return ConstraintComplete;
}
void transform ( void )
{
// Twist the first subassembly around so that the normals point in
// the same direction.
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
if ( !n0.IsEqual( n1, Precision::Angular() ) ) {
if ( n0.IsOpposite( n1, Precision::Angular() ) ) {
// Roll around on the same azimuth(?)
gp_Dir meln = parent_->characteristic().Direction();
parent_->subassembly()->rotate( meln, M_PI );
}
else {
// If the normals are not already parallel, this has the added
// convolution that the direction of rotation is the
// characteristic. So, we have to find the azimuthal angle
// which separates n0 and n1 with respect to the
// characteristic. Works like this:
// 1. Project n0 onto the plane defined by the characteristic.
double n0x = n0 * parent_->characteristic().XDirection();
double n0y = n0 * parent_->characteristic().YDirection();
gp_Vec n0p = n0x * parent_->characteristic().XDirection() +
n0y * parent_->characteristic().YDirection();
// 2. Project n1 onto the plane defined by the characteristic.
double n1x = n1 * parent_->characteristic().XDirection();
double n1y = n1 * parent_->characteristic().YDirection();
gp_Vec n1p = n1x * parent_->characteristic().XDirection() +
n1y * parent_->characteristic().YDirection();
// 3. The angle between the projected directions is just the
// azimuthal angle.
double dphi = n0p.Angle( n1p );
gp_Dir meln = n0p ^ n1p;
parent_->subassembly()->rotate( meln, dphi );
}
// Need to reconstruct the transformation of the face.
face0_ = TopoDS::Face( parent_->model()->lookupShape( surface0_id_));
plane0_ = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( face0_ ) );
}
// Translate the origin of plane0 so that it lies on plane1 with the
// added requirement that the origin must move parallel to the
// characteristic plane.
// Compute a direction mostly normal to plane1 which also lies in the
// plane of the characteristic.
gp_Dir omega = parent_->characteristic().Direction().
CrossCrossed( n1, parent_->characteristic().Direction() );
// Compute the distance along omega from plane0's origin to plane1.
double distance = ( gp_Vec( plane0_->Pln().Axis().Location(),
plane1_->Pln().Axis().Location() ) * n1 ) /
( omega * n1 );
gp_Vec translation = distance * omega;
parent_->subassembly()->translate( translation );
}
};
struct AlignPlnPln2 : public PlnPln {
AlignPlnPln2 ( const AssemblyConstraint* parent )
: PlnPln( parent )
{}
AlignPlnPln2 ( const QDomElement& xml_rep, const AssemblyConstraint* parent )
: PlnPln( xml_rep, parent )
{
// I guess it's possible that the XML representation was not complete.
if ( ! ( face0_.IsNull() || face1_.IsNull() ) )
transform();
}
//! \return a (more or less) deep copy of this object.
AlignPlnPln2* clone ( void ) const
{
AlignPlnPln2* new_plnpln = new AlignPlnPln2( parent_ );
new_plnpln->surface0_id_ = surface0_id_;
new_plnpln->face0_ = face0_;
new_plnpln->plane0_ = plane0_;
new_plnpln->surface1_id_ = surface1_id_;
new_plnpln->face1_ = face1_;
new_plnpln->plane1_ = plane1_;
return new_plnpln;
}
//! \return the coordinate system characteristic of this constraint. For a
//! plane, it's the normal of the SECOND plane.
gp_Ax2 characteristic ( void ) const
{
// Nothing comes after this so this method should never be invoked...
return gp::XOY();
}
AssemblyConstraintStatus validate0 ( const QVector<uint>& surface0_id )
{
surface0_id_ = surface0_id;
TopoDS_Shape shape0 = parent_->model()->lookupShape( surface0_id_ );
// We already know this is a plane of some sort.
face0_ = TopoDS::Face( shape0 );
Handle(Geom_Surface) surface0 = BRep_Tool::Surface( face0_ );
plane0_ = Handle(Geom_Plane)::DownCast( surface0 );
// In the third phase, the selected subassembly surface has to already be
// non-normal to the phase two characteristic.
if ( (plane0_->Pln().Axis().Direction().
IsNormal( parent_->characteristic().Direction(),
Precision::Angular() ) ) )
return Invalid;
return OK;
}
AssemblyConstraintStatus validate1 ( const QVector<uint>& surface1_id )
{
Handle(Standard_Type) type = parent_->model()->lookupType(surface1_id);
if ( type != STANDARD_TYPE( Geom_Plane ) )
return Invalid;
surface1_id_ = surface1_id;
TopoDS_Shape shape1 = parent_->model()->lookupShape( surface1_id_ );
// We already know this is a plane of some sort.
face1_ = TopoDS::Face( shape1 );
Handle(Geom_Surface) surface1 = BRep_Tool::Surface( face1_ );
plane1_ = Handle(Geom_Plane)::DownCast( surface1 );
// Since only a translation is possible in phase three, the surfaces
// must already have parallel normals.
gp_Dir n0 = plane0_->Pln().Axis().Direction();
gp_Dir n1 = plane1_->Pln().Axis().Direction();
if ( face0_.Orientation() == TopAbs_REVERSED ) n0.Reverse();
if ( face1_.Orientation() == TopAbs_REVERSED ) n1.Reverse();
if ( !n0.IsEqual( n1, Precision::Angular() ) )
return Invalid;
return ConstraintComplete;
}
void transform ( void )
{