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