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