Salome HOME
bd5ccdee1abe0067eabc79138f049df65bdae34d
[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/ or email : webmaster.salome@opencascade.com
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 #include <BRepTopAdaptor_FClass2d.hxx>
31 #include <ShapeAnalysis.hxx>
32
33 #include <TopAbs.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Vertex.hxx>
38 #include <TopExp.hxx>
39
40 #include <GC_MakePlane.hxx>
41 #include <Geom_Surface.hxx>
42
43 #include <Precision.hxx>
44 #include <gp_Pnt.hxx>
45 #include <gp_Pln.hxx>
46 #include <gp_Vec.hxx>
47 #include <gp_Dir.hxx>
48 #include <gp_Ax3.hxx>
49
50 #include <Standard_ConstructionError.hxx>
51 #include <Standard_TypeMismatch.hxx>
52
53 //=======================================================================
54 //function : GetID
55 //purpose  :
56 //======================================================================= 
57 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
58 {
59   static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
60   return aPlaneDriver; 
61 }
62
63
64 //=======================================================================
65 //function : GEOMImpl_PlaneDriver
66 //purpose  : 
67 //=======================================================================
68 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver() 
69 {
70 }
71
72 //=======================================================================
73 //function : Execute
74 //purpose  :
75 //======================================================================= 
76 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
77 {
78   if (Label().IsNull())  return 0;    
79   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
80
81   GEOMImpl_IPlane aPI (aFunction);
82   Standard_Integer aType = aFunction->GetType();
83
84   TopoDS_Shape aShape;
85
86   double aSize = aPI.GetSize() / 2.0;
87   if (aType == PLANE_PNT_VEC) {
88     Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
89     Handle(GEOM_Function) aRefVec = aPI.GetVector();
90     TopoDS_Shape aShape1 = aRefPnt->GetValue();
91     TopoDS_Shape aShape2 = aRefVec->GetValue();
92     if (aShape1.ShapeType() != TopAbs_VERTEX ||
93         aShape2.ShapeType() != TopAbs_EDGE) return 0;
94     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
95     TopoDS_Edge anE = TopoDS::Edge(aShape2);
96     TopoDS_Vertex V1, V2;
97     TopExp::Vertices(anE, V1, V2, Standard_True);
98     if (!V1.IsNull() && !V2.IsNull()) {
99       gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
100       gp_Pln aPln (aP, aV);
101       aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
102     }
103   } else if (aType == PLANE_THREE_PNT) {
104     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
105     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
106     Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
107     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
108     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
109     TopoDS_Shape aShape3 = aRefPnt3->GetValue();
110     if (aShape1.ShapeType() != TopAbs_VERTEX ||
111         aShape2.ShapeType() != TopAbs_VERTEX ||
112         aShape3.ShapeType() != TopAbs_VERTEX) return 0;
113     gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
114     gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
115     gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
116     if (aP1.Distance(aP2) < gp::Resolution() ||
117         aP1.Distance(aP3) < gp::Resolution() ||
118         aP2.Distance(aP3) < gp::Resolution())
119       Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
120     if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
121       Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
122     GC_MakePlane aMakePlane (aP1, aP2, aP3);
123     aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
124   } else if (aType == PLANE_FACE) {
125     Handle(GEOM_Function) aRef = aPI.GetFace();
126     TopoDS_Shape aRefShape = aRef->GetValue();
127     if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
128     Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
129     if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
130       Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
131     }
132     aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
133   } 
134   else if (aType == PLANE_TANGENT_FACE)
135   {
136     Handle(GEOM_Function) aRefFace = aPI.GetFace();
137     TopoDS_Shape aShape1 = aRefFace->GetValue();
138     if(aShape1.IsNull())
139       Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
140     TopoDS_Face aFace = TopoDS::Face(aShape1);
141     
142     Standard_Real aKoefU = aPI.GetParameterU();
143     Standard_Real aKoefV = aPI.GetParameterV();
144     Standard_Real aUmin,aUmax,aVmin,aVmax;
145     ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
146     Standard_Real aDeltaU = aUmax - aUmin;
147     Standard_Real aDeltaV = aVmax - aVmin;
148     Standard_Real aParamU =  aUmin + aDeltaU*aKoefU;
149     Standard_Real aParamV =  aVmin + aDeltaV*aKoefV;
150     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
151     if(aSurf.IsNull())
152       Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
153     gp_Vec aVecU,aVecV;
154     gp_Pnt aPLoc;
155     aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
156     BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
157     
158     TopAbs_State stOut= clas.PerformInfinitePoint();
159     gp_Pnt2d aP2d(aParamU,aParamV);
160     TopAbs_State st= clas.Perform(aP2d);
161     if(st == stOut)
162       Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
163     gp_Vec aNorm = aVecU^aVecV;
164     gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
165     gp_Pln aPlane(anAxis);
166     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
167     if(aTool.IsDone())
168       aShape = aTool.Shape();
169   }
170
171   else {
172   }
173
174   if (aShape.IsNull()) return 0;
175
176   aFunction->SetValue(aShape);
177
178   log.SetTouched(Label()); 
179
180   return 1;
181 }
182
183
184 //=======================================================================
185 //function :  GEOMImpl_PlaneDriver_Type_
186 //purpose  :
187 //======================================================================= 
188 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
189 {
190
191   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
192   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
193   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
194   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
195   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
196   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
197  
198
199   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
200   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
201                                                          sizeof(GEOMImpl_PlaneDriver),
202                                                          1,
203                                                          (Standard_Address)_Ancestors,
204                                                          (Standard_Address)NULL);
205
206   return _aType;
207 }
208
209 //=======================================================================
210 //function : DownCast
211 //purpose  :
212 //======================================================================= 
213 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
214        (const Handle(Standard_Transient)& AnObject)
215 {
216   Handle(GEOMImpl_PlaneDriver) _anOtherObject;
217
218   if (!AnObject.IsNull()) {
219      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
220        _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
221      }
222   }
223
224   return _anOtherObject ;
225 }