Salome HOME
PAL7924: Meaningfull error message provided
[modules/geom.git] / src / GEOMImpl / GEOMImpl_GlueDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_GlueDriver.hxx"
4 #include "GEOMImpl_IGlue.hxx"
5 #include "GEOMImpl_Types.hxx"
6
7 #include "GEOM_Object.hxx"
8 #include "GEOM_Function.hxx"
9
10 #include "GEOMAlgo_Gluer.hxx"
11
12 #include "utilities.h"
13
14 #include <TDataStd_IntegerArray.hxx>
15
16 #include <TopExp.hxx>
17 #include <TopoDS_Shape.hxx>
18 #include <TopTools_ListOfShape.hxx>
19 #include <TopTools_IndexedMapOfShape.hxx>
20 #include <TopTools_ListIteratorOfListOfShape.hxx>
21
22 #include <Standard_NullObject.hxx>
23 #include <Standard_Failure.hxx>
24
25 //=======================================================================
26 //function : GEOMImpl_GlueDriver
27 //purpose  :
28 //=======================================================================
29 GEOMImpl_GlueDriver::GEOMImpl_GlueDriver()
30 {
31 }
32
33 //=======================================================================
34 //function : GetID
35 //purpose  :
36 //=======================================================================
37 const Standard_GUID& GEOMImpl_GlueDriver::GetID()
38 {
39   static Standard_GUID aGlueDriver("FF1BBB63-5D14-4df2-980B-3A668264EA16");
40   return aGlueDriver;
41 }
42
43 //=======================================================================
44 //function : GlueFacesWithWarnings
45 //purpose  :
46 //=======================================================================
47 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesWithWarnings (const TopoDS_Shape& theShape,
48                                                          const Standard_Real theTolerance,
49                                                          TCollection_AsciiString& theWarning) const
50 {
51   Standard_Integer iErr, iWrn;
52   TopoDS_Shape aRes;
53   GEOMAlgo_Gluer aGluer;
54
55   aGluer.SetShape(theShape);
56   aGluer.SetTolerance(theTolerance);
57   aGluer.SetCheckGeometry(Standard_True);
58
59   aGluer.Perform();
60
61   iErr = aGluer.ErrorStatus();
62   if (iErr) {
63     switch (iErr) {
64     case 2:
65       Standard_Failure::Raise("No vertices found in source shape");
66       break;
67     case 5:
68       Standard_Failure::Raise("Source shape is Null");
69       break;
70     case 6:
71       Standard_Failure::Raise("Result shape is Null");
72       break;
73     case 101:
74       Standard_Failure::Raise("Argument shape is not a compound of hexahedral solids");
75       break;
76     case 200:
77       Standard_Failure::Raise("Error occured during check of geometric coincidence");
78       break;
79     default:
80       {
81         // description of all errors see in GEOMAlgo_Gluer.cxx
82         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
83         aMsg += TCollection_AsciiString(iErr);
84         Standard_Failure::Raise(aMsg.ToCString());
85         break;
86       }
87     }
88     return aRes;
89   }
90
91   iWrn = aGluer.WarningStatus();
92   if (iWrn) {
93     switch (iWrn) {
94     case 1:
95       {
96         Standard_Integer nbAlone = aGluer.AloneShapes();
97         theWarning = TCollection_AsciiString(nbAlone);
98         theWarning += " solid(s) can not be glued by faces";
99       }
100       break;
101     default:
102       // description of all warnings see in GEOMAlgo_Gluer.cxx
103       theWarning = "Warning in GEOMAlgo_Gluer with code ";
104       theWarning += TCollection_AsciiString(iWrn);
105       break;
106     }
107   }
108
109   aRes = aGluer.Result();
110
111   // Fill history to be used by GetInPlace functionality
112   TopTools_IndexedMapOfShape aResIndices;
113   TopExp::MapShapes(aRes, aResIndices);
114
115   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
116
117   // history for all argument shapes
118   TDF_LabelSequence aLabelSeq;
119   aFunction->GetDependency(aLabelSeq);
120   Standard_Integer nbArg = aLabelSeq.Length();
121
122   for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
123
124     TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
125
126     Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
127     TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
128
129     TopTools_IndexedMapOfShape anArgumentIndices;
130     TopExp::MapShapes(anArgumentShape, anArgumentIndices);
131     Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
132
133     // Find corresponding label in history
134     TDF_Label anArgumentHistoryLabel =
135       aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
136
137     for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
138       TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
139       const TopTools_ListOfShape& aModified = aGluer.Modified(anEntity);
140       Standard_Integer nbModified = aModified.Extent();
141
142       if (nbModified > 0) {
143         TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
144         Handle(TDataStd_IntegerArray) anAttr =
145           TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
146
147         TopTools_ListIteratorOfListOfShape itM (aModified);
148         for (int im = 1; itM.More(); itM.Next(), ++im) {
149           int id = aResIndices.FindIndex(itM.Value());
150           anAttr->SetValue(im, id);
151         }
152       }
153     }
154   }
155
156   return aRes;
157 }
158
159 //=======================================================================
160 //function : GlueFaces
161 //purpose  :
162 //=======================================================================
163 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
164                                              const Standard_Real theTolerance)
165 {
166   Standard_Integer iErr, iWrn;
167   TopoDS_Shape aRes;
168   GEOMAlgo_Gluer aGluer;
169
170   aGluer.SetShape(theShape);
171   aGluer.SetTolerance(theTolerance);
172   aGluer.SetCheckGeometry(Standard_True);
173
174   aGluer.Perform();
175
176   iErr = aGluer.ErrorStatus();
177   if (iErr) {
178     switch (iErr) {
179     case 2:
180       Standard_Failure::Raise("No vertices found in source shape");
181       break;
182     case 5:
183       Standard_Failure::Raise("Source shape is Null");
184       break;
185     case 6:
186       Standard_Failure::Raise("Result shape is Null");
187       break;
188     case 200:
189       Standard_Failure::Raise("Error occured during check of geometric coincidence");
190       break;
191     default:
192       {
193         // description of all errors see in GEOMAlgo_Gluer.cxx
194         TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
195         aMsg += TCollection_AsciiString(iErr);
196         Standard_Failure::Raise(aMsg.ToCString());
197         break;
198       }
199     }
200     return aRes;
201   }
202
203   iWrn = aGluer.WarningStatus();
204   if (iWrn) {
205     switch (iWrn) {
206     case 1:
207       MESSAGE("Some shapes can not be glued by faces");
208       break;
209     default:
210       // description of all warnings see in GEOMAlgo_Gluer.cxx
211       MESSAGE("Warning in GEOMAlgo_Gluer with code " << iWrn);
212       break;
213     }
214   }
215
216   aRes = aGluer.Result();
217
218   return aRes;
219 }
220
221 //=======================================================================
222 //function : Execute
223 //purpose  :
224 //=======================================================================
225 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
226 {
227   if (Label().IsNull()) return 0;
228   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
229
230   GEOMImpl_IGlue aCI (aFunction);
231   Standard_Integer aType = aFunction->GetType();
232
233   TopoDS_Shape aShape;
234   TCollection_AsciiString aWrn;
235
236   if (aType == GLUE_FACES) {
237     Handle(GEOM_Function) aRefBase = aCI.GetBase();
238     TopoDS_Shape aShapeBase = aRefBase->GetValue();
239     if (aShapeBase.IsNull()) {
240       Standard_NullObject::Raise("Shape for gluing is null");
241     }
242
243     Standard_Real tol3d = aCI.GetTolerance();
244     aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aWrn);
245   } else {
246   }
247
248   if (aShape.IsNull()) return 0;
249
250   aFunction->SetValue(aShape);
251
252   log.SetTouched(Label());
253
254   if (!aWrn.IsEmpty()) {
255     Standard_Failure::Raise(aWrn.ToCString());
256   }
257
258   return 1;
259 }
260
261 //=======================================================================
262 //function :  GEOMImpl_GlueDriver_Type_
263 //purpose  :
264 //=======================================================================
265 Standard_EXPORT Handle_Standard_Type& GEOMImpl_GlueDriver_Type_()
266 {
267
268   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
269   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
270   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
271   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
272   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
273   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
274
275
276   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
277   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_GlueDriver",
278                                                          sizeof(GEOMImpl_GlueDriver),
279                                                          1,
280                                                          (Standard_Address)_Ancestors,
281                                                          (Standard_Address)NULL);
282
283   return _aType;
284 }
285
286 //=======================================================================
287 //function : DownCast
288 //purpose  :
289 //=======================================================================
290 const Handle(GEOMImpl_GlueDriver) Handle(GEOMImpl_GlueDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
291 {
292   Handle(GEOMImpl_GlueDriver) _anOtherObject;
293
294   if (!AnObject.IsNull()) {
295      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_GlueDriver))) {
296        _anOtherObject = Handle(GEOMImpl_GlueDriver)((Handle(GEOMImpl_GlueDriver)&)AnObject);
297      }
298   }
299
300   return _anOtherObject ;
301 }