0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[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 <BOPDS_DS.hxx>
23#include <BOPTest.hxx>
24#include <BOPTest_DrawableShape.hxx>
25#include <BOPTest_Objects.hxx>
26#include <BRepTest_Objects.hxx>
27#include <DBRep.hxx>
28#include <OSD_Timer.hxx>
29#include <TopoDS_Shape.hxx>
30#include <Draw_ProgressIndicator.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**);
40static Standard_Integer buildbop (Draw_Interpretor&, Standard_Integer, const char**);
41
42//=======================================================================
43//function : PartitionCommands
44//purpose :
45//=======================================================================
46void 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", "Performs intersection of the arguments added for the operation by baddobjects and baddtools commands.\n"
55 "\t\tUsage: bfillds [-t]\n"
56 "\t\tWhere: -t is the optional parameter for enabling timer and showing elapsed time of the operation",
57 __FILE__, bfillds, g);
58
59 theCommands.Add("bbuild" , "Builds the result of General Fuse operation. Intersection (bfillds) has to be already performed by this moment.\n"
60 "\t\tUsage: bbuild result [-t]\n"
61 "\t\tWhere:\n"
62 "\t\tresult - name of the result shape\n"
63 "\t\t-t is the optional parameter for enabling timer and showing elapsed time of the operation",
64 __FILE__, bbuild, g);
65
66 theCommands.Add("bbop" , "Builds the result of Boolean operation. Intersection (bfillds) has to be already performed by this moment.\n"
67 "\t\tUsage: bbop result op [-t]\n"
68 "\t\tWhere:\n"
69 "\t\tresult - name of the result shape\n"
70 "\t\top - type of Boolean operation. Possible values:\n"
71 "\t\t - 0/common - for Common operation\n"
72 "\t\t - 1/fuse - for Fuse operation\n"
73 "\t\t - 2/cut - for Cut operation\n"
74 "\t\t - 3/tuc/cut21 - for Cut21 operation\n"
75 "\t\t - 4/section - for Section operation\n"
76 "\t\t-t - optional parameter for enabling timer and showing elapsed time of the operation",
77 __FILE__, bbop, g);
78
79 theCommands.Add("bsplit" , "Builds the result of Split operation. Intersection (bfillds) has to be already performed by this moment.\n"
80 "\t\tUsage: bsplit result [-t]\n"
81 "\t\tWhere:\n"
82 "\t\tresult - name of the result shape\n"
83 "\t\t-t is the optional parameter for enabling timer and showing elapsed time of the operation",
84 __FILE__, bsplit, g);
85
86 theCommands.Add("buildbop", "Builds the result of BOP basing on the GF, thus bbuild command has to be already performed\n"
87 "\t\tThe command uses classification approach for building the result of BOP\n"
88 "\t\t(thus it operates on solids only and can be used on open solids):\n"
89 "\t\t - FUSE is built from the faces OUT of all arguments\n"
90 "\t\t - COMMON is built from the faces IN any of the object/tools\n"
91 "\t\t - CUT is built from the objects faces OUT of the tools and tools faces IN the objects.\n"
92 "\t\tPlease note that history for solids will not be available.\n\n"
93 "\t\tUsage: buildbop result -o s1 [s2 ...] -t s3 [s4 ...] -op operation (common/fuse/cut/tuc)\n"
94 "\t\tWhere:\n"
95 "\t\tresult - result shape of the operation\n"
96 "\t\ts1 s2 s3 s4 - arguments (solids) of the GF operation\n"
97 "\t\toperation - type of boolean operation",
98 __FILE__, buildbop, g);
99}
100
101//=======================================================================
102//function : bfillds
103//purpose :
104//=======================================================================
105Standard_Integer bfillds(Draw_Interpretor& di,
106 Standard_Integer n,
107 const char** a)
108{
109 if (n > 2) {
110 di.PrintHelp(a[0]);
111 return 1;
112 }
113 //
114 char buf[32];
115 Standard_Boolean bRunParallel, bNonDestructive, bShowTime;
116 Standard_Integer aNbS;
117 Standard_Real aTol;
118 TopTools_ListIteratorOfListOfShape aIt;
119 TopTools_ListOfShape aLC;
120 TopTools_ListOfShape& aLS=BOPTest_Objects::Shapes();
121 aNbS=aLS.Extent();
122 if (!aNbS) {
123 di << "No objects to process\n";
124 return 0;
125 }
126 //
127 bShowTime = Standard_False;
128 //
129 bRunParallel=BOPTest_Objects::RunParallel();
130 bNonDestructive = BOPTest_Objects::NonDestructive();
131 aTol = BOPTest_Objects::FuzzyValue();
132 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
133 //
134 if (n == 2)
135 {
136 if (!strcmp(a[1], "-t"))
137 {
138 bShowTime=Standard_True;
139 }
140 else
141 {
142 di << "Warning: invalid key\n";
143 }
144 }
145 //
146 TopTools_ListOfShape& aLT=BOPTest_Objects::Tools();
147 //
148 aIt.Initialize(aLS);
149 for (; aIt.More(); aIt.Next()) {
150 const TopoDS_Shape& aS=aIt.Value();
151 aLC.Append(aS);
152 }
153 //
154 aIt.Initialize(aLT);
155 for (; aIt.More(); aIt.Next()) {
156 const TopoDS_Shape& aS=aIt.Value();
157 aLC.Append(aS);
158 }
159 //
160 BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
161 //
162 aPF.SetArguments(aLC);
163 aPF.SetRunParallel(bRunParallel);
164 aPF.SetNonDestructive(bNonDestructive);
165 aPF.SetFuzzyValue(aTol);
166 aPF.SetGlue(aGlue);
167 aPF.SetUseOBB(BOPTest_Objects::UseOBB());
168 //
169 OSD_Timer aTimer;
170 aTimer.Start();
171 //
172 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
173 aPF.Perform(aProgress->Start());
174 BOPTest::ReportAlerts(aPF.GetReport());
175 if (aPF.HasErrors()) {
176 return 0;
177 }
178 //
179 aTimer.Stop();
180 //
181 if (bShowTime)
182 {
183 Sprintf(buf, " Tps: %7.2lf\n", aTimer.ElapsedTime());
184 di << buf;
185 }
186 //
187 return 0;
188}
189//=======================================================================
190//function : bbuild
191//purpose :
192//=======================================================================
193Standard_Integer bbuild(Draw_Interpretor& di,
194 Standard_Integer n,
195 const char** a)
196{
197 if (n < 2 || n > 3) {
198 di.PrintHelp(a[0]);
199 return 1;
200 }
201 //
202 BOPDS_PDS pDS=BOPTest_Objects::PDS();
203 if (!pDS) {
204 di << "Prepare PaveFiller first\n";
205 return 0;
206 }
207 //
208 char buf[128];
209 Standard_Boolean bRunParallel, bShowTime;
210
211 TopTools_ListIteratorOfListOfShape aIt;
212 //
213 BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
214 //
215 BOPTest_Objects::SetBuilderDefault();
216 BOPAlgo_Builder& aBuilder=BOPTest_Objects::Builder();
217 aBuilder.Clear();
218 //
219 TopTools_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
220 aIt.Initialize(aLSObj);
221 for (; aIt.More(); aIt.Next()) {
222 const TopoDS_Shape& aS=aIt.Value();
223 aBuilder.AddArgument(aS);
224 }
225 //
226 TopTools_ListOfShape& aLSTool=BOPTest_Objects::Tools();
227 aIt.Initialize(aLSTool);
228 for (; aIt.More(); aIt.Next()) {
229 const TopoDS_Shape& aS=aIt.Value();
230 aBuilder.AddArgument(aS);
231 }
232 //
233 bShowTime=Standard_False;
234 bRunParallel=BOPTest_Objects::RunParallel();
235 if (n == 3)
236 {
237 if (!strcmp(a[2], "-t"))
238 {
239 bShowTime=Standard_True;
240 }
241 else
242 {
243 di << "Warning: invalid key\n";
244 }
245 }
246 aBuilder.SetRunParallel(bRunParallel);
247 aBuilder.SetCheckInverted(BOPTest_Objects::CheckInverted());
248 aBuilder.SetToFillHistory(BRepTest_Objects::IsHistoryNeeded());
249 //
250 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
251 //
252 OSD_Timer aTimer;
253 aTimer.Start();
254 //
255 aBuilder.PerformWithFiller(aPF, aProgress->Start());
256 BOPTest::ReportAlerts(aBuilder.GetReport());
257
258 // Set history of GF operation into the session
259 if (BRepTest_Objects::IsHistoryNeeded())
260 BRepTest_Objects::SetHistory(aPF.Arguments(), aBuilder);
261
262 if (aBuilder.HasErrors()) {
263 return 0;
264 }
265 //
266 aTimer.Stop();
267 //
268 if (bShowTime)
269 {
270 Sprintf(buf, " Tps: %7.2lf\n", aTimer.ElapsedTime());
271 di << buf;
272 }
273 //
274 const TopoDS_Shape& aR=aBuilder.Shape();
275 if (aR.IsNull()) {
276 di << "Result is a null shape\n";
277 return 0;
278 }
279 //
280 DBRep::Set(a[1], aR);
281 return 0;
282}
283//=======================================================================
284//function : bbop
285//purpose :
286//=======================================================================
287Standard_Integer bbop(Draw_Interpretor& di,
288 Standard_Integer n,
289 const char** a)
290{
291 if (n < 3 || n > 4) {
292 di.PrintHelp(a[0]);
293 return 1;
294 }
295 //
296 BOPDS_PDS pDS=BOPTest_Objects::PDS();
297 if (!pDS) {
298 di << "Prepare PaveFiller first\n";
299 return 0;
300 }
301 //
302 BOPAlgo_Operation anOp = BOPTest::GetOperationType(a[2]);
303 if (anOp == BOPAlgo_UNKNOWN)
304 {
305 di << "Invalid operation type\n";
306 return 0;
307 }
308
309 Standard_Boolean bShowTime=Standard_False;
310 Standard_Boolean bRunParallel=BOPTest_Objects::RunParallel();
311 if (n == 4)
312 {
313 if (!strcmp(a[3], "-t"))
314 {
315 bShowTime=Standard_True;
316 }
317 else
318 {
319 di << "Warning: invalid key\n";
320 }
321 }
322 //
323 BOPAlgo_PaveFiller& aPF=BOPTest_Objects::PaveFiller();
324 //
325 BOPAlgo_Builder *pBuilder=NULL;
326
327 if (anOp!=BOPAlgo_SECTION) {
328 pBuilder=&BOPTest_Objects::BOP();
329 }
330 else {
331 pBuilder=&BOPTest_Objects::Section();
332 }
333 //
334 pBuilder->Clear();
335 //
336 TopTools_ListOfShape& aLSObj=BOPTest_Objects::Shapes();
337 TopTools_ListIteratorOfListOfShape aIt(aLSObj);
338 for (; aIt.More(); aIt.Next()) {
339 const TopoDS_Shape& aS=aIt.Value();
340 pBuilder->AddArgument(aS);
341 }
342 //
343 if (anOp!=BOPAlgo_SECTION) {
344 BOPAlgo_BOP *pBOP=(BOPAlgo_BOP *)pBuilder;
345 //
346 TopTools_ListOfShape& aLSTools=BOPTest_Objects::Tools();
347 aIt.Initialize(aLSTools);
348 for (; aIt.More(); aIt.Next()) {
349 const TopoDS_Shape& aS=aIt.Value();
350 pBOP->AddTool(aS);
351 }
352 //
353 pBOP->SetOperation(anOp);
354 }
355 else {
356 TopTools_ListOfShape& aLSTools=BOPTest_Objects::Tools();
357 aIt.Initialize(aLSTools);
358 for (; aIt.More(); aIt.Next()) {
359 const TopoDS_Shape& aS=aIt.Value();
360 pBuilder->AddArgument(aS);
361 }
362 }
363 //
364 pBuilder->SetRunParallel(bRunParallel);
365 pBuilder->SetCheckInverted(BOPTest_Objects::CheckInverted());
366 pBuilder->SetToFillHistory(BRepTest_Objects::IsHistoryNeeded());
367 //
368 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
369 //
370 OSD_Timer aTimer;
371 aTimer.Start();
372 //
373 pBuilder->PerformWithFiller(aPF, aProgress->Start());
374 BOPTest::ReportAlerts(pBuilder->GetReport());
375
376 // Set history of Boolean operation into the session
377 if (BRepTest_Objects::IsHistoryNeeded())
378 BRepTest_Objects::SetHistory(aPF.Arguments(), *pBuilder);
379
380 if (pBuilder->HasErrors()) {
381 return 0;
382 }
383 //
384 aTimer.Stop();
385 //
386 if (bShowTime) {
387 char buf[32];
388 Sprintf(buf, " Tps: %7.2lf\n", aTimer.ElapsedTime());
389 di << buf;
390 }
391 //
392 const TopoDS_Shape& aR=pBuilder->Shape();
393 if (aR.IsNull()) {
394 di << "Result is a null shape\n";
395 return 0;
396 }
397 //
398 BOPTest_Objects::SetBuilder(pBuilder);
399 //
400 DBRep::Set(a[1], aR);
401 return 0;
402}
403
404//=======================================================================
405//function : bsplit
406//purpose :
407//=======================================================================
408Standard_Integer bsplit(Draw_Interpretor& di,
409 Standard_Integer n,
410 const char** a)
411{
412 if (n < 2 || n > 3) {
413 di.PrintHelp(a[0]);
414 return 1;
415 }
416 //
417 BOPDS_PDS pDS = BOPTest_Objects::PDS();
418 if (!pDS) {
419 di << "Prepare PaveFiller first\n";
420 return 0;
421 }
422 //
423 BOPAlgo_PaveFiller& aPF = BOPTest_Objects::PaveFiller();
424 //
425 BOPAlgo_Splitter* pSplitter = &BOPTest_Objects::Splitter();
426 pSplitter->Clear();
427 //
428 // set objects
429 const TopTools_ListOfShape& aLSObjects = BOPTest_Objects::Shapes();
430 pSplitter->SetArguments(aLSObjects);
431 //
432 // set tools
433 TopTools_ListOfShape& aLSTools = BOPTest_Objects::Tools();
434 pSplitter->SetTools(aLSTools);
435 //
436 // set options
437 pSplitter->SetRunParallel(BOPTest_Objects::RunParallel());
438 pSplitter->SetNonDestructive(BOPTest_Objects::NonDestructive());
439 pSplitter->SetFuzzyValue(BOPTest_Objects::FuzzyValue());
440 pSplitter->SetCheckInverted(BOPTest_Objects::CheckInverted());
441 pSplitter->SetToFillHistory(BRepTest_Objects::IsHistoryNeeded());
442 //
443 // measure the time of the operation
444 OSD_Timer aTimer;
445 aTimer.Start();
446 //
447 // perform the operation
448 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
449 pSplitter->PerformWithFiller(aPF, aProgress->Start());
450 //
451 aTimer.Stop();
452 BOPTest::ReportAlerts(pSplitter->GetReport());
453
454 // Set history of Split operation into the session
455 if (BRepTest_Objects::IsHistoryNeeded())
456 BRepTest_Objects::SetHistory(aPF.Arguments(), *pSplitter);
457
458 if (pSplitter->HasErrors()) {
459 return 0;
460 }
461 //
462 // show time if necessary
463 if (n == 3)
464 {
465 if (!strcmp(a[2], "-t"))
466 {
467 char buf[20];
468 Sprintf(buf, " Tps: %7.2lf\n", aTimer.ElapsedTime());
469 di << buf;
470 }
471 else
472 {
473 di << "Warning: invalid key\n";
474 }
475 }
476 //
477 // Debug commands support
478 BOPTest_Objects::SetBuilder(pSplitter);
479 //
480 const TopoDS_Shape& aR = pSplitter->Shape();
481 if (aR.IsNull()) {
482 di << " null shape\n";
483 return 0;
484 }
485 //
486 DBRep::Set(a[1], aR);
487 return 0;
488}
489
490//=======================================================================
491//function : buildbop
492//purpose :
493//=======================================================================
494Standard_Integer buildbop(Draw_Interpretor& di,
495 Standard_Integer n,
496 const char** a)
497{
498 if (n < 3)
499 {
500 di.PrintHelp(a[0]);
501 return 1;
502 }
503
504 BOPDS_PDS pDS = BOPTest_Objects::PDS();
505 if (!pDS)
506 {
507 di << "Error: perform intersection of arguments first";
508 return 1;
509 }
510
511 BOPAlgo_Builder *pBuilder = &BOPTest_Objects::Builder();
512 if (pBuilder->HasErrors())
513 {
514 di << "Error: there were problems during GF";
515 return 0;
516 }
517
518 if (pBuilder->Arguments().IsEmpty() ||
519 pBuilder->Shape().IsNull())
520 {
521 di << "Error: it seems the GF has not been yet performed";
522 return 1;
523 }
524
525 // Get arguments and operation
526 TopTools_ListOfShape aLObjects, aLTools;
527 BOPAlgo_Operation anOp = BOPAlgo_UNKNOWN;
528
529 for (Standard_Integer i = 2; i < n; ++i)
530 {
531 if (!strcmp(a[i], "-o") || !strcmp(a[i], "-t"))
532 {
533 if (i == (n - 1))
534 {
535 di << "Error: shapes are expected after the key " << a[i];
536 return 1;
537 }
538
539 TopTools_ListOfShape& aList = !strcmp(a[i], "-o") ? aLObjects : aLTools;
540 Standard_Integer j = i + 1;
541 for (; j < n; ++j)
542 {
543 if (a[j][0] == '-')
544 {
545 // reached the following key
546 i = j - 1;
547 break;
548 }
549 else
550 {
551 // Get the shape
552 TopoDS_Shape aS = DBRep::Get(a[j]);
553 if (aS.IsNull())
554 {
555 di << "Error: " << a[j] << " is a null shape";
556 return 1;
557 }
558 if (aS.ShapeType() != TopAbs_SOLID)
559 {
560 di << "Error: " << a[j] << " is not a solid";
561 return 1;
562 }
563 if (pDS->Index(aS) < 0)
564 {
565 di << "Error: " << a[j] << " is not an argument of GF";
566 return 1;
567 }
568 aList.Append(aS);
569 }
570 }
571 // End of arguments is reached
572 if (j == n) break;
573 }
574 else if (!strcmp(a[i], "-op"))
575 {
576 if (i == (n - 1))
577 {
578 di << "Error: operation type is expected after the key " << a[i];
579 return 1;
580 }
581
582 ++i;
583 if (!strcasecmp(a[i], "common"))
584 anOp = BOPAlgo_COMMON;
585 else if (!strcasecmp(a[i], "fuse"))
586 anOp = BOPAlgo_FUSE;
587 else if (!strcasecmp(a[i], "cut"))
588 anOp = BOPAlgo_CUT;
589 else if (!strcasecmp(a[i], "tuc"))
590 anOp = BOPAlgo_CUT21;
591 else
592 {
593 di << "Error: unknown operation type";
594 return 1;
595 }
596 }
597 else
598 {
599 di << "Error: " << a[i] << " invalid key";
600 return 1;
601 }
602 }
603
604 if (anOp == BOPAlgo_UNKNOWN)
605 {
606 di << "Error: operation has not been specified";
607 return 1;
608 }
609
610 Standard_Boolean hasObjects = !aLObjects.IsEmpty();
611 Standard_Boolean hasTools = !aLTools.IsEmpty();
612 if (!hasObjects && !hasTools)
613 {
614 di << "Error: no shapes are given";
615 return 1;
616 }
617
618 // Create new report for the operation
619 Handle(Message_Report) aReport = new Message_Report;
620 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1);
621 // Build specific operation
622 pBuilder->BuildBOP(aLObjects, aLTools, anOp, aProgress->Start(), aReport);
623
624 // Report alerts of the operation
625 BOPTest::ReportAlerts(aReport);
626
627 if (!aReport->GetAlerts(Message_Fail).IsEmpty())
628 {
629 return 0;
630 }
631
632 // Set history of Split operation into the session
633 if (BRepTest_Objects::IsHistoryNeeded())
634 BRepTest_Objects::SetHistory(pDS->Arguments(), *pBuilder);
635
636 // Result shape
637 const TopoDS_Shape& aR = pBuilder->Shape();
638 // Draw result shape
639 DBRep::Set(a[1], aR);
640
641 return 0;
642}