1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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, or (at your option) any later version.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_PointDriver.hxx>
26 #include <GEOMImpl_IPoint.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29 #include <GEOMAlgo_AlgoTools.hxx>
31 #include <ShapeAnalysis.hxx>
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>
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>
49 #include <GCPnts_AbscissaPoint.hxx>
50 #include <IntTools.hxx>
52 #include <Geom_Curve.hxx>
53 #include <Geom_Surface.hxx>
57 #include <Precision.hxx>
59 #include <Standard_NullObject.hxx>
61 //=======================================================================
64 //=======================================================================
65 const Standard_GUID& GEOMImpl_PointDriver::GetID()
67 static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
72 //=======================================================================
73 //function : GEOMImpl_PointDriver
75 //=======================================================================
76 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
80 //=======================================================================
81 //function : getExtremaSolution
82 //purpose : local function
83 //=======================================================================
84 static Standard_Boolean getExtremaSolution
85 (const gp_Pnt& theInitPnt,
86 const TopoDS_Shape& theRefShape,
89 BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
90 TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
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++ )
99 gp_Pnt aPnt = anExt.PointOnShape2(j);
100 Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
101 if ( aDist2 > aMinDist2)
106 return Standard_True;
109 //=======================================================================
112 //=======================================================================
113 Standard_Integer GEOMImpl_PointDriver::Execute(LOGBOOK& log) const
115 if (Label().IsNull()) return 0;
116 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
118 GEOMImpl_IPoint aPI (aFunction);
119 Standard_Integer aType = aFunction->GetType();
122 TopoDS_Compound aCompound;
123 bool retCompound = false;
125 if (aType == POINT_XYZ) {
126 aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
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");
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());
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");
145 Standard_Real aFP, aLP, aP;
146 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
147 if ( !aCurve.IsNull() ) {
148 if (aPI.GetTakeOrientationIntoAccount() &&
149 aRefShape.Orientation() == TopAbs_REVERSED) {
150 aP = 1. - aPI.GetParameter();
152 aP = aPI.GetParameter();
155 aP = aFP + (aLP - aFP) * aP;
156 aPnt = aCurve->Value(aP);
159 // null curve, e.g. degenerated edge
160 TopoDS_Iterator It(aRefShape, Standard_False, Standard_False);
161 TopoDS_Vertex aVertex;
163 TopoDS_Shape aShape = It.Value();
164 if ( !aShape.IsNull() )
165 aVertex = TopoDS::Vertex( aShape );
167 if ( !aVertex.IsNull() ) {
168 aPnt = BRep_Tool::Pnt( aVertex );
171 Standard_TypeMismatch::Raise
172 ("Point On Curve creation aborted : null curve");
176 else if (aType == POINT_CURVE_COORD) {
177 Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
178 TopoDS_Shape aRefShape = aRefFunc->GetValue();
179 if (aRefShape.ShapeType() != TopAbs_EDGE) {
180 Standard_TypeMismatch::Raise
181 ("Point On Curve creation aborted : curve shape is not an edge");
183 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
184 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
185 Standard_ConstructionError::Raise
186 ("Point On Curve creation aborted : cannot project point");
189 else if (aType == POINT_CURVE_LENGTH) {
191 Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
192 if (aRefFunc.IsNull()) {
193 Standard_NullObject::Raise
194 ("Point On Curve creation aborted : curve object is null");
196 TopoDS_Shape aRefShape1 = aRefFunc->GetValue();
197 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
198 Standard_TypeMismatch::Raise
199 ("Point On Curve creation aborted : curve shape is not an edge");
201 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
202 TopoDS_Vertex V1, V2;
203 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
206 TopoDS_Vertex aRefVertex;
207 Handle(GEOM_Function) aRefPoint = aPI.GetRef();
208 if (aRefPoint.IsNull()) {
212 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
213 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
214 Standard_TypeMismatch::Raise
215 ("Point On Curve creation aborted : start point shape is not a vertex");
217 aRefVertex = TopoDS::Vertex(aRefShape2);
219 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
222 Standard_Real aLength = aPI.GetLength();
223 //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
224 //if (aLength > theCurveLength) {
225 // Standard_ConstructionError::Raise
226 // ("Point On Curve creation aborted : given length is greater than edges length");
230 Standard_Real UFirst, ULast;
231 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
233 if ( !EdgeCurve.IsNull() ) {
234 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
236 Standard_Real dU = ULast - UFirst;
237 Standard_Real par1 = UFirst + 0.1 * dU;
238 Standard_Real par2 = ULast - 0.1 * dU;
240 gp_Pnt P1 = EdgeCurve->Value(par1);
241 gp_Pnt P2 = EdgeCurve->Value(par2);
243 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
244 ReOrientedCurve = EdgeCurve->Reversed();
245 UFirst = EdgeCurve->ReversedParameter(ULast);
248 // Get the point by length
249 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
250 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
251 Standard_Real aParam = anAbsPnt.Parameter();
252 aPnt = AdapCurve.Value(aParam);
255 // null curve, e.g. degenerated edge
256 TopoDS_Iterator It(aRefEdge, Standard_False, Standard_False);
257 TopoDS_Vertex aVertex;
259 TopoDS_Shape aShape = It.Value();
260 if ( !aShape.IsNull() )
261 aVertex = TopoDS::Vertex( aShape );
263 if ( !aVertex.IsNull() ) {
264 aPnt = BRep_Tool::Pnt( aVertex );
267 Standard_TypeMismatch::Raise
268 ("Point On Curve creation aborted : null curve");
272 else if (aType == POINT_SURFACE_PAR) {
273 Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
274 TopoDS_Shape aRefShape = aRefFunc->GetValue();
275 if (aRefShape.ShapeType() != TopAbs_FACE) {
276 Standard_TypeMismatch::Raise
277 ("Point On Surface creation aborted : surface shape is not a face");
279 TopoDS_Face F = TopoDS::Face(aRefShape);
280 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
281 Standard_Real U1,U2,V1,V2;
282 //aSurf->Bounds(U1,U2,V1,V2);
283 ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
284 Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
285 Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
286 aPnt = aSurf->Value(U,V);
288 else if (aType == POINT_SURFACE_COORD) {
289 Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
290 TopoDS_Shape aRefShape = aRefFunc->GetValue();
291 if (aRefShape.ShapeType() != TopAbs_FACE) {
292 Standard_TypeMismatch::Raise
293 ("Point On Surface creation aborted : surface shape is not a face");
295 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
296 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
297 Standard_ConstructionError::Raise
298 ("Point On Surface creation aborted : cannot project point");
301 else if (aType == POINT_FACE_ANY) {
302 Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
303 TopoDS_Shape aRefShape = aRefFunc->GetValue();
304 if (aRefShape.ShapeType() != TopAbs_FACE) {
305 Standard_TypeMismatch::Raise
306 ("Point On Surface creation aborted : surface shape is not a face");
308 TopoDS_Face F = TopoDS::Face(aRefShape);
310 GEOMAlgo_AlgoTools::PntInFace(F, aPnt, aP2d);
312 else if (aType == POINT_LINES_INTERSECTION) {
313 Handle(GEOM_Function) aRef1 = aPI.GetLine1();
314 Handle(GEOM_Function) aRef2 = aPI.GetLine2();
316 TopoDS_Shape aRefShape1 = aRef1->GetValue();
317 TopoDS_Shape aRefShape2 = aRef2->GetValue();
319 if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
320 || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
321 Standard_TypeMismatch::Raise
322 ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
325 if (aRefShape1.IsSame(aRefShape2))
326 Standard_ConstructionError::Raise("The lines to make intersection must be different");
328 //Calculate Lines Intersection Point
329 BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
333 B.MakeCompound( aCompound );
334 for (int i = 1; i <= dst.NbSolution(); i++) {
335 P1 = dst.PointOnShape1(i);
336 P2 = dst.PointOnShape2(i);
337 Standard_Real Dist = P1.Distance(P2);
338 if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
339 BRepBuilderAPI_MakeVertex mkVertex (P1);
340 B.Add(aCompound, mkVertex.Shape());
342 } else if ( Dist <= Precision::Confusion() ) {
345 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
358 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
359 aShape = mkVertex.Shape();
362 //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
363 aFunction->SetValue(aShape);
365 #if OCC_VERSION_MAJOR < 7
366 log.SetTouched(Label());
368 log->SetTouched(Label());
374 //================================================================================
376 * \brief Returns a name of creation operation and names and values of creation parameters
378 //================================================================================
380 bool GEOMImpl_PointDriver::
381 GetCreationInformation(std::string& theOperationName,
382 std::vector<GEOM_Param>& theParams)
384 if (Label().IsNull()) return 0;
385 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
387 GEOMImpl_IPoint aCI( function );
388 Standard_Integer aType = function->GetType();
390 theOperationName = "POINT";
394 AddParam( theParams, "X", aCI.GetX() );
395 AddParam( theParams, "Y", aCI.GetY() );
396 AddParam( theParams, "Z", aCI.GetZ() );
399 AddParam( theParams, "Point", aCI.GetRef() );
400 AddParam( theParams, "Dx", aCI.GetX() );
401 AddParam( theParams, "Dy", aCI.GetY() );
402 AddParam( theParams, "Dz", aCI.GetZ() );
404 case POINT_CURVE_PAR:
405 AddParam( theParams, "Edge", aCI.GetCurve() );
406 AddParam( theParams, "Parameter", aCI.GetParameter() );
407 AddParam( theParams, "Use Orientation", aCI.GetTakeOrientationIntoAccount() );
409 case POINT_CURVE_COORD:
410 AddParam( theParams, "X", aCI.GetX() );
411 AddParam( theParams, "Y", aCI.GetY() );
412 AddParam( theParams, "Z", aCI.GetZ() );
413 AddParam( theParams, "Edge", aCI.GetCurve() );
415 case POINT_CURVE_LENGTH:
416 AddParam( theParams, "Edge", aCI.GetCurve() );
417 AddParam( theParams, "Start Point", aCI.GetRef(), "First Vertex" );
418 AddParam( theParams, "Length", aCI.GetLength() );
420 case POINT_SURFACE_PAR:
421 AddParam( theParams, "Face", aCI.GetSurface() );
422 AddParam( theParams, "U-Parameter", aCI.GetParameter() );
423 AddParam( theParams, "V-Parameter", aCI.GetParameter2() );
425 case POINT_SURFACE_COORD:
426 AddParam( theParams, "X", aCI.GetX() );
427 AddParam( theParams, "Y", aCI.GetY() );
428 AddParam( theParams, "Z", aCI.GetZ() );
429 AddParam( theParams, "Face", aCI.GetSurface() );
432 AddParam( theParams, "Face", aCI.GetSurface() );
434 case POINT_LINES_INTERSECTION:
435 AddParam( theParams, "Line 1", aCI.GetLine1() );
436 AddParam( theParams, "Line 2", aCI.GetLine2() );
445 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PointDriver,GEOM_BaseDriver);