0026874: Implementation of the Partition operator in OCCT
[occt.git] / src / BOPTest / BOPTest_PartitionCommands.cxx
... / ...
CommitLineData
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 <DBRep.hxx>
26#include <Draw.hxx>
27#include <Draw_Color.hxx>
28#include <DrawTrSurf.hxx>
29#include <OSD_Timer.hxx>
30#include <TopoDS_Shape.hxx>
31
32#include <stdio.h>
33#include <string.h>
34//
35//
36static Standard_Integer bfillds (Draw_Interpretor&, Standard_Integer, const char**);
37static Standard_Integer bbuild (Draw_Interpretor&, Standard_Integer, const char**);
38static Standard_Integer bbop (Draw_Interpretor&, Standard_Integer, const char**);
39static Standard_Integer bsplit (Draw_Interpretor&, Standard_Integer, const char**);
40
41//=======================================================================
42//function : PartitionCommands
43//purpose :
44//=======================================================================
45void BOPTest::PartitionCommands(Draw_Interpretor& theCommands)
46{
47 static Standard_Boolean done = Standard_False;
48 if (done) return;
49 done = Standard_True;
50 // Chapter's name
51 const char* g = "BOPTest commands";
52 // Commands
53 theCommands.Add("bfillds", "use bfillds [-t]" , __FILE__, bfillds, g);
54 theCommands.Add("bbuild" , "use bbuild r [-t]" , __FILE__, bbuild, g);
55 theCommands.Add("bbop" , "use bbop r op [-t]", __FILE__, bbop, g);
56 theCommands.Add("bsplit" , "use bsplit r [-t]" , __FILE__, bsplit, g);
57}
58//=======================================================================
59//function : bfillds
60//purpose :
61//=======================================================================
62Standard_Integer bfillds(Draw_Interpretor& di,
63 Standard_Integer n,
64 const char** a)
65{
66 if (n > 2) {
67 di << " use bfillds [-t]\n";
68 return 0;
69 }
70 //
71 char buf[32];
72 Standard_Boolean bRunParallel, bNonDestructive, bShowTime;
73 Standard_Integer i, aNbS, iErr;
74 Standard_Real aTol;
75 BOPCol_ListIteratorOfListOfShape aIt;
76 BOPCol_ListOfShape aLC;
77 BOPCol_ListOfShape& aLS=BOPTest_Objects::Shapes();
78 aNbS=aLS.Extent();
79 if (!aNbS) {
80 di << " no objects to process\n";
81 return 0;
82 }
83 //
84 bShowTime = Standard_False;
85 //
86 bRunParallel=BOPTest_Objects::RunParallel();
87 bNonDestructive = BOPTest_Objects::NonDestructive();
88 aTol = BOPTest_Objects::FuzzyValue();
89 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
90 //
91 for (i=1; i<n; ++i) {
92 if (!strcmp(a[i], "-t")) {
93 bShowTime=Standard_True;
94 }
95 }
96 //
97 BOPCol_ListOfShape& aLT=BOPTest_Objects::Tools();
98 //
99 aIt.Initialize(aLS);
100 for (; aIt.More(); aIt.Next()) {
101 const TopoDS_Shape& aS=aIt.Value();
102 aLC.Append(aS);
103 }
104 //
105 aIt.Initialize(aLT);
106 for (; aIt.More(); aIt.Next()) {
107 const TopoDS_Shape& aS=aIt.Value();
108 aLC.Append(aS);
109 }
110 //
111 BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
112 //
113 aPF.SetArguments(aLC);
114 aPF.SetRunParallel(bRunParallel);
115 aPF.SetNonDestructive(bNonDestructive);
116 aPF.SetFuzzyValue(aTol);
117 aPF.SetGlue(aGlue);
118 //
119 OSD_Timer aTimer;
120 aTimer.Start();
121 //
122 aPF.Perform();
123 iErr=aPF.ErrorStatus();
124 if (iErr) {
125 Sprintf(buf, " error: %d\n", iErr);
126 di << buf;
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//=======================================================================
144Standard_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, iErr;
162
163 BOPCol_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 BOPCol_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 BOPCol_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 //
194 //
195 OSD_Timer aTimer;
196 aTimer.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 aTimer.Stop();
207 //
208 if (bShowTime)
209 {
210 Sprintf(buf, " Tps: %7.2lf\n", aTimer.ElapsedTime());
211 di << buf;
212 }
213 //
214 const TopoDS_Shape& aR=aBuilder.Shape();
215 if (aR.IsNull()) {
216 di << " null shape\n";
217 return 0;
218 }
219 //
220 DBRep::Set(a[1], aR);
221 return 0;
222}
223//=======================================================================
224//function : bbop
225//purpose :
226//=======================================================================
227Standard_Integer bbop(Draw_Interpretor& di,
228 Standard_Integer n,
229 const char** a)
230{
231 if (n<3) {
232 di << " use bbop r op [-t]\n";
233 return 0;
234 }
235 //
236 BOPDS_PDS pDS=BOPTest_Objects::PDS();
237 if (!pDS) {
238 di << " prepare PaveFiller first\n";
239 return 0;
240 }
241 //
242 char buf[32];
243 Standard_Boolean bRunParallel, bShowTime;
244 Standard_Integer iErr, iOp, i;
245 BOPAlgo_Operation aOp;
246 BOPCol_ListIteratorOfListOfShape aIt;
247 //
248 iOp=Draw::Atoi(a[2]);
249 if (iOp<0 || iOp>4) {
250 di << " invalid operation type\n";
251 return 0;
252 }
253 aOp=(BOPAlgo_Operation)iOp;
254 //
255 bShowTime=Standard_False;
256 bRunParallel=BOPTest_Objects::RunParallel();
257 for (i=3; i<n; ++i) {
258 if (!strcmp(a[i], "-t")) {
259 bShowTime=Standard_True;
260 }
261 }
262 //
263 BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
264 //
265 BOPAlgo_Builder *pBuilder=NULL;
266
267 if (aOp!=BOPAlgo_SECTION) {
268 pBuilder=&BOPTest_Objects::BOP();
269 }
270 else {
271 pBuilder=&BOPTest_Objects::Section();
272 }
273 //
274 pBuilder->Clear();
275 //
276 BOPCol_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
277 aIt.Initialize(aLSObj);
278 for (; aIt.More(); aIt.Next()) {
279 const TopoDS_Shape& aS=aIt.Value();
280 pBuilder->AddArgument(aS);
281 }
282 //
283 if (aOp!=BOPAlgo_SECTION) {
284 BOPAlgo_BOP *pBOP=(BOPAlgo_BOP *)pBuilder;
285 //
286 BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
287 aIt.Initialize(aLSTools);
288 for (; aIt.More(); aIt.Next()) {
289 const TopoDS_Shape& aS=aIt.Value();
290 pBOP->AddTool(aS);
291 }
292 //
293 pBOP->SetOperation(aOp);
294 }
295 else {
296 BOPCol_ListOfShape& aLSTools=BOPTest_Objects::Tools();
297 aIt.Initialize(aLSTools);
298 for (; aIt.More(); aIt.Next()) {
299 const TopoDS_Shape& aS=aIt.Value();
300 pBuilder->AddArgument(aS);
301 }
302 }
303 //
304 pBuilder->SetRunParallel(bRunParallel);
305 //
306 OSD_Timer aTimer;
307 aTimer.Start();
308 //
309 pBuilder->PerformWithFiller(aPF);
310 iErr=pBuilder->ErrorStatus();
311 if (iErr) {
312 Sprintf(buf, " error: %d\n", iErr);
313 di << buf;
314 return 0;
315 }
316 //
317 aTimer.Stop();
318 //
319 if (bShowTime) {
320 Sprintf(buf, " Tps: %7.2lf\n", aTimer.ElapsedTime());
321 di << buf;
322 }
323 //
324 const TopoDS_Shape& aR=pBuilder->Shape();
325 if (aR.IsNull()) {
326 di << " null shape\n";
327 return 0;
328 }
329 //
330 BOPTest_Objects::SetBuilder(pBuilder);
331 //
332 DBRep::Set(a[1], aR);
333 return 0;
334}
335
336//=======================================================================
337//function : bsplit
338//purpose :
339//=======================================================================
340Standard_Integer bsplit(Draw_Interpretor& di,
341 Standard_Integer n,
342 const char** a)
343{
344 if (n < 2) {
345 di << " use bsplit r [-t (show time)]\n";
346 return 1;
347 }
348 //
349 BOPDS_PDS pDS = BOPTest_Objects::PDS();
350 if (!pDS) {
351 di << " prepare PaveFiller first\n";
352 return 0;
353 }
354 //
355 BOPAlgo_PaveFiller& aPF = BOPTest_Objects::PaveFiller();
356 //
357 BOPAlgo_Splitter* pSplitter = &BOPTest_Objects::Splitter();
358 pSplitter->Clear();
359 //
360 // set objects
361 const BOPCol_ListOfShape& aLSObjects = BOPTest_Objects::Shapes();
362 pSplitter->SetArguments(aLSObjects);
363 //
364 // set tools
365 BOPCol_ListOfShape& aLSTools = BOPTest_Objects::Tools();
366 pSplitter->SetTools(aLSTools);
367 //
368 // set options
369 pSplitter->SetRunParallel(BOPTest_Objects::RunParallel());
370 pSplitter->SetNonDestructive(BOPTest_Objects::NonDestructive());
371 pSplitter->SetFuzzyValue(BOPTest_Objects::FuzzyValue());
372 //
373 // measure the time of the operation
374 OSD_Timer aTimer;
375 aTimer.Start();
376 //
377 // perform the operation
378 pSplitter->PerformWithFiller(aPF);
379 //
380 aTimer.Stop();
381 //
382 Standard_Integer iErr = pSplitter->ErrorStatus();
383 if (iErr) {
384 di << " error: " << iErr << "\n";
385 return 0;
386 }
387 //
388 // show time if necessary
389 if (n == 3 && !strcmp(a[2], "-t")) {
390 char buf[20];
391 Sprintf(buf, " Tps: %7.2lf\n", aTimer.ElapsedTime());
392 di << buf;
393 }
394 //
395 // DRAW history support
396 BOPTest_Objects::SetBuilder(pSplitter);
397 //
398 const TopoDS_Shape& aR = pSplitter->Shape();
399 if (aR.IsNull()) {
400 di << " null shape\n";
401 return 0;
402 }
403 //
404 DBRep::Set(a[1], aR);
405 return 0;
406}