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