Salome HOME
e8f31b4937617fe7dc20b36494b60e7702f74e4d
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PointDriver.cxx
1 //  Copyright (C) 2007-2008  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_PointDriver.hxx>
25 #include <GEOMImpl_IPoint.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <BRep_Tool.hxx>
30 #include <BRepBuilderAPI_MakeVertex.hxx>
31 #include <BRepExtrema_DistShapeShape.hxx>
32 #include <BRep_Builder.hxx>
33 #include <Precision.hxx>
34 #include <TopAbs.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopoDS_Compound.hxx>
40
41 #include <Geom_Curve.hxx>
42 #include <Geom_Surface.hxx>
43 #include <gp_Pnt.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <ShapeAnalysis.hxx>
46
47
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_PointDriver::GetID()
53 {
54   static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
55   return aPointDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_PointDriver
61 //purpose  :
62 //=======================================================================
63 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
64 {
65 }
66
67 //=======================================================================
68 //function : getExtremaSolution
69 //purpose  : local function
70 //=======================================================================
71 static Standard_Boolean getExtremaSolution
72 (GEOMImpl_IPoint& thePI,
73  TopoDS_Shape&    theRefShape,
74  gp_Pnt& thePnt)
75 {
76   gp_Pnt anInitPnt( thePI.GetX(), thePI.GetY(), thePI.GetZ() );
77   BRepBuilderAPI_MakeVertex mkVertex (anInitPnt);
78   TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
79   
80   BRepExtrema_DistShapeShape anExt( anInitV, theRefShape );
81   if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
82     return Standard_False;
83   thePnt = anExt.PointOnShape2(1);
84   Standard_Real aMinDist2 = anInitPnt.SquareDistance( thePnt );
85   for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
86   {
87     gp_Pnt aPnt = anExt.PointOnShape2(j);
88     Standard_Real aDist2 = anInitPnt.SquareDistance( aPnt );
89     if ( aDist2 > aMinDist2)
90       continue;
91     aMinDist2 = aDist2;
92     thePnt = aPnt;
93   }
94   return Standard_True;
95 }
96
97 //=======================================================================
98 //function : Execute
99 //purpose  :
100 //=======================================================================
101 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
102 {
103   if (Label().IsNull())  return 0;
104   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
105
106   GEOMImpl_IPoint aPI (aFunction);
107   Standard_Integer aType = aFunction->GetType();
108
109   gp_Pnt aPnt;
110   TopoDS_Compound aCompound;
111   bool retCompound = false;
112
113   if (aType == POINT_XYZ) {
114     aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
115
116   }
117   else if (aType == POINT_XYZ_REF) {
118
119     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
120     TopoDS_Shape aRefShape = aRefPoint->GetValue();
121     if (aRefShape.ShapeType() != TopAbs_VERTEX) {
122       Standard_TypeMismatch::Raise
123         ("Point creation aborted : referenced shape is not a vertex");
124     }
125     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
126     aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
127   }
128   else if (aType == POINT_CURVE_PAR) {
129     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
130     TopoDS_Shape aRefShape = aRefCurve->GetValue();
131     if (aRefShape.ShapeType() != TopAbs_EDGE) {
132       Standard_TypeMismatch::Raise
133         ("Point On Curve creation aborted : curve shape is not an edge");
134     }
135     Standard_Real aFP, aLP, aP;
136     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
137     aP = aFP + (aLP - aFP) * aPI.GetParameter();
138     aPnt = aCurve->Value(aP);
139   }
140   else if (aType == POINT_CURVE_COORD) {
141     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
142     TopoDS_Shape aRefShape = aRefCurve->GetValue();
143     if (aRefShape.ShapeType() != TopAbs_EDGE) {
144       Standard_TypeMismatch::Raise
145         ("Point On Curve creation aborted : curve shape is not an edge");
146     }
147     if (!getExtremaSolution( aPI, aRefShape, aPnt ) ) {
148       Standard_ConstructionError::Raise
149         ("Point On Curve creation aborted : cannot project point");
150     }
151   }
152   else if (aType == POINT_SURFACE_PAR) {
153     Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
154     TopoDS_Shape aRefShape = aRefCurve->GetValue();
155     if (aRefShape.ShapeType() != TopAbs_FACE) {
156       Standard_TypeMismatch::Raise
157         ("Point On Surface creation aborted : surface shape is not a face");
158     }
159     TopoDS_Face F = TopoDS::Face(aRefShape);
160     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
161     Standard_Real U1,U2,V1,V2;
162     //aSurf->Bounds(U1,U2,V1,V2);
163     ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
164     Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
165     Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
166     aPnt = aSurf->Value(U,V);
167   }
168   else if (aType == POINT_SURFACE_COORD) {
169     Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
170     TopoDS_Shape aRefShape = aRefCurve->GetValue();
171     if (aRefShape.ShapeType() != TopAbs_FACE) {
172       Standard_TypeMismatch::Raise
173         ("Point On Surface creation aborted : surface shape is not a face");
174     }
175     if (!getExtremaSolution( aPI, aRefShape, aPnt ) ) {
176       Standard_ConstructionError::Raise
177         ("Point On Surface creation aborted : cannot project point");
178     }
179   }
180   else if (aType == POINT_LINES_INTERSECTION) {
181     Handle(GEOM_Function) aRef1 = aPI.GetLine1();
182     Handle(GEOM_Function) aRef2 = aPI.GetLine2();
183
184     TopoDS_Shape aRefShape1 = aRef1->GetValue();
185     TopoDS_Shape aRefShape2 = aRef2->GetValue();
186
187     if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
188       || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
189       Standard_TypeMismatch::Raise
190         ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
191     }
192     //Calculate Lines Intersection Point
193     BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
194     if (dst.IsDone()) {
195       gp_Pnt P1, P2;
196       BRep_Builder B;
197       B.MakeCompound( aCompound );
198       for (int i = 1; i <= dst.NbSolution(); i++) {
199         P1 = dst.PointOnShape1(i);
200         P2 = dst.PointOnShape2(i);
201         Standard_Real Dist = P1.Distance(P2);
202         if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
203           BRepBuilderAPI_MakeVertex mkVertex (P1);
204           B.Add(aCompound, mkVertex.Shape());
205           retCompound = true;
206         } else if ( Dist <= Precision::Confusion() ) {
207           aPnt = P1;
208         } else {
209           Standard_TypeMismatch::Raise ("Shapes has not an Intersection Points");
210         }
211       }
212     }
213   }
214   else {
215     return 0;
216   }
217
218   TopoDS_Shape aShape;
219   if ( retCompound ) {
220     aShape = aCompound;
221   } else {
222     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
223     aShape = mkVertex.Shape();
224   }
225
226   aShape.Infinite(Standard_True);
227   aFunction->SetValue(aShape);
228
229   log.SetTouched(Label());
230
231   return 1;
232 }
233
234
235 //=======================================================================
236 //function :  GEOMImpl_PointDriver_Type_
237 //purpose  :
238 //=======================================================================
239 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_Type_()
240 {
241
242   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
243   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
244   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
245   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
246   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
247   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
248
249
250   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
251   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PointDriver",
252                                                          sizeof(GEOMImpl_PointDriver),
253                                                          1,
254                                                          (Standard_Address)_Ancestors,
255                                                          (Standard_Address)NULL);
256
257   return _aType;
258 }
259
260 //=======================================================================
261 //function : DownCast
262 //purpose  :
263 //=======================================================================
264
265 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
266 {
267   Handle(GEOMImpl_PointDriver) _anOtherObject;
268
269   if (!AnObject.IsNull()) {
270      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
271        _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
272      }
273   }
274
275   return _anOtherObject ;
276 }