Salome HOME
Merge from BR_PARAVIS_DEV 29Dec09
[modules/paravis.git] / src / PVGUI / PV_Tools.cxx
1 //  Copyright (C) 2007-2008  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 //  VISU OBJECT : interactive object for VISU entities implementation
23 //  File   : PV_Tools.cxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26 //
27 #include "PV_Tools.h"  
28 #include "PARAVIS_Gen_i.hh"
29
30 #include <omnithread.h>
31 #include CORBA_SERVER_HEADER(SALOME_Session)
32 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
33
34 #include <SALOME_NamingService.hxx>
35
36 #include <QString>
37
38 namespace PARAVIS
39 {
40 //----------------------------------------------------------------------------
41   SALOMEDS::ListOfFileNames* GetListOfFileNames(const QStringList& theFileNames)
42   {
43     SALOMEDS::ListOfFileNames_var aListOfFileNames = new SALOMEDS::ListOfFileNames;
44     if(!theFileNames.isEmpty()){
45       aListOfFileNames->length(theFileNames.size());
46       int i = 0;
47       foreach (QString aName, theFileNames) {
48         aListOfFileNames[i] = qPrintable(aName);
49         i++;
50       }
51     }
52     return aListOfFileNames._retn();
53   }
54
55   //----------------------------------------------------------------------------
56   SALOMEDS::SComponent_var FindOrCreateParaVisComponent(SALOMEDS::Study_ptr theStudyDocument)
57   {
58     SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("PARAVIS");
59     if (aSComponent->_is_nil()) {
60       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
61       aStudyBuilder->NewCommand();
62       int aLocked = theStudyDocument->GetProperties()->IsLocked();
63       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
64       aSComponent = aStudyBuilder->NewComponent("PARAVIS");
65       SALOMEDS::GenericAttribute_var anAttr = 
66         aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
67       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
68       
69       CORBA::ORB_var anORB = PARAVIS_Gen_i::GetORB();
70       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
71       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
72       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = 
73         SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
74       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "PARAVIS" );
75       if ( !CORBA::is_nil(Comp) ) {
76         aName->SetValue( Comp->componentusername() );
77       }
78       
79       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
80       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
81       aPixmap->SetPixMap( "pqAppIcon16.png" );
82       
83       PARAVIS_Gen_var aParaVisGen = PARAVIS_Gen_i::GetParavisGenImpl()->_this();
84       aStudyBuilder->DefineComponentInstance(aSComponent, aParaVisGen);
85       if (aLocked) 
86         theStudyDocument->GetProperties()->SetLocked(true);
87       aStudyBuilder->CommitCommand();
88     }
89     return aSComponent;
90   }
91
92   PARAVIS_Base_i* GET_SERVANT(CORBA::Object_ptr aObj) {
93     //dynamic_cast<PARAVIS_Base_i*>(PARAVIS_Gen_i::GetPOA()->reference_to_servant(OBJ).in())
94     if (CORBA::is_nil(aObj))
95       return NULL;
96     try {
97       PortableServer::POA_ptr aPOA = PARAVIS_Gen_i::GetPOA();
98       PortableServer::ServantBase_var aServant = aPOA->reference_to_servant(aObj);
99       return dynamic_cast<PARAVIS_Base_i*>(aServant.in());
100     } catch (...) {
101       MESSAGE("GET_SERVANT: Unknown exception!!!");
102     }
103     return NULL;
104   }
105 };