Salome HOME
0020956: [CEA 421] Problem with ChangeOrientation.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PartitionDriver.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_PartitionDriver.hxx>
26 #include <GEOMImpl_IPartition.hxx>
27 #include <GEOMImpl_Types.hxx>
28
29 #include <GEOM_Object.hxx>
30 #include <GEOM_Function.hxx>
31
32 //#include <NMTAlgo_Splitter1.hxx>
33 #include <GEOMAlgo_Splitter.hxx>
34 #include <TopTools_IndexedMapOfShape.hxx>
35
36 #include <TDataStd_IntegerArray.hxx>
37
38 #include <BRep_Tool.hxx>
39 #include <BRepAlgo.hxx>
40
41 #include <TopAbs.hxx>
42 #include <TopExp.hxx>
43 #include <TopoDS.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>
50
51 #include <ShapeFix_ShapeTolerance.hxx>
52 #include <ShapeFix_Shape.hxx>
53
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <TColStd_ListOfInteger.hxx>
56 #include <Standard_NullObject.hxx>
57 #include <Precision.hxx>
58 #include <gp_Pnt.hxx>
59
60 //=======================================================================
61 //function : GetID
62 //purpose  :
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
65 {
66   static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
67   return aPartitionDriver;
68 }
69
70
71 //=======================================================================
72 //function : GEOMImpl_PartitionDriver
73 //purpose  :
74 //=======================================================================
75 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
76 {
77 }
78
79 //=======================================================================
80 //function : SimplifyCompound
81 //purpose  :
82 //=======================================================================
83 static void PrepareShapes (const TopoDS_Shape&   theShape,
84                            Standard_Integer      theType,
85                            TopTools_ListOfShape& theSimpleList)
86 {
87   if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
88       theShape.ShapeType() != TopAbs_COMPOUND) {
89     theSimpleList.Append(theShape);
90     return;
91   }
92
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);
100     }
101   }
102 }
103
104 //=======================================================================
105 //function : Execute
106 //purpose  :
107 //=======================================================================
108 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
109 {
110   if (Label().IsNull()) return 0;
111   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
112
113   GEOMImpl_IPartition aCI (aFunction);
114   Standard_Integer aType = aFunction->GetType();
115
116   TopoDS_Shape aShape;
117   //sklNMTAlgo_Splitter1 PS;
118   GEOMAlgo_Splitter PS;
119
120   if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
121   {
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();
128
129     unsigned int ind;
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;
135
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");
142       }
143       //
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));
154           //skl }
155         }
156       }
157     }
158
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");
165       }
166       //
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);
174         }
175       }
176     }
177
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");
184       }
185       //
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);
193       }
194     }
195
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");
202       }
203       //
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);
211       }
212     }
213
214     PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
215     PS.SetLimit( (TopAbs_ShapeEnum)aCI.GetLimit() );
216     PS.Perform();
217
218     //skl PS.Compute();
219     //skl PS.SetRemoveWebs(!DoRemoveWebs);
220     //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
221     /*skl
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);
227     }
228
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);
234     }
235     */
236   }
237   else if (aType == PARTITION_HALF)
238   {
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();
243
244     if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
245       Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
246     }
247
248     // add object shapes that are in ListShapes;
249     PS.AddShape(aShapeArg);
250
251     // add tool shapes that are in ListTools and not in ListShapes;
252     PS.AddTool(aPlaneArg);
253
254     //skl PS.Compute();
255     PS.Perform();
256     //PS.SetRemoveWebs(Standard_False);
257     //PS.Build(aShapeArg.ShapeType());
258
259   } else {
260   }
261
262   aShape = PS.Shape();
263   if (aShape.IsNull()) return 0;
264
265   //Alternative case to check not valid partition IPAL21418
266   TopoDS_Iterator It (aShape, Standard_True, Standard_True);
267   int nbSubshapes=0;
268   for (; It.More(); It.Next())
269     nbSubshapes++;
270   if (!nbSubshapes)
271     Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
272   //end of IPAL21418
273
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);
280     aSfs->Perform();
281     aShape = aSfs->Shape();
282     if (!BRepAlgo::IsValid(aShape))
283       Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
284   }
285
286   aFunction->SetValue(aShape);
287
288   // Fill history to be used by GetInPlace functionality
289   TopTools_IndexedMapOfShape aResIndices;
290   TopExp::MapShapes(aShape, aResIndices);
291
292   // Map: source_shape/images of source_shape in Result
293   const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
294
295   // history for all argument shapes
296   TDF_LabelSequence aLabelSeq;
297   aFunction->GetDependency(aLabelSeq);
298   Standard_Integer nbArg = aLabelSeq.Length();
299
300   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
301
302     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
303
304     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
305     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
306
307     TopTools_IndexedMapOfShape anArgumentIndices;
308     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
309     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
310
311     // Find corresponding label in history
312     TDF_Label anArgumentHistoryLabel =
313       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
314
315     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
316       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
317       if (!aMR.Contains(anEntity)) continue;
318
319       const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
320       Standard_Integer nbModified = aModified.Extent();
321
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);
326
327         int ih = 1;
328         TopTools_ListIteratorOfListOfShape itM (aModified);
329         for (; itM.More(); itM.Next(), ++ih) {
330           int id = aResIndices.FindIndex(itM.Value());
331           anAttr->SetValue(ih, id);
332         }
333       }
334     }
335   }
336
337   log.SetTouched(Label());
338
339   return 1;
340 }
341
342
343 //=======================================================================
344 //function :  GEOMImpl_PartitionDriver_Type_
345 //purpose  :
346 //=======================================================================
347 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
348 {
349
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);
356
357
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),
361                                                          1,
362                                                          (Standard_Address)_Ancestors,
363                                                          (Standard_Address)NULL);
364
365   return _aType;
366 }
367
368 //=======================================================================
369 //function : DownCast
370 //purpose  :
371 //=======================================================================
372 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
373 {
374   Handle(GEOMImpl_PartitionDriver) _anOtherObject;
375
376   if (!AnObject.IsNull()) {
377      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
378        _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
379      }
380   }
381
382   return _anOtherObject ;
383 }