1 // Created on: 1993-07-22
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-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 <BRepTest.hxx>
18 #include <DBRep.hxx>
19 #include <Draw_Interpretor.hxx>
20 #include <Draw_Appli.hxx>
21 #include <DrawTrSurf.hxx>
22 #include <TopoDS_Solid.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI.hxx>
25 #include <BRepPreviewAPI_MakeBox.hxx>
26 #include <BRepPrimAPI_MakeBox.hxx>
27 #include <BRepPrimAPI_MakeWedge.hxx>
28 #include <BRepPrimAPI_MakeCylinder.hxx>
29 #include <BRepPrimAPI_MakeCone.hxx>
30 #include <BRepPrimAPI_MakeSphere.hxx>
31 #include <BRepPrimAPI_MakeTorus.hxx>
32 #include <BRepPrimAPI_MakeSphere.hxx>
33 #include <Geom_Plane.hxx>
34 #include <gp_Pln.hxx>
35 #include <Message.hxx>
36 
37 //=======================================================================
38 // box
39 //=======================================================================
40 
box(Draw_Interpretor &,Standard_Integer n,const char ** a)41 static Standard_Integer box(Draw_Interpretor& , Standard_Integer n, const char** a)
42 {
43   gp_Pnt anOrigin;
44   gp_XYZ aParams;
45   gp_Dir aDir;
46   gp_Dir aXDir;
47   Standard_Boolean isMinMax = Standard_False;
48   Standard_Boolean isPreview = Standard_False;
49   Standard_Boolean isAxis = Standard_False;
50 
51   for (Standard_Integer anArgIter = 2; anArgIter < n; ++anArgIter)
52   {
53     TCollection_AsciiString anArgCase (a[anArgIter]);
54     anArgCase.LowerCase();
55     if (anArgCase == "-min" && anArgIter + 3 <= n)
56     {
57       anOrigin.SetX (Draw::Atof(a[anArgIter + 1]));
58       anOrigin.SetY (Draw::Atof(a[anArgIter + 2]));
59       anOrigin.SetZ (Draw::Atof(a[anArgIter + 3]));
60       anArgIter += 3;
61     }
62     else if (anArgCase == "-max" && anArgIter + 3 <= n)
63     {
64       aParams.SetX (Draw::Atof(a[anArgIter + 1]));
65       aParams.SetY (Draw::Atof(a[anArgIter + 2]));
66       aParams.SetZ (Draw::Atof(a[anArgIter + 3]));
67       isMinMax = Standard_True;
68       anArgIter += 3;
69     }
70     else if (anArgCase == "-size" && anArgIter + 3 <= n)
71     {
72       aParams.SetX (Draw::Atof(a[anArgIter + 1]));
73       aParams.SetY (Draw::Atof(a[anArgIter + 2]));
74       aParams.SetZ (Draw::Atof(a[anArgIter + 3]));
75       isMinMax = Standard_False;
76       anArgIter += 3;
77     }
78     else if (anArgCase == "-dir" && anArgIter + 3 <= n)
79     {
80       Standard_Real aX = Draw::Atof(a[anArgIter + 1]);
81       Standard_Real anY = Draw::Atof(a[anArgIter + 2]);
82       Standard_Real aZ = Draw::Atof(a[anArgIter + 3]);
83       aDir.SetCoord (aX, anY, aZ);
84       isAxis = Standard_True;
85       anArgIter += 3;
86     }
87     else if (anArgCase == "-xdir" && anArgIter + 3 <= n)
88     {
89       Standard_Real aX = Draw::Atof(a[anArgIter + 1]);
90       Standard_Real anY = Draw::Atof(a[anArgIter + 2]);
91       Standard_Real aZ = Draw::Atof(a[anArgIter + 3]);
92       aXDir.SetCoord (aX, anY, aZ);
93       isAxis = Standard_True;
94       anArgIter += 3;
95     }
96     else if (anArgCase == "-preview")
97     {
98       isPreview = Standard_True;
99     }
100     else if (anArgIter + 5 < n || anArgIter + 2 < n)
101     {
102       Standard_Real aValue = 0.0;
103       Standard_Integer aCountReal = 0;
104       Standard_Integer anIter = anArgIter;
105       while (anIter < n && Draw::ParseReal(a[anIter], aValue))
106       {
107         anIter++;
108         aCountReal++;
109       }
110 
111       if (aCountReal == 6)
112       {
113         anOrigin.SetX (Draw::Atof(a[anArgIter]));
114         anOrigin.SetY (Draw::Atof(a[anArgIter + 1]));
115         anOrigin.SetZ (Draw::Atof(a[anArgIter + 2]));
116 
117         aParams.SetX (Draw::Atof(a[anArgIter + 3]));
118         aParams.SetY (Draw::Atof(a[anArgIter + 4]));
119         aParams.SetZ (Draw::Atof(a[anArgIter + 5]));
120         anArgIter += 5;
121       }
122 
123       else if (aCountReal == 3)
124       {
125         aParams.SetX (Draw::Atof(a[anArgIter]));
126         aParams.SetY (Draw::Atof(a[anArgIter + 1]));
127         aParams.SetZ (Draw::Atof(a[anArgIter + 2]));
128         anArgIter += 2;
129       }
130       else
131       {
132         Message::SendFail() << "Syntax error";
133         return 1;
134       }
135     }
136   }
137 
138   if (isPreview)
139   {
140     TopoDS_Shape S;
141     BRepPreviewAPI_MakeBox aPreview;
142 
143     if (isMinMax == Standard_True)
144     {
145       aPreview.Init (anOrigin, aParams);
146     }
147     else if (isMinMax == Standard_False && isAxis == Standard_False)
148     {
149       aPreview.Init (anOrigin, aParams.X(), aParams.Y(), aParams.Z());
150     }
151     else if (isAxis)
152     {
153       gp_Ax2 anAxis (anOrigin, aDir, aXDir);
154       aPreview.Init (anAxis, aParams.X(), aParams.Y(), aParams.Z());
155     }
156     else
157     {
158       aPreview.Init (aParams.X(), aParams.Y(), aParams.Z());
159     }
160 
161     S = aPreview;
162     DBRep::Set(a[1],S);
163   }
164   else
165   {
166     TopoDS_Solid S;
167     if (isMinMax == Standard_True)
168     {
169       S = BRepPrimAPI_MakeBox(anOrigin, aParams);
170     }
171     else if (isMinMax == Standard_False && isAxis == Standard_False)
172     {
173       S = BRepPrimAPI_MakeBox(anOrigin, aParams.X(), aParams.Y(), aParams.Z());
174     }
175     else if (isAxis)
176     {
177       gp_Ax2 anAxis (anOrigin, aDir, aXDir);
178       S = BRepPrimAPI_MakeBox(anAxis, aParams.X(), aParams.Y(), aParams.Z());
179     }
180     else
181     {
182       S = BRepPrimAPI_MakeBox(aParams.X(), aParams.Y(), aParams.Z());
183     }
184     DBRep::Set(a[1],S);
185   }
186   return 0;
187 }
188 
189 
190 //=======================================================================
191 // wedge
192 //=======================================================================
193 
wedge(Draw_Interpretor &,Standard_Integer n,const char ** a)194 static Standard_Integer wedge(Draw_Interpretor& , Standard_Integer n, const char** a)
195 {
196   TopoDS_Solid S;
197 
198 //  Standard_Integer i = 0;
199   if ( n == 15 || n == 18) {
200     gp_Pnt LocalP(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]));
201     gp_Dir LocalN(Draw::Atof(a[5]),Draw::Atof(a[6]),Draw::Atof(a[7]));
202     gp_Dir LocalVx(Draw::Atof(a[8]),Draw::Atof(a[9]),Draw::Atof(a[10]));
203     gp_Ax2 Axis(LocalP,LocalN,LocalVx);
204 //    gp_Ax2 Axis(gp_Pnt(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4])),
205 //		gp_Dir(Draw::Atof(a[5]),Draw::Atof(a[6]),Draw::Atof(a[7])),
206 //		gp_Dir(Draw::Atof(a[8]),Draw::Atof(a[9]),Draw::Atof(a[10])));
207     if ( n == 15) {
208       S = BRepPrimAPI_MakeWedge(Axis,
209 			    Draw::Atof(a[11]),Draw::Atof(a[12]),Draw::Atof(a[13]),Draw::Atof(a[14]));
210     }
211     else {
212       S = BRepPrimAPI_MakeWedge(Axis,
213 			    Draw::Atof(a[11]),Draw::Atof(a[12]),Draw::Atof(a[13]),
214 			    Draw::Atof(a[14]),Draw::Atof(a[15]),Draw::Atof(a[16]),Draw::Atof(a[17]));
215     }
216   }
217   else if (n == 6) {
218     S = BRepPrimAPI_MakeWedge(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]));
219   }
220   else if (n == 9){
221     S = BRepPrimAPI_MakeWedge(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]),
222 			  Draw::Atof(a[5]),Draw::Atof(a[6]),Draw::Atof(a[7]),Draw::Atof(a[8]));
223   }
224   else
225     return 1;
226 
227   DBRep::Set(a[1],S);
228   return 0;
229 }
230 
231 //=======================================================================
232 // cylinder
233 //=======================================================================
234 
cylinder(Draw_Interpretor &,Standard_Integer n,const char ** a)235 static Standard_Integer cylinder(Draw_Interpretor& , Standard_Integer n, const char** a)
236 {
237   if (n < 3) return 1;
238   TopoDS_Solid S;
239   Handle(Geom_Plane) P =
240     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
241 
242   if (n == 4) {
243       S = BRepPrimAPI_MakeCylinder(Draw::Atof(a[2]),Draw::Atof(a[3]));
244   }
245   else if (n == 5) {
246     if (P.IsNull())
247       S = BRepPrimAPI_MakeCylinder(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0));
248     else
249       S = BRepPrimAPI_MakeCylinder(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]));
250   }
251   else if (n == 6) {
252     if (P.IsNull())
253       return 1;
254     else
255       S = BRepPrimAPI_MakeCylinder(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0));
256   }
257   else
258     return 1;
259 
260   DBRep::Set(a[1],S);
261   return 0;
262 }
263 
264 //=======================================================================
265 // cone
266 //=======================================================================
267 
cone(Draw_Interpretor &,Standard_Integer n,const char ** a)268 static Standard_Integer cone(Draw_Interpretor& , Standard_Integer n, const char** a)
269 {
270   if (n < 3) return 1;
271   TopoDS_Solid S;
272 
273   Handle(Geom_Plane) P =
274     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
275 
276   if (n == 5) {
277     S = BRepPrimAPI_MakeCone(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]));
278   }
279   else if (n == 6) {
280     if (P.IsNull())
281       S = BRepPrimAPI_MakeCone(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0));
282     else
283       S = BRepPrimAPI_MakeCone(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]));
284   }
285   else if (n == 7) {
286     S = BRepPrimAPI_MakeCone(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]),Draw::Atof(a[6]) * (M_PI / 180.0));
287   }
288   else
289     return 1;
290 
291   DBRep::Set(a[1],S);
292   return 0;
293 }
294 
295 //=======================================================================
296 // sphere
297 //=======================================================================
298 
sphere(Draw_Interpretor &,Standard_Integer n,const char ** a)299 static Standard_Integer sphere(Draw_Interpretor& , Standard_Integer n, const char** a)
300 {
301   if (n < 3) return 1;
302   TopoDS_Solid S;
303 
304   Handle(Geom_Plane) P =
305     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
306 
307   if (n == 3) {
308     S = BRepPrimAPI_MakeSphere(Draw::Atof(a[2]));
309   }
310   else if (n == 4) {
311     if (P.IsNull())
312       S = BRepPrimAPI_MakeSphere(Draw::Atof(a[2]),Draw::Atof(a[3]) * (M_PI / 180.0));
313     else
314       S = BRepPrimAPI_MakeSphere(P->Pln().Position().Ax2(),Draw::Atof(a[3]));
315   }
316   else if (n == 5) {
317     if (P.IsNull())
318       S = BRepPrimAPI_MakeSphere(Draw::Atof(a[2]),Draw::Atof(a[3]) * (M_PI / 180.0),Draw::Atof(a[4]) * (M_PI / 180.0));
319     else
320       S = BRepPrimAPI_MakeSphere(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0));
321   }
322   else if (n == 6) {
323     if (P.IsNull())
324       S = BRepPrimAPI_MakeSphere(Draw::Atof(a[2]),Draw::Atof(a[3]) * (M_PI / 180.0),Draw::Atof(a[4]) * (M_PI / 180.0),Draw::Atof(a[5]) * (M_PI / 180.0));
325     else
326       S = BRepPrimAPI_MakeSphere(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0),Draw::Atof(a[5]) * (M_PI / 180.0));
327   }
328   else if (n == 7) {
329     S = BRepPrimAPI_MakeSphere(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0),Draw::Atof(a[5]) * (M_PI / 180.0),Draw::Atof(a[6]) * (M_PI / 180.0));
330   }
331   else
332     return 1;
333 
334   DBRep::Set(a[1],S);
335   return 0;
336 }
337 
338 //=======================================================================
339 // torus
340 //=======================================================================
341 
torus(Draw_Interpretor &,Standard_Integer n,const char ** a)342 static Standard_Integer torus(Draw_Interpretor& , Standard_Integer n, const char** a)
343 {
344   if (n < 3) return 1;
345   TopoDS_Solid S;
346 
347   Handle(Geom_Plane) P =
348     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
349 
350   if (n == 4) {
351     S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]));
352   }
353   else if (n == 5) {
354     if (P.IsNull())
355       S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0));
356     else
357       S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]));
358   }
359   else if (n == 6) {
360     if (P.IsNull())
361       S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]),
362 			    Draw::Atof(a[4]) * (M_PI / 180.0),Draw::Atof(a[5]) * (M_PI / 180.0));
363     else
364       S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),
365 			    Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0));
366   }
367   else if (n == 7) {
368     if (P.IsNull())
369       S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]),
370 			    Draw::Atof(a[4]) * (M_PI / 180.0),Draw::Atof(a[5]) * (M_PI / 180.0),Draw::Atof(a[6]) * (M_PI / 180.0));
371     else
372       S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),Draw::Atof(a[3]),
373 			    Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0),Draw::Atof(a[6]) * (M_PI / 180.0));
374   }
375   else if (n == 8) {
376     S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]),
377 			  Draw::Atof(a[5]) * (M_PI / 180.0),Draw::Atof(a[6]) * (M_PI / 180.0),Draw::Atof(a[7]) * (M_PI / 180.0));
378   }
379   else
380     return 1;
381 
382   DBRep::Set(a[1],S);
383   return 0;
384 }
385 
386 
387 
388 //=======================================================================
389 //function : PrimitiveCommands
390 //purpose  :
391 //=======================================================================
392 
PrimitiveCommands(Draw_Interpretor & theCommands)393 void  BRepTest::PrimitiveCommands(Draw_Interpretor& theCommands)
394 {
395   static Standard_Boolean done = Standard_False;
396   if (done) return;
397   done = Standard_True;
398 
399   DBRep::BasicCommands(theCommands);
400 
401   const char* g = "Primitive building commands";
402 
403   theCommands.Add ("box",
404                    "box name [dx dy dz] [x y z dx dy dz]"
405          "\n\t\t:            [-min x y z] [-size dx dy dz] [-max x y z]"
406          "\n\t\t:            [-dir x y z -xdir x y z] [-preview]"
407          "\n\t\t: Construct axes-aligned box and put result into 'name' variable"
408          "\n\t\t:  -min   box lower corner, origin; (0,0,0) by default"
409          "\n\t\t:  -size  box dimensions   (alternative to -max)"
410          "\n\t\t:  -max   box upper corner (alternative to -size)"
411          "\n\t\t:  -dir   main direction of coordinate system (DZ by default)"
412          "\n\t\t:  -xdir  x direction of coordinate system (DX by default)"
413          "\n\t\t:  -preview non-solid shape will be created (vertex, edge, rectangle or box);"
414          "\n\t\t:           otherwise, return NULL shape in case of zero box dimension.",
415                   __FILE__,box,g);
416 
417   theCommands.Add("wedge","wedge name [Ox Oy Oz Zx Zy Zz Xx Xy Xz] dx dy dz ltx / xmin zmin xmax zmax",__FILE__,wedge,g);
418 
419   theCommands.Add("pcylinder",
420                   "pcylinder name [plane(ax2)] R H [angle]"
421                   "\n\t\t: Construct a cylinder and put result into 'name' variable."
422                   "\n\t\t: Parameters of the cylinder :"
423                   "\n\t\t: - plane coordinate system for the construction of the cylinder"
424                   "\n\t\t: - R     cylinder radius"
425                   "\n\t\t: - H     cylinder height"
426                   "\n\t\t: - angle cylinder top radius",
427                   __FILE__, cylinder, g);
428 
429   theCommands.Add("pcone",
430                   "pcone name [plane(ax2)] R1 R2 H [angle]"
431                   "\n\t\t: Construct a cone, part cone or conical frustum and put result into 'name' variable."
432                   "\n\t\t: Parameters of the cone :"
433                   "\n\t\t: - plane  coordinate system for the construction of the cone"
434                   "\n\t\t: - R1     cone bottom radius"
435                   "\n\t\t: - R2     cone top radius"
436                   "\n\t\t: - H      cone height"
437                   "\n\t\t: - angle  angle to create a part cone",
438                   __FILE__, cone, g);
439 
440   theCommands.Add("psphere",
441                   "psphere name [plane(ax2)] R [angle1 angle2] [angle]"
442                   "\n\t\t: Construct a sphere, spherical segment or spherical wedge and put result into 'name' variable."
443                   "\n\t\t: Parameters of the sphere :"
444                   "\n\t\t: - plane  coordinate system for the construction of the sphere"
445                   "\n\t\t: - R      sphere radius"
446                   "\n\t\t: - angle1 first angle to create a spherical segment  [-90; 90]"
447                   "\n\t\t: - angle2 second angle to create a spherical segment [-90; 90]"
448                   "\n\t\t: - angle  angle to create a spherical wedge",
449                   __FILE__, sphere, g);
450 
451   theCommands.Add("ptorus",
452                   "ptorus name [plane(ax2)] R1 R2 [angle1 angle2] [angle]"
453                   "\n\t\t: Construct a torus or torus segment and put result into 'name' variable."
454                   "\n\t\t: Parameters of the torus :"
455                   "\n\t\t: - plane  coordinate system for the construction of the torus"
456                   "\n\t\t: - R1     distance from the center of the pipe to the center of the torus"
457                   "\n\t\t: - R2     radius of the pipe"
458                   "\n\t\t: - angle1 first angle to create a torus ring segment"
459                   "\n\t\t: - angle2 second angle to create a torus ring segment"
460                   "\n\t\t: - angle  angle to create a torus pipe segment",
461                   __FILE__, torus, g);
462 }
463 
464 
465