0022898: IGES import fails in german environment
[occt.git] / src / TestTopOpe / TestTopOpe_CORCommands.cxx
1 // Created on: 1997-07-24
2 // Created by: Xuan PHAM PHU
3 // Copyright (c) 1997-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
22 #include <TestTopOpe.hxx>
23 #include <TopOpeBRepTool_2d.hxx>
24 #include <TopOpeBRepTool_C2DF.hxx>
25 #include <TopOpeBRepTool_CORRISO.hxx>
26 #include <TopOpeBRepTool_TOOL.hxx>
27 #include <TopOpeBRepTool_REGUS.hxx>
28 #include <TopOpeBRepTool_CLASSI.hxx>
29 #include <TopOpeBRepTool_TOOL.hxx>
30 #include <TopOpeBRepBuild_define.hxx>
31
32 #include <gp_Pnt2d.hxx>
33 #include <gp_Trsf2d.hxx>
34 #include <gp_Pnt2d.hxx>
35 #include <gp_Dir2d.hxx>
36 #include <gp_Vec2d.hxx>
37 #include <BRepAdaptor_Curve2d.hxx>
38 #include <BndLib_Add2dCurve.hxx>
39 #include <BRepLProp_SLProps.hxx>
40 #include <Geom_Curve.hxx>
41 #include <Geom2dAdaptor_Curve.hxx>
42 #include <Geom2d_Line.hxx>
43 #include <Geom2d_Curve.hxx>
44 #include <Geom2d_TrimmedCurve.hxx>
45 #include <Geom_Line.hxx>
46 #include <Geom_Surface.hxx>
47 #include <GeomAPI_IntCS.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Solid.hxx>
50 #include <TopoDS_Wire.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopExp.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <BRep_Tool.hxx>
55 #include <BRep_Builder.hxx>
56 #include <Precision.hxx>
57 #include <Bnd_Box2d.hxx>
58 #include <Bnd_Array1OfBox2d.hxx>
59 #include <BRepClass3d_SolidClassifier.hxx>
60
61 #include <TopOpeBRepTool.hxx>
62 #include <TopOpeBRepDS.hxx>
63 #include <TopOpeBRepBuild_FaceBuilder.hxx>
64 #include <TopOpeBRepBuild_ShellToSolid.hxx>
65 #include <TopOpeBRepTool_EXPORT.hxx>
66 #include <TopOpeBRepTool_SolidClassifier.hxx>
67 #include <TopOpeBRepTool_ShapeClassifier.hxx>
68 #include <TopOpeBRepDS_EXPORT.hxx>
69
70 #include <Draw.hxx>
71 #include <Draw_Interpretor.hxx>
72 #include <DrawTrSurf.hxx>
73 #include <Draw_Color.hxx>
74 #include <DBRep.hxx>
75
76 #ifdef DRAW
77 #include <TopOpeBRepTool_DRAW.hxx>
78 #endif
79
80 Standard_IMPORT Draw_Color DrawTrSurf_CurveColor(const Draw_Color col);
81 Standard_IMPORT void DBRep_WriteColorOrientation ();
82 Standard_IMPORT Draw_Color DBRep_ColorOrientation (const TopAbs_Orientation Or);
83
84 Standard_IMPORT Draw_Color DrawTrSurf_CurveColor(const Draw_Color col);
85 Standard_IMPORT void DBRep_WriteColorOrientation ();
86 Standard_IMPORT Draw_Color DBRep_ColorOrientation (const TopAbs_Orientation Or);
87 Standard_EXPORT void FUN_tool_mkBnd2d(const TopoDS_Shape& W, const TopoDS_Shape& F,Bnd_Box2d& B2d);
88 Standard_EXPORT void FUN_tool_UpdateBnd2d(Bnd_Box2d& B2d, const Bnd_Box2d& newB2d);
89 Standard_IMPORT Standard_Integer FUN_tool_classiBnd2d(const Bnd_Array1OfBox2d& B,const Standard_Boolean chklarge = Standard_True);
90 Standard_IMPORT Standard_Boolean FUN_tool_chkp2dFORinE(const gp_Pnt2d& p2d,const TopoDS_Edge& E, 
91                                           const TopoDS_Face& F, Standard_Real& tol2d);
92 Standard_IMPORT TopoDS_Vertex FUN_tool_getv(const Standard_Integer Index,const TopoDS_Edge& E);
93
94
95 #define SAME     (-1)
96 #define DIFF     (-2)
97 #define UNKNOWN  ( 0)
98 #define oneINtwo ( 1)
99 #define twoINone ( 2)
100
101 #ifdef DEB
102 static void FUN_coutsta(const Standard_Integer& sta, const Standard_Integer& i1, const Standard_Integer& i2, Draw_Interpretor& di)
103 {
104   switch (sta) {
105   case SAME:
106     di<<i1<<" gives SAME bnd with "<<i2<<"\n"; break;
107   case DIFF:
108     di<<i1<<" gives  OUT bnd with "<<i2<<"\n"; break;
109   case oneINtwo:
110     di<<i1<<" is IN "<<i2<<"\n"; break;
111   case twoINone:
112     di<<i2<<" is IN "<<i1<<"\n"; break;
113   }
114 }
115 #endif
116
117 static void FUN_test_draw(TCollection_AsciiString aa,
118                           const TopoDS_Edge& E, const TopoDS_Face& F,
119                           const Standard_Integer ie,
120                           Draw_Interpretor& di)
121 {  
122   if (E.IsNull())  {di<<"************* null edge\n"; return;} 
123   Standard_Real f,l; const Handle(Geom2d_Curve)& PC = BRep_Tool::CurveOnSurface(E,F,f,l);  
124   if (PC.IsNull()) {di<<"************* no curv on surf\n"; return;}
125   TCollection_AsciiString bb(aa); bb += TCollection_AsciiString(ie);  
126   char* aaa = (char *)bb.ToCString();
127   
128   Standard_Boolean coldef = Standard_False;
129   TopExp_Explorer ex(F,TopAbs_EDGE);
130   Draw_Color col;
131 #ifdef DEB
132   Draw_Color savecol =
133 #endif
134                        DrawTrSurf_CurveColor(Draw_Color(Draw_rouge));
135   for (; ex.More(); ex.Next()) 
136     if (E.IsEqual(ex.Current())) 
137       {col = DBRep_ColorOrientation(ex.Current().Orientation()); 
138        coldef = Standard_True;
139        break;}
140   if (!coldef) col = DBRep_ColorOrientation(E.Orientation());
141   
142   DrawTrSurf_CurveColor(col);  
143   DrawTrSurf::Set(aaa,new Geom2d_TrimmedCurve(PC,f,l));
144 }
145
146 void FUN_draw(const TCollection_AsciiString aa,const TopoDS_Shape& s)
147 {char* aaa = (char *)aa.ToCString(); DBRep::Set(aaa,s);}
148
149 void FUN_cout(const gp_Pnt2d& p2d, Draw_Interpretor& di)
150 {di <<" = ("<<p2d.X()<<" "<<p2d.Y()<<") ";}
151
152 #ifdef DEB
153 Standard_IMPORT void FUN_tool_coutsta(const Standard_Integer& sta, const Standard_Integer& i1, const Standard_Integer& i2);
154 #endif
155
156
157 Standard_Integer regularize(Draw_Interpretor& di, Standard_Integer n, const char** a)
158 {
159   if (n < 2) return 1;
160   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
161   TopoDS_Face fa = TopoDS::Face(aLocalShape);
162 //  TopoDS_Face fa = TopoDS::Face(DBRep::Get(a[1]));
163   if (fa.IsNull()) {di<<"null face"<<"\n"; return 1;}
164   
165   TopTools_DataMapOfShapeListOfShape ESplits;
166   TopTools_ListOfShape lof;  
167   TopOpeBRepTool::Regularize(fa, lof, ESplits);  
168   Standard_Integer nfa = lof.Extent();
169   di<<"face gives "<<nfa<<" newfaces"<<"\n";
170   
171   Standard_Integer i = 0;
172   TopTools_ListIteratorOfListOfShape itlof(lof) ;
173   for ( ; itlof.More(); itlof.Next()) {
174     i++;
175     TCollection_AsciiString aa = TCollection_AsciiString("fa_");
176     aa += TCollection_AsciiString(i);
177     FUN_draw(aa,itlof.Value());        
178   }
179   BRep_Builder BB;
180   TopoDS_Compound CC; BB.MakeCompound(CC);
181   for (itlof.Initialize(lof); itlof.More(); itlof.Next()) BB.Add(CC, itlof.Value());
182   di<<"resulting compound is cmp"<<"\n";
183   TCollection_AsciiString aa = TCollection_AsciiString("cmp");
184   FUN_draw(aa,CC);  
185   
186   return 0;
187 }
188
189 static Standard_Integer splitF(Draw_Interpretor& di, Standard_Integer n, const char** a)
190 {
191   if (n < 2) return 1;
192   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
193   const TopoDS_Face& fa = TopoDS::Face(aLocalShape);
194 //  const TopoDS_Face& fa = TopoDS::Face(DBRep::Get(a[1]));
195   if (fa.IsNull()) {di<<"null face"<<"\n"; return 1;}
196   
197   TopTools_ListOfShape fsplits;
198 //  Standard_Boolean splitok = FUN_tool_SplitF(fa,fsplits);
199   Standard_Boolean splitok = TopOpeBRepTool_REGUS::SplitF(fa,fsplits);
200   
201   if (!splitok) {di<<"no splits"<<"\n"; return 0;}
202   di<<"fa gives "<<fsplits.Extent()<<" splits"<<"\n";
203   
204   BRep_Builder BB;
205   TopoDS_Compound CC; BB.MakeCompound(CC);
206   for (TopTools_ListIteratorOfListOfShape it(fsplits); it.More(); it.Next()) BB.Add(CC, it.Value());
207   
208   di<<"resulting compound is cmp"<<"\n";
209   TCollection_AsciiString aa = TCollection_AsciiString("cmp");
210   FUN_draw(aa,CC); 
211   return 0;
212 }
213
214
215 static Standard_Integer regush(Draw_Interpretor& di, Standard_Integer n, const char** a)
216 {
217   if (n < 2) return 1;
218   const TopoDS_Shape& sha = DBRep::Get(a[1]);
219   if (sha.IsNull()) {di<<"null shape"<<"\n"; return 1;}
220   TopExp_Explorer ex(sha, TopAbs_SOLID);
221   const TopoDS_Solid& so = TopoDS::Solid(ex.Current());
222   if (so.IsNull()) {di<<"no solid"<<"\n"; return 1;}
223   
224   TopTools_DataMapOfShapeListOfShape FSplits;
225   TopTools_DataMapOfShapeListOfShape OldSheNewShe;
226   
227   BRep_Builder BB;
228   TopoDS_Compound CC; BB.MakeCompound(CC);
229   Standard_Integer nshe = 0;
230   Standard_Boolean regu = TopOpeBRepTool::RegularizeShells(so,OldSheNewShe,FSplits);
231   if (!regu) {di<<"solid gives no new shell"<<"\n";}
232   else {  
233     Standard_Integer noldshe = OldSheNewShe.Extent();
234     TopTools_DataMapIteratorOfDataMapOfShapeListOfShape ite(OldSheNewShe);
235     for (; ite.More(); ite.Next()){
236       const TopoDS_Shape& oldshe = ite.Key();
237       const TopTools_ListOfShape& newshells =  ite.Value();
238       if (newshells.IsEmpty()) {BB.Add(CC,oldshe); nshe++;}
239       else {
240         for (TopTools_ListIteratorOfListOfShape it(newshells); it.More(); it.Next()){
241           BB.Add(CC,it.Value()); nshe++;
242         }
243       }
244     } // i = 1..noldshe
245     di <<"noldshe = "<<noldshe<<" gives nshe = "<<nshe<<"\n";
246     di<<"resulting compound is cmp"<<"\n";
247     TCollection_AsciiString aa = TCollection_AsciiString("cmp");
248     FUN_draw(aa,CC);  
249   }
250   return 0;
251 }
252
253 Standard_Integer reguso(Draw_Interpretor& di, Standard_Integer n, const char** a)
254 {
255   if (n < 2) return 1;
256   const TopoDS_Shape& sha = DBRep::Get(a[1]);
257   if (sha.IsNull()) {di<<"null shape"<<"\n"; return 1;}
258   TopExp_Explorer ex(sha, TopAbs_SOLID);
259   const TopoDS_Solid& so = TopoDS::Solid(ex.Current());
260   if (so.IsNull()) {di<<"no solid"<<"\n"; return 1;}
261   
262   TopTools_DataMapOfShapeListOfShape FSplits;
263   TopTools_DataMapOfShapeListOfShape OldSheNewShe;
264   
265   BRep_Builder BB;
266   TopoDS_Compound CC; BB.MakeCompound(CC);
267 //  Standard_Integer nshe = 0;
268   Standard_Boolean regu = TopOpeBRepTool::RegularizeShells(so,OldSheNewShe,FSplits);
269   if (!regu) {di<<"solid gives no new shell"<<"\n";}
270   else {  
271     TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itm(OldSheNewShe);
272     
273     TopOpeBRepBuild_ShellToSolid SheToSo;      
274     for (; itm.More(); itm.Next()) {
275       const TopTools_ListOfShape& lns = itm.Value();
276       TopTools_ListIteratorOfListOfShape itsh(lns);
277       for (; itsh.More(); itsh.Next()) {
278         const TopoDS_Shell& she = TopoDS::Shell(itsh.Value());
279         SheToSo.AddShell(she);
280       }
281     }
282     TopTools_ListOfShape splits; 
283     SheToSo.MakeSolids(so,splits);
284     
285     BRep_Builder BB;
286     TopoDS_Compound CC; BB.MakeCompound(CC);
287     Standard_Integer nSo = 0;
288     TopTools_ListIteratorOfListOfShape itSo(splits);
289     for (; itSo.More(); itSo.Next()) {
290       const TopoDS_Shape& spli = itSo.Value();
291       BB.Add(CC,spli);
292       nSo++;
293     } 
294     di<<"so gives "<<nSo<<" new solids"<<"\n";
295     di<<"resulting compound is cmp"<<"\n";
296     TCollection_AsciiString aa = TCollection_AsciiString("cmp");
297     FUN_draw(aa,CC);  
298   }
299   return 0;
300 }
301
302 static Standard_Integer purge(Draw_Interpretor& di, Standard_Integer n, const char** a)
303 {  
304
305   if (n < 2) return 1;
306   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
307   TopoDS_Face fa = TopoDS::Face(aLocalShape);
308 //  TopoDS_Face fa = TopoDS::Face(DBRep::Get(a[1]));
309   if (fa.IsNull()) return 1;
310     TopOpeBRepTool_CORRISO CORRISO(fa); 
311   Standard_Real tolF = BRep_Tool::Tolerance(fa);
312   Standard_Real uperiod; Standard_Boolean uclosed = CORRISO.Refclosed(1,uperiod);
313   Standard_Real vperiod; Standard_Boolean vclosed = CORRISO.Refclosed(2,vperiod);
314   if (!uclosed && !vclosed) return 1;
315   Standard_Boolean inU = uclosed ? Standard_True : Standard_False;  
316   Standard_Real xmin = inU ? (CORRISO.GASref().FirstUParameter()) : (CORRISO.GASref().LastUParameter());
317   Standard_Real xper = inU ? uperiod : vperiod;
318   Standard_Real tolx = inU ? (CORRISO.Tol(1,tolF)) : (CORRISO.Tol(2,tolF));
319
320   BRep_Builder BB; 
321   TopTools_ListOfShape lWs; Standard_Boolean hasnew = Standard_False;
322   TopExp_Explorer exw(fa, TopAbs_WIRE);
323   for (; exw.More(); exw.Next()){
324     const TopoDS_Shape& W = exw.Current();
325
326     CORRISO.Init(W);
327     Standard_Boolean ok = CORRISO.UVClosed();
328     if (ok) {lWs.Append(W); continue;}
329
330     TopTools_ListOfShape cEds; 
331     TopTools_ListIteratorOfListOfShape ite(CORRISO.Eds());
332     for (; ite.More(); ite.Next()){
333       const TopoDS_Edge& E = TopoDS::Edge(ite.Value());
334       Standard_Boolean closing = BRep_Tool::IsClosed(E,fa); 
335       if (!closing) {// xpu231198 : pcurve modified, the information is lost
336         TopOpeBRepTool_C2DF C2DF; Standard_Boolean isb = CORRISO.UVRep(E,C2DF);
337         if (!isb) return 1;//NYIRAISE
338         Standard_Boolean onclo = TopOpeBRepTool_TOOL::IsonCLO(C2DF,inU,xmin,xper,tolx);
339         if (onclo) closing=Standard_True;
340       }
341       if (closing) cEds.Append(E);
342     }          
343     Standard_Integer ncE = cEds.Extent();
344     Standard_Boolean nopurge = (ncE <= 1);
345     if (nopurge) {lWs.Append(W); continue;}
346     
347     // Checking <W>
348     TopTools_ListOfShape lfyE; Standard_Boolean topurge = CORRISO.PurgeFyClosingE(cEds,lfyE);
349     if (!topurge) {lWs.Append(W); continue;}
350     
351     TopoDS_Wire Wi; BB.MakeWire(Wi); // Add une TShape
352     Wi.Free(Standard_True);
353     ite.Initialize(CORRISO.Eds());
354     for (; ite.More(); ite.Next()) {
355       const TopoDS_Edge& ed = TopoDS::Edge(ite.Value());
356       Standard_Boolean equ = Standard_False;
357       for (TopTools_ListIteratorOfListOfShape itlfyE(lfyE);itlfyE.More();itlfyE.Next()) {
358         const TopoDS_Shape& fyE = itlfyE.Value();
359         if (ed.IsEqual(fyE)) {
360           equ = Standard_True; 
361           break;
362         }
363       }
364       if (equ) {
365         continue;
366       }
367       BB.Add(Wi,ed);
368     }
369     lWs.Append(Wi);
370   } // exw
371   
372   if (!hasnew) return 1;
373
374   // Building up the new face :
375   aLocalShape = fa.EmptyCopied();
376   TopoDS_Face newF = TopoDS::Face(aLocalShape);
377 //  TopoDS_Face newF = TopoDS::Face(fa.EmptyCopied());
378   TopTools_ListIteratorOfListOfShape itw(lWs);  
379   for (; itw.More(); itw.Next()) BB.Add(newF, TopoDS::Wire(itw.Value()));
380
381   di <<"New face built is newF"<<"\n";
382   TCollection_AsciiString aa("newF");
383   FUN_draw(aa, newF);
384   return 0;  
385 }
386
387
388 Standard_Integer correctONISO(Draw_Interpretor& di, Standard_Integer n, const char** a)
389 {
390   if (n < 3) return 1;
391   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
392   TopoDS_Face F   = TopoDS::Face(aLocalShape);
393   aLocalShape = DBRep::Get(a[2]);
394   TopoDS_Face Fsp = TopoDS::Face(aLocalShape);
395 //  TopoDS_Face F   = TopoDS::Face(DBRep::Get(a[1]));
396 //  TopoDS_Face Fsp = TopoDS::Face(DBRep::Get(a[2]));
397
398   FC2D_Prepare(F,F);
399
400   if (F.IsNull() || Fsp.IsNull()) {di<<"NULL shape(s)"<<"\n";return 1;}
401
402   TopOpeBRepTool::CorrectONUVISO(F,Fsp);
403   TCollection_AsciiString aa("newFsp");
404   FUN_draw(aa,Fsp);
405   di<<"-> newFsp"<<"\n";
406   
407   return 0;  
408 }
409
410 // ======================================================================
411 //                useful commands :
412 // ======================================================================
413
414 static Standard_Integer isclosingE(Draw_Interpretor& di, Standard_Integer n, const char** a)
415 {
416   if (n < 3) return 1;
417   TopoDS_Shape aLocalShape = DBRep::Get(a[1]) ;
418   TopoDS_Edge ed = TopoDS::Edge(aLocalShape);
419   aLocalShape = DBRep::Get(a[2]);
420   TopoDS_Face fa = TopoDS::Face(aLocalShape);
421 //  TopoDS_Edge ed = TopoDS::Edge(DBRep::Get(a[1]));
422 //  TopoDS_Face fa = TopoDS::Face(DBRep::Get(a[2]));
423   Standard_Boolean isclosing = BRep_Tool::IsClosed(ed,fa);
424   TopAbs_Orientation oriE = ed.Orientation();
425   if (isclosing) {di <<"edge is ";
426                   //TopAbs::Print(oriE,cout);
427                   Standard_SStream aSStream;
428                   TopAbs::Print(oriE,aSStream);
429                   di << aSStream;
430                   di<<" closing edge"<<"\n";}
431   else di <<"edge is NOT closing edge"<<"\n";
432   return 0;
433 }
434
435 static Standard_Integer compareshsh(Draw_Interpretor& di, Standard_Integer n, const char** a)
436 {
437   if (n < 3) return 1;
438   TopoDS_Shape sh1 = DBRep::Get(a[1]);
439   TopoDS_Shape sh2 = DBRep::Get(a[2]);
440   Standard_Boolean issame = sh1.IsSame(sh2); 
441   if (issame) di<<" same shapes"<<"\n";
442   else        di <<" shapes are not same"<<"\n";
443   Standard_Boolean isequal = sh1.IsEqual(sh2); if (isequal) di<<" equal shapes"<<"\n";
444   return 0;
445 }
446 static Standard_Integer pcurveedgeonface(Draw_Interpretor& di, Standard_Integer n, const char** a)
447 {
448   if (n < 3) return 1;
449   TopoDS_Shape ed = DBRep::Get(a[1]);
450   TopoDS_Shape fa = DBRep::Get(a[2]);
451   TCollection_AsciiString aa("edonfa_");
452   FUN_test_draw(aa,TopoDS::Edge(ed),TopoDS::Face(fa),0,di);
453   return 0;
454 }
455
456 static Standard_Integer pcurvesonface(Draw_Interpretor& di, Standard_Integer n, const char** a)
457 {
458   if (n < 3) return 1;
459   TopoDS_Shape sh = DBRep::Get(a[2]);
460   TopoDS_Shape fa = DBRep::Get(a[3]);
461   TopExp_Explorer exe(sh, TopAbs_EDGE);
462   Standard_Integer i = 0;
463   for (; exe.More(); exe.Next()){
464     i++;
465     TCollection_AsciiString aa(a[1]);
466     FUN_test_draw(aa,TopoDS::Edge(exe.Current()),TopoDS::Face(fa),i,di);    
467   }
468   return 0;
469 }
470
471 static Standard_Integer orivine(Draw_Interpretor& di, Standard_Integer n, const char** a)
472 {
473   if (n < 3) return 1;
474   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
475   TopoDS_Vertex v = TopoDS::Vertex(aLocalShape);
476   aLocalShape = DBRep::Get(a[2]);
477   TopoDS_Edge ed = TopoDS::Edge(aLocalShape);
478 //  TopoDS_Vertex v = TopoDS::Vertex(DBRep::Get(a[1]));
479 //  TopoDS_Edge ed = TopoDS::Edge(DBRep::Get(a[2]));
480
481   Standard_Integer ori = TopOpeBRepTool_TOOL::OriinSor(v,ed);
482   if      (ori==0) di<<"v not in ed"<<"\n";
483   else if (ori==1)  di<<"v FORWARD in ed"<<"\n";
484   else if (ori==2)  di<<"v REVERSED in ed"<<"\n";
485   else if (ori==3)  di<<"v INTERNAL in ed"<<"\n";
486   else if (ori==4)  di<<"v EXTERNAL in ed"<<"\n";
487   else if (ori==5)  di<<"v CLOSING in ed"<<"\n";
488   return 0;
489 }
490
491 static Standard_Integer vine(Draw_Interpretor& di, Standard_Integer n, const char** a)
492 {
493   if (n < 4) return 1;
494   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
495   TopoDS_Vertex v = TopoDS::Vertex(aLocalShape);
496   aLocalShape = DBRep::Get(a[2]);
497   TopoDS_Edge ed = TopoDS::Edge(aLocalShape);
498   aLocalShape = DBRep::Get(a[3]);
499   TopoDS_Face fa = TopoDS::Face(aLocalShape);
500 //  TopoDS_Vertex v = TopoDS::Vertex(DBRep::Get(a[1]));
501 //  TopoDS_Edge ed = TopoDS::Edge(DBRep::Get(a[2]));
502 //  TopoDS_Face fa = TopoDS::Face(DBRep::Get(a[3]));
503   
504   Standard_Real pf,pl;
505   const Handle(Geom2d_Curve) PC = BRep_Tool::CurveOnSurface(ed,fa,pf,pl);
506   gp_Pnt2d p2df, p2dl; PC->D0(pf,p2df); PC->D0(pl,p2dl);
507   di << "p2df";FUN_cout(p2df,di); di << "p2dl";FUN_cout(p2dl,di);di<<"\n";  
508   
509   Standard_Integer ori = TopOpeBRepTool_TOOL::OriinSor(v,ed);
510   if      (ori==0)  {di<<"v not in ed"<<"\n"; return 0;}
511   else if (ori==1)  di<<"v FORWARD in ed"<<"\n";
512   else if (ori==2)  di<<"v REVERSED in ed"<<"\n";
513   else if (ori==3)  di<<"v INTERNAL in ed"<<"\n";
514   else if (ori==4)  di<<"v EXTERNAL in ed"<<"\n";
515   else if (ori==5)  di<<"v CLOSING in ed"<<"\n";
516
517   if ((ori == 1) || (ori == 2)) {
518     Standard_Real par = TopOpeBRepTool_TOOL::ParE(ori,ed);//FUN_tool_parOnE(ind,ed,fa);
519     gp_Pnt2d p2d; PC->D0(par,p2d);
520     di <<"p2d : with TopOpeBRepTool_TOOL::ParE";FUN_cout(p2d,di);di<<"\n"; 
521   }
522   
523   Standard_Real par = BRep_Tool::Parameter(v,ed,fa);
524   gp_Pnt2d pp2d; PC->D0(par,pp2d);
525   di <<"p2d computed with BRep_Tool::Parameter";FUN_cout(pp2d,di);di<<"\n";
526   
527   return 0;
528 }
529
530 static Standard_Integer issubshape(Draw_Interpretor& di, Standard_Integer n, const char** a)
531 {  
532   if (n < 3) return 1;
533   TopoDS_Shape subshape = DBRep::Get(a[1]);
534   TopoDS_Shape shape    = DBRep::Get(a[2]);
535   TopExp_Explorer ex(shape, subshape.ShapeType());
536   Standard_Boolean issubs = Standard_False;
537   for (; ex.More(); ex.Next())
538     if (ex.Current().IsSame(subshape)) {issubs = Standard_True; break;}
539   
540   if (issubs) di<<" is subshape"<<"\n";
541   else        di<<" is NOT subshape"<<"\n";
542   return 0;
543 }
544
545 void FUN_mkBnd2dBREP(const TopoDS_Shape& W, const TopoDS_Shape& F,Bnd_Box2d& B2d,const Standard_Integer& i)
546 {
547   // greater <B> with <W>'s UV representation on <F>
548   Standard_Real tol = 1.e-8;  
549   TopExp_Explorer ex;
550   for (ex.Init(W, TopAbs_EDGE); ex.More(); ex.Next()) {
551 //  for (TopExp_Explorer ex(W, TopAbs_EDGE); ex.More(); ex.Next()) {
552     if (i == 0) {
553       FUN_tool_mkBnd2d(W,F,B2d);
554     }
555     if (i == 1) {
556       BRepAdaptor_Curve2d BC2d(TopoDS::Edge(ex.Current()), TopoDS::Face(F));
557       BndLib_Add2dCurve::Add(BC2d, tol, B2d);
558     }
559     if (i == 2) {
560       Standard_Real f,l;
561       Handle(Geom2d_Curve) PC = BRep_Tool::CurveOnSurface(TopoDS::Edge(ex.Current()),TopoDS::Face(F),f,l);
562       Geom2dAdaptor_Curve GC2d(PC);
563       BndLib_Add2dCurve::Add(GC2d, tol, B2d);
564     }
565     if (i == 3) {
566       TopLoc_Location L; Standard_Real f,l;
567       const Handle(Geom_Surface)& S = BRep_Tool::Surface(TopoDS::Face(F),L);
568       const Handle(Geom2d_Curve)& PC = BRep_Tool::CurveOnSurface(TopoDS::Edge(ex.Current()),S,L,f,l); 
569       Geom2dAdaptor_Curve GC2d(PC);
570       BndLib_Add2dCurve::Add(GC2d, tol, B2d);
571     }
572     
573   } //ex(W,EDGE)
574 }
575
576 static Standard_Integer drawbnd2d(Draw_Interpretor& , Standard_Integer n, const char** a)
577 {
578   if (n < 5) return 1;
579   Standard_Integer i = Draw::Atoi(a[4]);
580   TopoDS_Shape W = DBRep::Get(a[2]);
581   TopoDS_Shape F = DBRep::Get(a[3]);
582   if (W.IsNull() || F.IsNull()) return 1;
583   Bnd_Box2d B2d;
584   Standard_Real umin,vmin,umax,vmax;
585   FUN_mkBnd2dBREP(W,F,B2d,i);
586   B2d.Get(umin,vmin,umax,vmax);
587   
588   Handle(Geom2d_Line) cx = new Geom2d_Line(gp_Pnt2d(umin,vmin),gp_Dir2d(1.,0.));
589   Handle(Geom2d_Line) cy = new Geom2d_Line(gp_Pnt2d(umin,vmin),gp_Dir2d(0.,1.));
590   gp_Trsf2d tx; gp_Vec2d vx(umax-umin,0.); tx.SetTranslation(vx);
591   gp_Trsf2d ty; gp_Vec2d vy(0.,vmax-vmin); ty.SetTranslation(vy);
592   
593   Handle(Geom2d_TrimmedCurve) tcx = new Geom2d_TrimmedCurve(cx,0.,umax-umin);
594   Handle(Geom2d_TrimmedCurve) tcy = new Geom2d_TrimmedCurve(cy,0.,vmax-vmin);
595   Handle(Geom2d_TrimmedCurve) tccx = Handle(Geom2d_TrimmedCurve)::DownCast(tcx->Copy()); tccx->Transform(ty);
596   Handle(Geom2d_TrimmedCurve) tccy = Handle(Geom2d_TrimmedCurve)::DownCast(tcy->Copy()); tccy->Transform(tx);
597   
598   Draw_Color col(Draw_blanc);
599   DrawTrSurf_CurveColor(col);
600   
601   TCollection_AsciiString aa3 = TCollection_AsciiString(a[1]); aa3 += TCollection_AsciiString(3);
602   TCollection_AsciiString aa4 = TCollection_AsciiString(a[1]); aa4 += TCollection_AsciiString(4);
603   
604   TCollection_AsciiString aa;
605   aa=TCollection_AsciiString(a[1]); aa+=TCollection_AsciiString(1); char* aaa = (char *)aa.ToCString(); DrawTrSurf::Set(aaa,tcx);
606   aa=TCollection_AsciiString(a[1]); aa+=TCollection_AsciiString(2); aaa=(char *)aa.ToCString();         DrawTrSurf::Set(aaa,tcy);
607   aa=TCollection_AsciiString(a[1]); aa+=TCollection_AsciiString(3); aaa=(char *)aa.ToCString();         DrawTrSurf::Set(aaa,tccx);
608   aa=TCollection_AsciiString(a[1]); aa+=TCollection_AsciiString(4); aaa=(char *)aa.ToCString();         DrawTrSurf::Set(aaa,tccy);
609   return 0;
610 }
611
612 static Standard_Integer classifBnd2d(Draw_Interpretor& di, Standard_Integer n, const char** a)
613 {
614   if (n < 5) return 1;
615   TopoDS_Shape W1 = DBRep::Get(a[1]);
616   TopoDS_Shape W2 = DBRep::Get(a[2]);
617   TopoDS_Shape F = DBRep::Get(a[3]);
618   
619   TopoDS_Wire w1 = TopoDS::Wire(W1);
620   TopoDS_Wire w2 = TopoDS::Wire(W2);
621   TopoDS_Face Fref = TopoDS::Face(F);
622
623   TopOpeBRepTool_CLASSI classi; classi.Init2d(Fref);
624   Standard_Real tolF = BRep_Tool::Tolerance(Fref);
625   Standard_Real toluv = TopOpeBRepTool_TOOL::TolUV(Fref,tolF);
626
627   if (w1.IsNull() || w2.IsNull() || Fref.IsNull()) return 1;
628
629   Standard_Integer sta = classi.ClassiBnd2d(w1,w2,toluv,Standard_True);
630   di <<"wires classification : checklarge=true ";
631 #ifdef DEB
632   FUN_tool_coutsta(sta,1,2);
633 #endif
634
635   sta = classi.ClassiBnd2d(w1,w2,toluv,Standard_False);
636   di <<"wires classification : checklarge=false ";
637 #ifdef DEB
638   FUN_tool_coutsta(sta,1,2);
639 #endif
640   
641   return 0;
642 }
643
644 static Standard_Integer pntonc(Draw_Interpretor& di, Standard_Integer n, const char** a)
645 {
646   if (n < 3) return 1;
647   Standard_Real x = Draw::Atof(a[1]);
648   Handle(Geom_Curve) C = DrawTrSurf::GetCurve(a[2]);
649   if (C.IsNull()) {di<<"null curve"<<"\n"; return 1;}
650   gp_Pnt p = C->Value(x);
651   di<<"point on curve of parameter "<<x<<" =("<<p.X()<<",";
652   di<<p.Y()<<","<<p.Z()<<")"<<"\n";
653   return 0;
654 }
655
656 static Standard_Integer pntonc2d(Draw_Interpretor& di, Standard_Integer n, const char** a)
657 {
658   if (n < 4) return 1;
659   Standard_Real x = Draw::Atof(a[1]);
660   Handle(Geom2d_Curve) C2d = DrawTrSurf::GetCurve2d(a[2]);
661   if (C2d.IsNull()) {di<<"null curve"<<"\n"; return 1;}
662   Handle(Geom_Surface) S   = DrawTrSurf::GetSurface(a[3]);
663   if (S.IsNull()) {di<<"null surface"<<"\n"; return 1;}
664   gp_Pnt2d p2d = C2d->Value(x);
665   di<<"point on curve of parameter "<<x<<" =("<<p2d.X()<<","<<p2d.Y()<<")"<<"\n";
666   gp_Pnt p = S->Value(p2d.X(),p2d.Y());
667   di<<"point on curve of parameter "<<x<<" =("<<p.X()<<",";
668   di<<p.Y()<<","<<p.Z()<<")"<<"\n";     
669   return 0;                                                       
670 }
671
672 static Standard_Integer projponf(Draw_Interpretor& di, Standard_Integer n, const char** a)
673 {
674   if (n < 3) return 1;
675   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
676   TopoDS_Face f = TopoDS::Face(aLocalShape);
677 //  TopoDS_Face f = TopoDS::Face(DBRep::Get(a[1]));
678   if (f.IsNull()) {di<<"null shape"<<"\n";return 1;}
679   gp_Pnt p; DrawTrSurf::GetPoint(a[2], p);
680   Standard_Real dist=0.; gp_Pnt2d uv; Standard_Boolean ok = FUN_tool_projPonF(p,f,uv,dist);
681   if (!ok) {di<<"projection failed"<<"\n"; return 1;}
682   gp_Pnt pproj; ok = FUN_tool_value(uv,f,pproj);
683   if (!ok) {di<<"projection failed"<<"\n"; return 1;}
684   di<<"proj dist = "<<dist<<" uvproj = ("<<uv.X()<<" "<<uv.Y();
685   di<<"); pproj = ("<<pproj.X()<<" "<<pproj.Y()<<" "<<pproj.Z()<<")"<<"\n";
686   return 0;
687 }  
688
689 static Standard_Integer tolmax(Draw_Interpretor& di, Standard_Integer n, const char** a)
690 {
691   if (n < 2) return 1;
692   TopoDS_Shape s = DBRep::Get(a[1]);
693   if (s.IsNull()) {di<<"null shape"<<"\n"; return 1;}  
694   Standard_Real tol = FUN_tool_maxtol(s);
695   di<<"max tol = "<<tol<<"\n";
696   return 0;
697 }
698
699 static Standard_Integer solidclassifier(Draw_Interpretor& di, Standard_Integer n, const char** a)
700 {
701   if (n < 4) return 1;
702   TopoDS_Shape s = DBRep::Get(a[1]);
703   if (s.IsNull()) {di<<"null shape"<<"\n";return 1;}
704   gp_Pnt p; DrawTrSurf::GetPoint(a[2], p);
705   Standard_Real tol = Draw::Atof(a[3]);
706   
707   TopOpeBRepTool_SolidClassifier soclassi;
708   TopAbs_State sta = TopAbs_UNKNOWN;
709   if      (s.ShapeType() == TopAbs_SOLID) soclassi.Classify(TopoDS::Solid(s),p,tol);
710   else if (s.ShapeType() == TopAbs_SHELL) soclassi.Classify(TopoDS::Shell(s),p,tol);
711   else {di<<"shape is not a solid nor a shell => KO"<<"\n"; return 1;}
712   
713   sta = soclassi.State();
714   di<<"point is ";
715   //TopAbs::Print(sta,cout);
716   Standard_SStream aSStream;
717   TopAbs::Print(sta,aSStream);
718   di << aSStream;
719   di<<" shape s"<<"\n";
720   return 0;
721 }
722
723 static Standard_Integer class3dclassifier(Draw_Interpretor& di, Standard_Integer n, const char** a)
724 {
725   if (n < 4) return 1;
726   TopoDS_Shape s = DBRep::Get(a[1]);
727   if (s.IsNull()) {di<<"null shape"<<"\n";return 1;}
728   gp_Pnt p; DrawTrSurf::GetPoint(a[2], p);
729   Standard_Real tol = Draw::Atof(a[3]);
730   
731   BRepClass3d_SolidClassifier soclassi(s);
732   TopAbs_State sta = TopAbs_UNKNOWN;
733   soclassi.Perform(p,tol);
734   
735   sta = soclassi.State();
736   di<<"point is ";
737   //TopAbs::Print(sta,cout);
738   Standard_SStream aSStream;
739   TopAbs::Print(sta,aSStream);
740   di << aSStream;
741   di<<" shape s"<<"\n";
742   return 0;
743 }
744 static Standard_Integer shapeclassifier(Draw_Interpretor& di, Standard_Integer n, const char** a)
745 {
746   if (n < 3) return 1;
747   TopoDS_Shape sh = DBRep::Get(a[1]);
748   if (sh.IsNull()) {di<<"null shape"<<"\n";return 1;}
749   TopoDS_Shape shref = DBRep::Get(a[2]);
750   if (shref.IsNull()) {di<<"null reference shape"<<"\n";return 1;}
751   Standard_Boolean hastoavoid = (n > 3);
752   TopTools_ListOfShape toavoid;
753   if (hastoavoid) {
754     for (Standard_Integer i=3; i<=n; i++) {
755       TopoDS_Shape shtoavoid = DBRep::Get(a[i]); 
756       if (shtoavoid.IsNull()) {di<<"null toavoid shape"<<"\n";return 1;}    
757       toavoid.Append(shtoavoid);
758     }
759   }
760   
761   TopOpeBRepTool_ShapeClassifier shclassi;
762   TopAbs_State sta = TopAbs_UNKNOWN;
763   if (hastoavoid) sta = shclassi.StateShapeShape(sh,shref);
764   else if (toavoid.Extent() == 1) sta = shclassi.StateShapeShape(sh,toavoid.First(),shref);
765   else sta = shclassi.StateShapeShape(sh,toavoid,shref);
766   
767   di<<"shape is ";
768   //TopAbs::Print(sta,cout);
769   Standard_SStream aSStream;
770   TopAbs::Print(sta,aSStream);
771   di << aSStream;
772   di<<" shape ref"<<"\n";
773   return 0;
774 }
775
776
777 // normals ..
778 static Standard_Integer normal(Draw_Interpretor& di, Standard_Integer n, const char** a)
779 {
780   if (n < 3) return 1;
781   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
782   TopoDS_Face f = TopoDS::Face(aLocalShape);
783 //  TopoDS_Face f = TopoDS::Face(DBRep::Get(a[1]));
784   if (f.IsNull()) {di<<"null shape"<<"\n";return 1;}
785   gp_Pnt p; DrawTrSurf::GetPoint(a[2], p);
786 #ifdef DEB
787   Standard_Real length = Draw::Atof(a[3]);
788 #endif
789   Standard_Real dist=0.; gp_Pnt2d uv; Standard_Boolean ok = FUN_tool_projPonF(p,f,uv,dist);
790   if (!ok) {di<<"projection failed"<<"\n"; return 1;}
791 #ifdef DEB
792   gp_Vec ngf =
793 #endif
794                FUN_tool_nggeomF(uv,f);
795   TCollection_AsciiString aa("ngS"); 
796 #ifdef DRAW
797   FUN_tool_draw(aa,p,ngf,length);
798 #endif
799   return 0;
800 }  
801
802 static Standard_Integer curvature(Draw_Interpretor& di, Standard_Integer n, const char** a)
803 {
804   if (n < 5) return 1;
805   TopoDS_Shape aLocalShape = DBRep::Get(a[1]);
806   TopoDS_Face f = TopoDS::Face(aLocalShape);
807 //  TopoDS_Face f = TopoDS::Face(DBRep::Get(a[1]));
808   if (f.IsNull()) {di<<"null shape"<<"\n";return 1;}
809   Standard_Real x = Draw::Atof(a[2]);
810   Standard_Real y = Draw::Atof(a[3]);
811   Standard_Real z = Draw::Atof(a[4]);
812   Handle(Geom_Line) line = new Geom_Line(gp_Ax1(gp_Pnt(0.,0.,0.), gp_Dir(x,y,z)));
813   BRepAdaptor_Surface BS(f);
814   Handle(Geom_Surface) su = BRep_Tool::Surface(f);
815   GeomAPI_IntCS intcs(line,su);
816   Standard_Boolean done = intcs.IsDone();
817   if (!done) {di<<"intersection point on surface not found"<<"\n"; return 1;}
818   Standard_Integer npnts = intcs.NbPoints();
819   if (npnts < 1) {di<<"intersection point on surface not found"<<"\n"; return 1;}
820   
821   Standard_Real tol = Precision::Confusion();
822   BRepLProp_SLProps props(BS,2,tol);
823   Standard_Real Cur1=0., Cur2=0.;
824   gp_Dir Norm,D1,D2;  
825   for (Standard_Integer i = 1; i <= npnts; i++) {
826     gp_Pnt p = intcs.Point(i);
827     Standard_Real u,v,w; intcs.Parameters(i,u,v,w);
828     di<<"point("<<i<<") = { ("<<p.X()<<" "<<p.Y()<<" "<<p.Z()<<"), ("<<u<<" "<<v<<") }"<<"\n"; 
829     props.SetParameters(u,v);
830     Standard_Boolean curdef = props.IsCurvatureDefined();
831     if (!curdef) {di<<"!IsCurvatureDefined"<<"\n"; continue;}
832     Standard_Boolean umbilic = props.IsUmbilic();
833     if (umbilic) { 
834       Cur1 = Cur2 = props.MeanCurvature();
835       D1 = gp_Dir(0,0,1);
836       D2 = gp_Dir(0,1,0);   
837       di<<"umbilic";
838     }
839     else {
840       Cur1 = props.MaxCurvature();
841       Cur2 = props.MinCurvature();
842       props.CurvatureDirections(D1,D2);
843     }
844     Norm = gp_Dir(D1^D2); 
845     di<<"D1  = ("<<D1.X()<<" "<<D1.Y()<<" "<<D1.Z()<<" "<<")"<<"\n";
846     di<<"D2  = ("<<D2.X()<<" "<<D2.Y()<<" "<<D2.Z()<<" "<<")"<<"\n";
847     di<<"Norm  = ("<<Norm.X()<<" "<<Norm.Y()<<" "<<Norm.Z()<<" "<<")"<<"\n";
848   } // i
849   return 0;
850 }
851
852 void TestTopOpe::CORCommands(Draw_Interpretor& theCommands)
853 {
854   static Standard_Boolean done = Standard_False;
855   if (done) return; done = Standard_True;
856   const char* g = "TestTopOpe CORCommands";
857   // purge, regularization commands :
858   theCommands.Add("purge",   "purge f",                  __FILE__, purge, g);
859   theCommands.Add("corrISO", "corrISO f Fsp",            __FILE__, correctONISO, g);
860   theCommands.Add("regufa",  "regufa f",                 __FILE__, regularize, g);
861   theCommands.Add("splitf",  "splitf f",                 __FILE__, splitF, g);
862   theCommands.Add("regush",  "regush so",                __FILE__, regush, g);
863   theCommands.Add("reguso",  "reguso so",                __FILE__, reguso, g);
864   // builder commands :
865   theCommands.Add("soclass",  "soclass sh pnt tol",         __FILE__, solidclassifier, g);
866   theCommands.Add("shclass",  "shclass sh shref <toavoid>", __FILE__, shapeclassifier, g);
867   theCommands.Add("clclass",  "clclass sh shref <toavoid>", __FILE__, class3dclassifier, g);
868   
869   // useful commands :
870   theCommands.Add("cled",       "cled ed f",                __FILE__, isclosingE, g);
871   theCommands.Add("compare",    "compare s1 s2",            __FILE__, compareshsh, g);
872   theCommands.Add("edonfa",     "edonfa ed f",              __FILE__, pcurveedgeonface, g);
873   theCommands.Add("pconfa",     "pconfa name s f",          __FILE__, pcurvesonface, g);
874   theCommands.Add("orivine",    "orivine v ed",             __FILE__, orivine, g);
875   theCommands.Add("vine",       "vine v ed fa",             __FILE__, vine, g);
876   theCommands.Add("issubsh",    "issubsh subsh sh",         __FILE__, issubshape, g);
877   theCommands.Add("bnd2d",      "bnd2d name W F i",         __FILE__, drawbnd2d, g);
878   theCommands.Add("classibnd2d","classibnd2d W1 W2 F i",    __FILE__, classifBnd2d, g);
879   theCommands.Add("pntonc",     "pntonc par C3d",           __FILE__, pntonc, g);
880   theCommands.Add("pntonc2d",   "pntonc2d par C2d S",       __FILE__, pntonc2d, g);
881   theCommands.Add("projponf",   "projponf f pnt",           __FILE__, projponf, g);
882   theCommands.Add("tolmax",     "tolmax s",                 __FILE__, tolmax, g);
883   theCommands.Add("normal",     "normal f p3d length",      __FILE__, normal, g);
884   theCommands.Add("curvature",  "curvature f x y z",        __FILE__, curvature , g);
885   
886 }