Salome HOME
Synchronize adm files
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PlaneDriver.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_PlaneDriver.hxx>
26 #include <GEOMImpl_IPlane.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <GEOMUtils.hxx>
31
32 #include <Basics_OCCTVersion.hxx>
33
34 // OCCT Includes
35 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRepTopAdaptor_FClass2d.hxx>
38 #include <ShapeAnalysis.hxx>
39
40 #include <TopAbs.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopExp.hxx>
46
47 #include <GC_MakePlane.hxx>
48 #include <Geom_Surface.hxx>
49
50 #include <Precision.hxx>
51 #include <gp_Pnt.hxx>
52 #include <gp_Pln.hxx>
53 #include <gp_Vec.hxx>
54 #include <gp_Dir.hxx>
55 #include <gp_Ax3.hxx>
56
57 #include <Standard_ConstructionError.hxx>
58 #include <Standard_TypeMismatch.hxx>
59
60 //=======================================================================
61 //function : GetID
62 //purpose  :
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_PlaneDriver::GetID()
65 {
66   static Standard_GUID aPlaneDriver("FF1BBB05-5D14-4df2-980B-3A668264EA16");
67   return aPlaneDriver;
68 }
69
70
71 //=======================================================================
72 //function : GEOMImpl_PlaneDriver
73 //purpose  :
74 //=======================================================================
75 GEOMImpl_PlaneDriver::GEOMImpl_PlaneDriver()
76 {
77 }
78
79 //=======================================================================
80 //function : Execute
81 //purpose  :
82 //=======================================================================
83 Standard_Integer GEOMImpl_PlaneDriver::Execute(TFunction_Logbook& log) const
84 {
85   if (Label().IsNull())  return 0;
86   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
87
88   GEOMImpl_IPlane aPI (aFunction);
89   Standard_Integer aType = aFunction->GetType();
90
91   TopoDS_Shape aShape;
92
93   double aSize = aPI.GetSize() / 2.0;
94   if (aType == PLANE_PNT_VEC) {
95     Handle(GEOM_Function) aRefPnt = aPI.GetPoint();
96     Handle(GEOM_Function) aRefVec = aPI.GetVector();
97     TopoDS_Shape aShape1 = aRefPnt->GetValue();
98     TopoDS_Shape aShape2 = aRefVec->GetValue();
99     if (aShape1.ShapeType() != TopAbs_VERTEX ||
100         aShape2.ShapeType() != TopAbs_EDGE) return 0;
101     gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
102     TopoDS_Edge anE = TopoDS::Edge(aShape2);
103     TopoDS_Vertex V1, V2;
104     TopExp::Vertices(anE, V1, V2, Standard_True);
105     if (!V1.IsNull() && !V2.IsNull()) {
106       gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
107       gp_Pln aPln (aP, aV);
108       aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
109     }
110   } else if (aType == PLANE_THREE_PNT) {
111     Handle(GEOM_Function) aRefPnt1 = aPI.GetPoint1();
112     Handle(GEOM_Function) aRefPnt2 = aPI.GetPoint2();
113     Handle(GEOM_Function) aRefPnt3 = aPI.GetPoint3();
114     TopoDS_Shape aShape1 = aRefPnt1->GetValue();
115     TopoDS_Shape aShape2 = aRefPnt2->GetValue();
116     TopoDS_Shape aShape3 = aRefPnt3->GetValue();
117     if (aShape1.ShapeType() != TopAbs_VERTEX ||
118         aShape2.ShapeType() != TopAbs_VERTEX ||
119         aShape3.ShapeType() != TopAbs_VERTEX) return 0;
120     gp_Pnt aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aShape1));
121     gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aShape2));
122     gp_Pnt aP3 = BRep_Tool::Pnt(TopoDS::Vertex(aShape3));
123     if (aP1.Distance(aP2) < gp::Resolution() ||
124         aP1.Distance(aP3) < gp::Resolution() ||
125         aP2.Distance(aP3) < gp::Resolution())
126       Standard_ConstructionError::Raise("Plane creation aborted: coincident points given");
127     if (gp_Vec(aP1, aP2).IsParallel(gp_Vec(aP1, aP3), Precision::Angular()))
128       Standard_ConstructionError::Raise("Plane creation aborted: points lay on one line");
129     GC_MakePlane aMakePlane (aP1, aP2, aP3);
130 #if OCC_VERSION_LARGE > 0x06050100 // for OCC-6.5.2 and higher version
131     aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize,
132                                      Precision::Confusion()).Shape();
133 #else
134     aShape = BRepBuilderAPI_MakeFace(aMakePlane, -aSize, +aSize, -aSize, +aSize).Shape();
135 #endif
136   } else if (aType == PLANE_FACE) {
137     Handle(GEOM_Function) aRef = aPI.GetFace();
138     TopoDS_Shape aRefShape = aRef->GetValue();
139     //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
140     //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
141     //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
142     //  Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
143     //}
144     //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
145     gp_Ax3 anAx3 = GEOMUtils::GetPosition(aRefShape);
146     gp_Pln aPln (anAx3);
147     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
148   }
149   else if (aType == PLANE_TANGENT_FACE)
150   {
151     Handle(GEOM_Function) aRefFace = aPI.GetFace();
152     TopoDS_Shape aShape1 = aRefFace->GetValue();
153     if(aShape1.IsNull())
154       Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
155     TopoDS_Face aFace = TopoDS::Face(aShape1);
156
157     Standard_Real aKoefU = aPI.GetParameterU();
158     Standard_Real aKoefV = aPI.GetParameterV();
159     Standard_Real aUmin,aUmax,aVmin,aVmax;
160     ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
161     Standard_Real aDeltaU = aUmax - aUmin;
162     Standard_Real aDeltaV = aVmax - aVmin;
163     Standard_Real aParamU =  aUmin + aDeltaU*aKoefU;
164     Standard_Real aParamV =  aVmin + aDeltaV*aKoefV;
165     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
166     if(aSurf.IsNull())
167       Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
168     gp_Vec aVecU,aVecV;
169     gp_Pnt aPLoc;
170     aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
171     BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
172
173     TopAbs_State stOut= clas.PerformInfinitePoint();
174     gp_Pnt2d aP2d(aParamU,aParamV);
175     TopAbs_State st= clas.Perform(aP2d);
176     if(st == stOut)
177       Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
178     gp_Vec aNorm = aVecU^aVecV;
179     gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
180     gp_Pln aPlane(anAxis);
181     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
182     if(aTool.IsDone())
183       aShape = aTool.Shape();
184   }
185   else if (aType == PLANE_2_VEC) {
186     Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
187     Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
188     TopoDS_Shape aShape1 = aRefVec1->GetValue();
189     TopoDS_Shape aShape2 = aRefVec2->GetValue();
190     if (aShape1.ShapeType() != TopAbs_EDGE ||
191         aShape2.ShapeType() != TopAbs_EDGE) return 0;
192     TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
193     TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
194
195     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
196     TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
197     TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
198
199     gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
200     gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
201
202     if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
203       Standard_TypeMismatch::Raise("Invalid vector selected");
204
205     gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
206     gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
207
208     if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
209       Standard_TypeMismatch::Raise("Parallel vectors selected");
210
211     gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
212     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
213     if(aTool.IsDone())
214       aShape = aTool.Shape();
215   }   else if (aType == PLANE_LCS) {
216     Handle(GEOM_Function) aRef = aPI.GetLCS();
217     double anOrientation = aPI.GetOrientation();    
218     gp_Ax3 anAx3;
219     if (aRef.IsNull()) {
220       gp_Ax2 anAx2 = gp::XOY();
221       anAx3 = gp_Ax3( anAx2 );
222     } else {
223       TopoDS_Shape aRefShape = aRef->GetValue();
224       if (aRefShape.ShapeType() != TopAbs_FACE)
225         return 0;
226       anAx3 = GEOMUtils::GetPosition(aRefShape);
227     }
228
229     if ( anOrientation == 2)
230       anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
231     else if ( anOrientation == 3 )
232       anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
233
234     gp_Pln aPln(anAx3);
235     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
236   }
237   else {
238   }
239
240   if (aShape.IsNull()) return 0;
241
242   aFunction->SetValue(aShape);
243
244   log.SetTouched(Label());
245
246   return 1;
247 }
248
249
250 //================================================================================
251 /*!
252  * \brief Returns a name of creation operation and names and values of creation parameters
253  */
254 //================================================================================
255
256 bool GEOMImpl_PlaneDriver::
257 GetCreationInformation(std::string&             theOperationName,
258                        std::vector<GEOM_Param>& theParams)
259 {
260   if (Label().IsNull()) return 0;
261   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
262
263   GEOMImpl_IPlane aCI( function );
264   Standard_Integer aType = function->GetType();
265
266   theOperationName = "PLANE";
267
268   switch ( aType ) {
269   case PLANE_PNT_VEC:
270     AddParam( theParams, "Point", aCI.GetPoint() );
271     AddParam( theParams, "Vector", aCI.GetVector() );
272     AddParam( theParams, "Size of plane", aCI.GetSize() );
273     break;
274   case PLANE_THREE_PNT:
275     AddParam( theParams, "Point 1", aCI.GetPoint1() );
276     AddParam( theParams, "Point 2", aCI.GetPoint2() );
277     AddParam( theParams, "Point 3", aCI.GetPoint3() );
278     AddParam( theParams, "Size of plane", aCI.GetSize() );
279     break;
280   case PLANE_FACE:
281     AddParam( theParams, "Face", aCI.GetFace() );
282     AddParam( theParams, "Size of plane", aCI.GetSize() );
283     break;
284   case PLANE_TANGENT_FACE:
285     AddParam( theParams, "Face", aCI.GetFace() );
286     AddParam( theParams, "Parameter U", aCI.GetParameterU() );
287     AddParam( theParams, "Parameter V", aCI.GetParameterV() );
288     AddParam( theParams, "Size of plane", aCI.GetSize() );
289     break;
290   case PLANE_2_VEC:
291     AddParam( theParams, "Vector 1", aCI.GetVector1() );
292     AddParam( theParams, "Vector 2", aCI.GetVector2() );
293     AddParam( theParams, "Size of plane", aCI.GetSize() );
294     break;
295   case PLANE_LCS:
296     AddParam( theParams, "Local coordinate system", aCI.GetLCS() );
297     AddParam( theParams, "Orientation", aCI.GetOrientation() );
298     AddParam( theParams, "Size of plane", aCI.GetSize() );
299     break;
300   default:
301     return false;
302   }
303   
304   return true;
305 }
306
307 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_PlaneDriver,GEOM_BaseDriver);
308 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PlaneDriver,GEOM_BaseDriver);