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_Application.h>
35 #include <OCCViewer_ViewWindow.h>
37 #include <SVTK_ViewManager.h>
38 #include <SVTK_ViewModel.h>
39 #include <SVTK_ViewWindow.h>
41 #include <SALOME_Actor.h>
42 #include <VTKViewer_Algorithm.h>
44 #include <SelectMgr_Selection.hxx>
45 #include <TColStd_ListIteratorOfListOfInteger.hxx>
46 #include <SelectBasics_SensitiveEntity.hxx>
47 #include <TColStd_IndexedMapOfInteger.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <StdSelect_BRepOwner.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <BRepBuilderAPI_MakeVertex.hxx>
55 #include <Geom_Surface.hxx>
56 #include <ShapeAnalysis.hxx>
57 #include <BRep_Tool.hxx>
58 #include <BRep_Builder.hxx>
59 #include <Geom_Curve.hxx>
60 #include <BRepExtrema_DistShapeShape.hxx>
61 #include <BRepAdaptor_Curve.hxx>
62 #include <GCPnts_AbscissaPoint.hxx>
63 #include <TopoDS_Compound.hxx>
65 #include "HEXABLOCKGUI.hxx"
66 #include "HEXABLOCKGUI_SalomeTools.hxx"
67 #include "HEXABLOCKGUI_OccGraphicView.hxx"
79 SUIT_Study* GetActiveStudy()
81 SUIT_Application* app = SUIT_Session::session()->activeApplication();
83 return app->activeStudy();
89 _PTR(Study) getStudy()
91 static _PTR(Study) _study;
93 _study = SalomeApp_Application::getStudy();
98 SALOME_Actor* findActorByEntry( SVTK_ViewWindow *theVtkViewWindow, const char* theEntry)
100 SALOME_Actor *foundActor = NULL;
101 vtkActor *aVTKActor = NULL;
102 Handle(SALOME_InteractiveObject) anIO;
104 vtkRenderer *aRenderer = theVtkViewWindow->getRenderer();
105 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
106 vtkActorCollection *aCollection = aCopy.GetActors();
107 aCollection->InitTraversal();
108 while( aVTKActor = aCollection->GetNextActor() ){
109 foundActor = dynamic_cast<SALOME_Actor*>( aVTKActor );
110 if ( foundActor && foundActor->hasIO() ){
111 anIO = foundActor->getIO();
112 if( anIO->hasEntry() && strcmp(anIO->getEntry(), theEntry) == 0 )
117 return NULL; // no actor found
121 int GetNameOfSelectedElements( SVTK_ViewWindow *theWindow,
122 const Handle(SALOME_InteractiveObject)& theIO,
125 SVTK_Selector* theSelector = theWindow->GetSelector();
129 TColStd_IndexedMapOfInteger aMapIndex;
130 theSelector->GetIndex(theIO,aMapIndex);
132 typedef std::set<int> TIdContainer;
134 std::set<int> anIdContainer;
136 for( int i = 1; i <= aMapIndex.Extent(); i++)
137 anIdContainer.insert(aMapIndex(i));
139 std::set<int>::const_iterator anIter = anIdContainer.begin();
141 for( ; anIter != anIdContainer.end(); anIter++)
142 theName += QString(" %1").arg(*anIter);
144 return aMapIndex.Extent();
147 string shape2string( const TopoDS_Shape& aShape )
149 ostringstream streamShape;
150 BRepTools::Write(aShape, streamShape);
152 return streamShape.str();
155 void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
156 const Handle(AIS_InteractiveContext)& theIC,
157 SelectMgr_IndexedMapOfOwner& theMap )
159 if ( theObj.IsNull() || theIC.IsNull() )
162 TColStd_ListOfInteger modes;
163 theIC->ActivatedModes( theObj, modes );
165 TColStd_ListIteratorOfListOfInteger itr( modes );
166 for (; itr.More(); itr.Next() ) {
168 if ( !theObj->HasSelection( m ) )
171 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
173 for ( sel->Init(); sel->More(); sel->Next() ) {
174 const Handle(SelectMgr_SensitiveEntity) aHSenEntity = sel->Sensitive();
175 if ( aHSenEntity.IsNull() )
177 Handle(SelectBasics_SensitiveEntity) entity = aHSenEntity->BaseSensitive();
178 if ( entity.IsNull() )
181 Handle(SelectMgr_EntityOwner) owner =
182 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
183 if ( !owner.IsNull() )
189 void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
190 const TopoDS_Shape& aMainShape,
191 const SelectMgr_IndexedMapOfOwner& anAllMap,
192 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
194 TopTools_IndexedMapOfShape aMapOfShapes;
195 TopExp::MapShapes(aMainShape, aMapOfShapes);
197 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ )
199 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
200 if ( anOwner.IsNull() || !anOwner->HasShape() )
203 const TopoDS_Shape& aSubShape = anOwner->Shape();
204 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
205 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
208 if ( !aToHiliteMap.Contains( anOwner ) )
209 aToHiliteMap.Add( anOwner );
213 TopoDS_Shape getSubShape(const TopoDS_Shape& theShape, const int theIndex)
215 TopoDS_Shape theSubShape;
217 if (theShape.IsNull() || theIndex < 1)
220 TopTools_IndexedMapOfShape anIndices;
221 TopExp::MapShapes(theShape, anIndices);
222 if (theIndex > anIndices.Extent())
224 theSubShape = anIndices.FindKey(theIndex);
229 int getSubId(const TopoDS_Shape& theShape, const TopoDS_Shape& theSubShape)
231 if (theShape.IsNull() || theSubShape.IsNull())
234 TopTools_IndexedMapOfShape anIndices;
235 TopExp::MapShapes(theShape, anIndices);
237 return anIndices.FindIndex(theSubShape);
240 Standard_Boolean getExtremaSolution(const gp_Pnt& theInitPnt,
241 const TopoDS_Shape& theRefShape,
244 BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
245 TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
247 BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
248 if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
249 return Standard_False;
250 thePnt = anExt.PointOnShape2(1);
251 Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
252 for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
254 gp_Pnt aPnt = anExt.PointOnShape2(j);
255 Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
256 if ( aDist2 > aMinDist2)
261 return Standard_True;
264 TopoDS_Vertex makePoint(const double x, const double y, const double z)
266 gp_Pnt thePoint(x, y, z);
267 return BRepBuilderAPI_MakeVertex(thePoint);
270 TopoDS_Vertex makePointWithReference(const TopoDS_Shape& point, const double dx,
277 if (point.ShapeType() != TopAbs_VERTEX) {
278 Standard_TypeMismatch::Raise("Aborted: referenced shape is not a vertex");
281 gp_Pnt refPoint = BRep_Tool::Pnt(TopoDS::Vertex(point));
282 thePoint = gp_Pnt(refPoint.X() + dx, refPoint.Y() + dy, refPoint.Z() + dz);
284 return BRepBuilderAPI_MakeVertex(thePoint);
287 TopoDS_Vertex makePointOnCurve(const TopoDS_Shape& edge, const double param)
292 if (edge.ShapeType() != TopAbs_EDGE) {
293 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
296 Standard_Real aFP, aLP, aP;
297 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(edge), aFP, aLP);
298 aP = aFP + (aLP - aFP) * param;
299 thePoint = aCurve->Value(aP);
301 return BRepBuilderAPI_MakeVertex(thePoint);
304 TopoDS_Vertex makePointOnCurveByLength(const TopoDS_Shape& edge, const TopoDS_Shape& point, const double length)
310 if (edge.ShapeType() != TopAbs_EDGE)
312 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
315 TopoDS_Edge aRefEdge = TopoDS::Edge(edge);
316 TopoDS_Vertex V1, V2;
317 TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
320 TopoDS_Vertex aRefVertex;
325 if (point.ShapeType() != TopAbs_VERTEX)
327 Standard_TypeMismatch::Raise("Aborted: shape is not a vertex");
330 aRefVertex = TopoDS::Vertex(point);
332 gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
335 Standard_Real UFirst, ULast;
336 Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
337 Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
339 Standard_Real dU = ULast - UFirst;
340 Standard_Real par1 = UFirst + 0.1 * dU;
341 Standard_Real par2 = ULast - 0.1 * dU;
343 gp_Pnt P1 = EdgeCurve->Value(par1);
344 gp_Pnt P2 = EdgeCurve->Value(par2);
346 if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
347 ReOrientedCurve = EdgeCurve->Reversed();
348 UFirst = EdgeCurve->ReversedParameter(ULast);
351 // Get the point by length
352 GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
353 GCPnts_AbscissaPoint anAbsPnt (AdapCurve, length, UFirst);
354 Standard_Real aParam = anAbsPnt.Parameter();
355 thePoint = AdapCurve.Value(aParam);
357 return BRepBuilderAPI_MakeVertex(thePoint);
360 TopoDS_Vertex makePointOnCurveByCoord(const TopoDS_Shape& edge, const double x, const double y, const double z)
365 if (edge.ShapeType() != TopAbs_EDGE) {
366 Standard_TypeMismatch::Raise("Aborted: curve shape is not an edge");
369 gp_Pnt anInitPnt (x, y, z);
370 if (!getExtremaSolution(anInitPnt, edge, thePoint)) {
371 Standard_ConstructionError::Raise
372 ("Point On Surface creation aborted : cannot project point");
376 return BRepBuilderAPI_MakeVertex(thePoint);
379 TopoDS_Vertex makePointOnLinesIntersection(const TopoDS_Shape& line1, const TopoDS_Shape& line2)
384 if ( (line1.ShapeType() != TopAbs_EDGE && line1.ShapeType() != TopAbs_WIRE)
385 || (line2.ShapeType() != TopAbs_EDGE && line2.ShapeType() != TopAbs_WIRE) ) {
386 Standard_TypeMismatch::Raise("Aborted: Line shape is not an edge or wire");
390 if (line1.IsSame(line2))
392 Standard_ConstructionError::Raise("The lines to make intersection must be different");
396 TopoDS_Compound aCompound;
397 bool retCompound = false;
399 //Calculate Lines Intersection Point
400 BRepExtrema_DistShapeShape dst (line1, line2);
404 B.MakeCompound( aCompound );
405 for (int i = 1, nbSols = dst.NbSolution(); i <= nbSols; i++) {
406 P1 = dst.PointOnShape1(i);
407 P2 = dst.PointOnShape2(i);
408 Standard_Real Dist = P1.Distance(P2);
409 if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
410 BRepBuilderAPI_MakeVertex mkVertex (P1);
411 B.Add(aCompound, mkVertex.Shape());
413 } else if ( Dist <= Precision::Confusion() ) {
416 Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
420 // TopoDS_Shape aShape;
422 Standard_TypeMismatch::Raise
423 ("Aborted: Intersection is a compound of vertices (Not supported)");
424 //aShape = aCompound;
427 BRepBuilderAPI_MakeVertex mkVertex (thePoint);
428 aShape = mkVertex.Shape();
432 return BRepBuilderAPI_MakeVertex(thePoint);
435 TopoDS_Vertex makePointOnSurface(const TopoDS_Shape& face, const double param_u, const double param_v)
440 if (face.ShapeType() != TopAbs_FACE) {
441 Standard_TypeMismatch::Raise("Aborted: surface shape is not a face");
445 TopoDS_Face F = TopoDS::Face(face);
446 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
447 Standard_Real U1,U2,V1,V2;
448 ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
449 Standard_Real U = U1 + (U2-U1) * param_u;
450 Standard_Real V = V1 + (V2-V1) * param_v;
451 thePoint = aSurf->Value(U,V);
453 return BRepBuilderAPI_MakeVertex(thePoint);
456 TopoDS_Vertex makePointOnSurfaceByCoord(const TopoDS_Shape& face, const double x, const double y, const double z)
461 if (face.ShapeType() != TopAbs_FACE) {
462 Standard_TypeMismatch::Raise
463 ("Point On Surface creation aborted : surface shape is not a face");
466 gp_Pnt anInitPnt (x, y, z);
467 if (!getExtremaSolution(anInitPnt, face, thePoint)) {
468 Standard_ConstructionError::Raise
469 ("Point On Surface creation aborted : cannot project point");
472 return BRepBuilderAPI_MakeVertex(thePoint);