Salome HOME
fefb1531d7c524c79e83c73c9c4f5cdb3677df00
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MirrorDriver.cxx
1
2 using namespace std;
3 #include "GEOMImpl_MirrorDriver.hxx"
4
5 #include <GEOMImpl_IMirror.hxx>
6 #include <GEOMImpl_Types.hxx>
7 #include "GEOM_Function.hxx"
8
9 #include <BRep_Tool.hxx>
10 #include <BRepBuilderAPI_Transform.hxx>
11
12 #include <TopAbs.hxx>
13 #include <TopoDS.hxx>
14 #include <TopoDS_Shape.hxx>
15 #include <TopoDS_Edge.hxx>
16 #include <TopoDS_Face.hxx>
17 #include <TopoDS_Vertex.hxx>
18 #include <TopExp.hxx>
19
20 #include <Geom_Plane.hxx>
21
22 #include <gp_Trsf.hxx>
23 #include <gp_Pnt.hxx>
24 #include <gp_Vec.hxx>
25
26 //=======================================================================
27 //function : GetID
28 //purpose  :
29 //======================================================================= 
30 const Standard_GUID& GEOMImpl_MirrorDriver::GetID()
31 {
32   static Standard_GUID aMirrorDriver("FF1BBB57-5D14-4df2-980B-3A668264EA16");
33   return aMirrorDriver; 
34 }
35
36
37 //=======================================================================
38 //function : GEOMImpl_MirrorDriver
39 //purpose  : 
40 //=======================================================================
41
42 GEOMImpl_MirrorDriver::GEOMImpl_MirrorDriver() 
43 {
44 }
45
46 //=======================================================================
47 //function : Execute
48 //purpose  :
49 //======================================================================= 
50 Standard_Integer GEOMImpl_MirrorDriver::Execute(TFunction_Logbook& log) const
51 {
52   if (Label().IsNull())  return 0;    
53   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
54
55   if (aFunction.IsNull()) return 0;
56
57   TopoDS_Shape aShape;
58   gp_Trsf aTrsf;
59
60   GEOMImpl_IMirror TI (aFunction);
61   Standard_Integer aType = aFunction->GetType();
62
63   Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
64   if (anOriginalFunction.IsNull()) return 0;
65
66   TopoDS_Shape anOriginal = anOriginalFunction->GetValue();
67   if (anOriginal.IsNull()) return 0;
68
69   if (aType == MIRROR_PLANE || aType == MIRROR_PLANE_COPY) {
70     Handle(GEOM_Function) aPlane = TI.GetPlane();
71     if (aPlane.IsNull()) return 0;
72     TopoDS_Shape aFaceShape = aPlane->GetValue();
73     if (aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE) return 0;
74     TopoDS_Face aFace = TopoDS::Face(aFaceShape);
75
76     Handle(Geom_Surface) surf = BRep_Tool::Surface(aFace);
77     Handle(Geom_Plane) myPlane = Handle(Geom_Plane)::DownCast(surf);
78     const gp_Ax3 pos = myPlane->Position();
79     const gp_Pnt loc = pos.Location();  /* location of the plane */
80     const gp_Dir dir = pos.Direction(); /* Main direction of the plane (Z axis) */
81     gp_Ax2 aPln (loc, dir);
82     aTrsf.SetMirror(aPln);
83
84   } else if (aType == MIRROR_AXIS || aType == MIRROR_AXIS_COPY) {
85     Handle(GEOM_Function) anAxis = TI.GetAxis();
86     if (anAxis.IsNull()) return 0;
87     TopoDS_Shape anAxisShape = anAxis->GetValue();
88     if (anAxisShape.IsNull() || anAxisShape.ShapeType() != TopAbs_EDGE) return 0;
89     TopoDS_Edge anEdge = TopoDS::Edge(anAxisShape);
90
91     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
92     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex (anEdge));
93     gp_Vec aV (aP1, aP2);
94     gp_Ax1 anAx1 (aP1, aV);
95     aTrsf.SetMirror(anAx1);
96
97   } else if (aType == MIRROR_POINT || aType == MIRROR_POINT_COPY) {
98     Handle(GEOM_Function) aPoint = TI.GetPoint();
99     if (aPoint.IsNull()) return 0;
100     TopoDS_Shape aVertexShape = aPoint->GetValue();
101     if (aVertexShape.IsNull() || aVertexShape.ShapeType() != TopAbs_VERTEX) return 0;
102     TopoDS_Vertex aVertex = TopoDS::Vertex(aVertexShape);
103
104     gp_Pnt aP = BRep_Tool::Pnt(aVertex);
105     aTrsf.SetMirror(aP);
106   } else {
107     return 0;
108   }
109
110   BRepBuilderAPI_Transform aTransformation (anOriginal, aTrsf, Standard_False);
111   aShape = aTransformation.Shape();
112
113   if (aShape.IsNull()) return 0;
114
115   aFunction->SetValue(aShape);
116
117   log.SetTouched(Label()); 
118
119   return 1;
120 }
121
122
123 //=======================================================================
124 //function :  GEOMImpl_MirrorDriver_Type_
125 //purpose  :
126 //======================================================================= 
127 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MirrorDriver_Type_()
128 {
129
130   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
131   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
132   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
133   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
134   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
135   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
136  
137
138   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
139   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MirrorDriver",
140                                                          sizeof(GEOMImpl_MirrorDriver),
141                                                          1,
142                                                          (Standard_Address)_Ancestors,
143                                                          (Standard_Address)NULL);
144
145   return _aType;
146 }
147
148 //=======================================================================
149 //function : DownCast
150 //purpose  :
151 //======================================================================= 
152
153 const Handle(GEOMImpl_MirrorDriver) Handle(GEOMImpl_MirrorDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
154 {
155   Handle(GEOMImpl_MirrorDriver) _anOtherObject;
156
157   if (!AnObject.IsNull()) {
158      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MirrorDriver))) {
159        _anOtherObject = Handle(GEOMImpl_MirrorDriver)((Handle(GEOMImpl_MirrorDriver)&)AnObject);
160      }
161   }
162
163   return _anOtherObject ;
164 }
165
166