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
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 <NMTAlgo_Splitter1.hxx>
33 #include <GEOMAlgo_Splitter.hxx>
34 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <TDataStd_IntegerArray.hxx>
38 #include <BRep_Tool.hxx>
39 #include <BRepAlgo.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_ListIteratorOfListOfShape.hxx>
51 #include <ShapeFix_ShapeTolerance.hxx>
52 #include <ShapeFix_Shape.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <TColStd_ListOfInteger.hxx>
56 #include <Standard_NullObject.hxx>
57 #include <Precision.hxx>
60 //=======================================================================
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
66 static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
67 return aPartitionDriver;
71 //=======================================================================
72 //function : GEOMImpl_PartitionDriver
74 //=======================================================================
75 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
79 //=======================================================================
80 //function : SimplifyCompound
82 //=======================================================================
83 static void PrepareShapes (const TopoDS_Shape& theShape,
84 Standard_Integer theType,
85 TopTools_ListOfShape& theSimpleList)
87 if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
88 theShape.ShapeType() != TopAbs_COMPOUND) {
89 theSimpleList.Append(theShape);
93 // explode compound on simple shapes to allow their intersections
94 TopoDS_Iterator It (theShape, Standard_True, Standard_True);
95 TopTools_MapOfShape mapShape;
96 for (; It.More(); It.Next()) {
97 if (mapShape.Add(It.Value())) {
98 TopoDS_Shape curSh = It.Value();
99 PrepareShapes(curSh, theType, theSimpleList);
104 //=======================================================================
107 //=======================================================================
108 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
110 if (Label().IsNull()) return 0;
111 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
113 GEOMImpl_IPartition aCI (aFunction);
114 Standard_Integer aType = aFunction->GetType();
117 //sklNMTAlgo_Splitter1 PS;
118 GEOMAlgo_Splitter PS;
120 if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
122 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
123 Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetTools();
124 Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
125 Handle(TColStd_HSequenceOfTransient) aRemIns = aCI.GetRemoveIns();
126 Handle(TColStd_HArray1OfInteger) aMaterials = aCI.GetMaterials();
127 //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
130 //unsigned int ind, nbshapes = 0;
131 //nbshapes += aShapes->Length() + aTools->Length();
132 //nbshapes += aKeepIns->Length() + aRemIns->Length();
133 //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
134 TopTools_MapOfShape ShapesMap, ToolsMap;
136 // add object shapes that are in ListShapes;
137 for (ind = 1; ind <= aShapes->Length(); ind++) {
138 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
139 TopoDS_Shape aShape_i = aRefShape->GetValue();
140 if (aShape_i.IsNull()) {
141 Standard_NullObject::Raise("In Partition a shape is null");
144 TopTools_ListOfShape aSimpleShapes;
145 PrepareShapes(aShape_i, aType, aSimpleShapes);
146 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
147 for (; aSimpleIter.More(); aSimpleIter.Next()) {
148 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
149 if (ShapesMap.Add(aSimpleSh)) {
150 PS.AddShape(aSimpleSh);
151 //skl if (DoRemoveWebs) {
152 //skl if (aMaterials->Length() >= ind)
153 //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
159 // add tool shapes that are in ListTools and not in ListShapes;
160 for (ind = 1; ind <= aTools->Length(); ind++) {
161 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
162 TopoDS_Shape aShape_i = aRefShape->GetValue();
163 if (aShape_i.IsNull()) {
164 Standard_NullObject::Raise("In Partition a tool shape is null");
167 TopTools_ListOfShape aSimpleShapes;
168 PrepareShapes(aShape_i, aType, aSimpleShapes);
169 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
170 for (; aSimpleIter.More(); aSimpleIter.Next()) {
171 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
172 if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
173 PS.AddTool(aSimpleSh);
178 // add shapes that are in ListKeepInside, as object shapes;
179 for (ind = 1; ind <= aKeepIns->Length(); ind++) {
180 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
181 TopoDS_Shape aShape_i = aRefShape->GetValue();
182 if (aShape_i.IsNull()) {
183 Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
186 TopTools_ListOfShape aSimpleShapes;
187 PrepareShapes(aShape_i, aType, aSimpleShapes);
188 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
189 for (; aSimpleIter.More(); aSimpleIter.Next()) {
190 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
191 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
192 PS.AddShape(aSimpleSh);
196 // add shapes that are in ListRemoveInside, as object shapes;
197 for (ind = 1; ind <= aRemIns->Length(); ind++) {
198 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
199 TopoDS_Shape aShape_i = aRefShape->GetValue();
200 if (aShape_i.IsNull()) {
201 Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
204 TopTools_ListOfShape aSimpleShapes;
205 PrepareShapes(aShape_i, aType, aSimpleShapes);
206 TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
207 for (; aSimpleIter.More(); aSimpleIter.Next()) {
208 const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
209 if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
210 PS.AddShape(aSimpleSh);
214 PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
215 PS.SetLimit( (TopAbs_ShapeEnum)aCI.GetLimit() );
219 //skl PS.SetRemoveWebs(!DoRemoveWebs);
220 //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
222 // suppress result outside of shapes in KInsideMap
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 PS.KeepShapesInside(aShape_i);
229 // suppress result inside of shapes in RInsideMap
230 for (ind = 1; ind <= aRemIns->Length(); ind++) {
231 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
232 TopoDS_Shape aShape_i = aRefShape->GetValue();
233 PS.RemoveShapesInside(aShape_i);
237 else if (aType == PARTITION_HALF)
239 Handle(GEOM_Function) aRefShape = aCI.GetShape();
240 Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
241 TopoDS_Shape aShapeArg = aRefShape->GetValue();
242 TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
244 if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
245 Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
248 // add object shapes that are in ListShapes;
249 PS.AddShape(aShapeArg);
251 // add tool shapes that are in ListTools and not in ListShapes;
252 PS.AddTool(aPlaneArg);
256 //PS.SetRemoveWebs(Standard_False);
257 //PS.Build(aShapeArg.ShapeType());
263 if (aShape.IsNull()) return 0;
265 //Alternative case to check not valid partition IPAL21418
266 TopoDS_Iterator It (aShape, Standard_True, Standard_True);
268 for (; It.More(); It.Next())
271 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
274 if (!BRepAlgo::IsValid(aShape)) {
275 // 08.07.2008 added by skl during fixing bug 19761 from Mantis
276 ShapeFix_ShapeTolerance aSFT;
277 aSFT.LimitTolerance(aShape, Precision::Confusion(),
278 Precision::Confusion(), TopAbs_SHAPE);
279 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
281 aShape = aSfs->Shape();
282 if (!BRepAlgo::IsValid(aShape))
283 Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
286 aFunction->SetValue(aShape);
288 // Fill history to be used by GetInPlace functionality
289 TopTools_IndexedMapOfShape aResIndices;
290 TopExp::MapShapes(aShape, aResIndices);
292 // Map: source_shape/images of source_shape in Result
293 const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
295 // history for all argument shapes
296 TDF_LabelSequence aLabelSeq;
297 aFunction->GetDependency(aLabelSeq);
298 Standard_Integer nbArg = aLabelSeq.Length();
300 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
302 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
304 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
305 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
307 TopTools_IndexedMapOfShape anArgumentIndices;
308 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
309 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
311 // Find corresponding label in history
312 TDF_Label anArgumentHistoryLabel =
313 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
315 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
316 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
317 if (!aMR.Contains(anEntity)) continue;
319 const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
320 Standard_Integer nbModified = aModified.Extent();
322 if (nbModified > 0) {
323 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
324 Handle(TDataStd_IntegerArray) anAttr =
325 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
328 TopTools_ListIteratorOfListOfShape itM (aModified);
329 for (; itM.More(); itM.Next(), ++ih) {
330 int id = aResIndices.FindIndex(itM.Value());
331 anAttr->SetValue(ih, id);
337 log.SetTouched(Label());
343 //=======================================================================
344 //function : GEOMImpl_PartitionDriver_Type_
346 //=======================================================================
347 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
350 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
351 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
352 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
353 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
354 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
355 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
358 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
359 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PartitionDriver",
360 sizeof(GEOMImpl_PartitionDriver),
362 (Standard_Address)_Ancestors,
363 (Standard_Address)NULL);
368 //=======================================================================
369 //function : DownCast
371 //=======================================================================
372 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
374 Handle(GEOMImpl_PartitionDriver) _anOtherObject;
376 if (!AnObject.IsNull()) {
377 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
378 _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
382 return _anOtherObject ;