0024904: Visualization - Integration of VIS component:
[occt.git] / src / IVtkTools / IVtkTools_ShapeObject.cxx
1 // Created on: 2011-10-27
2 // Created by: Roman KOZLOV
3 // Copyright (c) 2011-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 <IVtkTools_ShapeObject.hxx>
17 #include <IVtkTools_ShapeDataSource.hxx>
18 #include <vtkActor.h>
19 #include <vtkObjectBase.h>
20 #include <vtkObjectFactory.h> 
21 #include <vtkDataSet.h>
22 #include <vtkInformation.h>
23 #include <vtkInformationObjectBaseKey.h>
24 #include <vtkDebugLeaks.h>
25 #include <vtkPolyData.h>
26
27 IVtkTools_ShapeObject::KeyPtr IVtkTools_ShapeObject::myKey = 0;
28
29 //============================================================================
30 //  Method: getKey
31 // Purpose: Static method to get vtkInformationKey for retrieving OccShape 
32 //          instance from the actor.
33 //============================================================================
34 IVtkTools_ShapeObject::KeyPtr IVtkTools_ShapeObject::getKey()
35 {
36   if (!myKey)
37   {
38     myKey = new vtkInformationObjectBaseKey( "OccShapePtr", "IVtkTools_ShapeObject::Key" );
39   }
40
41   return myKey;
42 }
43
44 //============================================================================
45 //  Method: GetOccShape
46 // Purpose: Static method to get OCC shape from VTK actor's data from 
47 //          information object by key.
48 //============================================================================
49 IVtkOCC_Shape::Handle IVtkTools_ShapeObject::GetOccShape (vtkActor* theActor)
50 {
51   IVtkOCC_Shape::Handle anOccShape;
52   IVtkTools_ShapeDataSource* aSrc = IVtkTools_ShapeObject::GetShapeSource (theActor);
53   if (aSrc)
54   {
55     anOccShape = aSrc->GetShape();
56   }
57   return anOccShape;
58 }
59
60 //============================================================================
61 //  Method: GetShapeSource
62 // Purpose: Static method to get OCC shape source from VTK actor's data from 
63 //          information object by key.
64 //============================================================================
65 IVtkTools_ShapeDataSource* IVtkTools_ShapeObject::GetShapeSource (vtkActor* theActor)
66 {
67   IVtkTools_ShapeDataSource* anOccShapeSource = 0;
68   vtkInformation* anInfo = theActor->GetPropertyKeys();
69   if (anInfo)
70   {
71     KeyPtr aKey = getKey();
72     if (aKey->Has(anInfo))
73     {
74       IVtkTools_ShapeObject* aShapeObj = (IVtkTools_ShapeObject*)(aKey->Get (anInfo));
75       anOccShapeSource = aShapeObj->GetShapeSource();
76     }
77   }
78   return anOccShapeSource;
79 }
80
81 //============================================================================
82 //  Method: SetShapeSource
83 // Purpose: Static method to set OCC shape source to VTK dataset in information 
84 //          object with key.
85 //============================================================================
86 void IVtkTools_ShapeObject::SetShapeSource (IVtkTools_ShapeDataSource* theDataSource,
87                                             vtkDataSet*                theDataSet)
88 {
89   if (!theDataSet->GetInformation() )
90   {
91     theDataSet->SetInformation (vtkInformation::New());
92   }
93   vtkInformation* aDatasetInfo = theDataSet->GetInformation();
94   KeyPtr aKey = getKey();
95   IVtkTools_ShapeObject* aShapeObj = IVtkTools_ShapeObject::New();
96   aShapeObj->SetShapeSource (theDataSource);
97   aKey->Set(aDatasetInfo, aShapeObj);
98   aShapeObj->Delete();
99 }
100
101 //============================================================================
102 //  Method: SetShapeSource
103 // Purpose: Static method to set OCC shape source to VTK actor in information 
104 //          object with key.
105 //============================================================================
106 void IVtkTools_ShapeObject::SetShapeSource (IVtkTools_ShapeDataSource* theDataSource,
107                                             vtkActor*                  theActor)
108 {
109   if ( !theActor->GetPropertyKeys() )
110   {
111     theActor->SetPropertyKeys (vtkInformation::New());
112   }
113
114   vtkInformation* anInfo = theActor->GetPropertyKeys();
115   KeyPtr aKey = getKey();
116   IVtkTools_ShapeObject* aShapeObj = IVtkTools_ShapeObject::New();
117   aShapeObj->SetShapeSource (theDataSource);
118   aKey->Set (anInfo, aShapeObj);
119   aShapeObj->Delete();
120 }
121
122 //! @class IVtkTools_ShapeObject 
123 //! @brief VTK holder class for OCC shapes to pass them through pipelines.
124 vtkStandardNewMacro(IVtkTools_ShapeObject);
125
126 //============================================================================
127 //  Method: Constructor
128 // Purpose: Protected constructor.
129 //============================================================================
130 IVtkTools_ShapeObject::IVtkTools_ShapeObject()
131 { }
132
133 //============================================================================
134 //  Method: Destructor
135 // Purpose: Protected destructor.
136 //============================================================================
137 IVtkTools_ShapeObject::~IVtkTools_ShapeObject()
138 { }
139
140 //============================================================================
141 //  Method: SetShapeSource
142 // Purpose: 
143 //============================================================================
144 void IVtkTools_ShapeObject::SetShapeSource (IVtkTools_ShapeDataSource* theDataSource)
145 {
146   myShapeSource = theDataSource;
147 }
148
149 //============================================================================
150 //  Method: GetShapeSource
151 // Purpose: 
152 //============================================================================
153 IVtkTools_ShapeDataSource* IVtkTools_ShapeObject::GetShapeSource () const
154 {
155   return myShapeSource;
156 }
157