0024947: Redesign OCCT legacy type system -- automatic
[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 //===========================================================
32 // Function : Constructor
33 // Purpose  :
34 //===========================================================
35
36 IVtkDraw_HighlightAndSelectionPipeline::IVtkDraw_HighlightAndSelectionPipeline (const TopoDS_Shape& theShape,
37                                                                                 const Standard_Integer theShapeID)
38 : Standard_Transient()
39 {
40   /* ===========================
41    *  Allocate involved filters
42    * =========================== */
43
44   myFilterMap.Bind (Filter_DM_Shape, vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
45   myFilterMap.Bind (Filter_DM_Hili,  vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
46   myFilterMap.Bind (Filter_DM_Sel,   vtkSmartPointer<IVtkTools_DisplayModeFilter>::New());
47   myFilterMap.Bind (Filter_SUB_Hili, vtkSmartPointer<IVtkTools_SubPolyDataFilter>::New());
48   myFilterMap.Bind (Filter_SUB_Sel,  vtkSmartPointer<IVtkTools_SubPolyDataFilter>::New());
49
50   /* ========================
51    *  Build primary pipeline
52    * ======================== */
53
54   myActor = vtkSmartPointer<vtkActor>::New();
55   IVtkOCC_Shape::Handle anIVtkShape = new IVtkOCC_Shape (theShape);
56   anIVtkShape->SetId (theShapeID);
57   vtkSmartPointer<IVtkTools_ShapeDataSource> aDataSource = vtkSmartPointer<IVtkTools_ShapeDataSource>::New();
58   aDataSource->SetShape (IVtkOCC_Shape::Handle::DownCast (anIVtkShape) );
59
60   IVtkTools_DisplayModeFilter*
61     aDMFilter = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Shape));
62
63   aDMFilter->AddInputConnection (aDataSource->GetOutputPort());
64   aDMFilter->SetDisplayMode (DM_Wireframe);
65
66   myMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
67   myMapper->AddInputConnection (aDMFilter->GetOutputPort());
68   myActor->SetMapper (myMapper);
69   IVtkTools_ShapeObject::SetShapeSource (aDataSource, myActor);
70
71   myMapper->ScalarVisibilityOn();
72   myMapper->SetScalarModeToUseCellFieldData();
73   IVtkTools::InitShapeMapper (myMapper);
74   myMapper->Update();
75
76   /* =================================
77    *  Build pipeline for highlighting
78    * ================================= */
79
80   IVtkTools_DisplayModeFilter*
81     aDMFilterH = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Hili) );
82   IVtkTools_SubPolyDataFilter*
83     aSUBFilterH = IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find(Filter_SUB_Hili) );
84
85   // No highligthing exists initially
86   aSUBFilterH->SetInputConnection (aDataSource->GetOutputPort() );
87   aDMFilterH->SetInputConnection (aSUBFilterH->GetOutputPort() );
88
89   myHiliMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
90   myHiliMapper->SetInputConnection (aDMFilterH->GetOutputPort() );
91
92   // Create non-pickable actor
93   myHiliActor = vtkSmartPointer<vtkActor>::New();
94   myHiliActor->SetPickable(0);
95   myHiliActor->SetVisibility(1);
96   myHiliActor->GetProperty()->SetColor(0, 1, 1);
97   myHiliActor->GetProperty()->SetOpacity(1);
98   myHiliActor->GetProperty()->SetPointSize (myHiliActor->GetProperty()->GetPointSize() + 4 );
99   myHiliActor->GetProperty()->SetLineWidth (myHiliActor->GetProperty()->GetLineWidth() + 2 );
100
101   // Set maper for actor
102   myHiliActor->SetMapper (myHiliMapper);
103   myHiliMapper->ScalarVisibilityOff();
104
105   /* ==============================
106    *  Build pipeline for selection
107    * ============================== */
108
109   IVtkTools_DisplayModeFilter*
110     aDMFilterS = IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Sel) );
111   IVtkTools_SubPolyDataFilter*
112     aSUBFilterS = IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find(Filter_SUB_Sel) );
113
114   // No highligthing exists initially
115   aSUBFilterS->SetInputConnection (aDataSource->GetOutputPort() );
116   aDMFilterS->SetInputConnection (aSUBFilterS->GetOutputPort() );
117
118   mySelMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
119   mySelMapper->SetInputConnection (aDMFilterS->GetOutputPort() );
120
121   // Create non-pickable actor
122   mySelActor = vtkSmartPointer<vtkActor>::New();
123   mySelActor->SetPickable (0);
124   mySelActor->SetVisibility (1);
125   mySelActor->GetProperty()->SetColor (1, 1, 1);
126   mySelActor->GetProperty()->SetOpacity (1);
127   mySelActor->GetProperty()->SetPointSize (myHiliActor->GetProperty()->GetPointSize() + 4 );
128   mySelActor->GetProperty()->SetLineWidth (myHiliActor->GetProperty()->GetLineWidth() + 2 );
129
130   // Set maper for actor
131   mySelActor->SetMapper (mySelMapper);
132   mySelMapper->ScalarVisibilityOff();
133 }
134
135 //===========================================================
136 // Function : AddToRenderer
137 // Purpose  :
138 //===========================================================
139 void IVtkDraw_HighlightAndSelectionPipeline::AddToRenderer (vtkRenderer* theRenderer)
140 {
141   theRenderer->AddActor (myActor);
142   theRenderer->AddActor (myHiliActor);
143   theRenderer->AddActor (mySelActor);
144 }
145
146 //===========================================================
147 // Function : RemoveFromRenderer
148 // Purpose  :
149 //===========================================================
150 void IVtkDraw_HighlightAndSelectionPipeline::RemoveFromRenderer (vtkRenderer* theRenderer)
151 {
152   theRenderer->RemoveActor (myActor);
153   theRenderer->RemoveActor (myHiliActor);
154   theRenderer->RemoveActor (mySelActor);
155 }
156
157 //===========================================================
158 // Function : ClearHighlightFilters
159 // Purpose  :
160 //===========================================================
161 void IVtkDraw_HighlightAndSelectionPipeline::ClearHighlightFilters()
162 {
163   this->GetHighlightFilter()->Clear();
164   this->GetHighlightFilter()->SetDoFiltering (true);
165   this->GetHighlightFilter()->Modified();
166 }
167
168 //===========================================================
169 // Function : ClearSelectionFilters
170 // Purpose  :
171 //===========================================================
172 void IVtkDraw_HighlightAndSelectionPipeline::ClearSelectionFilters()
173 {
174   this->GetSelectionFilter()->Clear();
175   this->GetSelectionFilter()->SetDoFiltering (true);
176   this->GetSelectionFilter()->Modified();
177 }
178
179 //===========================================================
180 // Function : GetDisplayModeFilter
181 // Purpose  :
182 //===========================================================
183 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetDisplayModeFilter()
184 {
185   return IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Shape) );
186 }
187
188 //===========================================================
189 // Function : GetHighlightFilter
190 // Purpose  :
191 //===========================================================
192 IVtkTools_SubPolyDataFilter* IVtkDraw_HighlightAndSelectionPipeline::GetHighlightFilter()
193 {
194   return IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find (Filter_SUB_Hili) );
195 }
196
197 //===========================================================
198 // Function : GetSelectionFilter
199 // Purpose  :
200 //===========================================================
201 IVtkTools_SubPolyDataFilter* IVtkDraw_HighlightAndSelectionPipeline::GetSelectionFilter()
202 {
203   return IVtkTools_SubPolyDataFilter::SafeDownCast (myFilterMap.Find (Filter_SUB_Sel) );
204 }
205
206 //===========================================================
207 // Function : GetHighlightDMFilter
208 // Purpose  :
209 //===========================================================
210 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetHighlightDMFilter()
211 {
212   return IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find (Filter_DM_Hili) );
213 }
214
215 //===========================================================
216 // Function : GetSelectionDMFilter
217 // Purpose  :
218 //===========================================================
219 IVtkTools_DisplayModeFilter* IVtkDraw_HighlightAndSelectionPipeline::GetSelectionDMFilter()
220 {
221   return IVtkTools_DisplayModeFilter::SafeDownCast (myFilterMap.Find(Filter_DM_Sel));
222 }
223
224 //===========================================================
225 // Function : SharedVerticesSelectionOn
226 // Purpose  :
227 //===========================================================
228 void IVtkDraw_HighlightAndSelectionPipeline::SharedVerticesSelectionOn()
229 {
230   this->GetHighlightDMFilter()->SetDisplaySharedVertices (Standard_True);
231   this->GetSelectionDMFilter()->SetDisplaySharedVertices (Standard_True);
232 }
233
234 //===========================================================
235 // Function : SharedVerticesSelectionOff
236 // Purpose  :
237 //===========================================================
238 void IVtkDraw_HighlightAndSelectionPipeline::SharedVerticesSelectionOff()
239 {
240   this->GetHighlightDMFilter()->SetDisplaySharedVertices (Standard_False);
241   this->GetSelectionDMFilter()->SetDisplaySharedVertices (Standard_False);
242 }