Salome HOME
Update Help for VISU module.
[modules/visu.git] / src / VISU_I / VISU_Prs3d_i.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //  File   : VISU_Prs3d_i.cc
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_PipeLine.hxx"
28 #include "VISU_Result_i.hh"
29 #include "VISU_Prs3d_i.hh"
30 #include "VISU_Actor.h"
31
32 #include <vtkGeometryFilter.h>
33 #include <vtkDataSetMapper.h>
34
35 using namespace VISU;
36 using namespace std;
37
38 #ifdef _DEBUG_
39 static int MYDEBUG = 0;
40 #else
41 static int MYDEBUG = 0;
42 #endif
43
44 VISU::Prs3d_i::
45 Prs3d_i(Result_i* theResult,
46         SALOMEDS::SObject_ptr theSObject) :
47   PrsObject_i(theResult->GetStudyDocument()),
48   myResult(theResult),
49   mySObject(SALOMEDS::SObject::_duplicate(theSObject)),
50   myAddToStudy(true),
51   myPipeLine(NULL)
52 {
53   myOffset[0] = myOffset[1] = myOffset[2] = 0;
54   myResult->Register();
55 }
56
57 VISU::Prs3d_i::
58 Prs3d_i(Result_i* theResult,
59         bool theAddToStudy) :
60   PrsObject_i(theResult->GetStudyDocument()),
61   myResult(theResult),
62   mySObject(SALOMEDS::SObject::_nil()),
63   myAddToStudy(theAddToStudy),
64   myPipeLine(NULL)
65 {
66   myOffset[0] = myOffset[1] = myOffset[2] = 0;
67   myResult->Register();
68 }
69
70 void VISU::Prs3d_i::SameAs(const Prs3d_i* theOrigin)
71 {
72   if (Prs3d_i* aOrigin = const_cast<Prs3d_i*>(theOrigin)) {
73     myPipeLine->SameAs(aOrigin->GetPL());
74     aOrigin->GetOffset(myOffset);
75   }
76 }
77
78 VISU::Prs3d_i::~Prs3d_i() {
79   if(MYDEBUG) MESSAGE("~Prs3d_i() - this = "<<this
80                       <<"; GetReferenceCount() = "<<myPipeLine->GetReferenceCount());
81   myPipeLine->Delete();
82   myResult->Destroy();
83 }
84
85 void VISU::Prs3d_i::CreateActor(VISU_Actor* theActor, const Handle(SALOME_InteractiveObject)& theIO)
86 {
87   try{
88     Update();
89     //    if(myAddToStudy){
90     Handle(SALOME_InteractiveObject) anIO = theIO;
91     if(anIO.IsNull() && (!mySObject->_is_nil())){
92       anIO = new SALOME_InteractiveObject(mySObject->GetID(),"VISU",GetName());
93       theActor->setIO(anIO);
94     }
95       // }
96     theActor->SetPipeLine(GetPipeLine());
97     theActor->SetPrs3d(this);
98     theActor->SetPosition(myOffset);
99   }catch(std::bad_alloc& ex){
100     throw std::runtime_error("CreateActor >> No enough memory");
101     throw ex;
102   } catch(std::exception& ex){
103     throw ex;
104   }catch(...) {
105     throw std::runtime_error("CreateActor >> unexpected exception was caught!!!");
106   }
107 }
108
109 void VISU::Prs3d_i::UpdateActor(VISU_Actor* theActor) {
110   if(MYDEBUG) MESSAGE("Prs3d_i::UpdateActor() - this = "<<this);
111
112   // fix for bug 9808 BEGIN
113   vtkMapper *aMapper = myPipeLine->GetMapper();
114   vtkDataSet *aDataSet = aMapper->GetInput();
115   if (!aDataSet)
116     throw std::runtime_error("There is no input data !!!");
117   aDataSet->Update();
118   static float eps = VTK_LARGE_FLOAT * 0.1 ;
119   if (!aDataSet->GetNumberOfCells())
120     throw std::runtime_error("There is no visible elements");
121   if (aDataSet->GetLength() > eps)
122     throw std::runtime_error("Diagonal of the actor is too large !!!");
123   // fix for bug 9808 END
124
125   //theActor->GetMapper()->ShallowCopy(myPipeLine->GetMapper());
126   theActor->GetMapper()->ShallowCopy(aMapper);
127   theActor->SetPosition(myOffset);
128   theActor->Modified();
129 }
130
131 VISU::Storable* VISU::Prs3d_i::Restore(const Storable::TRestoringMap& theMap)
132 {
133   myName = VISU::Storable::FindValue(theMap,"myName").latin1();
134   myOffset[0] = VISU::Storable::FindValue(theMap,"myOffset[0]").toFloat();
135   myOffset[1] = VISU::Storable::FindValue(theMap,"myOffset[1]").toFloat();
136   myOffset[2] = VISU::Storable::FindValue(theMap,"myOffset[2]").toFloat();
137   return this;
138 }
139
140 void VISU::Prs3d_i::ToStream(std::ostringstream& theStr){
141   Storable::DataToStream( theStr, "myName",   myName.c_str() );
142   Storable::DataToStream( theStr, "myOffset[0]", myOffset[0] );
143   Storable::DataToStream( theStr, "myOffset[1]", myOffset[1] );
144   Storable::DataToStream( theStr, "myOffset[2]", myOffset[2] );
145 }
146
147 void VISU::Prs3d_i::Update() {
148   if(MYDEBUG) MESSAGE("Prs3d_i::Update() - this = "<<this);
149   try{
150     myPipeLine->Update();
151   }catch(...){
152     throw std::runtime_error("Prs3d_i::Update >> unexpected exception was caught!!!");
153   }
154 }
155
156 VISU_PipeLine* VISU::Prs3d_i::GetPipeLine(){
157   return GetPL();
158 }
159
160 VISU_PipeLine* VISU::Prs3d_i::GetPL(){
161   return myPipeLine;
162 }
163
164 vtkUnstructuredGrid* VISU::Prs3d_i::GetInput(){
165   return myPipeLine->GetInput();
166 }
167
168
169 SALOMEDS::SObject_var VISU::Prs3d_i::GetSObject(){
170   if (CORBA::is_nil(mySObject.in())) {
171     const SALOMEDS::Study_var& aStudy = myResult->GetStudyDocument();
172     CORBA::String_var anIOR = GetID();
173     mySObject = aStudy->FindObjectIOR(anIOR);
174   }
175   return mySObject;
176 }
177
178 void VISU::Prs3d_i::GetBounds(float aBounds[6]){
179   myPipeLine->GetMapper()->GetBounds(aBounds);
180 }
181
182 // Clipping planes
183
184 void VISU::Prs3d_i::RemoveAllClippingPlanes(){
185   myPipeLine->RemoveAllClippingPlanes();
186 }
187
188 vtkIdType VISU::Prs3d_i::GetNumberOfClippingPlanes() const{
189   return myPipeLine->GetNumberOfClippingPlanes();
190 }
191
192 bool VISU::Prs3d_i::AddClippingPlane(vtkPlane* thePlane){
193   return myPipeLine->AddClippingPlane(thePlane);
194 }
195
196 vtkPlane* VISU::Prs3d_i::GetClippingPlane(vtkIdType theID) const{
197   return myPipeLine->GetClippingPlane(theID);
198 }
199
200 void VISU::Prs3d_i::SetPlaneParam (float theDir[3], float theDist, vtkPlane* thePlane) {
201   myPipeLine->SetPlaneParam(theDir, theDist, thePlane);
202 }
203
204 VISU::Result_i* VISU::GetResult(SALOMEDS::SObject_ptr theSObject){
205   VISU::Result_var aResult = FindResult(theSObject);
206   if(!aResult->_is_nil())
207     return dynamic_cast<VISU::Result_i*>(VISU::GetServant(aResult.in()).in());
208   return NULL;
209 }
210
211 void VISU::Prs3d_i::SetOffset(const float* theOffsets)
212 {
213   myOffset[0] = theOffsets[0];
214   myOffset[1] = theOffsets[1];
215   myOffset[2] = theOffsets[2];
216 }
217
218 void VISU::Prs3d_i::SetOffset(float theDx, float theDy, float theDz)
219 {
220   myOffset[0] = theDx;
221   myOffset[1] = theDy;
222   myOffset[2] = theDz;
223 }
224
225 void VISU::Prs3d_i::GetOffset(float* theOffsets)
226 {
227   theOffsets[0] = myOffset[0];
228   theOffsets[1] = myOffset[1];
229   theOffsets[2] = myOffset[2];
230 }
231 void VISU::Prs3d_i::GetOffset(float& theDx, float& theDy, float& theDz)
232 {
233   theDx = myOffset[0];
234   theDy = myOffset[1];
235   theDz = myOffset[2];
236 }