0030058: Visualization, Select3D_SensitivePrimitiveArray - the selection is not fast...
[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   aBuilder.SetToFillHistory(BRepTest_Objects::IsHistoryNeeded());
195   //
196   //
197   OSD_Timer aTimer;
198   aTimer.Start();
199   //
200   aBuilder.PerformWithFiller(aPF); 
201   BOPTest::ReportAlerts(aBuilder.GetReport());
202
203   // Set history of GF operation into the session
204   if (BRepTest_Objects::IsHistoryNeeded())
205     BRepTest_Objects::SetHistory(aPF.Arguments(), aBuilder);
206
207   if (aBuilder.HasErrors()) {
208     return 0;
209   }
210   //
211   aTimer.Stop();
212   //
213   if (bShowTime)
214   {
215     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
216     di << buf;
217   }
218   //
219   const TopoDS_Shape& aR=aBuilder.Shape();
220   if (aR.IsNull()) {
221     di << " null shape\n";
222     return 0;
223   }
224   //
225   DBRep::Set(a[1], aR);
226   return 0;
227 }
228 //=======================================================================
229 //function : bbop
230 //purpose  : 
231 //=======================================================================
232 Standard_Integer bbop(Draw_Interpretor& di, 
233                       Standard_Integer n, 
234                       const char** a) 
235
236   if (n<3) {
237     di << " use bbop r op [-t]\n";
238     return 0;
239   }
240   //
241   BOPDS_PDS pDS=BOPTest_Objects::PDS();
242   if (!pDS) {
243     di << " prepare PaveFiller first\n";
244     return 0;
245   }
246   //
247   char buf[32];
248   Standard_Boolean bRunParallel, bShowTime;
249   Standard_Integer iOp, i;
250   BOPAlgo_Operation aOp;
251   TopTools_ListIteratorOfListOfShape aIt; 
252   //
253   iOp=Draw::Atoi(a[2]);
254   if (iOp<0 || iOp>4) {
255     di << " invalid operation type\n";
256     return 0;
257   }
258   aOp=(BOPAlgo_Operation)iOp;
259   //
260   bShowTime=Standard_False;
261   bRunParallel=BOPTest_Objects::RunParallel();
262   for (i=3; i<n; ++i) {
263     if (!strcmp(a[i], "-t")) {
264       bShowTime=Standard_True;
265     }
266   }
267   //
268   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
269   //
270   BOPAlgo_Builder *pBuilder=NULL;
271   
272   if (aOp!=BOPAlgo_SECTION) { 
273     pBuilder=&BOPTest_Objects::BOP();
274   } 
275   else {
276     pBuilder=&BOPTest_Objects::Section();
277   }
278   //
279   pBuilder->Clear();
280   //
281   TopTools_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
282   aIt.Initialize(aLSObj);
283   for (; aIt.More(); aIt.Next()) {
284     const TopoDS_Shape& aS=aIt.Value();
285     pBuilder->AddArgument(aS);
286   }
287   //
288   if (aOp!=BOPAlgo_SECTION) {
289     BOPAlgo_BOP *pBOP=(BOPAlgo_BOP *)pBuilder;
290     //
291     TopTools_ListOfShape& aLSTools=BOPTest_Objects::Tools();
292     aIt.Initialize(aLSTools);
293     for (; aIt.More(); aIt.Next()) {
294       const TopoDS_Shape& aS=aIt.Value();
295       pBOP->AddTool(aS);
296     }
297     //
298     pBOP->SetOperation(aOp);
299   }
300   else {
301     TopTools_ListOfShape& aLSTools=BOPTest_Objects::Tools();
302     aIt.Initialize(aLSTools);
303     for (; aIt.More(); aIt.Next()) {
304       const TopoDS_Shape& aS=aIt.Value();
305       pBuilder->AddArgument(aS);
306     }
307   }
308   //
309   pBuilder->SetRunParallel(bRunParallel);
310   pBuilder->SetCheckInverted(BOPTest_Objects::CheckInverted());
311   pBuilder->SetToFillHistory(BRepTest_Objects::IsHistoryNeeded());
312   //
313   OSD_Timer aTimer;
314   aTimer.Start();
315   //
316   pBuilder->PerformWithFiller(aPF);
317   BOPTest::ReportAlerts(pBuilder->GetReport());
318
319   // Set history of Boolean operation into the session
320   if (BRepTest_Objects::IsHistoryNeeded())
321     BRepTest_Objects::SetHistory(aPF.Arguments(), *pBuilder);
322
323   if (pBuilder->HasErrors()) {
324     return 0;
325   }
326   //
327   aTimer.Stop();
328   //
329   if (bShowTime) {
330     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
331     di << buf;
332   }
333   //
334   const TopoDS_Shape& aR=pBuilder->Shape();
335   if (aR.IsNull()) {
336     di << " null shape\n";
337     return 0;
338   }
339   //
340   BOPTest_Objects::SetBuilder(pBuilder);
341   //
342   DBRep::Set(a[1], aR);
343   return 0;
344 }
345
346 //=======================================================================
347 //function : bsplit
348 //purpose  : 
349 //=======================================================================
350 Standard_Integer bsplit(Draw_Interpretor& di,
351                         Standard_Integer n,
352                         const char** a)
353
354   if (n < 2) {
355     di << " use bsplit r [-t (show time)]\n";
356     return 1;
357   }
358   //
359   BOPDS_PDS pDS = BOPTest_Objects::PDS();
360   if (!pDS) {
361     di << " prepare PaveFiller first\n";
362     return 0;
363   }
364   //
365   BOPAlgo_PaveFiller& aPF = BOPTest_Objects::PaveFiller();
366   //
367   BOPAlgo_Splitter* pSplitter = &BOPTest_Objects::Splitter();
368   pSplitter->Clear();
369   //
370   // set objects
371   const TopTools_ListOfShape& aLSObjects = BOPTest_Objects::Shapes();
372   pSplitter->SetArguments(aLSObjects);
373   //
374   // set tools
375   TopTools_ListOfShape& aLSTools = BOPTest_Objects::Tools();
376   pSplitter->SetTools(aLSTools);
377   //
378   // set options
379   pSplitter->SetRunParallel(BOPTest_Objects::RunParallel());
380   pSplitter->SetNonDestructive(BOPTest_Objects::NonDestructive());
381   pSplitter->SetFuzzyValue(BOPTest_Objects::FuzzyValue());
382   pSplitter->SetCheckInverted(BOPTest_Objects::CheckInverted());
383   pSplitter->SetToFillHistory(BRepTest_Objects::IsHistoryNeeded());
384   //
385   // measure the time of the operation
386   OSD_Timer aTimer;
387   aTimer.Start();
388   //
389   // perform the operation
390   pSplitter->PerformWithFiller(aPF);
391   //
392   aTimer.Stop();
393   BOPTest::ReportAlerts(pSplitter->GetReport());
394
395   // Set history of Split operation into the session
396   if (BRepTest_Objects::IsHistoryNeeded())
397     BRepTest_Objects::SetHistory(aPF.Arguments(), *pSplitter);
398
399   if (pSplitter->HasErrors()) {
400     return 0;
401   }
402   //
403   // show time if necessary
404   if (n == 3 && !strcmp(a[2], "-t")) {
405     char buf[20];
406     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
407     di << buf;
408   }
409   //
410   // Debug commands support
411   BOPTest_Objects::SetBuilder(pSplitter);
412   //
413   const TopoDS_Shape& aR = pSplitter->Shape();
414   if (aR.IsNull()) {
415     di << " null shape\n";
416     return 0;
417   }
418   //
419   DBRep::Set(a[1], aR);
420   return 0;
421 }