1 // Copyright (C) 2007-2012 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>
30 #include <ShapeAnalysis.hxx>
32 #include <BRep_Builder.hxx>
33 #include <BRep_Tool.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepBuilderAPI_MakeVertex.hxx>
36 #include <BRepExtrema_DistShapeShape.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Compound.hxx>
47 #include <GCPnts_AbscissaPoint.hxx>
48 #include <IntTools.hxx>
50 #include <Geom_Curve.hxx>
51 #include <Geom_Surface.hxx>
55 #include <Precision.hxx>
57 #include <Standard_NullObject.hxx>
59 //=======================================================================
62 //=======================================================================
63 const Standard_GUID& GEOMImpl_PointDriver::GetID()
65 static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
70 //=======================================================================
71 //function : GEOMImpl_PointDriver
73 //=======================================================================
74 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
78 //=======================================================================
79 //function : getExtremaSolution
80 //purpose : local function
81 //=======================================================================
82 static Standard_Boolean getExtremaSolution
83 (const gp_Pnt& theInitPnt,
84 const TopoDS_Shape& theRefShape,
87 BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
88 TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
90 BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
91 if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
92 return Standard_False;
93 thePnt = anExt.PointOnShape2(1);
94 Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
95 for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
97 gp_Pnt aPnt = anExt.PointOnShape2(j);
98 Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
99 if ( aDist2 > aMinDist2)
104 return Standard_True;
107 //=======================================================================
110 //=======================================================================
111 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
113 if (Label().IsNull()) return 0;
114 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
116 GEOMImpl_IPoint aPI (aFunction);
117 Standard_Integer aType = aFunction->GetType();
120 TopoDS_Compound aCompound;
121 bool retCompound = false;
123 if (aType == POINT_XYZ) {
124 aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
126 else if (aType == POINT_XYZ_REF) {
127 Handle(GEOM_Function) aRefPoint = aPI.GetRef();
128 TopoDS_Shape aRefShape = aRefPoint->GetValue();
129 if (aRefShape.ShapeType() != TopAbs_VERTEX) {
130 Standard_TypeMismatch::Raise
131 ("Point creation aborted : referenced shape is not a vertex");
133 gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
134 aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
136 else if (aType == POINT_CURVE_PAR) {
137 Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
138 TopoDS_Shape aRefShape = aRefCurve->GetValue();
139 if (aRefShape.ShapeType() != TopAbs_EDGE) {
140 Standard_TypeMismatch::Raise
141 ("Point On Curve creation aborted : curve shape is not an edge");
143 Standard_Real aFP, aLP, aP;
144 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
145 aP = aFP + (aLP - aFP) * aPI.GetParameter();
146 aPnt = aCurve->Value(aP);
148 else if (aType == POINT_CURVE_COORD) {
149 Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
150 TopoDS_Shape aRefShape = aRefCurve->GetValue();
151 if (aRefShape.ShapeType() != TopAbs_EDGE) {
152 Standard_TypeMismatch::Raise
153 ("Point On Curve creation aborted : curve shape is not an edge");
155 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
156 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
157 Standard_ConstructionError::Raise
158 ("Point On Curve creation aborted : cannot project point");
161 else if (aType == POINT_CURVE_LENGTH) {
163 Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
164 if (aRefCurve.IsNull()) {
165 Standard_NullObject::Raise
166 ("Point On Curve creation aborted : curve object is null");
168 TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
169 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
170 Standard_TypeMismatch::Raise
171 ("Point On Curve creation aborted : curve shape is not an edge");
173 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
174 TopoDS_Vertex V1, V2;
175 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
178 TopoDS_Vertex aRefVertex;
179 Handle(GEOM_Function) aRefPoint = aPI.GetRef();
180 if (aRefPoint.IsNull()) {
184 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
185 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
186 Standard_TypeMismatch::Raise
187 ("Point On Curve creation aborted : start point shape is not a vertex");
189 aRefVertex = TopoDS::Vertex(aRefShape2);
191 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
194 Standard_Real aLength = aPI.GetLength();
195 //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
196 //if (aLength > theCurveLength) {
197 // Standard_ConstructionError::Raise
198 // ("Point On Curve creation aborted : given length is greater than edges length");
202 Standard_Real UFirst, ULast;
203 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
204 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
206 Standard_Real dU = ULast - UFirst;
207 Standard_Real par1 = UFirst + 0.1 * dU;
208 Standard_Real par2 = ULast - 0.1 * dU;
210 gp_Pnt P1 = EdgeCurve->Value(par1);
211 gp_Pnt P2 = EdgeCurve->Value(par2);
213 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
214 ReOrientedCurve = EdgeCurve->Reversed();
215 UFirst = EdgeCurve->ReversedParameter(ULast);
218 // Get the point by length
219 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
220 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
221 Standard_Real aParam = anAbsPnt.Parameter();
222 aPnt = AdapCurve.Value(aParam);
224 else if (aType == POINT_SURFACE_PAR) {
225 Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
226 TopoDS_Shape aRefShape = aRefCurve->GetValue();
227 if (aRefShape.ShapeType() != TopAbs_FACE) {
228 Standard_TypeMismatch::Raise
229 ("Point On Surface creation aborted : surface shape is not a face");
231 TopoDS_Face F = TopoDS::Face(aRefShape);
232 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
233 Standard_Real U1,U2,V1,V2;
234 //aSurf->Bounds(U1,U2,V1,V2);
235 ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
236 Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
237 Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
238 aPnt = aSurf->Value(U,V);
240 else if (aType == POINT_SURFACE_COORD) {
241 Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
242 TopoDS_Shape aRefShape = aRefCurve->GetValue();
243 if (aRefShape.ShapeType() != TopAbs_FACE) {
244 Standard_TypeMismatch::Raise
245 ("Point On Surface creation aborted : surface shape is not a face");
247 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
248 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
249 Standard_ConstructionError::Raise
250 ("Point On Surface creation aborted : cannot project point");
253 else if (aType == POINT_LINES_INTERSECTION) {
254 Handle(GEOM_Function) aRef1 = aPI.GetLine1();
255 Handle(GEOM_Function) aRef2 = aPI.GetLine2();
257 TopoDS_Shape aRefShape1 = aRef1->GetValue();
258 TopoDS_Shape aRefShape2 = aRef2->GetValue();
260 if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
261 || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
262 Standard_TypeMismatch::Raise
263 ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
266 if (aRefShape1.IsSame(aRefShape2))
267 Standard_ConstructionError::Raise("The lines to make intersection must be different");
269 //Calculate Lines Intersection Point
270 BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
274 B.MakeCompound( aCompound );
275 for (int i = 1; i <= dst.NbSolution(); i++) {
276 P1 = dst.PointOnShape1(i);
277 P2 = dst.PointOnShape2(i);
278 Standard_Real Dist = P1.Distance(P2);
279 if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
280 BRepBuilderAPI_MakeVertex mkVertex (P1);
281 B.Add(aCompound, mkVertex.Shape());
283 } else if ( Dist <= Precision::Confusion() ) {
286 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
299 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
300 aShape = mkVertex.Shape();
303 //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
304 aFunction->SetValue(aShape);
306 log.SetTouched(Label());
312 //=======================================================================
313 //function : GEOMImpl_PointDriver_Type_
315 //=======================================================================
316 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_Type_()
319 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
320 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
321 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
322 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
323 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
324 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
327 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
328 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PointDriver",
329 sizeof(GEOMImpl_PointDriver),
331 (Standard_Address)_Ancestors,
332 (Standard_Address)NULL);
337 //=======================================================================
338 //function : DownCast
340 //=======================================================================
342 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
344 Handle(GEOMImpl_PointDriver) _anOtherObject;
346 if (!AnObject.IsNull()) {
347 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
348 _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
352 return _anOtherObject ;