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