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