Version: 6.3.1

src/SMESHGUI/SMESHGUI_Utils.cxx

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // SMESH SMESHGUI : GUI for SMESH component
00024 // File   : SMESHGUI_Utils.cxx
00025 // Author : Open CASCADE S.A.S.
00026 // SMESH includes
00027 //
00028 #include "SMESHGUI_Utils.h"
00029 #include "SMESHGUI.h"
00030 #include "SMESHGUI_Selection.h"
00031 #include "SMESH_Type.h"
00032 
00033 #include <SMDS_MeshNode.hxx>
00034 #include <SMDS_MeshFace.hxx>
00035 
00036 // SALOME GUI includes
00037 #include <SUIT_Desktop.h>
00038 #include <SUIT_Session.h>
00039 #include <SUIT_MessageBox.h>
00040 #include <SUIT_ResourceMgr.h>
00041 
00042 #include <LightApp_SelectionMgr.h>
00043 #include <SalomeApp_Application.h>
00044 #include <SalomeApp_Module.h>
00045 #include <SalomeApp_Study.h>
00046 
00047 #include <SALOME_ListIO.hxx>
00048 
00049 // OCC includes
00050 #include <gp_XYZ.hxx>
00051 #include <TColgp_Array1OfXYZ.hxx>
00052 
00053 namespace SMESH
00054 {
00055   SUIT_Desktop*
00056   GetDesktop(const CAM_Module* theModule)
00057   {
00058     return theModule->application()->desktop();
00059   }
00060 
00061   LightApp_SelectionMgr*
00062   GetSelectionMgr(const SalomeApp_Module* theModule)
00063   {
00064     return theModule->getApp()->selectionMgr();
00065   }
00066 
00067   SUIT_ResourceMgr*
00068   GetResourceMgr( const SalomeApp_Module* )
00069   {
00070     return SUIT_Session::session()->resourceMgr();
00071   }
00072 
00073   _PTR(Study)
00074   GetCStudy(const SalomeApp_Study* theStudy)
00075   {
00076     return theStudy->studyDS();
00077   }
00078 
00079   CORBA::Object_var 
00080   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
00081   {
00082     CORBA::Object_var anObj;
00083     if(theOwner){
00084       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
00085       if(!anIO.IsNull()){
00086         if(anIO->hasEntry()){
00087           _PTR(Study) aStudy = GetActiveStudyDocument();
00088           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
00089           anObj = SObjectToObject(aSObj,aStudy);
00090         }
00091       }
00092     }
00093     return anObj;
00094   }
00095 
00096   SUIT_Study* GetActiveStudy()
00097   {
00098     SUIT_Application* app = SUIT_Session::session()->activeApplication();
00099     if (app)
00100       return app->activeStudy();
00101     else
00102       return NULL;
00103   }
00104 
00105   SUIT_ViewWindow* GetActiveWindow()
00106   {
00107     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00108       (SUIT_Session::session()->activeApplication());
00109     if (app && app->desktop() )
00110       return app->desktop()->activeWindow();
00111     else
00112       return NULL;
00113   }
00114 
00115   _PTR(Study) GetActiveStudyDocument()
00116   {
00117     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
00118     if (aStudy)
00119       return aStudy->studyDS();
00120     else
00121       return _PTR(Study)();
00122   }
00123 
00124   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
00125   {
00126     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00127       (SUIT_Session::session()->activeApplication());
00128     if (app && !CORBA::is_nil(theObject)) {
00129       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
00130         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
00131         if (strcmp(anIOR.in(), "") != 0)
00132           return aStudy->FindObjectIOR(anIOR.in());
00133       }
00134     }
00135     return _PTR(SObject)();
00136   }
00137 
00138   void SetName (_PTR(SObject) theSObject, const QString& theName)
00139   {
00140     _PTR(Study) aStudy = GetActiveStudyDocument();
00141     if (aStudy->GetProperties()->IsLocked())
00142       return;
00143     SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toLatin1().data());
00144   }
00145 
00146   void SetValue (_PTR(SObject) theSObject, const QString& theValue)
00147   {
00148     _PTR(Study) aStudy = GetActiveStudyDocument();
00149     if (aStudy->GetProperties()->IsLocked())
00150       return;
00151     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00152     _PTR(GenericAttribute) anAttr =
00153       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
00154     _PTR(AttributeComment) aComment = anAttr;
00155     if (aComment)
00156       aComment->SetValue(theValue.toLatin1().data());
00157   }
00158   
00159   void setFileName (_PTR(SObject) theSObject, const QString& theValue)
00160   {
00161     _PTR(Study) aStudy = GetActiveStudyDocument();
00162     if (aStudy->GetProperties()->IsLocked())
00163       return;
00164     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00165     _PTR(GenericAttribute) anAttr =
00166       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
00167     _PTR(AttributeExternalFileDef) aFileName = anAttr;
00168     if (aFileName)
00169       aFileName->SetValue(theValue.toLatin1().data());
00170   }
00171   
00172   void setFileType (_PTR(SObject) theSObject, const QString& theValue)
00173   {
00174     _PTR(Study) aStudy = GetActiveStudyDocument();
00175     if (aStudy->GetProperties()->IsLocked())
00176       return;
00177     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00178     _PTR(GenericAttribute) anAttr =
00179       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
00180     _PTR(AttributeFileType) aFileType = anAttr;
00181     if (aFileType)
00182       aFileType->SetValue(theValue.toLatin1().data());
00183   }
00184 
00185   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
00186                                      _PTR(Study)   theStudy)
00187   {
00188     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00189       (SUIT_Session::session()->activeApplication());
00190     if (theSObject) {
00191       _PTR(GenericAttribute) anAttr;
00192       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
00193         _PTR(AttributeIOR) anIOR = anAttr;
00194         CORBA::String_var aVal = anIOR->Value().c_str();
00195         // string_to_object() DOC: If the input string is not valid ...
00196         // a CORBA::SystemException is thrown.
00197         if ( aVal && strlen( aVal ) > 0 )
00198           return app->orb()->string_to_object(aVal);
00199       }
00200     }
00201     return CORBA::Object::_nil();
00202   }
00203 
00204   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
00205   {
00206     _PTR(Study) aStudy = GetActiveStudyDocument();
00207     return SObjectToObject(theSObject,aStudy);
00208   }
00209 
00210   _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
00211   {
00212     _PTR(SObject) res;
00213     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00214       (SUIT_Session::session()->activeApplication());
00215     if ( app ) {
00216       QString IOR = app->orb()->object_to_string( theObject );
00217       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00218       if ( study && !IOR.isEmpty() )
00219         res = study->studyDS()->FindObjectIOR( IOR.toLatin1().constData() );
00220     }
00221     return res;
00222   }
00223 
00224   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
00225   {
00226     if (!theIO.IsNull()) {
00227       if (theIO->hasEntry()) {
00228         _PTR(Study) aStudy = GetActiveStudyDocument();
00229         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
00230         return SObjectToObject(anObj,aStudy);
00231       }
00232     }
00233     return CORBA::Object::_nil();
00234   }
00235 
00236   CORBA::Object_var IORToObject (const QString& theIOR)
00237   {
00238     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00239       (SUIT_Session::session()->activeApplication());
00240     return app->orb()->string_to_object(theIOR.toLatin1().data());
00241   }
00242 
00243   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
00244   {
00245     if (!theMgr)
00246       return 0;
00247 
00248     SALOME_ListIO selected;
00249     theMgr->selectedObjects(selected);
00250     int aNbSel = selected.Extent();
00251     if (aNbSel == 1) {
00252       Handle(SALOME_InteractiveObject) anIObject = selected.First();
00253       theName = anIObject->getName();
00254     } else {
00255       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
00256     }
00257     return aNbSel;
00258   }
00259 
00260   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
00261   {
00262     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
00263     if (!aShape->_is_nil()){ //It s a shape
00264       return theSObject->GetFather();
00265     }
00266     _PTR(SObject) aSObject;
00267     if (theSObject->ReferencedObject(aSObject)) {
00268       aSObject = theSObject->GetFather();
00269       return aSObject->GetFather();
00270     }
00271     return theSObject->GetFather();
00272   }
00273 
00274   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
00275   {
00276     _PTR(Study) aStudy = GetActiveStudyDocument();
00277     if (aStudy->GetProperties()->IsLocked())
00278       return;
00279 
00280     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00281     _PTR(GenericAttribute) anAttr =
00282       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
00283     _PTR(AttributePixMap) aPixmap = anAttr;
00284 
00285     std::string pmName;
00286     if (theIsNotModif)
00287       pmName = "ICON_SMESH_TREE_MESH";
00288     else if ( isEmptyMesh )
00289       pmName = "ICON_SMESH_TREE_MESH_WARN";
00290     else
00291       pmName = "ICON_SMESH_TREE_MESH_PARTIAL";
00292     aPixmap->SetPixMap( pmName );
00293 
00294     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
00295     for (int i = 1; anIter->More(); anIter->Next(), i++) {
00296       _PTR(SObject) aSObj = anIter->Value();
00297       if (i >= 4) {
00298         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
00299         for ( ; anIter1->More(); anIter1->Next())
00300         {
00301           _PTR(SObject) aSObj1 = anIter1->Value();
00302 
00303           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
00304           aPixmap = anAttr;
00305 
00306           std::string entry = aSObj1->GetID();
00307           int objType = SMESHGUI_Selection::type( entry.c_str(), aStudy );
00308 
00309           SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
00310           if ( !idSrc->_is_nil() )
00311           {
00312             SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
00313             const bool isEmpty = ( elemTypes->length() == 0 );
00314             if ( isEmpty )
00315               aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
00316             else if ( objType == GROUP )
00317               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
00318             else
00319               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
00320           }
00321           else
00322           {
00323             if ( !theIsNotModif )
00324               aPixmap->SetPixMap( pmName );
00325             else if ( objType == GROUP )
00326               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
00327             else
00328               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
00329           }
00330         }
00331       }
00332     }
00333   }
00334 
00335   void ShowHelpFile (const QString& theHelpFileName)
00336   {
00337     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
00338     if (app) {
00339       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
00340       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
00341                                theHelpFileName);
00342     }
00343     else {
00344       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
00345                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
00346                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
00347                                                                    "application")).
00348                                arg(theHelpFileName));
00349     }
00350   }
00351 
00352   //=======================================================================
00356   //=======================================================================
00357   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
00358   {
00359     gp_XYZ n;
00360     int aNbNode = theFace->NbNodes();
00361     TColgp_Array1OfXYZ anArrOfXYZ(1,4);
00362     SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
00363     int i = 1;
00364     for ( ; aNodeItr->more() && i <= 4; i++ ) {
00365       SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
00366       anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
00367     }
00368     
00369     gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
00370     gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
00371     n  = q1 ^ q2;
00372     if ( aNbNode > 3 ) {
00373       gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
00374       n += q2 ^ q3;
00375     }
00376     double len = n.Modulus();
00377     if ( len > 0 )
00378       n /= len;
00379     return n;
00380   }
00381   
00382 } // end of namespace SMESH
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS