]> SALOME platform Git repositories - tools/sat.git/blob - data/templates/CppComponent/src/CPPCMP/CPPCMP.cxx
Salome HOME
add the template command
[tools/sat.git] / data / templates / CppComponent / src / CPPCMP / CPPCMP.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include ":sat:{CPPCMP}.hxx"
24 #include ":sat:{CPPCMP}_version.h"
25
26 #include <SALOMEconfig.h>
27 #include CORBA_CLIENT_HEADER(SALOMEDS)
28 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
29
30 #include <Utils_ExceptHandlers.hxx>
31
32 #include <string>
33
34 namespace {
35     static std::string studyName(const std::string& name)
36     {
37         std::string fullName = "/:sat:{CPPCMP}/";
38         fullName += name;
39         return fullName;
40     }
41 }
42
43 /*!
44   \brief Constructor
45
46   Creates an instance of the :sat:{CPPCMP} component engine
47
48   \param orb reference to the ORB
49   \param poa reference to the POA
50   \param contId CORBA object ID, pointing to the owner SALOME container
51   \param instanceName SALOME component instance name
52   \param interfaceName SALOME component interface name
53 */
54 :sat:{CPPCMP}:::sat:{CPPCMP}(CORBA::ORB_ptr orb,
55           PortableServer::POA_ptr poa,
56           PortableServer::ObjectId* contId,
57           const char* instanceName,
58           const char* interfaceName)
59     : Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
60 {
61     _thisObj = this;
62     _id = _poa->activate_object(_thisObj); // register and activate this servant object
63 }
64
65 /*!
66   \brief Destructor
67
68   Clean up allocated resources
69 */
70 :sat:{CPPCMP}::~:sat:{CPPCMP}()
71 {
72   // nothing to do
73 }
74
75 /*!
76   \brief Say hello to \a name
77   \param study SALOME study
78   \param name person's name
79   \return operation status
80 */
81 :sat:{CPPCMP}_ORB::status :sat:{CPPCMP}::hello(SALOMEDS::Study_ptr study, const char* name)
82 {
83     // set exception handler to catch unexpected CORBA exceptions
84     Unexpect aCatch(SALOME_SalomeException);
85
86     // set result status to error initially
87     :sat:{CPPCMP}_ORB::status result = :sat:{CPPCMP}_ORB::OP_ERR_UNKNOWN;
88
89     // check if reference to study is valid
90     if (!CORBA::is_nil(study))
91     {
92         // get full object path
93         std::string fullName = studyName(name);
94         // check if the object with the same name is already registered in the study
95         SALOMEDS::SObject_var sobj = study->FindObjectByPath(fullName.c_str());
96         if (!CORBA::is_nil(sobj))
97         {
98             // person is already registered in the study -> ERROR
99             result = :sat:{CPPCMP}_ORB::OP_ERR_ALREADY_MET;
100         }
101         else
102         {
103             // person is not registered yet -> register
104             SALOMEDS::StudyBuilder_var     studyBuilder   = study->NewBuilder();          // study builder
105             SALOMEDS::UseCaseBuilder_var   useCaseBuilder = study->GetUseCaseBuilder();   // use case builder
106
107             // find :sat:{CPPCMP} component; create it if not found
108             SALOMEDS::SComponent_var father = study->FindComponent(":sat:{CPPCMP}");
109             if (CORBA::is_nil(father))
110             {
111                 // create component
112                 father = studyBuilder->NewComponent(":sat:{CPPCMP}");
113                 // set name attribute
114                 father->SetAttrString("AttributeName", ":sat:{CPPCMP}");
115                 // set icon attribute
116                 father->SetAttrString("AttributePixMap", "ICON_HELLO");
117                 // register component in the study
118                 studyBuilder->DefineComponentInstance(father, :sat:{CPPCMP}_Gen::_this());
119                 // add component to the use case tree
120                 // (to support tree representation customization and drag-n-drop)
121                 useCaseBuilder->SetRootCurrent();
122                 useCaseBuilder->Append(father); // component object is added as the top level item
123             }
124
125             // create new sub-object, as a child of the component object
126             sobj = studyBuilder->NewObject(father);
127             sobj->SetAttrString("AttributeName", name);
128             // add object to the use case tree
129             // (to support tree representation customization and drag-n-drop)
130             useCaseBuilder->AppendTo(father, sobj);
131
132             // cleanup
133             father->UnRegister();
134             sobj->UnRegister();
135
136             // set operation status
137             result = :sat:{CPPCMP}_ORB::OP_OK;
138         }
139     }
140
141     // return result of the operation
142     return result;
143 }
144
145 /*!
146   \brief Say goodbye to \a name
147   \param study SALOME study
148   \param name person's name
149   \return operation status
150 */
151 :sat:{CPPCMP}_ORB::status :sat:{CPPCMP}::goodbye(SALOMEDS::Study_ptr study, const char* name)
152 {
153     // set exception handler to catch unexpected CORBA exceptions
154     Unexpect aCatch(SALOME_SalomeException);
155
156     // set result status to error initially
157     :sat:{CPPCMP}_ORB::status result = :sat:{CPPCMP}_ORB::OP_ERR_UNKNOWN;
158
159     // check if reference to study is valid
160     if (!CORBA::is_nil(study))
161     {
162         // get full object path
163         std::string fullName = studyName(name);
164
165         // initially set error status: person is not registered
166         result = :sat:{CPPCMP}_ORB::OP_ERR_DID_NOT_MEET;
167
168         // check if the object with the same name is registered in the study
169         // find all objects with same name
170         SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();            // study builder
171         SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder(); // use case builder
172         SALOMEDS::SObject_var sobj = study->FindObjectByPath(fullName.c_str());
173         while (!CORBA::is_nil(sobj))
174         {
175             std::list<SALOMEDS::SObject_var> toRemove;
176             toRemove.push_back(sobj);
177             SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator(sobj); // use case iterator
178             for (useCaseIt->Init(true); useCaseIt->More(); useCaseIt->Next())
179                 toRemove.push_back(useCaseIt->Value());
180
181             // perform removing of all found objects (recursively with children)
182             std::list<SALOMEDS::SObject_var>::const_iterator it;
183             for(it = toRemove.begin(); it != toRemove.end(); ++it)
184             {
185                 sobj = *it;
186                 // remove object from the study
187                 // - normally it's enough to call RemoveObject() method
188                 // RemoveObject`WithChildren() also removes all children recursively if there are any
189                 // - it's not necessary to remove it from use case builder, it is done automatically
190                 studyBuilder->RemoveObjectWithChildren(sobj);
191
192                 // cleanup
193                 sobj->UnRegister();
194
195                 // set operation status to OK as at least one object is removed
196                 result = :sat:{CPPCMP}_ORB::OP_OK;
197             }
198
199             sobj = study->FindObjectByPath(fullName.c_str());
200         }
201     }
202
203     // return result of the operation
204     return result;
205 }
206
207 /*!
208   \brief Copy or move objects to the specified position
209
210   This function is used in the drag-n-drop functionality.
211
212   \param what objects being copied/moved
213   \param where parent object where objects are copied/moved to
214   \param row position in the parent object's children list at which objects are copied/moved
215   \param isCopy \c true if object are copied or \c false otherwise
216 */
217 void :sat:{CPPCMP}::copyOrMove(const :sat:{CPPCMP}_ORB::object_list& what,
218             SALOMEDS::SObject_ptr where,
219             CORBA::Long row, CORBA::Boolean isCopy)
220 {
221     if (CORBA::is_nil(where)) return; // bad parent
222
223     SALOMEDS::Study_var study = where->GetStudy();                               // study
224     SALOMEDS::StudyBuilder_var studyBuilder = study->NewBuilder();               // study builder
225     SALOMEDS::UseCaseBuilder_var useCaseBuilder = study->GetUseCaseBuilder();    // use case builder
226     SALOMEDS::SComponent_var father = where->GetFatherComponent();               // father component
227     std::string dataType = father->ComponentDataType();
228     if (dataType != ":sat:{CPPCMP}") return; // not a :sat:{CPPCMP} component
229
230     SALOMEDS::SObject_var objAfter;
231     if (row >= 0 && useCaseBuilder->HasChildren(where))
232     {
233         // insert at given row -> find insertion position
234         SALOMEDS::UseCaseIterator_var useCaseIt = useCaseBuilder->GetUseCaseIterator(where);
235         int i;
236         for (i = 0; i < row && useCaseIt->More(); i++, useCaseIt->Next());
237         {
238             if (i == row && useCaseIt->More())
239                 objAfter = useCaseIt->Value();
240         }
241     }
242
243     for (int i = 0; i < what.length(); i++)
244     {
245         SALOMEDS::SObject_var sobj = what[i];
246         if (CORBA::is_nil(sobj)) continue; // skip bad object
247
248         if (isCopy)
249         {
250             // copying is performed
251             // get name of the object
252             CORBA::String_var name = sobj->GetName();
253             // create new object, as a child of the component object
254             SALOMEDS::SObject_var new_sobj = studyBuilder->NewObject(father);
255             new_sobj->SetAttrString("AttributeName", name.in());
256             sobj = new_sobj;
257         }
258
259         // insert the object or its copy to the use case tree
260         if (!CORBA::is_nil(objAfter))
261             useCaseBuilder->InsertBefore(sobj, objAfter); // insert at given row
262         else
263             useCaseBuilder->AppendTo(where, sobj);        // append to the end of list
264     }
265 }
266
267 // Version information
268 char* :sat:{CPPCMP}::getVersion()
269 {
270 #if :sat:{CPPCMP}_DEVELOPMENT
271     return CORBA::string_dup(:sat:{CPPCMP}_VERSION_STR"dev");
272 #else
273     return CORBA::string_dup(:sat:{CPPCMP}_VERSION_STR);
274 #endif
275 }
276
277 extern "C"
278 {
279     /*!
280       \brief Exportable factory function: create an instance of the :sat:{CPPCMP} component engine
281       \param orb reference to the ORB
282       \param poa reference to the POA
283       \param contId CORBA object ID, pointing to the owner SALOME container
284       \param instanceName SALOME component instance name
285       \param interfaceName SALOME component interface name
286       \return CORBA object identifier of the registered servant
287     */
288     PortableServer::ObjectId* :sat:{CPPCMP}Engine_factory(CORBA::ORB_ptr orb,
289                          PortableServer::POA_ptr poa,
290                          PortableServer::ObjectId* contId,
291                          const char* instanceName,
292                          const char* interfaceName)
293     {
294         :sat:{CPPCMP}* component = new :sat:{CPPCMP}(orb, poa, contId, instanceName, interfaceName);
295         return component->getId();
296     }
297 }