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