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_ScaleDriver.hxx>
24 #include <GEOMImpl_IScale.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
28 #include <ShapeFix_Shape.hxx>
29 #include <ShapeFix_ShapeTolerance.hxx>
31 #include <BRepBuilderAPI_Transform.hxx>
32 #include <BRepBuilderAPI_GTransform.hxx>
33 #include <BRep_Tool.hxx>
34 #include <BRepAlgo.hxx>
35 #include <BRepCheck_Analyzer.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
44 #include <Precision.hxx>
46 #include <gp_Trsf.hxx>
47 #include <gp_GTrsf.hxx>
49 //=======================================================================
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_ScaleDriver::GetID()
55 static Standard_GUID aScaleDriver("FF1BBB52-5D14-4df2-980B-3A668264EA16");
60 //=======================================================================
61 //function : GEOMImpl_ScaleDriver
63 //=======================================================================
64 GEOMImpl_ScaleDriver::GEOMImpl_ScaleDriver()
68 //=======================================================================
71 //=======================================================================
72 Standard_Integer GEOMImpl_ScaleDriver::Execute(TFunction_Logbook& log) const
74 if (Label().IsNull()) return 0;
75 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
77 GEOMImpl_IScale aCI (aFunction);
78 Standard_Integer aType = aFunction->GetType();
82 if (aType == SCALE_SHAPE || aType == SCALE_SHAPE_COPY) {
83 Handle(GEOM_Function) aRefShape = aCI.GetShape();
84 TopoDS_Shape aShapeBase = aRefShape->GetValue();
85 if (aShapeBase.IsNull()) return 0;
88 Handle(GEOM_Function) aRefPoint = aCI.GetPoint();
89 if (!aRefPoint.IsNull()) {
90 TopoDS_Shape aShapePnt = aRefPoint->GetValue();
91 if (aShapePnt.IsNull()) return 0;
92 if (aShapePnt.ShapeType() != TopAbs_VERTEX) return 0;
93 aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
96 // Bug 6839: Check for standalone (not included in faces) degenerated edges
97 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
98 TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
99 Standard_Integer i, nbE = aEFMap.Extent();
100 for (i = 1; i <= nbE; i++) {
101 TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
102 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
103 const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
104 if (aFaces.IsEmpty())
105 Standard_ConstructionError::Raise
106 ("Scaling aborted : cannot scale standalone degenerated edge");
112 aTrsf.SetScale(aP, aCI.GetFactor());
113 BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
114 aShape = aBRepTrsf.Shape();
116 else if (aType == SCALE_SHAPE_AXES || aType == SCALE_SHAPE_AXES_COPY) {
117 Handle(GEOM_Function) aRefShape = aCI.GetShape();
118 TopoDS_Shape aShapeBase = aRefShape->GetValue();
119 if (aShapeBase.IsNull()) return 0;
123 Handle(GEOM_Function) aRefPoint = aCI.GetPoint();
124 if (!aRefPoint.IsNull()) {
125 TopoDS_Shape aShapePnt = aRefPoint->GetValue();
126 if (aShapePnt.IsNull()) return 0;
127 if (aShapePnt.ShapeType() != TopAbs_VERTEX) return 0;
128 aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
132 // Bug 6839: Check for standalone (not included in faces) degenerated edges
133 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
134 TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
135 Standard_Integer i, nbE = aEFMap.Extent();
136 for (i = 1; i <= nbE; i++) {
137 TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
138 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
139 const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
140 if (aFaces.IsEmpty())
141 Standard_ConstructionError::Raise
142 ("Scaling aborted : cannot scale standalone degenerated edge");
148 gp_Mat rot (aCI.GetFactorX(), 0, 0,
149 0, aCI.GetFactorY(), 0,
150 0, 0, aCI.GetFactorZ());
151 aGTrsf.SetVectorialPart(rot);
155 if (anO.Distance(aP) > Precision::Confusion()) {
157 aGTrsfP0.SetTranslationPart(anO.XYZ() - aP.XYZ());
159 aGTrsf0P.SetTranslationPart(aP.XYZ());
160 //aGTrsf = aGTrsf0P * aGTrsf * aGTrsfP0;
161 aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
162 aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
166 BRepBuilderAPI_GTransform aBRepGTrsf (aShapeBase, aGTrsf, Standard_False);
167 if (!aBRepGTrsf.IsDone())
168 Standard_ConstructionError::Raise("Scaling not done");
169 aShape = aBRepGTrsf.Shape();
174 if (aShape.IsNull()) return 0;
176 BRepCheck_Analyzer ana (aShape, Standard_False);
177 if (!ana.IsValid()) {
178 ShapeFix_ShapeTolerance aSFT;
179 aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
180 Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
181 aSfs->SetPrecision(Precision::Confusion());
183 aShape = aSfs->Shape();
185 ana.Init(aShape, Standard_False);
187 Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
190 aFunction->SetValue(aShape);
192 log.SetTouched(Label());
198 //=======================================================================
199 //function : GEOMImpl_ScaleDriver_Type_
201 //=======================================================================
202 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ScaleDriver_Type_()
205 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
206 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
207 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
208 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
209 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
210 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
213 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
214 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ScaleDriver",
215 sizeof(GEOMImpl_ScaleDriver),
217 (Standard_Address)_Ancestors,
218 (Standard_Address)NULL);
223 //=======================================================================
224 //function : DownCast
226 //=======================================================================
227 const Handle(GEOMImpl_ScaleDriver) Handle(GEOMImpl_ScaleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
229 Handle(GEOMImpl_ScaleDriver) _anOtherObject;
231 if (!AnObject.IsNull()) {
232 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ScaleDriver))) {
233 _anOtherObject = Handle(GEOMImpl_ScaleDriver)((Handle(GEOMImpl_ScaleDriver)&)AnObject);
237 return _anOtherObject ;