1 // Copyright (C) 2009-2013 CEA/DEN, EDF R&D
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
23 #include <BRepTools.hxx>
27 #include <vtkRenderer.h>
28 #include <vtkActorCollection.h>
29 #include <vtkUnstructuredGrid.h>
33 #include <SUIT_Session.h>
35 #include <SalomeApp_Study.h>
36 #include <SalomeApp_Application.h>
38 #include <OCCViewer_ViewWindow.h>
40 #include <SVTK_ViewManager.h>
41 #include <SVTK_ViewModel.h>
42 #include <SVTK_ViewWindow.h>
44 #include <SALOME_Actor.h>
45 #include <VTKViewer_Algorithm.h>
47 #include <SelectMgr_Selection.hxx>
48 #include <TColStd_ListIteratorOfListOfInteger.hxx>
49 #include <SelectBasics_SensitiveEntity.hxx>
50 #include <TColStd_IndexedMapOfInteger.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
53 #include <StdSelect_BRepOwner.hxx>
56 #include <TopoDS_Face.hxx>
57 #include <BRepBuilderAPI_MakeVertex.hxx>
58 #include <Geom_Surface.hxx>
59 #include <ShapeAnalysis.hxx>
60 #include <BRep_Tool.hxx>
61 #include <BRep_Builder.hxx>
62 #include <Geom_Curve.hxx>
63 #include <BRepExtrema_DistShapeShape.hxx>
64 #include <BRepAdaptor_Curve.hxx>
65 #include <GCPnts_AbscissaPoint.hxx>
66 #include <TopoDS_Compound.hxx>
68 #include "HEXABLOCKGUI.hxx"
69 #include "HEXABLOCKGUI_SalomeTools.hxx"
70 #include "HEXABLOCKGUI_OccGraphicView.hxx"
82 SUIT_Study* GetActiveStudy()
84 SUIT_Application* app = SUIT_Session::session()->activeApplication();
86 return app->activeStudy();
92 _PTR(Study) GetActiveStudyDocument()
94 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
96 return aStudy->studyDS();
102 SALOME_Actor* findActorByEntry( SVTK_ViewWindow *theVtkViewWindow, const char* theEntry)
104 SALOME_Actor *foundActor = NULL;
105 vtkActor *aVTKActor = NULL;
106 Handle(SALOME_InteractiveObject) anIO;
108 vtkRenderer *aRenderer = theVtkViewWindow->getRenderer();
109 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
110 vtkActorCollection *aCollection = aCopy.GetActors();
111 aCollection->InitTraversal();
112 while( aVTKActor = aCollection->GetNextActor() ){
113 foundActor = dynamic_cast<SALOME_Actor*>( aVTKActor );
114 if ( foundActor && foundActor->hasIO() ){
115 anIO = foundActor->getIO();
116 if( anIO->hasEntry() && strcmp(anIO->getEntry(), theEntry) == 0 )
121 return NULL; // no actor found
125 int GetNameOfSelectedElements( SVTK_ViewWindow *theWindow,
126 const Handle(SALOME_InteractiveObject)& theIO,
129 SVTK_Selector* theSelector = theWindow->GetSelector();
133 TColStd_IndexedMapOfInteger aMapIndex;
134 theSelector->GetIndex(theIO,aMapIndex);
136 typedef std::set<int> TIdContainer;
138 std::set<int> anIdContainer;
140 for( int i = 1; i <= aMapIndex.Extent(); i++)
141 anIdContainer.insert(aMapIndex(i));
143 std::set<int>::const_iterator anIter = anIdContainer.begin();
145 for( ; anIter != anIdContainer.end(); anIter++)
146 theName += QString(" %1").arg(*anIter);
148 return aMapIndex.Extent();
151 string shape2string( const TopoDS_Shape& aShape )
153 ostringstream streamShape;
154 BRepTools::Write(aShape, streamShape);
156 return streamShape.str();
159 void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
160 const Handle(AIS_InteractiveContext)& theIC,
161 SelectMgr_IndexedMapOfOwner& theMap )
163 if ( theObj.IsNull() || theIC.IsNull() )
166 TColStd_ListOfInteger modes;
167 theIC->ActivatedModes( theObj, modes );
169 TColStd_ListIteratorOfListOfInteger itr( modes );
170 for (; itr.More(); itr.Next() ) {
172 if ( !theObj->HasSelection( m ) )
175 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
177 for ( sel->Init(); sel->More(); sel->Next() ) {
178 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
179 if ( entity.IsNull() )
182 Handle(SelectMgr_EntityOwner) owner =
183 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
184 if ( !owner.IsNull() )
190 void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
191 const TopoDS_Shape& aMainShape,
192 const SelectMgr_IndexedMapOfOwner& anAllMap,
193 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
195 TopTools_IndexedMapOfShape aMapOfShapes;
196 TopExp::MapShapes(aMainShape, aMapOfShapes);
198 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ )
200 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
201 if ( anOwner.IsNull() || !anOwner->HasShape() )
204 const TopoDS_Shape& aSubShape = anOwner->Shape();
205 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
206 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
209 if ( !aToHiliteMap.Contains( anOwner ) )
210 aToHiliteMap.Add( anOwner );
214 TopoDS_Shape getSubShape(const TopoDS_Shape& theShape, const int theIndex)
216 TopoDS_Shape theSubShape;
218 if (theShape.IsNull() || theIndex < 1)
221 TopTools_IndexedMapOfShape anIndices;
222 TopExp::MapShapes(theShape, anIndices);
223 if (theIndex > anIndices.Extent())
225 theSubShape = anIndices.FindKey(theIndex);
230 int getSubId(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
232 if (theShape.IsNull() || theSubShape.IsNull())
235 TopTools_IndexedMapOfShape anIndices;
236 TopExp::MapShapes(theShape, anIndices);
238 return anIndices.FindIndex(theSubShape);
241 Standard_Boolean getExtremaSolution(const gp_Pnt& theInitPnt,
242 const TopoDS_Shape& theRefShape,
245 BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
246 TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
248 BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
249 if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
250 return Standard_False;
251 thePnt = anExt.PointOnShape2(1);
252 Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
253 for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
255 gp_Pnt aPnt = anExt.PointOnShape2(j);
256 Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
257 if ( aDist2 > aMinDist2)
262 return Standard_True;
265 TopoDS_Vertex makePoint(const double x, const double y, const double z)
267 gp_Pnt thePoint(x, y, z);
268 return BRepBuilderAPI_MakeVertex(thePoint);
271 TopoDS_Vertex makePointWithReference(const TopoDS_Shape& point, const double dx,
278 if (point.ShapeType() != TopAbs_VERTEX) {
279 Standard_TypeMismatch::Raise("Aborted: referenced shape is not a vertex");
282 gp_Pnt refPoint = BRep_Tool::Pnt(TopoDS::Vertex(point));
283 thePoint = gp_Pnt(refPoint.X() + dx, refPoint.Y() + dy, refPoint.Z() + dz);
285 return BRepBuilderAPI_MakeVertex(thePoint);
288 TopoDS_Vertex makePointOnCurve(const TopoDS_Shape& edge, const double param)
293 if (edge.ShapeType() != TopAbs_EDGE) {
294 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
297 Standard_Real aFP, aLP, aP;
298 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(edge), aFP, aLP);
299 aP = aFP + (aLP - aFP) * param;
300 thePoint = aCurve->Value(aP);
302 return BRepBuilderAPI_MakeVertex(thePoint);
305 TopoDS_Vertex makePointOnCurveByLength(const TopoDS_Shape& edge, const TopoDS_Shape& point, const double length)
311 if (edge.ShapeType() != TopAbs_EDGE)
313 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
316 TopoDS_Edge aRefEdge = TopoDS::Edge(edge);
317 TopoDS_Vertex V1, V2;
318 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
321 TopoDS_Vertex aRefVertex;
326 if (point.ShapeType() != TopAbs_VERTEX)
328 Standard_TypeMismatch::Raise("Aborted: shape is not a vertex");
331 aRefVertex = TopoDS::Vertex(point);
333 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
336 Standard_Real UFirst, ULast;
337 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
338 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
340 Standard_Real dU = ULast - UFirst;
341 Standard_Real par1 = UFirst + 0.1 * dU;
342 Standard_Real par2 = ULast - 0.1 * dU;
344 gp_Pnt P1 = EdgeCurve->Value(par1);
345 gp_Pnt P2 = EdgeCurve->Value(par2);
347 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
348 ReOrientedCurve = EdgeCurve->Reversed();
349 UFirst = EdgeCurve->ReversedParameter(ULast);
352 // Get the point by length
353 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
354 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, length, UFirst);
355 Standard_Real aParam = anAbsPnt.Parameter();
356 thePoint = AdapCurve.Value(aParam);
358 return BRepBuilderAPI_MakeVertex(thePoint);
361 TopoDS_Vertex makePointOnCurveByCoord(const TopoDS_Shape& edge, const double x, const double y, const double z)
366 if (edge.ShapeType() != TopAbs_EDGE) {
367 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
370 gp_Pnt anInitPnt (x, y, z);
371 if (!getExtremaSolution(anInitPnt, edge, thePoint)) {
372 Standard_ConstructionError::Raise
373 ("Point On Surface creation aborted : cannot project point");
377 return BRepBuilderAPI_MakeVertex(thePoint);
380 TopoDS_Vertex makePointOnLinesIntersection(const TopoDS_Shape& line1, const TopoDS_Shape& line2)
385 if ( (line1.ShapeType() != TopAbs_EDGE && line1.ShapeType() != TopAbs_WIRE)
386 || (line2.ShapeType() != TopAbs_EDGE && line2.ShapeType() != TopAbs_WIRE) ) {
387 Standard_TypeMismatch::Raise("Aborted: Line shape is not an edge or wire");
391 if (line1.IsSame(line2))
393 Standard_ConstructionError::Raise("The lines to make intersection must be different");
397 TopoDS_Compound aCompound;
398 bool retCompound = false;
400 //Calculate Lines Intersection Point
401 BRepExtrema_DistShapeShape dst (line1, line2);
405 B.MakeCompound( aCompound );
406 for (int i = 1, nbSols = dst.NbSolution(); i <= nbSols; i++) {
407 P1 = dst.PointOnShape1(i);
408 P2 = dst.PointOnShape2(i);
409 Standard_Real Dist = P1.Distance(P2);
410 if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
411 BRepBuilderAPI_MakeVertex mkVertex (P1);
412 B.Add(aCompound, mkVertex.Shape());
414 } else if ( Dist <= Precision::Confusion() ) {
417 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
421 // TopoDS_Shape aShape;
423 Standard_TypeMismatch::Raise
424 ("Aborted: Intersection is a compound of vertices (Not supported)");
425 //aShape = aCompound;
428 BRepBuilderAPI_MakeVertex mkVertex (thePoint);
429 aShape = mkVertex.Shape();
433 return BRepBuilderAPI_MakeVertex(thePoint);
436 TopoDS_Vertex makePointOnSurface(const TopoDS_Shape& face, const double param_u, const double param_v)
441 if (face.ShapeType() != TopAbs_FACE) {
442 Standard_TypeMismatch::Raise("Aborted: surface shape is not a face");
446 TopoDS_Face F = TopoDS::Face(face);
447 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
448 Standard_Real U1,U2,V1,V2;
449 ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
450 Standard_Real U = U1 + (U2-U1) * param_u;
451 Standard_Real V = V1 + (V2-V1) * param_v;
452 thePoint = aSurf->Value(U,V);
454 return BRepBuilderAPI_MakeVertex(thePoint);
457 TopoDS_Vertex makePointOnSurfaceByCoord(const TopoDS_Shape& face, const double x, const double y, const double z)
462 if (face.ShapeType() != TopAbs_FACE) {
463 Standard_TypeMismatch::Raise
464 ("Point On Surface creation aborted : surface shape is not a face");
467 gp_Pnt anInitPnt (x, y, z);
468 if (!getExtremaSolution(anInitPnt, face, thePoint)) {
469 Standard_ConstructionError::Raise
470 ("Point On Surface creation aborted : cannot project point");
473 return BRepBuilderAPI_MakeVertex(thePoint);