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