0027772: Foundation Classes - define Standard_Boolean using C++ type "bool" instead...
[occt.git] / src / QANewDBRepNaming / QANewDBRepNaming_PrimitiveCommands.cxx
1 // Created on: 1999-12-23
2 // Created by: Vladislav ROMASHKO
3 // Copyright (c) 1999-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
18 #include <BRep_Tool.hxx>
19 #include <BRepAdaptor_Surface.hxx>
20 #include <BRepFilletAPI_MakeChamfer.hxx>
21 #include <BRepFilletAPI_MakeFillet.hxx>
22 #include <BRepPrimAPI_MakeBox.hxx>
23 #include <BRepPrimAPI_MakeCylinder.hxx>
24 #include <BRepPrimAPI_MakePrism.hxx>
25 #include <BRepPrimAPI_MakeRevol.hxx>
26 #include <BRepPrimAPI_MakeSphere.hxx>
27 #include <DBRep.hxx>
28 #include <DDF.hxx>
29 #include <Draw.hxx>
30 #include <Draw_Interpretor.hxx>
31 #include <gp_Pln.hxx>
32 #include <QADNaming.hxx>
33 #include <QANewBRepNaming_Box.hxx>
34 #include <QANewBRepNaming_Chamfer.hxx>
35 #include <QANewBRepNaming_Cylinder.hxx>
36 #include <QANewBRepNaming_Fillet.hxx>
37 #include <QANewBRepNaming_ImportShape.hxx>
38 #include <QANewBRepNaming_Prism.hxx>
39 #include <QANewBRepNaming_Revol.hxx>
40 #include <QANewBRepNaming_Sphere.hxx>
41 #include <QANewDBRepNaming.hxx>
42 #include <TDataXtd_Geometry.hxx>
43 #include <TNaming_Builder.hxx>
44 #include <TNaming_NamedShape.hxx>
45 #include <TNaming_Tool.hxx>
46 #include <TopExp.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopoDS_Wire.hxx>
53
54 //=======================================================================
55 //function : QANewDBRepNaming_NameBox
56 //purpose  : NameCylinder Doc Label dx dy dz
57 //=======================================================================
58 static Standard_Integer QANewDBRepNaming_NameBox (Draw_Interpretor& di,
59                                                   Standard_Integer nb, 
60                                                   const char ** arg)
61 {
62   if(nb == 6) {
63     TDF_Label L;
64     if (!QADNaming::Entry(arg, L)) return 1;
65
66     BRepPrimAPI_MakeBox mkBox(Draw::Atof(arg[3]), Draw::Atof(arg[4]), Draw::Atof(arg[5]));
67     mkBox.Build();
68
69     if(!mkBox.IsDone()) {
70       di << "The box hasn't been built"  << "\n";
71       return 1;
72     }
73
74     QANewBRepNaming_Box nameBox(L);
75     nameBox.Load(mkBox,QANewBRepNaming_SOLID);
76     return 0;
77   }
78   di <<  "QANewDBRepNaming_NameBox : Error"  << "\n";
79   return 1;
80 }           
81
82 //=======================================================================
83 //function : QANewDBRepNaming_NameCylinder
84 //purpose  : NameCylinder Doc Label R H Angle [ShapeType(Shell/Solid)]
85 //=======================================================================
86
87 static Standard_Integer QANewDBRepNaming_NameCylinder (Draw_Interpretor& di,
88                                                   Standard_Integer nb, 
89                                                   const char ** arg)
90 {
91   if(nb == 6 || nb == 7) {
92     TDF_Label L;
93     if (!QADNaming::Entry(arg, L)) return 1;
94
95     BRepPrimAPI_MakeCylinder mkCylinder(Draw::Atof(arg[3]), Draw::Atof(arg[4]));
96     if (Draw::Atof(arg[5]) != 0.) {
97       // for Mandrake-10 - mkv,02.06.06 - mkCylinder = BRepPrimAPI_MakeCylinder(Draw::Atof(arg[3]), Draw::Atof(arg[4]), Draw::Atof(arg[5]));
98       BRepPrimAPI_MakeCylinder MakeCylinder(Draw::Atof(arg[3]), Draw::Atof(arg[4]), Draw::Atof(arg[5]));
99       mkCylinder = MakeCylinder;
100     }
101     mkCylinder.Build();
102
103     if(!mkCylinder.IsDone()) {
104       di << "The cylinder hasn't been built"  << "\n";
105       return 1;
106     }
107
108     QANewBRepNaming_Cylinder nameCylinder(L);
109     if (nb == 6) nameCylinder.Load(mkCylinder, QANewBRepNaming_SHELL);
110     else if (Draw::Atoi(arg[6]) == 0) nameCylinder.Load(mkCylinder, QANewBRepNaming_SHELL);
111     else nameCylinder.Load(mkCylinder, QANewBRepNaming_SOLID);
112
113     return 0;
114   }
115   di <<  "QANewDBRepNaming_NameCylinder : Error"  << "\n";
116   return 1;
117 }           
118
119
120 //=======================================================================
121 //function : QANewDBRepNaming_NameSphere
122 //purpose  : NameSphere Doc Label R X Y Z Angle1 Angle2 Angle3 [ShapeType(Shell/Solid)]
123 //=======================================================================
124
125  static Standard_Integer QANewDBRepNaming_NameSphere (Draw_Interpretor& di,
126                                                 Standard_Integer nb, 
127                                                 const char ** arg)
128  {
129    TDF_Label L;
130    if (!QADNaming::Entry(arg, L)) return 1;
131    Standard_Real R = Draw::Atof(arg[3]);
132    Standard_Real X = Draw::Atof(arg[4]);
133    Standard_Real Y = Draw::Atof(arg[5]);
134    Standard_Real Z = Draw::Atof(arg[6]);
135    Standard_Real DX = Draw::Atof(arg[7]);
136    Standard_Real DY = Draw::Atof(arg[8]);
137    Standard_Real DZ = Draw::Atof(arg[9]);
138    QANewBRepNaming_TypeOfPrimitive3D type = QANewBRepNaming_SHELL;
139    if (nb == 11) type = (QANewBRepNaming_TypeOfPrimitive3D) Draw::Atoi(arg[10]);
140    
141    QANewBRepNaming_Sphere nameSphere(L); 
142    BRepPrimAPI_MakeSphere mkSphere(R);
143    if (DX == 0 && DY == 0 && DZ == 0) {
144      // for Mandrake-10 - mkv,02.06.06 - mkSphere = BRepPrimAPI_MakeSphere(gp_Pnt(X, Y, Z), R);
145      BRepPrimAPI_MakeSphere MakeSphere1(gp_Pnt(X, Y, Z), R);
146      mkSphere = MakeSphere1;
147    } else {
148      // for Mandrake-10 - mkv,02.06.06 - mkSphere = BRepPrimAPI_MakeSphere(gp_Pnt(X, Y, Z), R, DX, DY, DZ);
149      BRepPrimAPI_MakeSphere MakeSphere2(gp_Pnt(X, Y, Z), R, DX, DY, DZ);
150      mkSphere = MakeSphere2;
151    }
152    mkSphere.Build();
153    if(!mkSphere.IsDone()) {
154      di << "The sphere hasn't been built"  << "\n";
155      return 1;
156    }
157    nameSphere.Load(mkSphere, type);
158    return 0;
159  }           
160
161 //===========================================================================
162 //function : QANewDBRepNaming_NamePrism
163 //purpose  : NamePrism Doc Label BasisLabel H Direction(X Y Z) [Inf/Semi-Inf]
164 //===========================================================================
165
166 static Standard_Integer QANewDBRepNaming_NamePrism (Draw_Interpretor& di,
167                                                Standard_Integer nb, 
168                                                const char ** arg)
169 {
170   if (nb < 5 || nb > 9 || nb == 6 || nb == 7) {
171     di <<  "QANewDBRepNaming_NamePrism : Error"  << "\n";
172     return 1;
173   }
174
175   TDF_Label L,BL;
176   if (!QADNaming::Entry(arg, L)) return 1;
177   if (!DDF::AddLabel(L.Data(), arg[3], BL)) return 1;  
178
179   Standard_Real H = Draw::Atof(arg[4]);
180
181   Handle(TNaming_NamedShape) BasisNS;
182   if (!BL.FindAttribute(TNaming_NamedShape::GetID(), BasisNS)) return 1;
183   const TopoDS_Shape& Basis = TNaming_Tool::CurrentShape(BasisNS);
184
185   QANewBRepNaming_Prism namePrism(L);
186
187   if (nb >= 8) {
188     gp_Dir Direction(Draw::Atof(arg[5]), Draw::Atof(arg[6]), Draw::Atof(arg[7]));
189     gp_Vec Vector(Direction);
190     Vector.Normalize();
191     Vector *= H;
192
193     if (nb == 8) {
194       BRepPrimAPI_MakePrism mkPrism(Basis, Vector);  
195       mkPrism.Build();
196       
197       if(!mkPrism.IsDone()) {
198         di << "The prism hasn't been built"  << "\n";
199         return 1;
200       }
201       
202       namePrism.Load(mkPrism, Basis);
203     }      
204     else if (nb == 9) {
205       BRepPrimAPI_MakePrism mkPrism(Basis, Direction, Draw::Atoi(arg[8]) != 0);  
206       mkPrism.Build();
207       
208       if(!mkPrism.IsDone()) {
209         di << "The prism hasn't been built"  << "\n";
210         return 1;
211       }
212       
213       namePrism.Load(mkPrism, Basis);
214     }
215   }
216   
217   return 0;
218 }           
219
220 //===========================================================================
221 //function : QANewDBRepNaming_NameRevol
222 //purpose  : NameRevol Doc Label BasisLabel AxisLabel Angle 
223 //===========================================================================
224
225 static Standard_Integer QANewDBRepNaming_NameRevol (Draw_Interpretor& di,
226                                                Standard_Integer nb, 
227                                                const char ** arg)
228 {
229   if (nb < 5 || nb > 6) {
230     di <<  "QANewDBRepNaming_NameRevol : Error"  << "\n";
231     return 1;
232   }
233
234   TDF_Label L, BL, AL;
235   if (!QADNaming::Entry(arg, L)) return 1;
236
237   if (!DDF::AddLabel(L.Data(), arg[3], BL)) return 1;  
238   if (!DDF::AddLabel(L.Data(), arg[4], AL)) return 1;  
239
240   Handle(TNaming_NamedShape) BasisNS, AxisNS;
241   if (!BL.FindAttribute(TNaming_NamedShape::GetID(), BasisNS)) return 1;
242   if (!AL.FindAttribute(TNaming_NamedShape::GetID(), AxisNS)) return 1;
243   const TopoDS_Shape& Basis = TNaming_Tool::CurrentShape(BasisNS);
244   const TopoDS_Shape& Axis = TNaming_Tool::CurrentShape(AxisNS);
245
246   if (Axis.ShapeType() != TopAbs_EDGE) return 1;
247
248   gp_Ax1 axis;
249   TopoDS_Vertex fV = TopExp::FirstVertex(TopoDS::Edge(Axis), Standard_True);
250   TopoDS_Vertex lV = TopExp::LastVertex(TopoDS::Edge(Axis), Standard_True);
251   if (!fV.IsNull() || !lV.IsNull()) {
252     gp_Vec alocalV(BRep_Tool::Pnt(fV),BRep_Tool::Pnt(lV));
253     gp_Dir Direction(alocalV);
254 //    gp_Dir Direction(gp_Vec(BRep_Tool::Pnt(fV), BRep_Tool::Pnt(lV)));
255     axis.SetLocation(BRep_Tool::Pnt(fV));
256     axis.SetDirection(Direction);
257   }
258   else {
259     TDataXtd_Geometry::Axis(AL, axis);
260   }
261
262   QANewBRepNaming_Revol nameRevol(L);
263
264   if(nb == 5) {
265
266     BRepPrimAPI_MakeRevol mkRevol(Basis, axis);  
267     mkRevol.Build();
268
269     if(!mkRevol.IsDone()) {
270       di << "The revol hasn't been built"  << "\n";
271       return 1;
272     }
273
274     nameRevol.Load(mkRevol, Basis);
275   }
276   else if (nb == 6) {
277
278     Standard_Real Angle = Draw::Atof(arg[5]);
279
280     BRepPrimAPI_MakeRevol mkRevol(Basis, axis, Angle);  
281     mkRevol.Build();
282
283     if(!mkRevol.IsDone()) {
284       di << "The revol hasn't been built"  << "\n";
285       return 1;
286     }
287
288     nameRevol.Load(mkRevol, Basis);    
289
290   }
291   
292   return 0;
293 }           
294
295 //===========================================================================
296 //function : QANewDBRepNaming_NameFillet
297 //purpose  : NameFillet Doc Label SourceShapeLabel PathLabel Radius
298 //===========================================================================
299
300 static Standard_Integer QANewDBRepNaming_NameFillet (Draw_Interpretor& di,
301                                                 Standard_Integer nb, 
302                                                 const char ** arg)
303 {
304   if (nb != 6) {
305     di <<  "QANewDBRepNaming_NameFillet : Error"  << "\n";
306     return 1;
307   }
308
309   TDF_Label L, BL, PL;
310   if (!QADNaming::Entry(arg, L)) return 1;
311
312   if (!DDF::AddLabel(L.Data(), arg[3], BL)) return 1;  
313   if (!DDF::AddLabel(L.Data(), arg[4], PL)) return 1;  
314   Standard_Real aRadius = Draw::Atoi(arg[5]);
315   Handle(TNaming_NamedShape) BasisNS, PathNS;
316   if (!BL.FindAttribute(TNaming_NamedShape::GetID(), BasisNS)) return 1;
317   if (!PL.FindAttribute(TNaming_NamedShape::GetID(), PathNS)) return 1;
318   const TopoDS_Shape& Basis = TNaming_Tool::CurrentShape(BasisNS);
319   const TopoDS_Shape& Path = TNaming_Tool::CurrentShape(PathNS);
320
321   BRepFilletAPI_MakeFillet aFillet(Basis);
322   TopExp_Explorer anExp(Path,TopAbs_EDGE);
323   if (anExp.More()) {
324     for(;anExp.More();anExp.Next()) {
325       aFillet.Add(aRadius,TopoDS::Edge(anExp.Current()));
326     }
327   } else aFillet.Add(aRadius,TopoDS::Edge(Path));
328   aFillet.Build();
329   if (!aFillet.IsDone()) return 1;
330   QANewBRepNaming_Fillet aNaming(L);
331   aNaming.Load(Basis, aFillet);
332   return 0;
333 }
334
335 //===========================================================================
336 //function : QANewDBRepNaming_NameChamfer
337 //purpose  : NameChamfer Doc Label SourceShapeLabel EdgeLabel FaceLabel Distance1 Distance2
338 //===========================================================================
339
340 static Standard_Integer QANewDBRepNaming_NameChamfer (Draw_Interpretor& di,
341                                                  Standard_Integer nb, 
342                                                  const char ** arg)
343 {
344   if (nb != 8) {
345     di <<  "QANewDBRepNaming_NameChamfer : Error"  << "\n";
346     return 1;
347   }
348
349   TDF_Label L, BL, EL, FL;
350   Standard_Real aDist1, aDist2;
351
352   if (!QADNaming::Entry(arg, L)) return 1;
353
354   if (!DDF::AddLabel(L.Data(), arg[3], BL)) return 1;  
355   if (!DDF::AddLabel(L.Data(), arg[4], EL)) return 1;  
356   if (!DDF::AddLabel(L.Data(), arg[5], FL)) return 1;  
357   aDist1 = Draw::Atoi(arg[6]);
358   aDist2 = Draw::Atoi(arg[7]);
359
360   Handle(TNaming_NamedShape) BasisNS, EdgeNS, FaceNS;
361   if (!BL.FindAttribute(TNaming_NamedShape::GetID(), BasisNS)) return 1;
362   if (!EL.FindAttribute(TNaming_NamedShape::GetID(), EdgeNS)) return 1;
363   if (!FL.FindAttribute(TNaming_NamedShape::GetID(), FaceNS)) return 1;
364   const TopoDS_Shape& Basis = TNaming_Tool::CurrentShape(BasisNS);
365   const TopoDS_Shape& Edge = TNaming_Tool::CurrentShape(EdgeNS);
366   const TopoDS_Shape& Face = TNaming_Tool::CurrentShape(FaceNS);
367
368   BRepFilletAPI_MakeChamfer aChamfer(Basis);
369   TopExp_Explorer anExp(Edge,TopAbs_EDGE);
370   if (anExp.More()) {
371     for(;anExp.More();anExp.Next()) {
372       aChamfer.Add(aDist1,aDist2,TopoDS::Edge(anExp.Current()),TopoDS::Face(Face));
373     }
374   } else {aChamfer.Add(aDist1,aDist2,TopoDS::Edge(Edge),TopoDS::Face(Face));}
375   aChamfer.Build();
376   if (!aChamfer.IsDone()) return 1;
377   QANewBRepNaming_Chamfer aNaming(L);
378   aNaming.Load(Basis, aChamfer);
379   return 0;
380 }
381
382 //==============================================================
383 //function : QANewDBRepNaming_NameImportShape
384 //purpose  : NameImportShape Doc Label DrawShape 
385 //==============================================================
386
387 static Standard_Integer QANewDBRepNaming_NameImportShape (Draw_Interpretor& di,
388                                                      Standard_Integer nb, 
389                                                      const char ** arg)
390 {
391   if (nb != 4) {
392     di <<  "QANewDBRepNaming_NameImportShape : Error"  << "\n";
393     return 1;
394   }
395
396   Handle(TDF_Data) D;
397   if (!DDF::GetDF(arg[1],D)) return 1;   
398   TDF_Label L;
399   if (!DDF::AddLabel(D, arg[2], L)) return 1;  
400   
401   const TopoDS_Shape& S = DBRep::Get(arg[3]);
402
403   QANewBRepNaming_ImportShape nameIS(L);
404   nameIS.Load(S);
405
406   return 0;
407 }    
408
409
410 //=======================================================================
411 //function : PrimitiveCommands
412 //purpose  : 
413 //=======================================================================
414
415 void QANewDBRepNaming::PrimitiveCommands (Draw_Interpretor& theCommands)
416 {  
417   static Standard_Boolean done = Standard_False;
418   if (done) return;
419   done = Standard_True;
420   const char* g = "Naming algorithm commands for primitives" ;
421   
422
423   theCommands.Add ("NameBox", 
424                    "NameBox Doc Label dx dy dz",
425                    __FILE__, QANewDBRepNaming_NameBox, g);
426
427   theCommands.Add ("NameCylinder", 
428                    "NameCylinder Doc Label R H Angle [ShapeType(Shell/Solid)]",
429                    __FILE__, QANewDBRepNaming_NameCylinder, g);
430
431   theCommands.Add ("NameSphere", 
432                    "NameSphere Doc Label R X Y Z Angle1 Angle2 Angle3 [ShapeType(Shell/Solid)]",
433                    __FILE__, QANewDBRepNaming_NameSphere, g);
434
435   theCommands.Add ("NamePrism", 
436                    "NamePrism Doc Label BasisLabel H Direction(X Y Z) Inf(1/0, by feafault = 0)",
437                    __FILE__, QANewDBRepNaming_NamePrism, g);
438
439   theCommands.Add ("NameRevol", 
440                    "NameRevol Doc Label BasisLabel AxisLabel Angle",
441                    __FILE__, QANewDBRepNaming_NameRevol, g);
442
443   theCommands.Add ("NameFillet", 
444                    "NameFillet Doc Label SourceShapeLabel PathLabel Radius",
445                    __FILE__, QANewDBRepNaming_NameFillet, g);
446
447   theCommands.Add ("NameChamfer", 
448                    "NameChamfer Doc Label SourceShapeLabel EdgeLabel FaceLabel Distance1 Distance2",
449                    __FILE__, QANewDBRepNaming_NameChamfer, g);
450
451   theCommands.Add ("NameImportShape", 
452                    "NameImportShape Doc Label DrawShape",
453                    __FILE__, QANewDBRepNaming_NameImportShape, g);
454
455 }