1 // Copyright (C) 2007-2010 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
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_PartitionDriver.hxx>
25 #include <GEOMImpl_IPartition.hxx>
26 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Object.hxx>
29 #include <GEOM_Function.hxx>
31 #include <GEOMAlgo_Splitter.hxx>
33 #include <TDataStd_IntegerArray.hxx>
34 #include <TNaming_CopyShape.hxx>
36 //#include <BRepBuilderAPI_Copy.hxx>
37 #include <BRep_Tool.hxx>
38 #include <BRepAlgo.hxx>
39 #include <BRepTools.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Wire.hxx>
47 #include <TopoDS_Iterator.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopTools_DataMapOfShapeShape.hxx>
53 #include <ShapeFix_ShapeTolerance.hxx>
54 #include <ShapeFix_Shape.hxx>
56 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TColStd_ListOfInteger.hxx>
59 #include <Standard_NullObject.hxx>
60 #include <Precision.hxx>
63 //=======================================================================
66 //=======================================================================
67 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
69 static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
70 return aPartitionDriver;
74 //=======================================================================
75 //function : GEOMImpl_PartitionDriver
77 //=======================================================================
78 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
82 //=======================================================================
83 //function : SimplifyCompound
85 //=======================================================================
86 static void PrepareShapes (const TopoDS_Shape& theShape,
87 Standard_Integer theType,
88 TopTools_ListOfShape& theSimpleList)
90 if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
91 theShape.ShapeType() != TopAbs_COMPOUND) {
92 theSimpleList.Append(theShape);
96 // explode compound on simple shapes to allow their intersections
97 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
98 TopTools_MapOfShape mapShape;
99 for (; It.More(); It.Next()) {
100 if (mapShape.Add(It.Value())) {
101 TopoDS_Shape curSh = It.Value();
102 PrepareShapes(curSh, theType, theSimpleList);
107 //=======================================================================
110 //=======================================================================
111 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
113 if (Label().IsNull()) return 0;
114 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
116 GEOMImpl_IPartition aCI (aFunction);
117 Standard_Integer aType = aFunction->GetType();
120 //sklNMTAlgo_Splitter1 PS;
121 GEOMAlgo_Splitter PS;
123 TopTools_DataMapOfShapeShape aCopyMap;
124 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
126 if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
128 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
129 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetTools();
130 Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
131 Handle(TColStd_HSequenceOfTransient) aRemIns = aCI.GetRemoveIns();
132 Handle(TColStd_HArray1OfInteger) aMaterials = aCI.GetMaterials();
133 //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
136 //unsigned int ind, nbshapes = 0;
137 //nbshapes += aShapes->Length() + aTools->Length();
138 //nbshapes += aKeepIns->Length() + aRemIns->Length();
139 //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
140 TopTools_MapOfShape ShapesMap, ToolsMap;
142 // add object shapes that are in ListShapes;
143 for (ind = 1; ind <= aShapes->Length(); ind++) {
144 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
145 TopoDS_Shape aShape_i = aRefShape->GetValue();
146 if (aShape_i.IsNull()) {
147 Standard_NullObject::Raise("In Partition a shape is null");
150 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
151 TopoDS_Shape aShape_i_copy;
152 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
153 //if (aCopyTool.IsDone())
154 // aShape_i_copy = aCopyTool.Shape();
156 // Standard_NullObject::Raise("Bad shape detected");
158 // fill aCopyMap for history
159 TopTools_IndexedMapOfShape aShape_i_inds;
160 TopTools_IndexedMapOfShape aShape_i_copy_inds;
161 TopExp::MapShapes(aShape_i, aShape_i_inds);
162 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
163 Standard_Integer nbInds = aShape_i_inds.Extent();
164 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
165 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
168 TopTools_ListOfShape aSimpleShapes;
169 //PrepareShapes(aShape_i, aType, aSimpleShapes);
170 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
171 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
172 for (; aSimpleIter.More(); aSimpleIter.Next()) {
173 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
174 if (ShapesMap.Add(aSimpleSh)) {
175 PS.AddShape(aSimpleSh);
176 //skl if (DoRemoveWebs) {
177 //skl if (aMaterials->Length() >= ind)
178 //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
184 // add tool shapes that are in ListTools and not in ListShapes;
185 for (ind = 1; ind <= aTools->Length(); ind++) {
186 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
187 TopoDS_Shape aShape_i = aRefShape->GetValue();
188 if (aShape_i.IsNull()) {
189 Standard_NullObject::Raise("In Partition a tool shape is null");
192 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
193 TopoDS_Shape aShape_i_copy;
194 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
195 //if (aCopyTool.IsDone())
196 // aShape_i_copy = aCopyTool.Shape();
198 // Standard_NullObject::Raise("Bad shape detected");
200 // fill aCopyMap for history
201 TopTools_IndexedMapOfShape aShape_i_inds;
202 TopTools_IndexedMapOfShape aShape_i_copy_inds;
203 TopExp::MapShapes(aShape_i, aShape_i_inds);
204 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
205 Standard_Integer nbInds = aShape_i_inds.Extent();
206 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
207 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
210 TopTools_ListOfShape aSimpleShapes;
211 //PrepareShapes(aShape_i, aType, aSimpleShapes);
212 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
213 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
214 for (; aSimpleIter.More(); aSimpleIter.Next()) {
215 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
216 if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
217 PS.AddTool(aSimpleSh);
222 // add shapes that are in ListKeepInside, as object shapes;
223 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
224 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
225 TopoDS_Shape aShape_i = aRefShape->GetValue();
226 if (aShape_i.IsNull()) {
227 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
230 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
231 TopoDS_Shape aShape_i_copy;
232 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
233 //if (aCopyTool.IsDone())
234 // aShape_i_copy = aCopyTool.Shape();
236 // Standard_NullObject::Raise("Bad shape detected");
238 // fill aCopyMap for history
239 TopTools_IndexedMapOfShape aShape_i_inds;
240 TopTools_IndexedMapOfShape aShape_i_copy_inds;
241 TopExp::MapShapes(aShape_i, aShape_i_inds);
242 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
243 Standard_Integer nbInds = aShape_i_inds.Extent();
244 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
245 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
248 TopTools_ListOfShape aSimpleShapes;
249 //PrepareShapes(aShape_i, aType, aSimpleShapes);
250 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
251 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
252 for (; aSimpleIter.More(); aSimpleIter.Next()) {
253 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
254 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
255 PS.AddShape(aSimpleSh);
259 // add shapes that are in ListRemoveInside, as object shapes;
260 for (ind = 1; ind <= aRemIns->Length(); ind++) {
261 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
262 TopoDS_Shape aShape_i = aRefShape->GetValue();
263 if (aShape_i.IsNull()) {
264 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
267 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
268 TopoDS_Shape aShape_i_copy;
269 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
270 //if (aCopyTool.IsDone())
271 // aShape_i_copy = aCopyTool.Shape();
273 // Standard_NullObject::Raise("Bad shape detected");
275 // fill aCopyMap for history
276 TopTools_IndexedMapOfShape aShape_i_inds;
277 TopTools_IndexedMapOfShape aShape_i_copy_inds;
278 TopExp::MapShapes(aShape_i, aShape_i_inds);
279 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
280 Standard_Integer nbInds = aShape_i_inds.Extent();
281 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
282 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
285 TopTools_ListOfShape aSimpleShapes;
286 //PrepareShapes(aShape_i, aType, aSimpleShapes);
287 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
288 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
289 for (; aSimpleIter.More(); aSimpleIter.Next()) {
290 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
291 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
292 PS.AddShape(aSimpleSh);
296 PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
297 PS.SetLimit((TopAbs_ShapeEnum)aCI.GetLimit());
301 //skl PS.SetRemoveWebs(!DoRemoveWebs);
302 //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
304 // suppress result outside of shapes in KInsideMap
305 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
306 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
307 TopoDS_Shape aShape_i = aRefShape->GetValue();
308 PS.KeepShapesInside(aShape_i);
311 // suppress result inside of shapes in RInsideMap
312 for (ind = 1; ind <= aRemIns->Length(); ind++) {
313 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
314 TopoDS_Shape aShape_i = aRefShape->GetValue();
315 PS.RemoveShapesInside(aShape_i);
319 else if (aType == PARTITION_HALF)
321 Handle(GEOM_Function) aRefShape = aCI.GetShape();
322 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
323 TopoDS_Shape aShapeArg = aRefShape->GetValue();
324 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
326 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
327 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
330 TopoDS_Shape aShapeArg_copy;
331 TopoDS_Shape aPlaneArg_copy;
333 TNaming_CopyShape::CopyTool(aShapeArg, aMapTShapes, aShapeArg_copy);
334 //BRepBuilderAPI_Copy aCopyTool (aShapeArg);
335 //if (aCopyTool.IsDone())
336 // aShapeArg_copy = aCopyTool.Shape();
338 // Standard_NullObject::Raise("Bad shape detected");
340 // fill aCopyMap for history
341 TopTools_IndexedMapOfShape aShapeArg_inds;
342 TopTools_IndexedMapOfShape aShapeArg_copy_inds;
343 TopExp::MapShapes(aShapeArg, aShapeArg_inds);
344 TopExp::MapShapes(aShapeArg_copy, aShapeArg_copy_inds);
345 Standard_Integer nbInds = aShapeArg_inds.Extent();
346 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
347 aCopyMap.Bind(aShapeArg_inds.FindKey(ie), aShapeArg_copy_inds.FindKey(ie));
351 TNaming_CopyShape::CopyTool(aPlaneArg, aMapTShapes, aPlaneArg_copy);
352 //BRepBuilderAPI_Copy aCopyTool (aPlaneArg);
353 //if (aCopyTool.IsDone())
354 // aPlaneArg_copy = aCopyTool.Shape();
356 // Standard_NullObject::Raise("Bad shape detected");
358 // fill aCopyMap for history
359 TopTools_IndexedMapOfShape aPlaneArg_inds;
360 TopTools_IndexedMapOfShape aPlaneArg_copy_inds;
361 TopExp::MapShapes(aPlaneArg, aPlaneArg_inds);
362 TopExp::MapShapes(aPlaneArg_copy, aPlaneArg_copy_inds);
363 Standard_Integer nbInds = aPlaneArg_inds.Extent();
364 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
365 aCopyMap.Bind(aPlaneArg_inds.FindKey(ie), aPlaneArg_copy_inds.FindKey(ie));
369 // add object shapes that are in ListShapes;
370 PS.AddShape(aShapeArg_copy);
371 //PS.AddShape(aShapeArg);
373 // add tool shapes that are in ListTools and not in ListShapes;
374 PS.AddTool(aPlaneArg_copy);
375 //PS.AddTool(aPlaneArg);
379 //PS.SetRemoveWebs(Standard_False);
380 //PS.Build(aShapeArg.ShapeType());
386 if (aShape.IsNull()) return 0;
388 //Alternative case to check not valid partition IPAL21418
389 TopoDS_Iterator It (aShape, Standard_True, Standard_True);
391 for (; It.More(); It.Next())
394 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
397 if (!BRepAlgo::IsValid(aShape)) {
398 // 08.07.2008 added by skl during fixing bug 19761 from Mantis
399 ShapeFix_ShapeTolerance aSFT;
400 aSFT.LimitTolerance(aShape, Precision::Confusion(),
401 Precision::Confusion(), TopAbs_SHAPE);
402 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
404 aShape = aSfs->Shape();
405 if (!BRepAlgo::IsValid(aShape))
406 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
409 aFunction->SetValue(aShape);
411 // Fill history to be used by GetInPlace functionality
412 TopTools_IndexedMapOfShape aResIndices;
413 TopExp::MapShapes(aShape, aResIndices);
415 // Map: source_shape/images of source_shape in Result
416 const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
418 // history for all argument shapes
419 // be sure to use aCopyMap
420 TDF_LabelSequence aLabelSeq;
421 aFunction->GetDependency(aLabelSeq);
422 Standard_Integer nbArg = aLabelSeq.Length();
424 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
426 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
428 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
429 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
431 TopTools_IndexedMapOfShape anArgumentIndices;
432 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
433 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
435 // Find corresponding label in history
436 TDF_Label anArgumentHistoryLabel =
437 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
439 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
440 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
441 // be sure to use aCopyMap here
442 if (aCopyMap.IsBound(anEntity))
443 anEntity = aCopyMap.Find(anEntity);
445 if (!aMR.Contains(anEntity)) continue;
447 const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
448 Standard_Integer nbModified = aModified.Extent();
450 if (nbModified > 0) { // Mantis issue 0021182
452 TopTools_ListIteratorOfListOfShape itM (aModified);
453 for (; itM.More() && nbModified > 0; itM.Next(), ++ih) {
454 if (!aResIndices.Contains(itM.Value())) {
459 if (nbModified > 0) {
460 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
461 Handle(TDataStd_IntegerArray) anAttr =
462 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
465 TopTools_ListIteratorOfListOfShape itM (aModified);
466 for (; itM.More(); itM.Next(), ++ih) {
467 int id = aResIndices.FindIndex(itM.Value());
468 anAttr->SetValue(ih, id);
474 log.SetTouched(Label());
480 //=======================================================================
481 //function : GEOMImpl_PartitionDriver_Type_
483 //=======================================================================
484 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
486 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
487 if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
488 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
489 if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
490 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
491 if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
493 static Handle_Standard_Transient _Ancestors[] = {aType1,aType2,aType3,NULL};
494 static Handle_Standard_Type _aType =
495 new Standard_Type ("GEOMImpl_PartitionDriver", sizeof(GEOMImpl_PartitionDriver),
496 1, (Standard_Address)_Ancestors, (Standard_Address)NULL);
501 //=======================================================================
502 //function : DownCast
504 //=======================================================================
505 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
507 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
509 if (!AnObject.IsNull()) {
510 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
511 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
515 return _anOtherObject;