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