0026392: Visualization, TKD3DHost - provide straight-forward base for integration...
[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 <BOPTest.hxx>
22 #include <BOPTest_DrawableShape.hxx>
23 #include <BOPTest_Objects.hxx>
24 #include <DBRep.hxx>
25 #include <Draw.hxx>
26 #include <Draw_Color.hxx>
27 #include <DrawTrSurf.hxx>
28 #include <NCollection_IncAllocator.hxx>
29 #include <OSD_Timer.hxx>
30 #include <TopoDS_Shape.hxx>
31
32 #include <stdio.h>
33 #include <string.h>
34 //
35 //
36 static Standard_Integer bfillds  (Draw_Interpretor&, Standard_Integer, const char**); 
37 static Standard_Integer bbuild   (Draw_Interpretor&, Standard_Integer, const char**);
38 static Standard_Integer bbop     (Draw_Interpretor&, Standard_Integer, const char**);
39
40 //=======================================================================
41 //function : PartitionCommands
42 //purpose  : 
43 //=======================================================================
44 void BOPTest::PartitionCommands(Draw_Interpretor& theCommands)
45 {
46   static Standard_Boolean done = Standard_False;
47   if (done) return;
48   done = Standard_True;
49   // Chapter's name
50   const char* g = "BOPTest commands";
51   // Commands  
52   theCommands.Add("bfillds", "use bfillds [-t]"  , __FILE__, bfillds, g);
53   theCommands.Add("bbuild" , "use bbuild r [-t]" , __FILE__, bbuild, g);
54   theCommands.Add("bbop"   , "use bbop r op [-t]", __FILE__, bbop, g);
55 }
56 //=======================================================================
57 //function : bfillds
58 //purpose  : 
59 //=======================================================================
60 Standard_Integer bfillds(Draw_Interpretor& di, 
61                          Standard_Integer n, 
62                          const char** a) 
63
64   if (n > 2) {
65     di << " use bfillds [-t]\n";
66     return 0;
67   }
68   //
69   char buf[32];
70   Standard_Boolean bRunParallel, bShowTime;
71   Standard_Integer i, aNbS, iErr;
72   Standard_Real aTol;
73   BOPCol_ListIteratorOfListOfShape aIt;
74   BOPCol_ListOfShape aLC;
75   BOPCol_ListOfShape& aLS=BOPTest_Objects::Shapes();
76   aNbS=aLS.Extent();
77   if (!aNbS) {
78     di << " no objects to process\n";
79     return 0;
80   }
81   //
82   bShowTime = Standard_False;
83   //
84   bRunParallel=BOPTest_Objects::RunParallel();
85   aTol=BOPTest_Objects::FuzzyValue();
86   //
87   for (i=1; i<n; ++i) {
88     if (!strcmp(a[i], "-t")) {
89       bShowTime=Standard_True;
90     }
91     }
92   //
93   BOPCol_ListOfShape& aLT=BOPTest_Objects::Tools();
94   //
95   aIt.Initialize(aLS);
96   for (; aIt.More(); aIt.Next()) {
97     const TopoDS_Shape& aS=aIt.Value();
98     aLC.Append(aS);
99   }
100   //
101   aIt.Initialize(aLT);
102   for (; aIt.More(); aIt.Next()) {
103     const TopoDS_Shape& aS=aIt.Value();
104      aLC.Append(aS);
105   }
106   //
107   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
108   //
109   aPF.SetArguments(aLC);
110   aPF.SetRunParallel(bRunParallel);
111   aPF.SetFuzzyValue(aTol);
112   //
113   OSD_Timer aTimer;
114   aTimer.Start();
115   //
116   aPF.Perform();
117   iErr=aPF.ErrorStatus();
118   if (iErr) {
119     Sprintf(buf, " error: %d\n",  iErr);
120     di << buf;
121     return 0;
122   }
123   //
124   aTimer.Stop();
125   //
126   if (bShowTime)
127   {
128     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
129     di << buf;
130   }
131   //
132   return 0;
133 }
134 //=======================================================================
135 //function : bbuild
136 //purpose  : 
137 //=======================================================================
138 Standard_Integer bbuild(Draw_Interpretor& di,
139                         Standard_Integer n, 
140                         const char** a) 
141
142   if (n<2) {
143     di << " use bbuild r [-t]\n";
144     return 0;
145   }
146   //
147   BOPDS_PDS pDS=BOPTest_Objects::PDS();
148   if (!pDS) {
149     di << " prepare PaveFiller first\n";
150     return 0;
151   }
152   //
153   char buf[128];
154   Standard_Boolean bRunParallel, bShowTime;
155   Standard_Integer i, iErr;
156
157   BOPCol_ListIteratorOfListOfShape aIt;
158   //
159   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
160   //
161   BOPAlgo_Builder& aBuilder=BOPTest_Objects::Builder();
162   aBuilder.Clear();
163   //
164   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
165   aIt.Initialize(aLSObj);
166   for (; aIt.More(); aIt.Next()) {
167     const TopoDS_Shape& aS=aIt.Value();
168     aBuilder.AddArgument(aS);
169   }
170   //
171   BOPCol_ListOfShape& aLSTool=BOPTest_Objects::Tools();
172   aIt.Initialize(aLSTool);
173   for (; aIt.More(); aIt.Next()) {
174     const TopoDS_Shape& aS=aIt.Value();
175     aBuilder.AddArgument(aS);
176   }
177   //
178   bShowTime=Standard_False;
179   bRunParallel=BOPTest_Objects::RunParallel();
180   for (i=2; i<n; ++i) {
181     if (!strcmp(a[i], "-t")) {
182       bShowTime=Standard_True;
183     }
184   }
185   aBuilder.SetRunParallel(bRunParallel);
186   //
187   //
188   OSD_Timer aTimer;
189   aTimer.Start();
190   //
191   aBuilder.PerformWithFiller(aPF); 
192   iErr=aBuilder.ErrorStatus();
193   if (iErr) {
194     Sprintf(buf, " error: %d\n",  iErr);
195     di << buf;
196     return 0;
197   }
198   //
199   aTimer.Stop();
200   //
201   if (bShowTime)
202   {
203     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
204     di << buf;
205   }
206   //
207   const TopoDS_Shape& aR=aBuilder.Shape();
208   if (aR.IsNull()) {
209     di << " null shape\n";
210     return 0;
211   }
212   //
213   DBRep::Set(a[1], aR);
214   return 0;
215 }
216 //=======================================================================
217 //function : bbop
218 //purpose  : 
219 //=======================================================================
220 Standard_Integer bbop(Draw_Interpretor& di, 
221                       Standard_Integer n, 
222                       const char** a) 
223
224   if (n<3) {
225     di << " use bbop r op [-t]\n";
226     return 0;
227   }
228   //
229   BOPDS_PDS pDS=BOPTest_Objects::PDS();
230   if (!pDS) {
231     di << " prepare PaveFiller first\n";
232     return 0;
233   }
234   //
235   char buf[32];
236   Standard_Boolean bRunParallel, bShowTime;
237   Standard_Integer iErr, iOp, i;
238   BOPAlgo_Operation aOp;
239   BOPCol_ListIteratorOfListOfShape aIt; 
240   //
241   iOp=Draw::Atoi(a[2]);
242   if (iOp<0 || iOp>4) {
243     di << " invalid operation type\n";
244     return 0;
245   }
246   aOp=(BOPAlgo_Operation)iOp;
247   //
248   bShowTime=Standard_False;
249   bRunParallel=BOPTest_Objects::RunParallel();
250   for (i=3; i<n; ++i) {
251     if (!strcmp(a[i], "-t")) {
252       bShowTime=Standard_True;
253     }
254   }
255   //
256   BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
257   //
258   BOPAlgo_Builder *pBuilder=NULL;
259   
260   if (aOp!=BOPAlgo_SECTION) { 
261     pBuilder=&BOPTest_Objects::BOP();
262   } 
263   else {
264     pBuilder=&BOPTest_Objects::Section();
265   }
266   //
267   pBuilder->Clear();
268   //
269   BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
270   aIt.Initialize(aLSObj);
271   for (; aIt.More(); aIt.Next()) {
272     const TopoDS_Shape& aS=aIt.Value();
273     pBuilder->AddArgument(aS);
274   }
275   //
276   if (aOp!=BOPAlgo_SECTION) {
277     BOPAlgo_BOP *pBOP=(BOPAlgo_BOP *)pBuilder;
278     //
279     BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
280     aIt.Initialize(aLSTools);
281     for (; aIt.More(); aIt.Next()) {
282       const TopoDS_Shape& aS=aIt.Value();
283       pBOP->AddTool(aS);
284     }
285     //
286     pBOP->SetOperation(aOp);
287   }
288   else {
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       pBuilder->AddArgument(aS);
294     }
295   }
296   //
297   pBuilder->SetRunParallel(bRunParallel);
298   //
299   OSD_Timer aTimer;
300   aTimer.Start();
301   //
302   pBuilder->PerformWithFiller(aPF);
303   iErr=pBuilder->ErrorStatus();
304   if (iErr) {
305     Sprintf(buf, " error: %d\n",  iErr);
306     di << buf;
307     return 0;
308   }
309   //
310   aTimer.Stop();
311   //
312   if (bShowTime) {
313     Sprintf(buf, "  Tps: %7.2lf\n", aTimer.ElapsedTime());
314     di << buf;
315   }
316   //
317   const TopoDS_Shape& aR=pBuilder->Shape();
318   if (aR.IsNull()) {
319     di << " null shape\n";
320     return 0;
321   }
322   //
323   DBRep::Set(a[1], aR);
324   return 0;
325 }
326