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