1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CurveCreator_Utils.h"
22 #include <GEOMUtils.hxx>
27 #include <TopoDS_Vertex.hxx>
28 #include <TopoDS_Wire.hxx>
29 #include <TopoDS_Edge.hxx>
31 #include <AIS_ListOfInteractive.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_Shape.hxx>
34 #include <AIS_Point.hxx>
35 #include <AIS_Line.hxx>
36 #include <AIS_Trihedron.hxx>
38 #include <Geom_Point.hxx>
39 #include <Geom_BSplineCurve.hxx>
40 #include <Geom_Line.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <BRep_Tool.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
45 #include <SelectMgr_EntityOwner.hxx>
47 #include <ProjLib.hxx>
50 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
51 const int SCENE_PIXEL_TOLERANCE = 10;
53 //=======================================================================
54 // function : ConvertClickToPoint()
55 // purpose : Returns the point clicked in 3D view
56 //=======================================================================
57 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
58 Handle(V3d_View) theView,
61 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
65 //=======================================================================
66 // function : ConvertClickToPoint()
67 // purpose : Returns the point clicked in 3D view
68 //=======================================================================
69 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
71 return GEOMUtils::ConvertClickToPoint( x, y, aView );
74 std::list<float> CurveCreator_Utils::getSelectedPoints( Handle(AIS_InteractiveContext) theContext )
76 std::list<float> aSelectedPoints;
79 for ( theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected() ) {
80 TopoDS_Vertex aVertex;
81 TopoDS_Shape aShape = theContext->SelectedShape();
82 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
83 aVertex = TopoDS::Vertex( theContext->SelectedShape() );
85 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
86 if ( !anOwner.IsNull() ) {
87 Handle(AIS_InteractiveObject) anAIS = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
88 if ( !anAIS.IsNull() ) {
89 Handle(AIS_Point) aPoint = Handle(AIS_Point)::DownCast( anAIS);
90 if ( !aPoint.IsNull() )
91 aVertex = TopoDS::Vertex( aPoint->Vertex() );
93 if ( aVertex.IsNull() ) {
94 // the following happens if there are no points in the current curve, there is only a shape
95 /*Handle(StdSelect_BRepOwner) aBrepOwner = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
96 if ( aBrepOwner.IsNull() )
98 if ( aBrepOwner->HasShape() ) {
99 const TopoDS_Shape& aShape = aBrepOwner->Shape();
100 if ( !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX )
102 aVertex = TopoDS::Vertex( aShape );
108 if ( aVertex.IsNull() )
110 aPnt = BRep_Tool::Pnt( aVertex );
111 aSelectedPoints.push_back( aPnt.X() );
112 aSelectedPoints.push_back( aPnt.Y() );
113 aSelectedPoints.push_back( aPnt.Z() );
116 return aSelectedPoints;
118 //=======================================================================
119 // function : setLocalPointContext
120 // purpose : Open/close the viewer local context
121 //=======================================================================
122 void CurveCreator_Utils::setLocalPointContext(
123 Handle(AIS_InteractiveContext) theContext,
130 // Open local context if there is no one
131 if ( !theContext->HasOpenedContext() ) {
132 theContext->ClearCurrents( false );
133 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
135 AIS_ListOfInteractive aList;
136 theContext->DisplayedObjects( aList );
138 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
141 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
143 Handle(AIS_InteractiveObject) anAIS = it.Value();
144 if ( !anAIS.IsNull() )
146 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
148 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
149 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
150 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
152 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
154 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
155 theContext->Activate( anAIS, TopAbs_VERTEX );
162 if ( theContext->HasOpenedContext() )
163 theContext->CloseAllContexts();
167 bool CurveCreator_Utils::getNeighbourPoints( Handle(AIS_InteractiveContext) theContext,
168 Handle(V3d_View) theView,
169 const int theX, const int theY,
170 gp_Pnt& thePoint, gp_Pnt& thePoint1,
173 bool isFoundPoint = false;
174 if ( theContext.IsNull() )
177 for ( theContext->InitSelected(); theContext->MoreSelected() && !isFoundPoint;
178 theContext->NextSelected() ) {
179 TopoDS_Shape aTShape = theContext->SelectedShape();
180 if ( !aTShape.IsNull() && aTShape.ShapeType() == TopAbs_VERTEX )
183 Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
184 if ( anOwner.IsNull() )
186 const TopLoc_Location& aLocation = anOwner->Location();
187 Handle(AIS_InteractiveObject) anAIS =
188 Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
189 isFoundPoint = CurveCreator_Utils::pointOnObject( theView, anAIS, theX, theY, thePoint,
190 thePoint1, thePoint2 );
196 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
197 Handle(AIS_InteractiveObject) theObject,
198 const int theX, const int theY,
200 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
202 bool isFound = false;
204 if ( theObject.IsNull() || theView.IsNull() )
208 Standard_Real aParameter;
210 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
211 if ( !aLine.IsNull() ) {
212 const Handle(Geom_Line) aGLine = aLine->Line();
213 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
215 aPoint = aGLine->Value( aParameter );
217 Handle(Geom_Point) aPStart;
218 Handle(Geom_Point) aPEnd;
219 aLine->Points( aPStart, aPEnd );
220 aPnt1 = aPStart->Pnt();
221 aPnt2 = aPEnd->Pnt();
223 // in case of Geom line a projection is performed to the infinite line,
224 // so it is necessary to bound it by the line size
226 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
227 isFound = !aLineBox.IsOut( aPoint );
231 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
232 if ( !aShape.IsNull() ) {
233 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
234 if ( !aWire.IsNull() ) {
235 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
236 for ( ; anExp.More(); anExp.Next())
238 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
239 if ( !anEdge.IsNull() ) {
240 Standard_Real aFirst, aLast;
241 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
243 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
244 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
245 if ( !aBSplineCurve.IsNull() ) {
246 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
248 aPoint = aBSplineCurve->Value( aParameter );
249 Standard_Integer anI1, anI2;
250 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
252 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
253 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
270 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
271 const int theX, const int theY,
272 const Handle(Geom_Curve)& theCurve,
273 Standard_Real& theParameter )
275 bool isFound = false;
276 if ( theView.IsNull() )
279 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
281 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
282 Standard_Integer aNbPoint = aProj.NbPoints();
284 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
285 gp_Pnt aNewPoint = aProj.Point( j );
286 theParameter = aProj.Parameter( j );
289 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
291 int aXDelta = abs( aX - theX );
292 int anYDelta = abs( anY - theY );
293 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;