Salome HOME
9dd5bcdcbd43861e3967395ab137a24d9bcccf48
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MirrorDriver.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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(Handle(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  * \brief Returns a name of creation operation and names and values of creation parameters
167  */
168 //================================================================================
169
170 bool GEOMImpl_MirrorDriver::
171 GetCreationInformation(std::string&             theOperationName,
172                        std::vector<GEOM_Param>& theParams)
173 {
174   if (Label().IsNull()) return 0;
175   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
176
177   GEOMImpl_IMirror aCI( function );
178   Standard_Integer aType = function->GetType();
179
180   theOperationName = "MIRROR";
181
182   switch ( aType ) {
183   case MIRROR_PLANE:
184   case MIRROR_PLANE_COPY:
185     AddParam( theParams, "Object", aCI.GetOriginal() );
186     AddParam( theParams, "Plane Mirror", aCI.GetPlane() );
187     break;
188   case MIRROR_AXIS:
189   case MIRROR_AXIS_COPY:
190     AddParam( theParams, "Object", aCI.GetOriginal() );
191     AddParam( theParams, "Axis Mirror", aCI.GetAxis() );
192     break;
193   case MIRROR_POINT:
194   case MIRROR_POINT_COPY:
195     AddParam( theParams, "Object", aCI.GetOriginal() );
196     AddParam( theParams, "Point Mirror", aCI.GetPoint() );
197     break;
198   default:
199     return false;
200   }
201   
202   return true;
203 }
204
205 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_MirrorDriver,GEOM_BaseDriver)