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 const Standard_Boolean doKeepNonSolids,
77 TCollection_AsciiString& theWarning) const
79 Standard_Integer iErr, iWrn;
81 GEOMAlgo_Gluer aGluer;
83 aGluer.SetShape(theShape);
84 aGluer.SetTolerance(theTolerance);
85 aGluer.SetCheckGeometry(Standard_True);
86 aGluer.SetKeepNonSolids(doKeepNonSolids);
90 iErr = aGluer.ErrorStatus();
94 Standard_Failure::Raise("No vertices found in source shape");
98 Standard_Failure::Raise(MSG_BAD_TOLERANCE " or " MSG_BAD_ARG_SHAPE);
101 Standard_Failure::Raise("Source shape is Null");
104 Standard_Failure::Raise("Result shape is Null");
107 Standard_Failure::Raise(MSG_BAD_TOLERANCE);
111 Standard_Failure::Raise(MSG_BAD_ARG_SHAPE);
114 Standard_Failure::Raise("Error occured during check of geometric coincidence");
118 // description of all errors see in GEOMAlgo_Gluer.cxx
119 TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
120 aMsg += TCollection_AsciiString(iErr);
121 Standard_Failure::Raise(aMsg.ToCString());
128 iWrn = aGluer.WarningStatus();
133 Standard_Integer nbAlone = aGluer.AloneShapes();
134 theWarning = TCollection_AsciiString(nbAlone);
135 theWarning += " solid(s) can not be glued by faces";
139 // description of all warnings see in GEOMAlgo_Gluer.cxx
140 theWarning = "Warning in GEOMAlgo_Gluer with code ";
141 theWarning += TCollection_AsciiString(iWrn);
146 aRes = aGluer.Result();
148 // Fill history to be used by GetInPlace functionality
149 TopTools_IndexedMapOfShape aResIndices;
150 TopExp::MapShapes(aRes, aResIndices);
152 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
154 // history for all argument shapes
155 TDF_LabelSequence aLabelSeq;
156 aFunction->GetDependency(aLabelSeq);
157 Standard_Integer nbArg = aLabelSeq.Length();
159 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
161 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
163 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
164 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
166 TopTools_IndexedMapOfShape anArgumentIndices;
167 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
168 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
170 // Find corresponding label in history
171 TDF_Label anArgumentHistoryLabel =
172 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
174 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
175 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
176 const TopTools_ListOfShape& aModified = aGluer.Modified(anEntity);
177 Standard_Integer nbModified = aModified.Extent();
179 if (nbModified > 0) {
180 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
181 Handle(TDataStd_IntegerArray) anAttr =
182 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
184 TopTools_ListIteratorOfListOfShape itM (aModified);
185 for (int im = 1; itM.More(); itM.Next(), ++im) {
186 int id = aResIndices.FindIndex(itM.Value());
187 anAttr->SetValue(im, id);
197 //=======================================================================
198 //function : GlueFaces
200 //=======================================================================
201 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
202 const Standard_Real theTolerance,
203 const Standard_Boolean doKeepNonSolids)
205 Standard_Integer iErr, iWrn;
207 GEOMAlgo_Gluer aGluer;
209 aGluer.SetShape(theShape);
210 aGluer.SetTolerance(theTolerance);
211 aGluer.SetCheckGeometry(Standard_True);
212 aGluer.SetKeepNonSolids(doKeepNonSolids);
216 iErr = aGluer.ErrorStatus();
220 Standard_Failure::Raise("No vertices found in source shape");
223 Standard_Failure::Raise("Source shape is Null");
226 Standard_Failure::Raise("Result shape is Null");
229 Standard_Failure::Raise("Error occured during check of geometric coincidence");
233 // description of all errors see in GEOMAlgo_Gluer.cxx
234 TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
235 aMsg += TCollection_AsciiString(iErr);
236 Standard_Failure::Raise(aMsg.ToCString());
243 iWrn = aGluer.WarningStatus();
247 MESSAGE("Some shapes can not be glued by faces");
250 // description of all warnings see in GEOMAlgo_Gluer.cxx
251 MESSAGE("Warning in GEOMAlgo_Gluer with code " << iWrn);
256 aRes = aGluer.Result();
262 //=======================================================================
263 //function : GlueFacesByList
265 //=======================================================================
266 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesByList (const TopoDS_Shape& theShape,
267 const Standard_Real theTolerance,
268 const Standard_Boolean doKeepNonSolids,
269 const TopTools_MapOfShape& aFaces)
273 GEOMAlgo_Gluer1 aGluer;
274 GEOMAlgo_ListIteratorOfListOfCoupleOfShapes aItCS;
275 GEOMAlgo_CoupleOfShapes aCS;
276 GEOMAlgo_ListOfCoupleOfShapes aLCS;
278 aGluer.SetShape(theShape);
279 aGluer.SetTolerance(theTolerance);
280 aGluer.SetKeepNonSolids(doKeepNonSolids);
282 Standard_Integer iErr = aGluer.ErrorStatus();
283 if (iErr) return aRes;
285 TopTools_ListOfShape listShape;
286 const GEOMAlgo_ListOfCoupleOfShapes& aLCSG = aGluer.GluedFaces();
288 aItCS.Initialize(aLCSG);
289 for (; aItCS.More(); aItCS.Next()) {
290 const GEOMAlgo_CoupleOfShapes& aCSG = aItCS.Value();
291 const TopoDS_Shape& aF1 = aCSG.Shape1();
292 const TopoDS_Shape& aF2 = aCSG.Shape2();
293 if( aFaces.Contains(aF1) || aFaces.Contains(aF2) )
295 aCS.SetShapes(aF1,aF2);
299 //cout<<"aLCS.Extent() = "<<aLCS.Extent()<<endl;
300 if(aLCS.Extent()>0) {
301 aGluer.SetFacesToUnglue(aLCS);
302 aGluer.UnglueFaces();
303 iErr = aGluer.ErrorStatus();
304 if (iErr) return aRes;
307 aRes = aGluer.Result();
313 //=======================================================================
316 //=======================================================================
317 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
319 if (Label().IsNull()) return 0;
320 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
322 GEOMImpl_IGlue aCI (aFunction);
323 Standard_Integer aType = aFunction->GetType();
326 TCollection_AsciiString aWrn;
328 Handle(GEOM_Function) aRefBase = aCI.GetBase();
329 TopoDS_Shape aShapeBase = aRefBase->GetValue();
330 if (aShapeBase.IsNull()) {
331 Standard_NullObject::Raise("Shape for gluing is null");
334 Standard_Real tol3d = aCI.GetTolerance();
336 Standard_Boolean aKeepNonSolids = aCI.GetKeepNonSolids();
338 if (aType == GLUE_FACES) {
339 aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aKeepNonSolids, aWrn);
341 else { // aType == GLUE_FACES_BY_LIST
342 Handle(TColStd_HSequenceOfTransient) SF = aCI.GetFaces();
343 TopTools_MapOfShape aFaces;
345 for( ; i <= SF->Length(); i++) {
346 Handle(Standard_Transient) anItem = SF->Value(i);
349 Handle(GEOM_Function) aRefSh = Handle(GEOM_Function)::DownCast(anItem);
352 TopoDS_Shape aFace = aRefSh->GetValue();
355 if(!aFaces.Contains(aFace))
358 aShape = GlueFacesByList(aShapeBase, tol3d, aKeepNonSolids, aFaces);
361 if (aShape.IsNull()) return 0;
363 aFunction->SetValue(aShape);
365 log.SetTouched(Label());
367 if (!aWrn.IsEmpty()) {
368 Standard_Failure::Raise(aWrn.ToCString());
374 //=======================================================================
375 //function : GEOMImpl_GlueDriver_Type_
377 //=======================================================================
378 Standard_EXPORT Handle_Standard_Type& GEOMImpl_GlueDriver_Type_()
381 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
382 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
383 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
384 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
385 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
386 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
389 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
390 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_GlueDriver",
391 sizeof(GEOMImpl_GlueDriver),
393 (Standard_Address)_Ancestors,
394 (Standard_Address)NULL);
399 //=======================================================================
400 //function : DownCast
402 //=======================================================================
403 const Handle(GEOMImpl_GlueDriver) Handle(GEOMImpl_GlueDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
405 Handle(GEOMImpl_GlueDriver) _anOtherObject;
407 if (!AnObject.IsNull()) {
408 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_GlueDriver))) {
409 _anOtherObject = Handle(GEOMImpl_GlueDriver)((Handle(GEOMImpl_GlueDriver)&)AnObject);
413 return _anOtherObject ;