1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_PartitionDriver.hxx>
26 #include <GEOMImpl_IPartition.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Object.hxx>
30 #include <GEOM_Function.hxx>
32 #include <GEOMAlgo_Splitter.hxx>
34 #include <TDataStd_IntegerArray.hxx>
35 #include <TNaming_CopyShape.hxx>
37 //#include <BRepBuilderAPI_Copy.hxx>
38 #include <BRep_Tool.hxx>
39 #include <BRepAlgo.hxx>
40 #include <BRepTools.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <TopoDS_Wire.hxx>
48 #include <TopoDS_Iterator.hxx>
49 #include <TopTools_MapOfShape.hxx>
50 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TopTools_DataMapOfShapeShape.hxx>
54 #include <ShapeFix_ShapeTolerance.hxx>
55 #include <ShapeFix_Shape.hxx>
57 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
58 #include <TColStd_ListIteratorOfListOfInteger.hxx>
59 #include <TColStd_ListOfInteger.hxx>
60 #include <Standard_NullObject.hxx>
61 #include <Precision.hxx>
64 //=======================================================================
67 //=======================================================================
68 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
70 static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
71 return aPartitionDriver;
75 //=======================================================================
76 //function : GEOMImpl_PartitionDriver
78 //=======================================================================
79 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
83 //=======================================================================
84 //function : SimplifyCompound
86 //=======================================================================
87 static void PrepareShapes (const TopoDS_Shape& theShape,
88 Standard_Integer theType,
89 TopTools_ListOfShape& theSimpleList)
91 if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
92 theShape.ShapeType() != TopAbs_COMPOUND) {
93 theSimpleList.Append(theShape);
97 // explode compound on simple shapes to allow their intersections
98 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
99 TopTools_MapOfShape mapShape;
100 for (; It.More(); It.Next()) {
101 if (mapShape.Add(It.Value())) {
102 TopoDS_Shape curSh = It.Value();
103 PrepareShapes(curSh, theType, theSimpleList);
108 //=======================================================================
111 //=======================================================================
112 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
114 if (Label().IsNull()) return 0;
115 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
117 GEOMImpl_IPartition aCI (aFunction);
118 Standard_Integer aType = aFunction->GetType();
121 //sklNMTAlgo_Splitter1 PS;
122 GEOMAlgo_Splitter PS;
124 TopTools_DataMapOfShapeShape aCopyMap;
125 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
127 if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
129 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
130 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetTools();
131 Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
132 Handle(TColStd_HSequenceOfTransient) aRemIns = aCI.GetRemoveIns();
133 Handle(TColStd_HArray1OfInteger) aMaterials = aCI.GetMaterials();
134 //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
137 //unsigned int ind, nbshapes = 0;
138 //nbshapes += aShapes->Length() + aTools->Length();
139 //nbshapes += aKeepIns->Length() + aRemIns->Length();
140 //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
141 TopTools_MapOfShape ShapesMap, ToolsMap;
143 // add object shapes that are in ListShapes;
144 for (ind = 1; ind <= aShapes->Length(); ind++) {
145 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
146 TopoDS_Shape aShape_i = aRefShape->GetValue();
147 if (aShape_i.IsNull()) {
148 Standard_NullObject::Raise("In Partition a shape is null");
151 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
152 TopoDS_Shape aShape_i_copy;
153 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
154 //if (aCopyTool.IsDone())
155 // aShape_i_copy = aCopyTool.Shape();
157 // Standard_NullObject::Raise("Bad shape detected");
159 // fill aCopyMap for history
160 TopTools_IndexedMapOfShape aShape_i_inds;
161 TopTools_IndexedMapOfShape aShape_i_copy_inds;
162 TopExp::MapShapes(aShape_i, aShape_i_inds);
163 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
164 Standard_Integer nbInds = aShape_i_inds.Extent();
165 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
166 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
169 TopTools_ListOfShape aSimpleShapes;
170 //PrepareShapes(aShape_i, aType, aSimpleShapes);
171 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
172 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
173 for (; aSimpleIter.More(); aSimpleIter.Next()) {
174 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
175 if (ShapesMap.Add(aSimpleSh)) {
176 PS.AddShape(aSimpleSh);
177 //skl if (DoRemoveWebs) {
178 //skl if (aMaterials->Length() >= ind)
179 //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
185 // add tool shapes that are in ListTools and not in ListShapes;
186 for (ind = 1; ind <= aTools->Length(); ind++) {
187 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
188 TopoDS_Shape aShape_i = aRefShape->GetValue();
189 if (aShape_i.IsNull()) {
190 Standard_NullObject::Raise("In Partition a tool shape is null");
193 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
194 TopoDS_Shape aShape_i_copy;
195 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
196 //if (aCopyTool.IsDone())
197 // aShape_i_copy = aCopyTool.Shape();
199 // Standard_NullObject::Raise("Bad shape detected");
201 // fill aCopyMap for history
202 TopTools_IndexedMapOfShape aShape_i_inds;
203 TopTools_IndexedMapOfShape aShape_i_copy_inds;
204 TopExp::MapShapes(aShape_i, aShape_i_inds);
205 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
206 Standard_Integer nbInds = aShape_i_inds.Extent();
207 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
208 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
211 TopTools_ListOfShape aSimpleShapes;
212 //PrepareShapes(aShape_i, aType, aSimpleShapes);
213 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
214 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
215 for (; aSimpleIter.More(); aSimpleIter.Next()) {
216 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
217 if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
218 PS.AddTool(aSimpleSh);
223 // add shapes that are in ListKeepInside, as object shapes;
224 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
225 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
226 TopoDS_Shape aShape_i = aRefShape->GetValue();
227 if (aShape_i.IsNull()) {
228 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
231 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
232 TopoDS_Shape aShape_i_copy;
233 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
234 //if (aCopyTool.IsDone())
235 // aShape_i_copy = aCopyTool.Shape();
237 // Standard_NullObject::Raise("Bad shape detected");
239 // fill aCopyMap for history
240 TopTools_IndexedMapOfShape aShape_i_inds;
241 TopTools_IndexedMapOfShape aShape_i_copy_inds;
242 TopExp::MapShapes(aShape_i, aShape_i_inds);
243 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
244 Standard_Integer nbInds = aShape_i_inds.Extent();
245 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
246 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
249 TopTools_ListOfShape aSimpleShapes;
250 //PrepareShapes(aShape_i, aType, aSimpleShapes);
251 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
252 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
253 for (; aSimpleIter.More(); aSimpleIter.Next()) {
254 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
255 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
256 PS.AddShape(aSimpleSh);
260 // add shapes that are in ListRemoveInside, as object shapes;
261 for (ind = 1; ind <= aRemIns->Length(); ind++) {
262 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
263 TopoDS_Shape aShape_i = aRefShape->GetValue();
264 if (aShape_i.IsNull()) {
265 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
268 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
269 TopoDS_Shape aShape_i_copy;
270 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
271 //if (aCopyTool.IsDone())
272 // aShape_i_copy = aCopyTool.Shape();
274 // Standard_NullObject::Raise("Bad shape detected");
276 // fill aCopyMap for history
277 TopTools_IndexedMapOfShape aShape_i_inds;
278 TopTools_IndexedMapOfShape aShape_i_copy_inds;
279 TopExp::MapShapes(aShape_i, aShape_i_inds);
280 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
281 Standard_Integer nbInds = aShape_i_inds.Extent();
282 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
283 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
286 TopTools_ListOfShape aSimpleShapes;
287 //PrepareShapes(aShape_i, aType, aSimpleShapes);
288 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
289 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
290 for (; aSimpleIter.More(); aSimpleIter.Next()) {
291 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
292 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
293 PS.AddShape(aSimpleSh);
297 PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
298 PS.SetLimit((TopAbs_ShapeEnum)aCI.GetLimit());
302 //skl PS.SetRemoveWebs(!DoRemoveWebs);
303 //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
305 // suppress result outside of shapes in KInsideMap
306 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
307 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
308 TopoDS_Shape aShape_i = aRefShape->GetValue();
309 PS.KeepShapesInside(aShape_i);
312 // suppress result inside of shapes in RInsideMap
313 for (ind = 1; ind <= aRemIns->Length(); ind++) {
314 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
315 TopoDS_Shape aShape_i = aRefShape->GetValue();
316 PS.RemoveShapesInside(aShape_i);
320 else if (aType == PARTITION_HALF)
322 Handle(GEOM_Function) aRefShape = aCI.GetShape();
323 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
324 TopoDS_Shape aShapeArg = aRefShape->GetValue();
325 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
327 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
328 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
331 TopoDS_Shape aShapeArg_copy;
332 TopoDS_Shape aPlaneArg_copy;
334 TNaming_CopyShape::CopyTool(aShapeArg, aMapTShapes, aShapeArg_copy);
335 //BRepBuilderAPI_Copy aCopyTool (aShapeArg);
336 //if (aCopyTool.IsDone())
337 // aShapeArg_copy = aCopyTool.Shape();
339 // Standard_NullObject::Raise("Bad shape detected");
341 // fill aCopyMap for history
342 TopTools_IndexedMapOfShape aShapeArg_inds;
343 TopTools_IndexedMapOfShape aShapeArg_copy_inds;
344 TopExp::MapShapes(aShapeArg, aShapeArg_inds);
345 TopExp::MapShapes(aShapeArg_copy, aShapeArg_copy_inds);
346 Standard_Integer nbInds = aShapeArg_inds.Extent();
347 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
348 aCopyMap.Bind(aShapeArg_inds.FindKey(ie), aShapeArg_copy_inds.FindKey(ie));
352 TNaming_CopyShape::CopyTool(aPlaneArg, aMapTShapes, aPlaneArg_copy);
353 //BRepBuilderAPI_Copy aCopyTool (aPlaneArg);
354 //if (aCopyTool.IsDone())
355 // aPlaneArg_copy = aCopyTool.Shape();
357 // Standard_NullObject::Raise("Bad shape detected");
359 // fill aCopyMap for history
360 TopTools_IndexedMapOfShape aPlaneArg_inds;
361 TopTools_IndexedMapOfShape aPlaneArg_copy_inds;
362 TopExp::MapShapes(aPlaneArg, aPlaneArg_inds);
363 TopExp::MapShapes(aPlaneArg_copy, aPlaneArg_copy_inds);
364 Standard_Integer nbInds = aPlaneArg_inds.Extent();
365 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
366 aCopyMap.Bind(aPlaneArg_inds.FindKey(ie), aPlaneArg_copy_inds.FindKey(ie));
370 // add object shapes that are in ListShapes;
371 PS.AddShape(aShapeArg_copy);
372 //PS.AddShape(aShapeArg);
374 // add tool shapes that are in ListTools and not in ListShapes;
375 PS.AddTool(aPlaneArg_copy);
376 //PS.AddTool(aPlaneArg);
380 //PS.SetRemoveWebs(Standard_False);
381 //PS.Build(aShapeArg.ShapeType());
387 if (aShape.IsNull()) return 0;
389 //Alternative case to check not valid partition IPAL21418
390 TopoDS_Iterator It (aShape, Standard_True, Standard_True);
392 for (; It.More(); It.Next())
395 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
398 if (!BRepAlgo::IsValid(aShape)) {
399 // 08.07.2008 added by skl during fixing bug 19761 from Mantis
400 ShapeFix_ShapeTolerance aSFT;
401 aSFT.LimitTolerance(aShape, Precision::Confusion(),
402 Precision::Confusion(), TopAbs_SHAPE);
403 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
405 aShape = aSfs->Shape();
406 if (!BRepAlgo::IsValid(aShape))
407 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
410 aFunction->SetValue(aShape);
412 // Fill history to be used by GetInPlace functionality
413 TopTools_IndexedMapOfShape aResIndices;
414 TopExp::MapShapes(aShape, aResIndices);
416 // Map: source_shape/images of source_shape in Result
417 const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
419 // history for all argument shapes
420 // be sure to use aCopyMap
421 TDF_LabelSequence aLabelSeq;
422 aFunction->GetDependency(aLabelSeq);
423 Standard_Integer nbArg = aLabelSeq.Length();
425 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
427 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
429 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
430 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
432 TopTools_IndexedMapOfShape anArgumentIndices;
433 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
434 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
436 // Find corresponding label in history
437 TDF_Label anArgumentHistoryLabel =
438 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
440 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
441 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
442 // be sure to use aCopyMap here
443 if (aCopyMap.IsBound(anEntity))
444 anEntity = aCopyMap.Find(anEntity);
446 if (!aMR.Contains(anEntity)) continue;
448 const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
449 Standard_Integer nbModified = aModified.Extent();
451 if (nbModified > 0) { // Mantis issue 0021182
453 TopTools_ListIteratorOfListOfShape itM (aModified);
454 for (; itM.More() && nbModified > 0; itM.Next(), ++ih) {
455 if (!aResIndices.Contains(itM.Value())) {
460 if (nbModified > 0) {
461 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
462 Handle(TDataStd_IntegerArray) anAttr =
463 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
466 TopTools_ListIteratorOfListOfShape itM (aModified);
467 for (; itM.More(); itM.Next(), ++ih) {
468 int id = aResIndices.FindIndex(itM.Value());
469 anAttr->SetValue(ih, id);
475 log.SetTouched(Label());
481 //=======================================================================
482 //function : GEOMImpl_PartitionDriver_Type_
484 //=======================================================================
485 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
487 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
488 if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
489 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
490 if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
491 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
492 if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
494 static Handle_Standard_Transient _Ancestors[] = {aType1,aType2,aType3,NULL};
495 static Handle_Standard_Type _aType =
496 new Standard_Type ("GEOMImpl_PartitionDriver", sizeof(GEOMImpl_PartitionDriver),
497 1, (Standard_Address)_Ancestors, (Standard_Address)NULL);
502 //=======================================================================
503 //function : DownCast
505 //=======================================================================
506 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
508 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
510 if (!AnObject.IsNull()) {
511 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
512 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
516 return _anOtherObject;