0027104: DownCast() cannot return null for mismatched handle
[occt.git] / src / IVtkDraw / IVtkDraw_HighlightAndSelectionPipeline.cxx
1 // Created on: 2012-04-02 
2
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS 
4 // 
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <IVtkDraw_HighlightAndSelectionPipeline.hxx>
17
18 #include <vtkRenderer.h>
19 #include <vtkActor.h>
20 #include <vtkPolyDataMapper.h>
21 #include <vtkPolyData.h>
22 #include <vtkAppendPolyData.h>
23 #include <vtkProperty.h>
24
25 #include <IVtkOCC_Shape.hxx>
26 #include <IVtkTools_DisplayModeFilter.hxx>
27 #include <IVtkTools_ShapeDataSource.hxx>
28 #include <IVtkTools_ShapeObject.hxx>
29
30
31 IMPLEMENT_STANDARD_RTTIEXT(IVtkDraw_HighlightAndSelectionPipeline,Standard_Transient)
32
33 //===========================================================
34 // Function : Constructor
35 // Purpose  :
36 //===========================================================
37
38 IVtkDraw_HighlightAndSelectionPipeline::IVtkDraw_HighlightAndSelectionPipeline (const TopoDS_Shape& theShape,
39                                                                                 const Standard_Integer theShapeID)
40 : Standard_Transient()
41 {
42   /* ===========================
43    *  Allocate involved filters
44    * =========================== */
45
46   myFilterMap.Bind (Filter_DM_Shape, vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
47   myFilterMap.Bind (Filter_DM_Hili,  vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
48   myFilterMap.Bind (Filter_DM_Sel,   vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
49   myFilterMap.Bind (Filter_SUB_Hili, vtkSmartPointer<IVtkTools_SubPolyDataFilter>::New());
50   myFilterMap.Bind (Filter_SUB_Sel,  vtkSmartPointer<IVtkTools_SubPolyDataFilter>::New());
51
52   /* ========================
53    *  Build primary pipeline
54    * ======================== */
55
56   myActor = vtkSmartPointer<vtkActor>::New();
57   IVtkOCC_Shape::Handle anIVtkShape = new IVtkOCC_Shape (theShape);
58   anIVtkShape->SetId (theShapeID);
59   vtkSmartPointer<IVtkTools_ShapeDataSource> aDataSource = vtkSmartPointer<IVtkTools_ShapeDataSource>::New();
60   aDataSource->SetShape (anIVtkShape);
61
62   IVtkTools_DisplayModeFilter*
63     aDMFilter = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Shape));
64
65   aDMFilter->AddInputConnection (aDataSource->GetOutputPort());
66   aDMFilter->SetDisplayMode (DM_Wireframe);
67
68   myMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
69   myMapper->AddInputConnection (aDMFilter->GetOutputPort());
70   myActor->SetMapper (myMapper);
71   IVtkTools_ShapeObject::SetShapeSource (aDataSource, myActor);
72
73   myMapper->ScalarVisibilityOn();
74   myMapper->SetScalarModeToUseCellFieldData();
75   IVtkTools::InitShapeMapper (myMapper);
76   myMapper->Update();
77
78   /* =================================
79    *  Build pipeline for highlighting
80    * ================================= */
81
82   IVtkTools_DisplayModeFilter*
83     aDMFilterH = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Hili) );
84   IVtkTools_SubPolyDataFilter*
85     aSUBFilterH = IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find(Filter_SUB_Hili) );
86
87   // No highligthing exists initially
88   aSUBFilterH->SetInputConnection (aDataSource->GetOutputPort() );
89   aDMFilterH->SetInputConnection (aSUBFilterH->GetOutputPort() );
90
91   myHiliMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
92   myHiliMapper->SetInputConnection (aDMFilterH->GetOutputPort() );
93
94   // Create non-pickable actor
95   myHiliActor = vtkSmartPointer<vtkActor>::New();
96   myHiliActor->SetPickable(0);
97   myHiliActor->SetVisibility(1);
98   myHiliActor->GetProperty()->SetColor(0, 1, 1);
99   myHiliActor->GetProperty()->SetOpacity(1);
100   myHiliActor->GetProperty()->SetPointSize (myHiliActor->GetProperty()->GetPointSize() + 4 );
101   myHiliActor->GetProperty()->SetLineWidth (myHiliActor->GetProperty()->GetLineWidth() + 2 );
102
103   // Set maper for actor
104   myHiliActor->SetMapper (myHiliMapper);
105   myHiliMapper->ScalarVisibilityOff();
106
107   /* ==============================
108    *  Build pipeline for selection
109    * ============================== */
110
111   IVtkTools_DisplayModeFilter*
112     aDMFilterS = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Sel) );
113   IVtkTools_SubPolyDataFilter*
114     aSUBFilterS = IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find(Filter_SUB_Sel) );
115
116   // No highligthing exists initially
117   aSUBFilterS->SetInputConnection (aDataSource->GetOutputPort() );
118   aDMFilterS->SetInputConnection (aSUBFilterS->GetOutputPort() );
119
120   mySelMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
121   mySelMapper->SetInputConnection (aDMFilterS->GetOutputPort() );
122
123   // Create non-pickable actor
124   mySelActor = vtkSmartPointer<vtkActor>::New();
125   mySelActor->SetPickable (0);
126   mySelActor->SetVisibility (1);
127   mySelActor->GetProperty()->SetColor (1, 1, 1);
128   mySelActor->GetProperty()->SetOpacity (1);
129   mySelActor->GetProperty()->SetPointSize (myHiliActor->GetProperty()->GetPointSize() + 4 );
130   mySelActor->GetProperty()->SetLineWidth (myHiliActor->GetProperty()->GetLineWidth() + 2 );
131
132   // Set maper for actor
133   mySelActor->SetMapper (mySelMapper);
134   mySelMapper->ScalarVisibilityOff();
135 }
136
137 //===========================================================
138 // Function : AddToRenderer
139 // Purpose  :
140 //===========================================================
141 void IVtkDraw_HighlightAndSelectionPipeline::AddToRenderer (vtkRenderer* theRenderer)
142 {
143   theRenderer->AddActor (myActor);
144   theRenderer->AddActor (myHiliActor);
145   theRenderer->AddActor (mySelActor);
146 }
147
148 //===========================================================
149 // Function : RemoveFromRenderer
150 // Purpose  :
151 //===========================================================
152 void IVtkDraw_HighlightAndSelectionPipeline::RemoveFromRenderer (vtkRenderer* theRenderer)
153 {
154   theRenderer->RemoveActor (myActor);
155   theRenderer->RemoveActor (myHiliActor);
156   theRenderer->RemoveActor (mySelActor);
157 }
158
159 //===========================================================
160 // Function : ClearHighlightFilters
161 // Purpose  :
162 //===========================================================
163 void IVtkDraw_HighlightAndSelectionPipeline::ClearHighlightFilters()
164 {
165   this->GetHighlightFilter()->Clear();
166   this->GetHighlightFilter()->SetDoFiltering (true);
167   this->GetHighlightFilter()->Modified();
168 }
169
170 //===========================================================
171 // Function : ClearSelectionFilters
172 // Purpose  :
173 //===========================================================
174 void IVtkDraw_HighlightAndSelectionPipeline::ClearSelectionFilters()
175 {
176   this->GetSelectionFilter()->Clear();
177   this->GetSelectionFilter()->SetDoFiltering (true);
178   this->GetSelectionFilter()->Modified();
179 }
180
181 //===========================================================
182 // Function : GetDisplayModeFilter
183 // Purpose  :
184 //===========================================================
185 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetDisplayModeFilter()
186 {
187   return IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Shape) );
188 }
189
190 //===========================================================
191 // Function : GetHighlightFilter
192 // Purpose  :
193 //===========================================================
194 IVtkTools_SubPolyDataFilter* IVtkDraw_HighlightAndSelectionPipeline::GetHighlightFilter()
195 {
196   return IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find (Filter_SUB_Hili) );
197 }
198
199 //===========================================================
200 // Function : GetSelectionFilter
201 // Purpose  :
202 //===========================================================
203 IVtkTools_SubPolyDataFilter* IVtkDraw_HighlightAndSelectionPipeline::GetSelectionFilter()
204 {
205   return IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find (Filter_SUB_Sel) );
206 }
207
208 //===========================================================
209 // Function : GetHighlightDMFilter
210 // Purpose  :
211 //===========================================================
212 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetHighlightDMFilter()
213 {
214   return IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find (Filter_DM_Hili) );
215 }
216
217 //===========================================================
218 // Function : GetSelectionDMFilter
219 // Purpose  :
220 //===========================================================
221 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetSelectionDMFilter()
222 {
223   return IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Sel));
224 }
225
226 //===========================================================
227 // Function : SharedVerticesSelectionOn
228 // Purpose  :
229 //===========================================================
230 void IVtkDraw_HighlightAndSelectionPipeline::SharedVerticesSelectionOn()
231 {
232   this->GetHighlightDMFilter()->SetDisplaySharedVertices (Standard_True);
233   this->GetSelectionDMFilter()->SetDisplaySharedVertices (Standard_True);
234 }
235
236 //===========================================================
237 // Function : SharedVerticesSelectionOff
238 // Purpose  :
239 //===========================================================
240 void IVtkDraw_HighlightAndSelectionPipeline::SharedVerticesSelectionOff()
241 {
242   this->GetHighlightDMFilter()->SetDisplaySharedVertices (Standard_False);
243   this->GetSelectionDMFilter()->SetDisplaySharedVertices (Standard_False);
244 }