1 // Copyright (C) 2009-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "HEXABLOCK.hxx"
23 #include "SALOME_NamingService.hxx"
25 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
27 #include "utilities.h"
28 #include "Utils_ExceptHandlers.hxx"
30 #include <TCollection_AsciiString.hxx>
32 static int MYDEBUG = 1;
35 using namespace HEXABLOCK_ORB;
37 //=============================================================================
39 * SMESH_Gen_i::CanPublishInStudy
41 * Returns true if object can be published in the study
43 //=============================================================================
44 bool HEXABLOCK_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
46 if(CORBA::is_nil(myCurrentStudy))
49 Document_var aDoc = Document::_narrow(theIOR);
50 if( !aDoc->_is_nil() )
57 //=======================================================================
58 //function : ObjectToSObject
60 //=======================================================================
62 SALOMEDS::SObject_ptr HEXABLOCK_Gen_i::ObjectToSObject( SALOMEDS::Study_ptr theStudy,
63 CORBA::Object_ptr theObject )
65 SALOMEDS::SObject_var aSO;
66 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
68 CORBA::String_var objStr = HEXABLOCK_Gen_i::GetORB()->object_to_string( theObject );
69 aSO = theStudy->FindObjectIOR( objStr.in() );
76 //=======================================================================
79 //=======================================================================
81 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr theStudy,
82 CORBA::Object_ptr theIOR,
83 SALOMEDS::SObject_ptr theFatherObject,
85 const char* thePixMap = 0,
86 const bool theSelectable = true)
88 SALOMEDS::SObject_var SO = HEXABLOCK_Gen_i::ObjectToSObject( theStudy, theIOR );
89 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
90 if ( SO->_is_nil() ) {
92 SO = aStudyBuilder->NewObject( theFatherObject );
93 else if ( !theFatherObject->FindSubObject( theTag, SO ))
94 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
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() );
105 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
106 SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
107 pm->SetPixMap( thePixMap );
114 //=======================================================================
117 //=======================================================================
118 std::string HEXABLOCK_Gen_i::SetName( SALOMEDS::SObject_ptr theSObject,
120 const char* theDefaultName )
122 std::string attrName;
123 if (theSObject->_is_nil() )
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 )
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();
140 aNameAttr->SetValue( attrName.c_str() );
144 // =======================================================================
145 // function : PublishComponent
147 // =======================================================================
148 SALOMEDS::SComponent_ptr HEXABLOCK_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
150 if ( CORBA::is_nil( theStudy ))
151 return SALOMEDS::SComponent::_nil();
153 SALOMEDS::SComponent_var father =
154 SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
155 if ( !CORBA::is_nil( father ) )
156 return father._retn();
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();
163 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
164 if ( CORBA::is_nil( aComp ) )
165 return father._retn();
167 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
168 SALOMEDS::GenericAttribute_var anAttr;
169 SALOMEDS::AttributePixMap_var aPixmap;
171 father = aStudyBuilder->NewComponent( ComponentDataType() );
172 aStudyBuilder->DefineComponentInstance( father, HEXABLOCK_Gen_i::_this() );
175 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
176 aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
177 aPixmap ->SetPixMap( "ICON_OBJBROWSER_HEXABLOCK" );
178 SetName( father, aComp->componentusername(), "HEXABLOCK" );
180 return father._retn();
188 // bool HEXABLOCKGUI::createSComponent() //addComponent
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;
196 // // --- Find or create "HEXABLOCK" SComponent in the study
197 // _PTR(SComponent) aComponent = aStudy->FindComponent("HEXABLOCK");
198 // if ( !aComponent )
200 // aComponent = aBuilder->NewComponent("HEXABLOCK");
201 // anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
202 // aName = _PTR(AttributeName) (anAttr);
203 // aName->SetValue(getApp()->moduleTitle("HEXABLOCK").toStdString());
205 // anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
206 // _PTR(AttributePixMap) aPixmap(anAttr);
207 // aPixmap->SetPixMap("share/salome/resources/hexablock/ModuleHexablock.png");
209 // aBuilder->DefineComponentInstance(aComponent, getApp()->defaultEngineIOR().toStdString());
211 // DEBTRACE("HEXABLOCKGUI::createSComponent defaultEngineIOR=>"<<getApp()->defaultEngineIOR().toStdString());
212 // // SalomeApp_DataModel::synchronize( aComponent, HEXABLOCKGUI::activeStudy() );
220 //=============================================================================
222 * findMaxChildTag [ static internal ]
224 * Finds maximum child tag for the given object
226 //=============================================================================
228 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
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();
245 //=======================================================================
246 //function : PublishMesh
248 //=======================================================================
249 SALOMEDS::SObject_ptr HEXABLOCK_Gen_i::PublishDoc ( SALOMEDS::Study_ptr theStudy,
253 if ( CORBA::is_nil( theStudy ) ||
254 CORBA::is_nil( theDoc ))
255 return SALOMEDS::SComponent::_nil();
257 // find or publish a mesh
259 SALOMEDS::SObject_var aDocSO = ObjectToSObject( theStudy, theDoc );
260 if ( aDocSO->_is_nil() )
262 SALOMEDS::SComponent_var father = PublishComponent( theStudy );
263 if ( father->_is_nil() )
264 return aDocSO._retn();
266 // Find correct free tag
267 long aTag = findMaxChildTag( father.in() );
270 aDocSO = publish (theStudy, theDoc, father, aTag);
271 if ( aDocSO->_is_nil() )
272 return aDocSO._retn();
274 std::string docName = SetName( aDocSO, theName, theDoc->getName()); // Bph
276 // Add shape reference
278 return aDocSO._retn();