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