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