1 // Copyright (C) 2009-2016 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, or (at your option) any later version.
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>
31 #include <SUIT_Session.h>
33 #include <SalomeApp_Study.h>
34 #include <SalomeApp_Application.h>
36 #include <OCCViewer_ViewWindow.h>
38 #include <SVTK_ViewManager.h>
39 #include <SVTK_ViewModel.h>
40 #include <SVTK_ViewWindow.h>
42 #include <SALOME_Actor.h>
43 #include <VTKViewer_Algorithm.h>
45 #include <SelectMgr_Selection.hxx>
46 #include <TColStd_ListIteratorOfListOfInteger.hxx>
47 #include <SelectBasics_SensitiveEntity.hxx>
48 #include <TColStd_IndexedMapOfInteger.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
51 #include <StdSelect_BRepOwner.hxx>
54 #include <TopoDS_Face.hxx>
55 #include <BRepBuilderAPI_MakeVertex.hxx>
56 #include <Geom_Surface.hxx>
57 #include <ShapeAnalysis.hxx>
58 #include <BRep_Tool.hxx>
59 #include <BRep_Builder.hxx>
60 #include <Geom_Curve.hxx>
61 #include <BRepExtrema_DistShapeShape.hxx>
62 #include <BRepAdaptor_Curve.hxx>
63 #include <GCPnts_AbscissaPoint.hxx>
64 #include <TopoDS_Compound.hxx>
66 #include "HEXABLOCKGUI.hxx"
67 #include "HEXABLOCKGUI_SalomeTools.hxx"
68 #include "HEXABLOCKGUI_OccGraphicView.hxx"
80 SUIT_Study* GetActiveStudy()
82 SUIT_Application* app = SUIT_Session::session()->activeApplication();
84 return app->activeStudy();
90 _PTR(Study) GetActiveStudyDocument()
92 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
94 return aStudy->studyDS();
100 SALOME_Actor* findActorByEntry( SVTK_ViewWindow *theVtkViewWindow, const char* theEntry)
102 SALOME_Actor *foundActor = NULL;
103 vtkActor *aVTKActor = NULL;
104 Handle(SALOME_InteractiveObject) anIO;
106 vtkRenderer *aRenderer = theVtkViewWindow->getRenderer();
107 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
108 vtkActorCollection *aCollection = aCopy.GetActors();
109 aCollection->InitTraversal();
110 while( aVTKActor = aCollection->GetNextActor() ){
111 foundActor = dynamic_cast<SALOME_Actor*>( aVTKActor );
112 if ( foundActor && foundActor->hasIO() ){
113 anIO = foundActor->getIO();
114 if( anIO->hasEntry() && strcmp(anIO->getEntry(), theEntry) == 0 )
119 return NULL; // no actor found
123 int GetNameOfSelectedElements( SVTK_ViewWindow *theWindow,
124 const Handle(SALOME_InteractiveObject)& theIO,
127 SVTK_Selector* theSelector = theWindow->GetSelector();
131 TColStd_IndexedMapOfInteger aMapIndex;
132 theSelector->GetIndex(theIO,aMapIndex);
134 typedef std::set<int> TIdContainer;
136 std::set<int> anIdContainer;
138 for( int i = 1; i <= aMapIndex.Extent(); i++)
139 anIdContainer.insert(aMapIndex(i));
141 std::set<int>::const_iterator anIter = anIdContainer.begin();
143 for( ; anIter != anIdContainer.end(); anIter++)
144 theName += QString(" %1").arg(*anIter);
146 return aMapIndex.Extent();
149 string shape2string( const TopoDS_Shape& aShape )
151 ostringstream streamShape;
152 BRepTools::Write(aShape, streamShape);
154 return streamShape.str();
157 void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
158 const Handle(AIS_InteractiveContext)& theIC,
159 SelectMgr_IndexedMapOfOwner& theMap )
161 if ( theObj.IsNull() || theIC.IsNull() )
164 TColStd_ListOfInteger modes;
165 theIC->ActivatedModes( theObj, modes );
167 TColStd_ListIteratorOfListOfInteger itr( modes );
168 for (; itr.More(); itr.Next() ) {
170 if ( !theObj->HasSelection( m ) )
173 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
175 for ( sel->Init(); sel->More(); sel->Next() ) {
176 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = sel->Sensitive();
177 if ( aHSenEntity.IsNull() )
179 Handle(SelectBasics_SensitiveEntity) entity = aHSenEntity->BaseSensitive();
180 if ( entity.IsNull() )
183 Handle(SelectMgr_EntityOwner) owner =
184 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
185 if ( !owner.IsNull() )
191 void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
192 const TopoDS_Shape& aMainShape,
193 const SelectMgr_IndexedMapOfOwner& anAllMap,
194 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
196 TopTools_IndexedMapOfShape aMapOfShapes;
197 TopExp::MapShapes(aMainShape, aMapOfShapes);
199 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ )
201 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
202 if ( anOwner.IsNull() || !anOwner->HasShape() )
205 const TopoDS_Shape& aSubShape = anOwner->Shape();
206 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
207 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
210 if ( !aToHiliteMap.Contains( anOwner ) )
211 aToHiliteMap.Add( anOwner );
215 TopoDS_Shape getSubShape(const TopoDS_Shape& theShape, const int theIndex)
217 TopoDS_Shape theSubShape;
219 if (theShape.IsNull() || theIndex < 1)
222 TopTools_IndexedMapOfShape anIndices;
223 TopExp::MapShapes(theShape, anIndices);
224 if (theIndex > anIndices.Extent())
226 theSubShape = anIndices.FindKey(theIndex);
231 int getSubId(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
233 if (theShape.IsNull() || theSubShape.IsNull())
236 TopTools_IndexedMapOfShape anIndices;
237 TopExp::MapShapes(theShape, anIndices);
239 return anIndices.FindIndex(theSubShape);
242 Standard_Boolean getExtremaSolution(const gp_Pnt& theInitPnt,
243 const TopoDS_Shape& theRefShape,
246 BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
247 TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
249 BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
250 if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
251 return Standard_False;
252 thePnt = anExt.PointOnShape2(1);
253 Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
254 for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
256 gp_Pnt aPnt = anExt.PointOnShape2(j);
257 Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
258 if ( aDist2 > aMinDist2)
263 return Standard_True;
266 TopoDS_Vertex makePoint(const double x, const double y, const double z)
268 gp_Pnt thePoint(x, y, z);
269 return BRepBuilderAPI_MakeVertex(thePoint);
272 TopoDS_Vertex makePointWithReference(const TopoDS_Shape& point, const double dx,
279 if (point.ShapeType() != TopAbs_VERTEX) {
280 Standard_TypeMismatch::Raise("Aborted: referenced shape is not a vertex");
283 gp_Pnt refPoint = BRep_Tool::Pnt(TopoDS::Vertex(point));
284 thePoint = gp_Pnt(refPoint.X() + dx, refPoint.Y() + dy, refPoint.Z() + dz);
286 return BRepBuilderAPI_MakeVertex(thePoint);
289 TopoDS_Vertex makePointOnCurve(const TopoDS_Shape& edge, const double param)
294 if (edge.ShapeType() != TopAbs_EDGE) {
295 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
298 Standard_Real aFP, aLP, aP;
299 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(edge), aFP, aLP);
300 aP = aFP + (aLP - aFP) * param;
301 thePoint = aCurve->Value(aP);
303 return BRepBuilderAPI_MakeVertex(thePoint);
306 TopoDS_Vertex makePointOnCurveByLength(const TopoDS_Shape& edge, const TopoDS_Shape& point, const double length)
312 if (edge.ShapeType() != TopAbs_EDGE)
314 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
317 TopoDS_Edge aRefEdge = TopoDS::Edge(edge);
318 TopoDS_Vertex V1, V2;
319 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
322 TopoDS_Vertex aRefVertex;
327 if (point.ShapeType() != TopAbs_VERTEX)
329 Standard_TypeMismatch::Raise("Aborted: shape is not a vertex");
332 aRefVertex = TopoDS::Vertex(point);
334 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
337 Standard_Real UFirst, ULast;
338 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
339 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
341 Standard_Real dU = ULast - UFirst;
342 Standard_Real par1 = UFirst + 0.1 * dU;
343 Standard_Real par2 = ULast - 0.1 * dU;
345 gp_Pnt P1 = EdgeCurve->Value(par1);
346 gp_Pnt P2 = EdgeCurve->Value(par2);
348 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
349 ReOrientedCurve = EdgeCurve->Reversed();
350 UFirst = EdgeCurve->ReversedParameter(ULast);
353 // Get the point by length
354 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
355 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, length, UFirst);
356 Standard_Real aParam = anAbsPnt.Parameter();
357 thePoint = AdapCurve.Value(aParam);
359 return BRepBuilderAPI_MakeVertex(thePoint);
362 TopoDS_Vertex makePointOnCurveByCoord(const TopoDS_Shape& edge, const double x, const double y, const double z)
367 if (edge.ShapeType() != TopAbs_EDGE) {
368 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
371 gp_Pnt anInitPnt (x, y, z);
372 if (!getExtremaSolution(anInitPnt, edge, thePoint)) {
373 Standard_ConstructionError::Raise
374 ("Point On Surface creation aborted : cannot project point");
378 return BRepBuilderAPI_MakeVertex(thePoint);
381 TopoDS_Vertex makePointOnLinesIntersection(const TopoDS_Shape& line1, const TopoDS_Shape& line2)
386 if ( (line1.ShapeType() != TopAbs_EDGE && line1.ShapeType() != TopAbs_WIRE)
387 || (line2.ShapeType() != TopAbs_EDGE && line2.ShapeType() != TopAbs_WIRE) ) {
388 Standard_TypeMismatch::Raise("Aborted: Line shape is not an edge or wire");
392 if (line1.IsSame(line2))
394 Standard_ConstructionError::Raise("The lines to make intersection must be different");
398 TopoDS_Compound aCompound;
399 bool retCompound = false;
401 //Calculate Lines Intersection Point
402 BRepExtrema_DistShapeShape dst (line1, line2);
406 B.MakeCompound( aCompound );
407 for (int i = 1, nbSols = dst.NbSolution(); i <= nbSols; i++) {
408 P1 = dst.PointOnShape1(i);
409 P2 = dst.PointOnShape2(i);
410 Standard_Real Dist = P1.Distance(P2);
411 if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
412 BRepBuilderAPI_MakeVertex mkVertex (P1);
413 B.Add(aCompound, mkVertex.Shape());
415 } else if ( Dist <= Precision::Confusion() ) {
418 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
422 // TopoDS_Shape aShape;
424 Standard_TypeMismatch::Raise
425 ("Aborted: Intersection is a compound of vertices (Not supported)");
426 //aShape = aCompound;
429 BRepBuilderAPI_MakeVertex mkVertex (thePoint);
430 aShape = mkVertex.Shape();
434 return BRepBuilderAPI_MakeVertex(thePoint);
437 TopoDS_Vertex makePointOnSurface(const TopoDS_Shape& face, const double param_u, const double param_v)
442 if (face.ShapeType() != TopAbs_FACE) {
443 Standard_TypeMismatch::Raise("Aborted: surface shape is not a face");
447 TopoDS_Face F = TopoDS::Face(face);
448 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
449 Standard_Real U1,U2,V1,V2;
450 ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
451 Standard_Real U = U1 + (U2-U1) * param_u;
452 Standard_Real V = V1 + (V2-V1) * param_v;
453 thePoint = aSurf->Value(U,V);
455 return BRepBuilderAPI_MakeVertex(thePoint);
458 TopoDS_Vertex makePointOnSurfaceByCoord(const TopoDS_Shape& face, const double x, const double y, const double z)
463 if (face.ShapeType() != TopAbs_FACE) {
464 Standard_TypeMismatch::Raise
465 ("Point On Surface creation aborted : surface shape is not a face");
468 gp_Pnt anInitPnt (x, y, z);
469 if (!getExtremaSolution(anInitPnt, face, thePoint)) {
470 Standard_ConstructionError::Raise
471 ("Point On Surface creation aborted : cannot project point");
474 return BRepBuilderAPI_MakeVertex(thePoint);