1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Standard_Stream.hxx>
22 #include <GEOMImpl_IBooleanOperations.hxx>
24 #include <GEOM_Function.hxx>
25 #include <GEOM_PythonDump.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include <GEOMImpl_BooleanDriver.hxx>
30 #include <GEOMImpl_IBoolean.hxx>
32 #include <GEOMImpl_PartitionDriver.hxx>
33 #include <GEOMImpl_IPartition.hxx>
35 #include <TDF_Tool.hxx>
37 #include "utilities.h"
39 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
41 //=============================================================================
45 //=============================================================================
46 GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations (GEOM_Engine* theEngine, int theDocID)
47 : GEOM_IOperations(theEngine, theDocID)
49 MESSAGE("GEOMImpl_IBooleanOperations::GEOMImpl_IBooleanOperations");
52 //=============================================================================
56 //=============================================================================
57 GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations()
59 MESSAGE("GEOMImpl_IBooleanOperations::~GEOMImpl_IBooleanOperations");
63 //=============================================================================
67 //=============================================================================
68 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeBoolean (Handle(GEOM_Object) theShape1,
69 Handle(GEOM_Object) theShape2,
70 Standard_Integer theOp)
74 if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
76 //Add a new Boolean object
77 Handle(GEOM_Object) aBool = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
79 //Add a new Boolean function
80 Handle(GEOM_Function) aFunction;
82 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_COMMON);
83 } else if (theOp == 2) {
84 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_CUT);
85 } else if (theOp == 3) {
86 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_FUSE);
87 } else if (theOp == 4) {
88 aFunction = aBool->AddFunction(GEOMImpl_BooleanDriver::GetID(), BOOLEAN_SECTION);
91 if (aFunction.IsNull()) return NULL;
93 //Check if the function is set correctly
94 if (aFunction->GetDriverGUID() != GEOMImpl_BooleanDriver::GetID()) return NULL;
96 GEOMImpl_IBoolean aCI (aFunction);
98 Handle(GEOM_Function) aRef1 = theShape1->GetLastFunction();
99 Handle(GEOM_Function) aRef2 = theShape2->GetLastFunction();
101 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
103 aCI.SetShape1(aRef1);
104 aCI.SetShape2(aRef2);
106 //Compute the Boolean value
108 if (!GetSolver()->ComputeFunction(aFunction)) {
109 SetErrorCode("Boolean driver failed");
113 catch (Standard_Failure) {
114 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
115 SetErrorCode(aFail->GetMessageString());
119 //Make a Python command
120 GEOM::TPythonDump pd (aFunction);
122 if (theOp == 1) pd << " = geompy.MakeCommon(";
123 else if (theOp == 2) pd << " = geompy.MakeCut(";
124 else if (theOp == 3) pd << " = geompy.MakeFuse(";
125 else if (theOp == 4) pd << " = geompy.MakeSection(";
127 pd << theShape1 << ", " << theShape2 << ")";
133 //=============================================================================
137 //=============================================================================
138 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakePartition
139 (const Handle(TColStd_HSequenceOfTransient)& theShapes,
140 const Handle(TColStd_HSequenceOfTransient)& theTools,
141 const Handle(TColStd_HSequenceOfTransient)& theKeepIns,
142 const Handle(TColStd_HSequenceOfTransient)& theRemoveIns,
143 const Standard_Integer theLimit,
144 const Standard_Boolean theRemoveWebs,
145 const Handle(TColStd_HArray1OfInteger)& theMaterials)
149 //Add a new Partition object
150 Handle(GEOM_Object) aPartition = GetEngine()->AddObject(GetDocID(), GEOM_PARTITION);
152 //Add a new Partition function
153 Handle(GEOM_Function) aFunction =
154 aPartition->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_PARTITION);
155 if (aFunction.IsNull()) return NULL;
157 //Check if the function is set correctly
158 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
160 GEOMImpl_IPartition aCI (aFunction);
162 // int aLen = theShapes.size();
163 // aCI.SetLength(aLen);
165 Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
166 Handle(TColStd_HSequenceOfTransient) aToolsSeq = new TColStd_HSequenceOfTransient;
167 Handle(TColStd_HSequenceOfTransient) aKeepInsSeq = new TColStd_HSequenceOfTransient;
168 Handle(TColStd_HSequenceOfTransient) aRemInsSeq = new TColStd_HSequenceOfTransient;
170 Standard_Integer ind, aLen;
171 TCollection_AsciiString anEntry;
172 TCollection_AsciiString aShapesDescr, aToolsDescr, aKeepInsDescr, aRemoveInsDescr;
175 aLen = theShapes->Length();
176 for (ind = 1; ind <= aLen; ind++) {
177 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theShapes->Value(ind));
178 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
179 if (aRefSh.IsNull()) {
180 SetErrorCode("NULL shape for Partition");
183 aShapesSeq->Append(aRefSh);
185 // For Python command
186 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
187 if (ind > 1) aShapesDescr += ", ";
188 aShapesDescr += anEntry;
190 aCI.SetShapes(aShapesSeq);
193 aLen = theTools->Length();
194 for (ind = 1; ind <= aLen; ind++) {
195 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theTools->Value(ind));
196 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
197 if (aRefSh.IsNull()) {
198 SetErrorCode("NULL tool shape for Partition");
201 aToolsSeq->Append(aRefSh);
203 // For Python command
204 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
205 if (ind > 1) aToolsDescr += ", ";
206 aToolsDescr += anEntry;
208 aCI.SetTools(aToolsSeq);
211 aLen = theKeepIns->Length();
212 for (ind = 1; ind <= aLen; ind++) {
213 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theKeepIns->Value(ind));
214 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
215 if (aRefSh.IsNull()) {
216 SetErrorCode("NULL <keep inside> shape for Partition");
219 aKeepInsSeq->Append(aRefSh);
221 // For Python command
222 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
223 if (ind > 1) aKeepInsDescr += ", ";
224 aKeepInsDescr += anEntry;
226 aCI.SetKeepIns(aKeepInsSeq);
229 aLen = theRemoveIns->Length();
230 for (ind = 1; ind <= aLen; ind++) {
231 Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(theRemoveIns->Value(ind));
232 Handle(GEOM_Function) aRefSh = anObj->GetLastFunction();
233 if (aRefSh.IsNull()) {
234 SetErrorCode("NULL <remove inside> shape for Partition");
237 aRemInsSeq->Append(aRefSh);
239 // For Python command
240 TDF_Tool::Entry(anObj->GetEntry(), anEntry);
241 if (ind > 1) aRemoveInsDescr += ", ";
242 aRemoveInsDescr += anEntry;
244 aCI.SetRemoveIns(aRemInsSeq);
247 aCI.SetLimit(theLimit);
251 if (theMaterials.IsNull()) {
252 Handle(TColStd_HArray1OfInteger) aMaterials =
253 new TColStd_HArray1OfInteger (1, aShapesSeq->Length());
254 aCI.SetMaterials(aMaterials);
256 aCI.SetMaterials(theMaterials);
260 //Compute the Partition
262 if (!GetSolver()->ComputeFunction(aFunction)) {
263 SetErrorCode("Partition driver failed");
267 catch (Standard_Failure) {
268 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
269 SetErrorCode(aFail->GetMessageString());
273 //Make a Python command
274 GEOM::TPythonDump pd (aFunction);
275 pd << aPartition << " = geompy.MakePartition([";
277 pd << aShapesDescr.ToCString() << "], [" << aToolsDescr.ToCString() << "], [";
278 // Keep Ins, Remove Ins
279 pd << aKeepInsDescr.ToCString() << "], [" << aRemoveInsDescr.ToCString() << "], ";
280 // Limit, Remove Webs
281 pd << theLimit << ", " << (int)theRemoveWebs << ", [";
283 if (theMaterials->Length() > 0) {
284 int i = theMaterials->Lower();
285 pd << theMaterials->Value(i);
287 for (; i <= theMaterials->Upper(); i++) {
288 pd << ", " << theMaterials->Value(i);
297 //=============================================================================
301 //=============================================================================
302 Handle(GEOM_Object) GEOMImpl_IBooleanOperations::MakeHalfPartition
303 (Handle(GEOM_Object) theShape, Handle(GEOM_Object) thePlane)
307 if (theShape.IsNull() || thePlane.IsNull()) return NULL;
309 //Add a new Boolean object
310 Handle(GEOM_Object) aPart = GetEngine()->AddObject(GetDocID(), GEOM_BOOLEAN);
312 //Add a new Partition function
313 Handle(GEOM_Function) aFunction =
314 aPart->AddFunction(GEOMImpl_PartitionDriver::GetID(), PARTITION_HALF);
315 if (aFunction.IsNull()) return NULL;
317 //Check if the function is set correctly
318 if (aFunction->GetDriverGUID() != GEOMImpl_PartitionDriver::GetID()) return NULL;
320 GEOMImpl_IPartition aCI (aFunction);
322 Handle(GEOM_Function) aRef1 = theShape->GetLastFunction();
323 Handle(GEOM_Function) aRef2 = thePlane->GetLastFunction();
325 if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
330 //Compute the Partition value
332 if (!GetSolver()->ComputeFunction(aFunction)) {
333 SetErrorCode("Partition driver failed");
337 catch (Standard_Failure) {
338 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
339 SetErrorCode(aFail->GetMessageString());
343 //Make a Python command
344 GEOM::TPythonDump(aFunction) << aPart << " = geompy.MakeHalfPartition("
345 << theShape << ", " << thePlane << ")";