6feb10d0352b3e0cd15206833928cc6deebfcc1f
[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 //#ifndef WNT
624   Standard_SStream sss;
625   sss << "\n## Tolerances on the shape " << a[1] << "  (nbFaces:" << nbF
626       << "  nbEdges:" << nbE << " nbVtx:" << nbV << ")\n" ;
627   sss.precision(5);
628   sss.setf(ios::scientific);
629   if(TmF<=TMF) sss << "\n    Face   : Min " << setw(8) << TmF <<"    Max  " << setw(8) << TMF << " \n ";
630   if(TmE<=TME) sss << "\n    Edge   : Min " << setw(8) << TmE <<"    Max  " << setw(8) << TME << " \n ";
631   if(TmV<=TMV) sss << "\n    Vertex : Min " << setw(8) << TmV <<"    Max  " << setw(8) << TMV << " \n ";
632   theCommands << sss;
633   //#endif*/
634   return 0;
635 }
636
637
638 static Standard_Integer vecdc(Draw_Interpretor& di,Standard_Integer ,const char** ) {
639   //cout << "Pick positions with button "<<endl;
640   di << "Pick positions with button "<< "\n";
641
642   Standard_Integer id,X,Y,b;
643   gp_Trsf T;
644   gp_Pnt P1,P2,PP1,PP2;
645   
646   //-----------------------------------------------------------
647   dout.Select(id,X,Y,b);    dout.GetTrsf(id,T);
648   T.Invert();
649   Standard_Real z = dout.Zoom(id);
650   P1.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
651   P1.Transform(T);
652   
653   dout.Select(id,X,Y,b);  dout.GetTrsf(id,T);
654   T.Invert();  z = dout.Zoom(id);
655   
656   P2.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
657   P2.Transform(T);
658   Standard_Real xa,ya,za;
659   if(Abs(P1.X())>Abs(P2.X())) xa = P1.X(); else xa = P2.X();
660   if(Abs(P1.Y())>Abs(P2.Y())) ya = P1.Y(); else ya = P2.Y();
661   if(Abs(P1.Z())>Abs(P2.Z())) za = P1.Z(); else za = P2.Z();
662   P1.SetCoord(xa,ya,za);
663   Handle(Draw_Marker3D) D0 = new Draw_Marker3D(gp_Pnt(P1.X(),
664                                                       P1.Y(),
665                                                       P1.Z()),
666                                                Draw_Square,Draw_blanc,1);
667   
668   dout << D0;
669   dout.Flush();
670   //-----------------------------------------------------------
671   dout.Select(id,X,Y,b);  
672   dout.GetTrsf(id,T);
673   T.Invert();
674   z = dout.Zoom(id);
675   PP1.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
676   PP1.Transform(T);
677   dout.Select(id,X,Y,b);
678   dout.GetTrsf(id,T);
679   T.Invert();
680   z = dout.Zoom(id);
681   PP2.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
682   PP2.Transform(T);
683   if(Abs(PP1.X())>Abs(PP2.X())) xa = PP1.X(); else xa = PP2.X();
684   if(Abs(PP1.Y())>Abs(PP2.Y())) ya = PP1.Y(); else ya = PP2.Y();
685   if(Abs(PP1.Z())>Abs(PP2.Z())) za = PP1.Z(); else za = PP2.Z();
686   PP1.SetCoord(xa,ya,za);
687   Handle(Draw_Segment3D) d = new Draw_Segment3D(P1,PP1,Draw_blanc);
688   dout << d;
689   dout.Flush();
690   //cout<<"\nttran   "<<PP1.X()-P1.X()<<" "<<PP1.Y()-P1.Y()<<" "<<PP1.Z()-P1.Z()<<endl;
691   di <<"\nttran   "<<PP1.X()-P1.X()<<" "<<PP1.Y()-P1.Y()<<" "<<PP1.Z()-P1.Z()<< "\n";
692
693   static Standard_Integer nboxvecdp=0;
694   //cout<<"\nbox  b"<<++nboxvecdp<<" "<<Min(P1.X(),PP1.X())<<" "<<Min(P1.Y(),PP1.Y())<<" "<<Min(PP1.Z(),P1.Z());
695   //cout<<"  "<<Abs(PP1.X()-P1.X())<<" "<<Abs(PP1.Y()-P1.Y())<<" "<<Abs(PP1.Z()-P1.Z())<<endl;
696
697   //cout<<"\nDistance :"<<sqrt( (PP1.X()-P1.X())*(PP1.X()-P1.X())
698         //                   +(PP1.Y()-P1.Y())*(PP1.Y()-P1.Y())
699         //                   +(PP1.Z()-P1.Z())*(PP1.Z()-P1.Z()))<<endl;
700
701   di <<"\nbox  b"<<++nboxvecdp<<" "<<Min(P1.X(),PP1.X())<<" "<<Min(P1.Y(),PP1.Y())<<" "<<Min(PP1.Z(),P1.Z());
702   di <<"  "<<Abs(PP1.X()-P1.X())<<" "<<Abs(PP1.Y()-P1.Y())<<" "<<Abs(PP1.Z()-P1.Z())<< "\n";
703
704   di <<"\nDistance :"<<sqrt( (PP1.X()-P1.X())*(PP1.X()-P1.X())
705                              +(PP1.Y()-P1.Y())*(PP1.Y()-P1.Y())
706                              +(PP1.Z()-P1.Z())*(PP1.Z()-P1.Z()))<< "\n";
707   return(0);
708 }
709 //=======================================================================
710 // nproject
711 //=======================================================================
712
713 #include <TopTools_SequenceOfShape.hxx>
714  static Standard_Integer nproject(Draw_Interpretor& di, Standard_Integer n, const char** a)
715 {
716   if ( n < 4) return 1;
717   TopoDS_Shape InpShape;
718   Standard_Integer arg = 2, i;
719   TopTools_SequenceOfShape Args; 
720
721   Standard_Real Tol = 1.e-4;        
722   Standard_Real Tol2d;
723   Standard_Real MaxDistance = 1.e-3;
724   GeomAbs_Shape Continuity = GeomAbs_C2;  
725   Standard_Integer MaxDeg = 14;           
726   Standard_Integer MaxSeg = 16;           
727
728   while((n > arg) && !(InpShape = DBRep::Get(a[arg])).IsNull()){
729     Args.Append(InpShape);
730     arg++;
731   }
732   if(Args.Length() < 2) return 1;
733   
734   BRepOffsetAPI_NormalProjection OrtProj(Args.Last());
735
736   for(i = 1; i < Args.Length(); i++)
737     OrtProj.Add(Args(i));
738
739   if(n > arg)
740     if (!strcmp(a[arg],"-g")) {
741       OrtProj.SetLimit(Standard_False);
742       arg++;
743     }
744   
745   if(n > arg)
746     if (!strcmp(a[arg],"-d")) {
747       arg++;
748       if(n > arg)
749         MaxDistance = Draw::Atof(a[arg++]);
750       OrtProj.SetMaxDistance(MaxDistance);
751     }
752   if(n > arg) {
753     Tol = Max(Draw::Atof(a[arg++]),1.e-10);
754   }
755
756   if(n > arg) {
757     if (Draw::Atoi(a[arg]) == 0) Continuity = GeomAbs_C0;
758     else if (Draw::Atoi(a[arg]) == 1) Continuity = GeomAbs_C1;
759     arg++;
760   }
761
762  
763   if(n > arg) {
764     MaxDeg = Draw::Atoi(a[arg++]);
765     if (MaxDeg<1 || MaxDeg>14) MaxDeg = 14;
766   }
767
768   if(n > arg) MaxSeg = Draw::Atoi(a[arg]);
769     
770   Tol2d = Pow(Tol, 2./3);
771
772   OrtProj.SetParams(Tol, Tol2d, Continuity, MaxDeg, MaxSeg);
773   OrtProj.Build();
774   TopTools_ListOfShape Wire;
775   Standard_Boolean IsWire=OrtProj.BuildWire(Wire);
776   if (IsWire) {
777     //cout << " BuildWire OK " << endl;
778     di << " BuildWire OK " << "\n";
779   }
780   DBRep::Set(a[1], OrtProj.Shape());
781   return 0;  
782 }
783
784 //==========================================================================
785 //function : wexplo
786 //           exploration of a wire 
787 //==========================================================================
788 static Standard_Integer wexplo (Draw_Interpretor&, 
789                                 Standard_Integer argc, const char** argv)
790
791   char name[100];
792   if (argc < 2) return 1;
793   
794   TopoDS_Shape C1 = DBRep::Get (argv[1],TopAbs_WIRE);
795   TopoDS_Shape C2 ;
796
797   if (argc > 2)  C2 = DBRep::Get (argv[2],TopAbs_FACE);
798
799   if (C1.IsNull()) return 1;
800
801   BRepTools_WireExplorer we;
802   if (C2.IsNull()) we.Init(TopoDS::Wire(C1));
803   else             we.Init(TopoDS::Wire(C1),TopoDS::Face(C2));
804
805   Standard_Integer k = 1;
806   while (we.More()) {
807     TopoDS_Edge E = we.Current();
808     Sprintf(name,"WEDGE_%d",k); 
809           DBRep::Set(name,E);
810     we.Next();
811     k++;
812   }
813
814   return 0;
815 }
816
817 static Standard_Integer scalexyz(Draw_Interpretor& /*di*/, Standard_Integer n, const char** a)
818 {
819   if (n < 6) return 1;
820
821   TopoDS_Shape aShapeBase = DBRep::Get(a[2]);
822   if (aShapeBase.IsNull()) return 1;
823   
824   Standard_Real aFactorX = Draw::Atof(a[3]);
825   Standard_Real aFactorY = Draw::Atof(a[4]);
826   Standard_Real aFactorZ = Draw::Atof(a[5]);
827
828   gp_GTrsf aGTrsf;
829   gp_Mat rot (aFactorX, 0, 0,
830               0, aFactorY, 0,
831               0, 0, aFactorZ);
832   aGTrsf.SetVectorialPart(rot);
833   BRepBuilderAPI_GTransform aBRepGTrsf (aShapeBase, aGTrsf, Standard_False);
834   if (!aBRepGTrsf.IsDone())
835     Standard_ConstructionError::Raise("Scaling not done");
836   TopoDS_Shape Result = aBRepGTrsf.Shape();
837
838   DBRep::Set(a[1], Result);
839   return 0;  
840 }
841
842 void  BRepTest::BasicCommands(Draw_Interpretor& theCommands)
843 {
844   static Standard_Boolean done = Standard_False;
845   if (done) return;
846   done = Standard_True;
847
848   DBRep::BasicCommands(theCommands);
849
850   const char* g = "TOPOLOGY Basic shape commands";
851
852   theCommands.Add("addpcurve",
853                   "addpcurve edge 2dcurve face [tol (default 1.e-7)]",
854                   __FILE__,
855                   addpcurve,g);
856
857   theCommands.Add("reset",
858                   "reset name1 name2 ..., remove location",
859                   __FILE__,
860                   transform,g);
861
862   theCommands.Add("tmove",
863                   "tmove name1 name2 ... name, set location from name",
864                   __FILE__,
865                   transform,g);
866
867   theCommands.Add("ttranslate",
868                   "ttranslate name1 name2 ... dx dy dz",
869                   __FILE__,
870                   transform,g);
871
872   theCommands.Add("trotate",
873                   "trotate name1 name2 ... x y z dx dy dz angle",
874                   __FILE__,
875                   transform,g);
876
877   theCommands.Add("tmirror",
878                   "tmirror name x y z dx dy dz",
879                   __FILE__,
880                   transform,g);
881
882   theCommands.Add("tscale",
883                   "tscale name x y z scale",
884                   __FILE__,
885                   transform,g);
886
887   theCommands.Add("tcopy",
888                   "tcopy [-n(ogeom)] [-m(esh)] name1 result1 [name2 result2 ...]",
889                   __FILE__,
890                   tcopy,g);
891
892   theCommands.Add("bmove",
893                   "bmove name1 name2 ... name, set location from name",
894                   __FILE__,
895                   transform,g);
896
897   theCommands.Add("btranslate",
898                   "btranslate name1 name2 ... dx dy dz",
899                   __FILE__,
900                   transform,g);
901
902   theCommands.Add("brotate",
903                   "brotate name1 name2 ... x y z dx dy dz angle",
904                   __FILE__,
905                   transform,g);
906
907   theCommands.Add("bmirror",
908                   "bmirror name x y z dx dy dz",
909                   __FILE__,
910                   transform,g);
911
912   theCommands.Add("bscale",
913                   "bscale name x y z scale",
914                   __FILE__,
915                   transform,g);
916
917   theCommands.Add("precision",
918                   "precision [preci]",
919                   __FILE__,
920                   precision,g);
921
922   theCommands.Add("mkedgecurve",
923                   "mkedgecurve name tolerance",
924                   __FILE__,
925                   mkedgecurve,g);
926
927   theCommands.Add("fsameparameter",
928                   "fsameparameter shapename [tol (default 1.e-7)], \nforce sameparameter on all edges of the shape",
929                   __FILE__,
930                   sameparameter,g);
931
932   theCommands.Add("sameparameter",
933                   "sameparameter shapename [tol (default 1.e-7)]",
934                   __FILE__,
935                   sameparameter,g);
936
937   theCommands.Add("updatetolerance",
938                   "updatetolerance myShape [param] \n  if [param] is absent - not verify of face tolerance, else - perform it",
939                   __FILE__,
940                   updatetol,g);
941
942   theCommands.Add("solidorientation",
943                   "orientsolid myClosedSolid",
944                   __FILE__,
945                   orientsolid,g);
946
947   theCommands.Add("getcoords",
948     "getcoords vertex1 vertex 2... ; shows coords of input vertices",
949     __FILE__,
950     getcoords,g);
951   
952   theCommands.Add("bounding",
953                   "bounding shape [ xmin ymin zmin xmax ymax zmax] ; draw bounds",
954                   __FILE__,
955                   bounding,g);
956
957   theCommands.Add("boundingstr",
958                   "boundingstr shape [ xmin ymin zmin xmax ymax zmax] ; print bounding box",
959                   __FILE__,
960                   boundingstr,g);
961
962   theCommands.Add("nurbsconvert",
963                   "nurbsconvert result name [result name]",
964                   __FILE__,
965                   nurbsconvert,g);
966
967   theCommands.Add("deform",
968                   "deform newname name CoeffX CoeffY CoeffZ",
969                   __FILE__,
970                   deform,g);
971   
972   theCommands.Add("findplane",
973                   "findplane name planename ",
974                   __FILE__,
975                   findplane,g) ;
976   
977   theCommands.Add("maxtolerance",
978                   "maxtolerance shape ",
979                   __FILE__,
980                   maxtolerance,g) ;
981
982   theCommands.Add("reperageshape",
983                   "reperage shape -> list of shape (result of interstion shape , line)",
984                   __FILE__,
985                   reperageshape,g) ;
986
987   theCommands.Add("vecdc",
988                   "vecdc + Pointe double click ",
989                   __FILE__,
990                   vecdc,g) ;
991
992   theCommands.Add("nproject","nproject pj e1 e2 e3 ... surf -g -d [dmax] [Tol [continuity [maxdeg [maxseg]]]",
993                   __FILE__,
994                   nproject,g);
995
996   theCommands.Add("wexplo","wexplo wire [face] create WEDGE_i",
997                   __FILE__,
998                   wexplo,g);
999
1000   theCommands.Add("scalexyz",
1001                   "scalexyz res shape factor_x factor_y factor_z",
1002                   __FILE__,
1003                   scalexyz, g);
1004 }