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