Salome HOME
9a064d4fd1a08e5ca72c74b373a52648e03925f3
[modules/shaper.git] / src / ModuleBase / ModuleBase_ArrowPrs.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_ArrowPrs.h"
21
22 #include <Prs3d_ArrowAspect.hxx>
23
24 #include <Events_Loop.h>
25 #include <ModelAPI_Events.h>
26
27 #include <BRep_Builder.hxx>
28 #include <BRep_Tool.hxx>
29 #include <GCPnts_AbscissaPoint.hxx>
30 #include <GeomAdaptor_Curve.hxx>
31 #include <Prs3d_Arrow.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopExp.hxx>
35
36
37 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ArrowPrs, AIS_InteractiveContext)
38
39
40 ModuleBase_ArrowPrs::ModuleBase_ArrowPrs(const Handle(V3d_Viewer)& theViewer,
41                                          const GeomEdgePtr& theEdge)
42   : AIS_InteractiveContext(theViewer),
43     myEdge(theEdge)
44 {
45 }
46
47 //********************************************************************
48 void ModuleBase_ArrowPrs::DrawArrow(const Handle(Prs3d_Presentation)& thePrs,
49                                     Quantity_Color theColor)
50 {
51   Handle(Prs3d_Drawer) aDrawer = myDefaultDrawer;
52   Handle(Prs3d_ArrowAspect) anArrowAspect = aDrawer->ArrowAspect();
53
54   Handle(Graphic3d_AspectLine3d) PtA = anArrowAspect->Aspect();
55   PtA->SetColor(theColor);
56
57   Handle(Graphic3d_Group) TheGroup = thePrs->CurrentGroup();
58   TheGroup->SetPrimitivesAspect(PtA);
59
60   TopoDS_Vertex aV1, aV2;
61   TopoDS_Edge anEdgeE = myEdge->impl<TopoDS_Edge>();
62   anEdgeE.Orientation(TopAbs_FORWARD);
63   if (anEdgeE.IsNull()) return;
64
65   TopExp::Vertices(anEdgeE, aV1, aV2);
66   gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
67   gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
68
69   double fp, lp;
70   gp_Vec aDirVec;
71   Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE, fp, lp);
72
73   if (C.IsNull()) return;
74
75   if (anEdgeE.Orientation() == TopAbs_FORWARD)
76     C->D1(lp, aP2, aDirVec);
77   else {
78     C->D1(fp, aP1, aDirVec);
79     aP2 = aP1;
80   }
81
82   GeomAdaptor_Curve aAdC;
83   aAdC.Load(C, fp, lp);
84   Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
85
86   if (aDist > gp::Resolution()) {
87     gp_Dir aDir;
88     if (anEdgeE.Orientation() == TopAbs_FORWARD)
89       aDir = aDirVec;
90     else
91       aDir = -aDirVec;
92
93     TopoDS_Vertex aVertex;
94     BRep_Builder aB;
95     aB.MakeVertex(aVertex, aP2, Precision::Confusion());
96     Prs3d_Arrow::Draw(TheGroup, aP2, aDir, M_PI / 180. * 5., aDist / 10.);
97   }
98 }
99
100 //********************************************************************
101 bool ModuleBase_ArrowPrs::Comparator::operator()(const std::shared_ptr<GeomAPI_Edge>& theEdge1,
102                                                  const std::shared_ptr<GeomAPI_Edge>& theEdge2) const
103 {
104   const TopoDS_Edge& aShape1 = theEdge1->impl<TopoDS_Edge>();
105   const TopoDS_Edge& aShape2 = theEdge2->impl<TopoDS_Edge>();
106   bool isLess = aShape1.TShape() < aShape2.TShape();
107   if (aShape1.TShape() == aShape2.TShape()) {
108     Standard_Integer aHash1 = aShape1.Location().HashCode(IntegerLast());
109     Standard_Integer aHash2 = aShape2.Location().HashCode(IntegerLast());
110     isLess = aHash1 < aHash2;
111   }
112   return isLess;
113 }