Salome HOME
Bug 0020057: EDF GEOM: Impossible to explode an object to faces. Infinite loop.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_GlueDriver.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_GlueDriver.hxx>
24 #include <GEOMImpl_IGlue.hxx>
25 #include <GEOMImpl_Types.hxx>
26
27 #include <GEOM_Object.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <GEOMAlgo_Gluer.hxx>
31 #include "GEOMAlgo_Gluer1.hxx"
32 #include "GEOMAlgo_ListIteratorOfListOfCoupleOfShapes.hxx"
33 #include "GEOMAlgo_CoupleOfShapes.hxx"
34 #include "GEOMAlgo_ListOfCoupleOfShapes.hxx"
35
36 #include "utilities.h"
37
38 #include <TDataStd_IntegerArray.hxx>
39
40 #include <TopExp.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45
46 #include <Standard_NullObject.hxx>
47 #include <Standard_Failure.hxx>
48
49 #define MSG_BAD_TOLERANCE "Tolerance is too big"
50 #define MSG_BAD_ARG_SHAPE "Argument shape is not a compound of hexahedral solids"
51
52 //=======================================================================
53 //function : GEOMImpl_GlueDriver
54 //purpose  :
55 //=======================================================================
56 GEOMImpl_GlueDriver::GEOMImpl_GlueDriver()
57 {
58 }
59
60 //=======================================================================
61 //function : GetID
62 //purpose  :
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_GlueDriver::GetID()
65 {
66   static Standard_GUID aGlueDriver("FF1BBB63-5D14-4df2-980B-3A668264EA16");
67   return aGlueDriver;
68 }
69
70 //=======================================================================
71 //function : GlueFacesWithWarnings
72 //purpose  :
73 //=======================================================================
74 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesWithWarnings (const TopoDS_Shape& theShape,
75                                                          const Standard_Real theTolerance,
76                                                          const Standard_Boolean doKeepNonSolids,
77                                                          TCollection_AsciiString& theWarning) const
78 {
79   Standard_Integer iErr, iWrn;
80   TopoDS_Shape aRes;
81   GEOMAlgo_Gluer aGluer;
82
83   aGluer.SetShape(theShape);
84   aGluer.SetTolerance(theTolerance);
85   aGluer.SetCheckGeometry(Standard_True);
86   aGluer.SetKeepNonSolids(doKeepNonSolids);
87
88   aGluer.Perform();
89
90   iErr = aGluer.ErrorStatus();
91   if (iErr) {
92     switch (iErr) {
93     case 2:
94       Standard_Failure::Raise("No vertices found in source shape");
95       break;
96     case 3:
97     case 4:
98       Standard_Failure::Raise(MSG_BAD_TOLERANCE " or " MSG_BAD_ARG_SHAPE);
99       break;
100     case 5:
101       Standard_Failure::Raise("Source shape is Null");
102       break;
103     case 6:
104       Standard_Failure::Raise("Result shape is Null");
105       break;
106     case 100:
107       Standard_Failure::Raise(MSG_BAD_TOLERANCE);
108       break;
109     case 101:
110     case 102:
111       Standard_Failure::Raise(MSG_BAD_ARG_SHAPE);
112       break;
113     case 200:
114       Standard_Failure::Raise("Error occured during check of geometric coincidence");
115       break;
116     default:
117       {
118         // description of all errors see in GEOMAlgo_Gluer.cxx
119         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
120         aMsg += TCollection_AsciiString(iErr);
121         Standard_Failure::Raise(aMsg.ToCString());
122         break;
123       }
124     }
125     return aRes;
126   }
127
128   iWrn = aGluer.WarningStatus();
129   if (iWrn) {
130     switch (iWrn) {
131     case 1:
132       {
133         Standard_Integer nbAlone = aGluer.AloneShapes();
134         theWarning = TCollection_AsciiString(nbAlone);
135         theWarning += " solid(s) can not be glued by faces";
136       }
137       break;
138     default:
139       // description of all warnings see in GEOMAlgo_Gluer.cxx
140       theWarning = "Warning in GEOMAlgo_Gluer with code ";
141       theWarning += TCollection_AsciiString(iWrn);
142       break;
143     }
144   }
145
146   aRes = aGluer.Result();
147
148   // Fill history to be used by GetInPlace functionality
149   TopTools_IndexedMapOfShape aResIndices;
150   TopExp::MapShapes(aRes, aResIndices);
151
152   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
153
154   // history for all argument shapes
155   TDF_LabelSequence aLabelSeq;
156   aFunction->GetDependency(aLabelSeq);
157   Standard_Integer nbArg = aLabelSeq.Length();
158
159   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
160
161     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
162
163     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
164     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
165
166     TopTools_IndexedMapOfShape anArgumentIndices;
167     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
168     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
169
170     // Find corresponding label in history
171     TDF_Label anArgumentHistoryLabel =
172       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
173
174     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
175       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
176       const TopTools_ListOfShape& aModified = aGluer.Modified(anEntity);
177       Standard_Integer nbModified = aModified.Extent();
178
179       if (nbModified > 0) {
180         TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
181         Handle(TDataStd_IntegerArray) anAttr =
182           TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
183
184         TopTools_ListIteratorOfListOfShape itM (aModified);
185         for (int im = 1; itM.More(); itM.Next(), ++im) {
186           int id = aResIndices.FindIndex(itM.Value());
187           anAttr->SetValue(im, id);
188         }
189       }
190     }
191   }
192
193   return aRes;
194 }
195
196
197 //=======================================================================
198 //function : GlueFaces
199 //purpose  :
200 //=======================================================================
201 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
202                                              const Standard_Real theTolerance,
203                                              const Standard_Boolean doKeepNonSolids)
204 {
205   Standard_Integer iErr, iWrn;
206   TopoDS_Shape aRes;
207   GEOMAlgo_Gluer aGluer;
208
209   aGluer.SetShape(theShape);
210   aGluer.SetTolerance(theTolerance);
211   aGluer.SetCheckGeometry(Standard_True);
212   aGluer.SetKeepNonSolids(doKeepNonSolids);
213
214   aGluer.Perform();
215
216   iErr = aGluer.ErrorStatus();
217   if (iErr) {
218     switch (iErr) {
219     case 2:
220       Standard_Failure::Raise("No vertices found in source shape");
221       break;
222     case 5:
223       Standard_Failure::Raise("Source shape is Null");
224       break;
225     case 6:
226       Standard_Failure::Raise("Result shape is Null");
227       break;
228     case 200:
229       Standard_Failure::Raise("Error occured during check of geometric coincidence");
230       break;
231     default:
232       {
233         // description of all errors see in GEOMAlgo_Gluer.cxx
234         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
235         aMsg += TCollection_AsciiString(iErr);
236         Standard_Failure::Raise(aMsg.ToCString());
237         break;
238       }
239     }
240     return aRes;
241   }
242
243   iWrn = aGluer.WarningStatus();
244   if (iWrn) {
245     switch (iWrn) {
246     case 1:
247       MESSAGE("Some shapes can not be glued by faces");
248       break;
249     default:
250       // description of all warnings see in GEOMAlgo_Gluer.cxx
251       MESSAGE("Warning in GEOMAlgo_Gluer with code " << iWrn);
252       break;
253     }
254   }
255
256   aRes = aGluer.Result();
257
258   return aRes;
259 }
260
261
262 //=======================================================================
263 //function : GlueFacesByList
264 //purpose  :
265 //=======================================================================
266 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesByList (const TopoDS_Shape& theShape,
267                                                    const Standard_Real theTolerance,
268                                                    const Standard_Boolean doKeepNonSolids,
269                                                    const TopTools_MapOfShape& aFaces)
270 {
271   TopoDS_Shape aRes;
272
273   GEOMAlgo_Gluer1 aGluer;
274   GEOMAlgo_ListIteratorOfListOfCoupleOfShapes aItCS;
275   GEOMAlgo_CoupleOfShapes aCS;
276   GEOMAlgo_ListOfCoupleOfShapes aLCS;
277
278   aGluer.SetShape(theShape);
279   aGluer.SetTolerance(theTolerance);
280   aGluer.SetKeepNonSolids(doKeepNonSolids);
281   aGluer.Perform();
282   Standard_Integer iErr = aGluer.ErrorStatus();
283   if (iErr) return aRes;
284
285   TopTools_ListOfShape listShape;
286   const GEOMAlgo_ListOfCoupleOfShapes& aLCSG = aGluer.GluedFaces();
287   // Access to faces
288   aItCS.Initialize(aLCSG);
289   for (; aItCS.More(); aItCS.Next()) {
290     const GEOMAlgo_CoupleOfShapes& aCSG = aItCS.Value();
291     const TopoDS_Shape& aF1 = aCSG.Shape1();
292     const TopoDS_Shape& aF2 = aCSG.Shape2();
293     if( aFaces.Contains(aF1) || aFaces.Contains(aF2) )
294       continue;
295     aCS.SetShapes(aF1,aF2);
296     aLCS.Append(aCS);
297   }
298
299   //cout<<"aLCS.Extent() = "<<aLCS.Extent()<<endl;
300   if(aLCS.Extent()>0) {
301     aGluer.SetFacesToUnglue(aLCS);
302     aGluer.UnglueFaces();
303     iErr = aGluer.ErrorStatus();
304     if (iErr) return aRes;
305   }
306
307   aRes = aGluer.Result();
308
309   return aRes;
310 }
311
312
313 //=======================================================================
314 //function : Execute
315 //purpose  :
316 //=======================================================================
317 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
318 {
319   if (Label().IsNull()) return 0;
320   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
321
322   GEOMImpl_IGlue aCI (aFunction);
323   Standard_Integer aType = aFunction->GetType();
324
325   TopoDS_Shape aShape;
326   TCollection_AsciiString aWrn;
327
328   Handle(GEOM_Function) aRefBase = aCI.GetBase();
329   TopoDS_Shape aShapeBase = aRefBase->GetValue();
330   if (aShapeBase.IsNull()) {
331     Standard_NullObject::Raise("Shape for gluing is null");
332   }
333
334   Standard_Real tol3d = aCI.GetTolerance();
335
336   Standard_Boolean aKeepNonSolids = aCI.GetKeepNonSolids();
337
338   if (aType == GLUE_FACES) {
339     aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aKeepNonSolids, aWrn);
340   }
341   else { // aType == GLUE_FACES_BY_LIST
342     Handle(TColStd_HSequenceOfTransient) SF = aCI.GetFaces();
343     TopTools_MapOfShape aFaces;
344     int i=1;
345     for( ; i <= SF->Length(); i++) {
346       Handle(Standard_Transient) anItem = SF->Value(i);
347       if(anItem.IsNull())
348         continue;
349       Handle(GEOM_Function) aRefSh = Handle(GEOM_Function)::DownCast(anItem);
350       if(aRefSh.IsNull())
351         continue;
352       TopoDS_Shape aFace = aRefSh->GetValue();
353       if(aFace.IsNull())
354         continue;
355       if(!aFaces.Contains(aFace))
356         aFaces.Add(aFace);
357     }
358     aShape = GlueFacesByList(aShapeBase, tol3d, aKeepNonSolids, aFaces);
359   }
360
361   if (aShape.IsNull()) return 0;
362
363   aFunction->SetValue(aShape);
364
365   log.SetTouched(Label());
366
367   if (!aWrn.IsEmpty()) {
368     Standard_Failure::Raise(aWrn.ToCString());
369   }
370
371   return 1;
372 }
373
374 //=======================================================================
375 //function :  GEOMImpl_GlueDriver_Type_
376 //purpose  :
377 //=======================================================================
378 Standard_EXPORT Handle_Standard_Type& GEOMImpl_GlueDriver_Type_()
379 {
380
381   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
382   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
383   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
384   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
385   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
386   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
387
388
389   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
390   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_GlueDriver",
391                                                          sizeof(GEOMImpl_GlueDriver),
392                                                          1,
393                                                          (Standard_Address)_Ancestors,
394                                                          (Standard_Address)NULL);
395
396   return _aType;
397 }
398
399 //=======================================================================
400 //function : DownCast
401 //purpose  :
402 //=======================================================================
403 const Handle(GEOMImpl_GlueDriver) Handle(GEOMImpl_GlueDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
404 {
405   Handle(GEOMImpl_GlueDriver) _anOtherObject;
406
407   if (!AnObject.IsNull()) {
408      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_GlueDriver))) {
409        _anOtherObject = Handle(GEOMImpl_GlueDriver)((Handle(GEOMImpl_GlueDriver)&)AnObject);
410      }
411   }
412
413   return _anOtherObject ;
414 }