Salome HOME
0020628: EDF 1144 GEOM : In TUI, need a function to know the orientation of an edge...
[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 if (aType == PLANE_2_VEC) {
178     Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
179     Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
180     TopoDS_Shape aShape1 = aRefVec1->GetValue();
181     TopoDS_Shape aShape2 = aRefVec2->GetValue();
182     if (aShape1.ShapeType() != TopAbs_EDGE ||
183         aShape2.ShapeType() != TopAbs_EDGE) return 0;
184     TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
185     TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
186
187     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
188     TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
189     TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
190
191     gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
192     gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
193
194     if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
195       Standard_TypeMismatch::Raise("Invalid vector selected");
196
197     gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
198     gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
199
200     if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
201       Standard_TypeMismatch::Raise("Parallel vectors selected");
202
203     gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
204     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
205     if(aTool.IsDone())
206       aShape = aTool.Shape();
207   }   else if (aType == PLANE_LCS) {
208     Handle(GEOM_Function) aRef = aPI.GetLCS();
209     double anOrientation = aPI.GetOrientation();    
210     gp_Ax3 anAx3;
211     if (aRef.IsNull()) {
212       gp_Ax2 anAx2 = gp::XOY();
213       anAx3 = gp_Ax3( anAx2 );
214     } else {
215       TopoDS_Shape aRefShape = aRef->GetValue();
216       if (aRefShape.ShapeType() != TopAbs_FACE)
217         return 0;
218       anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
219     }
220
221     if ( anOrientation == 2)
222       anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
223     else if ( anOrientation == 3 )
224       anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
225
226     gp_Pln aPln(anAx3);
227     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
228   }
229   else {
230   }
231
232   if (aShape.IsNull()) return 0;
233
234   aFunction->SetValue(aShape);
235
236   log.SetTouched(Label());
237
238   return 1;
239 }
240
241
242 //=======================================================================
243 //function :  GEOMImpl_PlaneDriver_Type_
244 //purpose  :
245 //=======================================================================
246 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
247 {
248
249   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
250   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
251   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
252   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
253   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
254   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
255
256
257   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
258   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
259                                                          sizeof(GEOMImpl_PlaneDriver),
260                                                          1,
261                                                          (Standard_Address)_Ancestors,
262                                                          (Standard_Address)NULL);
263
264   return _aType;
265 }
266
267 //=======================================================================
268 //function : DownCast
269 //purpose  :
270 //=======================================================================
271 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
272        (const Handle(Standard_Transient)& AnObject)
273 {
274   Handle(GEOMImpl_PlaneDriver) _anOtherObject;
275
276   if (!AnObject.IsNull()) {
277      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
278        _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
279      }
280   }
281
282   return _anOtherObject ;
283 }