Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MirrorDriver.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/
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_MirrorDriver.hxx>
24
25 #include <GEOMImpl_IMirror.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepBuilderAPI_Transform.hxx>
31
32 #include <TopAbs.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Shape.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopExp.hxx>
39
40 #include <Geom_Plane.hxx>
41
42 #include <gp_Trsf.hxx>
43 #include <gp_Pnt.hxx>
44 #include <gp_Vec.hxx>
45
46 //=======================================================================
47 //function : GetID
48 //purpose  :
49 //======================================================================= 
50 const Standard_GUID& GEOMImpl_MirrorDriver::GetID()
51 {
52   static Standard_GUID aMirrorDriver("FF1BBB57-5D14-4df2-980B-3A668264EA16");
53   return aMirrorDriver; 
54 }
55
56
57 //=======================================================================
58 //function : GEOMImpl_MirrorDriver
59 //purpose  : 
60 //=======================================================================
61
62 GEOMImpl_MirrorDriver::GEOMImpl_MirrorDriver() 
63 {
64 }
65
66 //=======================================================================
67 //function : Execute
68 //purpose  :
69 //======================================================================= 
70 Standard_Integer GEOMImpl_MirrorDriver::Execute(TFunction_Logbook& log) const
71 {
72   if (Label().IsNull())  return 0;    
73   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
74
75   if (aFunction.IsNull()) return 0;
76
77   TopoDS_Shape aShape;
78   gp_Trsf aTrsf;
79
80   GEOMImpl_IMirror TI (aFunction);
81   Standard_Integer aType = aFunction->GetType();
82
83   Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
84   if (anOriginalFunction.IsNull()) return 0;
85
86   TopoDS_Shape anOriginal = anOriginalFunction->GetValue();
87   if (anOriginal.IsNull()) return 0;
88
89   if (aType == MIRROR_PLANE || aType == MIRROR_PLANE_COPY) {
90     Handle(GEOM_Function) aPlane = TI.GetPlane();
91     if (aPlane.IsNull()) return 0;
92     TopoDS_Shape aFaceShape = aPlane->GetValue();
93     if (aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE) return 0;
94     TopoDS_Face aFace = TopoDS::Face(aFaceShape);
95
96     Handle(Geom_Surface) surf = BRep_Tool::Surface(aFace);
97     Handle(Geom_Plane) myPlane = Handle(Geom_Plane)::DownCast(surf);
98     const gp_Ax3 pos = myPlane->Position();
99     const gp_Pnt loc = pos.Location();  /* location of the plane */
100     const gp_Dir dir = pos.Direction(); /* Main direction of the plane (Z axis) */
101     gp_Ax2 aPln (loc, dir);
102     aTrsf.SetMirror(aPln);
103
104   } else if (aType == MIRROR_AXIS || aType == MIRROR_AXIS_COPY) {
105     Handle(GEOM_Function) anAxis = TI.GetAxis();
106     if (anAxis.IsNull()) return 0;
107     TopoDS_Shape anAxisShape = anAxis->GetValue();
108     if (anAxisShape.IsNull() || anAxisShape.ShapeType() != TopAbs_EDGE) return 0;
109     TopoDS_Edge anEdge = TopoDS::Edge(anAxisShape);
110
111     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
112     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge));
113     gp_Vec aV (aP1, aP2);
114     gp_Ax1 anAx1 (aP1, aV);
115     aTrsf.SetMirror(anAx1);
116
117   } else if (aType == MIRROR_POINT || aType == MIRROR_POINT_COPY) {
118     Handle(GEOM_Function) aPoint = TI.GetPoint();
119     if (aPoint.IsNull()) return 0;
120     TopoDS_Shape aVertexShape = aPoint->GetValue();
121     if (aVertexShape.IsNull() || aVertexShape.ShapeType() != TopAbs_VERTEX) return 0;
122     TopoDS_Vertex aVertex = TopoDS::Vertex(aVertexShape);
123
124     gp_Pnt aP = BRep_Tool::Pnt(aVertex);
125     aTrsf.SetMirror(aP);
126   } else {
127     return 0;
128   }
129
130   BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
131   aShape = aTransformation.Shape();
132
133   if (aShape.IsNull()) return 0;
134
135   aFunction->SetValue(aShape);
136
137   log.SetTouched(Label()); 
138
139   return 1;
140 }
141
142
143 //=======================================================================
144 //function :  GEOMImpl_MirrorDriver_Type_
145 //purpose  :
146 //======================================================================= 
147 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MirrorDriver_Type_()
148 {
149
150   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
151   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
152   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
153   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
154   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
155   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
156  
157
158   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
159   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MirrorDriver",
160                                                          sizeof(GEOMImpl_MirrorDriver),
161                                                          1,
162                                                          (Standard_Address)_Ancestors,
163                                                          (Standard_Address)NULL);
164
165   return _aType;
166 }
167
168 //=======================================================================
169 //function : DownCast
170 //purpose  :
171 //======================================================================= 
172
173 const Handle(GEOMImpl_MirrorDriver) Handle(GEOMImpl_MirrorDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
174 {
175   Handle(GEOMImpl_MirrorDriver) _anOtherObject;
176
177   if (!AnObject.IsNull()) {
178      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MirrorDriver))) {
179        _anOtherObject = Handle(GEOMImpl_MirrorDriver)((Handle(GEOMImpl_MirrorDriver)&)AnObject);
180      }
181   }
182
183   return _anOtherObject ;
184 }
185
186