1 // Copyright (C) 2007-2010 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
22 #include <Standard_Stream.hxx>
24 #include <GEOMImpl_PointDriver.hxx>
25 #include <GEOMImpl_IPoint.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
29 #include <ShapeAnalysis.hxx>
31 #include <BRep_Builder.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <BRepBuilderAPI_MakeVertex.hxx>
35 #include <BRepExtrema_DistShapeShape.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_Compound.hxx>
46 #include <GCPnts_AbscissaPoint.hxx>
47 #include <IntTools.hxx>
49 #include <Geom_Curve.hxx>
50 #include <Geom_Surface.hxx>
54 #include <Precision.hxx>
56 #include <Standard_NullObject.hxx>
58 //=======================================================================
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_PointDriver::GetID()
64 static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
69 //=======================================================================
70 //function : GEOMImpl_PointDriver
72 //=======================================================================
73 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
77 //=======================================================================
78 //function : getExtremaSolution
79 //purpose : local function
80 //=======================================================================
81 static Standard_Boolean getExtremaSolution
82 (const gp_Pnt& theInitPnt,
83 const TopoDS_Shape& theRefShape,
86 BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
87 TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
89 BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
90 if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
91 return Standard_False;
92 thePnt = anExt.PointOnShape2(1);
93 Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
94 for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
96 gp_Pnt aPnt = anExt.PointOnShape2(j);
97 Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
98 if ( aDist2 > aMinDist2)
103 return Standard_True;
106 //=======================================================================
109 //=======================================================================
110 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
112 if (Label().IsNull()) return 0;
113 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
115 GEOMImpl_IPoint aPI (aFunction);
116 Standard_Integer aType = aFunction->GetType();
119 TopoDS_Compound aCompound;
120 bool retCompound = false;
122 if (aType == POINT_XYZ) {
123 aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
125 else if (aType == POINT_XYZ_REF) {
126 Handle(GEOM_Function) aRefPoint = aPI.GetRef();
127 TopoDS_Shape aRefShape = aRefPoint->GetValue();
128 if (aRefShape.ShapeType() != TopAbs_VERTEX) {
129 Standard_TypeMismatch::Raise
130 ("Point creation aborted : referenced shape is not a vertex");
132 gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
133 aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
135 else if (aType == POINT_CURVE_PAR) {
136 Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
137 TopoDS_Shape aRefShape = aRefCurve->GetValue();
138 if (aRefShape.ShapeType() != TopAbs_EDGE) {
139 Standard_TypeMismatch::Raise
140 ("Point On Curve creation aborted : curve shape is not an edge");
142 Standard_Real aFP, aLP, aP;
143 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
144 aP = aFP + (aLP - aFP) * aPI.GetParameter();
145 aPnt = aCurve->Value(aP);
147 else if (aType == POINT_CURVE_COORD) {
148 Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
149 TopoDS_Shape aRefShape = aRefCurve->GetValue();
150 if (aRefShape.ShapeType() != TopAbs_EDGE) {
151 Standard_TypeMismatch::Raise
152 ("Point On Curve creation aborted : curve shape is not an edge");
154 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
155 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
156 Standard_ConstructionError::Raise
157 ("Point On Curve creation aborted : cannot project point");
160 else if (aType == POINT_CURVE_LENGTH) {
162 Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
163 if (aRefCurve.IsNull()) {
164 Standard_NullObject::Raise
165 ("Point On Curve creation aborted : curve object is null");
167 TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
168 if (aRefShape1.ShapeType() != TopAbs_EDGE) {
169 Standard_TypeMismatch::Raise
170 ("Point On Curve creation aborted : curve shape is not an edge");
172 TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
173 TopoDS_Vertex V1, V2;
174 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
177 TopoDS_Vertex aRefVertex;
178 Handle(GEOM_Function) aRefPoint = aPI.GetRef();
179 if (aRefPoint.IsNull()) {
183 TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
184 if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
185 Standard_TypeMismatch::Raise
186 ("Point On Curve creation aborted : start point shape is not a vertex");
188 aRefVertex = TopoDS::Vertex(aRefShape2);
190 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
193 Standard_Real aLength = aPI.GetLength();
194 //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
195 //if (aLength > theCurveLength) {
196 // Standard_ConstructionError::Raise
197 // ("Point On Curve creation aborted : given length is greater than edges length");
201 Standard_Real UFirst, ULast;
202 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
203 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
205 Standard_Real dU = ULast - UFirst;
206 Standard_Real par1 = UFirst + 0.1 * dU;
207 Standard_Real par2 = ULast - 0.1 * dU;
209 gp_Pnt P1 = EdgeCurve->Value(par1);
210 gp_Pnt P2 = EdgeCurve->Value(par2);
212 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
213 ReOrientedCurve = EdgeCurve->Reversed();
214 UFirst = EdgeCurve->ReversedParameter(ULast);
217 // Get the point by length
218 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
219 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
220 Standard_Real aParam = anAbsPnt.Parameter();
221 aPnt = AdapCurve.Value(aParam);
223 else if (aType == POINT_SURFACE_PAR) {
224 Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
225 TopoDS_Shape aRefShape = aRefCurve->GetValue();
226 if (aRefShape.ShapeType() != TopAbs_FACE) {
227 Standard_TypeMismatch::Raise
228 ("Point On Surface creation aborted : surface shape is not a face");
230 TopoDS_Face F = TopoDS::Face(aRefShape);
231 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
232 Standard_Real U1,U2,V1,V2;
233 //aSurf->Bounds(U1,U2,V1,V2);
234 ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
235 Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
236 Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
237 aPnt = aSurf->Value(U,V);
239 else if (aType == POINT_SURFACE_COORD) {
240 Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
241 TopoDS_Shape aRefShape = aRefCurve->GetValue();
242 if (aRefShape.ShapeType() != TopAbs_FACE) {
243 Standard_TypeMismatch::Raise
244 ("Point On Surface creation aborted : surface shape is not a face");
246 gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
247 if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
248 Standard_ConstructionError::Raise
249 ("Point On Surface creation aborted : cannot project point");
252 else if (aType == POINT_LINES_INTERSECTION) {
253 Handle(GEOM_Function) aRef1 = aPI.GetLine1();
254 Handle(GEOM_Function) aRef2 = aPI.GetLine2();
256 TopoDS_Shape aRefShape1 = aRef1->GetValue();
257 TopoDS_Shape aRefShape2 = aRef2->GetValue();
259 if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
260 || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
261 Standard_TypeMismatch::Raise
262 ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
265 if (aRefShape1.IsSame(aRefShape2))
266 Standard_ConstructionError::Raise("The lines to make intersection must be different");
268 //Calculate Lines Intersection Point
269 BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
273 B.MakeCompound( aCompound );
274 for (int i = 1; i <= dst.NbSolution(); i++) {
275 P1 = dst.PointOnShape1(i);
276 P2 = dst.PointOnShape2(i);
277 Standard_Real Dist = P1.Distance(P2);
278 if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
279 BRepBuilderAPI_MakeVertex mkVertex (P1);
280 B.Add(aCompound, mkVertex.Shape());
282 } else if ( Dist <= Precision::Confusion() ) {
285 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
298 BRepBuilderAPI_MakeVertex mkVertex (aPnt);
299 aShape = mkVertex.Shape();
302 //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
303 aFunction->SetValue(aShape);
305 log.SetTouched(Label());
311 //=======================================================================
312 //function : GEOMImpl_PointDriver_Type_
314 //=======================================================================
315 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_Type_()
318 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
319 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
320 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
321 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
322 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
323 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
326 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
327 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PointDriver",
328 sizeof(GEOMImpl_PointDriver),
330 (Standard_Address)_Ancestors,
331 (Standard_Address)NULL);
336 //=======================================================================
337 //function : DownCast
339 //=======================================================================
341 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
343 Handle(GEOMImpl_PointDriver) _anOtherObject;
345 if (!AnObject.IsNull()) {
346 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
347 _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
351 return _anOtherObject ;