Salome HOME
Merge from OCC_development_generic_2006
[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       myName = GenerateName().latin1();
232       myCellColor.R = 0.0;  myCellColor.G = myCellColor.B = 1.0;
233       myNodeColor.R = myNodeColor.G = 1.0;  myNodeColor.B = 1.0;
234       myLinkColor.R = myLinkColor.G = myLinkColor.B = 83/255.;
235     }
236
237     if(myEntity >= 0)
238       if(mySubMeshName == "")
239         myType = VISU::TENTITY;
240       else
241         myType = VISU::TFAMILY;
242     else
243       myType = VISU::TGROUP;
244
245     if(MYDEBUG) MESSAGE("Mesh_i::Build - myType = "<<myType);
246     QString aComment;
247
248     VISU::PIDMapper anIDMapper;
249     switch(myType){
250     case VISU::TENTITY :
251       anIDMapper = myResult->GetInput()->GetMeshOnEntity(myMeshName,(VISU::TEntity)myEntity);//jfa IPAL9284
252       aComment.sprintf("myComment=ENTITY;myType=%d;myMeshName=%s;myId=%d",
253                        VISU::TENTITY,myMeshName.c_str(),myEntity);
254       break;
255     case VISU::TFAMILY :
256       anIDMapper = myResult->GetInput()->GetFamilyOnEntity(myMeshName,(VISU::TEntity)myEntity,mySubMeshName);
257       aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
258                        VISU::TFAMILY,myMeshName.c_str(),myEntity,mySubMeshName.c_str());
259       break;
260     case VISU::TGROUP :
261       anIDMapper = myResult->GetInput()->GetMeshOnGroup(myMeshName,mySubMeshName);
262       aComment.sprintf("myComment=GROUP;myType=%d;myMeshName=%s;myName=%s",
263                        VISU::TGROUP,myMeshName.c_str(),mySubMeshName.c_str());
264       break;
265     }
266     if(!anIDMapper) 
267       throw std::runtime_error("Mesh_i::Build - !anIDMapper !!!");
268
269     myMeshPL->SetIDMapper(anIDMapper);
270     myMeshPL->Build();
271
272     if(!theRestoring) { //Setting IOR on the label
273       myMeshPL->Init();
274       string aResultEntry = myResult->GetEntry();
275       string anEntry = myResult->GetEntry(aComment.latin1());
276       if(anEntry == "") throw std::runtime_error("Mesh_i::Build - anEntry was not found !!!");
277       mySObject = myStudy->FindObjectID(anEntry.c_str());
278       SALOMEDS::StudyBuilder_var aStudyBuilder = myStudy->NewBuilder();
279       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(mySObject,"AttributeIOR");
280       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
281       CORBA::String_var aString = GetID();
282       anIOR->SetValue(aString);
283     }
284   }catch(std::exception& exc){
285     INFOS("Follow exception was occured :\n"<<exc.what());
286     return NULL;
287   }catch(...){
288     INFOS("Unknown exception was occured!");
289     return NULL;
290   }
291   aStudyBuilder->CommitCommand();
292   return this;
293 }
294
295
296 VISU_Actor* VISU::Mesh_i::CreateActor(const Handle(SALOME_InteractiveObject)& theIO)
297 {
298   VISU_MeshAct* anActor = VISU_MeshAct::New();
299   try{
300     VISU::Prs3d_i::CreateActor(anActor,theIO);
301     UpdateActor(anActor);
302   }catch (...) {
303     anActor->Delete();
304     throw ;
305   }
306   return anActor;
307 }
308
309
310 void VISU::Mesh_i::UpdateActor(VISU_Actor* theActor) {
311   if(VISU_MeshAct* anActor = dynamic_cast<VISU_MeshAct*>(theActor)){
312     if(MYDEBUG) MESSAGE("Mesh_i::UpdateActor");
313     VISU::Prs3d_i::UpdateActor(anActor);
314     anActor->SetRepresentation(myPresentType);
315     anActor->GetSurfaceProperty()->SetColor(myCellColor.R, myCellColor.G, myCellColor.B);
316     anActor->GetEdgeProperty()->SetColor(myLinkColor.R, myLinkColor.G, myLinkColor.B);
317     anActor->GetNodeProperty()->SetColor(myNodeColor.R, myNodeColor.G, myNodeColor.B);
318   }
319 }