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