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