1 // Copyright (C) 2007-2008 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
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_MirrorDriver.hxx>
26 #include <GEOMImpl_IMirror.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
30 #include <BRep_Tool.hxx>
31 #include <BRepBuilderAPI_Transform.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>
42 #include <Geom_Plane.hxx>
44 #include <gp_Trsf.hxx>
48 //=======================================================================
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_MirrorDriver::GetID()
54 static Standard_GUID aMirrorDriver("FF1BBB57-5D14-4df2-980B-3A668264EA16");
59 //=======================================================================
60 //function : GEOMImpl_MirrorDriver
62 //=======================================================================
64 GEOMImpl_MirrorDriver::GEOMImpl_MirrorDriver()
68 //=======================================================================
71 //=======================================================================
72 Standard_Integer GEOMImpl_MirrorDriver::Execute(TFunction_Logbook& log) const
74 if (Label().IsNull()) return 0;
75 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
77 if (aFunction.IsNull()) return 0;
82 GEOMImpl_IMirror TI (aFunction);
83 Standard_Integer aType = aFunction->GetType();
85 Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
86 if (anOriginalFunction.IsNull()) return 0;
88 TopoDS_Shape anOriginal = anOriginalFunction->GetValue();
89 if (anOriginal.IsNull()) return 0;
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);
100 Standard_ConstructionError::Raise
101 ("Mirror aborted : cannot process standalone degenerated edge");
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);
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);
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);
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);
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);
141 gp_Pnt aP = BRep_Tool::Pnt(aVertex);
147 BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
148 aShape = aTransformation.Shape();
150 if (aShape.IsNull()) return 0;
152 aFunction->SetValue(aShape);
154 log.SetTouched(Label());
160 //=======================================================================
161 //function : GEOMImpl_MirrorDriver_Type_
163 //=======================================================================
164 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MirrorDriver_Type_()
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);
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),
179 (Standard_Address)_Ancestors,
180 (Standard_Address)NULL);
185 //=======================================================================
186 //function : DownCast
188 //=======================================================================
190 const Handle(GEOMImpl_MirrorDriver) Handle(GEOMImpl_MirrorDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
192 Handle(GEOMImpl_MirrorDriver) _anOtherObject;
194 if (!AnObject.IsNull()) {
195 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MirrorDriver))) {
196 _anOtherObject = Handle(GEOMImpl_MirrorDriver)((Handle(GEOMImpl_MirrorDriver)&)AnObject);
200 return _anOtherObject ;