Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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 <BRep_Tool.hxx>
30 #include <BRepAlgo.hxx>
31 #include <TopAbs.hxx>
32 #include <TopExp.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
37
38 #include <Precision.hxx>
39 #include <gp_Pnt.hxx>
40
41 //=======================================================================
42 //function : GetID
43 //purpose  :
44 //======================================================================= 
45 const Standard_GUID& GEOMImpl_ScaleDriver::GetID()
46 {
47   static Standard_GUID aScaleDriver("FF1BBB52-5D14-4df2-980B-3A668264EA16");
48   return aScaleDriver; 
49 }
50
51
52 //=======================================================================
53 //function : GEOMImpl_ScaleDriver
54 //purpose  : 
55 //=======================================================================
56 GEOMImpl_ScaleDriver::GEOMImpl_ScaleDriver() 
57 {
58 }
59
60 //=======================================================================
61 //function : Execute
62 //purpose  :
63 //======================================================================= 
64 Standard_Integer GEOMImpl_ScaleDriver::Execute(TFunction_Logbook& log) const
65 {
66   if (Label().IsNull()) return 0;    
67   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
68
69   GEOMImpl_IScale aCI (aFunction);
70   Standard_Integer aType = aFunction->GetType();
71
72   TopoDS_Shape aShape;
73
74   if (aType == SCALE_SHAPE || aType == SCALE_SHAPE_COPY) {
75     Handle(GEOM_Function) aRefShape = aCI.GetShape();
76     Handle(GEOM_Function) aRefPoint = aCI.GetPoint();
77     TopoDS_Shape aShapeBase = aRefShape->GetValue();
78     TopoDS_Shape aShapePnt  = aRefPoint->GetValue();
79     if (aShapeBase.IsNull() || aShapePnt.IsNull()) return 0;
80     if (aShapePnt.ShapeType() != TopAbs_VERTEX) return 0;
81
82     // Bug 6839: Check for standalone (not included in faces) degenerated edges
83     TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
84     TopExp::MapShapesAndAncestors(aShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
85     Standard_Integer i, nbE = aEFMap.Extent();
86     for (i = 1; i <= nbE; i++) {
87       TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
88       if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
89         const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
90         if (aFaces.IsEmpty())
91           Standard_ConstructionError::Raise
92             ("Scaling aborted : cannot scale standalone degenerated edge");
93       }
94     }
95
96     // Perform Scaling
97     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
98     gp_Trsf aTrsf;
99     aTrsf.SetScale(aP, aCI.GetFactor());
100     BRepBuilderAPI_Transform aBRepTrsf (aShapeBase, aTrsf, Standard_False);
101     aShape = aBRepTrsf.Shape();
102   } else {
103   }
104
105   if (aShape.IsNull()) return 0;
106
107   if (!BRepAlgo::IsValid(aShape))
108     Standard_ConstructionError::Raise("Scaling aborted : non valid shape result");
109
110   aFunction->SetValue(aShape);
111
112   log.SetTouched(Label()); 
113
114   return 1;    
115 }
116
117
118 //=======================================================================
119 //function :  GEOMImpl_ScaleDriver_Type_
120 //purpose  :
121 //======================================================================= 
122 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ScaleDriver_Type_()
123 {
124
125   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
126   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
127   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
128   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
129   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
130   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
131  
132
133   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
134   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ScaleDriver",
135                                                          sizeof(GEOMImpl_ScaleDriver),
136                                                          1,
137                                                          (Standard_Address)_Ancestors,
138                                                          (Standard_Address)NULL);
139
140   return _aType;
141 }
142
143 //=======================================================================
144 //function : DownCast
145 //purpose  :
146 //======================================================================= 
147 const Handle(GEOMImpl_ScaleDriver) Handle(GEOMImpl_ScaleDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
148 {
149   Handle(GEOMImpl_ScaleDriver) _anOtherObject;
150
151   if (!AnObject.IsNull()) {
152      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ScaleDriver))) {
153        _anOtherObject = Handle(GEOMImpl_ScaleDriver)((Handle(GEOMImpl_ScaleDriver)&)AnObject);
154      }
155   }
156
157   return _anOtherObject ;
158 }