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