0022898: IGES import fails in german environment
[occt.git] / src / SWDRAW / SWDRAW_ShapeFix.cxx
1 // Created on: 1999-03-09
2 // Created by: data exchange team
3 // Copyright (c) 1999-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
21 #include <SWDRAW_ShapeFix.ixx>
22
23 #include <Draw.hxx>
24 #include <DBRep.hxx>
25 #include <SWDRAW.hxx>
26 #include <gp_XYZ.hxx>
27 #include <gp_Pnt2d.hxx>
28 #include <TopoDS.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS_Wire.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TopoDS_Iterator.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <BRep_Tool.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRepBuilderAPI.hxx>
38 #include <BRepTopAdaptor_FClass2d.hxx>
39
40 #include <ShapeBuild_ReShape.hxx>
41 #include <ShapeAnalysis_Edge.hxx>
42 #include <ShapeAnalysis_WireOrder.hxx>
43 #include <ShapeAnalysis_WireVertex.hxx>
44 #include <ShapeAnalysis_Wire.hxx>
45 #include <ShapeExtend_WireData.hxx>
46 #include <ShapeFix.hxx>
47 #include <ShapeFix_ShapeTolerance.hxx>
48 #include <ShapeFix_Wire.hxx>
49 #include <ShapeFix_WireVertex.hxx>
50 #include <ShapeFix_Wireframe.hxx>
51 #include <ShapeFix_Face.hxx>
52 #include <ShapeFix_Shape.hxx>
53 #include <Precision.hxx>
54 #include <ShapeExtend_DataMapOfShapeListOfMsg.hxx>
55 #include <ShapeExtend_MsgRegistrator.hxx>
56 #include <ShapeExtend_DataMapIteratorOfDataMapOfShapeListOfMsg.hxx>
57 #include <Message_ListIteratorOfListOfMsg.hxx>
58 #include <Message_Msg.hxx>
59 #include <TCollection_AsciiString.hxx>
60 #include <TColStd_DataMapIteratorOfDataMapOfAsciiStringInteger.hxx>
61 #include <TColStd_DataMapOfAsciiStringInteger.hxx>
62 #include <TopTools_MapOfShape.hxx>
63 #include <TopTools_DataMapOfShapeListOfShape.hxx>
64 #include <TopAbs_State.hxx>
65
66 #include <Draw_ProgressIndicator.hxx>
67 #include <ShapeAnalysis_FreeBounds.hxx>
68 #include <TopTools_HSequenceOfShape.hxx>
69 #include <BRep_Builder.hxx>
70 #include <TopTools_IndexedMapOfShape.hxx>
71 #include <TopExp.hxx>
72
73 #ifdef AIX
74 #include <strings.h>
75 #endif
76 #include <stdio.h>
77 #include <ShapeExtend_WireData.hxx>
78
79 //=======================================================================
80 //function : edgesameparam
81 //purpose  : 
82 //=======================================================================
83
84 static Standard_Integer edgesameparam (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
85 {
86 //  const Standard_CString arg1 = argv[1];
87   const Standard_CString arg2 (argc > 2 ? argv[2] : NULL);
88 //        ****    Edge:SameParameter         ****
89   if (argc < 2) { di<<"shapename ,  option f to force, else only Edges not-SameParameter are computed"<<"\n"; return 1 /* Error */; }
90   TopoDS_Shape Shape = DBRep::Get(argv[1]);
91
92   if (!ShapeFix::SameParameter(Shape, (argc > 2 && arg2[0] == 'f') , BRepBuilderAPI::Precision()) )
93     di<<"Some edges were not processed"<<"\n";
94   di<<"\n";
95   return 0; // Done
96 }
97
98 //=======================================================================
99 //function : settolerance
100 //purpose  : 
101 //=======================================================================
102
103 static Standard_Integer settolerance (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
104 {
105   if (argc < 3) {
106     di<<    "myshape val   : forces tolerances at <val>"<<"\n"
107       <<      "myshape < max : sets maximum tolerance to <max>"<<"\n"
108         <<    "myshape > min : sets minimum tolerance to <min>"<<"\n"
109           <<  "myshape min max : bounds tolerances between <min> and <max>"<<"\n"
110             <<"myshape mode=v-e-f other args : idem but only on vertex-edge-face"<<"\n";
111     return (argc < 2 ? 0 : 1 /* Error */);
112   }
113   Standard_CString arg1 = argv[1];
114   Standard_CString arg2 = argv[2];
115   TopoDS_Shape Shape = DBRep::Get(arg1);
116   if (Shape.IsNull()) { di<<"Shape unknown : "<<arg1<<"\n"; return 1 /* Error */; }
117   char mod2 = arg2[0];
118   Standard_Integer premarg = 2;
119   TopAbs_ShapeEnum styp = TopAbs_SHAPE;
120   if (mod2 == 'v') { styp = TopAbs_VERTEX; premarg = 3; }
121   if (mod2 == 'e') { styp = TopAbs_EDGE;   premarg = 3; }
122   if (mod2 == 'w') { styp = TopAbs_WIRE;   premarg = 3; }
123   if (mod2 == 'f') { styp = TopAbs_FACE;   premarg = 3; }
124   if (mod2 == 'a') { styp = TopAbs_SHAPE;  premarg = 3; }
125
126   Standard_Real tmin,tmax;
127   mod2 = argv[premarg][0];
128   if (mod2 == '=') tmin = tmax = Draw::Atof (argv[argc-1]);
129   else if (mod2 == '<') { tmin = 0; tmax = Draw::Atof (argv[argc-1]); }
130   else if (mod2 == '>') { tmin = Draw::Atof (argv[argc-1]); tmax = 0; }
131   else { tmin = Draw::Atof (argv[premarg]);  tmax = Draw::Atof (argv[argc-1]); }
132
133   if (argc == premarg + 1 || tmin == tmax) di<<"Setting Tolerance to "<<tmin<<"\n";
134   else if (tmax < tmin) di<<"Minimum Tolerance to "<<tmin<<"\n";
135   else if (tmin <= 0)   di<<"Maximum Tolerance to "<<tmax<<"\n";
136   else                  di<<"Tolerance Limited between "<<tmin<<" and "<<tmax<<"\n";
137   ShapeFix_ShapeTolerance sat;
138   sat.LimitTolerance (Shape,tmin,tmax,styp);
139   return 0; // Done
140 }
141
142 //=======================================================================
143 //function : stwire
144 //purpose  : 
145 //=======================================================================
146
147 static Standard_Integer stwire (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
148 {
149   if (argc < 2) { // help
150     di<<"Donner nom shape depart + nom shape resultat + option"<<"\n";
151     di<<"Options de chargement :  x add connected  (sinon add simple)\n"
152       <<  "Options de traitement :  l fix little/BRepBuilderAPI\n"
153       <<"Options de sortie : aucune make simple\n"
154       <<"  m MakeAPI r avec reorder  v vertex\n"
155       <<"Autres (se cumulent) :  q quid(stats)"<<"\n";
156     return 0;
157   }
158   if (argc < 4) { di<<"stwire tout court pour help"<<"\n"; return 1 /* Error */; }
159   Standard_CString arg1 = argv[1];
160   Standard_CString arg2 = argv[2];
161
162 //  Options
163   Standard_Integer i;  int ox,ol,om,orint,oq,ov;  ox=ol=om=orint=oq=ov=0;
164   for (i = 3; i < argc; i ++) {
165     int valopt = 1;
166     char opt = argv[i][0];
167     if (opt == '+')   opt = argv[i][1];
168     if (opt == '-') { opt = argv[i][1]; valopt = 0; }
169     switch (opt) {
170     case 'l' : ol = valopt; break;
171     case 'm' : om = valopt; break;
172     case 'q' : oq = valopt; break;
173     case 'r' : orint = valopt; break;
174     case 'v' : ov = valopt; break;
175     case 'x' : ox = valopt; break;
176     default : break;
177     }
178   }
179   TopoDS_Shape Shape = DBRep::Get(arg1);
180   if (Shape.IsNull()) { di<<arg1<<" inconnu"<<"\n"; return 1 /* Error */; }
181
182 //  On y va
183   Handle(ShapeExtend_WireData) sbwd = new ShapeExtend_WireData;
184   Handle(ShapeAnalysis_Wire) saw = new ShapeAnalysis_Wire;
185   saw->Load ( sbwd );
186   
187   TopoDS_Shape awire;  // en principe un Wire
188   if (Shape.ShapeType() == TopAbs_WIRE) {
189     awire = Shape;
190   } else if (Shape.ShapeType() == TopAbs_FACE) {
191     saw->SetFace (TopoDS::Face(Shape));
192     TopExp_Explorer expw(Shape,TopAbs_WIRE);
193     if (expw.More()) awire = expw.Current();
194     saw->SetPrecision (BRepBuilderAPI::Precision());
195   }
196   if (awire.IsNull()) {
197     di<<"Neither FACE nor WIRE : "<<arg1<<"\n";
198     di<<"Considering as list of edges ..."<<"\n";
199     awire = Shape;
200 //    return 1 /* Error */;
201   }
202
203 // Chargement : normal ou par connected(oriented)
204 //  if (ox) {
205     for (TopExp_Explorer exp(Shape,TopAbs_EDGE); exp.More(); exp.Next()) {
206       TopoDS_Edge E = TopoDS::Edge (exp.Current());
207       Standard_Integer orient = saw->CheckShapeConnect (E);
208       di<<"Orientation : "<<orient<<" LowerDist : "<< saw->MinDistance3d() << "\n";
209       if (ox) sbwd->AddOriented (E,orient);
210       else sbwd->Add (E);
211     }
212 //  }
213 //  else sbwd->Init (awire);
214   
215   Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
216   sfw->Init ( saw );
217
218 //   Traitement en cours
219   if (ol) {
220     Standard_Integer nb = sfw->NbEdges();
221     for (i = 1; i <= nb; i ++) {
222       Standard_Boolean stat = sfw->FixSmall (i, Standard_True, 0.0);
223       //cout<<"FixSmall for"<<i<<(stat ? " done" : " not done"); //:sw <<" StatusFix="<<STW.StatusFix()<<"\n";
224       di<<"FixSmall for"<<i;
225       if (!stat) {
226         di<<" not done";
227       } else {
228         di<<" done";
229       }
230       if ( sfw->LastFixStatus ( ShapeExtend_FAIL ) ) di << " (failed)";
231       di << "\n";
232     }
233   }
234
235 //  Traitement
236   if (orint) {  // reorder ?
237     ShapeAnalysis_WireOrder WO ( (Shape.ShapeType() != TopAbs_FACE), BRepBuilderAPI::Precision());
238
239     Standard_Integer stwo = saw->CheckOrder (WO);
240     Standard_Integer nb = WO.NbEdges();
241     di<<"Reorder status : "<<stwo<<"  NbEdges="<<nb<<"\n";
242     for (i = 1; i <= nb; i ++) {
243       Standard_Integer iord = WO.Ordered(i);
244       di<<"Edge n0 "<<i;
245       if ( sbwd->Edge(iord).Orientation() == TopAbs_REVERSED) di<<" REV";
246       else di<<" FWD";
247       di<<" ordered to "<<iord<<" Gap="<<WO.Gap(i)<<"\n";
248     }
249     di<<"Reorder not yet done"<<"\n";
250     sfw->FixReorder (WO);
251 //   Mais on n execute pas
252   }
253
254 //  Statistiques
255   if (oq) {
256     ShapeAnalysis_Edge sae;
257     Standard_Integer nb = sbwd->NbEdges();
258     di<<"NbEdges : "<<nb<<"\n";
259     for (i = 1; i <= nb; i ++) {
260       TopoDS_Edge E = sbwd->Edge(i);
261       di<<"Edge "<<i;
262       if (E.Orientation() == TopAbs_REVERSED) di<<" REV";
263       else di<<" FWD";
264       if (BRep_Tool::Degenerated(E)) di<<" DGNR";
265       if (sbwd->IsSeam(i))        di<<" SEAM_WIRE";
266       if (Shape.ShapeType() == TopAbs_FACE &&
267           sae.IsSeam(E,TopoDS::Face(Shape)))  di<<" SEAM_FACE";
268       if (Shape.ShapeType() == TopAbs_FACE )
269         if (sae.HasPCurve(E,TopoDS::Face(Shape)))     di<<" PCU";
270         else di<<" NO_PCU";
271       if (sae.HasCurve3d(E))    di<<" C3D";
272       else di<<" NO_C3D";
273       if (sae.IsClosed3d(E))  di<<" CLOSED";
274       di<<"\n";
275     }
276   }
277
278 //  Resultat
279   TopoDS_Wire result;
280   if (ov) {
281     ShapeAnalysis_WireVertex sawv;
282     sawv.Init ( sbwd, saw->Precision() );
283     sawv.Analyze();
284     Standard_Integer nb = sbwd->NbEdges();
285     di<<"Nb(End)Vertex : "<<nb<<"\n";
286     for (i = 1; i <= nb; i ++) {
287       gp_XYZ pos; Standard_Real upre,ufol;
288       Standard_Integer stat = sawv.Data (i,pos,upre,ufol);
289       di<<i<<" : ";
290       switch (stat) {
291       case 0 : di<<"Same Vertex"<<"\n"; break;
292       case 1 : di<<"Same Coords with recorded precisions (but not Same Vertex)"<<"\n"; break;
293       case 2 : di<<"Close (with preci="<< saw->Precision()<<")"<<"\n"; break;
294       case 3 : di<<"End of "<<i<<" OK, Start of "<<(i == nb ? 1 : i+1)<<" at U="<<ufol; break;
295       case 4 : di<<"End of "<<i<<" at U="<<upre<<", Start of "<<(i == nb ? 1 : i+1)<<" OK"; break;
296       case 5 : di<<"Intersection, End of "<<i<<" at U="<<upre<<", Start of "<<(i == nb ? 1 : i+1)<<" at U="<<ufol; break;
297         default : di<<"Disjoined"<<"\n";
298       }
299       if (stat >= 3 && stat <= 5) di<<"\n   - Position : "<<pos.X()<<"  "<<pos.Y()<<"  "<<pos.Z()<<"\n";
300     }
301     ShapeFix_WireVertex sfwv;
302     sfwv.Init ( sawv );
303     di<<"Nb Fixed Vertex : "<< sfwv.Fix() <<"\n";
304   }
305   /*
306   if (oc) {
307     if ( Shape.ShapeType() == TopAbs_FACE ) {
308       Correct_Wire CW ( TopoDS::Face(Shape) );
309       Standard_Integer i, nb = sbwd->NbEdges();
310       Standard_Integer num = 1;
311       for (i = 1; i <= nb; i ++)  CW.Add (sbwd->Edge(i));
312       CW.Perform ( saw->Precision() );
313       nb = CW.NbWires();
314       if (nb != 1) {
315         //  On prend celui qui a le plus d edges
316         Standard_Integer nbe, maxe = 0;
317         for (i = 1; i <= nb; i ++) {
318           TopoDS_Wire wir = CW.Wire(i);
319           nbe = 0;
320           for (TopoDS_Iterator ite(wir); ite.More(); ite.Next()) nbe ++;
321           if (nbe > maxe) { num = i; maxe = nbe; }
322         }
323         di<<"Correct_Wire produced "<<nb<<" Wires, taken n0 "<<num<<"\n";
324       }
325       result = CW.Wire (num);
326     }
327     else di << "Cannot apply Correct_Wire: face not defined" << "\n";
328   }
329   */
330   else if (om) result = sbwd->WireAPIMake();
331   else         result = sbwd->Wire();
332   if (result.IsNull()) {
333     di<<"Pas de resultat, desole"<<"\n";
334     return 1;  // Fail
335   }
336   DBRep::Set (arg2,result);
337   return 0; // Done
338 }
339
340 //=======================================================================
341 //function : reface
342 //purpose  : 
343 //=======================================================================
344
345 static Standard_Integer reface (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
346 {
347   if (argc < 3) { di<<"Donner un nom de SHAPE (SHELL ou FACE) + un nom de RESULTAT"<<"\n"; return 1 /* Error */; }
348   Standard_CString arg1 = argv[1];
349   Standard_CString arg2 = argv[2];
350   TopoDS_Shape Shape = DBRep::Get(arg1);
351   if (Shape.IsNull()) { di<<"Shape unknown : "<<arg1<<"\n"; return 1 /* Error */; }
352
353   Standard_Boolean rebuild = Standard_False;
354
355   Handle(ShapeFix_Face) STF = new ShapeFix_Face;
356 //  Options ?
357   Standard_Integer i; // svv Jan11 2000 : porting on DEC
358   for (i = 3; i < argc; i ++) {
359     Standard_Boolean valopt = Standard_True;
360     char opt = argv[i][0];
361     if (opt == '+')   opt = argv[i][1];
362     if (opt == '-') { opt = argv[i][1]; valopt = Standard_False; }
363     //cout<<(valopt ? ".." : ".. NO");
364     if (!valopt) {
365       di<<".. NO";
366     } else {
367       di<<"..";
368     }
369
370     if (opt == 'R') { di<<" REBUILD-ANYWAY .."; rebuild = valopt; }
371     if (opt == 'd') { di<<" fix-dgnr .."; STF->FixWireTool()->FixDegeneratedMode() = valopt; }
372     if (opt == 'r') { di<<" fix-reorder-wire .."; STF->FixWireTool()->FixReorderMode()    = valopt; }
373     if (opt == 'k') {
374     }
375   }
376
377   TopoDS_Face  face;
378   ShapeBuild_ReShape resh;
379
380   Standard_Integer nbf = 0, nbfc = 0;
381   for (TopExp_Explorer EF (Shape,TopAbs_FACE); EF.More(); EF.Next()) {
382     TopoDS_Face F = TopoDS::Face (EF.Current());    face = F;
383     nbf ++;
384     Standard_Boolean newface = Standard_False;
385 //    on va voir si ShapeTool_Face trouve qqchose a redire
386 //:sw    ShapeTool_Wire STW;
387 //:sw    STW.SetFace (F);
388
389     STF->Init (F);  // qui fait tout
390     STF->Perform();
391     face = STF->Face();
392     newface = STF->Status(ShapeExtend_DONE) || rebuild;
393     if (newface) { nbfc ++; resh.Replace (F,face); }
394   }
395   if (nbfc > 0) {
396     di<<"Faces reprises par ShapeFix_Face : "<<nbfc<<"\n";
397     DBRep::Set (arg2,resh.Apply (Shape,TopAbs_FACE,2));
398     return 0; // Done
399   }
400   else di<<"ShapeFix_Face n a rien trouve a redire"<<"\n";
401   return 0;
402 }
403
404
405 //=======================================================================
406 //function : fixshape
407 //purpose  : 
408 //=======================================================================
409
410 static Standard_Integer fixshape (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
411 {
412   Handle(ShapeExtend_MsgRegistrator) msg = new ShapeExtend_MsgRegistrator;
413   Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
414   sfs->SetMsgRegistrator ( msg );
415   
416   Standard_CString res = 0;
417   Standard_Integer par = 0, mess=0;
418   for ( Standard_Integer i=1; i < argc; i++ ) {
419     if ( argv[i][0] == '-' || argv[i][0] == '+' || argv[i][0] == '*' ) {
420       Standard_Integer val = ( argv[i][0] == '-' ? 0 : argv[i][0] == '+' ? 1 : -1 );
421       switch ( argv[i][1] ) {
422       case 'l': sfs->FixWireTool()->FixLackingMode()          = val; break;
423       case 'o': sfs->FixFaceTool()->FixOrientationMode()      = val; break;
424       case 'h': sfs->FixWireTool()->FixShiftedMode()          = val; break;
425       case 'm': sfs->FixFaceTool()->FixMissingSeamMode()      = val; break;
426       case 'd': sfs->FixWireTool()->FixDegeneratedMode()      = val; break;
427       case 's': sfs->FixWireTool()->FixSmallMode()            = val; break;
428       case 'i': sfs->FixWireTool()->FixSelfIntersectionMode() = val; break;
429       case 'n': sfs->FixWireTool()->FixNotchedEdgesMode()     = val; break;
430       case '?': mess                                          = val; break;
431       }
432       continue;
433     }
434     else {
435       switch ( par ) {
436       case 0: res = argv[i];                       break;
437       case 1: {
438         TopoDS_Shape initShape =  DBRep::Get(argv[i]);
439         if(initShape.IsNull()) continue;
440         sfs->Init ( initShape );
441       } break;
442       case 2: sfs->SetPrecision   (Draw::Atof(argv[i])); break;
443       case 3: sfs->SetMaxTolerance(Draw::Atof(argv[i])); break;
444       }
445     }
446     par++;
447   }
448
449   if ( par <2 ) {
450     di << "Use: " << argv[0] << " result shape [tolerance [max_tolerance]] [switches]" << "\n"; 
451     di << "Switches allow to tune parameters of ShapeFix" << "\n"; 
452     di << "The following syntax is used: <symbol><parameter>" << "\n"; 
453     di << "- symbol may be - to set parameter off, + to set on or * to set default" << "\n"; 
454     di << "- parameters are identified by letters:" << "\n"; 
455     di << "  l - FixLackingMode" << "\n"; 
456     di << "  o - FixOrientationMode" << "\n"; 
457     di << "  h - FixShiftedMode" << "\n"; 
458     di << "  m - FixMissingSeamMode" << "\n"; 
459     di << "  d - FixDegeneratedMode" << "\n"; 
460     di << "  s - FixSmallMode" << "\n"; 
461     di << "  i - FixSelfIntersectionMode" << "\n"; 
462     di << "  n - FixNotchedEdgesMode" << "\n"; 
463     di << "For enhanced message output, use switch '+?'" << "\n"; 
464     return 1;
465   }
466
467   Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di, 1);
468   sfs->Perform (aProgress);
469   DBRep::Set (res,sfs->Shape());
470
471   if ( mess ) 
472   {
473     TColStd_DataMapOfAsciiStringInteger aMapOfNumberOfFixes;
474     Standard_SStream aSStream;
475     TopoDS_Compound aCompound;
476     BRep_Builder aBuilder;
477     aBuilder.MakeCompound (aCompound);
478     const ShapeExtend_DataMapOfShapeListOfMsg &map = msg->MapShape();
479     // Counting the number of each type of fixes. If the switch '*?' store all modified shapes in compound.
480     for ( ShapeExtend_DataMapIteratorOfDataMapOfShapeListOfMsg it ( map ); it.More(); it.Next() )
481     {
482       for ( Message_ListIteratorOfListOfMsg iter ( it.Value() ); iter.More(); iter.Next() )
483       {
484         if ( aMapOfNumberOfFixes.IsBound ( iter.Value().Value() ) )
485         {
486           aMapOfNumberOfFixes ( iter.Value().Value() )++;
487         }
488         else
489         {
490           aMapOfNumberOfFixes.Bind ( iter.Value().Value(), 1 );
491         }
492       }
493       if ( mess < 0 )
494       {
495         aBuilder.Add ( aCompound, it.Key() );
496       }
497     }
498     
499     aSStream << " Fix" << setw (58) << "Count\n";
500     aSStream << " ------------------------------------------------------------\n";
501     for ( TColStd_DataMapIteratorOfDataMapOfAsciiStringInteger anIter ( aMapOfNumberOfFixes ); anIter.More(); anIter.Next() )
502     {
503       aSStream << " " << anIter.Key() << setw ( 60 - anIter.Key().Length() ) << anIter.Value() << "\n";
504     }
505     aSStream << " ------------------------------------------------------------\n";
506     di << aSStream;
507
508     if ( mess < 0 )
509     {
510       char buff[256];
511       Sprintf ( buff, "%s_%s", res, "m" );
512       di << " Modified shapes saved in compound: " << buff;
513       DBRep::Set (buff, aCompound);
514     }
515   }
516   
517   return 0; // Done
518 }
519
520 //=======================================================================
521 //function : fixgaps
522 //purpose  : 
523 //=======================================================================
524
525 Standard_Integer fixgaps(Draw_Interpretor& di, Standard_Integer n, const char** a)
526 {
527   if (n < 3) return 1;
528   
529   TopoDS_Shape S = DBRep::Get(a[2]);
530   if (S.IsNull()) {
531     di << " Shape is null" << "\n";
532     return 1;
533   }
534
535   Handle(ShapeFix_Wireframe) SFWF = new ShapeFix_Wireframe(S);
536   Standard_Real prec = ( n >3 ? Draw::Atof(a[3]) : 0. );
537   SFWF->SetPrecision(prec);
538   if ( SFWF->FixWireGaps() ) {
539     DBRep::Set(a[1],SFWF->Shape());
540     di<<" Wireframe gaps fixed on shape"<<"\n";
541   }
542
543   return 0;
544 }
545
546 //=======================================================================
547 //function : fixsmall
548 //purpose  : 
549 //=======================================================================
550
551 Standard_Integer fixsmall(Draw_Interpretor& di, Standard_Integer n, const char** a)
552 {
553   if (n < 3) return 1;
554   
555   TopoDS_Shape S = DBRep::Get(a[2]);
556   if (S.IsNull()) {
557     di << " Shape is null" << "\n";
558     return 1;
559   }
560
561   Standard_Real prec = (n == 4)? Draw::Atof(a[3]) : 1.;
562   ShapeFix_Wireframe SFWF(S);
563   SFWF.SetPrecision(prec);
564   
565   if (SFWF.FixSmallEdges()) {
566     DBRep::Set(a[1],SFWF.Shape());
567     di<<" Small edges fixed on shape"<<"\n";
568   }
569
570   return 0;
571 }
572
573 //=======================================================================
574 //function : fixsmalledges
575 //purpose  : 
576 //=======================================================================
577
578 static Standard_Integer fixsmalledges(Draw_Interpretor& di, Standard_Integer n, const char** a)
579 {
580   if( n < 3) {
581     di<<"Invalid number of arguments"<<"\n";
582     return 1;
583   }
584   TopoDS_Shape Sh = DBRep::Get(a[2]);
585  
586   Standard_Integer k = 3;
587   Standard_Real tol = 100000;
588   Standard_Integer mode = 2;
589   Standard_Real tolang = M_PI/2;
590   if(n > k) 
591     tol = Draw::Atof(a[k++]);
592   
593   if(n > k) 
594     mode=  Draw::Atoi(a[k++]);
595    
596   if(n > k) 
597     tolang = Draw::Atof(a[k++]);
598      
599   Handle(ShapeFix_Wireframe) aSfwr = new ShapeFix_Wireframe();
600   Handle(ShapeBuild_ReShape) aReShape = new ShapeBuild_ReShape;
601   aSfwr->SetContext(aReShape);
602   aSfwr->Load(Sh);
603   aSfwr->SetPrecision(tol);
604   Standard_Boolean aModeDrop = Standard_True;
605   if(mode == 2) 
606     aModeDrop = Standard_False;
607    
608   TopTools_MapOfShape theSmallEdges, theMultyEdges;
609   TopTools_DataMapOfShapeListOfShape theEdgeToFaces,theFaceWithSmall;
610   aSfwr->CheckSmallEdges(theSmallEdges,theEdgeToFaces,theFaceWithSmall, theMultyEdges);
611   aSfwr->MergeSmallEdges (theSmallEdges,theEdgeToFaces,theFaceWithSmall, theMultyEdges, aModeDrop,tolang);
612    //aSfwr->FixSmallEdges(); 
613   TopoDS_Shape resShape =  aSfwr->Shape();;
614   DBRep::Set ( a[1], resShape );
615   return 0;
616 }
617
618 //=======================================================================
619 //function : checkoverlapedges
620 //purpose  : 
621 //=======================================================================
622
623 static Standard_Integer checkoverlapedges(Draw_Interpretor& di, Standard_Integer n, const char** a)
624 {
625    if( n < 3) {
626      di<<"Invalid number of arguments"<<"\n";
627      return 1;
628    }
629    TopoDS_Shape Sh1 = DBRep::Get(a[1]);
630    TopoDS_Shape Sh2 = DBRep::Get(a[2]);
631    if(Sh1.IsNull() || Sh2.IsNull()) {
632      di<<"Invalid arguments"<<"\n";
633      return 1;
634    }
635    TopoDS_Edge e1 = TopoDS::Edge(Sh1);
636    TopoDS_Edge e2 = TopoDS::Edge(Sh2);
637    if(e1.IsNull() || e2.IsNull()) {
638      di<<"Invalid type of arguments"<<"\n";
639      return 1;
640    }
641    Standard_Real aTol = Precision::Confusion();
642    Standard_Real aDistDomain = 0.0;
643    Standard_Integer k = 3;
644    if(k < n)
645      aTol = Draw::Atof(a[k++]);
646    if(k < n)
647      aDistDomain = Draw::Atof(a[k++]);
648    
649    ShapeAnalysis_Edge sae;
650    if(sae.CheckOverlapping(e1,e2,aTol,aDistDomain)) {
651      if(aDistDomain ==0.0)
652        di<<"Edges is overlaping comletly"<<"\n";
653      else {
654         di<<"Edges is overlaped"<<"\n";
655         di<<"with tolerance = "<<aTol<<"\n";
656         di<<"on segment length = "<<aDistDomain<<"\n";
657      }
658    }
659    else di<<"Edges is not overlaped"<<"\n";
660    return 0;
661 }
662
663 //=======================================================================
664 //function : checkfclass2d
665 //purpose  : 
666 //=======================================================================
667
668 static Standard_Integer checkfclass2d(Draw_Interpretor& di, Standard_Integer n, const char** a)
669 {
670   if( n < 4) {
671     di<<"Invalid number of arguments"<<"\n";
672     return 1;
673   }
674   TopoDS_Shape Sh1 = DBRep::Get(a[1]);
675   Standard_Real ucoord = Draw::Atof(a[2]);
676   Standard_Real vcoord = Draw::Atof(a[3]);
677   if(Sh1.IsNull() || Sh1.ShapeType()!= TopAbs_FACE) {
678     di<<"Invalid arguments"<<"\n";
679     return 1;
680   }
681   TopoDS_Face aFace = TopoDS::Face(Sh1);
682   gp_Pnt2d p2d(ucoord,vcoord);
683   BRepTopAdaptor_FClass2d f2d(aFace,Precision::Confusion());
684   TopAbs_State stat = f2d.Perform(p2d);
685   if(stat == TopAbs_OUT)
686     di<<"Point is OUT"<<"\n";
687   else if(stat == TopAbs_IN)
688     di<<"Point is IN"<<"\n";
689   else if(stat == TopAbs_ON)
690     di<<"Point is ON"<<"\n";
691   else
692     di<<"Point is UNKNOWN"<<"\n";
693   return 0;
694 }
695
696 static Standard_Integer connectedges(Draw_Interpretor& di, Standard_Integer n, const char** a)
697 {
698   if( n < 3) {
699     di<<"Invalid number of arguments. Should be : result shape [toler shared]"<<"\n";
700     return 1;
701   }
702   TopoDS_Shape aSh1 = DBRep::Get(a[2]);
703   if(aSh1.IsNull()) {
704     di<<"Shape is null"<<"\n";
705     return 1;
706   }
707   Standard_Real aTol = Precision::Confusion();
708   if( n > 3)
709     aTol = Draw::Atof(a[3]);
710   
711   Standard_Boolean shared = Standard_True;
712   if( n > 4)
713     shared = (Draw::Atoi(a[4]) == 1);
714   TopExp_Explorer aExpE(aSh1,TopAbs_EDGE);
715   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
716   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
717   TopTools_IndexedMapOfShape aMapEdges;
718   for( ; aExpE.More(); aExpE.Next())
719   {
720     aSeqEdges->Append(aExpE.Current());
721     aMapEdges.Add(aExpE.Current());
722   }
723   
724   ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,aTol,shared,aSeqWires );
725   TopoDS_Compound aComp;
726   BRep_Builder aB;
727   aB.MakeCompound(aComp);
728   Standard_Integer i = 1;
729   for( ; i <= aSeqWires->Length() ; i++)
730   {
731     TopoDS_Shape aW = aSeqWires->Value(i);
732     di<<"Wire - "<<i<<" : "<<"\n";
733     
734     TopExp_Explorer aExp1(aW, TopAbs_EDGE);
735     for( ; aExp1.More(); aExp1.Next())
736     {
737       if(shared)
738       {
739         Standard_Integer ind = aMapEdges.FindIndex(aExp1.Current());
740         di<<ind<<" ";
741       }
742        else
743       {
744         TopoDS_Vertex aV1, aV2;
745         TopExp::Vertices(TopoDS::Edge(aExp1.Current()), aV1,aV2);
746         gp_Pnt aP = BRep_Tool::Pnt(aV1);
747         di<<aP.X()<<" "<<aP.Y()<<" "<<aP.Z()<<"\n";
748       }
749     }
750    
751     di<<"\n";
752     aB.Add( aComp,aSeqWires->Value(i));
753   }
754   DBRep::Set(a[1],aComp);
755   return 0;
756   
757 }
758
759 //=======================================================================
760 //function : InitCommands
761 //purpose  : 
762 //=======================================================================
763
764  void SWDRAW_ShapeFix::InitCommands(Draw_Interpretor& theCommands) 
765 {
766   static Standard_Integer initactor = 0;
767   if (initactor) return;  initactor = 1;
768   
769   Standard_CString g = SWDRAW::GroupName();
770   
771   theCommands.Add ("edgesameparam","nom shape draw ou * [+ option force]",
772                    __FILE__,edgesameparam,g);
773   theCommands.Add ("settolerance","shape [mode=v-e-f-a] val(fix value) or tolmin tolmax",
774                    __FILE__,settolerance,g);
775   theCommands.Add ("stwire","stwire tout court pour help complet",
776                    __FILE__,stwire,g);
777   theCommands.Add ("reface","shape result : controle sens wire",
778                    __FILE__,reface,g);
779   theCommands.Add ("fixshape","res shape [preci [maxpreci]] [{switches}]",
780                    __FILE__,fixshape,g);
781 //  theCommands.Add ("testfill","result edge1 edge2",
782 //                 __FILE__,XSHAPE_testfill,g);
783   theCommands.Add ("fixwgaps","result shape [toler=0]",
784                    __FILE__,fixgaps,g);
785   theCommands.Add ("fixsmall","result shape [toler=1.]",
786                    __FILE__,fixsmall,g);
787   theCommands.Add ("fixsmalledges","result shape [toler mode amxangle]",
788                    __FILE__,fixsmalledges,g);
789   theCommands.Add ("checkoverlapedges","edge1 edge2 [toler domaindist]",
790                    __FILE__,checkoverlapedges,g);
791   theCommands.Add ("checkfclass2d","face ucoord vcoord",
792                    __FILE__,checkfclass2d,g);
793   theCommands.Add ("connectedges","res shape [toler shared]",
794                    __FILE__,connectedges,g);
795   
796 }
797