Salome HOME
Heal invalid shape after scaling, because different scaling along axes can produce...
[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 <ShapeFix_Shape.hxx>
29 #include <ShapeFix_ShapeTolerance.hxx>
30
31 #include <BRepBuilderAPI_Transform.hxx>
32 #include <BRepBuilderAPI_GTransform.hxx>
33 #include <BRep_Tool.hxx>
34 #include <BRepAlgo.hxx>
35 #include <BRepCheck_Analyzer.hxx>
36
37 #include <TopAbs.hxx>
38 #include <TopExp.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
43
44 #include <Precision.hxx>
45 #include <gp_Pnt.hxx>
46 #include <gp_Trsf.hxx>
47 #include <gp_GTrsf.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //======================================================================= 
53 const Standard_GUID& GEOMImpl_ScaleDriver::GetID()
54 {
55   static Standard_GUID aScaleDriver("FF1BBB52-5D14-4df2-980B-3A668264EA16");
56   return aScaleDriver; 
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_ScaleDriver
62 //purpose  : 
63 //=======================================================================
64 GEOMImpl_ScaleDriver::GEOMImpl_ScaleDriver() 
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //======================================================================= 
72 Standard_Integer GEOMImpl_ScaleDriver::Execute(TFunction_Logbook& log) const
73 {
74   if (Label().IsNull()) return 0;    
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76
77   GEOMImpl_IScale aCI (aFunction);
78   Standard_Integer aType = aFunction->GetType();
79
80   TopoDS_Shape aShape;
81
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;
86
87     gp_Pnt aP (0,0,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));
94     }
95
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");
107       }
108     }
109
110     // Perform Scaling
111     gp_Trsf aTrsf;
112     aTrsf.SetScale(aP, aCI.GetFactor());
113     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
114     aShape = aBRepTrsf.Shape();
115   }
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;
120
121     bool isP = false;
122     gp_Pnt aP (0,0,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));
129       isP = true;
130     }
131
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");
143       }
144     }
145
146     // Perform Scaling
147     gp_GTrsf aGTrsf;
148     gp_Mat rot (aCI.GetFactorX(), 0, 0,
149                 0, aCI.GetFactorY(), 0,
150                 0, 0, aCI.GetFactorZ());
151     aGTrsf.SetVectorialPart(rot);
152
153     if (isP) {
154       gp_Pnt anO (0,0,0);
155       if (anO.Distance(aP) > Precision::Confusion()) {
156         gp_GTrsf aGTrsfP0;
157         aGTrsfP0.SetTranslationPart(anO.XYZ() - aP.XYZ());
158         gp_GTrsf aGTrsf0P;
159         aGTrsf0P.SetTranslationPart(aP.XYZ());
160         //aGTrsf = aGTrsf0P * aGTrsf * aGTrsfP0;
161         aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
162         aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
163       }
164     }
165
166     BRepBuilderAPI_GTransform aBRepGTrsf (aShapeBase, aGTrsf, Standard_False);
167     if (!aBRepGTrsf.IsDone())
168       Standard_ConstructionError::Raise("Scaling not done");
169     aShape = aBRepGTrsf.Shape();
170   }
171   else {
172   }
173
174   if (aShape.IsNull()) return 0;
175
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());
182     aSfs->Perform();
183     aShape = aSfs->Shape();
184
185     ana.Init(aShape, Standard_False);
186     if (!ana.IsValid())
187       Standard_ConstructionError::Raise("Scaling aborted : algorithm has produced an invalid shape result");
188   }
189
190   aFunction->SetValue(aShape);
191
192   log.SetTouched(Label()); 
193
194   return 1;    
195 }
196
197
198 //=======================================================================
199 //function :  GEOMImpl_ScaleDriver_Type_
200 //purpose  :
201 //======================================================================= 
202 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ScaleDriver_Type_()
203 {
204
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);
211  
212
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),
216                                                          1,
217                                                          (Standard_Address)_Ancestors,
218                                                          (Standard_Address)NULL);
219
220   return _aType;
221 }
222
223 //=======================================================================
224 //function : DownCast
225 //purpose  :
226 //======================================================================= 
227 const Handle(GEOMImpl_ScaleDriver) Handle(GEOMImpl_ScaleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
228 {
229   Handle(GEOMImpl_ScaleDriver) _anOtherObject;
230
231   if (!AnObject.IsNull()) {
232      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ScaleDriver))) {
233        _anOtherObject = Handle(GEOMImpl_ScaleDriver)((Handle(GEOMImpl_ScaleDriver)&)AnObject);
234      }
235   }
236
237   return _anOtherObject ;
238 }