Salome HOME
0021031: EDF 1646 GEOM: Modify Location leads to Position driver failed
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PlaneDriver.cxx
1 //  Copyright (C) 2007-2010  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
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 <GEOMImpl_IMeasureOperations.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(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, -aSize, +aSize, -aSize, +aSize).Shape();
129   } else if (aType == PLANE_FACE) {
130     Handle(GEOM_Function) aRef = aPI.GetFace();
131     TopoDS_Shape aRefShape = aRef->GetValue();
132     //if (aRefShape.ShapeType() != TopAbs_FACE) return 0;
133     //Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aRefShape));
134     //if (!aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
135     //  Standard_TypeMismatch::Raise("Plane creation aborted: non-planar face given as argument");
136     //}
137     //aShape = BRepBuilderAPI_MakeFace(aGS, -aSize, +aSize, -aSize, +aSize).Shape();
138     gp_Ax3 anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
139     gp_Pln aPln (anAx3);
140     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
141   }
142   else if (aType == PLANE_TANGENT_FACE)
143   {
144     Handle(GEOM_Function) aRefFace = aPI.GetFace();
145     TopoDS_Shape aShape1 = aRefFace->GetValue();
146     if(aShape1.IsNull())
147       Standard_TypeMismatch::Raise("Plane was not created.Basis face was not specified");
148     TopoDS_Face aFace = TopoDS::Face(aShape1);
149
150     Standard_Real aKoefU = aPI.GetParameterU();
151     Standard_Real aKoefV = aPI.GetParameterV();
152     Standard_Real aUmin,aUmax,aVmin,aVmax;
153     ShapeAnalysis::GetFaceUVBounds(aFace,aUmin,aUmax,aVmin,aVmax);
154     Standard_Real aDeltaU = aUmax - aUmin;
155     Standard_Real aDeltaV = aVmax - aVmin;
156     Standard_Real aParamU =  aUmin + aDeltaU*aKoefU;
157     Standard_Real aParamV =  aVmin + aDeltaV*aKoefV;
158     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
159     if(aSurf.IsNull())
160       Standard_TypeMismatch::Raise("Plane was not created.Base surface is absent");
161     gp_Vec aVecU,aVecV;
162     gp_Pnt aPLoc;
163     aSurf->D1(aParamU,aParamV,aPLoc,aVecU,aVecV);
164     BRepTopAdaptor_FClass2d clas(aFace,Precision::PConfusion());
165
166     TopAbs_State stOut= clas.PerformInfinitePoint();
167     gp_Pnt2d aP2d(aParamU,aParamV);
168     TopAbs_State st= clas.Perform(aP2d);
169     if(st == stOut)
170       Standard_TypeMismatch::Raise("Plane was not created.Point lies outside the face");
171     gp_Vec aNorm = aVecU^aVecV;
172     gp_Ax3 anAxis(aPLoc,gp_Dir(aNorm),gp_Dir(aVecU));
173     gp_Pln aPlane(anAxis);
174     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
175     if(aTool.IsDone())
176       aShape = aTool.Shape();
177   }
178   else if (aType == PLANE_2_VEC) {
179     Handle(GEOM_Function) aRefVec1 = aPI.GetVector1();
180     Handle(GEOM_Function) aRefVec2 = aPI.GetVector2();
181     TopoDS_Shape aShape1 = aRefVec1->GetValue();
182     TopoDS_Shape aShape2 = aRefVec2->GetValue();
183     if (aShape1.ShapeType() != TopAbs_EDGE ||
184         aShape2.ShapeType() != TopAbs_EDGE) return 0;
185     TopoDS_Edge aVectX = TopoDS::Edge(aShape1);
186     TopoDS_Edge aVectZ = TopoDS::Edge(aShape2);
187
188     TopoDS_Vertex VX1, VX2, VZ1, VZ2;
189     TopExp::Vertices( aVectX, VX1, VX2, Standard_True );
190     TopExp::Vertices( aVectZ, VZ1, VZ2, Standard_True );
191
192     gp_Vec aVX = gp_Vec( BRep_Tool::Pnt( VX1 ), BRep_Tool::Pnt( VX2 ) );
193     gp_Vec aVZ = gp_Vec( BRep_Tool::Pnt( VZ1 ), BRep_Tool::Pnt( VZ2 ) );
194
195     if ( aVX.Magnitude() < Precision::Confusion() || aVZ.Magnitude() < Precision::Confusion())
196       Standard_TypeMismatch::Raise("Invalid vector selected");
197
198     gp_Dir aDirX = gp_Dir( aVX.X(), aVX.Y(), aVX.Z() );
199     gp_Dir aDirZ = gp_Dir( aVZ.X(), aVZ.Y(), aVZ.Z() );
200
201     if ( aDirX.IsParallel( aDirZ, Precision::Angular() ) )
202       Standard_TypeMismatch::Raise("Parallel vectors selected");
203
204     gp_Ax3 aPlane = gp_Ax3( BRep_Tool::Pnt( VX1 ), aDirZ, aDirX );
205     BRepBuilderAPI_MakeFace aTool(aPlane, -aSize, +aSize, -aSize, +aSize);
206     if(aTool.IsDone())
207       aShape = aTool.Shape();
208   }   else if (aType == PLANE_LCS) {
209     Handle(GEOM_Function) aRef = aPI.GetLCS();
210     double anOrientation = aPI.GetOrientation();    
211     gp_Ax3 anAx3;
212     if (aRef.IsNull()) {
213       gp_Ax2 anAx2 = gp::XOY();
214       anAx3 = gp_Ax3( anAx2 );
215     } else {
216       TopoDS_Shape aRefShape = aRef->GetValue();
217       if (aRefShape.ShapeType() != TopAbs_FACE)
218         return 0;
219       anAx3 = GEOMImpl_IMeasureOperations::GetPosition(aRefShape);
220     }
221
222     if ( anOrientation == 2)
223       anAx3 = gp_Ax3(anAx3.Location(), anAx3.XDirection(), anAx3.YDirection() );
224     else if ( anOrientation == 3 )
225       anAx3 = gp_Ax3(anAx3.Location(), anAx3.YDirection(), anAx3.XDirection() );
226
227     gp_Pln aPln(anAx3);
228     aShape = BRepBuilderAPI_MakeFace(aPln, -aSize, +aSize, -aSize, +aSize).Shape();
229   }
230   else {
231   }
232
233   if (aShape.IsNull()) return 0;
234
235   aFunction->SetValue(aShape);
236
237   log.SetTouched(Label());
238
239   return 1;
240 }
241
242
243 //=======================================================================
244 //function :  GEOMImpl_PlaneDriver_Type_
245 //purpose  :
246 //=======================================================================
247 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PlaneDriver_Type_()
248 {
249
250   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
251   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
252   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
253   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
254   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
255   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
256
257
258   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
259   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PlaneDriver",
260                                                          sizeof(GEOMImpl_PlaneDriver),
261                                                          1,
262                                                          (Standard_Address)_Ancestors,
263                                                          (Standard_Address)NULL);
264
265   return _aType;
266 }
267
268 //=======================================================================
269 //function : DownCast
270 //purpose  :
271 //=======================================================================
272 const Handle(GEOMImpl_PlaneDriver) Handle(GEOMImpl_PlaneDriver)::DownCast
273        (const Handle(Standard_Transient)& AnObject)
274 {
275   Handle(GEOMImpl_PlaneDriver) _anOtherObject;
276
277   if (!AnObject.IsNull()) {
278      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PlaneDriver))) {
279        _anOtherObject = Handle(GEOMImpl_PlaneDriver)((Handle(GEOMImpl_PlaneDriver)&)AnObject);
280      }
281   }
282
283   return _anOtherObject ;
284 }