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>
32 #include "utilities.h"
34 #include <TDataStd_IntegerArray.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopTools_ListOfShape.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopTools_ListIteratorOfListOfShape.hxx>
42 #include <Standard_NullObject.hxx>
43 #include <Standard_Failure.hxx>
45 #define MSG_BAD_TOLERANCE "Tolerance is too big"
46 #define MSG_BAD_ARG_SHAPE "Argument shape is not a compound of hexahedral solids"
48 //=======================================================================
49 //function : GEOMImpl_GlueDriver
51 //=======================================================================
52 GEOMImpl_GlueDriver::GEOMImpl_GlueDriver()
56 //=======================================================================
59 //=======================================================================
60 const Standard_GUID& GEOMImpl_GlueDriver::GetID()
62 static Standard_GUID aGlueDriver("FF1BBB63-5D14-4df2-980B-3A668264EA16");
66 //=======================================================================
67 //function : GlueFacesWithWarnings
69 //=======================================================================
70 TopoDS_Shape GEOMImpl_GlueDriver::GlueFacesWithWarnings (const TopoDS_Shape& theShape,
71 const Standard_Real theTolerance,
72 TCollection_AsciiString& theWarning) const
74 Standard_Integer iErr, iWrn;
76 GEOMAlgo_Gluer aGluer;
78 aGluer.SetShape(theShape);
79 aGluer.SetTolerance(theTolerance);
80 aGluer.SetCheckGeometry(Standard_True);
84 iErr = aGluer.ErrorStatus();
88 Standard_Failure::Raise("No vertices found in source shape");
92 Standard_Failure::Raise(MSG_BAD_TOLERANCE " or " MSG_BAD_ARG_SHAPE);
95 Standard_Failure::Raise("Source shape is Null");
98 Standard_Failure::Raise("Result shape is Null");
101 Standard_Failure::Raise(MSG_BAD_TOLERANCE);
105 Standard_Failure::Raise(MSG_BAD_ARG_SHAPE);
108 Standard_Failure::Raise("Error occured during check of geometric coincidence");
112 // description of all errors see in GEOMAlgo_Gluer.cxx
113 TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
114 aMsg += TCollection_AsciiString(iErr);
115 Standard_Failure::Raise(aMsg.ToCString());
122 iWrn = aGluer.WarningStatus();
127 Standard_Integer nbAlone = aGluer.AloneShapes();
128 theWarning = TCollection_AsciiString(nbAlone);
129 theWarning += " solid(s) can not be glued by faces";
133 // description of all warnings see in GEOMAlgo_Gluer.cxx
134 theWarning = "Warning in GEOMAlgo_Gluer with code ";
135 theWarning += TCollection_AsciiString(iWrn);
140 aRes = aGluer.Result();
142 // Fill history to be used by GetInPlace functionality
143 TopTools_IndexedMapOfShape aResIndices;
144 TopExp::MapShapes(aRes, aResIndices);
146 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
148 // history for all argument shapes
149 TDF_LabelSequence aLabelSeq;
150 aFunction->GetDependency(aLabelSeq);
151 Standard_Integer nbArg = aLabelSeq.Length();
153 for (Standard_Integer iarg = 1; iarg <= nbArg; iarg++) {
155 TDF_Label anArgumentRefLabel = aLabelSeq.Value(iarg);
157 Handle(GEOM_Object) anArgumentObject = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
158 TopoDS_Shape anArgumentShape = anArgumentObject->GetValue();
160 TopTools_IndexedMapOfShape anArgumentIndices;
161 TopExp::MapShapes(anArgumentShape, anArgumentIndices);
162 Standard_Integer nbArgumentEntities = anArgumentIndices.Extent();
164 // Find corresponding label in history
165 TDF_Label anArgumentHistoryLabel =
166 aFunction->GetArgumentHistoryEntry(anArgumentRefLabel, Standard_True);
168 for (Standard_Integer ie = 1; ie <= nbArgumentEntities; ie++) {
169 TopoDS_Shape anEntity = anArgumentIndices.FindKey(ie);
170 const TopTools_ListOfShape& aModified = aGluer.Modified(anEntity);
171 Standard_Integer nbModified = aModified.Extent();
173 if (nbModified > 0) {
174 TDF_Label aWhatHistoryLabel = anArgumentHistoryLabel.FindChild(ie, Standard_True);
175 Handle(TDataStd_IntegerArray) anAttr =
176 TDataStd_IntegerArray::Set(aWhatHistoryLabel, 1, nbModified);
178 TopTools_ListIteratorOfListOfShape itM (aModified);
179 for (int im = 1; itM.More(); itM.Next(), ++im) {
180 int id = aResIndices.FindIndex(itM.Value());
181 anAttr->SetValue(im, id);
190 //=======================================================================
191 //function : GlueFaces
193 //=======================================================================
194 TopoDS_Shape GEOMImpl_GlueDriver::GlueFaces (const TopoDS_Shape& theShape,
195 const Standard_Real theTolerance)
197 Standard_Integer iErr, iWrn;
199 GEOMAlgo_Gluer aGluer;
201 aGluer.SetShape(theShape);
202 aGluer.SetTolerance(theTolerance);
203 aGluer.SetCheckGeometry(Standard_True);
207 iErr = aGluer.ErrorStatus();
211 Standard_Failure::Raise("No vertices found in source shape");
214 Standard_Failure::Raise("Source shape is Null");
217 Standard_Failure::Raise("Result shape is Null");
220 Standard_Failure::Raise("Error occured during check of geometric coincidence");
224 // description of all errors see in GEOMAlgo_Gluer.cxx
225 TCollection_AsciiString aMsg ("Error in GEOMAlgo_Gluer with code ");
226 aMsg += TCollection_AsciiString(iErr);
227 Standard_Failure::Raise(aMsg.ToCString());
234 iWrn = aGluer.WarningStatus();
238 MESSAGE("Some shapes can not be glued by faces");
241 // description of all warnings see in GEOMAlgo_Gluer.cxx
242 MESSAGE("Warning in GEOMAlgo_Gluer with code " << iWrn);
247 aRes = aGluer.Result();
252 //=======================================================================
255 //=======================================================================
256 Standard_Integer GEOMImpl_GlueDriver::Execute(TFunction_Logbook& log) const
258 if (Label().IsNull()) return 0;
259 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
261 GEOMImpl_IGlue aCI (aFunction);
262 Standard_Integer aType = aFunction->GetType();
265 TCollection_AsciiString aWrn;
267 if (aType == GLUE_FACES) {
268 Handle(GEOM_Function) aRefBase = aCI.GetBase();
269 TopoDS_Shape aShapeBase = aRefBase->GetValue();
270 if (aShapeBase.IsNull()) {
271 Standard_NullObject::Raise("Shape for gluing is null");
274 Standard_Real tol3d = aCI.GetTolerance();
275 aShape = GlueFacesWithWarnings(aShapeBase, tol3d, aWrn);
279 if (aShape.IsNull()) return 0;
281 aFunction->SetValue(aShape);
283 log.SetTouched(Label());
285 if (!aWrn.IsEmpty()) {
286 Standard_Failure::Raise(aWrn.ToCString());
292 //=======================================================================
293 //function : GEOMImpl_GlueDriver_Type_
295 //=======================================================================
296 Standard_EXPORT Handle_Standard_Type& GEOMImpl_GlueDriver_Type_()
299 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
300 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
301 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
302 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
303 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
304 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
307 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
308 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_GlueDriver",
309 sizeof(GEOMImpl_GlueDriver),
311 (Standard_Address)_Ancestors,
312 (Standard_Address)NULL);
317 //=======================================================================
318 //function : DownCast
320 //=======================================================================
321 const Handle(GEOMImpl_GlueDriver) Handle(GEOMImpl_GlueDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
323 Handle(GEOMImpl_GlueDriver) _anOtherObject;
325 if (!AnObject.IsNull()) {
326 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_GlueDriver))) {
327 _anOtherObject = Handle(GEOMImpl_GlueDriver)((Handle(GEOMImpl_GlueDriver)&)AnObject);
331 return _anOtherObject ;