Salome HOME
0021684: EDF 2221 : Display the arguments and the name of the operations
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PointDriver.cxx
1 // Copyright (C) 2007-2013  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 #include <GEOMAlgo_Tools3D.hxx>
30
31 #include <ShapeAnalysis.hxx>
32
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <BRepBuilderAPI_MakeVertex.hxx>
37 #include <BRepExtrema_DistShapeShape.hxx>
38
39 #include <TopAbs.hxx>
40 #include <TopExp.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_Iterator.hxx>
48
49 #include <GCPnts_AbscissaPoint.hxx>
50 #include <IntTools.hxx>
51
52 #include <Geom_Curve.hxx>
53 #include <Geom_Surface.hxx>
54
55 #include <gp_Pnt.hxx>
56
57 #include <Precision.hxx>
58
59 #include <Standard_NullObject.hxx>
60
61 //=======================================================================
62 //function : GetID
63 //purpose  :
64 //=======================================================================
65 const Standard_GUID& GEOMImpl_PointDriver::GetID()
66 {
67   static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
68   return aPointDriver;
69 }
70
71
72 //=======================================================================
73 //function : GEOMImpl_PointDriver
74 //purpose  :
75 //=======================================================================
76 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
77 {
78 }
79
80 //=======================================================================
81 //function : getExtremaSolution
82 //purpose  : local function
83 //=======================================================================
84 static Standard_Boolean getExtremaSolution
85 (const gp_Pnt&       theInitPnt,
86  const TopoDS_Shape& theRefShape,
87  gp_Pnt& thePnt)
88 {
89   BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
90   TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
91   
92   BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
93   if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
94     return Standard_False;
95   thePnt = anExt.PointOnShape2(1);
96   Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
97   for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
98   {
99     gp_Pnt aPnt = anExt.PointOnShape2(j);
100     Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
101     if ( aDist2 > aMinDist2)
102       continue;
103     aMinDist2 = aDist2;
104     thePnt = aPnt;
105   }
106   return Standard_True;
107 }
108
109 //=======================================================================
110 //function : Execute
111 //purpose  :
112 //=======================================================================
113 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
114 {
115   if (Label().IsNull())  return 0;
116   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
117
118   GEOMImpl_IPoint aPI (aFunction);
119   Standard_Integer aType = aFunction->GetType();
120
121   gp_Pnt aPnt;
122   TopoDS_Compound aCompound;
123   bool retCompound = false;
124
125   if (aType == POINT_XYZ) {
126     aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
127   }
128   else if (aType == POINT_XYZ_REF) {
129     Handle(GEOM_Function) aRefFunc = aPI.GetRef();
130     TopoDS_Shape aRefShape = aRefFunc->GetValue();
131     if (aRefShape.ShapeType() != TopAbs_VERTEX) {
132       Standard_TypeMismatch::Raise
133         ("Point creation aborted : referenced shape is not a vertex");
134     }
135     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
136     aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
137   }
138   else if (aType == POINT_CURVE_PAR) {
139     Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
140     TopoDS_Shape aRefShape = aRefFunc->GetValue();
141     if (aRefShape.ShapeType() != TopAbs_EDGE) {
142       Standard_TypeMismatch::Raise
143         ("Point On Curve creation aborted : curve shape is not an edge");
144     }
145     Standard_Real aFP, aLP, aP;
146     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
147     if ( !aCurve.IsNull() ) {
148       aP = aFP + (aLP - aFP) * aPI.GetParameter();
149       aPnt = aCurve->Value(aP);
150     }
151     else {
152       // null curve, e.g. degenerated edge
153       TopoDS_Iterator It(aRefShape, Standard_False, Standard_False);
154       TopoDS_Vertex aVertex;
155       if ( It.More() ) {
156         TopoDS_Shape aShape = It.Value();
157         if ( !aShape.IsNull() )
158           aVertex = TopoDS::Vertex( aShape );
159       }
160       if ( !aVertex.IsNull() ) {
161         aPnt = BRep_Tool::Pnt( aVertex );
162       }
163       else {
164         Standard_TypeMismatch::Raise
165           ("Point On Curve creation aborted : null curve");
166       }
167     }
168   }
169   else if (aType == POINT_CURVE_COORD) {
170     Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
171     TopoDS_Shape aRefShape = aRefFunc->GetValue();
172     if (aRefShape.ShapeType() != TopAbs_EDGE) {
173       Standard_TypeMismatch::Raise
174         ("Point On Curve creation aborted : curve shape is not an edge");
175     }
176     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
177     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
178       Standard_ConstructionError::Raise
179         ("Point On Curve creation aborted : cannot project point");
180     }
181   }
182   else if (aType == POINT_CURVE_LENGTH) {
183     // RefCurve
184     Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
185     if (aRefFunc.IsNull()) {
186       Standard_NullObject::Raise
187         ("Point On Curve creation aborted : curve object is null");
188     }
189     TopoDS_Shape aRefShape1 = aRefFunc->GetValue();
190     if (aRefShape1.ShapeType() != TopAbs_EDGE) {
191       Standard_TypeMismatch::Raise
192         ("Point On Curve creation aborted : curve shape is not an edge");
193     }
194     TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
195     TopoDS_Vertex V1, V2;
196     TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
197
198     // RefPoint
199     TopoDS_Vertex aRefVertex;
200     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
201     if (aRefPoint.IsNull()) {
202       aRefVertex = V1;
203     }
204     else {
205       TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
206       if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
207         Standard_TypeMismatch::Raise
208           ("Point On Curve creation aborted : start point shape is not a vertex");
209       }
210       aRefVertex = TopoDS::Vertex(aRefShape2);
211     }
212     gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
213
214     // Length
215     Standard_Real aLength = aPI.GetLength();
216     //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
217     //if (aLength > theCurveLength) {
218     //  Standard_ConstructionError::Raise
219     //    ("Point On Curve creation aborted : given length is greater than edges length");
220     //}
221
222     // Check orientation
223     Standard_Real UFirst, ULast;
224     Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
225
226     if ( !EdgeCurve.IsNull() ) {
227       Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
228
229       Standard_Real dU = ULast - UFirst;
230       Standard_Real par1 = UFirst + 0.1 * dU;
231       Standard_Real par2 = ULast  - 0.1 * dU;
232       
233       gp_Pnt P1 = EdgeCurve->Value(par1);
234       gp_Pnt P2 = EdgeCurve->Value(par2);
235       
236       if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
237         ReOrientedCurve = EdgeCurve->Reversed();
238         UFirst = EdgeCurve->ReversedParameter(ULast);
239       }
240       
241       // Get the point by length
242       GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
243       GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst); 
244       Standard_Real aParam = anAbsPnt.Parameter();
245       aPnt = AdapCurve.Value(aParam);
246     }
247     else {
248       // null curve, e.g. degenerated edge
249       TopoDS_Iterator It(aRefEdge, Standard_False, Standard_False);
250       TopoDS_Vertex aVertex;
251       if ( It.More() ) {
252         TopoDS_Shape aShape = It.Value();
253         if ( !aShape.IsNull() )
254           aVertex = TopoDS::Vertex( aShape );
255       }
256       if ( !aVertex.IsNull() ) {
257         aPnt = BRep_Tool::Pnt( aVertex );
258       }
259       else {
260         Standard_TypeMismatch::Raise
261           ("Point On Curve creation aborted : null curve");
262       }
263     }
264   }
265   else if (aType == POINT_SURFACE_PAR) {
266     Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
267     TopoDS_Shape aRefShape = aRefFunc->GetValue();
268     if (aRefShape.ShapeType() != TopAbs_FACE) {
269       Standard_TypeMismatch::Raise
270         ("Point On Surface creation aborted : surface shape is not a face");
271     }
272     TopoDS_Face F = TopoDS::Face(aRefShape);
273     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
274     Standard_Real U1,U2,V1,V2;
275     //aSurf->Bounds(U1,U2,V1,V2);
276     ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
277     Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
278     Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
279     aPnt = aSurf->Value(U,V);
280   }
281   else if (aType == POINT_SURFACE_COORD) {
282     Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
283     TopoDS_Shape aRefShape = aRefFunc->GetValue();
284     if (aRefShape.ShapeType() != TopAbs_FACE) {
285       Standard_TypeMismatch::Raise
286         ("Point On Surface creation aborted : surface shape is not a face");
287     }
288     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
289     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
290       Standard_ConstructionError::Raise
291         ("Point On Surface creation aborted : cannot project point");
292     }
293   }
294   else if (aType == POINT_FACE_ANY) {
295     Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
296     TopoDS_Shape aRefShape = aRefFunc->GetValue();
297     if (aRefShape.ShapeType() != TopAbs_FACE) {
298       Standard_TypeMismatch::Raise
299         ("Point On Surface creation aborted : surface shape is not a face");
300     }
301     TopoDS_Face F = TopoDS::Face(aRefShape);
302     gp_Pnt2d aP2d;
303     GEOMAlgo_Tools3D::PntInFace(F, aPnt, aP2d);
304   }
305   else if (aType == POINT_LINES_INTERSECTION) {
306     Handle(GEOM_Function) aRef1 = aPI.GetLine1();
307     Handle(GEOM_Function) aRef2 = aPI.GetLine2();
308
309     TopoDS_Shape aRefShape1 = aRef1->GetValue();
310     TopoDS_Shape aRefShape2 = aRef2->GetValue();
311
312     if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
313       || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
314       Standard_TypeMismatch::Raise
315         ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
316     }
317
318     if (aRefShape1.IsSame(aRefShape2))
319       Standard_ConstructionError::Raise("The lines to make intersection must be different");
320
321     //Calculate Lines Intersection Point
322     BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
323     if (dst.IsDone()) {
324       gp_Pnt P1, P2;
325       BRep_Builder B;
326       B.MakeCompound( aCompound );
327       for (int i = 1; i <= dst.NbSolution(); i++) {
328         P1 = dst.PointOnShape1(i);
329         P2 = dst.PointOnShape2(i);
330         Standard_Real Dist = P1.Distance(P2);
331         if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
332           BRepBuilderAPI_MakeVertex mkVertex (P1);
333           B.Add(aCompound, mkVertex.Shape());
334           retCompound = true;
335         } else if ( Dist <= Precision::Confusion() ) {
336           aPnt = P1;
337         } else {
338           Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
339         }
340       }
341     }
342   }
343   else {
344     return 0;
345   }
346
347   TopoDS_Shape aShape;
348   if ( retCompound ) {
349     aShape = aCompound;
350   } else {
351     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
352     aShape = mkVertex.Shape();
353   }
354
355   //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
356   aFunction->SetValue(aShape);
357
358   log.SetTouched(Label());
359
360   return 1;
361 }
362
363 //================================================================================
364 /*!
365  * \brief Returns a name of creation operation and names and values of creation parameters
366  */
367 //================================================================================
368
369 bool GEOMImpl_PointDriver::
370 GetCreationInformation(std::string&             theOperationName,
371                        std::vector<GEOM_Param>& theParams)
372 {
373   if (Label().IsNull()) return 0;
374   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
375
376   GEOMImpl_IPoint aCI( function );
377   Standard_Integer aType = function->GetType();
378
379   theOperationName = "POINT";
380
381   switch ( aType ) {
382   case POINT_XYZ:
383     AddParam( theParams, "X", aCI.GetX() );
384     AddParam( theParams, "Y", aCI.GetY() );
385     AddParam( theParams, "Z", aCI.GetZ() );
386     break;
387   case POINT_XYZ_REF:
388     AddParam( theParams, "Point", aCI.GetRef() );
389     AddParam( theParams, "Dx", aCI.GetX() );
390     AddParam( theParams, "Dy", aCI.GetY() );
391     AddParam( theParams, "Dz", aCI.GetZ() );
392     break;
393   case POINT_CURVE_PAR:
394     AddParam( theParams, "Edge", aCI.GetCurve() );
395     AddParam( theParams, "Parameter", aCI.GetParameter() );
396     break;
397   case POINT_CURVE_COORD:
398     AddParam( theParams, "X", aCI.GetX() );
399     AddParam( theParams, "Y", aCI.GetY() );
400     AddParam( theParams, "Z", aCI.GetZ() );
401     AddParam( theParams, "Edge", aCI.GetCurve() );
402     break;
403   case POINT_CURVE_LENGTH:
404     AddParam( theParams, "Edge", aCI.GetCurve() );
405     AddParam( theParams, "Start Point", aCI.GetRef(), "First Vertex" );
406     AddParam( theParams, "Length", aCI.GetLength() );
407     break;
408   case POINT_SURFACE_PAR:
409     AddParam( theParams, "Face", aCI.GetSurface() );
410     AddParam( theParams, "U-Parameter", aCI.GetParameter() );
411     AddParam( theParams, "V-Parameter", aCI.GetParameter2() );
412     break;
413   case POINT_SURFACE_COORD:
414     AddParam( theParams, "X", aCI.GetX() );
415     AddParam( theParams, "Y", aCI.GetY() );
416     AddParam( theParams, "Z", aCI.GetZ() );
417     AddParam( theParams, "Face", aCI.GetSurface() );
418     break;
419   case POINT_FACE_ANY:
420     AddParam( theParams, "Face", aCI.GetSurface() );
421     break;
422   case POINT_LINES_INTERSECTION:
423     AddParam( theParams, "Line 1", aCI.GetLine1() );
424     AddParam( theParams, "Line 2", aCI.GetLine2() );
425     break;
426   default:
427     return false;
428   }
429
430   return true;
431 }
432
433 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_PointDriver,GEOM_BaseDriver);
434 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PointDriver,GEOM_BaseDriver);