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