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