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