0023831: Ghosts appears in 2D viewer of HLR MFC sample
[occt.git] / samples / mfc / standard / Common / ISession2D / ISession2D_Shape.cpp
1 #include "stdafx.h"
2
3 #include "ISession2D_Shape.h"
4 #include <TopTools_ListIteratorOfListOfShape.hxx>
5 #include <HLRBRep_PolyHLRToShape.hxx>
6 #include <HLRBRep_HLRToShape.hxx>
7 #include <TopExp.hxx>
8
9 IMPLEMENT_STANDARD_HANDLE(ISession2D_Shape,AIS_InteractiveObject)
10 IMPLEMENT_STANDARD_RTTIEXT(ISession2D_Shape,AIS_InteractiveObject)
11
12 ISession2D_Shape::ISession2D_Shape ()
13     :AIS_InteractiveObject(PrsMgr_TOP_ProjectorDependant)
14 {}
15
16 void ISession2D_Shape::Add(const TopoDS_Shape& aShape)
17 {
18   myListOfShape.Append(aShape);
19   myAlgo.Nullify();
20   myPolyAlgo.Nullify();
21   Update(); // protected method used to specify that the presentation are not up to date 
22 }
23
24 void ISession2D_Shape::SetProjector(HLRAlgo_Projector& aProjector) 
25 {
26   myProjector= aProjector;
27   myAlgo.Nullify();
28   myPolyAlgo.Nullify();
29   Update(); // protected method used to specify that the presentation are not up to date 
30 };
31
32
33 void ISession2D_Shape::SetNbIsos(Standard_Integer& aNbIsos)
34
35         myNbIsos= aNbIsos; 
36         myAlgo.Nullify(); 
37     
38         // declare the mode 100 to 110 as non valid
39         for (int i=100;i<=110;i++)
40        Update(i,Standard_False); // protected method used to specify that the presentation are not up to date 
41
42         // declare the mode 1100 to 1110 as non valid
43         for (int i=1100;i<=1110;i++)
44        Update(i,Standard_False); // protected method used to specify that the presentation are not up to date 
45
46 };
47
48 void ISession2D_Shape::BuildAlgo() 
49 {
50   myAlgo = new HLRBRep_Algo();
51   TopTools_ListIteratorOfListOfShape anIterator(myListOfShape);
52   for (;anIterator.More();anIterator.Next()) myAlgo->Add(anIterator.Value(),myNbIsos);
53   myAlgo->Projector(myProjector);
54   myAlgo->Update();
55   myAlgo->Hide();
56
57 }
58 void ISession2D_Shape::BuildPolyAlgo() 
59 {
60   myPolyAlgo = new HLRBRep_PolyAlgo();
61   TopTools_ListIteratorOfListOfShape anIterator(myListOfShape);
62   for (;anIterator.More();anIterator.Next()) myPolyAlgo->Load(anIterator.Value());
63   myPolyAlgo->Projector(myProjector);
64   myPolyAlgo->Update();
65 }
66
67 void ISession2D_Shape::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
68                              const Handle(Prs3d_Presentation)& thePresentation,
69                              const Standard_Integer theMode) 
70 {
71   
72   Standard_Integer aMode = theMode;
73   Standard_Boolean DrawHiddenLine= Standard_True;
74   thePresentation->Clear();
75   if (aMode >= 1000)
76   {
77     DrawHiddenLine = Standard_False;
78     aMode -= 1000;
79   }
80
81   Standard_Boolean UsePolyAlgo= Standard_True;
82   if (aMode >= 100)
83   {
84     UsePolyAlgo = Standard_False;
85     aMode -= 100;
86   }
87   TopoDS_Shape VCompound;
88   TopoDS_Shape Rg1LineVCompound;
89   TopoDS_Shape RgNLineVCompound;
90   TopoDS_Shape OutLineVCompound;
91   TopoDS_Shape IsoLineVCompound;  // only for Exact algo
92   TopoDS_Shape HCompound;
93   TopoDS_Shape Rg1LineHCompound;
94   TopoDS_Shape RgNLineHCompound;
95   TopoDS_Shape OutLineHCompound;
96   TopoDS_Shape IsoLineHCompound;  // only for Exact algo
97
98   if (UsePolyAlgo)
99   {
100     if (myPolyAlgo.IsNull()) BuildPolyAlgo();
101     HLRBRep_PolyHLRToShape aPolyHLRToShape;
102     aPolyHLRToShape.Update(myPolyAlgo);
103
104     VCompound        = aPolyHLRToShape.VCompound();
105     Rg1LineVCompound = aPolyHLRToShape.Rg1LineVCompound();
106     RgNLineVCompound = aPolyHLRToShape.RgNLineVCompound();
107     OutLineVCompound = aPolyHLRToShape.OutLineVCompound();
108     HCompound        = aPolyHLRToShape.HCompound();
109     Rg1LineHCompound = aPolyHLRToShape.Rg1LineHCompound();
110     RgNLineHCompound = aPolyHLRToShape.RgNLineHCompound();
111     OutLineHCompound = aPolyHLRToShape.OutLineHCompound();
112   }
113   else
114   {
115     if (myAlgo.IsNull()) BuildAlgo();
116     HLRBRep_HLRToShape aHLRToShape(myAlgo);
117
118     VCompound        = aHLRToShape.VCompound();
119     Rg1LineVCompound = aHLRToShape.Rg1LineVCompound();
120     RgNLineVCompound = aHLRToShape.RgNLineVCompound();
121     OutLineVCompound = aHLRToShape.OutLineVCompound();
122     IsoLineVCompound = aHLRToShape.IsoLineVCompound();
123     HCompound        = aHLRToShape.HCompound();
124     Rg1LineHCompound = aHLRToShape.Rg1LineHCompound();
125     RgNLineHCompound = aHLRToShape.RgNLineHCompound();
126     OutLineHCompound = aHLRToShape.OutLineHCompound();
127     IsoLineHCompound = aHLRToShape.IsoLineHCompound();
128   }
129
130   if (UsePolyAlgo)
131   {
132     Handle(Prs3d_LineAspect) aLineAspectHighlighted = new Prs3d_LineAspect(Quantity_NOC_ALICEBLUE,
133       Aspect_TOL_DOTDASH,1);
134     Handle(Prs3d_LineAspect) aLineAspect = new Prs3d_LineAspect(Quantity_NOC_WHITE,
135       Aspect_TOL_SOLID,1);
136
137   
138     switch(aMode)
139     {
140     case (1):
141       {
142         DrawCompound(thePresentation, VCompound, aLineAspectHighlighted);
143         break;
144       }
145     case (2):
146       {
147         DrawCompound(thePresentation, Rg1LineVCompound, aLineAspectHighlighted);
148         break;
149       }
150     case (3):
151       {
152         DrawCompound(thePresentation, RgNLineVCompound, aLineAspectHighlighted);
153         break;
154       }
155     case (4):
156       {
157         DrawCompound(thePresentation, OutLineVCompound, aLineAspectHighlighted);
158         break;
159       }
160     default:
161       {
162         DrawCompound(thePresentation,VCompound, aLineAspect);
163         DrawCompound(thePresentation,Rg1LineVCompound, aLineAspect);
164         DrawCompound(thePresentation,RgNLineVCompound, aLineAspect);
165         DrawCompound(thePresentation,OutLineVCompound, aLineAspect);
166       }
167     }
168
169     if (DrawHiddenLine)
170     {
171       Handle(Prs3d_LineAspect) aLineAspectHighlighted = new Prs3d_LineAspect(Quantity_NOC_RED,
172         Aspect_TOL_DOTDASH,2);
173       Handle(Prs3d_LineAspect) aLineAspect = new Prs3d_LineAspect(Quantity_NOC_BLUE1,
174         Aspect_TOL_DOTDASH,1);
175
176       switch(aMode)
177       {
178       case (6):
179         {
180           DrawCompound(thePresentation, HCompound, aLineAspectHighlighted);
181           break;
182         }
183       case (7):
184         {
185           DrawCompound(thePresentation, Rg1LineHCompound, aLineAspectHighlighted);
186           break;
187         }
188       case (8):
189         {
190           DrawCompound(thePresentation, RgNLineHCompound, aLineAspectHighlighted);
191           break;
192         }
193       case (9):
194         {
195           DrawCompound(thePresentation, OutLineHCompound, aLineAspectHighlighted);
196           break;
197         }
198       default:
199         {
200           DrawCompound(thePresentation, HCompound, aLineAspect);
201           DrawCompound(thePresentation, Rg1LineHCompound, aLineAspect);
202           DrawCompound(thePresentation, RgNLineHCompound, aLineAspect);
203           DrawCompound(thePresentation, OutLineHCompound, aLineAspect);
204         }
205       }
206     }
207   }
208   else
209   {
210     Handle(Prs3d_LineAspect) aLineAspectHighlighted = new Prs3d_LineAspect(Quantity_NOC_RED,
211       Aspect_TOL_SOLID,2);
212     Handle(Prs3d_LineAspect) aLineAspect = new Prs3d_LineAspect(Quantity_NOC_WHITE,
213       Aspect_TOL_SOLID,1);  
214
215     switch (aMode)
216     {
217     case (1):
218       {
219         DrawCompound(thePresentation, VCompound, aLineAspectHighlighted);
220         break;
221       }
222     case (2):
223       {
224         DrawCompound(thePresentation, Rg1LineVCompound, aLineAspectHighlighted);
225         break;
226       }
227     case (3):
228       {
229         DrawCompound(thePresentation, RgNLineVCompound, aLineAspectHighlighted);
230         break;
231       }
232     case (4):
233       {
234         DrawCompound(thePresentation, OutLineVCompound, aLineAspectHighlighted);
235         break;
236       }
237     case (5):
238       {
239         DrawCompound(thePresentation, IsoLineVCompound, aLineAspectHighlighted);
240         break;
241       }
242     default:
243       {
244         DrawCompound(thePresentation, VCompound, aLineAspect);
245         DrawCompound(thePresentation, Rg1LineVCompound, aLineAspect);
246         DrawCompound(thePresentation, RgNLineVCompound, aLineAspect);
247         DrawCompound(thePresentation, OutLineVCompound, aLineAspect);
248         DrawCompound(thePresentation, IsoLineVCompound , aLineAspect);
249       }
250     }
251
252     if (DrawHiddenLine)
253     {
254       Handle(Prs3d_LineAspect) aLineAspectHighlighted = new Prs3d_LineAspect(Quantity_NOC_RED,
255         Aspect_TOL_DOT,2);
256       Handle(Prs3d_LineAspect) aLineAspect = new Prs3d_LineAspect(Quantity_NOC_ALICEBLUE,
257         Aspect_TOL_DOT,1);  
258
259             switch(aMode)
260       {
261       case (6):
262         {
263           DrawCompound(thePresentation, HCompound, aLineAspectHighlighted);
264           break;
265         }
266       case (7):
267         {
268           DrawCompound(thePresentation, Rg1LineHCompound, aLineAspectHighlighted);
269           break;
270         }
271       case (8):
272         {
273           DrawCompound(thePresentation, RgNLineHCompound, aLineAspectHighlighted);
274           break;
275         }
276       case (9):
277         {
278           DrawCompound(thePresentation, OutLineHCompound, aLineAspectHighlighted);
279           break;
280         }
281       case (10):
282         {
283           DrawCompound(thePresentation, IsoLineHCompound, aLineAspectHighlighted);
284           break;
285         }
286       default:
287         {
288           DrawCompound(thePresentation, HCompound, aLineAspect);
289           DrawCompound(thePresentation, Rg1LineHCompound, aLineAspect);
290           DrawCompound(thePresentation, RgNLineHCompound, aLineAspect);
291           DrawCompound(thePresentation, OutLineHCompound, aLineAspect);
292           DrawCompound(thePresentation, IsoLineHCompound, aLineAspect);
293         }
294       }
295     }
296   }
297 }
298
299 void ISession2D_Shape::DrawCompound(const Handle(Prs3d_Presentation)& thePresentation,
300                                    const TopoDS_Shape& theCompound, 
301                                    const Handle(Prs3d_LineAspect) theAspect)
302 {
303   if (theCompound.IsNull())
304     return;
305   myDrawer->SetWireAspect(theAspect);
306   StdPrs_WFDeflectionShape::Add(thePresentation,TopoDS_Shape(theCompound),myDrawer);
307 }
308
309 void ISession2D_Shape::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
310                                       const Standard_Integer aMode) 
311 {
312
313 }
314