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