0025700: Ensuring uniform control of the functionalities of the Boolean operations...
[occt.git] / src / BOPTest / BOPTest_PartitionCommands.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <BOPTest.ixx>
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #include <NCollection_IncAllocator.hxx>
21
22 #include <DBRep.hxx>
23 #include <Draw.hxx>
24 #include <Draw_Color.hxx>
25 #include <DrawTrSurf.hxx>
26
27 #include <TopoDS_Shape.hxx>
28 //
29 #include <BOPAlgo_Builder.hxx>
30 #include <BOPAlgo_PaveFiller.hxx>
31 #include <BOPAlgo_Operation.hxx>
32 #include <BOPAlgo_BOP.hxx>
33 #include <BOPAlgo_Section.hxx>
34 //
35 #include <BOPTest_DrawableShape.hxx>
36 #include <BOPTest_Objects.hxx>
37
38 #include <BOPTest_Chronometer.hxx>
39
40 static Standard_Integer bfillds  (Draw_Interpretor&, Standard_Integer, const char**); 
41 static Standard_Integer bbuild   (Draw_Interpretor&, Standard_Integer, const char**);
42 static Standard_Integer bbop     (Draw_Interpretor&, Standard_Integer, const char**);
43
44 //=======================================================================
45 //function : PartitionCommands
46 //purpose  : 
47 //=======================================================================
48 void BOPTest::PartitionCommands(Draw_Interpretor& theCommands)
49 {
50   static Standard_Boolean done = Standard_False;
51   if (done) return;
52   done = Standard_True;
53   // Chapter's name
54   const char* g = "BOPTest commands";
55   // Commands  
56   theCommands.Add("bfillds", "use bfillds [-t]"  , __FILE__, bfillds, g);
57   theCommands.Add("bbuild" , "use bbuild r [-t]" , __FILE__, bbuild, g);
58   theCommands.Add("bbop"   , "use bbop r op [-t]", __FILE__, bbop, g);
59 }
60 //=======================================================================
61 //function : bfillds
62 //purpose  : 
63 //=======================================================================
64 Standard_Integer bfillds(Draw_Interpretor& di, 
65                          Standard_Integer n, 
66                          const char** a) 
67
68   if (n > 2) {
69     di << " use bfillds [-t]\n";
70     return 0;
71   }
72   //
73   char buf[32];
74   Standard_Boolean bRunParallel, bShowTime;
75   Standard_Integer i, aNbS, iErr;
76   Standard_Real aTol;
77   BOPCol_ListIteratorOfListOfShape aIt;
78   BOPCol_ListOfShape aLC;
79   BOPTest_Chronometer aChrono;
80   
81   BOPCol_ListOfShape& aLS=BOPTest_Objects::Shapes();
82   aNbS=aLS.Extent();
83   if (!aNbS) {
84     di << " no objects to process\n";
85     return 0;
86   }
87   //
88   bShowTime = Standard_False;
89   //
90   bRunParallel=BOPTest_Objects::RunParallel();
91   aTol=BOPTest_Objects::FuzzyValue();
92   //
93   for (i=1; i<n; ++i) {
94     if (!strcmp(a[i], "-t")) {
95       bShowTime=Standard_True;
96     }
97   }
98   //
99   BOPCol_ListOfShape& aLT=BOPTest_Objects::Tools();
100   //
101   aIt.Initialize(aLS);
102   for (; aIt.More(); aIt.Next()) {
103     const TopoDS_Shape& aS=aIt.Value();
104     aLC.Append(aS);
105   }
106   //
107   aIt.Initialize(aLT);
108   for (; aIt.More(); aIt.Next()) {
109     const TopoDS_Shape& aS=aIt.Value();
110     aLC.Append(aS);
111   }
112   //
113   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
114   //
115   aPF.SetArguments(aLC);
116   aPF.SetRunParallel(bRunParallel);
117   aPF.SetFuzzyValue(aTol);
118   //
119   aChrono.Start();
120   //
121   aPF.Perform();
122   iErr=aPF.ErrorStatus();
123   if (iErr) {
124     Sprintf(buf, " error: %d\n",  iErr);
125     di << buf;
126     return 0;
127   }
128   //
129   aChrono.Stop();
130   //
131   if (bShowTime) {
132     Standard_Real aTime;
133     //
134     aTime=aChrono.Time();
135     Sprintf(buf, "  Tps: %7.2lf\n", aTime);
136     di << buf;
137   }
138   //
139   return 0;
140 }
141 //=======================================================================
142 //function : bbuild
143 //purpose  : 
144 //=======================================================================
145 Standard_Integer bbuild(Draw_Interpretor& di,
146                         Standard_Integer n, 
147                         const char** a) 
148
149   if (n<2) {
150     di << " use bbuild r [-t]\n";
151     return 0;
152   }
153   //
154   BOPDS_PDS pDS=BOPTest_Objects::PDS();
155   if (!pDS) {
156     di << " prepare PaveFiller first\n";
157     return 0;
158   }
159   //
160   char buf[128];
161   Standard_Boolean bRunParallel, bShowTime;
162   Standard_Integer i, iErr;
163   
164   BOPTest_Chronometer aChrono;
165   BOPCol_ListIteratorOfListOfShape aIt;
166   //
167   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
168   //
169   BOPAlgo_Builder& aBuilder=BOPTest_Objects::Builder();
170   aBuilder.Clear();
171   //
172   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
173   aIt.Initialize(aLSObj);
174   for (; aIt.More(); aIt.Next()) {
175     const TopoDS_Shape& aS=aIt.Value();
176     aBuilder.AddArgument(aS);
177   }
178   //
179   BOPCol_ListOfShape& aLSTool=BOPTest_Objects::Tools();
180   aIt.Initialize(aLSTool);
181   for (; aIt.More(); aIt.Next()) {
182     const TopoDS_Shape& aS=aIt.Value();
183     aBuilder.AddArgument(aS);
184   }
185   //
186   bShowTime=Standard_False;
187   bRunParallel=BOPTest_Objects::RunParallel();
188   for (i=2; i<n; ++i) {
189     if (!strcmp(a[i], "-t")) {
190       bShowTime=Standard_True;
191     }
192   }
193   aBuilder.SetRunParallel(bRunParallel);
194   //
195   //
196   aChrono.Start();
197   //
198   aBuilder.PerformWithFiller(aPF); 
199   iErr=aBuilder.ErrorStatus();
200   if (iErr) {
201     Sprintf(buf, " error: %d\n",  iErr);
202     di << buf;
203     return 0;
204   }
205   //
206   aChrono.Stop();
207   //
208   if (bShowTime) {
209     Standard_Real aTime;
210     //
211     aTime=aChrono.Time();
212     Sprintf(buf, "  Tps: %7.2lf\n", aTime);
213     di << buf;
214   }
215   //
216   const TopoDS_Shape& aR=aBuilder.Shape();
217   if (aR.IsNull()) {
218     di << " null shape\n";
219     return 0;
220   }
221   //
222   DBRep::Set(a[1], aR);
223   return 0;
224 }
225 //=======================================================================
226 //function : bbop
227 //purpose  : 
228 //=======================================================================
229 Standard_Integer bbop(Draw_Interpretor& di, 
230                       Standard_Integer n, 
231                       const char** a) 
232
233   if (n<3) {
234     di << " use bbop r op [-t]\n";
235     return 0;
236   }
237   //
238   BOPDS_PDS pDS=BOPTest_Objects::PDS();
239   if (!pDS) {
240     di << " prepare PaveFiller first\n";
241     return 0;
242   }
243   //
244   char buf[32];
245   Standard_Boolean bRunParallel, bShowTime;
246   Standard_Integer iErr, iOp, i;
247   BOPAlgo_Operation aOp;
248   BOPCol_ListIteratorOfListOfShape aIt; 
249   BOPTest_Chronometer aChrono;
250   //
251   iOp=Draw::Atoi(a[2]);
252   if (iOp<0 || iOp>4) {
253     di << " invalid operation type\n";
254     return 0;
255   }
256   aOp=(BOPAlgo_Operation)iOp;
257   //
258   bShowTime=Standard_False;
259   bRunParallel=BOPTest_Objects::RunParallel();
260   for (i=3; i<n; ++i) {
261     if (!strcmp(a[i], "-t")) {
262       bShowTime=Standard_True;
263     }
264   }
265   //
266   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
267   //
268   BOPAlgo_Builder *pBuilder=NULL;
269   
270   if (aOp!=BOPAlgo_SECTION) { 
271     pBuilder=&BOPTest_Objects::BOP();
272   } 
273   else {
274     pBuilder=&BOPTest_Objects::Section();
275   }
276   //
277   pBuilder->Clear();
278   //
279   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
280   aIt.Initialize(aLSObj);
281   for (; aIt.More(); aIt.Next()) {
282     const TopoDS_Shape& aS=aIt.Value();
283     pBuilder->AddArgument(aS);
284   }
285   //
286   if (aOp!=BOPAlgo_SECTION) {
287     BOPAlgo_BOP *pBOP=(BOPAlgo_BOP *)pBuilder;
288     //
289     BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
290     aIt.Initialize(aLSTools);
291     for (; aIt.More(); aIt.Next()) {
292       const TopoDS_Shape& aS=aIt.Value();
293       pBOP->AddTool(aS);
294     }
295     //
296     pBOP->SetOperation(aOp);
297   }
298   else {
299     BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
300     aIt.Initialize(aLSTools);
301     for (; aIt.More(); aIt.Next()) {
302       const TopoDS_Shape& aS=aIt.Value();
303       pBuilder->AddArgument(aS);
304     }
305   }
306   //
307   pBuilder->SetRunParallel(bRunParallel);
308   //
309   aChrono.Start();
310   //
311   pBuilder->PerformWithFiller(aPF);
312   iErr=pBuilder->ErrorStatus();
313   if (iErr) {
314     Sprintf(buf, " error: %d\n",  iErr);
315     di << buf;
316     return 0;
317   }
318   //
319   aChrono.Stop();
320   //
321   if (bShowTime) {
322     Standard_Real aTime;
323     //
324     aTime=aChrono.Time();
325     Sprintf(buf, "  Tps: %7.2lf\n", aTime);
326     di << buf;
327   }
328   //
329   const TopoDS_Shape& aR=pBuilder->Shape();
330   if (aR.IsNull()) {
331     di << " null shape\n";
332     return 0;
333   }
334   //
335   DBRep::Set(a[1], aR);
336   return 0;
337 }
338