Salome HOME
8941ba280deddaed8e52ac62c12b7396d471cf29
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PlaneDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_PlaneDriver.hxx>
24 #include <GEOMImpl_IPlane.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRepBuilderAPI_MakeFace.hxx>
29 #include <BRep_Tool.hxx>
30
31 #include <TopAbs.hxx>
32 #include <TopoDS.hxx>
33 #include <TopoDS_Shape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopExp.hxx>
37
38 #include <GC_MakePlane.hxx>
39 #include <Geom_Surface.hxx>
40
41 #include <Precision.hxx>
42 #include <gp_Pnt.hxx>
43 #include <gp_Pln.hxx>
44 #include <gp_Vec.hxx>
45
46 #include <Standard_ConstructionError.hxx>
47 #include <Standard_TypeMismatch.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //======================================================================= 
53 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
54 {
55   static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
56   return aPlaneDriver; 
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_PlaneDriver
62 //purpose  : 
63 //=======================================================================
64 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver() 
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //======================================================================= 
72 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
73 {
74   if (Label().IsNull())  return 0;    
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76
77   GEOMImpl_IPlane aPI (aFunction);
78   Standard_Integer aType = aFunction->GetType();
79
80   TopoDS_Shape aShape;
81
82   double aSize = aPI.GetSize() / 2.0;
83   if (aType == PLANE_PNT_VEC) {
84     Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
85     Handle(GEOM_Function) aRefVec = aPI.GetVector();
86     TopoDS_Shape aShape1 = aRefPnt->GetValue();
87     TopoDS_Shape aShape2 = aRefVec->GetValue();
88     if (aShape1.ShapeType() != TopAbs_VERTEX ||
89         aShape2.ShapeType() != TopAbs_EDGE) return 0;
90     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
91     TopoDS_Edge anE = TopoDS::Edge(aShape2);
92     TopoDS_Vertex V1, V2;
93     TopExp::Vertices(anE, V1, V2, Standard_True);
94     if (!V1.IsNull() && !V2.IsNull()) {
95       gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
96       gp_Pln aPln (aP, aV);
97       aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
98     }
99   } else if (aType == PLANE_THREE_PNT) {
100     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
101     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
102     Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
103     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
104     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
105     TopoDS_Shape aShape3 = aRefPnt3->GetValue();
106     if (aShape1.ShapeType() != TopAbs_VERTEX ||
107         aShape2.ShapeType() != TopAbs_VERTEX ||
108         aShape3.ShapeType() != TopAbs_VERTEX) return 0;
109     gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
110     gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
111     gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
112     if (aP1.Distance(aP2) < gp::Resolution() ||
113         aP1.Distance(aP3) < gp::Resolution() ||
114         aP2.Distance(aP3) < gp::Resolution())
115       Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
116     if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
117       Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
118     GC_MakePlane aMakePlane (aP1, aP2, aP3);
119     aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
120   } else if (aType == PLANE_FACE) {
121     Handle(GEOM_Function) aRef = aPI.GetFace();
122     TopoDS_Shape aRefShape = aRef->GetValue();
123     if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
124     Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
125     if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
126       Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
127     }
128     aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
129   } else {
130   }
131
132   if (aShape.IsNull()) return 0;
133
134   aFunction->SetValue(aShape);
135
136   log.SetTouched(Label()); 
137
138   return 1;
139 }
140
141
142 //=======================================================================
143 //function :  GEOMImpl_PlaneDriver_Type_
144 //purpose  :
145 //======================================================================= 
146 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
147 {
148
149   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
150   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
151   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
152   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
153   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
154   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
155  
156
157   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
158   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
159                                                          sizeof(GEOMImpl_PlaneDriver),
160                                                          1,
161                                                          (Standard_Address)_Ancestors,
162                                                          (Standard_Address)NULL);
163
164   return _aType;
165 }
166
167 //=======================================================================
168 //function : DownCast
169 //purpose  :
170 //======================================================================= 
171 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
172        (const Handle(Standard_Transient)& AnObject)
173 {
174   Handle(GEOMImpl_PlaneDriver) _anOtherObject;
175
176   if (!AnObject.IsNull()) {
177      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
178        _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
179      }
180   }
181
182   return _anOtherObject ;
183 }