]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCK_I/HEXABLOCK_1.cxx
Salome HOME
Merge from V6_main 01/04/2013
[modules/hexablock.git] / src / HEXABLOCK_I / HEXABLOCK_1.cxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
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.
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
20
21 #include "HEXABLOCK.hxx"
22
23 #include "SALOME_NamingService.hxx"
24
25 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
26
27 #include "utilities.h"
28 #include "Utils_ExceptHandlers.hxx"
29
30 #include <TCollection_AsciiString.hxx>
31
32 static int MYDEBUG = 1;
33
34
35 using namespace HEXABLOCK_ORB;
36
37 //=============================================================================
38 /*!
39  *  SMESH_Gen_i::CanPublishInStudy
40  *
41  *  Returns true if object can be published in the study
42  */
43 //=============================================================================
44 bool HEXABLOCK_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
45 {
46   if(CORBA::is_nil(myCurrentStudy))
47     return false;
48   
49   Document_var aDoc       = Document::_narrow(theIOR);
50   if( !aDoc->_is_nil() )
51     return true;
52
53
54   return false;
55 }
56
57 //=======================================================================
58 //function : ObjectToSObject
59 //purpose  : 
60 //=======================================================================
61
62 SALOMEDS::SObject_ptr HEXABLOCK_Gen_i::ObjectToSObject( SALOMEDS::Study_ptr theStudy,
63                                                   CORBA::Object_ptr   theObject )
64 {
65   SALOMEDS::SObject_var aSO;
66   if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
67   {
68     CORBA::String_var objStr = HEXABLOCK_Gen_i::GetORB()->object_to_string( theObject );
69     aSO = theStudy->FindObjectIOR( objStr.in() );
70   }
71   return aSO._retn();
72 }
73
74
75
76 //=======================================================================
77 //function : publish
78 //purpose  : 
79 //=======================================================================
80
81 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr   theStudy,
82                                      CORBA::Object_ptr     theIOR,
83                                      SALOMEDS::SObject_ptr theFatherObject,
84                                      const int             theTag = 0,
85                                      const char*           thePixMap = 0,
86                                      const bool            theSelectable = true)
87 {
88   SALOMEDS::SObject_var SO = HEXABLOCK_Gen_i::ObjectToSObject( theStudy, theIOR );
89   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
90   if ( SO->_is_nil() ) {
91     if ( theTag == 0 )
92       SO = aStudyBuilder->NewObject( theFatherObject );
93     else if ( !theFatherObject->FindSubObject( theTag, SO ))
94       SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
95   }
96
97   SALOMEDS::GenericAttribute_var anAttr;
98   if ( !CORBA::is_nil( theIOR )) {
99     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
100     CORBA::String_var objStr = HEXABLOCK_Gen_i::GetORB()->object_to_string( theIOR );
101     SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
102   }
103
104   if ( thePixMap ) {
105     anAttr  = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
106     SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
107     pm->SetPixMap( thePixMap );
108   }
109
110   return SO._retn();
111 }
112
113
114 //=======================================================================
115 //function : setName
116 //purpose  : 
117 //=======================================================================
118 std::string HEXABLOCK_Gen_i::SetName( SALOMEDS::SObject_ptr theSObject,
119                          const char*           theName,
120                          const char*           theDefaultName )
121 {
122   std::string attrName;
123   if (theSObject->_is_nil() ) 
124      return attrName;
125      
126   SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
127   SALOMEDS::GenericAttribute_var anAttr =
128       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
129   SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
130     if ( theName && strlen( theName ) != 0 )
131       attrName = theName;
132     else {
133       CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
134       if ( strlen( curName ) == 0 ) {
135         TCollection_AsciiString aName( (char*) theDefaultName );
136         attrName = aName.ToCString();
137       }
138     }
139
140   aNameAttr->SetValue( attrName.c_str() );
141   return attrName;
142 }
143
144 // =======================================================================
145 // function : PublishComponent
146 // purpose  : 
147 // =======================================================================
148 SALOMEDS::SComponent_ptr HEXABLOCK_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
149 {
150   if ( CORBA::is_nil( theStudy ))
151     return SALOMEDS::SComponent::_nil();
152
153   SALOMEDS::SComponent_var father =
154     SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
155   if ( !CORBA::is_nil( father ) )
156     return father._retn();
157
158   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
159     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
160   if ( CORBA::is_nil( aCat ) )
161     return father._retn();
162
163   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
164   if ( CORBA::is_nil( aComp ) )
165     return father._retn();
166
167   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
168   SALOMEDS::GenericAttribute_var anAttr;
169   SALOMEDS::AttributePixMap_var  aPixmap;
170
171   father  = aStudyBuilder->NewComponent( ComponentDataType() );
172   aStudyBuilder->DefineComponentInstance( father, HEXABLOCK_Gen_i::_this() );
173
174
175   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
176   aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
177   aPixmap ->SetPixMap( "ICON_OBJBROWSER_HEXABLOCK" );
178   SetName( father, aComp->componentusername(), "HEXABLOCK" );
179
180   return father._retn();
181 }
182
183
184
185
186
187
188 // bool HEXABLOCKGUI::createSComponent() //addComponent
189 // {
190 //   DEBTRACE("HEXABLOCKGUI::createSComponent");
191 //   _PTR(Study)            aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
192 //   _PTR(StudyBuilder)     aBuilder (aStudy->NewBuilder());
193 //   _PTR(GenericAttribute) anAttr;
194 //   _PTR(AttributeName)    aName;
195 // 
196 //   // --- Find or create "HEXABLOCK" SComponent in the study
197 //   _PTR(SComponent) aComponent = aStudy->FindComponent("HEXABLOCK");
198 //   if ( !aComponent )
199 //     {
200 //       aComponent = aBuilder->NewComponent("HEXABLOCK");
201 //       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
202 //       aName = _PTR(AttributeName) (anAttr);
203 //       aName->SetValue(getApp()->moduleTitle("HEXABLOCK").toStdString());
204 //       
205 //       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
206 //       _PTR(AttributePixMap) aPixmap(anAttr);
207 //       aPixmap->SetPixMap("share/salome/resources/hexablock/ModuleHexablock.png");
208 //       
209 //       aBuilder->DefineComponentInstance(aComponent, getApp()->defaultEngineIOR().toStdString());
210 // 
211 //       DEBTRACE("HEXABLOCKGUI::createSComponent defaultEngineIOR=>"<<getApp()->defaultEngineIOR().toStdString());
212 // //       SalomeApp_DataModel::synchronize( aComponent, HEXABLOCKGUI::activeStudy() );
213 //       return true;
214 //     }
215 //   return false;
216 // }
217
218
219
220 //=============================================================================
221 /*!
222  *  findMaxChildTag [ static internal ]
223  *
224  *  Finds maximum child tag for the given object
225  */
226 //=============================================================================
227
228 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
229 {
230   long aTag = 0;
231   if ( !theSObject->_is_nil() ) {
232     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
233     if ( !aStudy->_is_nil() ) {
234       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
235       for ( ; anIter->More(); anIter->Next() ) {
236         long nTag = anIter->Value()->Tag();
237         if ( nTag > aTag )
238           aTag = nTag;
239       }
240     }
241   }
242   return aTag;
243 }
244
245 //=======================================================================
246 //function : PublishMesh
247 //purpose  : 
248 //=======================================================================
249 SALOMEDS::SObject_ptr HEXABLOCK_Gen_i::PublishDoc ( SALOMEDS::Study_ptr     theStudy,
250                                               Document_ptr theDoc,
251                                               const char*             theName)
252 {
253   if ( CORBA::is_nil( theStudy ) ||
254        CORBA::is_nil( theDoc ))
255     return SALOMEDS::SComponent::_nil();
256
257   // find or publish a mesh
258
259   SALOMEDS::SObject_var aDocSO = ObjectToSObject( theStudy, theDoc );
260   if ( aDocSO->_is_nil() )
261   {
262     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
263     if ( father->_is_nil() )
264       return aDocSO._retn();
265
266     // Find correct free tag
267     long aTag = findMaxChildTag( father.in() );
268     aTag++;
269
270     aDocSO = publish (theStudy, theDoc, father, aTag);
271     if ( aDocSO->_is_nil() )
272       return aDocSO._retn();
273   }
274   std::string docName = SetName( aDocSO, theName, theDoc->getName()); // Bph 
275
276   // Add shape reference
277
278   return aDocSO._retn();
279 }
280