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