Salome HOME
Synchronize adm files
[modules/kernel.git] / src / KernelHelpers / SALOME_StudyEditor.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author: Guillaume Boulant (EDF/R&D) 
20
21 #include "SALOME_StudyEditor.hxx"
22 #include "SALOME_KernelServices.hxx"
23
24 /** Canonic constructor. The object can't be used without a setStudy() */
25 SALOME_StudyEditor::SALOME_StudyEditor() {
26 }
27
28 void SALOME_StudyEditor::setStudy(SALOMEDS::Study_ptr study) {
29   _study = study;
30   _sbuilder = _study->NewBuilder();    
31 }
32
33 void SALOME_StudyEditor::setStudyById(int studyId) {
34   this->setStudy(KERNEL::getStudyManager()->GetStudyByID(studyId));
35 }
36
37 int SALOME_StudyEditor::getStudyId() {
38   if ( _study->_is_nil() ) return UNDEFINED; 
39   return _study->StudyId();
40 }
41
42 SALOME_StudyEditor::SALOME_StudyEditor(int studyId) {
43   this->setStudyById(studyId);
44 }
45 SALOME_StudyEditor::SALOME_StudyEditor(SALOMEDS::Study_ptr study) {
46   this->setStudy(study);
47 }
48
49 SALOMEDS::SObject_ptr SALOME_StudyEditor::newObject(SALOMEDS::SObject_ptr parent) {
50   return _sbuilder->NewObject(parent);
51 }
52
53 SALOMEDS::SObject_ptr SALOME_StudyEditor::findObject(const char * entry) {
54   SALOMEDS::SObject_var sobject = _study->FindObjectID(entry);
55   return sobject._retn();
56 }
57
58 SALOMEDS::SComponent_ptr SALOME_StudyEditor::newRoot(const char * moduleName) {
59   SALOMEDS::SComponent_var sroot = findRoot(moduleName);
60   if ( sroot->_is_nil() ) {
61     sroot = _sbuilder->NewComponent(moduleName);
62     _sbuilder->SetName(sroot,moduleName);
63   }
64   return sroot._retn();
65 }
66
67 bool SALOME_StudyEditor::bindEngine(SALOMEDS::SComponent_var studyRoot,
68                              Engines::EngineComponent_var engine)
69 {
70   // WARN: The engine should be associated to the SComponent IF
71   // AND ONLY IF it is a SALOMEDS::Driver. Otherwise, there is no
72   // need to do that, and it could even lead to exception
73   // raising (eh, you work on SALOME isn't it?)
74   SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(engine);
75   if ( driver->_is_nil() || studyRoot->_is_nil() ) return false;
76   _sbuilder->DefineComponentInstance(studyRoot, engine);
77   return true;
78 }
79
80 SALOMEDS::SComponent_ptr SALOME_StudyEditor::findRoot(const char * moduleName) {
81   return _study->FindComponent(moduleName);
82 }
83
84 void SALOME_StudyEditor::setName(SALOMEDS::SObject_var sobject, const char * value) {
85   _sbuilder->SetName(sobject,value);  
86 }
87 const char * SALOME_StudyEditor::getName(SALOMEDS::SObject_var sobject) {
88   if (sobject->_is_nil()) return NULL;
89   return sobject->GetName();
90 }
91
92 /**
93  * This function specifies which resource file is to be used to
94  * associate an icon to the specified object. Note that even if the
95  * icon as no sens outside the GUI context, it can be defined here
96  * because only the resource name is provided. The effective rendering
97  * is done in the object browser of course, and use this string
98  * attribute. WARN: note that the resource name is supposed to be the
99  * base name of a file managed by the SALOME resource manager (i.e. a
100  * file located in the directory specified in the SalomeApp.xml).
101  */
102 void SALOME_StudyEditor::setIcon(SALOMEDS::SObject_var sobject, const char * resourcename) {
103   SALOMEDS::GenericAttribute_var anAttr;
104   SALOMEDS::AttributePixMap_var aPixmap;
105   anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributePixMap");
106   aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
107   aPixmap->SetPixMap(resourcename);
108 }
109
110 //
111 // MEM: AttributeParameter
112 // Note that the AttributeParameter is a multitype dictionnary whose
113 // keys are the names specified when setting the value
114 //
115
116 /**
117  * Add a parameter attribute of type integer or simply set its value
118  * if it already exists.
119  */
120 void SALOME_StudyEditor::setParameterInt(SALOMEDS::SObject_var sobject, const char * name, int value) { 
121   SALOMEDS::GenericAttribute_var anAttr;
122   SALOMEDS::AttributeParameter_var aParam;
123   anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributeParameter");
124   aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
125   aParam->SetInt(name,value);
126 }
127
128 int SALOME_StudyEditor::getParameterInt(SALOMEDS::SObject_var sobject, const char * name) {
129   if (sobject->_is_nil()) return UNDEFINED;
130
131   SALOMEDS::GenericAttribute_var anAttr;
132   SALOMEDS::AttributeParameter_var aParam;
133   if ( sobject->FindAttribute(anAttr,"AttributeParameter") ) {
134     aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
135     return aParam->GetInt(name);
136   }
137   return UNDEFINED;
138 }
139
140 /**
141  * Add a parameter attribute of type boolean or simply set its value
142  * if it already exists.
143  */
144 void SALOME_StudyEditor::setParameterBool(SALOMEDS::SObject_var sobject, const char * name, bool value) {
145   SALOMEDS::GenericAttribute_var anAttr;
146   SALOMEDS::AttributeParameter_var aParam;
147   anAttr = _sbuilder->FindOrCreateAttribute(sobject, "AttributeParameter");
148   aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
149   aParam->SetBool(name,value);
150 }
151
152 bool SALOME_StudyEditor::getParameterBool(SALOMEDS::SObject_var sobject, const char * name) {
153   if (sobject->_is_nil()) return false;
154   // _GBO_ Q: Maybe it's better in this case to raise an exception?
155
156   SALOMEDS::GenericAttribute_var anAttr;
157   SALOMEDS::AttributeParameter_var aParam;
158   if ( sobject->FindAttribute(anAttr,"AttributeParameter") ) {
159     aParam = SALOMEDS::AttributeParameter::_narrow(anAttr);
160     return aParam->GetBool(name);
161   }
162   return false;
163
164 }