Back to index

salome-smesh  6.5.0
SMESHGUI_Utils.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  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 #include CORBA_SERVER_HEADER(SMESH_Group)
00054 
00055 namespace SMESH
00056 {
00057   SUIT_Desktop*
00058   GetDesktop(const CAM_Module* theModule)
00059   {
00060     return theModule->application()->desktop();
00061   }
00062 
00063   LightApp_SelectionMgr*
00064   GetSelectionMgr(const SalomeApp_Module* theModule)
00065   {
00066     return theModule->getApp()->selectionMgr();
00067   }
00068 
00069   SUIT_ResourceMgr*
00070   GetResourceMgr( const SalomeApp_Module* )
00071   {
00072     return SUIT_Session::session()->resourceMgr();
00073   }
00074 
00075   _PTR(Study)
00076   GetCStudy(const SalomeApp_Study* theStudy)
00077   {
00078     return theStudy->studyDS();
00079   }
00080 
00081   CORBA::Object_var 
00082   DataOwnerToObject(const LightApp_DataOwnerPtr& theOwner)
00083   {
00084     CORBA::Object_var anObj;
00085     if(theOwner){
00086       const Handle(SALOME_InteractiveObject)& anIO = theOwner->IO();
00087       if(!anIO.IsNull()){
00088         if(anIO->hasEntry()){
00089           _PTR(Study) aStudy = GetActiveStudyDocument();
00090           _PTR(SObject) aSObj = aStudy->FindObjectID(anIO->getEntry());
00091           anObj = SObjectToObject(aSObj,aStudy);
00092         }
00093       }
00094     }
00095     return anObj;
00096   }
00097 
00098   SUIT_Study* GetActiveStudy()
00099   {
00100     SUIT_Application* app = SUIT_Session::session()->activeApplication();
00101     if (app)
00102       return app->activeStudy();
00103     else
00104       return NULL;
00105   }
00106 
00107   SUIT_ViewWindow* GetActiveWindow()
00108   {
00109     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00110       (SUIT_Session::session()->activeApplication());
00111     if (app && app->desktop() )
00112       return app->desktop()->activeWindow();
00113     else
00114       return NULL;
00115   }
00116 
00117   _PTR(Study) GetActiveStudyDocument()
00118   {
00119     SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(GetActiveStudy());
00120     if (aStudy)
00121       return aStudy->studyDS();
00122     else
00123       return _PTR(Study)();
00124   }
00125 
00126   _PTR(SObject) FindSObject (CORBA::Object_ptr theObject)
00127   {
00128     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00129       (SUIT_Session::session()->activeApplication());
00130     if (app && !CORBA::is_nil(theObject)) {
00131       if(_PTR(Study) aStudy = GetActiveStudyDocument()){
00132         CORBA::String_var anIOR = app->orb()->object_to_string(theObject);
00133         if (strcmp(anIOR.in(), "") != 0)
00134           return aStudy->FindObjectIOR(anIOR.in());
00135       }
00136     }
00137     return _PTR(SObject)();
00138   }
00139 
00140   void SetName (_PTR(SObject) theSObject, const QString& theName)
00141   {
00142     _PTR(Study) aStudy = GetActiveStudyDocument();
00143     if (aStudy->GetProperties()->IsLocked())
00144       return;
00145     SMESHGUI::GetSMESHGen()->SetName(theSObject->GetIOR().c_str(), theName.toLatin1().data());
00146   }
00147 
00148   void SetValue (_PTR(SObject) theSObject, const QString& theValue)
00149   {
00150     _PTR(Study) aStudy = GetActiveStudyDocument();
00151     if (aStudy->GetProperties()->IsLocked())
00152       return;
00153     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00154     _PTR(GenericAttribute) anAttr =
00155       aBuilder->FindOrCreateAttribute(theSObject, "AttributeComment");
00156     _PTR(AttributeComment) aComment = anAttr;
00157     if (aComment)
00158       aComment->SetValue(theValue.toLatin1().data());
00159   }
00160   
00161   void setFileName (_PTR(SObject) theSObject, const QString& theValue)
00162   {
00163     _PTR(Study) aStudy = GetActiveStudyDocument();
00164     if (aStudy->GetProperties()->IsLocked())
00165       return;
00166     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00167     _PTR(GenericAttribute) anAttr =
00168       aBuilder->FindOrCreateAttribute(theSObject, "AttributeExternalFileDef");
00169     _PTR(AttributeExternalFileDef) aFileName = anAttr;
00170     if (aFileName)
00171       aFileName->SetValue(theValue.toLatin1().data());
00172   }
00173   
00174   void setFileType (_PTR(SObject) theSObject, const QString& theValue)
00175   {
00176     _PTR(Study) aStudy = GetActiveStudyDocument();
00177     if (aStudy->GetProperties()->IsLocked())
00178       return;
00179     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00180     _PTR(GenericAttribute) anAttr =
00181       aBuilder->FindOrCreateAttribute(theSObject, "AttributeFileType");
00182     _PTR(AttributeFileType) aFileType = anAttr;
00183     if (aFileType)
00184       aFileType->SetValue(theValue.toLatin1().data());
00185   }
00186 
00187   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject,
00188                                      _PTR(Study)   theStudy)
00189   {
00190     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00191       (SUIT_Session::session()->activeApplication());
00192     if (theSObject) {
00193       _PTR(GenericAttribute) anAttr;
00194       if (theSObject->FindAttribute(anAttr, "AttributeIOR")) {
00195         _PTR(AttributeIOR) anIOR = anAttr;
00196         CORBA::String_var aVal = anIOR->Value().c_str();
00197         // string_to_object() DOC: If the input string is not valid ...
00198         // a CORBA::SystemException is thrown.
00199         if ( aVal && strlen( aVal ) > 0 )
00200           return app->orb()->string_to_object(aVal);
00201       }
00202     }
00203     return CORBA::Object::_nil();
00204   }
00205 
00206   CORBA::Object_var SObjectToObject (_PTR(SObject) theSObject)
00207   {
00208     _PTR(Study) aStudy = GetActiveStudyDocument();
00209     return SObjectToObject(theSObject,aStudy);
00210   }
00211 
00212   _PTR(SObject) ObjectToSObject( CORBA::Object_ptr theObject )
00213   {
00214     _PTR(SObject) res;
00215     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00216       (SUIT_Session::session()->activeApplication());
00217     if ( app ) {
00218       QString IOR = app->orb()->object_to_string( theObject );
00219       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00220       if ( study && !IOR.isEmpty() )
00221         res = study->studyDS()->FindObjectIOR( IOR.toLatin1().constData() );
00222     }
00223     return res;
00224   }
00225 
00226   CORBA::Object_var IObjectToObject (const Handle(SALOME_InteractiveObject)& theIO)
00227   {
00228     if (!theIO.IsNull()) {
00229       if (theIO->hasEntry()) {
00230         _PTR(Study) aStudy = GetActiveStudyDocument();
00231         _PTR(SObject) anObj = aStudy->FindObjectID(theIO->getEntry());
00232         return SObjectToObject(anObj,aStudy);
00233       }
00234     }
00235     return CORBA::Object::_nil();
00236   }
00237 
00238   CORBA::Object_var IORToObject (const QString& theIOR)
00239   {
00240     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
00241       (SUIT_Session::session()->activeApplication());
00242     return app->orb()->string_to_object(theIOR.toLatin1().data());
00243   }
00244 
00245   int GetNameOfSelectedIObjects(LightApp_SelectionMgr* theMgr, QString& theName)
00246   {
00247     if (!theMgr)
00248       return 0;
00249 
00250     SALOME_ListIO selected;
00251     theMgr->selectedObjects(selected);
00252     int aNbSel = selected.Extent();
00253     if (aNbSel == 1) {
00254       Handle(SALOME_InteractiveObject) anIObject = selected.First();
00255       theName = QString( anIObject->getName() ).trimmed();
00256     } else {
00257       theName = QObject::tr("SMESH_OBJECTS_SELECTED").arg(aNbSel);
00258     }
00259     return aNbSel;
00260   }
00261 
00262   _PTR(SObject) GetMeshOrSubmesh (_PTR(SObject) theSObject)
00263   {
00264     GEOM::GEOM_Object_var aShape = SObjectToInterface<GEOM::GEOM_Object>(theSObject);
00265     if (!aShape->_is_nil()){ //It s a shape
00266       return theSObject->GetFather();
00267     }
00268     _PTR(SObject) aSObject;
00269     if (theSObject->ReferencedObject(aSObject)) {
00270       aSObject = theSObject->GetFather();
00271       return aSObject->GetFather();
00272     }
00273     return theSObject->GetFather();
00274   }
00275 
00276   void ModifiedMesh (_PTR(SObject) theSObject, bool theIsNotModif, bool isEmptyMesh)
00277   {
00278     _PTR(Study) aStudy = GetActiveStudyDocument();
00279     if (aStudy->GetProperties()->IsLocked())
00280       return;
00281 
00282     _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
00283     _PTR(GenericAttribute) anAttr =
00284       aBuilder->FindOrCreateAttribute(theSObject,"AttributePixMap");
00285     _PTR(AttributePixMap) aPixmap = anAttr;
00286 
00287     std::string pmName;
00288     if (theIsNotModif)
00289       pmName = "ICON_SMESH_TREE_MESH";
00290     else if ( isEmptyMesh )
00291       pmName = "ICON_SMESH_TREE_MESH_WARN";
00292     else
00293       pmName = "ICON_SMESH_TREE_MESH_PARTIAL";
00294     aPixmap->SetPixMap( pmName );
00295 
00296     _PTR(ChildIterator) anIter = aStudy->NewChildIterator(theSObject);
00297     for (int i = 1; anIter->More(); anIter->Next(), i++) {
00298       _PTR(SObject) aSObj = anIter->Value();
00299       if (i >= 4) {
00300         _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSObj);
00301         for ( ; anIter1->More(); anIter1->Next())
00302         {
00303           _PTR(SObject) aSObj1 = anIter1->Value();
00304 
00305           anAttr = aBuilder->FindOrCreateAttribute(aSObj1, "AttributePixMap");
00306           aPixmap = anAttr;
00307 
00308           std::string entry = aSObj1->GetID();
00309           int objType = SMESHGUI_Selection::type( entry.c_str(), aStudy );
00310 
00311           SMESH::SMESH_IDSource_var idSrc = SObjectToInterface<SMESH::SMESH_IDSource>( aSObj1 );
00312           if ( !idSrc->_is_nil() )
00313           {
00314             SMESH::SMESH_GroupOnFilter_var gof =
00315               SObjectToInterface<SMESH::SMESH_GroupOnFilter>( aSObj1 );
00316             const bool isGroupOnFilter = !gof->_is_nil();
00317 
00318             bool isEmpty = false;
00319             if ( !isGroupOnFilter ) // GetTypes() can be very long on isGroupOnFilter!
00320             {
00321               SMESH::array_of_ElementType_var elemTypes = idSrc->GetTypes();
00322               isEmpty = ( elemTypes->length() == 0 );
00323             }
00324             if ( isEmpty )
00325               aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_WARN");
00326             else if ( objType != GROUP )
00327               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
00328             else if ( isGroupOnFilter )
00329               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP_ON_FILTER" );
00330             else
00331               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
00332           }
00333           else
00334           {
00335             if ( !theIsNotModif )
00336               aPixmap->SetPixMap( pmName );
00337             else if ( objType == GROUP )
00338               aPixmap->SetPixMap( "ICON_SMESH_TREE_GROUP" );
00339             else
00340               aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH" );
00341           }
00342         }
00343       }
00344     }
00345   }
00346 
00347   void ShowHelpFile (const QString& theHelpFileName)
00348   {
00349     LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
00350     if (app) {
00351       SMESHGUI* gui = SMESHGUI::GetSMESHGUI();
00352       app->onHelpContextModule(gui ? app->moduleName(gui->moduleName()) : QString(""),
00353                                theHelpFileName);
00354     }
00355     else {
00356       SUIT_MessageBox::warning(0, QObject::tr("WRN_WARNING"),
00357                                QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
00358                                arg(app->resourceMgr()->stringValue("ExternalBrowser", 
00359                                                                    "application")).
00360                                arg(theHelpFileName));
00361     }
00362   }
00363 
00364   //=======================================================================
00368   //=======================================================================
00369   gp_XYZ getNormale( const SMDS_MeshFace* theFace )
00370   {
00371     gp_XYZ n;
00372     int aNbNode = theFace->NbNodes();
00373     TColgp_Array1OfXYZ anArrOfXYZ(1,4);
00374     SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
00375     int i = 1;
00376     for ( ; aNodeItr->more() && i <= 4; i++ ) {
00377       SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
00378       anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
00379     }
00380     
00381     gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
00382     gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
00383     n  = q1 ^ q2;
00384     if ( aNbNode > 3 ) {
00385       gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
00386       n += q2 ^ q3;
00387     }
00388     double len = n.Modulus();
00389     if ( len > 0 )
00390       n /= len;
00391     return n;
00392   }
00393   
00394 } // end of namespace SMESH