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