1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_GlueDriver.hxx>
24 #include <GEOMImpl_IGlue.hxx>
25 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Object.hxx>
28 #include <GEOM_Function.hxx>
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"
36 #include "utilities.h"
38 #include <TDataStd_IntegerArray.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <Standard_NullObject.hxx>
47 #include <Standard_Failure.hxx>
49 #define MSG_BAD_TOLERANCE "Tolerance is too big"
50 #define MSG_BAD_ARG_SHAPE "Argument shape is not a compound of hexahedral solids"
52 //=======================================================================
53 //function : GEOMImpl_GlueDriver
55 //=======================================================================
56 GEOMImpl_GlueDriver::GEOMImpl_GlueDriver()
60 //=======================================================================
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_GlueDriver::GetID()
66 static Standard_GUID aGlueDriver("FF1BBB63-5D14-4df2-980B-3A668264EA16");
70 //=======================================================================
71 //function : GlueFacesWithWarnings
73 //=======================================================================
74 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesWithWarnings (const TopoDS_Shape& theShape,
75 const Standard_Real theTolerance,
76 TCollection_AsciiString& theWarning) const
78 Standard_Integer iErr, iWrn;
80 GEOMAlgo_Gluer aGluer;
82 aGluer.SetShape(theShape);
83 aGluer.SetTolerance(theTolerance);
84 aGluer.SetCheckGeometry(Standard_True);
88 iErr = aGluer.ErrorStatus();
92 Standard_Failure::Raise("No vertices found in source shape");
96 Standard_Failure::Raise(MSG_BAD_TOLERANCE " or " MSG_BAD_ARG_SHAPE);
99 Standard_Failure::Raise("Source shape is Null");
102 Standard_Failure::Raise("Result shape is Null");
105 Standard_Failure::Raise(MSG_BAD_TOLERANCE);
109 Standard_Failure::Raise(MSG_BAD_ARG_SHAPE);
112 Standard_Failure::Raise("Error occured during check of geometric coincidence");
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());
126 iWrn = aGluer.WarningStatus();
131 Standard_Integer nbAlone = aGluer.AloneShapes();
132 theWarning = TCollection_AsciiString(nbAlone);
133 theWarning += " solid(s) can not be glued by faces";
137 // description of all warnings see in GEOMAlgo_Gluer.cxx
138 theWarning = "Warning in GEOMAlgo_Gluer with code ";
139 theWarning += TCollection_AsciiString(iWrn);
144 aRes = aGluer.Result();
146 // Fill history to be used by GetInPlace functionality
147 TopTools_IndexedMapOfShape aResIndices;
148 TopExp::MapShapes(aRes, aResIndices);
150 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
152 // history for all argument shapes
153 TDF_LabelSequence aLabelSeq;
154 aFunction->GetDependency(aLabelSeq);
155 Standard_Integer nbArg = aLabelSeq.Length();
157 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
159 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
161 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
162 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
164 TopTools_IndexedMapOfShape anArgumentIndices;
165 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
166 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
168 // Find corresponding label in history
169 TDF_Label anArgumentHistoryLabel =
170 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
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();
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);
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);
195 //=======================================================================
196 //function : GlueFaces
198 //=======================================================================
199 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
200 const Standard_Real theTolerance)
202 Standard_Integer iErr, iWrn;
204 GEOMAlgo_Gluer aGluer;
206 aGluer.SetShape(theShape);
207 aGluer.SetTolerance(theTolerance);
208 aGluer.SetCheckGeometry(Standard_True);
212 iErr = aGluer.ErrorStatus();
216 Standard_Failure::Raise("No vertices found in source shape");
219 Standard_Failure::Raise("Source shape is Null");
222 Standard_Failure::Raise("Result shape is Null");
225 Standard_Failure::Raise("Error occured during check of geometric coincidence");
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());
239 iWrn = aGluer.WarningStatus();
243 MESSAGE("Some shapes can not be glued by faces");
246 // description of all warnings see in GEOMAlgo_Gluer.cxx
247 MESSAGE("Warning in GEOMAlgo_Gluer with code " << iWrn);
252 aRes = aGluer.Result();
258 //=======================================================================
259 //function : GlueFacesByList
261 //=======================================================================
262 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesByList (const TopoDS_Shape& theShape,
263 const Standard_Real theTolerance,
264 const TopTools_MapOfShape& aFaces)
268 GEOMAlgo_Gluer1 aGluer;
269 GEOMAlgo_ListIteratorOfListOfCoupleOfShapes aItCS;
270 GEOMAlgo_CoupleOfShapes aCS;
271 GEOMAlgo_ListOfCoupleOfShapes aLCS;
273 aGluer.SetShape(theShape);
274 aGluer.SetTolerance(theTolerance);
276 Standard_Integer iErr = aGluer.ErrorStatus();
277 if (iErr) return aRes;
279 TopTools_ListOfShape listShape;
280 const GEOMAlgo_ListOfCoupleOfShapes& aLCSG = aGluer.GluedFaces();
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) )
289 aCS.SetShapes(aF1,aF2);
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;
301 aRes = aGluer.Result();
307 //=======================================================================
310 //=======================================================================
311 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
313 if (Label().IsNull()) return 0;
314 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
316 GEOMImpl_IGlue aCI (aFunction);
317 Standard_Integer aType = aFunction->GetType();
320 TCollection_AsciiString aWrn;
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");
328 Standard_Real tol3d = aCI.GetTolerance();
330 if (aType == GLUE_FACES) {
331 aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aWrn);
333 else { // aType == GLUE_FACES_BY_LIST
334 Handle(TColStd_HSequenceOfTransient) SF = aCI.GetFaces();
335 TopTools_MapOfShape aFaces;
337 for( ; i <= SF->Length(); i++) {
338 Handle(Standard_Transient) anItem = SF->Value(i);
341 Handle(GEOM_Function) aRefSh = Handle(GEOM_Function)::DownCast(anItem);
344 TopoDS_Shape aFace = aRefSh->GetValue();
347 if(!aFaces.Contains(aFace))
350 aShape = GlueFacesByList(aShapeBase, tol3d, aFaces);
353 if (aShape.IsNull()) return 0;
355 aFunction->SetValue(aShape);
357 log.SetTouched(Label());
359 if (!aWrn.IsEmpty()) {
360 Standard_Failure::Raise(aWrn.ToCString());
366 //=======================================================================
367 //function : GEOMImpl_GlueDriver_Type_
369 //=======================================================================
370 Standard_EXPORT Handle_Standard_Type& GEOMImpl_GlueDriver_Type_()
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);
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),
385 (Standard_Address)_Ancestors,
386 (Standard_Address)NULL);
391 //=======================================================================
392 //function : DownCast
394 //=======================================================================
395 const Handle(GEOMImpl_GlueDriver) Handle(GEOMImpl_GlueDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
397 Handle(GEOMImpl_GlueDriver) _anOtherObject;
399 if (!AnObject.IsNull()) {
400 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_GlueDriver))) {
401 _anOtherObject = Handle(GEOMImpl_GlueDriver)((Handle(GEOMImpl_GlueDriver)&)AnObject);
405 return _anOtherObject ;