0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / BRepTest / BRepTest_BasicCommands.cxx
1 // Created on: 1994-12-13
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Standard_Stream.hxx>
18 #include <Standard_Macro.hxx>
19
20 #include <BRepTest.hxx>
21
22 #include <DBRep.hxx>
23 #include <Draw_Appli.hxx>
24 #include <Draw_Interpretor.hxx>
25 #include <Draw_Box.hxx>
26
27 #include <BRepBuilderAPI.hxx>
28 #include <BRepBuilderAPI_FindPlane.hxx>
29 #include <BRepBuilderAPI_Copy.hxx>
30 #include <BRepBuilderAPI_Transform.hxx>
31 #include <BRepBuilderAPI_GTransform.hxx>
32 #include <BRepBuilderAPI_NurbsConvert.hxx>
33 #include <gp_Ax2.hxx>
34 #include <gp_Mat.hxx>
35 #include <gp_GTrsf.hxx>
36 #include <BRepOffsetAPI_NormalProjection.hxx>
37 #include <BRepLib.hxx>
38 #include <BRep_Builder.hxx>
39 #include <BRepBndLib.hxx>
40 #include <Bnd_Box.hxx>
41 #include <Bnd_Box2d.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopoDS.hxx>
44 #include <BRepTools_WireExplorer.hxx>
45
46 #include <GCPnts_QuasiUniformAbscissa.hxx>
47 #include <Geom2dAdaptor_Curve.hxx>
48 #include <GeomAdaptor_Curve.hxx>
49 #include <ProjLib_ComputeApproxOnPolarSurface.hxx>
50 #include <DrawTrSurf.hxx>
51 #include <Geom_Plane.hxx>
52
53 #include <Draw_Segment3D.hxx>
54 #include <Draw_Marker3D.hxx>
55 #include <Draw_MarkerShape.hxx>
56
57 #include <stdio.h>
58
59 Standard_IMPORT Draw_Viewer dout;
60
61
62
63 //=======================================================================
64 // addpcurve
65 //=======================================================================
66
67 static Standard_Integer addpcurve(Draw_Interpretor& , Standard_Integer n, const char** a)
68 {
69   if(n < 4) return 1;
70   TopoDS_Shape E = DBRep::Get(a[1]);
71   if (E.IsNull()) return 1;
72   Handle(Geom2d_Curve) PC = DrawTrSurf::GetCurve2d(a[2]);
73   TopoDS_Shape F = DBRep::Get(a[3]);
74   Standard_Real tol = 1.e-7;
75   if (n > 4) {
76     tol = Draw::Atof(a[4]);
77   }
78   BRep_Builder BB;
79   BB.UpdateEdge(TopoDS::Edge(E), PC, TopoDS::Face(F),tol); 
80   DBRep::Set(a[1], E);
81   return 0;
82 }
83
84
85 //=======================================================================
86 // transform
87 //=======================================================================
88
89 static Standard_Integer transform(Draw_Interpretor& ,Standard_Integer n,const char** a)
90 {
91   if (n <= 1) return 1;
92
93   gp_Trsf T;
94   Standard_Integer last = n;
95   const char* aName = a[0];
96
97   Standard_Boolean isBasic = Standard_False;
98
99   if (!strcmp(aName,"reset")) {
100   }
101   else {
102     isBasic = (aName[0] == 'b');
103     aName++;
104
105     if (!strcmp(aName,"move")) {
106       if (n < 3) return 1;
107       TopoDS_Shape SL = DBRep::Get(a[n-1]);
108       if (SL.IsNull()) return 0;
109       T = SL.Location().Transformation();
110       last = n-1;
111     }
112     else if (!strcmp(aName,"translate")) {
113       if (n < 5) return 1;
114       T.SetTranslation(gp_Vec(Draw::Atof(a[n-3]),Draw::Atof(a[n-2]),Draw::Atof(a[n-1])));
115       last = n-3;
116     }
117     else if (!strcmp(aName,"rotate")) {
118       if (n < 9) return 1;
119       T.SetRotation(gp_Ax1(gp_Pnt(Draw::Atof(a[n-7]),Draw::Atof(a[n-6]),Draw::Atof(a[n-5])),
120                     gp_Vec(Draw::Atof(a[n-4]),Draw::Atof(a[n-3]),Draw::Atof(a[n-2]))),
121                     Draw::Atof(a[n-1])* (M_PI / 180.0));
122       last = n-7;
123     }
124     else if (!strcmp(aName,"mirror")) {
125       if (n < 8) return 1;
126       T.SetMirror(gp_Ax2(gp_Pnt(Draw::Atof(a[n-6]),Draw::Atof(a[n-5]),Draw::Atof(a[n-4])),
127                   gp_Vec(Draw::Atof(a[n-3]),Draw::Atof(a[n-2]),Draw::Atof(a[n-1]))));
128       last = n-6;
129     }
130     else if (!strcmp(aName,"scale")) {
131       if (n < 6) return 1;
132       T.SetScale(gp_Pnt(Draw::Atof(a[n-4]),Draw::Atof(a[n-3]),Draw::Atof(a[n-2])),Draw::Atof(a[n-1]));
133       last = n-4;
134     }
135   }
136
137   if (T.Form() == gp_Identity || isBasic) {
138     TopLoc_Location L(T);
139     for (Standard_Integer i = 1; i < last; i++) {
140       TopoDS_Shape S = DBRep::Get(a[i]);
141       if (S.IsNull())
142       {
143         std::cerr << "Error: " << a[i] << " is not a valid shape\n";
144         return 1;
145       }
146       else
147         DBRep::Set(a[i],S.Located(L));
148     }
149   }
150   else {
151     BRepBuilderAPI_Transform trf(T);
152     for (Standard_Integer i = 1; i < last; i++) {
153       TopoDS_Shape S = DBRep::Get(a[i]);
154       if (S.IsNull()) {
155         std::cerr << "Error: " << a[i] << " is not a valid shape\n";
156         return 1;
157       }
158       else {
159         trf.Perform(S);
160         if (!trf.IsDone())
161           return 1;
162         DBRep::Set(a[i],trf.Shape());
163       }
164     }
165   }
166   return 0;
167 }
168
169 ///=======================================================================
170 // gtransform
171 //=======================================================================
172
173 static Standard_Integer deform(Draw_Interpretor& di,Standard_Integer n,const char** a)
174 {
175   if (n <= 1) return 1;
176   
177   Standard_Integer last = n;
178   
179   gp_Trsf T;
180   gp_GTrsf GT(T);
181   
182 //  gp_Mat rot(Draw::Atof(a[last-3]),0,0,0,Draw::Atof(a[last-2]),0,0,0,Draw::Atof(a[last-1]));
183   gp_Mat rot(Draw::Atof(a[3]),0,0,0,Draw::Atof(a[4]),0,0,0,Draw::Atof(a[5]));
184   GT.SetVectorialPart(rot);
185   last -= 3;
186   BRepBuilderAPI_GTransform gtrf(GT);
187   BRepBuilderAPI_NurbsConvert nbscv;
188   //  for (Standard_Integer i = 1; i < last; i++) {
189   //    TopoDS_Shape S = DBRep::Get(a[i]);
190   TopoDS_Shape S = DBRep::Get(a[2]);    
191   if (S.IsNull()) {
192     //cout << a[2] << " is not a valid shape" << endl;
193     di << a[2] << " is not a valid shape\n";
194   }
195   else {
196     gtrf.Perform(S);
197     if (gtrf.IsDone()){
198       DBRep::Set(a[1],gtrf.Shape());
199     }
200     else {
201       return 1;
202     }
203   }
204   
205   return 0;
206 }
207
208 //=======================================================================
209 // tcopy
210 //=======================================================================
211
212 static Standard_Integer tcopy(Draw_Interpretor& di,Standard_Integer n,const char** a)
213 {
214   Standard_Boolean copyGeom = Standard_True;
215   Standard_Boolean copyMesh = Standard_False;
216   Standard_Integer iFirst = 1; // index of first shape argument
217
218   if (n > 1)
219   {
220     for (Standard_Integer i = 1; i <= 2; i++)
221     {
222       if (a[i][0] != '-')
223         break;
224       if (a[i][1] == 'n')
225       {
226         copyGeom = Standard_False;
227         iFirst++;
228       }
229       else if (a[i][1] == 'm')
230       {
231         copyMesh = Standard_True;
232         iFirst++;
233       }
234     }
235   }
236
237   if (n < 3 || (n - iFirst) % 2) {
238     cout << "Use: " << a[0] << " [-n(ogeom)] [-m(esh)] shape1 copy1 [shape2 copy2 [...]]" << endl;
239     cout << "Option -n forbids copying of geometry (it will be shared)" << endl;
240     cout << "Option -m forces copying of mesh (disabled by default)" << endl;
241     return 1;
242   }
243
244   BRepBuilderAPI_Copy cop;
245   Standard_Integer nbPairs = (n - iFirst) / 2;
246   for (Standard_Integer i=0; i < nbPairs; i++) {
247     cop.Perform(DBRep::Get(a[i+iFirst]), copyGeom, copyMesh);
248     DBRep::Set(a[i+iFirst+1],cop.Shape());
249     di << a[i+iFirst+1] << " ";
250   }
251   return 0;
252 }
253
254
255 //=======================================================================
256 // NurbsConvert
257 //=======================================================================
258
259 static Standard_Integer nurbsconvert(Draw_Interpretor& di,Standard_Integer n,const char** a)
260 {
261   if (n < 3) return 1;
262   if ((n-1)%2 != 0) return 1;
263   BRepBuilderAPI_NurbsConvert nbscv;
264   for (Standard_Integer i=0; i<(n-1)/2; i++) {
265     TopoDS_Shape S = DBRep::Get(a[2*i+2]);
266     if (S.IsNull()) {
267       //cout << a[2*i+2] << " is not a valid shape" << endl;
268       di << a[2*i+2] << " is not a valid shape\n";
269     }
270     else {
271       nbscv.Perform(S);
272       if (nbscv.IsDone()){
273         DBRep::Set(a[2*i+1],nbscv.Shape());
274       }
275       else {
276         return 1;
277       }
278     }
279   }
280   
281   return 0;
282   
283 }
284
285 //=======================================================================
286 // make a 3D edge curve
287 //=======================================================================
288
289 static Standard_Integer mkedgecurve (Draw_Interpretor& ,Standard_Integer n,const char** a)
290 {
291
292   if (n < 3) return 1;
293   Standard_Real Tolerance = Draw::Atof(a[2]) ;
294
295   TopoDS_Shape S = DBRep::Get(a[1]);
296   
297   if (S.IsNull()) return 1;
298   
299    BRepLib::BuildCurves3d(S,
300                           Tolerance) ;
301    return 0 ;
302 }
303
304 //=======================================================================
305 // sameparameter
306 //=======================================================================
307
308 static Standard_Integer sameparameter(Draw_Interpretor& ,Standard_Integer n,const char** a)
309 {
310   if (n < 2) return 1;
311   Standard_Real tol = 1.e-7;
312   TopoDS_Shape S = DBRep::Get(a[1]);
313   if (S.IsNull()) return 1;
314   Standard_Boolean force  = !strcmp(a[0],"fsameparameter");
315   if (n == 3) tol = Draw::Atof(a[2]);
316
317   BRepLib::SameParameter(S,tol,force);
318
319   DBRep::Set(a[1],S);
320   return 0;
321 }
322 //=======================================================================
323 //function : updatetol
324 //purpose  : 
325 //=======================================================================
326 static Standard_Integer updatetol(Draw_Interpretor& ,Standard_Integer n,const char** a)
327 {
328   if (n < 2) return 1;
329
330   TopoDS_Shape S = DBRep::Get(a[1]);
331   if (S.IsNull()) return 1;
332
333   if (n==2) BRepLib::UpdateTolerances(S);
334   else BRepLib::UpdateTolerances(S,Standard_True);
335   DBRep::Set(a[1],S);
336   return 0;
337
338 }
339
340 //=======================================================================
341 //function : OrienSolid
342 //purpose  : 
343 //=======================================================================
344 static Standard_Integer orientsolid(Draw_Interpretor& ,Standard_Integer n,const char** a)
345 {
346   if (n < 2) return 1;
347
348   TopoDS_Shape S = DBRep::Get(a[1]);
349   if (S.IsNull()) return 1;
350   if (S.ShapeType()!=TopAbs_SOLID) return 1;
351
352   BRepLib::OrientClosedSolid(TopoDS::Solid(S));
353
354   DBRep::Set(a[1],S);
355   return 0;
356
357 }
358
359 //=======================================================================
360 //function : boundingstr
361 //purpose  : 
362 //=======================================================================
363 static Standard_Integer boundingstr(Draw_Interpretor& di,Standard_Integer n,const char** a)
364 {
365   if (n < 2) return 1;
366   TopoDS_Shape S = DBRep::Get(a[1]);
367   if (S.IsNull()) return 1;
368   Bnd_Box B;
369   BRepBndLib::Add(S,B);
370   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
371   B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
372   di << axmin<<" "<< aymin<<" "<< azmin<<" "<< axmax<<" "<< aymax<<" "<< azmax;
373   if (n >= 8) {
374     Draw::Set(a[2],axmin) ;
375     Draw::Set(a[3],aymin) ;
376     Draw::Set(a[4],azmin) ;
377     Draw::Set(a[5],axmax) ;
378     Draw::Set(a[6],aymax) ;
379     Draw::Set(a[7],azmax) ;
380   }
381   return 0;
382 }
383
384 //=======================================================================
385 //function : getcoords
386 //purpose  : 
387 //=======================================================================
388 static Standard_Integer getcoords(Draw_Interpretor& di,Standard_Integer n,const char** a)
389 {
390   if(n < 2) 
391     return 1;
392
393   for (Standard_Integer i = 1; i < n; i++) 
394   {
395     const TopoDS_Shape aShape = DBRep::Get (a[i]);
396
397     if (aShape.IsNull())
398       continue;
399
400     if (aShape.ShapeType() == TopAbs_VERTEX)
401     {
402       const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
403       gp_Pnt aPnt = BRep_Tool::Pnt(aVertex);
404
405       di << a[i] << " (x,y,z) : " << aPnt.X() << " " << aPnt.Y() << " " << aPnt.Z() << "\n";
406     }
407   }
408
409   return 0;
410 }
411
412 //=======================================================================
413 //function : bounding
414 //purpose  : 
415 //=======================================================================
416 static Standard_Integer bounding(Draw_Interpretor& di,Standard_Integer n,const char** a)
417 {
418   if (n < 2) return 1;
419   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
420   Bnd_Box B; Handle(Draw_Box) DB;
421   
422   if (n == 2) { 
423     TopoDS_Shape S = DBRep::Get(a[1]);
424     if (S.IsNull()) return 1;
425     BRepBndLib::Add(S,B);
426     B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
427     DB = new Draw_Box(gp_Pnt(axmin,aymin,azmin),gp_Pnt(axmax,aymax,azmax),Draw_orange);
428     dout<<DB;
429     di << axmin<<" "<< aymin<<" "<< azmin<<" "<< axmax<<" "<< aymax<<" "<< azmax;
430   }
431   else if (n == 7) {
432     axmin=Draw::Atof(a[1]);
433     aymin=Draw::Atof(a[2]);
434     azmin=Draw::Atof(a[3]);
435     axmax=Draw::Atof(a[4]);
436     aymax=Draw::Atof(a[5]);
437     azmax=Draw::Atof(a[6]);
438     DB = new Draw_Box(gp_Pnt(axmin,aymin,azmin),gp_Pnt(axmax,aymax,azmax),Draw_orange);
439     dout<<DB;
440   }
441   return 0;
442 }
443 //=======================================================================
444 //function : optbounding
445 //purpose  : 
446 //=======================================================================
447 static Standard_Integer optbounding(Draw_Interpretor& di,Standard_Integer n,const char** a)
448 {
449   if (n < 2)
450   {
451     di << "Usage: optbounding shape [usetri [usetol]]\n";
452     di << "usetri and usetol can be 0 or 1, by default usetri = 1, usetol = 0\n";
453     return 1;
454   }
455   Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
456   Bnd_Box B; Handle(Draw_Box) DB;
457   
458   TopoDS_Shape S = DBRep::Get(a[1]);
459   if (S.IsNull()) 
460   {
461     di << "Null shape\n";
462     return 1;
463   }
464   Standard_Boolean useTri = Standard_True;
465   Standard_Boolean useTol = Standard_False;
466   if(n > 2 )
467   {
468     Standard_Integer ii = atoi(a[2]);
469     useTri = ii > 0;
470   }
471   if(n > 3 )
472   {
473     Standard_Integer ii = atoi(a[3]);
474     useTol = ii > 0;
475   }
476   BRepBndLib::AddOptimal(S, B, useTri, useTol);
477   B.Get(axmin, aymin, azmin, axmax, aymax, azmax);
478   DB = new Draw_Box(gp_Pnt(axmin,aymin,azmin),gp_Pnt(axmax,aymax,azmax),Draw_vert);
479   dout<<DB;
480   di << axmin<<" "<< aymin<<" "<< azmin<<" "<< axmax<<" "<< aymax<<" "<< azmax;
481   return 0;
482 }
483 //=======================================================================
484 //function : gbounding
485 //purpose  : 
486 //=======================================================================
487 #include <GeomAdaptor_Surface.hxx>
488 #include <BndLib_AddSurface.hxx>
489 #include <BndLib_Add3dCurve.hxx>
490 #include <BndLib_Add2dCurve.hxx>
491 #include <Draw_Segment2D.hxx>
492 static Standard_Integer gbounding(Draw_Interpretor& di,Standard_Integer n,const char** a)
493 {
494   if (n != 2 && n != 3) 
495   {
496     di << "Usage: gbounding surf/curve/curve2d [-o] \n";
497     di << "[-o] turn on Optimal mode ('off' by default) \n";
498     return 1;
499   }
500   else
501   {
502     Standard_Boolean IsOptimal = Standard_False;
503     if (n == 3 && !strcmp(a[2], "-o"))
504       IsOptimal = Standard_True;
505     
506     Standard_Real axmin,aymin,azmin,axmax,aymax,azmax;
507     Bnd_Box B;
508     Bnd_Box2d B2d;
509     Handle(Draw_Box) DB;
510     Standard_Boolean Is3d = Standard_True;
511     Handle(Geom_Curve) C;
512     Handle(Geom_Surface) S;
513     Handle_Geom2d_Curve C2d;
514     S = DrawTrSurf::GetSurface(a[1]);
515     if (!S.IsNull())
516     {
517       //add surf
518       GeomAdaptor_Surface aGAS(S);
519       if (IsOptimal)
520         BndLib_AddSurface::AddOptimal(aGAS, Precision::Confusion(), B);
521       else
522         BndLib_AddSurface::Add(aGAS, Precision::Confusion(), B);
523     }
524     else
525     {
526       C = DrawTrSurf::GetCurve(a[1]);
527       if (!C.IsNull())
528       {
529         // add cur
530         GeomAdaptor_Curve aGAC(C);
531         if (IsOptimal)
532           BndLib_Add3dCurve::AddOptimal(aGAC, Precision::Confusion(), B);
533         else
534           BndLib_Add3dCurve::Add(aGAC, Precision::Confusion(), B);
535       }
536       else
537       {
538         C2d = DrawTrSurf::GetCurve2d(a[1]);
539         if (!C2d.IsNull())
540         {
541           //add cur2d
542           Is3d = Standard_False;
543           if (IsOptimal)
544             BndLib_Add2dCurve::AddOptimal(C2d, C2d->FirstParameter(), C2d->LastParameter(), Precision::Confusion(), B2d); 
545           else
546             BndLib_Add2dCurve::Add(C2d, C2d->FirstParameter(), C2d->LastParameter(), Precision::Confusion(), B2d); 
547         }
548         else
549         {
550           di << "Wrong argument \n";
551           return 1;
552         }
553       }
554     }
555
556     if (Is3d)
557     {
558       B.Get(axmin,aymin,azmin,axmax,aymax,azmax);
559       DB = new Draw_Box(gp_Pnt(axmin,aymin,azmin),gp_Pnt(axmax,aymax,azmax),Draw_vert);
560       dout<<DB;
561       di << axmin<<" "<< aymin<<" "<< azmin<<" "<< axmax<<" "<< aymax<<" "<< azmax;
562     }
563     else
564     {
565       B2d.Get(axmin,aymin,axmax,aymax);
566       gp_Pnt2d p1(axmin, aymin);
567       gp_Pnt2d p2(axmax, aymin);
568       gp_Pnt2d p3(axmax, aymax);
569       gp_Pnt2d p4(axmin, aymax);
570       Draw_Segment2D* S1 = new Draw_Segment2D(p1, p2, Draw_vert);
571       Draw_Segment2D* S2 = new Draw_Segment2D(p2, p3, Draw_vert);
572       Draw_Segment2D* S3 = new Draw_Segment2D(p3, p4, Draw_vert);
573       Draw_Segment2D* S4 = new Draw_Segment2D(p4, p1, Draw_vert);
574       dout << S1 << S2 << S3 << S4;
575       di << axmin<<" "<< aymin<<" "<< axmax<<" "<< aymax;
576     }
577   }
578   return 0;
579 }
580
581 //=======================================================================
582 //function : findplane
583 //purpose  : 
584 //=======================================================================
585 static Standard_Integer findplane(Draw_Interpretor& di,Standard_Integer n,const char** a)
586 {
587   if (n < 3) return 1;
588   TopoDS_Shape S = DBRep::Get(a[1]);
589   if (S.IsNull()) return 1;
590   Standard_Real tolerance = 1.0e-5 ;
591   BRepBuilderAPI_FindPlane a_plane_finder(S,
592                                    tolerance) ;
593   if (a_plane_finder.Found()) {
594     //cout << " a plane is found "   ;
595     di << " a plane is found \n";
596     const Handle(Geom_Geometry)& aSurf = a_plane_finder.Plane(); // to avoid ambiguity
597     DrawTrSurf::Set(a[2],aSurf) ;
598   }
599   return 0 ;
600 }
601 //=======================================================================
602 //function : precision
603 //purpose  : 
604 //=======================================================================
605
606 static Standard_Integer precision(Draw_Interpretor& di,Standard_Integer n,const char** a)
607 {
608   n--;
609
610   if ( n == 0) {
611     //cout << " Current Precision = " << BRepBuilderAPI::Precision() << endl;
612     di << " Current Precision = " << BRepBuilderAPI::Precision() << "\n";
613   }
614   else {
615     BRepBuilderAPI::Precision(Draw::Atof(a[1]));
616   }
617   return 0;
618 }
619
620
621 //=======================================================================
622 //function : reperage shape (Int lin Shape) + pointe double click   + maxtol
623 //purpose  : 
624 //=======================================================================
625 #include <IntCurvesFace_ShapeIntersector.hxx>
626 #include <gp_Lin.hxx>
627
628 static Standard_Integer reperageshape(Draw_Interpretor& di, Standard_Integer narg , const char** a) 
629 {
630   Standard_Integer details=0;
631   if(narg<2) return 1;
632   if(narg==3) details=1;
633   const char *id1 = a[1];
634   TopoDS_Shape TheShape1 = DBRep::Get(id1);
635   
636   //cout << "Pick positions with button "<<endl;
637   di << "Pick positions with button \n";
638   Standard_Integer id,X,Y,b;
639   gp_Trsf T;
640   gp_Pnt P1,P2;
641   dout.Select(id,X,Y,b);
642   
643   dout.GetTrsf(id,T);
644   T.Invert();
645   Standard_Real z = dout.Zoom(id);
646   P2.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z, 0.0);
647   P2.Transform(T);
648   P1.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,-1.0);
649   P1.Transform(T);
650   
651   
652   gp_Ax1 Axe(P1,gp_Vec(P1,P2));
653   IntCurvesFace_ShapeIntersector Inter;
654   Inter.Load(TheShape1,1e-7);
655   
656   Inter.Perform(Axe,-RealLast(),RealLast());
657   
658   //cout<<"\n --> ";
659   di <<"\n --> ";
660   if(Inter.NbPnt()) { 
661     for(Standard_Integer i=1; i<=Inter.NbPnt(); i++) { 
662       Standard_Integer numface=1;
663       TopExp_Explorer ExF;
664       for(ExF.Init(TheShape1,TopAbs_FACE);
665           ExF.More();
666           ExF.Next(),numface++) { 
667         TopoDS_Face Face=TopoDS::Face(ExF.Current());
668         if(Face.IsEqual(Inter.Face(i))) { 
669           //cout<<" "<<a[1]<<"_"<<numface;
670           di<<" "<<a[1]<<"_"<<numface;
671           continue;       
672         }
673       }
674       const gp_Pnt& P = Inter.Pnt(i);
675       Standard_Real PMin = Inter.WParameter(i);
676       if(details) { 
677         //cout<<" w:"<<PMin<<endl;
678         di<<" w:"<<PMin<< "\n";
679       }
680       if(Inter.Transition(i) == IntCurveSurface_In) { 
681         if(Inter.State(i) == TopAbs_IN) { 
682           Handle(Draw_Marker3D) p = new Draw_Marker3D(P, Draw_Square, Draw_rouge,2); 
683           dout << p;   dout.Flush();
684         }
685         else if(Inter.State(i) == TopAbs_ON) { 
686           Handle(Draw_Marker3D) p = new Draw_Marker3D(P, Draw_Square, Draw_vert,2); 
687           dout << p;   dout.Flush();
688         }
689       }
690       else { 
691         if(Inter.Transition(i) == IntCurveSurface_Out) { 
692           if(Inter.State(i) == TopAbs_IN) { 
693             Handle(Draw_Marker3D) p = new Draw_Marker3D(P, Draw_X, Draw_rouge,2); 
694             dout << p;   dout.Flush();
695           }
696           else if(Inter.State(i) == TopAbs_ON) { 
697             Handle(Draw_Marker3D) p = new Draw_Marker3D(P, Draw_X, Draw_vert,2); 
698             dout << p;   dout.Flush();
699           }
700         } 
701       }
702     }
703   }
704   //cout<<endl;
705   di << "\n";
706   return(0);
707 }
708
709
710 static Standard_Integer maxtolerance(Draw_Interpretor& theCommands, 
711                                      Standard_Integer n, const char** a) { 
712   if(n<2) return(1);
713   TopoDS_Shape TheShape = DBRep::Get(a[1]);
714   if(TheShape.IsNull()) return(1);
715
716   Standard_Real T,TMF,TME,TMV,TmF,TmE,TmV;
717   Standard_Integer nbF,nbE,nbV;
718   TMF=TME=TMV=-RealLast();
719   TmF=TmE=TmV=RealLast();
720   
721   TopTools_MapOfShape mapS;
722   mapS.Clear();
723
724   for(TopExp_Explorer ex(TheShape,TopAbs_FACE);
725       ex.More();
726       ex.Next()) { 
727     TopoDS_Face Face=TopoDS::Face(ex.Current());
728     T=BRep_Tool::Tolerance(Face);
729     if(T>TMF) TMF=T;
730     if(T<TmF) TmF=T;
731     mapS.Add(Face);
732   }
733   
734   nbF = mapS.Extent();
735   mapS.Clear();
736   
737   for(TopExp_Explorer ex(TheShape,TopAbs_EDGE);
738       ex.More();
739       ex.Next()) { 
740     TopoDS_Edge Edge=TopoDS::Edge(ex.Current());
741     T=BRep_Tool::Tolerance(Edge);
742     if(T>TME) TME=T;
743     if(T<TmE) TmE=T;
744     mapS.Add(Edge);
745   }
746
747   nbE = mapS.Extent();
748   mapS.Clear();
749
750   for(TopExp_Explorer ex(TheShape,TopAbs_VERTEX);
751       ex.More();
752       ex.Next()) { 
753     TopoDS_Vertex Vertex=TopoDS::Vertex(ex.Current());
754     T=BRep_Tool::Tolerance(Vertex);
755     if(T>TMV) TMV=T;
756     if(T<TmV) TmV=T;
757     mapS.Add(Vertex);
758   }
759
760   nbV = mapS.Extent();
761
762   Standard_SStream sss;
763   sss << "\n## Tolerances on the shape " << a[1] << "  (nbFaces:" << nbF
764       << "  nbEdges:" << nbE << " nbVtx:" << nbV << ")\n" ;
765   sss.precision(5);
766   sss.setf(ios::scientific);
767   if(TmF<=TMF) sss << "\n    Face   : Min " << setw(8) << TmF <<"    Max  " << setw(8) << TMF << " \n ";
768   if(TmE<=TME) sss << "\n    Edge   : Min " << setw(8) << TmE <<"    Max  " << setw(8) << TME << " \n ";
769   if(TmV<=TMV) sss << "\n    Vertex : Min " << setw(8) << TmV <<"    Max  " << setw(8) << TMV << " \n ";
770   theCommands << sss;
771
772   return 0;
773 }
774
775
776 static Standard_Integer vecdc(Draw_Interpretor& di,Standard_Integer ,const char** ) {
777   //cout << "Pick positions with button "<<endl;
778   di << "Pick positions with button \n";
779
780   Standard_Integer id,X,Y,b;
781   gp_Trsf T;
782   gp_Pnt P1,P2,PP1,PP2;
783   
784   //-----------------------------------------------------------
785   dout.Select(id,X,Y,b);    dout.GetTrsf(id,T);
786   T.Invert();
787   Standard_Real z = dout.Zoom(id);
788   P1.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
789   P1.Transform(T);
790   
791   dout.Select(id,X,Y,b);  dout.GetTrsf(id,T);
792   T.Invert();  z = dout.Zoom(id);
793   
794   P2.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
795   P2.Transform(T);
796   Standard_Real xa,ya,za;
797   if(Abs(P1.X())>Abs(P2.X())) xa = P1.X(); else xa = P2.X();
798   if(Abs(P1.Y())>Abs(P2.Y())) ya = P1.Y(); else ya = P2.Y();
799   if(Abs(P1.Z())>Abs(P2.Z())) za = P1.Z(); else za = P2.Z();
800   P1.SetCoord(xa,ya,za);
801   Handle(Draw_Marker3D) D0 = new Draw_Marker3D(gp_Pnt(P1.X(),
802                                                       P1.Y(),
803                                                       P1.Z()),
804                                                Draw_Square,Draw_blanc,1);
805   
806   dout << D0;
807   dout.Flush();
808   //-----------------------------------------------------------
809   dout.Select(id,X,Y,b);  
810   dout.GetTrsf(id,T);
811   T.Invert();
812   z = dout.Zoom(id);
813   PP1.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
814   PP1.Transform(T);
815   dout.Select(id,X,Y,b);
816   dout.GetTrsf(id,T);
817   T.Invert();
818   z = dout.Zoom(id);
819   PP2.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
820   PP2.Transform(T);
821   if(Abs(PP1.X())>Abs(PP2.X())) xa = PP1.X(); else xa = PP2.X();
822   if(Abs(PP1.Y())>Abs(PP2.Y())) ya = PP1.Y(); else ya = PP2.Y();
823   if(Abs(PP1.Z())>Abs(PP2.Z())) za = PP1.Z(); else za = PP2.Z();
824   PP1.SetCoord(xa,ya,za);
825   Handle(Draw_Segment3D) d = new Draw_Segment3D(P1,PP1,Draw_blanc);
826   dout << d;
827   dout.Flush();
828   //cout<<"\nttran   "<<PP1.X()-P1.X()<<" "<<PP1.Y()-P1.Y()<<" "<<PP1.Z()-P1.Z()<<endl;
829   di <<"\nttran   "<<PP1.X()-P1.X()<<" "<<PP1.Y()-P1.Y()<<" "<<PP1.Z()-P1.Z()<< "\n";
830
831   static Standard_Integer nboxvecdp=0;
832   //cout<<"\nbox  b"<<++nboxvecdp<<" "<<Min(P1.X(),PP1.X())<<" "<<Min(P1.Y(),PP1.Y())<<" "<<Min(PP1.Z(),P1.Z());
833   //cout<<"  "<<Abs(PP1.X()-P1.X())<<" "<<Abs(PP1.Y()-P1.Y())<<" "<<Abs(PP1.Z()-P1.Z())<<endl;
834
835   //cout<<"\nDistance :"<<sqrt( (PP1.X()-P1.X())*(PP1.X()-P1.X())
836         //                   +(PP1.Y()-P1.Y())*(PP1.Y()-P1.Y())
837         //                   +(PP1.Z()-P1.Z())*(PP1.Z()-P1.Z()))<<endl;
838
839   di <<"\nbox  b"<<++nboxvecdp<<" "<<Min(P1.X(),PP1.X())<<" "<<Min(P1.Y(),PP1.Y())<<" "<<Min(PP1.Z(),P1.Z());
840   di <<"  "<<Abs(PP1.X()-P1.X())<<" "<<Abs(PP1.Y()-P1.Y())<<" "<<Abs(PP1.Z()-P1.Z())<< "\n";
841
842   di <<"\nDistance :"<<sqrt( (PP1.X()-P1.X())*(PP1.X()-P1.X())
843                              +(PP1.Y()-P1.Y())*(PP1.Y()-P1.Y())
844                              +(PP1.Z()-P1.Z())*(PP1.Z()-P1.Z()))<< "\n";
845   return(0);
846 }
847 //=======================================================================
848 // nproject
849 //=======================================================================
850
851 #include <TopTools_SequenceOfShape.hxx>
852  static Standard_Integer nproject(Draw_Interpretor& di, Standard_Integer n, const char** a)
853 {
854   if ( n < 4) return 1;
855   TopoDS_Shape InpShape;
856   Standard_Integer arg = 2, i;
857   TopTools_SequenceOfShape Args; 
858
859   Standard_Real Tol = 1.e-4;        
860   Standard_Real Tol2d;
861   Standard_Real MaxDistance = 1.e-3;
862   GeomAbs_Shape Continuity = GeomAbs_C2;  
863   Standard_Integer MaxDeg = 14;           
864   Standard_Integer MaxSeg = 16;           
865
866   while((n > arg) && !(InpShape = DBRep::Get(a[arg])).IsNull()){
867     Args.Append(InpShape);
868     arg++;
869   }
870   if(Args.Length() < 2) return 1;
871   
872   BRepOffsetAPI_NormalProjection OrtProj(Args.Last());
873
874   for(i = 1; i < Args.Length(); i++)
875     OrtProj.Add(Args(i));
876
877   if(n > arg)
878     if (!strcmp(a[arg],"-g")) {
879       OrtProj.SetLimit(Standard_False);
880       arg++;
881     }
882   
883   if(n > arg)
884     if (!strcmp(a[arg],"-d")) {
885       arg++;
886       if(n > arg)
887         MaxDistance = Draw::Atof(a[arg++]);
888       OrtProj.SetMaxDistance(MaxDistance);
889     }
890   if(n > arg) {
891     Tol = Max(Draw::Atof(a[arg++]),1.e-10);
892   }
893
894   if(n > arg) {
895     if (Draw::Atoi(a[arg]) == 0) Continuity = GeomAbs_C0;
896     else if (Draw::Atoi(a[arg]) == 1) Continuity = GeomAbs_C1;
897     arg++;
898   }
899
900  
901   if(n > arg) {
902     MaxDeg = Draw::Atoi(a[arg++]);
903     if (MaxDeg<1 || MaxDeg>14) MaxDeg = 14;
904   }
905
906   if(n > arg) MaxSeg = Draw::Atoi(a[arg]);
907     
908   Tol2d = Pow(Tol, 2./3);
909
910   OrtProj.SetParams(Tol, Tol2d, Continuity, MaxDeg, MaxSeg);
911   OrtProj.Build();
912   TopTools_ListOfShape Wire;
913   Standard_Boolean IsWire=OrtProj.BuildWire(Wire);
914   if (IsWire) {
915     //cout << " BuildWire OK " << endl;
916     di << " BuildWire OK \n";
917   }
918   DBRep::Set(a[1], OrtProj.Shape());
919   return 0;  
920 }
921
922 //==========================================================================
923 //function : wexplo
924 //           exploration of a wire 
925 //==========================================================================
926 static Standard_Integer wexplo (Draw_Interpretor&, 
927                                 Standard_Integer argc, const char** argv)
928
929   char name[100];
930   if (argc < 2) return 1;
931   
932   TopoDS_Shape C1 = DBRep::Get (argv[1],TopAbs_WIRE);
933   TopoDS_Shape C2 ;
934
935   if (argc > 2)  C2 = DBRep::Get (argv[2],TopAbs_FACE);
936
937   if (C1.IsNull()) return 1;
938
939   BRepTools_WireExplorer we;
940   if (C2.IsNull()) we.Init(TopoDS::Wire(C1));
941   else             we.Init(TopoDS::Wire(C1),TopoDS::Face(C2));
942
943   Standard_Integer k = 1;
944   while (we.More()) {
945     TopoDS_Edge E = we.Current();
946     Sprintf(name,"WEDGE_%d",k); 
947           DBRep::Set(name,E);
948     we.Next();
949     k++;
950   }
951
952   return 0;
953 }
954
955 static Standard_Integer scalexyz(Draw_Interpretor& /*di*/, Standard_Integer n, const char** a)
956 {
957   if (n < 6) return 1;
958
959   TopoDS_Shape aShapeBase = DBRep::Get(a[2]);
960   if (aShapeBase.IsNull()) return 1;
961   
962   Standard_Real aFactorX = Draw::Atof(a[3]);
963   Standard_Real aFactorY = Draw::Atof(a[4]);
964   Standard_Real aFactorZ = Draw::Atof(a[5]);
965
966   gp_GTrsf aGTrsf;
967   gp_Mat rot (aFactorX, 0, 0,
968               0, aFactorY, 0,
969               0, 0, aFactorZ);
970   aGTrsf.SetVectorialPart(rot);
971   BRepBuilderAPI_GTransform aBRepGTrsf (aShapeBase, aGTrsf, Standard_False);
972   if (!aBRepGTrsf.IsDone())
973     throw Standard_ConstructionError("Scaling not done");
974   TopoDS_Shape Result = aBRepGTrsf.Shape();
975
976   DBRep::Set(a[1], Result);
977   return 0;  
978 }
979
980 void  BRepTest::BasicCommands(Draw_Interpretor& theCommands)
981 {
982   static Standard_Boolean done = Standard_False;
983   if (done) return;
984   done = Standard_True;
985
986   DBRep::BasicCommands(theCommands);
987
988   const char* g = "TOPOLOGY Basic shape commands";
989
990   theCommands.Add("addpcurve",
991                   "addpcurve edge 2dcurve face [tol (default 1.e-7)]",
992                   __FILE__,
993                   addpcurve,g);
994
995   theCommands.Add("reset",
996                   "reset name1 name2 ..., remove location",
997                   __FILE__,
998                   transform,g);
999
1000   theCommands.Add("tmove",
1001                   "tmove name1 name2 ... name, set location from name",
1002                   __FILE__,
1003                   transform,g);
1004
1005   theCommands.Add("ttranslate",
1006                   "ttranslate name1 name2 ... dx dy dz",
1007                   __FILE__,
1008                   transform,g);
1009
1010   theCommands.Add("trotate",
1011                   "trotate name1 name2 ... x y z dx dy dz angle",
1012                   __FILE__,
1013                   transform,g);
1014
1015   theCommands.Add("tmirror",
1016                   "tmirror name x y z dx dy dz",
1017                   __FILE__,
1018                   transform,g);
1019
1020   theCommands.Add("tscale",
1021                   "tscale name x y z scale",
1022                   __FILE__,
1023                   transform,g);
1024
1025   theCommands.Add("tcopy",
1026                   "tcopy [-n(ogeom)] [-m(esh)] name1 result1 [name2 result2 ...]",
1027                   __FILE__,
1028                   tcopy,g);
1029
1030   theCommands.Add("bmove",
1031                   "bmove name1 name2 ... name, set location from name",
1032                   __FILE__,
1033                   transform,g);
1034
1035   theCommands.Add("btranslate",
1036                   "btranslate name1 name2 ... dx dy dz",
1037                   __FILE__,
1038                   transform,g);
1039
1040   theCommands.Add("brotate",
1041                   "brotate name1 name2 ... x y z dx dy dz angle",
1042                   __FILE__,
1043                   transform,g);
1044
1045   theCommands.Add("bmirror",
1046                   "bmirror name x y z dx dy dz",
1047                   __FILE__,
1048                   transform,g);
1049
1050   theCommands.Add("bscale",
1051                   "bscale name x y z scale",
1052                   __FILE__,
1053                   transform,g);
1054
1055   theCommands.Add("precision",
1056                   "precision [preci]",
1057                   __FILE__,
1058                   precision,g);
1059
1060   theCommands.Add("mkedgecurve",
1061                   "mkedgecurve name tolerance",
1062                   __FILE__,
1063                   mkedgecurve,g);
1064
1065   theCommands.Add("fsameparameter",
1066                   "fsameparameter shapename [tol (default 1.e-7)], \nforce sameparameter on all edges of the shape",
1067                   __FILE__,
1068                   sameparameter,g);
1069
1070   theCommands.Add("sameparameter",
1071                   "sameparameter shapename [tol (default 1.e-7)]",
1072                   __FILE__,
1073                   sameparameter,g);
1074
1075   theCommands.Add("updatetolerance",
1076                   "updatetolerance myShape [param] \n  if [param] is absent - not verify of face tolerance, else - perform it",
1077                   __FILE__,
1078                   updatetol,g);
1079
1080   theCommands.Add("solidorientation",
1081                   "orientsolid myClosedSolid",
1082                   __FILE__,
1083                   orientsolid,g);
1084
1085   theCommands.Add("getcoords",
1086     "getcoords vertex1 vertex 2... ; shows coords of input vertices",
1087     __FILE__,
1088     getcoords,g);
1089   
1090   theCommands.Add("bounding",
1091                   "bounding shape [ xmin ymin zmin xmax ymax zmax] ; draw bounds",
1092                   __FILE__,
1093                   bounding,g);
1094
1095   theCommands.Add("optbounding",
1096                   "optbounding shape [usetri (0/1) [usetol (0/1)]] ; ",
1097                   __FILE__,
1098                   optbounding,g);
1099  //
1100   theCommands.Add("gbounding",
1101                   "gbounding surf/curve/curve2d [-o] ",
1102                   __FILE__,
1103                   gbounding,g);
1104
1105   theCommands.Add("boundingstr",
1106                   "boundingstr shape [ xmin ymin zmin xmax ymax zmax] ; print bounding box",
1107                   __FILE__,
1108                   boundingstr,g);
1109
1110   theCommands.Add("nurbsconvert",
1111                   "nurbsconvert result name [result name]",
1112                   __FILE__,
1113                   nurbsconvert,g);
1114
1115   theCommands.Add("deform",
1116                   "deform newname name CoeffX CoeffY CoeffZ",
1117                   __FILE__,
1118                   deform,g);
1119   
1120   theCommands.Add("findplane",
1121                   "findplane name planename ",
1122                   __FILE__,
1123                   findplane,g) ;
1124   
1125   theCommands.Add("maxtolerance",
1126                   "maxtolerance shape ",
1127                   __FILE__,
1128                   maxtolerance,g) ;
1129
1130   theCommands.Add("reperageshape",
1131                   "reperage shape -> list of shape (result of interstion shape , line)",
1132                   __FILE__,
1133                   reperageshape,g) ;
1134
1135   theCommands.Add("vecdc",
1136                   "vecdc + Pointe double click ",
1137                   __FILE__,
1138                   vecdc,g) ;
1139
1140   theCommands.Add("nproject","nproject pj e1 e2 e3 ... surf -g -d [dmax] [Tol [continuity [maxdeg [maxseg]]]",
1141                   __FILE__,
1142                   nproject,g);
1143
1144   theCommands.Add("wexplo","wexplo wire [face] create WEDGE_i",
1145                   __FILE__,
1146                   wexplo,g);
1147
1148   theCommands.Add("scalexyz",
1149                   "scalexyz res shape factor_x factor_y factor_z",
1150                   __FILE__,
1151                   scalexyz, g);
1152 }