]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_PlaneDriver.cxx
Salome HOME
Merge remote branch 'origin/hydro/imps_2015'
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PlaneDriver.cxx
1 // Copyright (C) 2007-2016  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(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     aShape = BRepBuilderAPI_MakeFace(aMakePlane.Value(),
131                                      -aSize, +aSize, -aSize, +aSize,
132                                      Precision::Confusion()).Shape();
133   } else if (aType == PLANE_FACE) {
134     Handle(GEOM_Function) aRef = aPI.GetFace();
135     TopoDS_Shape aRefShape = aRef->GetValue();
136     //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
137     //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
138     //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
139     //  Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
140     //}
141     //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
142     gp_Ax3 anAx3 = GEOMUtils::GetPosition(aRefShape);
143     gp_Pln aPln (anAx3);
144     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
145   }
146   else if (aType == PLANE_TANGENT_FACE)
147   {
148     Handle(GEOM_Function) aRefFace = aPI.GetFace();
149     TopoDS_Shape aShape1 = aRefFace->GetValue();
150     if(aShape1.IsNull())
151       Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
152     TopoDS_Face aFace = TopoDS::Face(aShape1);
153
154     Standard_Real aKoefU = aPI.GetParameterU();
155     Standard_Real aKoefV = aPI.GetParameterV();
156     Standard_Real aUmin,aUmax,aVmin,aVmax;
157     ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
158     Standard_Real aDeltaU = aUmax - aUmin;
159     Standard_Real aDeltaV = aVmax - aVmin;
160     Standard_Real aParamU =  aUmin + aDeltaU*aKoefU;
161     Standard_Real aParamV =  aVmin + aDeltaV*aKoefV;
162     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
163     if(aSurf.IsNull())
164       Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
165     gp_Vec aVecU,aVecV;
166     gp_Pnt aPLoc;
167     aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
168     BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
169
170     TopAbs_State stOut= clas.PerformInfinitePoint();
171     gp_Pnt2d aP2d(aParamU,aParamV);
172     TopAbs_State st= clas.Perform(aP2d);
173     if(st == stOut)
174       Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
175     gp_Vec aNorm = aVecU^aVecV;
176     gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
177     gp_Pln aPlane(anAxis);
178     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
179     if(aTool.IsDone())
180       aShape = aTool.Shape();
181   }
182   else if (aType == PLANE_2_VEC) {
183     Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
184     Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
185     TopoDS_Shape aShape1 = aRefVec1->GetValue();
186     TopoDS_Shape aShape2 = aRefVec2->GetValue();
187     if (aShape1.ShapeType() != TopAbs_EDGE ||
188         aShape2.ShapeType() != TopAbs_EDGE) return 0;
189     TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
190     TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
191
192     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
193     TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
194     TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
195
196     gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
197     gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
198
199     if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
200       Standard_TypeMismatch::Raise("Invalid vector selected");
201
202     gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
203     gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
204
205     if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
206       Standard_TypeMismatch::Raise("Parallel vectors selected");
207
208     gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
209     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
210     if(aTool.IsDone())
211       aShape = aTool.Shape();
212   }   else if (aType == PLANE_LCS) {
213     Handle(GEOM_Function) aRef = aPI.GetLCS();
214     double anOrientation = aPI.GetOrientation();    
215     gp_Ax3 anAx3;
216     if (aRef.IsNull()) {
217       gp_Ax2 anAx2 = gp::XOY();
218       anAx3 = gp_Ax3( anAx2 );
219     } else {
220       TopoDS_Shape aRefShape = aRef->GetValue();
221       if (aRefShape.ShapeType() != TopAbs_FACE)
222         return 0;
223       anAx3 = GEOMUtils::GetPosition(aRefShape);
224     }
225
226     if ( anOrientation == 2)
227       anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
228     else if ( anOrientation == 3 )
229       anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
230
231     gp_Pln aPln(anAx3);
232     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
233   }
234   else {
235   }
236
237   if (aShape.IsNull()) return 0;
238
239   aFunction->SetValue(aShape);
240
241 #if OCC_VERSION_MAJOR < 7
242   log.SetTouched(Label());
243 #else
244   log->SetTouched(Label());
245 #endif
246
247   return 1;
248 }
249
250
251 //================================================================================
252 /*!
253  * \brief Returns a name of creation operation and names and values of creation parameters
254  */
255 //================================================================================
256
257 bool GEOMImpl_PlaneDriver::
258 GetCreationInformation(std::string&             theOperationName,
259                        std::vector<GEOM_Param>& theParams)
260 {
261   if (Label().IsNull()) return 0;
262   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
263
264   GEOMImpl_IPlane aCI( function );
265   Standard_Integer aType = function->GetType();
266
267   theOperationName = "PLANE";
268
269   switch ( aType ) {
270   case PLANE_PNT_VEC:
271     AddParam( theParams, "Point", aCI.GetPoint() );
272     AddParam( theParams, "Vector", aCI.GetVector() );
273     AddParam( theParams, "Size of plane", aCI.GetSize() );
274     break;
275   case PLANE_THREE_PNT:
276     AddParam( theParams, "Point 1", aCI.GetPoint1() );
277     AddParam( theParams, "Point 2", aCI.GetPoint2() );
278     AddParam( theParams, "Point 3", aCI.GetPoint3() );
279     AddParam( theParams, "Size of plane", aCI.GetSize() );
280     break;
281   case PLANE_FACE:
282     AddParam( theParams, "Face", aCI.GetFace() );
283     AddParam( theParams, "Size of plane", aCI.GetSize() );
284     break;
285   case PLANE_TANGENT_FACE:
286     AddParam( theParams, "Face", aCI.GetFace() );
287     AddParam( theParams, "Parameter U", aCI.GetParameterU() );
288     AddParam( theParams, "Parameter V", aCI.GetParameterV() );
289     AddParam( theParams, "Size of plane", aCI.GetSize() );
290     break;
291   case PLANE_2_VEC:
292     AddParam( theParams, "Vector 1", aCI.GetVector1() );
293     AddParam( theParams, "Vector 2", aCI.GetVector2() );
294     AddParam( theParams, "Size of plane", aCI.GetSize() );
295     break;
296   case PLANE_LCS:
297     AddParam( theParams, "Local coordinate system", aCI.GetLCS() );
298     AddParam( theParams, "Orientation", aCI.GetOrientation() );
299     AddParam( theParams, "Size of plane", aCI.GetSize() );
300     break;
301   default:
302     return false;
303   }
304   
305   return true;
306 }
307
308 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PlaneDriver,GEOM_BaseDriver);