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