Salome HOME
22526: SMESH 2864 - Projection and Extrusion
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GEOMGenUtils.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 #include <GEOM_wrap.hxx>
34
35 // SALOME KERNEL includes
36 #include <SALOMEDS_SObject.hxx>
37
38 // IDL includes
39 #include <SALOMEconfig.h>
40 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
41
42 namespace SMESH
43 {
44   GEOM::GEOM_Gen_var GetGEOMGen()
45   {
46     static GEOM::GEOM_Gen_var aGEOMGen;
47
48     if(CORBA::is_nil(aGEOMGen)) {
49       if ( GeometryGUI::GetGeomGen()->_is_nil() )
50         GeometryGUI::InitGeomGen();
51       aGEOMGen = GeometryGUI::GetGeomGen();
52     }
53     return aGEOMGen;
54   }
55
56   GEOM::GEOM_Object_var GetShapeOnMeshOrSubMesh(_PTR(SObject) theMeshOrSubmesh,
57                                                 bool*         isMesh)
58   {
59     SALOMEDS_SObject* aMeshOrSubmesh = _CAST(SObject,theMeshOrSubmesh);
60     if(aMeshOrSubmesh) {
61       CORBA::Object_var Obj = aMeshOrSubmesh->GetObject();
62       if ( !CORBA::is_nil( Obj ) ) {
63         SMESH::SMESH_Mesh_var aMesh =
64           SObjectToInterface<SMESH::SMESH_Mesh>( theMeshOrSubmesh );
65         if ( !aMesh->_is_nil() )
66         {
67           if ( isMesh ) *isMesh = true;
68           return aMesh->GetShapeToMesh();
69         }
70         SMESH::SMESH_subMesh_var aSubmesh =
71           SObjectToInterface<SMESH::SMESH_subMesh>( theMeshOrSubmesh );
72         if ( !aSubmesh->_is_nil() )
73         {
74           if ( isMesh ) *isMesh = false;
75           return aSubmesh->GetSubShape();
76         }
77       }
78     }
79     return GEOM::GEOM_Object::_nil();
80   }
81
82   GEOM::GEOM_Object_ptr GetGeom (_PTR(SObject) theSO)
83   {
84     if (!theSO)
85       return GEOM::GEOM_Object::_nil();
86
87     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
88     if (!aStudy)
89       return GEOM::GEOM_Object::_nil();
90
91     _PTR(ChildIterator) anIter (aStudy->NewChildIterator(theSO));
92     for ( ; anIter->More(); anIter->Next()) {
93       _PTR(SObject) aSObject = anIter->Value();
94       _PTR(SObject) aRefSOClient;
95       GEOM::GEOM_Object_var aMeshShape;
96
97       if (aSObject->ReferencedObject(aRefSOClient)) {
98         SALOMEDS_SObject* aRefSO = _CAST(SObject,aRefSOClient);
99         aMeshShape = GEOM::GEOM_Object::_narrow(aRefSO->GetObject());
100       } else {
101         SALOMEDS_SObject* aSO = _CAST(SObject,aSObject);
102         aMeshShape = GEOM::GEOM_Object::_narrow(aSO->GetObject());
103       }
104
105       if (!aMeshShape->_is_nil())
106         return aMeshShape._retn();
107     }
108     return GEOM::GEOM_Object::_nil();
109   }
110
111   SMESHGUI_EXPORT char* GetGeomName( _PTR(SObject) smeshSO )
112   {
113     if (!smeshSO)
114       return 0;
115
116     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
117     if (!aStudy)
118       return 0;
119
120     _PTR(ChildIterator) anIter (aStudy->NewChildIterator( smeshSO ));
121     for ( ; anIter->More(); anIter->Next()) {
122       _PTR(SObject) aSObject = anIter->Value();
123       _PTR(SObject) aRefSOClient;
124       GEOM::GEOM_Object_var aMeshShape;
125
126       if (aSObject->ReferencedObject(aRefSOClient)) {
127         SALOMEDS_SObject* aRefSO = _CAST(SObject,aRefSOClient);
128         aMeshShape = GEOM::GEOM_Object::_narrow(aRefSO->GetObject());
129         aSObject = aRefSOClient;
130       }
131       else {
132         SALOMEDS_SObject* aSO = _CAST(SObject,aSObject);
133         aMeshShape = GEOM::GEOM_Object::_narrow(aSO->GetObject());
134       }
135
136       if (!aMeshShape->_is_nil())
137       {
138         std::string name = aSObject->GetName();
139         return CORBA::string_dup( name.c_str() );
140       }
141     }
142     return 0;
143   }
144
145   GEOM::GEOM_Object_ptr GetSubShape (GEOM::GEOM_Object_ptr theMainShape,
146                                      long                  theID)
147   {
148     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
149     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
150     if (!aStudy || geomGen->_is_nil())
151       return GEOM::GEOM_Object::_nil();
152     GEOM::GEOM_IShapesOperations_wrap aShapesOp =
153       geomGen->GetIShapesOperations(aStudy->StudyId());
154     if (aShapesOp->_is_nil())
155       return GEOM::GEOM_Object::_nil();
156     GEOM::GEOM_Object_wrap subShape = aShapesOp->GetSubShape (theMainShape,theID);
157     return subShape._retn();
158   }
159 } // end of namespace SMESH