1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_MirrorDriver.hxx>
27 #include <GEOMImpl_IMirror.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
31 #include <BRep_Tool.hxx>
32 #include <BRepBuilderAPI_Transform.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>
43 #include <Geom_Plane.hxx>
45 #include <gp_Trsf.hxx>
49 //=======================================================================
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_MirrorDriver::GetID()
55 static Standard_GUID aMirrorDriver("FF1BBB57-5D14-4df2-980B-3A668264EA16");
60 //=======================================================================
61 //function : GEOMImpl_MirrorDriver
63 //=======================================================================
65 GEOMImpl_MirrorDriver::GEOMImpl_MirrorDriver()
69 //=======================================================================
72 //=======================================================================
73 Standard_Integer GEOMImpl_MirrorDriver::Execute(TFunction_Logbook& log) const
75 if (Label().IsNull()) return 0;
76 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
78 if (aFunction.IsNull()) return 0;
83 GEOMImpl_IMirror TI (aFunction);
84 Standard_Integer aType = aFunction->GetType();
86 Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
87 if (anOriginalFunction.IsNull()) return 0;
89 TopoDS_Shape anOriginal = anOriginalFunction->GetValue();
90 if (anOriginal.IsNull()) return 0;
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");
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);
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);
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);
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);
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);
142 gp_Pnt aP = BRep_Tool::Pnt(aVertex);
148 BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
149 aShape = aTransformation.Shape();
151 if (aShape.IsNull()) return 0;
153 aFunction->SetValue(aShape);
155 log.SetTouched(Label());
161 //=======================================================================
162 //function : GEOMImpl_MirrorDriver_Type_
164 //=======================================================================
165 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MirrorDriver_Type_()
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);
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),
180 (Standard_Address)_Ancestors,
181 (Standard_Address)NULL);
186 //=======================================================================
187 //function : DownCast
189 //=======================================================================
191 const Handle(GEOMImpl_MirrorDriver) Handle(GEOMImpl_MirrorDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
193 Handle(GEOMImpl_MirrorDriver) _anOtherObject;
195 if (!AnObject.IsNull()) {
196 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MirrorDriver))) {
197 _anOtherObject = Handle(GEOMImpl_MirrorDriver)((Handle(GEOMImpl_MirrorDriver)&)AnObject);
201 return _anOtherObject ;