1 // Copyright (C) 2007-2013 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.
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_Tools3D.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(TFunction_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 aP = aFP + (aLP - aFP) * aPI.GetParameter();
149 aPnt = aCurve->Value(aP);
152 // null curve, e.g. degenerated edge
153 TopoDS_Iterator It(aRefShape, Standard_False, Standard_False);
154 TopoDS_Vertex aVertex;
156 TopoDS_Shape aShape = It.Value();
157 if ( !aShape.IsNull() )
158 aVertex = TopoDS::Vertex( aShape );
160 if ( !aVertex.IsNull() ) {
161 aPnt = BRep_Tool::Pnt( aVertex );
164 Standard_TypeMismatch::Raise
165 ("Point On Curve creation aborted : null curve");
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");
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");
182 else if (aType == POINT_CURVE_LENGTH) {
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");
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");
194 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
195 TopoDS_Vertex V1, V2;
196 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
199 TopoDS_Vertex aRefVertex;
200 Handle(GEOM_Function) aRefPoint = aPI.GetRef();
201 if (aRefPoint.IsNull()) {
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");
210 aRefVertex = TopoDS::Vertex(aRefShape2);
212 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
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");
223 Standard_Real UFirst, ULast;
224 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
226 if ( !EdgeCurve.IsNull() ) {
227 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
229 Standard_Real dU = ULast - UFirst;
230 Standard_Real par1 = UFirst + 0.1 * dU;
231 Standard_Real par2 = ULast - 0.1 * dU;
233 gp_Pnt P1 = EdgeCurve->Value(par1);
234 gp_Pnt P2 = EdgeCurve->Value(par2);
236 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
237 ReOrientedCurve = EdgeCurve->Reversed();
238 UFirst = EdgeCurve->ReversedParameter(ULast);
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);
248 // null curve, e.g. degenerated edge
249 TopoDS_Iterator It(aRefEdge, Standard_False, Standard_False);
250 TopoDS_Vertex aVertex;
252 TopoDS_Shape aShape = It.Value();
253 if ( !aShape.IsNull() )
254 aVertex = TopoDS::Vertex( aShape );
256 if ( !aVertex.IsNull() ) {
257 aPnt = BRep_Tool::Pnt( aVertex );
260 Standard_TypeMismatch::Raise
261 ("Point On Curve creation aborted : null curve");
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");
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);
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");
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");
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");
301 TopoDS_Face F = TopoDS::Face(aRefShape);
303 GEOMAlgo_Tools3D::PntInFace(F, aPnt, aP2d);
305 else if (aType == POINT_LINES_INTERSECTION) {
306 Handle(GEOM_Function) aRef1 = aPI.GetLine1();
307 Handle(GEOM_Function) aRef2 = aPI.GetLine2();
309 TopoDS_Shape aRefShape1 = aRef1->GetValue();
310 TopoDS_Shape aRefShape2 = aRef2->GetValue();
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");
318 if (aRefShape1.IsSame(aRefShape2))
319 Standard_ConstructionError::Raise("The lines to make intersection must be different");
321 //Calculate Lines Intersection Point
322 BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
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());
335 } else if ( Dist <= Precision::Confusion() ) {
338 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
351 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
352 aShape = mkVertex.Shape();
355 //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
356 aFunction->SetValue(aShape);
358 log.SetTouched(Label());
364 //=======================================================================
365 //function : GEOMImpl_PointDriver_Type_
367 //=======================================================================
368 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_Type_()
371 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
372 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
373 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
374 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
375 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
376 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
379 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
380 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PointDriver",
381 sizeof(GEOMImpl_PointDriver),
383 (Standard_Address)_Ancestors,
384 (Standard_Address)NULL);
389 //=======================================================================
390 //function : DownCast
392 //=======================================================================
394 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
396 Handle(GEOMImpl_PointDriver) _anOtherObject;
398 if (!AnObject.IsNull()) {
399 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
400 _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
404 return _anOtherObject ;