Salome HOME
Fix pb with Animation
[modules/visu.git] / src / VISU_I / VISU_Mesh_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_PrsObject_i.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_Mesh_i.hh"
28
29 #include "VISU_Result_i.hh"
30 #include "VISU_ViewManager_i.hh"
31
32 #include "VISU_MeshPL.hxx"
33 #include "VISU_MeshAct.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 static int INCMEMORY = 4;
45
46 int VISU::Mesh_i::myNbPresent = 0;
47
48 QString VISU::Mesh_i::GenerateName() { return VISU::GenerateName("Mesh",myNbPresent++);}
49
50 const string VISU::Mesh_i::myComment = "MESH";
51
52 const char* VISU::Mesh_i::GetComment() const { return myComment.c_str();}
53
54 VISU::Mesh_i::
55 Mesh_i(Result_i* theResult) :
56   PrsObject_i(theResult->GetStudyDocument()),
57   Prs3d_i(theResult,false),
58   myMeshPL(VISU_MeshPL::New())
59 {
60   myPipeLine = myMeshPL;
61 }
62
63
64 VISU::Mesh_i::
65 Mesh_i(Result_i* theResult,
66        SALOMEDS::SObject_ptr theSObject) :
67   PrsObject_i(theResult->GetStudyDocument()),
68   Prs3d_i(theResult,theSObject),
69   myMeshPL(VISU_MeshPL::New())
70 {
71   myPipeLine = myMeshPL;
72 }
73
74
75 void VISU::Mesh_i::RemoveFromStudy()
76 {
77   VISU::DeleteActors(this);
78   VISU::RemoveFromStudy(mySObject);
79 }
80
81
82 void VISU::Mesh_i::SameAs (const Mesh_i* theOrigin)
83 {
84   return; // "SameAs" command for mesh is not valid in current architecture
85 }
86
87
88
89 int VISU::Mesh_i::IsPossible (Result_i* theResult, const char* theMeshName,
90                               Entity theEntity, const char* theFamilyName)
91 {
92   try{
93     MESSAGE("Mesh_i::IsPossible - theMeshName = '"<<theMeshName<<"'; theEntity = "<<theEntity<<"; theFamilyName = '"<<theFamilyName<<"'");
94     float aSize = INCMEMORY*
95       theResult->GetInput()->GetMeshOnEntitySize(theMeshName,(VISU::TEntity)theEntity,theFamilyName);
96     bool aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
97     MESSAGE("Mesh_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
98     return aResult;
99   }catch(std::exception& exc){
100     INFOS("Follow exception was occured :\n"<<exc.what());
101   }catch(...){
102     INFOS("Unknown exception was occured!");
103   }
104   return 0;
105 }
106
107 VISU::Storable* VISU::Mesh_i::Create (const char* theMeshName,
108                                       Entity theEntity,
109                                       const char* theFamilyName)
110 {
111   myMeshName = theMeshName;
112   //jfa IPAL9284:myEntity = VISU::TEntity(theEntity);
113   myEntity = int(theEntity);//jfa IPAL9284
114   mySubMeshName = theFamilyName;
115   return Build(false);
116 }
117
118
119 int VISU::Mesh_i::IsPossible (Result_i* theResult, const char* theMeshName, const char* theGroupName)
120 {
121   try{
122     MESSAGE("Mesh_i::IsPossible - theMeshName = '"<<theMeshName<<"'; theGroupName = '"<<theGroupName<<"'");
123     float aSize = INCMEMORY*
124       theResult->GetInput()->GetMeshOnGroupSize(theMeshName,theGroupName);
125     bool aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
126     INFOS("Mesh_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<aResult);
127     return aResult;
128   }catch(std::exception& exc){
129     INFOS("Follow exception was occured :\n"<<exc.what());
130   }catch(...){
131     INFOS("Unknown exception was occured!");
132   }
133   return 0;
134 }
135
136
137 VISU::Storable* VISU::Mesh_i::Create (const char* theMeshName,
138                                       const char* theGroupName)
139 {
140   myMeshName = theMeshName;
141   //jfa IPAL9284:myEntity = VISU::TEntity(-1);
142   myEntity = -1;//jfa IPAL9284
143   mySubMeshName = theGroupName;
144   return Build(false);
145 }
146
147
148 VISU::Storable* VISU::Mesh_i::Restore(const Storable::TRestoringMap& theMap)
149 {
150   Prs3d_i::Restore(theMap);
151
152   myMeshName = VISU::Storable::FindValue(theMap,"myMeshName").latin1();
153   //jfa IPAL9284:myEntity = (VISU::TEntity)VISU::Storable::FindValue(theMap,"myEntity").toInt();
154   myEntity = VISU::Storable::FindValue(theMap,"myEntity").toInt();//jfa IPAL9284
155   mySubMeshName = VISU::Storable::FindValue(theMap,"mySubMeshName").latin1();
156
157   myPresentType = VISU::PresentationType(VISU::Storable::FindValue(theMap,"myPresentType").toInt());
158
159   myCellColor.R = VISU::Storable::FindValue(theMap,"myCellColor.R").toDouble();
160   myCellColor.G = VISU::Storable::FindValue(theMap,"myCellColor.G").toDouble();
161   myCellColor.B = VISU::Storable::FindValue(theMap,"myCellColor.B").toDouble();
162
163   myNodeColor.R = VISU::Storable::FindValue(theMap,"myNodeColor.R").toDouble();
164   myNodeColor.G = VISU::Storable::FindValue(theMap,"myNodeColor.G").toDouble();
165   myNodeColor.B = VISU::Storable::FindValue(theMap,"myNodeColor.B").toDouble();
166
167   myLinkColor.R = VISU::Storable::FindValue(theMap,"myLinkColor.R").toDouble();
168   myLinkColor.G = VISU::Storable::FindValue(theMap,"myLinkColor.G").toDouble();
169   myLinkColor.B = VISU::Storable::FindValue(theMap,"myLinkColor.B").toDouble();
170
171   return Build(true);
172 }
173
174
175 void VISU::Mesh_i::ToStream(std::ostringstream& theStr){
176   Prs3d_i::ToStream(theStr);
177
178   Storable::DataToStream( theStr, "myMeshName", myMeshName.c_str() );
179   Storable::DataToStream( theStr, "myEntity", myEntity );
180   Storable::DataToStream( theStr, "mySubMeshName", mySubMeshName.c_str() );
181
182   Storable::DataToStream( theStr, "myPresentType", int(myPresentType) );
183
184   Storable::DataToStream( theStr, "myCellColor.R", myCellColor.R );
185   Storable::DataToStream( theStr, "myCellColor.G", myCellColor.G );
186   Storable::DataToStream( theStr, "myCellColor.B", myCellColor.B );
187
188   Storable::DataToStream( theStr, "myNodeColor.R", myNodeColor.R );
189   Storable::DataToStream( theStr, "myNodeColor.G", myNodeColor.G );
190   Storable::DataToStream( theStr, "myNodeColor.B", myNodeColor.B );
191
192   Storable::DataToStream( theStr, "myLinkColor.R", myLinkColor.R );
193   Storable::DataToStream( theStr, "myLinkColor.G", myLinkColor.G );
194   Storable::DataToStream( theStr, "myLinkColor.B", myLinkColor.B );
195 }
196
197
198 VISU::Mesh_i::~Mesh_i(){
199   if(MYDEBUG) MESSAGE("Mesh_i::~Mesh_i()");
200 }
201
202
203 VISU::Storable* VISU::Mesh_i::Build (int theRestoring)
204 {
205   SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
206   aStudyBuilder->NewCommand();  // There is a transaction
207   try {
208     if (myResult->GetInput() == NULL)
209       throw std::runtime_error("Mesh_i::Build - myResult->GetInput() == NULL !!!");
210     if (!theRestoring) {
211       myPresentType = VISU::SHADED;
212       myName = GenerateName().latin1();
213       myCellColor.R = 0.0;  myCellColor.G = myCellColor.B = 1.0;
214       myNodeColor.R = myNodeColor.G = 1.0;  myNodeColor.B = 1.0;
215       myLinkColor.R = myLinkColor.G = myLinkColor.B = 83/255.;
216     }
217     //jfa IPAL9284:if (int(myEntity) >= 0)
218     if (myEntity >= 0) //jfa IPAL9284
219       if (mySubMeshName == "")
220         myType = VISU::TENTITY;
221       else
222         myType = VISU::TFAMILY;
223     else
224       myType = VISU::TGROUP;
225     if(MYDEBUG) MESSAGE("Mesh_i::Build - myType = "<<myType);
226     QString aComment;
227
228     VISU_Convertor::TOutput *anOutput;
229     switch(myType){
230     case VISU::TENTITY :
231       //jfa IPAL9284:anOutput = myResult->GetInput()->GetMeshOnEntity(myMeshName,myEntity);
232       anOutput = myResult->GetInput()->GetMeshOnEntity(myMeshName,(VISU::TEntity)myEntity);//jfa IPAL9284
233       aComment.sprintf("myComment=ENTITY;myType=%d;myMeshName=%s;myId=%d",
234                        VISU::TENTITY,myMeshName.c_str(),myEntity);
235       break;
236     case VISU::TFAMILY :
237       //jfa IPAL9284:anOutput = myResult->GetInput()->GetMeshOnEntity(myMeshName,myEntity,mySubMeshName);
238       anOutput = myResult->GetInput()->GetMeshOnEntity
239         (myMeshName,(VISU::TEntity)myEntity,mySubMeshName);//jfa IPAL9284
240       aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
241                        VISU::TFAMILY,myMeshName.c_str(),myEntity,mySubMeshName.c_str());
242       break;
243     case VISU::TGROUP :
244       anOutput = myResult->GetInput()->GetMeshOnGroup(myMeshName,mySubMeshName);
245       aComment.sprintf("myComment=GROUP;myType=%d;myMeshName=%s;myName=%s",
246                        VISU::TGROUP,myMeshName.c_str(),mySubMeshName.c_str());
247       break;
248     }
249     if(anOutput == NULL) throw std::runtime_error("Mesh_i::Build - anOutput == NULL !!!");
250     myMeshPL->SetInput(anOutput);
251     myMeshPL->Build();
252     if(!theRestoring) { //Setting IOR on the label
253       myMeshPL->Init();
254       string aResultEntry = myResult->GetEntry();
255       string anEntry = myResult->GetEntry(aComment.latin1());
256       if(anEntry == "") throw std::runtime_error("Mesh_i::Build - anEntry was not found !!!");
257       mySObject = myStudy->FindObjectID(anEntry.c_str());
258       SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
259       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(mySObject,"AttributeIOR");
260       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
261       CORBA::String_var aString = GetID();
262       anIOR->SetValue(aString);
263     }
264   }catch(std::exception& exc){
265     INFOS("Follow exception was occured :\n"<<exc.what());
266     return NULL;
267   }catch(...){
268     INFOS("Unknown exception was occured!");
269     return NULL;
270   }
271   aStudyBuilder->CommitCommand();
272   return this;
273 }
274
275
276 VISU_Actor* VISU::Mesh_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO)
277 {
278   VISU_MeshAct* anActor = VISU_MeshAct::New();
279   try{
280     VISU::Prs3d_i::CreateActor(anActor,theIO);
281     UpdateActor(anActor);
282   }catch (...) {
283     anActor->Delete();
284     throw ;
285   }
286   return anActor;
287 }
288
289
290 void VISU::Mesh_i::UpdateActor(VISU_Actor* theActor) {
291   if(VISU_MeshAct* anActor = dynamic_cast<VISU_MeshAct*>(theActor)){
292     if(MYDEBUG) MESSAGE("Mesh_i::UpdateActor");
293     VISU::Prs3d_i::UpdateActor(anActor);
294     anActor->SetRepresentation(myPresentType);
295     anActor->GetSurfaceProperty()->SetColor(myCellColor.R, myCellColor.G, myCellColor.B);
296     anActor->GetEdgeProperty()->SetColor(myLinkColor.R, myLinkColor.G, myLinkColor.B);
297     anActor->GetNodeProperty()->SetColor(myNodeColor.R, myNodeColor.G, myNodeColor.B);
298   }
299 }