0028574: Get rid of the TestTopOpe* packages
[occt.git] / src / TestTopOpe / TestTopOpe_OtherCommands.cxx
1 // Created on: 1997-01-21
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1997-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
18 #include <Bnd_Box.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepBndLib.hxx>
22 #include <DBRep.hxx>
23 #include <Draw.hxx>
24 #include <Draw_Appli.hxx>
25 #include <Draw_Interpretor.hxx>
26 #include <Draw_Marker3D.hxx>
27 #include <Draw_Segment3D.hxx>
28 #include <DrawTrSurf.hxx>
29 #include <DrawTrSurf_Point.hxx>
30 #include <gp.hxx>
31 #include <gp_Pnt.hxx>
32 #include <Precision.hxx>
33 #include <TestTopOpe.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopoDS_Solid.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopOpeBRepBuild_HBuilder.hxx>
40 #include <TopOpeBRepDS_HDataStructure.hxx>
41
42 #ifdef _WIN32
43 Standard_IMPORT Draw_Viewer dout;
44 #endif
45
46 //=======================================================================
47 // bounds S xmin ymin zmin zmax ymax zmax
48 //=======================================================================
49 Standard_Integer BOUNDS(Draw_Interpretor& di, Standard_Integer narg, const char** a)
50 {
51   if (narg != 8) return 1;
52   TopoDS_Shape S = DBRep::Get(a[1]);
53   Standard_Real xmin,ymin,zmin,xmax,ymax,zmax;
54   Bnd_Box b; BRepBndLib::Add(S,b);
55   b.Enlarge(Precision::Confusion());
56   b.Get(xmin,ymin,zmin,xmax,ymax,zmax);
57   Draw::Set(a[2],xmin);
58   Draw::Set(a[3],ymin);
59   Draw::Set(a[4],zmin);
60   Draw::Set(a[5],xmax);
61   Draw::Set(a[6],ymax);
62   Draw::Set(a[7],zmax);
63   di<<"xmin:"<<xmin<<" ";
64   di<<"ymin:"<<ymin<<" ";
65   di<<"zmin:"<<zmin<<" ";
66   di<<"xmax:"<<xmax<<" ";
67   di<<"ymax:"<<ymax<<" ";
68   di<<"zmax:"<<zmax<<"\n";
69   return 0 ;
70 }
71
72 //---------------------------------------------------------------------
73 static Standard_Boolean PersEyeDir(const Standard_Integer ViewId,
74                                    gp_Pnt& E,
75                                    gp_Dir& D)
76 //---------------------------------------------------------------------
77 {
78   gp_Trsf T;
79   gp_Pnt Eye;
80   gp_Dir Dirz(0,0,1);
81   dout.GetTrsf(ViewId,T);
82   T.Invert();
83   Dirz.Transform(T);
84   Standard_Boolean pers = Standard_False;
85   if (!strcmp("PERS",dout.GetType(ViewId))) {
86     pers = Standard_True;
87     Eye.SetXYZ(dout.Focal(ViewId)*Dirz.XYZ());
88   }
89   E = Eye;
90   D = Dirz;
91   return pers;
92 }
93
94 //=======================================================================
95 // DOTVIEW dx dy dz id  ddx ddy ddz   (ddx,ddy,ddz) = (dx,dy,dz)^(<id> axis)
96 //=======================================================================
97 Standard_Integer DOTVIEW(Draw_Interpretor&, Standard_Integer narg, const char** a)
98 {
99   if (narg < 8) return 1;
100
101   Standard_Real dx,dy,dz;
102   Draw::Get(a[1],dx); Draw::Get(a[2],dy); Draw::Get(a[3],dz);
103   gp_Dir d1(dx,dy,dz);
104   Standard_Real idr; Draw::Get(a[4],idr); 
105   gp_Pnt p; gp_Dir d2; PersEyeDir((Standard_Integer)idr,p,d2);
106   
107   gp_Dir d3(d1.Crossed(d2));
108   Draw::Set(a[5],d3.X());
109   Draw::Set(a[6],d3.Y());
110   Draw::Set(a[7],d3.Z());
111   
112   return 0;
113 }
114
115 Standard_Integer VECTEUR(Draw_Interpretor& di, Standard_Integer , const char** ) {
116   di << "Pick positions with button \n";
117   Standard_Integer id,X,Y,b;
118   gp_Trsf T;
119   gp_Pnt P1,P2,PP1,PP2;
120   
121   //-----------------------------------------------------------
122   dout.Select(id,X,Y,b);    dout.GetTrsf(id,T);
123   T.Invert();
124   Standard_Real z = dout.Zoom(id);
125   P1.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
126   P1.Transform(T);
127   
128   dout.Select(id,X,Y,b);  dout.GetTrsf(id,T);
129   T.Invert();  z = dout.Zoom(id);
130   
131   P2.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
132   P2.Transform(T);
133   Standard_Real xa,ya,za;
134   if(Abs(P1.X())>Abs(P2.X())) xa = P1.X(); else xa = P2.X();
135   if(Abs(P1.Y())>Abs(P2.Y())) ya = P1.Y(); else ya = P2.Y();
136   if(Abs(P1.Z())>Abs(P2.Z())) za = P1.Z(); else za = P2.Z();
137   P1.SetCoord(xa,ya,za);
138   Handle(Draw_Marker3D) D0 = new Draw_Marker3D
139     (gp_Pnt(P1.X(),P1.Y(),P1.Z()),Draw_Square,Draw_blanc,1);
140   dout << D0;
141   dout.Flush();
142   //-----------------------------------------------------------
143   dout.Select(id,X,Y,b);  
144   dout.GetTrsf(id,T);
145   T.Invert();
146   z = dout.Zoom(id);
147   PP1.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
148   PP1.Transform(T);
149   dout.Select(id,X,Y,b);
150   dout.GetTrsf(id,T);
151   T.Invert();
152   z = dout.Zoom(id);
153   PP2.SetCoord((Standard_Real)X /z,(Standard_Real)Y /z,0.0);
154   PP2.Transform(T);
155   if(Abs(PP1.X())>Abs(PP2.X())) xa = PP1.X(); else xa = PP2.X();
156   if(Abs(PP1.Y())>Abs(PP2.Y())) ya = PP1.Y(); else ya = PP2.Y();
157   if(Abs(PP1.Z())>Abs(PP2.Z())) za = PP1.Z(); else za = PP2.Z();
158   PP1.SetCoord(xa,ya,za);
159   Handle(Draw_Segment3D) d = new Draw_Segment3D(P1, PP1, Draw_blanc);
160   dout << d;
161   dout.Flush();
162   di<<"\n";
163   di<<"cdinp P1 "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z()<<"\n";
164   di<<"cdinp P2 "<<PP1.X()<<" "<<PP1.Y()<<" "<<PP1.Z()<<"\n";
165   di<<"cdinp PM "<<(PP1.X()+P1.X())/2<<" "<<(PP1.Y()+P1.Y())/2<<" "<<(PP1.Z()+P1.Z())/2<<"\n";
166   di<<"\n";
167   di<<"ttranslate "<<PP1.X()-P1.X()<<" "<<PP1.Y()-P1.Y()<<" "<<PP1.Z()-P1.Z()<<"\n";
168   
169   return 0;
170 }
171  
172 Standard_Integer MKSOLSHE(Draw_Interpretor&, Standard_Integer narg, const char** a) {
173   // 0 = [mksol | mkshe], 1 = solide/shell a creer avec 1 = shell ou face
174   // 0 = [mksol | mkshe], 1 = solide/shell a creer avec 2...narg-1 subsshapes
175   if (narg < 2) return 1;
176   
177   Standard_Integer i;
178   BRep_Builder BB;
179   TopoDS_Shape res;
180
181   Standard_Integer i1 = (narg == 2) ? 1 : 2;
182   Standard_Integer i2 = (narg > 2) ? narg : 2;
183
184   // take all the FACE args, place them in the shell <she>
185   TopoDS_Shell she; BB.MakeShell(she); she.Closed(Standard_False);
186   Standard_Boolean yaface = Standard_False;
187   for (i = i1; i < i2; i++) {
188     const TopoDS_Shape& S = DBRep::Get(a[i]);
189     if (S.IsNull()) continue;
190     if (S.ShapeType() == TopAbs_FACE) {
191       BB.Add(she,S);
192       she.Closed (BRep_Tool::IsClosed (she));
193       yaface = Standard_True;
194     }
195   }
196
197   // take all the SHELL args, place them in the solid <sol>
198   TopoDS_Solid sol; BB.MakeSolid(sol);
199   for (i = i1; i < i2; i++) {
200     const TopoDS_Shape& S = DBRep::Get(a[i]);
201     if (S.IsNull()) continue;
202     if (S.ShapeType() == TopAbs_SHELL) {
203       BB.Add(sol,S);
204     }
205   }
206
207   if      (!strcmp("mksol",a[0])) {
208     if (yaface) BB.Add(sol,she);
209     res = sol;
210   }
211   else if (!strcmp("mkshe",a[0])) {
212     res = she;
213   }
214   else {
215     return 1;
216   }
217
218   DBRep::Set(a[1],res);
219   return 0;
220 }
221
222 //=======================================================================
223 // Grille  xmin ymin zmin zmax ymax zmax nbx nby nbz
224 //=======================================================================
225 Standard_Integer GRILLE(Draw_Interpretor& di, Standard_Integer narg, const char** a)
226 {
227   if (narg != 10) return 1;
228   Standard_Real xmin,ymin,zmin,xmax,ymax,zmax,dx,dy;
229   Standard_Integer nbx,nby,nbz;
230   xmin = Draw::Atof(a[1]);
231   ymin = Draw::Atof(a[2]);
232   zmin = Draw::Atof(a[3]);
233   xmax = Draw::Atof(a[4]);
234   ymax = Draw::Atof(a[5]);
235   zmax = Draw::Atof(a[6]);
236   nbx  = Draw::Atoi(a[7]);
237   nby  = Draw::Atoi(a[8]);
238   nbz  = Draw::Atoi(a[9]);
239   if(xmax<xmin || ymax<ymin || zmax<zmin || nbx<0 || nby<0 || nbz<0) 
240     return 1;
241   if(nbx) dx = (xmax-xmin)/nbx; else dx=0;
242   if(nby) dy = (ymax-ymin)/nby; else dy=0;
243   di<<"compound CE\n";
244   for(Standard_Real x=xmin; x<xmax; x+=dx) { 
245     for(Standard_Real y=ymin; y<ymax; y+=dx) { 
246       //-- cout<<x<<" "<<y<<" "<<zmin<<"   "<<x<<" "<<y<<" "<<zmax<<"\n";
247       di<<"line l "<<x<<" "<<y<<" "<<zmin<<" 0 0 1;mkedge e l 0 ";
248       di<<zmax-zmin<<" ;orient e EXTERNAL; add e CE\n";
249       if(dy==0) y=ymax;
250     } 
251     if(dx==0) x=xmax;
252   }
253   return 0 ;
254 }
255
256 Standard_Integer GETP3D(Draw_Interpretor& di, Standard_Integer /*narg*/, const char** a)
257 {
258   TopoDS_Shape V = DBRep::Get(a[1]);
259   if (V.IsNull()) {
260     di<<"null shape\n";
261     return 1;
262   }
263   gp_Pnt pV = BRep_Tool::Pnt(TopoDS::Vertex(V));
264   di<<pV.X()<<" "<<pV.Y()<<" "<<pV.Z()<<"\n";
265   return 0;
266 }
267 #ifdef OCCT_DEBUG
268 #include <Draw_Chronometer.hxx>
269 Standard_IMPORT Standard_Boolean Draw_Chrono;
270 Standard_Integer CHROBOOP(Draw_Interpretor& di, Standard_Integer n, const char** a)
271 {
272   Standard_Real ns = 0.;
273   Standard_Integer nm = 0;
274   Standard_Integer nh = 0;
275   Standard_Real ct = 0.;
276
277   if ((n == 1) || (*a[1] == '0') || (*a[1] == '1')) {
278     if (n == 1)
279       Draw_Chrono = !Draw_Chrono;
280     else
281       Draw_Chrono = (*a[1] == '1');
282     
283     if (Draw_Chrono) di << "Chronometers activated.\n";
284     else di << "Chronometers desactivated.\n";
285   }
286   else {
287     Handle(Draw_Drawable3D) D = Draw::Get(a[1]);
288     Handle(Draw_Chronometer) C;
289     if (!D.IsNull()) {
290       C = Handle(Draw_Chronometer)::DownCast(D);
291     }
292     if (C.IsNull()) {
293       C = new Draw_Chronometer();
294     Draw::Set(a[1],C,Standard_False);
295     }
296     if (n <= 2) {
297       C->Timer().Reset();
298     }
299     else if (n <= 3) {
300       if (!strcasecmp(a[2],"reset"))
301         C->Timer().Reset();
302       else if (!strcasecmp(a[2],"start"))
303         C->Timer().Start();
304       else if (!strcasecmp(a[2],"stop"))
305         C->Timer().Stop();
306       else if (!strcasecmp(a[2],"show"))
307         C->Timer().Show();
308       else {
309         C->Timer().Show(ns,nm,nh,ct);
310         if      (!strcasecmp(a[2],"-s")) {
311           di<<ns;
312         }
313         else if (!strcasecmp(a[2],"-m")) {
314           di<<nm;
315         }
316         else if (!strcasecmp(a[2],"-h")) {
317           di<<nh;
318         }
319         else if (!strcasecmp(a[2],"-c")) {
320           di<<ct;
321         }
322       }
323     }
324   }
325   return 0;
326 }
327 #endif
328
329 //=======================================================================
330 //function : OtherCommands
331 //purpose  : 
332 //=======================================================================
333 void TestTopOpe::OtherCommands(Draw_Interpretor& theCommands)
334 {
335   const char* g = "TestTopOpe OtherCommands";
336   theCommands.Add("grille","grille x0 y0 z0 x1 y1 z1 nbx nby nbz",__FILE__,GRILLE,g);
337   theCommands.Add("vecteur","4 Pick",__FILE__,VECTEUR,g);
338   theCommands.Add("bounds","bounds S x1 y1 z1 z2 y2 z2 = (box of S)",__FILE__,BOUNDS,g);
339   theCommands.Add("dotview","dotview dx dy dz id _dx1 _dx2 _dx3",__FILE__,DOTVIEW,g);
340   theCommands.Add("mksol","make a solid [1] with [2] ...",__FILE__,MKSOLSHE,g);
341   theCommands.Add("mkshe","make a shell [1] with [2] ...",__FILE__,MKSOLSHE,g);
342   theCommands.Add("getp3d","getp3d vertex",__FILE__,GETP3D,g);
343 #ifdef OCCT_DEBUG
344   theCommands.Add("chroboop","",__FILE__,CHROBOOP,g);
345 #endif
346 }