7be074fcd42a530424e03a30659069cf5542e384
[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
16 #include <BOPAlgo_BOP.hxx>
17 #include <BOPAlgo_Builder.hxx>
18 #include <BOPAlgo_Operation.hxx>
19 #include <BOPAlgo_PaveFiller.hxx>
20 #include <BOPAlgo_Section.hxx>
21 #include <BOPAlgo_Splitter.hxx>
22 #include <BOPTest.hxx>
23 #include <BOPTest_DrawableShape.hxx>
24 #include <BOPTest_Objects.hxx>
25 #include <BRepTest_Objects.hxx>
26 #include <DBRep.hxx>
27 #include <Draw.hxx>
28 #include <Draw_Color.hxx>
29 #include <DrawTrSurf.hxx>
30 #include <OSD_Timer.hxx>
31 #include <TopoDS_Shape.hxx>
32
33 #include <stdio.h>
34 #include <string.h>
35 //
36 //
37 static Standard_Integer bfillds  (Draw_Interpretor&, Standard_Integer, const char**);
38 static Standard_Integer bbuild   (Draw_Interpretor&, Standard_Integer, const char**);
39 static Standard_Integer bbop     (Draw_Interpretor&, Standard_Integer, const char**);
40 static Standard_Integer bsplit   (Draw_Interpretor&, Standard_Integer, const char**);
41
42 //=======================================================================
43 //function : PartitionCommands
44 //purpose  : 
45 //=======================================================================
46 void BOPTest::PartitionCommands(Draw_Interpretor& theCommands)
47 {
48   static Standard_Boolean done = Standard_False;
49   if (done) return;
50   done = Standard_True;
51   // Chapter's name
52   const char* g = "BOPTest commands";
53   // Commands  
54   theCommands.Add("bfillds", "use bfillds [-t]"  , __FILE__, bfillds, g);
55   theCommands.Add("bbuild" , "use bbuild r [-t]" , __FILE__, bbuild, g);
56   theCommands.Add("bbop"   , "use bbop r op [-t]", __FILE__, bbop, g);
57   theCommands.Add("bsplit" , "use bsplit r [-t]" , __FILE__, bsplit, g);
58 }
59 //=======================================================================
60 //function : bfillds
61 //purpose  : 
62 //=======================================================================
63 Standard_Integer bfillds(Draw_Interpretor& di, 
64                          Standard_Integer n, 
65                          const char** a) 
66
67   if (n > 2) {
68     di << " use bfillds [-t]\n";
69     return 0;
70   }
71   //
72   char buf[32];
73   Standard_Boolean bRunParallel, bNonDestructive, bShowTime;
74   Standard_Integer i, aNbS;
75   Standard_Real aTol;
76   TopTools_ListIteratorOfListOfShape aIt;
77   TopTools_ListOfShape aLC;
78   TopTools_ListOfShape& aLS=BOPTest_Objects::Shapes();
79   aNbS=aLS.Extent();
80   if (!aNbS) {
81     di << " no objects to process\n";
82     return 0;
83   }
84   //
85   bShowTime = Standard_False;
86   //
87   bRunParallel=BOPTest_Objects::RunParallel();
88   bNonDestructive = BOPTest_Objects::NonDestructive();
89   aTol = BOPTest_Objects::FuzzyValue();
90   BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
91   //
92   for (i=1; i<n; ++i) {
93     if (!strcmp(a[i], "-t")) {
94       bShowTime=Standard_True;
95     }
96   }
97   //
98   TopTools_ListOfShape& aLT=BOPTest_Objects::Tools();
99   //
100   aIt.Initialize(aLS);
101   for (; aIt.More(); aIt.Next()) {
102     const TopoDS_Shape& aS=aIt.Value();
103     aLC.Append(aS);
104   }
105   //
106   aIt.Initialize(aLT);
107   for (; aIt.More(); aIt.Next()) {
108     const TopoDS_Shape& aS=aIt.Value();
109      aLC.Append(aS);
110   }
111   //
112   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
113   //
114   aPF.SetArguments(aLC);
115   aPF.SetRunParallel(bRunParallel);
116   aPF.SetNonDestructive(bNonDestructive);
117   aPF.SetFuzzyValue(aTol);
118   aPF.SetGlue(aGlue);
119   aPF.SetUseOBB(BOPTest_Objects::UseOBB());
120   //
121   OSD_Timer aTimer;
122   aTimer.Start();
123   //
124   aPF.Perform();
125   BOPTest::ReportAlerts(aPF.GetReport());
126   if (aPF.HasErrors()) {
127     return 0;
128   }
129   //
130   aTimer.Stop();
131   //
132   if (bShowTime)
133   {
134     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
135     di << buf;
136   }
137   //
138   return 0;
139 }
140 //=======================================================================
141 //function : bbuild
142 //purpose  : 
143 //=======================================================================
144 Standard_Integer bbuild(Draw_Interpretor& di,
145                         Standard_Integer n, 
146                         const char** a) 
147
148   if (n<2) {
149     di << " use bbuild r [-t]\n";
150     return 0;
151   }
152   //
153   BOPDS_PDS pDS=BOPTest_Objects::PDS();
154   if (!pDS) {
155     di << " prepare PaveFiller first\n";
156     return 0;
157   }
158   //
159   char buf[128];
160   Standard_Boolean bRunParallel, bShowTime;
161   Standard_Integer i;
162
163   TopTools_ListIteratorOfListOfShape aIt;
164   //
165   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
166   //
167   BOPTest_Objects::SetBuilderDefault();
168   BOPAlgo_Builder& aBuilder=BOPTest_Objects::Builder();
169   aBuilder.Clear();
170   //
171   TopTools_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
172   aIt.Initialize(aLSObj);
173   for (; aIt.More(); aIt.Next()) {
174     const TopoDS_Shape& aS=aIt.Value();
175     aBuilder.AddArgument(aS);
176   }
177   //
178   TopTools_ListOfShape& aLSTool=BOPTest_Objects::Tools();
179   aIt.Initialize(aLSTool);
180   for (; aIt.More(); aIt.Next()) {
181     const TopoDS_Shape& aS=aIt.Value();
182     aBuilder.AddArgument(aS);
183   }
184   //
185   bShowTime=Standard_False;
186   bRunParallel=BOPTest_Objects::RunParallel();
187   for (i=2; i<n; ++i) {
188     if (!strcmp(a[i], "-t")) {
189       bShowTime=Standard_True;
190     }
191   }
192   aBuilder.SetRunParallel(bRunParallel);
193   aBuilder.SetCheckInverted(BOPTest_Objects::CheckInverted());
194   //
195   //
196   OSD_Timer aTimer;
197   aTimer.Start();
198   //
199   aBuilder.PerformWithFiller(aPF); 
200   BOPTest::ReportAlerts(aBuilder.GetReport());
201
202   // Set history of GF operation into the session
203   BRepTest_Objects::SetHistory(aPF.Arguments(), aBuilder);
204
205   if (aBuilder.HasErrors()) {
206     return 0;
207   }
208   //
209   aTimer.Stop();
210   //
211   if (bShowTime)
212   {
213     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
214     di << buf;
215   }
216   //
217   const TopoDS_Shape& aR=aBuilder.Shape();
218   if (aR.IsNull()) {
219     di << " null shape\n";
220     return 0;
221   }
222   //
223   DBRep::Set(a[1], aR);
224   return 0;
225 }
226 //=======================================================================
227 //function : bbop
228 //purpose  : 
229 //=======================================================================
230 Standard_Integer bbop(Draw_Interpretor& di, 
231                       Standard_Integer n, 
232                       const char** a) 
233
234   if (n<3) {
235     di << " use bbop r op [-t]\n";
236     return 0;
237   }
238   //
239   BOPDS_PDS pDS=BOPTest_Objects::PDS();
240   if (!pDS) {
241     di << " prepare PaveFiller first\n";
242     return 0;
243   }
244   //
245   char buf[32];
246   Standard_Boolean bRunParallel, bShowTime;
247   Standard_Integer iOp, i;
248   BOPAlgo_Operation aOp;
249   TopTools_ListIteratorOfListOfShape aIt; 
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   TopTools_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     TopTools_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     TopTools_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   pBuilder->SetCheckInverted(BOPTest_Objects::CheckInverted());
309   //
310   OSD_Timer aTimer;
311   aTimer.Start();
312   //
313   pBuilder->PerformWithFiller(aPF);
314   BOPTest::ReportAlerts(pBuilder->GetReport());
315
316   // Set history of Boolean operation into the session
317   BRepTest_Objects::SetHistory(aPF.Arguments(), *pBuilder);
318
319   if (pBuilder->HasErrors()) {
320     return 0;
321   }
322   //
323   aTimer.Stop();
324   //
325   if (bShowTime) {
326     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
327     di << buf;
328   }
329   //
330   const TopoDS_Shape& aR=pBuilder->Shape();
331   if (aR.IsNull()) {
332     di << " null shape\n";
333     return 0;
334   }
335   //
336   BOPTest_Objects::SetBuilder(pBuilder);
337   //
338   DBRep::Set(a[1], aR);
339   return 0;
340 }
341
342 //=======================================================================
343 //function : bsplit
344 //purpose  : 
345 //=======================================================================
346 Standard_Integer bsplit(Draw_Interpretor& di,
347                         Standard_Integer n,
348                         const char** a)
349
350   if (n < 2) {
351     di << " use bsplit r [-t (show time)]\n";
352     return 1;
353   }
354   //
355   BOPDS_PDS pDS = BOPTest_Objects::PDS();
356   if (!pDS) {
357     di << " prepare PaveFiller first\n";
358     return 0;
359   }
360   //
361   BOPAlgo_PaveFiller& aPF = BOPTest_Objects::PaveFiller();
362   //
363   BOPAlgo_Splitter* pSplitter = &BOPTest_Objects::Splitter();
364   pSplitter->Clear();
365   //
366   // set objects
367   const TopTools_ListOfShape& aLSObjects = BOPTest_Objects::Shapes();
368   pSplitter->SetArguments(aLSObjects);
369   //
370   // set tools
371   TopTools_ListOfShape& aLSTools = BOPTest_Objects::Tools();
372   pSplitter->SetTools(aLSTools);
373   //
374   // set options
375   pSplitter->SetRunParallel(BOPTest_Objects::RunParallel());
376   pSplitter->SetNonDestructive(BOPTest_Objects::NonDestructive());
377   pSplitter->SetFuzzyValue(BOPTest_Objects::FuzzyValue());
378   pSplitter->SetCheckInverted(BOPTest_Objects::CheckInverted());
379   //
380   // measure the time of the operation
381   OSD_Timer aTimer;
382   aTimer.Start();
383   //
384   // perform the operation
385   pSplitter->PerformWithFiller(aPF);
386   //
387   aTimer.Stop();
388   BOPTest::ReportAlerts(pSplitter->GetReport());
389
390   // Set history of Split operation into the session
391   BRepTest_Objects::SetHistory(aPF.Arguments(), *pSplitter);
392
393   if (pSplitter->HasErrors()) {
394     return 0;
395   }
396   //
397   // show time if necessary
398   if (n == 3 && !strcmp(a[2], "-t")) {
399     char buf[20];
400     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
401     di << buf;
402   }
403   //
404   // Debug commands support
405   BOPTest_Objects::SetBuilder(pSplitter);
406   //
407   const TopoDS_Shape& aR = pSplitter->Shape();
408   if (aR.IsNull()) {
409     di << " null shape\n";
410     return 0;
411   }
412   //
413   DBRep::Set(a[1], aR);
414   return 0;
415 }