0031431: Visualization, PrsMgr_PresentableObject - simplify HLR computing interface
[occt.git] / src / AIS / AIS_Line.cxx
CommitLineData
b311480e 1// Created on: 1997-01-21
2// Created by: Prestataire Christiane ARMAND
3// Copyright (c) 1997-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
87432b82 17#include <AIS_Line.hxx>
42cf5bc1 18
19#include <AIS_GraphicTool.hxx>
7fd59977 20#include <Aspect_TypeOfLine.hxx>
42cf5bc1 21#include <GC_MakeSegment.hxx>
22#include <Geom_Line.hxx>
23#include <Geom_Point.hxx>
24#include <Geom_Transformation.hxx>
25#include <GeomAdaptor_Curve.hxx>
7fd59977 26#include <Graphic3d_AspectLine3d.hxx>
27#include <Graphic3d_Structure.hxx>
42cf5bc1 28#include <Precision.hxx>
29#include <Prs3d_Drawer.hxx>
30#include <Prs3d_LineAspect.hxx>
31#include <Prs3d_Presentation.hxx>
42cf5bc1 32#include <Quantity_Color.hxx>
33#include <Select3D_SensitiveSegment.hxx>
7fd59977 34#include <SelectMgr_EntityOwner.hxx>
f751596e 35#include <SelectMgr_Selection.hxx>
42cf5bc1 36#include <Standard_Type.hxx>
7fd59977 37#include <StdPrs_Curve.hxx>
42cf5bc1 38#include <TColgp_Array1OfPnt.hxx>
7fd59977 39#include <UnitsAPI.hxx>
cb389a77 40
92efcf78 41IMPLEMENT_STANDARD_RTTIEXT(AIS_Line,AIS_InteractiveObject)
42
7fd59977 43//=======================================================================
44//function : AIS_Line
45//purpose :
46//=======================================================================
47AIS_Line::AIS_Line(const Handle(Geom_Line)& aComponent):
48myComponent (aComponent),
49myLineIsSegment(Standard_False)
50{
51 SetInfiniteState();
52}
53
54//=======================================================================
55//function : AIS_Line
56//purpose :
57//=======================================================================
58AIS_Line::AIS_Line(const Handle(Geom_Point)& aStartPoint,
59 const Handle(Geom_Point)& aEndPoint):
60myStartPoint(aStartPoint),
61myEndPoint(aEndPoint),
62myLineIsSegment(Standard_True)
63{}
64
65//=======================================================================
66//function : Compute
67//purpose :
68//=======================================================================
69void AIS_Line::Compute(const Handle(PrsMgr_PresentationManager3d)&,
70 const Handle(Prs3d_Presentation)& aPresentation,
71 const Standard_Integer)
72{
7fd59977 73 aPresentation->SetDisplayPriority(5);
74
75 if (!myLineIsSegment) ComputeInfiniteLine(aPresentation);
76 else ComputeSegmentLine(aPresentation);
77
78}
79
7fd59977 80//=======================================================================
81//function : ComputeSelection
82//purpose :
83//=======================================================================
84
270675f5 85void AIS_Line::ComputeSelection(const Handle(SelectMgr_Selection)& theSelection,
86 const Standard_Integer theMode)
7fd59977 87{
270675f5 88 // Do not support selection modes different from 0 currently
89 if (theMode)
90 return;
91
92 if (!myLineIsSegment)
93 {
94 ComputeInfiniteLineSelection(theSelection);
95 }
96 else
97 {
98 ComputeSegmentLineSelection(theSelection);
99 }
7fd59977 100}
101
226fce20 102//=======================================================================
103//function : replaceWithNewLineAspect
104//purpose :
105//=======================================================================
106void AIS_Line::replaceWithNewLineAspect (const Handle(Prs3d_LineAspect)& theAspect)
107{
108 if (!myDrawer->HasLink())
109 {
110 myDrawer->SetLineAspect (theAspect);
111 return;
112 }
113
114 const Handle(Graphic3d_Aspects)& anAspectOld = myDrawer->LineAspect()->Aspect();
115 const Handle(Graphic3d_Aspects)& anAspectNew = !theAspect.IsNull() ? theAspect->Aspect() : myDrawer->Link()->LineAspect()->Aspect();
116 if (anAspectNew != anAspectOld)
117 {
118 myDrawer->SetLineAspect (theAspect);
119 Graphic3d_MapOfAspectsToAspects aReplaceMap;
120 aReplaceMap.Bind (anAspectOld, anAspectNew);
121 replaceAspects (aReplaceMap);
122 }
123}
124
7fd59977 125//=======================================================================
126//function : SetColor
87432b82 127//purpose :
7fd59977 128//=======================================================================
7fd59977 129void AIS_Line::SetColor(const Quantity_Color &aCol)
7fd59977 130{
131 hasOwnColor=Standard_True;
f838dac4 132 myDrawer->SetColor (aCol);
7fd59977 133
134 Standard_Real WW = HasWidth()? myOwnWidth:
6262338c 135 myDrawer->HasLink() ?
136 AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Line) : 1.;
7fd59977 137
226fce20 138 if (!myDrawer->HasOwnLineAspect())
139 {
140 replaceWithNewLineAspect (new Prs3d_LineAspect (aCol, Aspect_TOL_SOLID, WW));
141 }
7fd59977 142 else
226fce20 143 {
144 myDrawer->LineAspect()->SetColor (aCol);
145 SynchronizeAspects();
146 }
7fd59977 147}
148
149
150//=======================================================================
151//function : UnsetColor
152//purpose :
153//=======================================================================
154void AIS_Line::UnsetColor()
155{
156 hasOwnColor = Standard_False;
157
226fce20 158 if (!HasWidth())
159 {
160 replaceWithNewLineAspect (Handle(Prs3d_LineAspect)());
161 }
162 else
163 {
6262338c 164 Quantity_Color CC = Quantity_NOC_YELLOW;
f838dac4 165 if( HasColor() ) CC = myDrawer->Color();
6262338c 166 else if (myDrawer->HasLink()) AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Line, CC);
7fd59977 167 myDrawer->LineAspect()->SetColor(CC);
f838dac4 168 myDrawer->SetColor (CC);
226fce20 169 SynchronizeAspects();
170 }
7fd59977 171}
172
173//=======================================================================
174//function : SetWidth
175//purpose :
176//=======================================================================
177void AIS_Line::SetWidth(const Standard_Real aValue)
178{
226fce20 179 myOwnWidth = (Standard_ShortReal )aValue;
7fd59977 180
226fce20 181 if (!myDrawer->HasOwnLineAspect())
182 {
6262338c 183 Quantity_Color CC = Quantity_NOC_YELLOW;
f838dac4 184 if( HasColor() ) CC = myDrawer->Color();
6262338c 185 else if(myDrawer->HasLink()) AIS_GraphicTool::GetLineColor (myDrawer->Link(), AIS_TOA_Line, CC);
226fce20 186 replaceWithNewLineAspect (new Prs3d_LineAspect (CC, Aspect_TOL_SOLID, aValue));
187 }
188 else
189 {
190 myDrawer->LineAspect()->SetWidth (aValue);
191 SynchronizeAspects();
192 }
7fd59977 193}
194
195
196//=======================================================================
197//function : UnsetWidth
198//purpose :
199//=======================================================================
200void AIS_Line::UnsetWidth()
201{
226fce20 202 if (!HasColor())
203 {
204 replaceWithNewLineAspect (Handle(Prs3d_LineAspect)());
205 }
206 else
207 {
208 Standard_ShortReal WW = myDrawer->HasLink() ? (Standard_ShortReal )AIS_GraphicTool::GetLineWidth (myDrawer->Link(), AIS_TOA_Line) : 1.0f;
209 myDrawer->LineAspect()->SetWidth (WW);
7fd59977 210 myOwnWidth = WW;
226fce20 211 SynchronizeAspects();
7fd59977 212 }
213}
214
215//=======================================================================
216//function : ComputeInfiniteLine
217//purpose :
218//=======================================================================
219void AIS_Line::ComputeInfiniteLine( const Handle(Prs3d_Presentation)& aPresentation)
220{
7fd59977 221 GeomAdaptor_Curve curv(myComponent);
7fd59977 222 StdPrs_Curve::Add(aPresentation,curv,myDrawer);
7fd59977 223
224 //pas de prise en compte lors du FITALL
225 aPresentation->SetInfiniteState (Standard_True);
7fd59977 226}
227
228//=======================================================================
229//function : ComputeSegmentLine
230//purpose :
231//=======================================================================
232void AIS_Line::ComputeSegmentLine( const Handle(Prs3d_Presentation)& aPresentation)
233{
7fd59977 234 gp_Pnt P1 = myStartPoint->Pnt();
235 gp_Pnt P2 = myEndPoint->Pnt();
236
237 myComponent = new Geom_Line(P1,gp_Dir(P2.XYZ()-P1.XYZ()));
238
239 Standard_Real dist = P1.Distance(P2);
240 GeomAdaptor_Curve curv(myComponent,0.,dist);
7fd59977 241 StdPrs_Curve::Add(aPresentation,curv,myDrawer);
7fd59977 242}
243
244
245//=======================================================================
246//function : ComputeInfiniteLineSelection
247//purpose :
248//=======================================================================
249
250void AIS_Line::ComputeInfiniteLineSelection(const Handle(SelectMgr_Selection)& aSelection)
251{
252
253/* // on calcule les points min max a partir desquels on cree un segment sensible...
254 GeomAdaptor_Curve curv(myComponent);
255 gp_Pnt P1,P2;
256 FindLimits(curv,myDrawer->MaximalParameterValue(),P1,P2);
257*/
258 const gp_Dir& thedir = myComponent->Position().Direction();
259 const gp_Pnt& loc = myComponent->Position().Location();
260 const gp_XYZ& dir_xyz = thedir.XYZ();
261 const gp_XYZ& loc_xyz = loc.XYZ();
262//POP Standard_Real aLength = UnitsAPI::CurrentToLS (250000. ,"LENGTH");
263 Standard_Real aLength = UnitsAPI::AnyToLS (250000. ,"mm");
264 gp_Pnt P1 = loc_xyz + aLength*dir_xyz;
265 gp_Pnt P2 = loc_xyz - aLength*dir_xyz;
266 Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner(this,5);
267 Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(eown,P1,P2);
268 aSelection->Add(seg);
269}
270//=======================================================================
271//function : ComputeSegmentLineSelection
272//purpose :
273//=======================================================================
274
275void AIS_Line::ComputeSegmentLineSelection(const Handle(SelectMgr_Selection)& aSelection)
276{
277
278
279 Handle(SelectMgr_EntityOwner) eown = new SelectMgr_EntityOwner(this,5);
280 Handle(Select3D_SensitiveSegment) seg = new Select3D_SensitiveSegment(eown,
281 myStartPoint->Pnt(),
282 myEndPoint->Pnt());
283 aSelection->Add(seg);
284}