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