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