0026874: Implementation of the Partition operator in OCCT
[occt.git] / src / BOPTest / BOPTest_APICommands.cxx
CommitLineData
49b0c452 1// Created by: Peter KURNEV
2// Copyright (c) 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
49b0c452 15
42cf5bc1 16#include <BOPAlgo_PaveFiller.hxx>
49b0c452 17#include <BOPCol_ListOfShape.hxx>
42cf5bc1 18#include <BOPTest.hxx>
49b0c452 19#include <BOPTest_Objects.hxx>
49b0c452 20#include <BRepAlgoAPI_BooleanOperation.hxx>
21#include <BRepAlgoAPI_BuilderAlgo.hxx>
22#include <BRepAlgoAPI_Common.hxx>
49b0c452 23#include <BRepAlgoAPI_Cut.hxx>
42cf5bc1 24#include <BRepAlgoAPI_Fuse.hxx>
49b0c452 25#include <BRepAlgoAPI_Section.hxx>
c58055ad 26#include <BRepAlgoAPI_Splitter.hxx>
42cf5bc1 27#include <DBRep.hxx>
28#include <Draw.hxx>
29#include <TopoDS_Shape.hxx>
30#include <TopTools_ListOfShape.hxx>
49b0c452 31
42cf5bc1 32#include <stdio.h>
33#include <string.h>
49b0c452 34static
35 void ConvertList(const BOPCol_ListOfShape& aLSB,
36 TopTools_ListOfShape& aLS);
37
c58055ad 38static Standard_Integer bapibuild(Draw_Interpretor&, Standard_Integer, const char**);
39static Standard_Integer bapibop (Draw_Interpretor&, Standard_Integer, const char**);
40static Standard_Integer bapisplit(Draw_Interpretor&, Standard_Integer, const char**);
49b0c452 41
42//=======================================================================
43//function : APICommands
44//purpose :
45//=======================================================================
46void BOPTest::APICommands(Draw_Interpretor& theCommands)
47{
48 static Standard_Boolean done = Standard_False;
49 if (done) return;
50 done = Standard_True;
51 // Chapter's name
43cb0011 52 const char* g = "BOPTest commands";
49b0c452 53 // Commands
54 theCommands.Add("bapibuild", "use bapibuild r" , __FILE__, bapibuild, g);
55 theCommands.Add("bapibop", "use bapibop r type" , __FILE__, bapibop, g);
c58055ad 56 theCommands.Add("bapisplit", "use bapisplit r" , __FILE__, bapisplit, g);
49b0c452 57}
58//=======================================================================
59//function : bapibop
60//purpose :
61//=======================================================================
62Standard_Integer bapibop(Draw_Interpretor& di,
63 Standard_Integer n,
64 const char** a)
65{
66 if (n<3) {
67 di << " use bapibop r type\n";
68 return 0;
69 }
70 //
71 char buf[128];
3510db62 72 Standard_Boolean bRunParallel, bNonDestructive;
49b0c452 73 Standard_Integer iErr, iOp;
74 Standard_Real aFuzzyValue;
75 BRepAlgoAPI_Common aCommon;
76 BRepAlgoAPI_Fuse aFuse;
77 BRepAlgoAPI_Cut aCut;
78 BRepAlgoAPI_Section aSection;
79 BRepAlgoAPI_BooleanOperation *pBuilder;
80 BOPAlgo_Operation aOp;
81 //
82 pBuilder=NULL;
83 iOp=atoi(a[2]);
84 if (iOp<0 || iOp>4) {
85 printf(" invalid operation type\n");
86 return 0;
87 }
88 aOp=(BOPAlgo_Operation)iOp;
89 //
90 switch (aOp) {
91 case BOPAlgo_COMMON:
92 pBuilder=&aCommon;
93 break;
94 //
95 case BOPAlgo_FUSE:
96 pBuilder=&aFuse;
97 break;
98 //
99 case BOPAlgo_CUT:
100 case BOPAlgo_CUT21:
101 pBuilder=&aCut;
102 break;
103 //
104 case BOPAlgo_SECTION:
105 pBuilder=&aSection;
106 break;
107 //
108 default:
109 break;
110 }
111 //
112 BOPCol_ListOfShape& aLSB=BOPTest_Objects::Shapes();
113 BOPCol_ListOfShape& aLTB=BOPTest_Objects::Tools();
114 //
115 TopTools_ListOfShape aLS, aLT;
116 ConvertList(aLSB, aLS);
117 ConvertList(aLTB, aLT);
118 //
119 bRunParallel=BOPTest_Objects::RunParallel();
120 aFuzzyValue=BOPTest_Objects::FuzzyValue();
3510db62 121 bNonDestructive = BOPTest_Objects::NonDestructive();
483ce1bd 122 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
49b0c452 123 //
124 if (aOp!=BOPAlgo_CUT21) {
125 pBuilder->SetArguments(aLS);
126 pBuilder->SetTools(aLT);
127 }
128 else {
129 pBuilder->SetArguments(aLT);
130 pBuilder->SetTools(aLS);
131 }
132 //
133 pBuilder->SetRunParallel(bRunParallel);
134 pBuilder->SetFuzzyValue(aFuzzyValue);
3510db62 135 pBuilder->SetNonDestructive(bNonDestructive);
483ce1bd 136 pBuilder->SetGlue(aGlue);
49b0c452 137 //
138 pBuilder->Build();
139 iErr=pBuilder->ErrorStatus();
140 if (iErr) {
141 Sprintf(buf, " error: %d\n", iErr);
142 di << buf;
143 return 0;
144 }
145 //
146 const TopoDS_Shape& aR=pBuilder->Shape();
147 if (aR.IsNull()) {
148 di << " null shape\n";
149 return 0;
150 }
151 //
152 DBRep::Set(a[1], aR);
153 return 0;
154}
155//=======================================================================
156//function : bapibuild
157//purpose :
158//=======================================================================
159Standard_Integer bapibuild(Draw_Interpretor& di,
160 Standard_Integer n,
161 const char** a)
162{
163 if (n<2) {
164 di << " use bapibuild r\n";
165 return 0;
166 }
167 //
168 char buf[128];
3510db62 169 Standard_Boolean bRunParallel, bNonDestructive;
49b0c452 170 Standard_Integer iErr;
171 Standard_Real aFuzzyValue;
172 BRepAlgoAPI_BuilderAlgo aBuilder;
173 //
174 BOPCol_ListOfShape& aLSB=BOPTest_Objects::Shapes();
43cb0011 175 BOPCol_ListOfShape& aLTB=BOPTest_Objects::Tools();
49b0c452 176 //
43cb0011 177 TopTools_ListOfShape aLS;
49b0c452 178 ConvertList(aLSB, aLS);
43cb0011 179 ConvertList(aLTB, aLS);
49b0c452 180 //
181 bRunParallel=BOPTest_Objects::RunParallel();
182 aFuzzyValue=BOPTest_Objects::FuzzyValue();
3510db62 183 bNonDestructive = BOPTest_Objects::NonDestructive();
483ce1bd 184 BOPAlgo_GlueEnum aGlue = BOPTest_Objects::Glue();
49b0c452 185 //
186 aBuilder.SetArguments(aLS);
187 aBuilder.SetRunParallel(bRunParallel);
188 aBuilder.SetFuzzyValue(aFuzzyValue);
3510db62 189 aBuilder.SetNonDestructive(bNonDestructive);
483ce1bd 190 aBuilder.SetGlue(aGlue);
49b0c452 191 //
192 aBuilder.Build();
193 iErr=aBuilder.ErrorStatus();
194 if (iErr) {
195 Sprintf(buf, " error: %d\n", iErr);
196 di << buf;
197 return 0;
198 }
199 //
200 const TopoDS_Shape& aR=aBuilder.Shape();
201 if (aR.IsNull()) {
202 di << " null shape\n";
203 return 0;
204 }
205 //
206 DBRep::Set(a[1], aR);
207 return 0;
208}
209//=======================================================================
210//function : ConvertLists
211//purpose :
212//=======================================================================
213void ConvertList(const BOPCol_ListOfShape& aLSB,
214 TopTools_ListOfShape& aLS)
215{
216 BOPCol_ListIteratorOfListOfShape aItB;
217 //
49b0c452 218 aItB.Initialize(aLSB);
219 for (; aItB.More(); aItB.Next()) {
220 const TopoDS_Shape& aS=aItB.Value();
221 aLS.Append(aS);
222 }
223}
224
c58055ad 225
226//=======================================================================
227//function : bapisplit
228//purpose :
229//=======================================================================
230Standard_Integer bapisplit(Draw_Interpretor& di,
231 Standard_Integer n,
232 const char** a)
233{
234 if (n < 2) {
235 di << " use bapisplit r\n";
236 return 1;
237 }
238 //
239 BRepAlgoAPI_Splitter aSplitter;
240 // setting arguments
241 aSplitter.SetArguments(BOPTest_Objects::Shapes());
242 aSplitter.SetTools(BOPTest_Objects::Tools());
243 // setting options
244 aSplitter.SetRunParallel(BOPTest_Objects::RunParallel());
245 aSplitter.SetFuzzyValue(BOPTest_Objects::FuzzyValue());
246 aSplitter.SetNonDestructive(BOPTest_Objects::NonDestructive());
247 aSplitter.SetGlue(BOPTest_Objects::Glue());
248 //
249 // performing operation
250 aSplitter.Build();
251 // checking error status
252 Standard_Integer iErr = aSplitter.ErrorStatus();
253 if (iErr) {
254 di << "Error: " << iErr << "\n";
255 return 0;
256 }
257 //
258 // getting the result of the operation
259 const TopoDS_Shape& aR = aSplitter.Shape();
260 if (aR.IsNull()) {
261 di << " null shape\n";
262 return 0;
263 }
264 //
265 DBRep::Set(a[1], aR);
266 return 0;
267}