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