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