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