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