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