1 // Created on: 1993-10-15
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <BRepAlgoAPI_BooleanOperation.hxx>
20 #include <BOPAlgo_Alerts.hxx>
21 #include <BOPAlgo_BOP.hxx>
22 #include <BOPAlgo_PaveFiller.hxx>
23 #include <BOPAlgo_Section.hxx>
24 #include <BRepAlgoAPI_Check.hxx>
25 #include <BRepTools.hxx>
27 #include <OSD_Environment.hxx>
28 #include <OSD_File.hxx>
29 #include <TCollection_AsciiString.hxx>
33 //=======================================================================
34 //class : BRepAlgoAPI_DumpOper
35 //purpose : Dumps the arguments ad script to perform operation in DRAW
36 //=======================================================================
37 class BRepAlgoAPI_DumpOper {
39 BRepAlgoAPI_DumpOper() :
40 myIsDump(Standard_False),
41 myIsDumpArgs(Standard_False),
42 myIsDumpRes(Standard_False) {
43 OSD_Environment env("CSF_DEBUG_BOP");
44 TCollection_AsciiString pathdump = env.Value();
45 myIsDump = (!pathdump.IsEmpty() ? Standard_True: Standard_False);
46 myPath=pathdump.ToCString();
49 virtual ~BRepAlgoAPI_DumpOper() {
52 Standard_Boolean IsDump()const {
56 void SetIsDumpArgs(const Standard_Boolean bFlag) {
60 Standard_Boolean IsDumpArgs()const {
64 void SetIsDumpRes(const Standard_Boolean bFlag) {
68 Standard_Boolean IsDumpRes()const {
73 const TopoDS_Shape& theShape1,
74 const TopoDS_Shape& theShape2,
75 const TopoDS_Shape& theResult,
76 BOPAlgo_Operation theOperation);
79 Standard_Boolean myIsDump;
80 Standard_Boolean myIsDumpArgs;
81 Standard_Boolean myIsDumpRes;
82 Standard_CString myPath;
85 //=======================================================================
86 //function : BRepAlgoAPI_BooleanOperation
88 //=======================================================================
89 BRepAlgoAPI_BooleanOperation::BRepAlgoAPI_BooleanOperation()
91 BRepAlgoAPI_BuilderAlgo(),
92 myOperation(BOPAlgo_UNKNOWN)
95 //=======================================================================
96 //function : BRepAlgoAPI_BooleanOperation
98 //=======================================================================
99 BRepAlgoAPI_BooleanOperation::BRepAlgoAPI_BooleanOperation
100 (const BOPAlgo_PaveFiller& thePF)
102 BRepAlgoAPI_BuilderAlgo(thePF),
103 myOperation(BOPAlgo_UNKNOWN)
106 //=======================================================================
107 //function : BRepAlgoAPI_BooleanOperation
109 //=======================================================================
110 BRepAlgoAPI_BooleanOperation::BRepAlgoAPI_BooleanOperation
111 (const TopoDS_Shape& theS1,
112 const TopoDS_Shape& theS2,
113 const BOPAlgo_Operation theOp)
115 BRepAlgoAPI_BuilderAlgo(),
118 myArguments.Append(theS1);
119 myTools.Append(theS2);
121 //=======================================================================
122 //function : BRepAlgoAPI_BooleanOperation
124 //=======================================================================
125 BRepAlgoAPI_BooleanOperation::BRepAlgoAPI_BooleanOperation
126 (const TopoDS_Shape& theS1,
127 const TopoDS_Shape& theS2,
128 const BOPAlgo_PaveFiller& thePF,
129 const BOPAlgo_Operation theOp)
131 BRepAlgoAPI_BuilderAlgo(thePF),
134 myArguments.Append(theS1);
135 myTools.Append(theS2);
137 //=======================================================================
140 //=======================================================================
141 void BRepAlgoAPI_BooleanOperation::Build(const Message_ProgressRange& theRange)
143 // Set Not Done status by default
145 // Clear from previous runs
147 // Check for availability of arguments and tools
148 // Both should be present
149 if (myArguments.IsEmpty() || myTools.IsEmpty())
151 AddError (new BOPAlgo_AlertTooFewArguments);
154 // Check if the operation is set
155 if (myOperation == BOPAlgo_UNKNOWN)
157 AddError (new BOPAlgo_AlertBOPNotSet);
161 // DEBUG option for dumping shapes and scripts
162 BRepAlgoAPI_DumpOper aDumpOper;
164 if (aDumpOper.IsDump()) {
165 BRepAlgoAPI_Check aChekArgs(myArguments.First(), myTools.First(), myOperation);
166 aDumpOper.SetIsDumpArgs(!aChekArgs.IsValid());
170 TCollection_AsciiString aPSName;
174 aPSName = "Performing COMMON operation";
177 aPSName = "Performing FUSE operation";
181 aPSName = "Performing CUT operation";
183 case BOPAlgo_SECTION:
184 aPSName = "Performing SECTION operation";
190 Message_ProgressScope aPS(theRange, aPSName, myIsIntersectionNeeded ? 100 : 30);
191 // If necessary perform intersection of the argument shapes
192 if (myIsIntersectionNeeded)
194 // Combine Objects and Tools into a single list for intersection
195 TopTools_ListOfShape aLArgs = myArguments;
196 for (TopTools_ListOfShape::Iterator it(myTools); it.More(); it.Next())
197 aLArgs.Append(it.Value());
199 // Perform intersection
200 IntersectShapes(aLArgs, aPS.Next(70));
203 if (aDumpOper.IsDump())
205 aDumpOper.SetIsDumpRes(Standard_False);
206 aDumpOper.Dump(myArguments.First(), myTools.First(), TopoDS_Shape(), myOperation);
212 // Builder Initialization
213 if (myOperation == BOPAlgo_SECTION)
215 myBuilder = new BOPAlgo_Section(myAllocator);
216 myBuilder->SetArguments(myDSFiller->Arguments());
220 myBuilder = new BOPAlgo_BOP(myAllocator);
221 myBuilder->SetArguments(myArguments);
222 ((BOPAlgo_BOP*)myBuilder)->SetTools(myTools);
223 ((BOPAlgo_BOP*)myBuilder)->SetOperation(myOperation);
227 BuildResult(aPS.Next(30));
233 if (aDumpOper.IsDump()) {
234 Standard_Boolean isDumpRes = myShape.IsNull() ||
235 !BRepAlgoAPI_Check(myShape).IsValid();
236 aDumpOper.SetIsDumpRes(isDumpRes);
237 aDumpOper.Dump(myArguments.First(), myTools.First(), myShape, myOperation);
241 //=======================================================================
243 //purpose : DEBUG: Dumping the shapes and script of the operation
244 //=======================================================================
245 void BRepAlgoAPI_DumpOper::Dump(const TopoDS_Shape& theShape1,
246 const TopoDS_Shape& theShape2,
247 const TopoDS_Shape& theResult,
248 BOPAlgo_Operation theOperation)
250 if (!(myIsDumpArgs && myIsDumpRes)) {
254 TCollection_AsciiString aPath(myPath);
256 Standard_Integer aNumOper = 1;
257 Standard_Boolean isExist = Standard_True;
258 TCollection_AsciiString aFileName;
262 aFileName = aPath + "BO_" + TCollection_AsciiString(aNumOper) +".tcl";
263 OSD_File aScript(aFileName);
264 isExist = aScript.Exists();
269 FILE* afile = fopen(aFileName.ToCString(), "w+");
273 fprintf(afile,"%s\n","# Arguments are invalid");
275 TCollection_AsciiString aName1;
276 TCollection_AsciiString aName2;
277 TCollection_AsciiString aNameRes;
278 if(!theShape1.IsNull())
281 "Arg1_" + TCollection_AsciiString(aNumOper) + ".brep";
282 BRepTools::Write(theShape1, aName1.ToCString());
285 fprintf(afile,"%s\n","# First argument is Null ");
287 if(!theShape2.IsNull())
290 "Arg2_"+ TCollection_AsciiString(aNumOper) + ".brep";
292 BRepTools::Write(theShape2, aName2.ToCString());
295 fprintf(afile,"%s\n","# Second argument is Null ");
297 if(!theResult.IsNull())
300 "Result_"+ TCollection_AsciiString(aNumOper) + ".brep";
302 BRepTools::Write(theResult, aNameRes.ToCString());
305 fprintf(afile,"%s\n","# Result is Null ");
307 fprintf(afile, "%s %s %s\n","restore", aName1.ToCString(), "arg1");
308 fprintf(afile, "%s %s %s\n","restore", aName2.ToCString(), "arg2");
309 TCollection_AsciiString aBopString;
310 switch (theOperation)
312 case BOPAlgo_COMMON : aBopString += "bcommon Res "; break;
313 case BOPAlgo_FUSE : aBopString += "bfuse Res "; break;
315 case BOPAlgo_CUT21 : aBopString += "bcut Res "; break;
316 case BOPAlgo_SECTION : aBopString += "bsection Res "; break;
319 aBopString += ("arg1 arg2");
320 if(theOperation == BOPAlgo_CUT21)
323 fprintf(afile, "%s\n",aBopString.ToCString());