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>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <TopoDS_Iterator.hxx>
47 #include <TopTools_MapOfShape.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_DataMapOfShapeShape.hxx>
52 #include <ShapeFix_ShapeTolerance.hxx>
53 #include <ShapeFix_Shape.hxx>
55 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
56 #include <TColStd_ListIteratorOfListOfInteger.hxx>
57 #include <TColStd_ListOfInteger.hxx>
58 #include <Standard_NullObject.hxx>
59 #include <Precision.hxx>
62 //=======================================================================
65 //=======================================================================
66 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
68 static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
69 return aPartitionDriver;
73 //=======================================================================
74 //function : GEOMImpl_PartitionDriver
76 //=======================================================================
77 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
81 //=======================================================================
82 //function : SimplifyCompound
84 //=======================================================================
85 static void PrepareShapes (const TopoDS_Shape& theShape,
86 Standard_Integer theType,
87 TopTools_ListOfShape& theSimpleList)
89 if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
90 theShape.ShapeType() != TopAbs_COMPOUND) {
91 theSimpleList.Append(theShape);
95 // explode compound on simple shapes to allow their intersections
96 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
97 TopTools_MapOfShape mapShape;
98 for (; It.More(); It.Next()) {
99 if (mapShape.Add(It.Value())) {
100 TopoDS_Shape curSh = It.Value();
101 PrepareShapes(curSh, theType, theSimpleList);
106 //=======================================================================
109 //=======================================================================
110 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
112 if (Label().IsNull()) return 0;
113 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
115 GEOMImpl_IPartition aCI (aFunction);
116 Standard_Integer aType = aFunction->GetType();
119 //sklNMTAlgo_Splitter1 PS;
120 GEOMAlgo_Splitter PS;
122 TopTools_DataMapOfShapeShape aCopyMap;
123 TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
125 if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
127 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
128 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetTools();
129 Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
130 Handle(TColStd_HSequenceOfTransient) aRemIns = aCI.GetRemoveIns();
131 Handle(TColStd_HArray1OfInteger) aMaterials = aCI.GetMaterials();
132 //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
135 //unsigned int ind, nbshapes = 0;
136 //nbshapes += aShapes->Length() + aTools->Length();
137 //nbshapes += aKeepIns->Length() + aRemIns->Length();
138 //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
139 TopTools_MapOfShape ShapesMap, ToolsMap;
141 // add object shapes that are in ListShapes;
142 for (ind = 1; ind <= aShapes->Length(); ind++) {
143 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
144 TopoDS_Shape aShape_i = aRefShape->GetValue();
145 if (aShape_i.IsNull()) {
146 Standard_NullObject::Raise("In Partition a shape is null");
149 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
150 TopoDS_Shape aShape_i_copy;
151 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
152 //if (aCopyTool.IsDone())
153 // aShape_i_copy = aCopyTool.Shape();
155 // Standard_NullObject::Raise("Bad shape detected");
157 // fill aCopyMap for history
158 TopTools_IndexedMapOfShape aShape_i_inds;
159 TopTools_IndexedMapOfShape aShape_i_copy_inds;
160 TopExp::MapShapes(aShape_i, aShape_i_inds);
161 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
162 Standard_Integer nbInds = aShape_i_inds.Extent();
163 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
164 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
167 TopTools_ListOfShape aSimpleShapes;
168 //PrepareShapes(aShape_i, aType, aSimpleShapes);
169 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
170 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
171 for (; aSimpleIter.More(); aSimpleIter.Next()) {
172 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
173 if (ShapesMap.Add(aSimpleSh)) {
174 PS.AddShape(aSimpleSh);
175 //skl if (DoRemoveWebs) {
176 //skl if (aMaterials->Length() >= ind)
177 //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
183 // add tool shapes that are in ListTools and not in ListShapes;
184 for (ind = 1; ind <= aTools->Length(); ind++) {
185 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
186 TopoDS_Shape aShape_i = aRefShape->GetValue();
187 if (aShape_i.IsNull()) {
188 Standard_NullObject::Raise("In Partition a tool shape is null");
191 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
192 TopoDS_Shape aShape_i_copy;
193 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
194 //if (aCopyTool.IsDone())
195 // aShape_i_copy = aCopyTool.Shape();
197 // Standard_NullObject::Raise("Bad shape detected");
199 // fill aCopyMap for history
200 TopTools_IndexedMapOfShape aShape_i_inds;
201 TopTools_IndexedMapOfShape aShape_i_copy_inds;
202 TopExp::MapShapes(aShape_i, aShape_i_inds);
203 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
204 Standard_Integer nbInds = aShape_i_inds.Extent();
205 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
206 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
209 TopTools_ListOfShape aSimpleShapes;
210 //PrepareShapes(aShape_i, aType, aSimpleShapes);
211 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
212 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
213 for (; aSimpleIter.More(); aSimpleIter.Next()) {
214 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
215 if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
216 PS.AddTool(aSimpleSh);
221 // add shapes that are in ListKeepInside, as object shapes;
222 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
223 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
224 TopoDS_Shape aShape_i = aRefShape->GetValue();
225 if (aShape_i.IsNull()) {
226 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
229 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
230 TopoDS_Shape aShape_i_copy;
231 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
232 //if (aCopyTool.IsDone())
233 // aShape_i_copy = aCopyTool.Shape();
235 // Standard_NullObject::Raise("Bad shape detected");
237 // fill aCopyMap for history
238 TopTools_IndexedMapOfShape aShape_i_inds;
239 TopTools_IndexedMapOfShape aShape_i_copy_inds;
240 TopExp::MapShapes(aShape_i, aShape_i_inds);
241 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
242 Standard_Integer nbInds = aShape_i_inds.Extent();
243 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
244 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
247 TopTools_ListOfShape aSimpleShapes;
248 //PrepareShapes(aShape_i, aType, aSimpleShapes);
249 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
250 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
251 for (; aSimpleIter.More(); aSimpleIter.Next()) {
252 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
253 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
254 PS.AddShape(aSimpleSh);
258 // add shapes that are in ListRemoveInside, as object shapes;
259 for (ind = 1; ind <= aRemIns->Length(); ind++) {
260 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
261 TopoDS_Shape aShape_i = aRefShape->GetValue();
262 if (aShape_i.IsNull()) {
263 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
266 //BRepBuilderAPI_Copy aCopyTool (aShape_i);
267 TopoDS_Shape aShape_i_copy;
268 TNaming_CopyShape::CopyTool(aShape_i, aMapTShapes, aShape_i_copy);
269 //if (aCopyTool.IsDone())
270 // aShape_i_copy = aCopyTool.Shape();
272 // Standard_NullObject::Raise("Bad shape detected");
274 // fill aCopyMap for history
275 TopTools_IndexedMapOfShape aShape_i_inds;
276 TopTools_IndexedMapOfShape aShape_i_copy_inds;
277 TopExp::MapShapes(aShape_i, aShape_i_inds);
278 TopExp::MapShapes(aShape_i_copy, aShape_i_copy_inds);
279 Standard_Integer nbInds = aShape_i_inds.Extent();
280 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
281 aCopyMap.Bind(aShape_i_inds.FindKey(ie), aShape_i_copy_inds.FindKey(ie));
284 TopTools_ListOfShape aSimpleShapes;
285 //PrepareShapes(aShape_i, aType, aSimpleShapes);
286 PrepareShapes(aShape_i_copy, aType, aSimpleShapes);
287 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
288 for (; aSimpleIter.More(); aSimpleIter.Next()) {
289 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
290 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
291 PS.AddShape(aSimpleSh);
295 PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
296 PS.SetLimit((TopAbs_ShapeEnum)aCI.GetLimit());
300 //skl PS.SetRemoveWebs(!DoRemoveWebs);
301 //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
303 // suppress result outside of shapes in KInsideMap
304 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
305 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
306 TopoDS_Shape aShape_i = aRefShape->GetValue();
307 PS.KeepShapesInside(aShape_i);
310 // suppress result inside of shapes in RInsideMap
311 for (ind = 1; ind <= aRemIns->Length(); ind++) {
312 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
313 TopoDS_Shape aShape_i = aRefShape->GetValue();
314 PS.RemoveShapesInside(aShape_i);
318 else if (aType == PARTITION_HALF)
320 Handle(GEOM_Function) aRefShape = aCI.GetShape();
321 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
322 TopoDS_Shape aShapeArg = aRefShape->GetValue();
323 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
325 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
326 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
329 TopoDS_Shape aShapeArg_copy;
330 TopoDS_Shape aPlaneArg_copy;
332 TNaming_CopyShape::CopyTool(aShapeArg, aMapTShapes, aShapeArg_copy);
333 //BRepBuilderAPI_Copy aCopyTool (aShapeArg);
334 //if (aCopyTool.IsDone())
335 // aShapeArg_copy = aCopyTool.Shape();
337 // Standard_NullObject::Raise("Bad shape detected");
339 // fill aCopyMap for history
340 TopTools_IndexedMapOfShape aShapeArg_inds;
341 TopTools_IndexedMapOfShape aShapeArg_copy_inds;
342 TopExp::MapShapes(aShapeArg, aShapeArg_inds);
343 TopExp::MapShapes(aShapeArg_copy, aShapeArg_copy_inds);
344 Standard_Integer nbInds = aShapeArg_inds.Extent();
345 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
346 aCopyMap.Bind(aShapeArg_inds.FindKey(ie), aShapeArg_copy_inds.FindKey(ie));
350 TNaming_CopyShape::CopyTool(aPlaneArg, aMapTShapes, aPlaneArg_copy);
351 //BRepBuilderAPI_Copy aCopyTool (aPlaneArg);
352 //if (aCopyTool.IsDone())
353 // aPlaneArg_copy = aCopyTool.Shape();
355 // Standard_NullObject::Raise("Bad shape detected");
357 // fill aCopyMap for history
358 TopTools_IndexedMapOfShape aPlaneArg_inds;
359 TopTools_IndexedMapOfShape aPlaneArg_copy_inds;
360 TopExp::MapShapes(aPlaneArg, aPlaneArg_inds);
361 TopExp::MapShapes(aPlaneArg_copy, aPlaneArg_copy_inds);
362 Standard_Integer nbInds = aPlaneArg_inds.Extent();
363 for (Standard_Integer ie = 1; ie <= nbInds; ie++) {
364 aCopyMap.Bind(aPlaneArg_inds.FindKey(ie), aPlaneArg_copy_inds.FindKey(ie));
368 // add object shapes that are in ListShapes;
369 PS.AddShape(aShapeArg_copy);
370 //PS.AddShape(aShapeArg);
372 // add tool shapes that are in ListTools and not in ListShapes;
373 PS.AddTool(aPlaneArg_copy);
374 //PS.AddTool(aPlaneArg);
378 //PS.SetRemoveWebs(Standard_False);
379 //PS.Build(aShapeArg.ShapeType());
385 if (aShape.IsNull()) return 0;
387 //Alternative case to check not valid partition IPAL21418
388 TopoDS_Iterator It (aShape, Standard_True, Standard_True);
390 for (; It.More(); It.Next())
393 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
396 if (!BRepAlgo::IsValid(aShape)) {
397 // 08.07.2008 added by skl during fixing bug 19761 from Mantis
398 ShapeFix_ShapeTolerance aSFT;
399 aSFT.LimitTolerance(aShape, Precision::Confusion(),
400 Precision::Confusion(), TopAbs_SHAPE);
401 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
403 aShape = aSfs->Shape();
404 if (!BRepAlgo::IsValid(aShape))
405 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
408 aFunction->SetValue(aShape);
410 // Fill history to be used by GetInPlace functionality
411 TopTools_IndexedMapOfShape aResIndices;
412 TopExp::MapShapes(aShape, aResIndices);
414 // Map: source_shape/images of source_shape in Result
415 const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
417 // history for all argument shapes
418 // be sure to use aCopyMap
419 TDF_LabelSequence aLabelSeq;
420 aFunction->GetDependency(aLabelSeq);
421 Standard_Integer nbArg = aLabelSeq.Length();
423 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
425 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
427 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
428 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
430 TopTools_IndexedMapOfShape anArgumentIndices;
431 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
432 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
434 // Find corresponding label in history
435 TDF_Label anArgumentHistoryLabel =
436 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
438 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
439 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
440 // be sure to use aCopyMap here
441 if (aCopyMap.IsBound(anEntity))
442 anEntity = aCopyMap.Find(anEntity);
444 if (!aMR.Contains(anEntity)) continue;
446 const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
447 Standard_Integer nbModified = aModified.Extent();
449 if (nbModified > 0) {
450 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
451 Handle(TDataStd_IntegerArray) anAttr =
452 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
455 TopTools_ListIteratorOfListOfShape itM (aModified);
456 for (; itM.More(); itM.Next(), ++ih) {
457 int id = aResIndices.FindIndex(itM.Value());
458 anAttr->SetValue(ih, id);
464 log.SetTouched(Label());
470 //=======================================================================
471 //function : GEOMImpl_PartitionDriver_Type_
473 //=======================================================================
474 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
476 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
477 if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
478 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
479 if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
480 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
481 if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
483 static Handle_Standard_Transient _Ancestors[] = {aType1,aType2,aType3,NULL};
484 static Handle_Standard_Type _aType =
485 new Standard_Type ("GEOMImpl_PartitionDriver", sizeof(GEOMImpl_PartitionDriver),
486 1, (Standard_Address)_Ancestors, (Standard_Address)NULL);
491 //=======================================================================
492 //function : DownCast
494 //=======================================================================
495 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
497 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
499 if (!AnObject.IsNull()) {
500 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
501 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
505 return _anOtherObject;