Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GEOMGenUtils.cxx
1 // Copyright (C) 2007-2012  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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File   : SMESHGUI_GEOMGenUtils.cxx
25 // Author : Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_GEOMGenUtils.h"
29 #include "SMESHGUI_Utils.h"
30
31 // SALOME GEOM includes
32 #include <GeometryGUI.h>
33
34 // SALOME KERNEL includes
35 #include <SALOMEDS_SObject.hxx>
36
37 // IDL includes
38 #include <SALOMEconfig.h>
39 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
40
41 namespace SMESH
42 {
43   GEOM::GEOM_Gen_var GetGEOMGen()
44   {
45     static GEOM::GEOM_Gen_var aGEOMGen;
46
47     if(CORBA::is_nil(aGEOMGen)) {
48       if ( GeometryGUI::GetGeomGen()->_is_nil() )
49         GeometryGUI::InitGeomGen();
50       aGEOMGen = GeometryGUI::GetGeomGen();
51     }
52     return aGEOMGen;
53   }
54
55   GEOM::GEOM_Object_var GetShapeOnMeshOrSubMesh(_PTR(SObject) theMeshOrSubmesh,
56                                                 bool*         isMesh)
57   {
58     SALOMEDS_SObject* aMeshOrSubmesh = _CAST(SObject,theMeshOrSubmesh);
59     if(aMeshOrSubmesh) {
60       CORBA::Object_var Obj = aMeshOrSubmesh->GetObject();
61       if ( !CORBA::is_nil( Obj ) ) {
62         SMESH::SMESH_Mesh_var aMesh =
63           SObjectToInterface<SMESH::SMESH_Mesh>( theMeshOrSubmesh );
64         if ( !aMesh->_is_nil() )
65         {
66           if ( isMesh ) *isMesh = true;
67           return aMesh->GetShapeToMesh();
68         }
69         SMESH::SMESH_subMesh_var aSubmesh =
70           SObjectToInterface<SMESH::SMESH_subMesh>( theMeshOrSubmesh );
71         if ( !aSubmesh->_is_nil() )
72         {
73           if ( isMesh ) *isMesh = false;
74           return aSubmesh->GetSubShape();
75         }
76       }
77     }
78     return GEOM::GEOM_Object::_nil();
79   }
80
81   GEOM::GEOM_Object_ptr GetGeom (_PTR(SObject) theSO)
82   {
83     if (!theSO)
84       return GEOM::GEOM_Object::_nil();
85
86     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
87     if (!aStudy)
88       return GEOM::GEOM_Object::_nil();
89
90     _PTR(ChildIterator) anIter (aStudy->NewChildIterator(theSO));
91     for ( ; anIter->More(); anIter->Next()) {
92       _PTR(SObject) aSObject = anIter->Value();
93       _PTR(SObject) aRefSOClient;
94       GEOM::GEOM_Object_var aMeshShape;
95
96       if (aSObject->ReferencedObject(aRefSOClient)) {
97         SALOMEDS_SObject* aRefSO = _CAST(SObject,aRefSOClient);
98         aMeshShape = GEOM::GEOM_Object::_narrow(aRefSO->GetObject());
99       } else {
100         SALOMEDS_SObject* aSO = _CAST(SObject,aSObject);
101         aMeshShape = GEOM::GEOM_Object::_narrow(aSO->GetObject());
102       }
103
104       if (!aMeshShape->_is_nil())
105         return aMeshShape._retn();
106     }
107     return GEOM::GEOM_Object::_nil();
108   }
109
110   GEOM::GEOM_Object_ptr GetSubShape (GEOM::GEOM_Object_ptr theMainShape,
111                                      long                  theID)
112   {
113     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
114     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
115     if (!aStudy || geomGen->_is_nil())
116       return GEOM::GEOM_Object::_nil();
117     GEOM::GEOM_IShapesOperations_var aShapesOp = geomGen->GetIShapesOperations(aStudy->StudyId());
118     if (aShapesOp->_is_nil())
119       return GEOM::GEOM_Object::_nil();
120     GEOM::GEOM_Object_var subShape = aShapesOp->GetSubShape (theMainShape,theID);
121     return subShape._retn();
122   }
123 } // end of namespace SMESH