1 // Copyright (C) 2007-2012 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 TopoDS_Shape aShape_i_copy;
152 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
154 // fill aCopyMap for history
155 TopTools_IndexedMapOfShape aShape_i_inds;
156 TopTools_IndexedMapOfShape aShape_i_copy_inds;
157 TopExp::MapShapes(aShape_i, aShape_i_inds);
158 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
159 Standard_Integer nbInds = aShape_i_inds.Extent();
160 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
161 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
164 TopTools_ListOfShape aSimpleShapes;
165 //PrepareShapes(aShape_i, aType, aSimpleShapes);
166 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
167 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
168 for (; aSimpleIter.More(); aSimpleIter.Next()) {
169 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
170 if (ShapesMap.Add(aSimpleSh)) {
171 PS.AddShape(aSimpleSh);
172 //skl if (DoRemoveWebs) {
173 //skl if (aMaterials->Length() >= ind)
174 //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
180 // add tool shapes that are in ListTools and not in ListShapes;
181 for (ind = 1; ind <= aTools->Length(); ind++) {
182 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
183 TopoDS_Shape aShape_i = aRefShape->GetValue();
184 if (aShape_i.IsNull()) {
185 Standard_NullObject::Raise("In Partition a tool shape is null");
188 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
189 TopoDS_Shape aShape_i_copy;
190 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
191 //if (aCopyTool.IsDone())
192 // aShape_i_copy = aCopyTool.Shape();
194 // Standard_NullObject::Raise("Bad shape detected");
196 // fill aCopyMap for history
197 TopTools_IndexedMapOfShape aShape_i_inds;
198 TopTools_IndexedMapOfShape aShape_i_copy_inds;
199 TopExp::MapShapes(aShape_i, aShape_i_inds);
200 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
201 Standard_Integer nbInds = aShape_i_inds.Extent();
202 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
203 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
206 TopTools_ListOfShape aSimpleShapes;
207 //PrepareShapes(aShape_i, aType, aSimpleShapes);
208 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
209 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
210 for (; aSimpleIter.More(); aSimpleIter.Next()) {
211 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
212 if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
213 PS.AddTool(aSimpleSh);
218 // add shapes that are in ListKeepInside, as object shapes;
219 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
220 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
221 TopoDS_Shape aShape_i = aRefShape->GetValue();
222 if (aShape_i.IsNull()) {
223 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
226 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
227 TopoDS_Shape aShape_i_copy;
228 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
229 //if (aCopyTool.IsDone())
230 // aShape_i_copy = aCopyTool.Shape();
232 // Standard_NullObject::Raise("Bad shape detected");
234 // fill aCopyMap for history
235 TopTools_IndexedMapOfShape aShape_i_inds;
236 TopTools_IndexedMapOfShape aShape_i_copy_inds;
237 TopExp::MapShapes(aShape_i, aShape_i_inds);
238 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
239 Standard_Integer nbInds = aShape_i_inds.Extent();
240 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
241 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
244 TopTools_ListOfShape aSimpleShapes;
245 //PrepareShapes(aShape_i, aType, aSimpleShapes);
246 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
247 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
248 for (; aSimpleIter.More(); aSimpleIter.Next()) {
249 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
250 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
251 PS.AddShape(aSimpleSh);
255 // add shapes that are in ListRemoveInside, as object shapes;
256 for (ind = 1; ind <= aRemIns->Length(); ind++) {
257 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
258 TopoDS_Shape aShape_i = aRefShape->GetValue();
259 if (aShape_i.IsNull()) {
260 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
263 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
264 TopoDS_Shape aShape_i_copy;
265 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
266 //if (aCopyTool.IsDone())
267 // aShape_i_copy = aCopyTool.Shape();
269 // Standard_NullObject::Raise("Bad shape detected");
271 // fill aCopyMap for history
272 TopTools_IndexedMapOfShape aShape_i_inds;
273 TopTools_IndexedMapOfShape aShape_i_copy_inds;
274 TopExp::MapShapes(aShape_i, aShape_i_inds);
275 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
276 Standard_Integer nbInds = aShape_i_inds.Extent();
277 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
278 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
281 TopTools_ListOfShape aSimpleShapes;
282 //PrepareShapes(aShape_i, aType, aSimpleShapes);
283 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
284 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
285 for (; aSimpleIter.More(); aSimpleIter.Next()) {
286 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
287 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
288 PS.AddShape(aSimpleSh);
292 PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
293 PS.SetLimit((TopAbs_ShapeEnum)aCI.GetLimit());
297 //skl PS.SetRemoveWebs(!DoRemoveWebs);
298 //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
300 // suppress result outside of shapes in KInsideMap
301 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
302 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
303 TopoDS_Shape aShape_i = aRefShape->GetValue();
304 PS.KeepShapesInside(aShape_i);
307 // suppress result inside of shapes in RInsideMap
308 for (ind = 1; ind <= aRemIns->Length(); ind++) {
309 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
310 TopoDS_Shape aShape_i = aRefShape->GetValue();
311 PS.RemoveShapesInside(aShape_i);
315 else if (aType == PARTITION_HALF)
317 Handle(GEOM_Function) aRefShape = aCI.GetShape();
318 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
319 TopoDS_Shape aShapeArg = aRefShape->GetValue();
320 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
322 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
323 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
326 TopoDS_Shape aShapeArg_copy;
327 TopoDS_Shape aPlaneArg_copy;
329 TNaming_CopyShape::CopyTool(aShapeArg, aMapTShapes, aShapeArg_copy);
330 //BRepBuilderAPI_Copy aCopyTool (aShapeArg);
331 //if (aCopyTool.IsDone())
332 // aShapeArg_copy = aCopyTool.Shape();
334 // Standard_NullObject::Raise("Bad shape detected");
336 // fill aCopyMap for history
337 TopTools_IndexedMapOfShape aShapeArg_inds;
338 TopTools_IndexedMapOfShape aShapeArg_copy_inds;
339 TopExp::MapShapes(aShapeArg, aShapeArg_inds);
340 TopExp::MapShapes(aShapeArg_copy, aShapeArg_copy_inds);
341 Standard_Integer nbInds = aShapeArg_inds.Extent();
342 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
343 aCopyMap.Bind(aShapeArg_inds.FindKey(ie), aShapeArg_copy_inds.FindKey(ie));
347 TNaming_CopyShape::CopyTool(aPlaneArg, aMapTShapes, aPlaneArg_copy);
348 //BRepBuilderAPI_Copy aCopyTool (aPlaneArg);
349 //if (aCopyTool.IsDone())
350 // aPlaneArg_copy = aCopyTool.Shape();
352 // Standard_NullObject::Raise("Bad shape detected");
354 // fill aCopyMap for history
355 TopTools_IndexedMapOfShape aPlaneArg_inds;
356 TopTools_IndexedMapOfShape aPlaneArg_copy_inds;
357 TopExp::MapShapes(aPlaneArg, aPlaneArg_inds);
358 TopExp::MapShapes(aPlaneArg_copy, aPlaneArg_copy_inds);
359 Standard_Integer nbInds = aPlaneArg_inds.Extent();
360 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
361 aCopyMap.Bind(aPlaneArg_inds.FindKey(ie), aPlaneArg_copy_inds.FindKey(ie));
365 // add object shapes that are in ListShapes;
366 PS.AddShape(aShapeArg_copy);
367 //PS.AddShape(aShapeArg);
369 // add tool shapes that are in ListTools and not in ListShapes;
370 PS.AddTool(aPlaneArg_copy);
371 //PS.AddTool(aPlaneArg);
375 //PS.SetRemoveWebs(Standard_False);
376 //PS.Build(aShapeArg.ShapeType());
382 if (aShape.IsNull()) {
383 // Mantis issue 22009
384 if (PS.ErrorStatus() == 10 && PS.Tools().Extent() == 0 && PS.Shapes().Extent() == 1)
385 aShape = PS.Shapes().First();
390 //Alternative case to check not valid partition IPAL21418
391 TopoDS_Iterator It (aShape, Standard_True, Standard_True);
393 for (; It.More(); It.Next())
396 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
399 if (!BRepAlgo::IsValid(aShape)) {
400 // 08.07.2008 added by skl during fixing bug 19761 from Mantis
401 ShapeFix_ShapeTolerance aSFT;
402 aSFT.LimitTolerance(aShape, Precision::Confusion(),
403 Precision::Confusion(), TopAbs_SHAPE);
404 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
406 aShape = aSfs->Shape();
407 if (!BRepAlgo::IsValid(aShape))
408 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
411 aFunction->SetValue(aShape);
413 // Fill history to be used by GetInPlace functionality
414 TopTools_IndexedMapOfShape aResIndices;
415 TopExp::MapShapes(aShape, aResIndices);
417 // Map: source_shape/images of source_shape in Result
418 const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
420 // history for all argument shapes
421 // be sure to use aCopyMap
422 TDF_LabelSequence aLabelSeq;
423 aFunction->GetDependency(aLabelSeq);
424 Standard_Integer nbArg = aLabelSeq.Length();
426 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
428 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
430 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
431 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
433 TopTools_IndexedMapOfShape anArgumentIndices;
434 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
435 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
437 // Find corresponding label in history
438 TDF_Label anArgumentHistoryLabel =
439 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
441 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
442 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
443 // be sure to use aCopyMap here
444 if (aCopyMap.IsBound(anEntity))
445 anEntity = aCopyMap.Find(anEntity);
447 if (!aMR.Contains(anEntity)) continue;
449 const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
450 Standard_Integer nbModified = aModified.Extent();
452 if (nbModified > 0) { // Mantis issue 0021182
454 TopTools_ListIteratorOfListOfShape itM (aModified);
455 for (; itM.More() && nbModified > 0; itM.Next(), ++ih) {
456 if (!aResIndices.Contains(itM.Value())) {
461 if (nbModified > 0) {
462 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
463 Handle(TDataStd_IntegerArray) anAttr =
464 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
467 TopTools_ListIteratorOfListOfShape itM (aModified);
468 for (; itM.More(); itM.Next(), ++ih) {
469 int id = aResIndices.FindIndex(itM.Value());
470 anAttr->SetValue(ih, id);
476 log.SetTouched(Label());
482 //=======================================================================
483 //function : GEOMImpl_PartitionDriver_Type_
485 //=======================================================================
486 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
488 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
489 if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
490 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
491 if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
492 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
493 if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
495 static Handle_Standard_Transient _Ancestors[] = {aType1,aType2,aType3,NULL};
496 static Handle_Standard_Type _aType =
497 new Standard_Type ("GEOMImpl_PartitionDriver", sizeof(GEOMImpl_PartitionDriver),
498 1, (Standard_Address)_Ancestors, (Standard_Address)NULL);
503 //=======================================================================
504 //function : DownCast
506 //=======================================================================
507 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
509 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
511 if (!AnObject.IsNull()) {
512 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
513 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
517 return _anOtherObject;