Salome HOME
[Bug PAL7444] display mesh takes a lot of more memory in 2.1.0 than in 2.0.0.
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Utils.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
19
20
21 #include <qstring.h>
22
23 #include "SMESHGUI_Utils.h"
24
25 #include "QAD_Desktop.h"
26
27 #include "SALOMEconfig.h"
28 #include CORBA_CLIENT_HEADER(GEOM_Gen)
29 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
30
31 #include "utilities.h"
32
33 using namespace std;
34
35 namespace SMESH{
36
37   QAD_Study* GetActiveStudy(){
38     if(QAD_Desktop* aDesktop = QAD_Application::getDesktop())
39       return aDesktop->getActiveStudy();
40     return NULL;
41   }
42
43
44   QAD_StudyFrame* GetActiveStudyFrame(){
45     if(QAD_Study* aStudy = GetActiveStudy())
46       return aStudy->getActiveStudyFrame();
47     return NULL;
48   }
49
50
51   SALOMEDS::Study_var GetActiveStudyDocument(){
52     if(QAD_Study* aStudy = GetActiveStudy())
53       return aStudy->getStudyDocument();
54     return SALOMEDS::Study::_nil();
55   }
56
57
58   SALOMEDS::SObject_var FindSObject(CORBA::Object_ptr theObject){
59     if(!CORBA::is_nil(theObject)){
60       SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
61       CORBA::String_var anIOR = aStudy->ConvertObjectToIOR(theObject);
62       if(strcmp(anIOR.in(),"") != 0)
63         return aStudy->FindObjectIOR(anIOR);
64     }
65     return SALOMEDS::SObject::_nil();
66   }
67
68
69   void SetName(SALOMEDS::SObject_ptr theSObject, const char* theName){
70     using namespace SALOMEDS;
71     Study_var aStudy = GetActiveStudyDocument();
72     if(aStudy->GetProperties()->IsLocked())
73       return;
74     StudyBuilder_var aBuilder = aStudy->NewBuilder();
75     GenericAttribute_var anAttr = aBuilder->FindOrCreateAttribute(theSObject,"AttributeName");
76     AttributeName_var aName = AttributeName::_narrow(anAttr);
77     if(!aName->_is_nil())
78       aName->SetValue(theName);
79   }
80
81   void SetValue(SALOMEDS::SObject_ptr theSObject, const char* theValue){
82     using namespace SALOMEDS;
83     Study_var aStudy = GetActiveStudyDocument();
84     if(aStudy->GetProperties()->IsLocked())
85       return;
86     StudyBuilder_var aBuilder = aStudy->NewBuilder();
87     GenericAttribute_var anAttr = aBuilder->FindOrCreateAttribute(theSObject,"AttributeComment");
88     AttributeComment_var aComment = AttributeComment::_narrow(anAttr);
89     if(!aComment->_is_nil())
90       aComment->SetValue(theValue);
91   }
92   
93
94   CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject,
95                                     SALOMEDS::Study_ptr theStudy)
96   {
97     if(!theSObject->_is_nil()){
98       SALOMEDS::GenericAttribute_var anAttr;
99       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
100         SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
101         CORBA::String_var aVal = anIOR->Value();
102         return theStudy->ConvertIORToObject(aVal.in());
103       }
104     }
105     return CORBA::Object::_nil();
106   }
107
108
109   CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
110     SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
111     return SObjectToObject(theSObject,aStudy);
112   }
113
114
115   CORBA::Object_var IObjectToObject(const Handle(SALOME_InteractiveObject)& theIO){
116     if(!theIO.IsNull()){
117       if(theIO->hasEntry()){
118         SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
119         SALOMEDS::SObject_var anObj = aStudy->FindObjectID(theIO->getEntry());
120         return SObjectToObject(anObj,aStudy);
121       }
122     }
123     return CORBA::Object::_nil();
124   }
125
126
127   CORBA::Object_var IORToObject(const char* theIOR){
128     SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
129     return aStudy->ConvertIORToObject(theIOR);
130   }
131
132
133   int GetNameOfSelectedIObjects(SALOME_Selection* theSel, QString& theName)
134   {
135     int aNbSel = theSel->IObjectCount();
136     if (aNbSel == 1) {
137       Handle(SALOME_InteractiveObject) anIObject = theSel->firstIObject();
138       theName = anIObject->getName();
139     }else{
140       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
141     }
142     return aNbSel;
143   }
144
145
146   SALOMEDS::SObject_var GetMeshOrSubmesh(SALOMEDS::SObject_ptr theSObject){
147     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
148     if(!aShape->_is_nil()){ //It s a shape
149       return theSObject->GetFather();
150     }
151     SALOMEDS::SObject_var aSObject;
152     if(theSObject->ReferencedObject(aSObject)){
153       aSObject = theSObject->GetFather();
154       return aSObject->GetFather();
155     }
156     return theSObject->GetFather();
157   }
158
159
160   void ModifiedMesh(SALOMEDS::SObject_ptr theSObject, bool theIsRight)
161   {
162     SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
163     if(aStudy->GetProperties()->IsLocked())
164       return ;
165
166     SALOMEDS::StudyBuilder_var aBuilder = aStudy->NewBuilder();
167     SALOMEDS::GenericAttribute_var anAttr = 
168       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
169     SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
170     if(theIsRight){
171       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
172     }else{
173       aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
174     }
175     
176     SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theSObject);
177     for (int i = 1; anIter->More(); anIter->Next(), i++) {
178       SALOMEDS::SObject_var aSObj = anIter->Value();
179       if(i >= 4){
180         SALOMEDS::ChildIterator_var anIter1 = aStudy->NewChildIterator(aSObj);
181         for(; anIter1->More(); anIter1->Next()){
182           SALOMEDS::SObject_var aSObj1 = anIter1->Value();
183           anAttr = aBuilder->FindOrCreateAttribute(aSObj1,"AttributePixMap");
184           aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
185           if(theIsRight){
186             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH");
187           }else{
188             aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
189           }
190         }
191       }
192     }
193   }
194
195
196 }