16c7d8c3092eb509d264ee53811a7a1a17935c10
[occt.git] / src / BRepTest / BRepTest_PrimitiveCommands.cxx
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 <BRepPrimAPI_MakeBox.hxx>
26 #include <BRepPrimAPI_MakeWedge.hxx>
27 #include <BRepPrimAPI_MakeCylinder.hxx>
28 #include <BRepPrimAPI_MakeCone.hxx>
29 #include <BRepPrimAPI_MakeSphere.hxx>
30 #include <BRepPrimAPI_MakeTorus.hxx>
31 #include <BRepPrimAPI_MakeSphere.hxx>
32 #include <Geom_Plane.hxx>
33 #include <gp_Pln.hxx>
34
35
36 //=======================================================================
37 // box
38 //=======================================================================
39
40 static Standard_Integer box(Draw_Interpretor& , Standard_Integer n, const char** a)
41 {
42   if (n < 5) return 1;
43   Standard_Real dx = Draw::Atof(a[n-3]);
44   Standard_Real dy = Draw::Atof(a[n-2]);
45   Standard_Real dz = Draw::Atof(a[n-1]);
46
47   TopoDS_Solid S;
48
49   if (n > 5) {
50     if (n < 8) return 1;
51     Standard_Real x = Draw::Atof(a[2]);
52     Standard_Real y = Draw::Atof(a[3]);
53     Standard_Real z = Draw::Atof(a[4]);
54     S = BRepPrimAPI_MakeBox(gp_Pnt(x,y,z),dx,dy,dz);
55   }
56   else {
57     S = BRepPrimAPI_MakeBox(dx,dy,dz);
58   }
59
60   DBRep::Set(a[1],S);
61   return 0;
62 }
63
64 //=======================================================================
65 // wedge
66 //=======================================================================
67
68 static Standard_Integer wedge(Draw_Interpretor& , Standard_Integer n, const char** a)
69 {
70   TopoDS_Solid S;
71
72 //  Standard_Integer i = 0;
73   if ( n == 15 || n == 18) {
74     gp_Pnt LocalP(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]));
75     gp_Dir LocalN(Draw::Atof(a[5]),Draw::Atof(a[6]),Draw::Atof(a[7]));
76     gp_Dir LocalVx(Draw::Atof(a[8]),Draw::Atof(a[9]),Draw::Atof(a[10]));
77     gp_Ax2 Axis(LocalP,LocalN,LocalVx);
78 //    gp_Ax2 Axis(gp_Pnt(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4])),
79 //              gp_Dir(Draw::Atof(a[5]),Draw::Atof(a[6]),Draw::Atof(a[7])),
80 //              gp_Dir(Draw::Atof(a[8]),Draw::Atof(a[9]),Draw::Atof(a[10])));
81     if ( n == 15) {
82       S = BRepPrimAPI_MakeWedge(Axis,
83                             Draw::Atof(a[11]),Draw::Atof(a[12]),Draw::Atof(a[13]),Draw::Atof(a[14]));
84     }
85     else {
86       S = BRepPrimAPI_MakeWedge(Axis,
87                             Draw::Atof(a[11]),Draw::Atof(a[12]),Draw::Atof(a[13]),
88                             Draw::Atof(a[14]),Draw::Atof(a[15]),Draw::Atof(a[16]),Draw::Atof(a[17]));
89     }
90   }
91   else if (n == 6) {
92     S = BRepPrimAPI_MakeWedge(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]));
93   }
94   else if (n == 9){
95     S = BRepPrimAPI_MakeWedge(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]),
96                           Draw::Atof(a[5]),Draw::Atof(a[6]),Draw::Atof(a[7]),Draw::Atof(a[8]));
97   }
98   else
99     return 1;
100
101   DBRep::Set(a[1],S);
102   return 0;
103 }
104
105 //=======================================================================
106 // cylinder
107 //=======================================================================
108
109 static Standard_Integer cylinder(Draw_Interpretor& , Standard_Integer n, const char** a)
110 {
111   if (n < 3) return 1;
112   TopoDS_Solid S;
113   Handle(Geom_Plane) P =
114     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
115
116   if (n == 4) {
117       S = BRepPrimAPI_MakeCylinder(Draw::Atof(a[2]),Draw::Atof(a[3]));
118   }
119   else if (n == 5) {
120     if (P.IsNull())
121       S = BRepPrimAPI_MakeCylinder(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0));
122     else
123       S = BRepPrimAPI_MakeCylinder(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]));
124   }
125   else if (n == 6) {
126     if (P.IsNull())
127       return 1;
128     else
129       S = BRepPrimAPI_MakeCylinder(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0));
130   }
131   else
132     return 1;
133
134   DBRep::Set(a[1],S);
135   return 0;
136 }
137
138 //=======================================================================
139 // cone
140 //=======================================================================
141
142 static Standard_Integer cone(Draw_Interpretor& , Standard_Integer n, const char** a)
143 {
144   if (n < 3) return 1;
145   TopoDS_Solid S;
146
147   Handle(Geom_Plane) P =
148     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
149
150   if (n == 5) {
151     S = BRepPrimAPI_MakeCone(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]));
152   }
153   else if (n == 6) {
154     if (P.IsNull())
155       S = BRepPrimAPI_MakeCone(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0));
156     else
157       S = BRepPrimAPI_MakeCone(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]));
158   }
159   else if (n == 7) {
160     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));
161   }
162   else
163     return 1;
164
165   DBRep::Set(a[1],S);
166   return 0;
167 }
168
169 //=======================================================================
170 // sphere
171 //=======================================================================
172
173 static Standard_Integer sphere(Draw_Interpretor& , Standard_Integer n, const char** a)
174 {
175   if (n < 3) return 1;
176   TopoDS_Solid S;
177
178   Handle(Geom_Plane) P =
179     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
180
181   if (n == 3) {
182     S = BRepPrimAPI_MakeSphere(Draw::Atof(a[2]));
183   }
184   else if (n == 4) {
185     if (P.IsNull())
186       S = BRepPrimAPI_MakeSphere(Draw::Atof(a[2]),Draw::Atof(a[3]) * (M_PI / 180.0));
187     else
188       S = BRepPrimAPI_MakeSphere(P->Pln().Position().Ax2(),Draw::Atof(a[3]));
189   }
190   else if (n == 5) {
191     if (P.IsNull())
192       S = BRepPrimAPI_MakeSphere(Draw::Atof(a[2]),Draw::Atof(a[3]) * (M_PI / 180.0),Draw::Atof(a[4]) * (M_PI / 180.0));
193     else
194       S = BRepPrimAPI_MakeSphere(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0));
195   }
196   else if (n == 6) {
197     if (P.IsNull())
198       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));
199     else
200       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));
201   }
202   else if (n == 7) {
203     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));
204   }
205   else
206     return 1;
207
208   DBRep::Set(a[1],S);
209   return 0;
210 }
211
212 //=======================================================================
213 // torus
214 //=======================================================================
215
216 static Standard_Integer torus(Draw_Interpretor& , Standard_Integer n, const char** a)
217 {
218   if (n < 3) return 1;
219   TopoDS_Solid S;
220
221   Handle(Geom_Plane) P =
222     Handle(Geom_Plane)::DownCast(DrawTrSurf::Get(a[2]));
223
224   if (n == 4) {
225     S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]));
226   }
227   else if (n == 5) {
228     if (P.IsNull())
229       S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]),Draw::Atof(a[4]) * (M_PI / 180.0));
230     else
231       S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]));
232   }
233   else if (n == 6) {
234     if (P.IsNull())
235       S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]),
236                             Draw::Atof(a[4]) * (M_PI / 180.0),Draw::Atof(a[5]) * (M_PI / 180.0));
237     else
238       S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),
239                             Draw::Atof(a[3]),Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0));
240   }
241   else if (n == 7) {
242     if (P.IsNull())
243       S = BRepPrimAPI_MakeTorus(Draw::Atof(a[2]),Draw::Atof(a[3]),
244                             Draw::Atof(a[4]) * (M_PI / 180.0),Draw::Atof(a[5]) * (M_PI / 180.0),Draw::Atof(a[6]) * (M_PI / 180.0));
245     else
246       S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),Draw::Atof(a[3]),
247                             Draw::Atof(a[4]),Draw::Atof(a[5]) * (M_PI / 180.0),Draw::Atof(a[6]) * (M_PI / 180.0));
248   }
249   else if (n == 8) {
250     S = BRepPrimAPI_MakeTorus(P->Pln().Position().Ax2(),Draw::Atof(a[3]),Draw::Atof(a[4]),
251                           Draw::Atof(a[5]) * (M_PI / 180.0),Draw::Atof(a[6]) * (M_PI / 180.0),Draw::Atof(a[7]) * (M_PI / 180.0));
252   }
253   else
254     return 1;
255
256   DBRep::Set(a[1],S);
257   return 0;
258 }
259
260
261
262 //=======================================================================
263 //function : PrimitiveCommands
264 //purpose  : 
265 //=======================================================================
266
267 void  BRepTest::PrimitiveCommands(Draw_Interpretor& theCommands)
268 {
269   static Standard_Boolean done = Standard_False;
270   if (done) return;
271   done = Standard_True;
272
273   DBRep::BasicCommands(theCommands);
274
275   const char* g = "Primitive building commands";
276     
277   theCommands.Add("box","box name [x1 y1 z1] dx dy dz",__FILE__,box,g);
278   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);
279   
280   theCommands.Add("pcylinder","pcylinder name [plane(ax2)] R H [angle]",__FILE__,cylinder,g);
281   theCommands.Add("pcone",    "pcone name [plane(ax2)] R1 R2 H [angle]",__FILE__,cone,g);
282   theCommands.Add("psphere",  "psphere name [plane(ax2)] R [angle1 angle2] [angle]",__FILE__,sphere,g);
283   theCommands.Add("ptorus",   "ptorus name [plane(ax2)] R1 R2 [angle1 angle2] [angle]",__FILE__,torus,g);
284 }
285
286