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