Salome HOME
Add new idl function GEOM_IBooleanOperations::MakePartitionNonSelfIntersectedShape...
[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 <TColStd_ListIteratorOfListOfInteger.hxx>
50 #include <TColStd_ListOfInteger.hxx>
51 #include <Standard_NullObject.hxx>
52 #include <Precision.hxx>
53 #include <gp_Pnt.hxx>
54
55 //=======================================================================
56 //function : GetID
57 //purpose  :
58 //=======================================================================
59 const Standard_GUID& GEOMImpl_PartitionDriver::GetID()
60 {
61   static Standard_GUID aPartitionDriver("FF1BBB22-5D14-4df2-980B-3A668264EA16");
62   return aPartitionDriver;
63 }
64
65
66 //=======================================================================
67 //function : GEOMImpl_PartitionDriver
68 //purpose  :
69 //=======================================================================
70 GEOMImpl_PartitionDriver::GEOMImpl_PartitionDriver()
71 {
72 }
73
74 //=======================================================================
75 //function : SimplifyCompound
76 //purpose  :
77 //=======================================================================
78 static void PrepareShapes (const TopoDS_Shape&   theShape,
79                            Standard_Integer      theType,
80                            TopTools_ListOfShape& theSimpleList)
81 {
82   if (theType == PARTITION_NO_SELF_INTERSECTIONS ||
83       theShape.ShapeType() != TopAbs_COMPOUND) {
84     theSimpleList.Append(theShape);
85     return;
86   }
87
88   // explode compound on simple shapes to allow their intersections
89   TopoDS_Iterator It (theShape, Standard_True, Standard_True);
90   TopTools_MapOfShape mapShape;
91   for (; It.More(); It.Next()) {
92     if (mapShape.Add(It.Value())) {
93       TopoDS_Shape curSh = It.Value();
94       PrepareShapes(curSh, theType, theSimpleList);
95     }
96   }
97 }
98
99 //=======================================================================
100 //function : Execute
101 //purpose  :
102 //=======================================================================
103 Standard_Integer GEOMImpl_PartitionDriver::Execute(TFunction_Logbook& log) const
104 {
105   if (Label().IsNull()) return 0;
106   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
107
108   GEOMImpl_IPartition aCI (aFunction);
109   Standard_Integer aType = aFunction->GetType();
110
111   TopoDS_Shape aShape;
112   //sklNMTAlgo_Splitter1 PS;
113   GEOMAlgo_Splitter PS;
114
115   if (aType == PARTITION_PARTITION || aType == PARTITION_NO_SELF_INTERSECTIONS)
116   {
117     Handle(TColStd_HSequenceOfTransient) aShapes  = aCI.GetShapes();
118     Handle(TColStd_HSequenceOfTransient) aTools   = aCI.GetTools();
119     Handle(TColStd_HSequenceOfTransient) aKeepIns = aCI.GetKeepIns();
120     Handle(TColStd_HSequenceOfTransient) aRemIns  = aCI.GetRemoveIns();
121     Handle(TColStd_HArray1OfInteger) aMaterials   = aCI.GetMaterials();
122     //skl Standard_Boolean DoRemoveWebs = !aMaterials.IsNull();
123
124     unsigned int ind;
125     //unsigned int ind, nbshapes = 0;
126     //nbshapes += aShapes->Length() + aTools->Length();
127     //nbshapes += aKeepIns->Length() + aRemIns->Length();
128     //TopTools_MapOfShape ShapesMap(nbshapes), ToolsMap(nbshapes);
129     TopTools_MapOfShape ShapesMap, ToolsMap;
130
131     // add object shapes that are in ListShapes;
132     for (ind = 1; ind <= aShapes->Length(); ind++) {
133       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
134       TopoDS_Shape aShape_i = aRefShape->GetValue();
135       if (aShape_i.IsNull()) {
136         Standard_NullObject::Raise("In Partition a shape is null");
137       }
138       //
139       TopTools_ListOfShape aSimpleShapes;
140       PrepareShapes(aShape_i, aType, aSimpleShapes);
141       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
142       for (; aSimpleIter.More(); aSimpleIter.Next()) {
143         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
144         if (ShapesMap.Add(aSimpleSh)) {
145           PS.AddShape(aSimpleSh);
146           //skl if (DoRemoveWebs) {
147           //skl if (aMaterials->Length() >= ind)
148           //skl PS.SetMaterial(aSimpleSh, aMaterials->Value(ind));
149           //skl }
150         }
151       }
152     }
153
154     // add tool shapes that are in ListTools and not in ListShapes;
155     for (ind = 1; ind <= aTools->Length(); ind++) {
156       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aTools->Value(ind));
157       TopoDS_Shape aShape_i = aRefShape->GetValue();
158       if (aShape_i.IsNull()) {
159         Standard_NullObject::Raise("In Partition a tool shape is null");
160       }
161       //
162       TopTools_ListOfShape aSimpleShapes;
163       PrepareShapes(aShape_i, aType, aSimpleShapes);
164       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
165       for (; aSimpleIter.More(); aSimpleIter.Next()) {
166         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
167         if (!ShapesMap.Contains(aSimpleSh) && ToolsMap.Add(aSimpleSh)) {
168           PS.AddTool(aSimpleSh);
169         }
170       }
171     }
172
173     // add shapes that are in ListKeepInside, as object shapes;
174     for (ind = 1; ind <= aKeepIns->Length(); ind++) {
175       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
176       TopoDS_Shape aShape_i = aRefShape->GetValue();
177       if (aShape_i.IsNull()) {
178         Standard_NullObject::Raise("In Partition a Keep Inside shape is null");
179       }
180       //
181       TopTools_ListOfShape aSimpleShapes;
182       PrepareShapes(aShape_i, aType, aSimpleShapes);
183       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
184       for (; aSimpleIter.More(); aSimpleIter.Next()) {
185         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
186         if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
187           PS.AddShape(aSimpleSh);
188       }
189     }
190
191     // add shapes that are in ListRemoveInside, as object shapes;
192     for (ind = 1; ind <= aRemIns->Length(); ind++) {
193       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
194       TopoDS_Shape aShape_i = aRefShape->GetValue();
195       if (aShape_i.IsNull()) {
196         Standard_NullObject::Raise("In Partition a Remove Inside shape is null");
197       }
198       //
199       TopTools_ListOfShape aSimpleShapes;
200       PrepareShapes(aShape_i, aType, aSimpleShapes);
201       TopTools_ListIteratorOfListOfShape aSimpleIter (aSimpleShapes);
202       for (; aSimpleIter.More(); aSimpleIter.Next()) {
203         const TopoDS_Shape& aSimpleSh = aSimpleIter.Value();
204         if (!ToolsMap.Contains(aSimpleSh) && ShapesMap.Add(aSimpleSh))
205           PS.AddShape(aSimpleSh);
206       }
207     }
208
209     PS.SetLimit( (TopAbs_ShapeEnum)aCI.GetLimit() );
210     PS.Perform();
211
212     //skl PS.Compute();
213     //skl PS.SetRemoveWebs(!DoRemoveWebs);
214     //skl PS.Build((TopAbs_ShapeEnum) aCI.GetLimit());
215     /*skl
216     // suppress result outside of shapes in KInsideMap
217     for (ind = 1; ind <= aKeepIns->Length(); ind++) {
218       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aKeepIns->Value(ind));
219       TopoDS_Shape aShape_i = aRefShape->GetValue();
220       PS.KeepShapesInside(aShape_i);
221     }
222
223     // suppress result inside of shapes in RInsideMap
224     for (ind = 1; ind <= aRemIns->Length(); ind++) {
225       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aRemIns->Value(ind));
226       TopoDS_Shape aShape_i = aRefShape->GetValue();
227       PS.RemoveShapesInside(aShape_i);
228     }
229     */
230   }
231   else if (aType == PARTITION_HALF)
232   {
233     Handle(GEOM_Function) aRefShape = aCI.GetShape();
234     Handle(GEOM_Function) aRefPlane = aCI.GetPlane();
235     TopoDS_Shape aShapeArg = aRefShape->GetValue();
236     TopoDS_Shape aPlaneArg = aRefPlane->GetValue();
237
238     if (aShapeArg.IsNull() || aPlaneArg.IsNull()) {
239       Standard_NullObject::Raise("In Half Partition a shape or a plane is null");
240     }
241
242     // add object shapes that are in ListShapes;
243     PS.AddShape(aShapeArg);
244
245     // add tool shapes that are in ListTools and not in ListShapes;
246     PS.AddTool(aPlaneArg);
247
248     //skl PS.Compute();
249     PS.Perform();
250     //PS.SetRemoveWebs(Standard_False);
251     //PS.Build(aShapeArg.ShapeType());
252
253   } else {
254   }
255
256   aShape = PS.Shape();
257   if (aShape.IsNull()) return 0;
258
259   if (!BRepAlgo::IsValid(aShape)) {
260     Standard_ConstructionError::Raise("Partition aborted : non valid shape result");
261   }
262
263   aFunction->SetValue(aShape);
264
265   // Fill history to be used by GetInPlace functionality
266   TopTools_IndexedMapOfShape aResIndices;
267   TopExp::MapShapes(aShape, aResIndices);
268
269   // Map: source_shape/images of source_shape in Result
270   const TopTools_IndexedDataMapOfShapeListOfShape& aMR = PS.ImagesResult();
271
272   // history for all argument shapes
273   TDF_LabelSequence aLabelSeq;
274   aFunction->GetDependency(aLabelSeq);
275   Standard_Integer nbArg = aLabelSeq.Length();
276
277   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
278
279     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
280
281     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
282     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
283
284     TopTools_IndexedMapOfShape anArgumentIndices;
285     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
286     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
287
288     // Find corresponding label in history
289     TDF_Label anArgumentHistoryLabel =
290       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
291
292     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
293       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
294       if (!aMR.Contains(anEntity)) continue;
295
296       const TopTools_ListOfShape& aModified = aMR.FindFromKey(anEntity);
297       Standard_Integer nbModified = aModified.Extent();
298
299       if (nbModified > 0) {
300         TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
301         Handle(TDataStd_IntegerArray) anAttr =
302           TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
303
304         int ih = 1;
305         TopTools_ListIteratorOfListOfShape itM (aModified);
306         for (; itM.More(); itM.Next(), ++ih) {
307           int id = aResIndices.FindIndex(itM.Value());
308           anAttr->SetValue(ih, id);
309         }
310       }
311     }
312   }
313
314   log.SetTouched(Label());
315
316   return 1;
317 }
318
319
320 //=======================================================================
321 //function :  GEOMImpl_PartitionDriver_Type_
322 //purpose  :
323 //=======================================================================
324 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PartitionDriver_Type_()
325 {
326
327   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
328   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
329   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
330   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
331   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
332   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
333
334
335   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
336   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PartitionDriver",
337                                                          sizeof(GEOMImpl_PartitionDriver),
338                                                          1,
339                                                          (Standard_Address)_Ancestors,
340                                                          (Standard_Address)NULL);
341
342   return _aType;
343 }
344
345 //=======================================================================
346 //function : DownCast
347 //purpose  :
348 //=======================================================================
349 const Handle(GEOMImpl_PartitionDriver) Handle(GEOMImpl_PartitionDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
350 {
351   Handle(GEOMImpl_PartitionDriver) _anOtherObject;
352
353   if (!AnObject.IsNull()) {
354      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PartitionDriver))) {
355        _anOtherObject = Handle(GEOMImpl_PartitionDriver)((Handle(GEOMImpl_PartitionDriver)&)AnObject);
356      }
357   }
358
359   return _anOtherObject ;
360 }