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