Salome HOME
0f56de37205ca439e53b584fcb6e18b9047b4dc6
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ScaleDriver.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_ScaleDriver.hxx>
25 #include <GEOMImpl_IScale.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <ShapeFix_Shape.hxx>
30 #include <ShapeFix_ShapeTolerance.hxx>
31
32 #include <BRepBuilderAPI_Transform.hxx>
33 #include <BRepBuilderAPI_GTransform.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepAlgo.hxx>
36 #include <BRepCheck_Analyzer.hxx>
37
38 #include <TopAbs.hxx>
39 #include <TopExp.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
44
45 #include <Precision.hxx>
46 #include <gp_Pnt.hxx>
47 #include <gp_Trsf.hxx>
48 #include <gp_GTrsf.hxx>
49
50 //=======================================================================
51 //function : GetID
52 //purpose  :
53 //======================================================================= 
54 const Standard_GUID& GEOMImpl_ScaleDriver::GetID()
55 {
56   static Standard_GUID aScaleDriver("FF1BBB52-5D14-4df2-980B-3A668264EA16");
57   return aScaleDriver; 
58 }
59
60
61 //=======================================================================
62 //function : GEOMImpl_ScaleDriver
63 //purpose  : 
64 //=======================================================================
65 GEOMImpl_ScaleDriver::GEOMImpl_ScaleDriver() 
66 {
67 }
68
69 //=======================================================================
70 //function : Execute
71 //purpose  :
72 //======================================================================= 
73 Standard_Integer GEOMImpl_ScaleDriver::Execute(TFunction_Logbook& log) const
74 {
75   if (Label().IsNull()) return 0;    
76   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
77
78   GEOMImpl_IScale aCI (aFunction);
79   Standard_Integer aType = aFunction->GetType();
80
81   TopoDS_Shape aShape;
82
83   if (aType == SCALE_SHAPE || aType == SCALE_SHAPE_COPY) {
84     Handle(GEOM_Function) aRefShape = aCI.GetShape();
85     TopoDS_Shape aShapeBase = aRefShape->GetValue();
86     if (aShapeBase.IsNull()) return 0;
87
88     gp_Pnt aP (0,0,0);
89     Handle(GEOM_Function) aRefPoint = aCI.GetPoint();
90     if (!aRefPoint.IsNull()) {
91       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
92       if (aShapePnt.IsNull()) return 0;
93       if (aShapePnt.ShapeType() != TopAbs_VERTEX) return 0;
94       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
95     }
96
97     // Bug 6839: Check for standalone (not included in faces) degenerated edges
98     TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
99     TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
100     Standard_Integer i, nbE = aEFMap.Extent();
101     for (i = 1; i <= nbE; i++) {
102       TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
103       if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
104         const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
105         if (aFaces.IsEmpty())
106           Standard_ConstructionError::Raise
107             ("Scaling aborted : cannot scale standalone degenerated edge");
108       }
109     }
110
111     // Perform Scaling
112     gp_Trsf aTrsf;
113     aTrsf.SetScale(aP, aCI.GetFactor());
114     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
115     aShape = aBRepTrsf.Shape();
116   }
117   else if (aType == SCALE_SHAPE_AXES || aType == SCALE_SHAPE_AXES_COPY) {
118     Handle(GEOM_Function) aRefShape = aCI.GetShape();
119     TopoDS_Shape aShapeBase = aRefShape->GetValue();
120     if (aShapeBase.IsNull()) return 0;
121
122     bool isP = false;
123     gp_Pnt aP (0,0,0);
124     Handle(GEOM_Function) aRefPoint = aCI.GetPoint();
125     if (!aRefPoint.IsNull()) {
126       TopoDS_Shape aShapePnt = aRefPoint->GetValue();
127       if (aShapePnt.IsNull()) return 0;
128       if (aShapePnt.ShapeType() != TopAbs_VERTEX) return 0;
129       aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
130       isP = true;
131     }
132
133     // Bug 6839: Check for standalone (not included in faces) degenerated edges
134     TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
135     TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
136     Standard_Integer i, nbE = aEFMap.Extent();
137     for (i = 1; i <= nbE; i++) {
138       TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
139       if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
140         const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
141         if (aFaces.IsEmpty())
142           Standard_ConstructionError::Raise
143             ("Scaling aborted : cannot scale standalone degenerated edge");
144       }
145     }
146
147     // Perform Scaling
148     gp_GTrsf aGTrsf;
149     gp_Mat rot (aCI.GetFactorX(), 0, 0,
150                 0, aCI.GetFactorY(), 0,
151                 0, 0, aCI.GetFactorZ());
152     aGTrsf.SetVectorialPart(rot);
153
154     if (isP) {
155       gp_Pnt anO (0,0,0);
156       if (anO.Distance(aP) > Precision::Confusion()) {
157         gp_GTrsf aGTrsfP0;
158         aGTrsfP0.SetTranslationPart(anO.XYZ() - aP.XYZ());
159         gp_GTrsf aGTrsf0P;
160         aGTrsf0P.SetTranslationPart(aP.XYZ());
161         //aGTrsf = aGTrsf0P * aGTrsf * aGTrsfP0;
162         aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
163         aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
164       }
165     }
166
167     BRepBuilderAPI_GTransform aBRepGTrsf (aShapeBase, aGTrsf, Standard_False);
168     if (!aBRepGTrsf.IsDone())
169       Standard_ConstructionError::Raise("Scaling not done");
170     aShape = aBRepGTrsf.Shape();
171   }
172   else {
173   }
174
175   if (aShape.IsNull()) return 0;
176
177   BRepCheck_Analyzer ana (aShape, Standard_False);
178   if (!ana.IsValid()) {
179     ShapeFix_ShapeTolerance aSFT;
180     aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
181     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
182     aSfs->SetPrecision(Precision::Confusion());
183     aSfs->Perform();
184     aShape = aSfs->Shape();
185
186     ana.Init(aShape, Standard_False);
187     if (!ana.IsValid())
188       Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
189   }
190
191   aFunction->SetValue(aShape);
192
193   log.SetTouched(Label()); 
194
195   return 1;    
196 }
197
198
199 //=======================================================================
200 //function :  GEOMImpl_ScaleDriver_Type_
201 //purpose  :
202 //======================================================================= 
203 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ScaleDriver_Type_()
204 {
205
206   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
207   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
208   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
209   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
210   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
211   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
212  
213
214   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
215   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ScaleDriver",
216                                                          sizeof(GEOMImpl_ScaleDriver),
217                                                          1,
218                                                          (Standard_Address)_Ancestors,
219                                                          (Standard_Address)NULL);
220
221   return _aType;
222 }
223
224 //=======================================================================
225 //function : DownCast
226 //purpose  :
227 //======================================================================= 
228 const Handle(GEOMImpl_ScaleDriver) Handle(GEOMImpl_ScaleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
229 {
230   Handle(GEOMImpl_ScaleDriver) _anOtherObject;
231
232   if (!AnObject.IsNull()) {
233      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ScaleDriver))) {
234        _anOtherObject = Handle(GEOMImpl_ScaleDriver)((Handle(GEOMImpl_ScaleDriver)&)AnObject);
235      }
236   }
237
238   return _anOtherObject ;
239 }