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