1 // Copyright (C) 2007-2022 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 <Basics_OCCTVersion.hxx>
27 #include <GEOMImpl_PointDriver.hxx>
28 #include <GEOMImpl_IPoint.hxx>
29 #include <GEOMImpl_Types.hxx>
30 #include <GEOM_Function.hxx>
31 #include <GEOMAlgo_AlgoTools.hxx>
33 #include <ShapeAnalysis.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRepAdaptor_Curve.hxx>
38 #include <BRepBuilderAPI_MakeVertex.hxx>
39 #include <BRepExtrema_DistShapeShape.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopoDS_Vertex.hxx>
48 #include <TopoDS_Compound.hxx>
49 #include <TopoDS_Iterator.hxx>
51 #include <GCPnts_AbscissaPoint.hxx>
52 #include <IntTools.hxx>
54 #include <Geom_Curve.hxx>
55 #include <Geom_Surface.hxx>
59 #include <Precision.hxx>
61 #include <Standard_NullObject.hxx>
62 #include <Standard_NotImplemented.hxx>
64 //=======================================================================
67 //=======================================================================
68 const Standard_GUID& GEOMImpl_PointDriver::GetID()
70 static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
75 //=======================================================================
76 //function : GEOMImpl_PointDriver
78 //=======================================================================
79 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
83 //=======================================================================
84 //function : getExtremaSolution
85 //purpose : local function
86 //=======================================================================
87 static Standard_Boolean getExtremaSolution
88 (const gp_Pnt& theInitPnt,
89 const TopoDS_Shape& theRefShape,
92 BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
93 TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
95 BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
96 if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
97 return Standard_False;
98 thePnt = anExt.PointOnShape2(1);
99 Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
100 for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
102 gp_Pnt aPnt = anExt.PointOnShape2(j);
103 Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
104 if ( aDist2 > aMinDist2)
109 return Standard_True;
112 //=======================================================================
115 //=======================================================================
116 Standard_Integer GEOMImpl_PointDriver::Execute(Handle(TFunction_Logbook)& log) const
118 if (Label().IsNull()) return 0;
119 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
121 GEOMImpl_IPoint aPI (aFunction);
122 Standard_Integer aType = aFunction->GetType();
125 TopoDS_Compound aCompound;
126 bool retCompound = false;
128 if (aType == POINT_XYZ) {
129 aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
131 else if (aType == POINT_XYZ_REF) {
132 Handle(GEOM_Function) aRefFunc = aPI.GetRef();
133 TopoDS_Shape aRefShape = aRefFunc->GetValue();
134 if (aRefShape.ShapeType() != TopAbs_VERTEX) {
135 Standard_TypeMismatch::Raise
136 ("Point creation aborted : referenced shape is not a vertex");
138 gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
139 aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
141 else if (aType == POINT_CURVE_PAR) {
142 Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
143 TopoDS_Shape aRefShape = aRefFunc->GetValue();
144 if (aRefShape.ShapeType() != TopAbs_EDGE) {
145 Standard_TypeMismatch::Raise
146 ("Point On Curve creation aborted : curve shape is not an edge");
148 Standard_Real aFP, aLP, aP;
149 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
150 if ( !aCurve.IsNull() ) {
151 if (aPI.GetTakeOrientationIntoAccount() &&
152 aRefShape.Orientation() == TopAbs_REVERSED) {
153 aP = 1. - aPI.GetParameter();
155 aP = aPI.GetParameter();
158 aP = aFP + (aLP - aFP) * aP;
159 aPnt = aCurve->Value(aP);
162 // null curve, e.g. degenerated edge
163 TopoDS_Iterator It(aRefShape, Standard_False, Standard_False);
164 TopoDS_Vertex aVertex;
166 TopoDS_Shape aShape = It.Value();
167 if ( !aShape.IsNull() )
168 aVertex = TopoDS::Vertex( aShape );
170 if ( !aVertex.IsNull() ) {
171 aPnt = BRep_Tool::Pnt( aVertex );
174 Standard_TypeMismatch::Raise
175 ("Point On Curve creation aborted : null curve");
179 else if (aType == POINT_CURVE_COORD) {
180 Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
181 TopoDS_Shape aRefShape = aRefFunc->GetValue();
182 if (aRefShape.ShapeType() != TopAbs_EDGE) {
183 Standard_TypeMismatch::Raise
184 ("Point On Curve creation aborted : curve shape is not an edge");
186 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
187 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
188 Standard_ConstructionError::Raise
189 ("Point On Curve creation aborted : cannot project point");
192 else if (aType == POINT_CURVE_LENGTH) {
194 Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
195 if (aRefFunc.IsNull()) {
196 Standard_NullObject::Raise
197 ("Point On Curve creation aborted : curve object is null");
199 TopoDS_Shape aRefShape1 = aRefFunc->GetValue();
200 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
201 Standard_TypeMismatch::Raise
202 ("Point On Curve creation aborted : curve shape is not an edge");
204 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
205 TopoDS_Vertex V1, V2;
206 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
209 TopoDS_Vertex aRefVertex;
210 Handle(GEOM_Function) aRefPoint = aPI.GetRef();
211 if (aRefPoint.IsNull()) {
215 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
216 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
217 Standard_TypeMismatch::Raise
218 ("Point On Curve creation aborted : start point shape is not a vertex");
220 aRefVertex = TopoDS::Vertex(aRefShape2);
222 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
225 Standard_Real aLength = aPI.GetLength();
226 //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
227 //if (aLength > theCurveLength) {
228 // Standard_ConstructionError::Raise
229 // ("Point On Curve creation aborted : given length is greater than edges length");
233 Standard_Real UFirst, ULast;
234 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
236 if ( !EdgeCurve.IsNull() ) {
237 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
239 Standard_Real dU = ULast - UFirst;
240 Standard_Real par1 = UFirst + 0.1 * dU;
241 Standard_Real par2 = ULast - 0.1 * dU;
243 gp_Pnt P1 = EdgeCurve->Value(par1);
244 gp_Pnt P2 = EdgeCurve->Value(par2);
246 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
247 ReOrientedCurve = EdgeCurve->Reversed();
248 UFirst = EdgeCurve->ReversedParameter(ULast);
251 // Get the point by length
252 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
253 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
254 Standard_Real aParam = anAbsPnt.Parameter();
255 aPnt = AdapCurve.Value(aParam);
258 // null curve, e.g. degenerated edge
259 TopoDS_Iterator It(aRefEdge, Standard_False, Standard_False);
260 TopoDS_Vertex aVertex;
262 TopoDS_Shape aShape = It.Value();
263 if ( !aShape.IsNull() )
264 aVertex = TopoDS::Vertex( aShape );
266 if ( !aVertex.IsNull() ) {
267 aPnt = BRep_Tool::Pnt( aVertex );
270 Standard_TypeMismatch::Raise
271 ("Point On Curve creation aborted : null curve");
275 else if (aType == POINT_SURFACE_PAR) {
276 Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
277 TopoDS_Shape aRefShape = aRefFunc->GetValue();
278 if (aRefShape.ShapeType() != TopAbs_FACE) {
279 Standard_TypeMismatch::Raise
280 ("Point On Surface creation aborted : surface shape is not a face");
282 TopoDS_Face F = TopoDS::Face(aRefShape);
283 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
284 Standard_Real U1,U2,V1,V2;
285 //aSurf->Bounds(U1,U2,V1,V2);
286 ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
287 Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
288 Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
289 aPnt = aSurf->Value(U,V);
291 else if (aType == POINT_SURFACE_COORD) {
292 Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
293 TopoDS_Shape aRefShape = aRefFunc->GetValue();
294 if (aRefShape.ShapeType() != TopAbs_FACE) {
295 Standard_TypeMismatch::Raise
296 ("Point On Surface creation aborted : surface shape is not a face");
298 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
299 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
300 Standard_ConstructionError::Raise
301 ("Point On Surface creation aborted : cannot project point");
304 else if (aType == POINT_FACE_ANY) {
305 Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
306 TopoDS_Shape aRefShape = aRefFunc->GetValue();
307 int aNbPnts = aPI.GetNumberOfPoints();
309 Standard_TypeMismatch::Raise
310 ("Point On Surface creation aborted : number of points is zero or negative");
312 if (aRefShape.ShapeType() != TopAbs_FACE) {
313 Standard_TypeMismatch::Raise
314 ("Point On Surface creation aborted : surface shape is not a face");
316 TopoDS_Face F = TopoDS::Face(aRefShape);
320 GEOMAlgo_AlgoTools::PntInFace(F, aPnt, aP2d);
324 #if OCC_VERSION_LARGE < 0x07050304
325 Standard_NotImplemented::Raise("Point cloud creation aborted. Improper OCCT version: please, use OCCT 7.5.3p4 or newer.");
327 if (GEOMAlgo_AlgoTools::PointCloudInFace(F, aNbPnts, aCompound) < 0)
328 Standard_ConstructionError::Raise("Point cloud creation aborted : algorithm failed");
333 else if (aType == POINT_LINES_INTERSECTION) {
334 Handle(GEOM_Function) aRef1 = aPI.GetLine1();
335 Handle(GEOM_Function) aRef2 = aPI.GetLine2();
337 TopoDS_Shape aRefShape1 = aRef1->GetValue();
338 TopoDS_Shape aRefShape2 = aRef2->GetValue();
340 if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
341 || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
342 Standard_TypeMismatch::Raise
343 ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
346 if (aRefShape1.IsSame(aRefShape2))
347 Standard_ConstructionError::Raise("The lines to make intersection must be different");
349 //Calculate Lines Intersection Point
350 BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
354 B.MakeCompound( aCompound );
355 for (int i = 1; i <= dst.NbSolution(); i++) {
356 P1 = dst.PointOnShape1(i);
357 P2 = dst.PointOnShape2(i);
358 Standard_Real Dist = P1.Distance(P2);
359 if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
360 BRepBuilderAPI_MakeVertex mkVertex (P1);
361 B.Add(aCompound, mkVertex.Shape());
363 } else if ( Dist <= Precision::Confusion() ) {
366 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
379 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
380 aShape = mkVertex.Shape();
383 //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
384 aFunction->SetValue(aShape);
386 log->SetTouched(Label());
391 //================================================================================
393 * \brief Returns a name of creation operation and names and values of creation parameters
395 //================================================================================
397 bool GEOMImpl_PointDriver::
398 GetCreationInformation(std::string& theOperationName,
399 std::vector<GEOM_Param>& theParams)
401 if (Label().IsNull()) return 0;
402 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
404 GEOMImpl_IPoint aCI( function );
405 Standard_Integer aType = function->GetType();
407 theOperationName = "POINT";
411 AddParam( theParams, "X", aCI.GetX() );
412 AddParam( theParams, "Y", aCI.GetY() );
413 AddParam( theParams, "Z", aCI.GetZ() );
416 AddParam( theParams, "Point", aCI.GetRef() );
417 AddParam( theParams, "Dx", aCI.GetX() );
418 AddParam( theParams, "Dy", aCI.GetY() );
419 AddParam( theParams, "Dz", aCI.GetZ() );
421 case POINT_CURVE_PAR:
422 AddParam( theParams, "Edge", aCI.GetCurve() );
423 AddParam( theParams, "Parameter", aCI.GetParameter() );
424 AddParam( theParams, "Use Orientation", aCI.GetTakeOrientationIntoAccount() );
426 case POINT_CURVE_COORD:
427 AddParam( theParams, "X", aCI.GetX() );
428 AddParam( theParams, "Y", aCI.GetY() );
429 AddParam( theParams, "Z", aCI.GetZ() );
430 AddParam( theParams, "Edge", aCI.GetCurve() );
432 case POINT_CURVE_LENGTH:
433 AddParam( theParams, "Edge", aCI.GetCurve() );
434 AddParam( theParams, "Start Point", aCI.GetRef(), "First Vertex" );
435 AddParam( theParams, "Length", aCI.GetLength() );
437 case POINT_SURFACE_PAR:
438 AddParam( theParams, "Face", aCI.GetSurface() );
439 AddParam( theParams, "U-Parameter", aCI.GetParameter() );
440 AddParam( theParams, "V-Parameter", aCI.GetParameter2() );
442 case POINT_SURFACE_COORD:
443 AddParam( theParams, "X", aCI.GetX() );
444 AddParam( theParams, "Y", aCI.GetY() );
445 AddParam( theParams, "Z", aCI.GetZ() );
446 AddParam( theParams, "Face", aCI.GetSurface() );
449 AddParam( theParams, "Face", aCI.GetSurface() );
451 case POINT_LINES_INTERSECTION:
452 AddParam( theParams, "Line 1", aCI.GetLine1() );
453 AddParam( theParams, "Line 2", aCI.GetLine2() );
462 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PointDriver,GEOM_BaseDriver)