Salome HOME
Bug 0020029: EDF 845 GEOM: impossible to set a negative angle for the revolution...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PartitionDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_PartitionDriver.hxx>
24 #include <GEOMImpl_IPartition.hxx>
25 #include <GEOMImpl_Types.hxx>
26
27 #include <GEOM_Object.hxx>
28 #include <GEOM_Function.hxx>
29
30 //#include <NMTAlgo_Splitter1.hxx>
31 #include <GEOMAlgo_Splitter.hxx>
32 #include <TopTools_IndexedMapOfShape.hxx>
33
34 #include <TDataStd_IntegerArray.hxx>
35
36 #include <BRep_Tool.hxx>
37 #include <BRepAlgo.hxx>
38
39 #include <TopAbs.hxx>
40 #include <TopExp.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_Wire.hxx>
45 #include <TopoDS_Iterator.hxx>
46 #include <TopTools_MapOfShape.hxx>
47 #include <TopTools_ListIteratorOfListOfShape.hxx>
48
49 #include <ShapeFix_ShapeTolerance.hxx>
50 #include <ShapeFix_Shape.hxx>
51
52 #include <TColStd_ListIteratorOfListOfInteger.hxx>
53 #include <TColStd_ListOfInteger.hxx>
54 #include <Standard_NullObject.hxx>
55 #include <Precision.hxx>
56 #include <gp_Pnt.hxx>
57
58 //=======================================================================
59 //function : GetID
60 //purpose  :
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
63 {
64   static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
65   return aPartitionDriver;
66 }
67
68
69 //=======================================================================
70 //function : GEOMImpl_PartitionDriver
71 //purpose  :
72 //=======================================================================
73 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
74 {
75 }
76
77 //=======================================================================
78 //function : SimplifyCompound
79 //purpose  :
80 //=======================================================================
81 static void PrepareShapes (const TopoDS_Shape&   theShape,
82                            Standard_Integer      theType,
83                            TopTools_ListOfShape& theSimpleList)
84 {
85   if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
86       theShape.ShapeType() != TopAbs_COMPOUND) {
87     theSimpleList.Append(theShape);
88     return;
89   }
90
91   // explode compound on simple shapes to allow their intersections
92   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
93   TopTools_MapOfShape mapShape;
94   for (; It.More(); It.Next()) {
95     if (mapShape.Add(It.Value())) {
96       TopoDS_Shape curSh = It.Value();
97       PrepareShapes(curSh, theType, theSimpleList);
98     }
99   }
100 }
101
102 //=======================================================================
103 //function : Execute
104 //purpose  :
105 //=======================================================================
106 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
107 {
108   if (Label().IsNull()) return 0;
109   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
110
111   GEOMImpl_IPartition aCI (aFunction);
112   Standard_Integer aType = aFunction->GetType();
113
114   TopoDS_Shape aShape;
115   //sklNMTAlgo_Splitter1 PS;
116   GEOMAlgo_Splitter PS;
117
118   if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
119   {
120     Handle(TColStd_HSequenceOfTransient) aShapes  = aCI.GetShapes();
121     Handle(TColStd_HSequenceOfTransient) aTools   = aCI.GetTools();
122     Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
123     Handle(TColStd_HSequenceOfTransient) aRemIns  = aCI.GetRemoveIns();
124     Handle(TColStd_HArray1OfInteger) aMaterials   = aCI.GetMaterials();
125     //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
126
127     unsigned int ind;
128     //unsigned int ind, nbshapes = 0;
129     //nbshapes += aShapes->Length() + aTools->Length();
130     //nbshapes += aKeepIns->Length() + aRemIns->Length();
131     //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
132     TopTools_MapOfShape ShapesMap, ToolsMap;
133
134     // add object shapes that are in ListShapes;
135     for (ind = 1; ind <= aShapes->Length(); ind++) {
136       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
137       TopoDS_Shape aShape_i = aRefShape->GetValue();
138       if (aShape_i.IsNull()) {
139         Standard_NullObject::Raise("In Partition a shape is null");
140       }
141       //
142       TopTools_ListOfShape aSimpleShapes;
143       PrepareShapes(aShape_i, aType, aSimpleShapes);
144       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
145       for (; aSimpleIter.More(); aSimpleIter.Next()) {
146         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
147         if (ShapesMap.Add(aSimpleSh)) {
148           PS.AddShape(aSimpleSh);
149           //skl if (DoRemoveWebs) {
150           //skl if (aMaterials->Length() >= ind)
151           //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
152           //skl }
153         }
154       }
155     }
156
157     // add tool shapes that are in ListTools and not in ListShapes;
158     for (ind = 1; ind <= aTools->Length(); ind++) {
159       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
160       TopoDS_Shape aShape_i = aRefShape->GetValue();
161       if (aShape_i.IsNull()) {
162         Standard_NullObject::Raise("In Partition a tool shape is null");
163       }
164       //
165       TopTools_ListOfShape aSimpleShapes;
166       PrepareShapes(aShape_i, aType, aSimpleShapes);
167       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
168       for (; aSimpleIter.More(); aSimpleIter.Next()) {
169         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
170         if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
171           PS.AddTool(aSimpleSh);
172         }
173       }
174     }
175
176     // add shapes that are in ListKeepInside, as object shapes;
177     for (ind = 1; ind <= aKeepIns->Length(); ind++) {
178       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
179       TopoDS_Shape aShape_i = aRefShape->GetValue();
180       if (aShape_i.IsNull()) {
181         Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
182       }
183       //
184       TopTools_ListOfShape aSimpleShapes;
185       PrepareShapes(aShape_i, aType, aSimpleShapes);
186       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
187       for (; aSimpleIter.More(); aSimpleIter.Next()) {
188         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
189         if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
190           PS.AddShape(aSimpleSh);
191       }
192     }
193
194     // add shapes that are in ListRemoveInside, as object shapes;
195     for (ind = 1; ind <= aRemIns->Length(); ind++) {
196       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
197       TopoDS_Shape aShape_i = aRefShape->GetValue();
198       if (aShape_i.IsNull()) {
199         Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
200       }
201       //
202       TopTools_ListOfShape aSimpleShapes;
203       PrepareShapes(aShape_i, aType, aSimpleShapes);
204       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
205       for (; aSimpleIter.More(); aSimpleIter.Next()) {
206         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
207         if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
208           PS.AddShape(aSimpleSh);
209       }
210     }
211
212     PS.SetLimitMode(aCI.GetKeepNonlimitShapes());
213     PS.SetLimit( (TopAbs_ShapeEnum)aCI.GetLimit() );
214     PS.Perform();
215
216     //skl PS.Compute();
217     //skl PS.SetRemoveWebs(!DoRemoveWebs);
218     //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
219     /*skl
220     // suppress result outside of shapes in KInsideMap
221     for (ind = 1; ind <= aKeepIns->Length(); ind++) {
222       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
223       TopoDS_Shape aShape_i = aRefShape->GetValue();
224       PS.KeepShapesInside(aShape_i);
225     }
226
227     // suppress result inside of shapes in RInsideMap
228     for (ind = 1; ind <= aRemIns->Length(); ind++) {
229       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
230       TopoDS_Shape aShape_i = aRefShape->GetValue();
231       PS.RemoveShapesInside(aShape_i);
232     }
233     */
234   }
235   else if (aType == PARTITION_HALF)
236   {
237     Handle(GEOM_Function) aRefShape = aCI.GetShape();
238     Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
239     TopoDS_Shape aShapeArg = aRefShape->GetValue();
240     TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
241
242     if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
243       Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
244     }
245
246     // add object shapes that are in ListShapes;
247     PS.AddShape(aShapeArg);
248
249     // add tool shapes that are in ListTools and not in ListShapes;
250     PS.AddTool(aPlaneArg);
251
252     //skl PS.Compute();
253     PS.Perform();
254     //PS.SetRemoveWebs(Standard_False);
255     //PS.Build(aShapeArg.ShapeType());
256
257   } else {
258   }
259
260   aShape = PS.Shape();
261   if (aShape.IsNull()) return 0;
262
263   if (!BRepAlgo::IsValid(aShape)) {
264     // 08.07.2008 added by skl during fixing bug 19761 from Mantis
265     ShapeFix_ShapeTolerance aSFT;
266     aSFT.LimitTolerance(aShape, Precision::Confusion(),
267                         Precision::Confusion(), TopAbs_SHAPE);
268     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
269     aSfs->Perform();
270     aShape = aSfs->Shape();
271     if (!BRepAlgo::IsValid(aShape))
272       Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
273   }
274
275   aFunction->SetValue(aShape);
276
277   // Fill history to be used by GetInPlace functionality
278   TopTools_IndexedMapOfShape aResIndices;
279   TopExp::MapShapes(aShape, aResIndices);
280
281   // Map: source_shape/images of source_shape in Result
282   const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
283
284   // history for all argument shapes
285   TDF_LabelSequence aLabelSeq;
286   aFunction->GetDependency(aLabelSeq);
287   Standard_Integer nbArg = aLabelSeq.Length();
288
289   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
290
291     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
292
293     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
294     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
295
296     TopTools_IndexedMapOfShape anArgumentIndices;
297     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
298     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
299
300     // Find corresponding label in history
301     TDF_Label anArgumentHistoryLabel =
302       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
303
304     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
305       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
306       if (!aMR.Contains(anEntity)) continue;
307
308       const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
309       Standard_Integer nbModified = aModified.Extent();
310
311       if (nbModified > 0) {
312         TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
313         Handle(TDataStd_IntegerArray) anAttr =
314           TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
315
316         int ih = 1;
317         TopTools_ListIteratorOfListOfShape itM (aModified);
318         for (; itM.More(); itM.Next(), ++ih) {
319           int id = aResIndices.FindIndex(itM.Value());
320           anAttr->SetValue(ih, id);
321         }
322       }
323     }
324   }
325
326   log.SetTouched(Label());
327
328   return 1;
329 }
330
331
332 //=======================================================================
333 //function :  GEOMImpl_PartitionDriver_Type_
334 //purpose  :
335 //=======================================================================
336 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
337 {
338
339   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
340   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
341   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
342   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
343   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
344   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
345
346
347   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
348   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PartitionDriver",
349                                                          sizeof(GEOMImpl_PartitionDriver),
350                                                          1,
351                                                          (Standard_Address)_Ancestors,
352                                                          (Standard_Address)NULL);
353
354   return _aType;
355 }
356
357 //=======================================================================
358 //function : DownCast
359 //purpose  :
360 //=======================================================================
361 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
362 {
363   Handle(GEOMImpl_PartitionDriver) _anOtherObject;
364
365   if (!AnObject.IsNull()) {
366      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
367        _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
368      }
369   }
370
371   return _anOtherObject ;
372 }