1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_GlueDriver.hxx>
26 #include <GEOMImpl_IGlue.hxx>
27 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Object.hxx>
30 #include <GEOM_Function.hxx>
32 #include <GEOMAlgo_Gluer.hxx>
33 #include "GEOMAlgo_Gluer1.hxx"
34 #include "GEOMAlgo_ListIteratorOfListOfCoupleOfShapes.hxx"
35 #include "GEOMAlgo_CoupleOfShapes.hxx"
36 #include "GEOMAlgo_ListOfCoupleOfShapes.hxx"
38 #include "utilities.h"
40 #include <TDataStd_IntegerArray.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopTools_ListOfShape.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
48 #include <ShapeFix_Shape.hxx>
50 #include <Standard_NullObject.hxx>
51 #include <Standard_Failure.hxx>
53 #define MSG_BAD_TOLERANCE "Tolerance is too big"
54 #define MSG_BAD_ARG_SHAPE "Argument shape is not a compound of hexahedral solids"
56 //=======================================================================
57 //function : GEOMImpl_GlueDriver
59 //=======================================================================
60 GEOMImpl_GlueDriver::GEOMImpl_GlueDriver()
64 //=======================================================================
67 //=======================================================================
68 const Standard_GUID& GEOMImpl_GlueDriver::GetID()
70 static Standard_GUID aGlueDriver("FF1BBB63-5D14-4df2-980B-3A668264EA16");
74 //=======================================================================
75 //function : GlueFacesWithWarnings
77 //=======================================================================
78 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesWithWarnings (const TopoDS_Shape& theShape,
79 const Standard_Real theTolerance,
80 const Standard_Boolean doKeepNonSolids,
81 TCollection_AsciiString& theWarning) const
83 Standard_Integer iErr, iWrn;
85 GEOMAlgo_Gluer aGluer;
87 aGluer.SetShape(theShape);
88 aGluer.SetTolerance(theTolerance);
89 aGluer.SetCheckGeometry(Standard_True);
90 aGluer.SetKeepNonSolids(doKeepNonSolids);
94 iErr = aGluer.ErrorStatus();
98 Standard_Failure::Raise("No vertices found in source shape");
102 Standard_Failure::Raise(MSG_BAD_TOLERANCE " or " MSG_BAD_ARG_SHAPE);
105 Standard_Failure::Raise("Source shape is Null");
108 Standard_Failure::Raise("Result shape is Null");
111 Standard_Failure::Raise(MSG_BAD_TOLERANCE);
115 Standard_Failure::Raise(MSG_BAD_ARG_SHAPE);
118 Standard_Failure::Raise("Error occured during check of geometric coincidence");
122 // description of all errors see in GEOMAlgo_Gluer.cxx
123 TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
124 aMsg += TCollection_AsciiString(iErr);
125 Standard_Failure::Raise(aMsg.ToCString());
132 iWrn = aGluer.WarningStatus();
137 Standard_Integer nbAlone = aGluer.AloneShapes();
138 theWarning = TCollection_AsciiString(nbAlone);
139 theWarning += " solid(s) can not be glued by faces";
143 // description of all warnings see in GEOMAlgo_Gluer.cxx
144 theWarning = "Warning in GEOMAlgo_Gluer with code ";
145 theWarning += TCollection_AsciiString(iWrn);
150 aRes = aGluer.Result();
152 // SKL 18.01.2010 - patch for 20662
153 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aRes);
154 aSfs->SetPrecision(Precision::Confusion());
156 aRes = aSfs->Shape();
159 // Fill history to be used by GetInPlace functionality
160 TopTools_IndexedMapOfShape aResIndices;
161 TopExp::MapShapes(aRes, aResIndices);
163 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
165 // history for all argument shapes
166 TDF_LabelSequence aLabelSeq;
167 aFunction->GetDependency(aLabelSeq);
168 Standard_Integer nbArg = aLabelSeq.Length();
170 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
172 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
174 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
175 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
177 TopTools_IndexedMapOfShape anArgumentIndices;
178 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
179 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
181 // Find corresponding label in history
182 TDF_Label anArgumentHistoryLabel =
183 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
185 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
186 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
187 const TopTools_ListOfShape& aModified = aGluer.Modified(anEntity);
188 Standard_Integer nbModified = aModified.Extent();
190 if (nbModified > 0) {
191 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
192 Handle(TDataStd_IntegerArray) anAttr =
193 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
195 TopTools_ListIteratorOfListOfShape itM (aModified);
196 for (int im = 1; itM.More(); itM.Next(), ++im) {
197 int id = aResIndices.FindIndex(itM.Value());
198 anAttr->SetValue(im, id);
208 //=======================================================================
209 //function : GlueFaces
211 //=======================================================================
212 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
213 const Standard_Real theTolerance,
214 const Standard_Boolean doKeepNonSolids)
216 Standard_Integer iErr, iWrn;
218 GEOMAlgo_Gluer aGluer;
220 aGluer.SetShape(theShape);
221 aGluer.SetTolerance(theTolerance);
222 aGluer.SetCheckGeometry(Standard_True);
223 aGluer.SetKeepNonSolids(doKeepNonSolids);
227 iErr = aGluer.ErrorStatus();
231 Standard_Failure::Raise("No vertices found in source shape");
234 Standard_Failure::Raise("Source shape is Null");
237 Standard_Failure::Raise("Result shape is Null");
240 Standard_Failure::Raise("Error occured during check of geometric coincidence");
244 // description of all errors see in GEOMAlgo_Gluer.cxx
245 TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
246 aMsg += TCollection_AsciiString(iErr);
247 Standard_Failure::Raise(aMsg.ToCString());
254 iWrn = aGluer.WarningStatus();
258 MESSAGE("Some shapes can not be glued by faces");
261 // description of all warnings see in GEOMAlgo_Gluer.cxx
262 MESSAGE("Warning in GEOMAlgo_Gluer with code " << iWrn);
267 aRes = aGluer.Result();
273 //=======================================================================
274 //function : GlueFacesByList
276 //=======================================================================
277 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesByList (const TopoDS_Shape& theShape,
278 const Standard_Real theTolerance,
279 const Standard_Boolean doKeepNonSolids,
280 const TopTools_MapOfShape& aFaces)
284 GEOMAlgo_Gluer1 aGluer;
285 GEOMAlgo_ListIteratorOfListOfCoupleOfShapes aItCS;
286 GEOMAlgo_CoupleOfShapes aCS;
287 GEOMAlgo_ListOfCoupleOfShapes aLCS;
289 aGluer.SetShape(theShape);
290 aGluer.SetTolerance(theTolerance);
291 aGluer.SetKeepNonSolids(doKeepNonSolids);
293 Standard_Integer iErr = aGluer.ErrorStatus();
294 if (iErr) return aRes;
296 TopTools_ListOfShape listShape;
297 const GEOMAlgo_ListOfCoupleOfShapes& aLCSG = aGluer.GluedFaces();
299 aItCS.Initialize(aLCSG);
300 for (; aItCS.More(); aItCS.Next()) {
301 const GEOMAlgo_CoupleOfShapes& aCSG = aItCS.Value();
302 const TopoDS_Shape& aF1 = aCSG.Shape1();
303 const TopoDS_Shape& aF2 = aCSG.Shape2();
304 if( aFaces.Contains(aF1) || aFaces.Contains(aF2) )
306 aCS.SetShapes(aF1,aF2);
310 //cout<<"aLCS.Extent() = "<<aLCS.Extent()<<endl;
311 if(aLCS.Extent()>0) {
312 aGluer.SetFacesToUnglue(aLCS);
313 aGluer.UnglueFaces();
314 iErr = aGluer.ErrorStatus();
315 if (iErr) return aRes;
318 aRes = aGluer.Result();
324 //=======================================================================
327 //=======================================================================
328 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
330 if (Label().IsNull()) return 0;
331 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
333 GEOMImpl_IGlue aCI (aFunction);
334 Standard_Integer aType = aFunction->GetType();
337 TCollection_AsciiString aWrn;
339 Handle(GEOM_Function) aRefBase = aCI.GetBase();
340 TopoDS_Shape aShapeBase = aRefBase->GetValue();
341 if (aShapeBase.IsNull()) {
342 Standard_NullObject::Raise("Shape for gluing is null");
345 Standard_Real tol3d = aCI.GetTolerance();
347 Standard_Boolean aKeepNonSolids = aCI.GetKeepNonSolids();
349 if (aType == GLUE_FACES) {
350 aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aKeepNonSolids, aWrn);
352 else { // aType == GLUE_FACES_BY_LIST
353 Handle(TColStd_HSequenceOfTransient) SF = aCI.GetFaces();
354 TopTools_MapOfShape aFaces;
356 for( ; i <= SF->Length(); i++) {
357 Handle(Standard_Transient) anItem = SF->Value(i);
360 Handle(GEOM_Function) aRefSh = Handle(GEOM_Function)::DownCast(anItem);
363 TopoDS_Shape aFace = aRefSh->GetValue();
366 if(!aFaces.Contains(aFace))
369 aShape = GlueFacesByList(aShapeBase, tol3d, aKeepNonSolids, aFaces);
372 if (aShape.IsNull()) return 0;
374 aFunction->SetValue(aShape);
376 log.SetTouched(Label());
378 if (!aWrn.IsEmpty()) {
379 Standard_Failure::Raise(aWrn.ToCString());
385 //=======================================================================
386 //function : GEOMImpl_GlueDriver_Type_
388 //=======================================================================
389 Standard_EXPORT Handle_Standard_Type& GEOMImpl_GlueDriver_Type_()
392 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
393 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
394 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
395 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
396 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
397 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
400 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
401 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_GlueDriver",
402 sizeof(GEOMImpl_GlueDriver),
404 (Standard_Address)_Ancestors,
405 (Standard_Address)NULL);
410 //=======================================================================
411 //function : DownCast
413 //=======================================================================
414 const Handle(GEOMImpl_GlueDriver) Handle(GEOMImpl_GlueDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
416 Handle(GEOMImpl_GlueDriver) _anOtherObject;
418 if (!AnObject.IsNull()) {
419 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_GlueDriver))) {
420 _anOtherObject = Handle(GEOMImpl_GlueDriver)((Handle(GEOMImpl_GlueDriver)&)AnObject);
424 return _anOtherObject ;