Prepared branch for raising to master
[occt.git] / src / DBRep / DBRep.cxx
CommitLineData
b311480e 1// Created on: 1993-07-21
2// Created by: Remi LEQUETTE
3// Copyright (c) 1993-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
26#include <DBRep.ixx>
27#include <DBRep_DrawableShape.hxx>
28#include <Draw_Appli.hxx>
29#include <Draw.hxx>
30#include <Draw_Segment3D.hxx>
31#include <TopAbs.hxx>
32#include <TopoDS.hxx>
33#include <TopoDS_Iterator.hxx>
34#include <TopoDS_Compound.hxx>
35#include <TopTools_MapOfShape.hxx>
36#include <TopTools_ListOfShape.hxx>
37#include <TopExp_Explorer.hxx>
38#include <BRepTools.hxx>
39#include <BRepTools_WireExplorer.hxx>
40#include <BRepTools_ShapeSet.hxx>
41#include <BRepAdaptor_Surface.hxx>
42#include <Precision.hxx>
43#include <Poly_Triangulation.hxx>
44#include <gp_Ax2.hxx>
45
46#include <TopExp.hxx>
47#include <TopTools_Array1OfShape.hxx>
48#include <TColStd_Array1OfInteger.hxx>
49#include <GProp.hxx>
50#include <GProp_GProps.hxx>
51#include <BRepGProp.hxx>
52#include <TColStd_Array1OfReal.hxx>
53#include <Draw_ProgressIndicator.hxx>
54
55// memory management
56#include <Standard.hxx>
57//#ifdef WNT
58#include <stdio.h>
59#ifdef WNT
60extern Draw_Viewer dout;
61#endif
62#ifdef HAVE_STRINGS_H
63# include <strings.h>
64#endif
65
66#define Characters(IArg) (strspn (Arg[IArg], "0123456789.+-eE") != strlen (Arg[IArg]))
67#define Float(IArg) (strspn (Arg[IArg], "0123456789+-") != strlen (Arg[IArg]))
68
69
70//==========================================
0d969553 71// useful methods
7fd59977 72//==========================================
73
74Standard_EXPORT void DBRep_WriteColorOrientation ()
75{
76 cout << "\nrouge FORWARD";
77 cout << "\nbleu REVERSED";
78 cout << "\nrose EXTERNAL";
79 cout << "\norange INTERNAL"<<endl;
80}
81
82Standard_EXPORT Draw_Color DBRep_ColorOrientation (const TopAbs_Orientation Or)
83{
84 Draw_Color col;
85 switch (Or) {
86
87 case TopAbs_FORWARD :
88 col = Draw_rouge;
89 break;
90
91 case TopAbs_REVERSED :
92 col = Draw_bleu;
93 break;
94
95 case TopAbs_EXTERNAL :
96 col = Draw_rose;
97 break;
98
99 case TopAbs_INTERNAL :
100 col = Draw_orange;
101 break;
102
103 }
104 return col;
105}
106
107//==========================================
108// static variables
109//==========================================
110
111static Standard_Integer nbIsos = 2;
112static Standard_Real size = 100.;
113static Standard_Integer discret = 30;
114static Standard_Boolean disptriangles = Standard_False;
115static Standard_Boolean disppolygons = Standard_False;
c6541a0c
D
116static Standard_Real anglHLR = 35 * M_PI / 180;
117static Standard_Real HAngMin = 1 * M_PI / 180;
118static Standard_Real HAngMax = 35 * M_PI / 180;
7fd59977 119static Standard_Boolean withHLR = Standard_False;
120static Standard_Boolean withRg1 = Standard_True;
121static Standard_Boolean withRgN = Standard_False;
122static Standard_Boolean withHid = Standard_False;
123
124//=======================================================================
125// isos
126//=======================================================================
127
128static Standard_Integer isos (Draw_Interpretor& di,
129 Standard_Integer NbArg, const char **Arg)
130{
131 NbArg-- ;
132
133 if (NbArg <= 0) {
134 di << "Current number of isos : " << nbIsos << "\n" ;
135 return 0 ;
136 }
137
138 Standard_Integer NbIsos = 0 ;
139 Standard_Boolean Change = Standard_False ;
140 if (!Characters (NbArg) && Float (NbArg)) return 1 ;
141 if (!Characters (NbArg)) {
142 NbIsos = atoi (Arg[NbArg]) ;
143 NbArg-- ;
144 Change = Standard_True ;
145 }
146
147 if (NbArg <= 0) {
148 nbIsos = NbIsos ;
149 di << "New current number of isos : " << nbIsos << "\n" ;
150 } else {
151 for (Standard_Integer IArg = 1 ; IArg <= NbArg ; IArg++) {
152 Handle (Draw_Drawable3D) Shape1 = Draw::Get (Arg[IArg]) ;
153 if (!Shape1.IsNull()) {
154 Handle (DBRep_DrawableShape) Shape2 =
155 Handle (DBRep_DrawableShape)::DownCast (Shape1) ;
156 if (!Shape2.IsNull()) {
157 if (Change) {
158 Shape2->ChangeNbIsos (NbIsos) ;
159 } else {
160 di << "Number of isos for " << Arg[IArg] << " : " << Shape2->NbIsos() << "\n";
161 }
162 }
163 }
164 }
165 if (Change) dout.RepaintAll() ;
166 }
167
168 return 0 ;
169}
170
171//=======================================================================
172// hlr
173//=======================================================================
174
175static Standard_Integer hlr (Draw_Interpretor& di,
176 Standard_Integer n, const char **a)
177{
178 if (n <= 1) {
179 if (withHLR) {
180 di << " HLR";
181 if (withRgN) di << " RgNLines";
182 else {
183 if (withRg1) di << " Rg1Lines";
184 else di << " no RegLines";
185 }
186 if (withHid) di << " HiddenLines";
187 else di << " no HiddenLines";
188 di << "\n";
189 if (withHLR) {
0d969553 190 di << "Angle of discretization : ";
c6541a0c 191 di << anglHLR * 180 / M_PI << " degrees" << "\n";
7fd59977 192 }
193 }
194 else di << " wireframe";
195 di << "\n";
196 return 0 ;
197 }
198
199 if (n == 2) {
200 if (!strcasecmp(a[1],"nohlr")) withHLR = Standard_False;
201 else if (!strcasecmp(a[1],"hlr" )) withHLR = Standard_True;
202 else if (!strcasecmp(a[1],"nohid")) withHid = Standard_False;
203 else if (!strcasecmp(a[1],"hid" )) {
204 withHLR = Standard_True;
205 withHid = Standard_True;
206 }
207 else if (!strcasecmp(a[1],"norg1")) {
208 withRg1 = Standard_False;
209 withRgN = Standard_False;
210 }
211 else if (!strcasecmp(a[1],"rg1" )) {
212 withHLR = Standard_True;
213 withRg1 = Standard_True;
214 withRgN = Standard_False;
215 }
216 else if (!strcasecmp(a[1],"norgn")) {
217 withRgN = Standard_False;
218 }
219 else if (!strcasecmp(a[1],"rgn" )) {
220 withHLR = Standard_True;
221 withRg1 = Standard_True;
222 withRgN = Standard_True;
223 }
224 else if (!strcasecmp(a[1],"ang" )) {
225 di << "Angle de discretisation : ";
c6541a0c 226 di << anglHLR * 180 / M_PI << " degres" << "\n";
7fd59977 227 }
228 else return 1;
229 }
230
231 Standard_Integer nFirst = 2;
232
233 if (n >= 3 && !strcasecmp(a[1],"ang" )) {
234 nFirst = 3;
235 if (n == 3) {
236 Standard_Real ang = atof(a[2]);
c6541a0c 237 anglHLR = ang * M_PI / 180;
7fd59977 238 if (anglHLR < HAngMin) anglHLR = HAngMin;
239 if (anglHLR > HAngMax) anglHLR = HAngMax;
240 }
0d969553 241 di << "Angle of discretization : ";
c6541a0c 242 di << anglHLR * 180 / M_PI << " degrees" << "\n";
7fd59977 243 }
244
245 if (n >= nFirst + 1) {
246
247 for (Standard_Integer i = nFirst ; i < n; i++) {
248 Handle (Draw_Drawable3D) D = Draw::Get (a[i]) ;
249 if (!D.IsNull()) {
250 Handle (DBRep_DrawableShape) S =
251 Handle (DBRep_DrawableShape)::DownCast (D) ;
252 if (!S.IsNull()) {
253 Standard_Boolean localHLR, localRg1, localRgN, localHid;
254 Standard_Real localAng;
255 S->GetDisplayHLR(localHLR, localRg1, localRgN, localHid,
256 localAng);
257 if (!strcasecmp(a[1],"nohlr")) localHLR = Standard_False;
258 else if (!strcasecmp(a[1],"hlr" )) localHLR = Standard_True;
259 else if (!strcasecmp(a[1],"nohid")) localHid = Standard_False;
260 else if (!strcasecmp(a[1],"hid" )) {
261 localHLR = Standard_True;
262 localHid = Standard_True;
263 }
264 else if (!strcasecmp(a[1],"norg1")) {
265 localRg1 = Standard_False;
266 localRgN = Standard_False;
267 }
268 else if (!strcasecmp(a[1],"rg1" )) {
269 localHLR = Standard_True;
270 localRg1 = Standard_True;
271 localRgN = Standard_False;
272 }
273 else if (!strcasecmp(a[1],"norgn")) {
274 localRgN = Standard_False;
275 }
276 else if (!strcasecmp(a[1],"rgn" )) {
277 localHLR = Standard_True;
278 localRg1 = Standard_True;
279 localRgN = Standard_True;
280 }
281 else if (!strcasecmp(a[1],"ang" )) {
282 Standard_Real ang = atof(a[2]);
c6541a0c 283 localAng = ang * M_PI / 180;
7fd59977 284 }
285 else return 1;
286 S->DisplayHLR(localHLR, localRg1, localRgN, localHid,
287 localAng);
288 }
289 }
290 }
291 }
292 dout.RepaintAll() ;
293
294 return 0 ;
295}
296
297
298//=======================================================================
299// dispor, dispcon
300//=======================================================================
301
302static Standard_Integer dispor (Draw_Interpretor& ,
303 Standard_Integer n, const char** a)
304{
305 Standard_Boolean d = !strcasecmp(a[0],"vori");
306
307 if (d)
308 DBRep_WriteColorOrientation();
309
310 Standard_Integer i;
311 for (i = 1; i < n; i++) {
312 Handle(Draw_Drawable3D) d1 = Draw::Get(a[i]);
313 if (!d1.IsNull()) {
314 Handle(DBRep_DrawableShape) d2 =
315 Handle(DBRep_DrawableShape)::DownCast(d1);
316 if (!d2.IsNull()) {
317 d2->DisplayOrientation(d);
318 Draw::Repaint();
319 }
320 }
321 }
322 return 0;
323}
324
325//=======================================================================
326// discretisation
327//=======================================================================
328
329static Standard_Integer discretisation(Draw_Interpretor& di,
330 Standard_Integer n, const char** a)
331{
332 if (n <= 1)
333 di << "Current number of points : "<<discret<<"\n";
334 else {
335 discret = atoi(a[1]);
336 }
337 return 0;
338}
339
340
341//=======================================================================
342// triangles
343//=======================================================================
344
345static Standard_Integer triangles(Draw_Interpretor& ,
346 Standard_Integer n, const char** a)
347{
348 if (n < 1) return 1;
349
350 if (n == 1) {
351 disptriangles = !disptriangles;
352#ifdef DEB
353 if (disptriangles) cout <<"Triangulations are always displayed"<<endl;
354 else cout <<"Triangulations are displayed only if there is no geometric representation"<<endl;
355#endif
356 }
357 else {
358 Standard_Integer i;
359 for (i = 1; i <= n-1; i++) {
360 Handle(Draw_Drawable3D) d1 = Draw::Get(a[i]);
361 if (!d1.IsNull()) {
362 Handle(DBRep_DrawableShape) d2 =
363 Handle(DBRep_DrawableShape)::DownCast(d1);
364 if (!d2.IsNull()) {
365 d2->DisplayTriangulation(!(d2->DisplayTriangulation()));
366 }
367 }
368 }
369 }
370
371 Draw::Repaint();
372 return 0;
373}
374
375//=======================================================================
376// tclean
377//=======================================================================
378
379static Standard_Integer tclean(Draw_Interpretor& ,
380 Standard_Integer n, const char** a)
381{
382 if (n < 1) return 1;
383
384 for (Standard_Integer i = 1; i < n; i++) {
385 TopoDS_Shape S = DBRep::Get(a[i]);
386 BRepTools::Clean(S);
387 }
388 return 0;
389}
390
391//=======================================================================
392// polygons
393//=======================================================================
394
395static Standard_Integer polygons(Draw_Interpretor& ,
396 Standard_Integer n, const char** a)
397{
398 if (n < 1) return 1;
399
400 if (n == 1) {
401 disppolygons = !disppolygons;
402#ifdef DEB
403 if (disppolygons) cout <<"Polygons are always displayed"<<endl;
404 else cout <<"Polygons are displayed only if there is no geometric representation"<<endl;
405#endif
406 }
407 else {
408 Standard_Integer i;
409 for (i = 1; i <= n-1; i++) {
410 Handle(Draw_Drawable3D) d1 = Draw::Get(a[i]);
411 if (!d1.IsNull()) {
412 Handle(DBRep_DrawableShape) d2 =
413 Handle(DBRep_DrawableShape)::DownCast(d1);
414 if (!d2.IsNull()) {
415 d2->DisplayPolygons(!(d2->DisplayPolygons()));
416 }
417 }
418 }
419 }
420
421 Draw::Repaint();
422 return 0;
423}
424
425
426//=======================================================================
427// compound
428//=======================================================================
429
430static Standard_Integer compound(Draw_Interpretor& ,
431 Standard_Integer n, const char** a)
432{
433 if (n <= 1) return 1;
434 BRep_Builder B;
435 TopoDS_Compound C;
436 B.MakeCompound(C);
437 for (Standard_Integer i = 1; i < n-1; i++) {
438 TopoDS_Shape S2 = DBRep::Get(a[i]);
439 if (!S2.IsNull()) B.Add(C,S2);
440 }
441 DBRep::Set(a[n-1],C);
442 return 0;
443}
444
445//=======================================================================
446// emptycopy
447//=======================================================================
448
449static Standard_Integer emptycopy(Draw_Interpretor& ,
450 Standard_Integer n, const char** a)
451{
452 if (n <= 1) return 1;
453 TopoDS_Shape S = DBRep::Get(a[(n == 2) ? 1 : 2]);
454 if (S.IsNull()) return 1;
455 S.EmptyCopy();
456 DBRep::Set(a[1],S);
457 return 0;
458}
459
460//=======================================================================
461// add
462//=======================================================================
463
464static Standard_Integer add(Draw_Interpretor& ,
465 Standard_Integer n, const char** a)
466{
467 if (n < 3) return 1;
468 BRep_Builder B;
469 TopoDS_Shape S1 = DBRep::Get(a[1]);
470 if (S1.IsNull()) return 1;
471 TopoDS_Shape S2 = DBRep::Get(a[2]);
472 if (S2.IsNull()) return 1;
473 B.Add(S2,S1);
474 DBRep::Set(a[2],S2);
475 return 0;
476}
477
478//=======================================================================
479// explode
480//=======================================================================
481
482static Standard_Integer explode(Draw_Interpretor& di,
483 Standard_Integer n, const char** a)
484{
485 if (n <= 1) return 1;
486 TopoDS_Shape S = DBRep::Get(a[1]);
487 if (S.IsNull()) return 0;
488 char newname[1024];
489 strcpy(newname,a[1]);
490 char* p = newname;
491 while (*p != '\0') p++;
492 *p = '_';
493 p++;
494 Standard_Integer i = 0;
495 if (n == 2) {
496 TopoDS_Iterator itr(S);
497 while (itr.More()) {
498 i++;
499 sprintf(p,"%d",i);
500 DBRep::Set(newname,itr.Value());
501 di.AppendElement(newname);
502 itr.Next();
503 }
504 }
505 else {
506 // explode a type
507 TopAbs_ShapeEnum typ;
508 switch (a[2][0]) {
509
510 case 'C' :
511 case 'c' :
512 if ((a[2][1] == 'd')||(a[2][1] == 'D'))
513 typ = TopAbs_COMPOUND;
514 else
515 typ = TopAbs_COMPSOLID;
516 break;
517
518 case 'S' :
519 case 's' :
520 if ((a[2][1] == 'O')||(a[2][1] == 'o'))
521 typ = TopAbs_SOLID;
522 else if ((a[2][1] == 'H')||(a[2][1] == 'h'))
523 typ = TopAbs_SHELL;
524 else
525 return 1;
526 break;
527
528 case 'F' :
529 case 'f' :
530 typ = TopAbs_FACE;
531 break;
532
533 case 'W' :
534 case 'w' :
535 typ = TopAbs_WIRE;
536 break;
537
538 case 'E' :
539 case 'e' :
540 typ = TopAbs_EDGE;
541 break;
542
543 case 'V' :
544 case 'v' :
545 typ = TopAbs_VERTEX;
546 break;
547
548 default :
549 return 1;
550 }
551
552 TopTools_MapOfShape M;
553 M.Add(S);
554 TopExp_Explorer ex(S,typ);
555 for (; ex.More(); ex.Next()) {
556 const TopoDS_Shape& Sx = ex.Current();
557 Standard_Boolean added = M.Add(Sx);
558 if (added) {
559 i++;
560 sprintf(p,"%d",i);
561 DBRep::Set(newname,Sx);
562 di.AppendElement(newname);
563 }
564 }
565 }
566 return 0;
567}
568
569//=======================================================================
570// nexplode : stable numbered explode (from Serguey Nizhny)
571//=======================================================================
572
573static Standard_Integer nexplode(Draw_Interpretor& di,
574 Standard_Integer n, const char** a)
575{
576 if (n <= 2) return 1;
577 TopoDS_Shape S = DBRep::Get(a[1]);
578 if (S.IsNull()) return 0;
579 char newname[1024];
580 strcpy(newname,a[1]);
581 char* p = newname;
582 while (*p != '\0') p++;
583 *p = '_';
584 p++;
585 TopAbs_ShapeEnum typ;
586 // explode a type
587 switch (a[2][0]) {
588 case 'F' :
589 case 'f' :
590 typ = TopAbs_FACE;
591 break;
592
593 case 'E' :
594 case 'e' :
595 typ = TopAbs_EDGE;
596 break;
597
598 default :
599 return 1;
600 }
601 TopTools_IndexedMapOfShape IMOStmp;
602 TopTools_MapOfShape MShape;
603 IMOStmp.Add(S);
604 TopExp::MapShapes(S,typ,IMOStmp);
605 TopExp_Explorer Exp(S,typ);
606 Standard_Integer MaxShapes, Index = 0;
607 MaxShapes = IMOStmp.Extent()-1;
608 TopTools_Array1OfShape aShapes(1,MaxShapes);
609
610 // explode
611 while (Exp.More()) {
612 if (MShape.Add(Exp.Current())) {
613 Index++;
614 aShapes.SetValue(Index,Exp.Current());
615 }
616 Exp.Next();
617 }
618
619 TColStd_Array1OfInteger OrderInd(1,MaxShapes);
620// gp_Pnt GPoint;
621 GProp_GProps GPr;
622// Standard_Integer InOfminX = 1,aTemp;
623 Standard_Integer aTemp;
624 TColStd_Array1OfReal MidXYZ(1,MaxShapes); //X,Y,Z;
625 Standard_Boolean NoSort = Standard_True;
626
627 // Computing of CentreOfMass
628 for (Index=1; Index <= MaxShapes; Index++) {
629 OrderInd.SetValue(Index,Index);
630 BRepGProp::LinearProperties(aShapes(Index),GPr);
631 gp_Pnt GPoint = GPr.CentreOfMass();
632 MidXYZ.SetValue(Index, GPoint.X()*999 + GPoint.Y()*99 +
633 GPoint.Z()*0.9);
634 }
635 // Sorting
636 while(NoSort) {
637 NoSort = Standard_False;
638 for (Index=1; Index < MaxShapes; Index++) {
639 if (MidXYZ(OrderInd(Index)) > MidXYZ(OrderInd(Index+1))) {
640 aTemp = OrderInd(Index);
641 OrderInd(Index) = OrderInd(Index+1);
642 OrderInd(Index+1) = aTemp;
643 NoSort = Standard_True;
644 }
645 }
646 }
647 // Check of equality of MidXYZ
648 for (Index=1; Index < MaxShapes; Index++) {
649 if (MidXYZ(OrderInd(Index+1)) == MidXYZ(OrderInd(Index)))
650 di<<"Warning! For this shape the results may be incorrect."<<"\n";
651 }
652
653 for (Index=1 ;Index <= MaxShapes; Index++) {
654 sprintf(p,"%d",Index);
655 DBRep::Set(newname,aShapes(OrderInd(Index)));
656 di.AppendElement(newname);
657 }
658
659 return 0;
660}
661
662//=======================================================================
663// exwire
664//=======================================================================
665
666static Standard_Integer exwire(Draw_Interpretor& ,
667 Standard_Integer n, const char** a)
668{
669 if (n <= 1) return 1;
670 TopoDS_Shape S = DBRep::Get(a[1]);
671 if (S.IsNull()) return 0;
672 if (S.ShapeType() != TopAbs_WIRE) return 0;
673 char newname[1024];
674 strcpy(newname,a[1]);
675 char* p = newname;
676 while (*p != '\0') p++;
677 *p = '_';
678 p++;
679 Standard_Integer i = 0;
680 BRepTools_WireExplorer ex(TopoDS::Wire(S));
681 while (ex.More()) {
682 i++;
683 sprintf(p,"%d",i);
684 DBRep::Set(newname,ex.Current());
685 ex.Next();
686 }
687 return 0;
688}
689
690//=======================================================================
691// invert
692//=======================================================================
693
694static Standard_Integer invert(Draw_Interpretor& ,
695 Standard_Integer n, const char** a)
696{
697 if (n <= 1) return 1;
698 TopoDS_Shape S = DBRep::Get(a[1]);
699 if (S.IsNull()) return 0;
700
701 BRep_Builder B;
702 TopoDS_Shape NS = S.EmptyCopied();
703 TopoDS_Iterator itr(S);
704
705 while (itr.More()) {
706 B.Add(NS,itr.Value().Reversed());
707 itr.Next();
708 }
709 DBRep::Set(a[1],NS);
710
711 return 0;
712}
713
714//=======================================================================
715// orientation, reverse, complement
716//=======================================================================
717
718static Standard_Integer orientation(Draw_Interpretor& ,
719 Standard_Integer n, const char** a)
720{
721 if (n <= 1) return 1;
722 Standard_Integer cas = 0;
723#ifdef DEB
724 TopAbs_Orientation ori;
725#else
726 TopAbs_Orientation ori=TopAbs_FORWARD;
727#endif
728 Standard_Integer last = n;
729 if (!strcasecmp(a[0],"orientation")) {
730 if (n <= 2) return 1;
731 last--;
732 switch (*a[n-1]) {
733
734 case 'F' :
735 ori = TopAbs_FORWARD;
736 break;
737
738 case 'R' :
739 ori = TopAbs_REVERSED;
740 break;
741
742 case 'I' :
743 ori = TopAbs_INTERNAL;
744 break;
745
746 case 'E' :
747 ori = TopAbs_EXTERNAL;
748 break;
749 }
750 }
751
752 else if (!strcasecmp(a[0],"treverse")) {
753 cas = -1;
754 }
755
756 else if (!strcasecmp(a[0],"complement")) {
757 cas = -2;
758 }
759
760 for (Standard_Integer i = 1; i < last; i++) {
761 TopoDS_Shape S = DBRep::Get(a[i]);
762 if (!S.IsNull()) {
763 if (cas == -2)
764 S.Complement();
765 else if (cas == -1)
766 S.Reverse();
767 else
768 S.Orientation(ori);
769 DBRep::Set(a[i],S);
770 }
771 }
772 return 0;
773}
774
775#include <TCollection_AsciiString.hxx>
776
777//=======================================================================
0d969553 778// numshapes same as nbshapes but the output is cout
7fd59977 779//=======================================================================
780
781static Standard_Integer numshapes(Draw_Interpretor& di,
782 Standard_Integer n, const char** a)
783{
784 if (n < 2) return 1;
785
786 Standard_Integer i;
787 TopExp_Explorer ex;
788 for (i = 1; i < n; i++) {
789 TopoDS_Shape S = DBRep::Get(a[i]);
790 if (!S.IsNull()) {
791 BRepTools_ShapeSet BS;
792 BS.Add(S);
793 di <<"Number of shapes in "<<a[i]<<"\n";
794 TCollection_AsciiString Astr;
795 BS.DumpExtent(Astr);
796 di <<Astr.ToCString();
797 di << "\n" ;
798 }
799 }
800
801 return 0;
802}
803
804//=======================================================================
805// nbshapes
806//=======================================================================
807
808static Standard_Integer nbshapes(Draw_Interpretor& di,
809 Standard_Integer n, const char** a)
810{
811 if (n < 2) return 1;
812
813 Standard_Integer i;
814 TopExp_Explorer ex;
815 for (i = 1; i < n; i++) {
816 TopoDS_Shape S = DBRep::Get(a[i]);
817 if (!S.IsNull()) {
818 BRepTools_ShapeSet BS;
819 BS.Add(S);
820 di<<"Number of shapes in "<<a[i]<<"\n";
821 TCollection_AsciiString Astr;
822 BS.DumpExtent(Astr);
823 di<<Astr.ToCString();
824 }
825 }
826
827 return 0;
828}
829
830//=======================================================================
831//
832//=======================================================================
833
834static Standard_Integer countshapes(Draw_Interpretor& di,
835 Standard_Integer n, const char** a)
836{
837 if (n < 2) return 1;
838
839 Standard_Integer i;
840 TopExp_Explorer ex;
841 for (i = 1; i < n; i++) {
842 TopoDS_Shape Sh = DBRep::Get(a[i]);
843 Standard_Integer nbElem = 0;
844 if (!Sh.IsNull()) {
845 di <<"Number of shapes in "<<a[i]<<"\n";
846 TopTools_MapOfShape M;
847
848 for (ex.Init (Sh,TopAbs_VERTEX); ex.More(); ex.Next()) {
849 const TopoDS_Shape& S = ex.Current();
850 Standard_Boolean added = M.Add(S);
851 if (added) {
852 nbElem++;
853 }
854 }
855 di << " VERTEX : " << nbElem << "\n";
856 nbElem = 0;
857
858 for (ex.Init (Sh,TopAbs_EDGE); ex.More(); ex.Next()) {
859 const TopoDS_Shape& S = ex.Current();
860 Standard_Boolean added = M.Add(S);
861 if (added) {
862 nbElem++;
863 }
864 }
865 di << " EDGE : " << nbElem << "\n";
866 nbElem = 0;
867
868 for (ex.Init (Sh,TopAbs_WIRE); ex.More(); ex.Next()) {
869 const TopoDS_Shape& S = ex.Current();
870 Standard_Boolean added = M.Add(S);
871 if (added) {
872 nbElem++;
873 }
874 }
875 di << " WIRE : " << nbElem << "\n";
876 nbElem = 0;
877
878 for (ex.Init (Sh,TopAbs_FACE); ex.More(); ex.Next()) {
879 const TopoDS_Shape& S = ex.Current();
880 Standard_Boolean added = M.Add(S);
881 if (added) {
882 nbElem++;
883 }
884 }
885 di << " FACE : " << nbElem << "\n";
886 nbElem = 0;
887
888 for (ex.Init (Sh,TopAbs_SHELL); ex.More(); ex.Next()) {
889 const TopoDS_Shape& S = ex.Current();
890 Standard_Boolean added = M.Add(S);
891 if (added) {
892 nbElem++;
893 }
894 }
895 di << " SHELL : " << nbElem << "\n";
896 nbElem = 0;
897
898 for (ex.Init (Sh,TopAbs_SOLID); ex.More(); ex.Next()) {
899 const TopoDS_Shape& S = ex.Current();
900 Standard_Boolean added = M.Add(S);
901 if (added) {
902 nbElem++;
903 }
904 }
905 di << " SOLID : " << nbElem << "\n";
906 nbElem = 0;
907
908 for (ex.Init (Sh,TopAbs_COMPSOLID); ex.More(); ex.Next()) {
909 const TopoDS_Shape& S = ex.Current();
910 Standard_Boolean added = M.Add(S);
911 if (added) {
912 nbElem++;
913 }
914 }
915 di << " COMPSOLID : " << nbElem << "\n";
916 nbElem = 0;
917
918 for (ex.Init (Sh,TopAbs_COMPOUND); ex.More(); ex.Next()) {
919 const TopoDS_Shape& S = ex.Current();
920 Standard_Boolean added = M.Add(S);
921 if (added) {
922 nbElem++;
923 }
924 }
925 di << " COMPOUND : " << nbElem << "\n";
926 nbElem = 0;
927
928 di << " SHAPE : " << M.Extent() << "\n";
929 di << "\n" ;
930 }
931}
932
933 return 0;
934}
935
936//=======================================================================
937//memory management
938//=======================================================================
939static Standard_Integer purgemmgt(Draw_Interpretor&, Standard_Integer , const char**) {
940 Standard::Purge();
941 return 0;
942}
943//=======================================================================
944
945//=======================================================================
946// check
947//=======================================================================
948
949static Standard_Integer check(Draw_Interpretor& ,
950 Standard_Integer n, const char** a)
951{
952 if (n < 2) return 1;
953
954 Standard_Integer i;
955 TopExp_Explorer ex;
956 for (i = 1; i < n; i++) {
957 TopoDS_Shape S = DBRep::Get(a[i]);
958 TopoDS_Shape C;
959 if (S.IsNull()) continue;
960 for (ex.Init(S,TopAbs_FACE);ex.More();ex.Next()) {
961 C = ex.Current();
962 C.Checked(Standard_False);
963 BRepTools::Update(C);
964 }
965 }
966
967 return 0;
968}
969
970//=======================================================================
971// normals
972//=======================================================================
973
974static Standard_Integer normals(Draw_Interpretor& di,
975 Standard_Integer n, const char** a)
976{
977 if (n <= 1) return 1;
978 Standard_Real l = 1.;
979 if (n > 2)
980 l = atof(a[2]);
981
982 TopoDS_Shape S = DBRep::Get(a[1]);
983 if (S.IsNull()) return 1;
984
985 DBRep_WriteColorOrientation();
986
987 gp_Pnt P1,P2;
988 gp_Vec V,V1,V2;
989 Draw_Color col;
990
991 TopExp_Explorer ex(S,TopAbs_FACE);
992 while (ex.More()) {
993
994 const TopoDS_Face& F = TopoDS::Face(ex.Current());
995
996 // find the center of the minmax
997 BRepAdaptor_Surface SF(F);
998
999 Standard_Real u, v, x;
1000
1001 u = SF.FirstUParameter();
1002 x = SF.LastUParameter();
1003 if (Precision::IsInfinite(u))
1004 u = (Precision::IsInfinite(x)) ? 0. : x;
1005 else if (!Precision::IsInfinite(x))
1006 u = (u+x) / 2.;
1007
1008 v = SF.FirstVParameter();
1009 x = SF.LastVParameter();
1010 if (Precision::IsInfinite(v))
1011 v = (Precision::IsInfinite(x)) ? 0. : x;
1012 else if (!Precision::IsInfinite(x))
1013 v = (v+x) / 2.;
1014
1015 SF.D1(u,v,P1,V1,V2);
1016 V = V1.Crossed(V2);
1017 x = V.Magnitude();
1018 if (x > 1.e-10)
1019 V.Multiply(l/x);
1020 else {
1021 V.SetCoord(l/2.,0,0);
1022 di << "Null normal"<< "\n";
1023 }
1024
1025 P2 = P1;
1026 P2.Translate(V);
1027
1028 col = DBRep_ColorOrientation(F.Orientation());
1029
1030 Handle(Draw_Segment3D) seg = new Draw_Segment3D(P1,P2,col);
1031 dout << seg;
1032
1033
1034 ex.Next();
1035 }
1036 return 0;
1037}
1038
1039
1040//=======================================================================
1041//function : Set
1042//purpose :
1043//=======================================================================
1044void DBRep::Set(const Standard_CString Name, const TopoDS_Shape& S)
1045{
1046 Handle(DBRep_DrawableShape) D =
1047 new DBRep_DrawableShape(S,
1048 Draw_vert,
1049 Draw_jaune,
1050 Draw_rouge,
1051 Draw_bleu,
1052 size,
1053 nbIsos,
1054 discret);
1055 D->DisplayTriangulation(disptriangles);
1056 D->DisplayPolygons(disppolygons);
1057 D->DisplayHLR(withHLR,withRg1,withRgN,withHid,anglHLR);
1058 Draw::Set(Name,D);
1059}
1060//=======================================================================
1061//function : Get
1062//purpose :
1063//=======================================================================
1064TopoDS_Shape DBRep::Get(Standard_CString& name,
1065 const TopAbs_ShapeEnum typ,
1066 const Standard_Boolean complain)
1067{
1068 Standard_Boolean pick = name[0] == '.';
1069 TopoDS_Shape S;
1070 Handle(DBRep_DrawableShape) D;
1071 Handle(Draw_Drawable3D) DD = Draw::Get(name,complain);
1072 if (!DD.IsNull())
1073 D = Handle(DBRep_DrawableShape)::DownCast(DD);
1074 if (!D.IsNull()) {
1075 S = D->Shape();
1076 if (typ != TopAbs_SHAPE) {
1077 if (typ != S.ShapeType()) {
1078 // try to find prom pick
1079 if (pick) {
1080 Standard_Real u,v;
1081 DBRep_DrawableShape::LastPick(S,u,v);
1082 }
1083 }
1084 if (typ != S.ShapeType()) {
1085 if (complain) {
1086 cout << name << " is not a ";
1087 TopAbs::Print(typ,cout);
1088 cout << " but a ";
1089 TopAbs::Print(S.ShapeType(),cout);
1090 cout << endl;
1091 }
1092 S = TopoDS_Shape();
1093 }
1094 }
1095 }
1096 return S;
1097}
1098
1099static Standard_Integer XProgress (Draw_Interpretor& di, Standard_Integer argc, const char **argv)
1100{
1101 for ( Standard_Integer i=1; i < argc; i++ ) {
1102 Standard_Boolean turn = Standard_True;
1103 if ( argv[i][0] == '-' ) turn = Standard_False;
1104 else if ( argv[i][0] != '+' ) continue;
1105 if ( argv[i][1] == 't' ) Draw_ProgressIndicator::DefaultTextMode() = turn;
1106 else if ( argv[i][1] == 'g' ) Draw_ProgressIndicator::DefaultGraphMode() = turn;
1107 else if ( ! strcmp ( argv[i], "-stop" ) && i+1 < argc ) {
1108 Draw_ProgressIndicator::StopIndicator() = atol(argv[++i]);
1109 return 0;
1110 }
1111 }
1112 di << "Progress Indicator defaults: text mode is ";
1113 if ( Draw_ProgressIndicator::DefaultTextMode() ) {
1114 di<<"ON";
1115 } else {
1116 di<<"OFF";
1117 }
1118 di<<", graphical mode is ";
1119 if ( Draw_ProgressIndicator::DefaultGraphMode() ) {
1120 di<<"ON";
1121 } else {
1122 di<<"OFF";
1123 }
1124 di<< "\n";
1125 return 0;
1126}
1127
1128//=======================================================================
1129//function : BasicCommands
1130//purpose :
1131//=======================================================================
1132
1133static Standard_Boolean done = Standard_False;
1134void DBRep::BasicCommands(Draw_Interpretor& theCommands)
1135{
1136 if (done) return;
1137 done = Standard_True;
1138 Draw::Commands(theCommands);
1139
1140 const char* g = "Basic shape commands";
1141
1142 theCommands.Add("isos","isos [name1 ...] [nbisos]",__FILE__,isos,g);
1143 theCommands.Add("hlr" ,"[no]hlr, rg1, rgn, hid, ang",__FILE__,hlr ,g);
1144 theCommands.Add("vori","vori [name1 ...], edges are colored by orientation (see vconn)",__FILE__,dispor,g);
1145 theCommands.Add("triangles", "triangles [name1]..., display triangles of shapes if exists",__FILE__, triangles, g);
1146 theCommands.Add("tclean", "tclean [name1]..., erase triangulations and polygons on triangulations from shapes",__FILE__, tclean, g);
1147 theCommands.Add("polygons", "polygons [name1]..., display polygons of shapes if exists",__FILE__, polygons, g);
1148 theCommands.Add("vconn","vconn [name1 ...] , edges are colored by number of faces (see vori)",__FILE__,dispor,g);
1149 theCommands.Add("discretisation","discretisation [nbpoints]",__FILE__,discretisation,g);
1150 theCommands.Add("compound","compound [name1 name2 ..] compound",__FILE__,compound,g);
1151 theCommands.Add("add","add name1 name2",__FILE__,add,g);
1152 theCommands.Add("explode","explode name [Cd/C/So/Sh/F/W/E/V]",__FILE__,explode,g);
1153 theCommands.Add("nexplode","stable numbered explode for edge and face: nexplode name [F/E]",__FILE__,nexplode,g);
1154 theCommands.Add("exwire","exwire wirename",__FILE__,exwire,g);
1155 theCommands.Add("emptycopy","emptycopy [copyshape] originalshape",__FILE__,emptycopy,g);
1156 theCommands.Add("check","check shape1 shape2 ...",__FILE__,check,g);
1157
1158 theCommands.Add("orientation","orientation name1 name2.. F/R/E/I",__FILE__,orientation,g);
1159 theCommands.Add("treverse","treverse name1 name2 ...",__FILE__,orientation,g);
1160 theCommands.Add("complement","complement name1 name2 ...",__FILE__,orientation,g);
1161 theCommands.Add("invert","invert name, reverse subshapes",__FILE__,invert,g);
1162 theCommands.Add("normals","normals s (length = 10), disp normals",__FILE__,normals,g);
1163 theCommands.Add("nbshapes","nbshapes s; size of shape",__FILE__,nbshapes,g);
1164 theCommands.Add("numshapes","numshapes s; size of shape",__FILE__,numshapes,g);
1165 theCommands.Add("countshapes","countshapes s; count of shape",__FILE__,countshapes,g);
1166
1167// theCommands.Add("dumpmmgt",
1168// "dump le contenu du gestionnaire de memoire",__FILE__,dumpmmgt,g);
1169 theCommands.Add("purgemmgt",
0d969553 1170 "returns the free memory from the system to the memory manager",
7fd59977 1171 __FILE__,purgemmgt,g);
1172
1173 // Add command for DRAW-specific ProgressIndicator
1174 theCommands.Add ( "XProgress","XProgress [+|-t] [+|-g]: switch on/off textual and graphical mode of Progress Indicator",XProgress,"DE: General");
1175}
1176
1177//=======================================================================
1178//function : HLRMode
1179//purpose :
1180//=======================================================================
1181
1182Standard_Boolean DBRep::HLRMode()
1183{ return withHLR; }
1184
1185//=======================================================================
1186//function : Rg1Mode
1187//purpose :
1188//=======================================================================
1189
1190Standard_Boolean DBRep::Rg1Mode()
1191{ return withRg1; }
1192
1193//=======================================================================
1194//function : RgNMode
1195//purpose :
1196//=======================================================================
1197
1198Standard_Boolean DBRep::RgNMode()
1199{ return withRgN; }
1200
1201//=======================================================================
1202//function : HidMode
1203//purpose :
1204//=======================================================================
1205
1206Standard_Boolean DBRep::HidMode()
1207{ return withHid; }
1208
1209//=======================================================================
1210//function : HLRAngle
1211//purpose :
1212//=======================================================================
1213
1214Standard_Real DBRep::HLRAngle()
1215{ return anglHLR; }
1216
1217//=======================================================================
1218//function :
1219//purpose : save and restore shapes
1220//=======================================================================
1221
1222static Standard_Boolean stest(const Handle(Draw_Drawable3D)& d)
1223{
1224 return d->IsInstance(STANDARD_TYPE(DBRep_DrawableShape));
1225}
1226
1227static void ssave(const Handle(Draw_Drawable3D)&d, ostream& OS)
1228{
1229 Handle(DBRep_DrawableShape)
1230 N = Handle(DBRep_DrawableShape)::DownCast(d);
1231 BRep_Builder B;
1232 BRepTools_ShapeSet S(B);
1233 if(!Draw::GetProgressBar().IsNull())
1234 S.SetProgress(Draw::GetProgressBar());
1235 S.Add(N->Shape());
1236 S.Write(OS);
1237 if(!Draw::GetProgressBar().IsNull() && Draw::GetProgressBar()->UserBreak())
1238 return;
1239 S.Write(N->Shape(),OS);
1240}
1241
1242static Handle(Draw_Drawable3D) srestore (istream& IS)
1243{
1244 BRep_Builder B;
1245 BRepTools_ShapeSet S(B);
1246 if(!Draw::GetProgressBar().IsNull())
1247 S.SetProgress(Draw::GetProgressBar());
1248 S.Read(IS);
1249 Handle(DBRep_DrawableShape) N;
1250 if(!Draw::GetProgressBar().IsNull() && Draw::GetProgressBar()->UserBreak())
1251 return N;
1252 TopoDS_Shape theShape;
1253 S.Read(theShape,IS );
1254 N = new DBRep_DrawableShape(theShape,
1255 Draw_vert,
1256 Draw_jaune,
1257 Draw_rouge,
1258 Draw_bleu,
1259 size,
1260 nbIsos,
1261 discret);
1262 N->DisplayTriangulation(disptriangles);
1263 N->DisplayPolygons(disppolygons);
1264 N->DisplayHLR(withHLR,withRg1,withRgN,withHid,anglHLR);
1265
1266 return N;
1267}
1268
1269
1270static Draw_SaveAndRestore ssr("DBRep_DrawableShape",
1271 stest,ssave,srestore);
1272
1273
1274void dumps (const TopoDS_Shape& S)
1275{
1276 BRepTools::Dump(S,cout);
1277}
1278
1279//=======================================================================
1280//function : NbIsos
1281//purpose :
1282//=======================================================================
1283
1284Standard_Integer DBRep::NbIsos()
1285{ return nbIsos; }
1286
1287
1288//=======================================================================
1289//function : Discretisation
1290//purpose :
1291//=======================================================================
1292
1293Standard_Integer DBRep::Discretisation()
1294{ return discret; }