Salome HOME
23586: [EDF] HYDRO: Copy mesh to new geometry
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GEOMGenUtils.cxx
1 // Copyright (C) 2007-2016  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 #include "SMESHGUI.h"
31
32 // SALOME GEOM includes
33 #include <GeometryGUI.h>
34 #include <GEOM_wrap.hxx>
35
36 // SALOME KERNEL includes
37 #include <SALOMEDS_SObject.hxx>
38
39 // IDL includes
40 #include <SALOMEconfig.h>
41 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
42
43 #include <QString>
44
45 namespace SMESH
46 {
47   GEOM::GEOM_Gen_var GetGEOMGen()
48   {
49     static GEOM::GEOM_Gen_var aGEOMGen;
50
51     if(CORBA::is_nil(aGEOMGen)) {
52       if ( GeometryGUI::GetGeomGen()->_is_nil() )
53         GeometryGUI::InitGeomGen();
54       aGEOMGen = GeometryGUI::GetGeomGen();
55     }
56     return aGEOMGen;
57   }
58
59   GEOM::GEOM_Object_var GetShapeOnMeshOrSubMesh(_PTR(SObject) theMeshOrSubmesh,
60                                                 bool*         isMesh)
61   {
62     SALOMEDS_SObject* aMeshOrSubmesh = _CAST(SObject,theMeshOrSubmesh);
63     if(aMeshOrSubmesh) {
64       CORBA::Object_var Obj = aMeshOrSubmesh->GetObject();
65       if ( !CORBA::is_nil( Obj ) ) {
66         SMESH::SMESH_Mesh_var aMesh =
67           SObjectToInterface<SMESH::SMESH_Mesh>( theMeshOrSubmesh );
68         if ( !aMesh->_is_nil() )
69         {
70           if ( isMesh ) *isMesh = true;
71           return aMesh->GetShapeToMesh();
72         }
73         SMESH::SMESH_subMesh_var aSubmesh =
74           SObjectToInterface<SMESH::SMESH_subMesh>( theMeshOrSubmesh );
75         if ( !aSubmesh->_is_nil() )
76         {
77           if ( isMesh ) *isMesh = false;
78           return aSubmesh->GetSubShape();
79         }
80       }
81     }
82     return GEOM::GEOM_Object::_nil();
83   }
84
85   GEOM::GEOM_Object_var GetGeom (_PTR(SObject) theSO)
86   {
87     GEOM::GEOM_Object_var aMeshShape;
88     if (!theSO)
89       return aMeshShape;
90
91     CORBA::Object_var obj = _CAST( SObject,theSO )->GetObject();
92     aMeshShape = GEOM::GEOM_Object::_narrow( obj );
93     if ( !aMeshShape->_is_nil() )
94       return aMeshShape;
95
96     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
97     if (!aStudy)
98       return aMeshShape;
99
100     _PTR(ChildIterator) anIter (aStudy->NewChildIterator(theSO));
101     for ( ; anIter->More(); anIter->Next()) {
102       _PTR(SObject) aSObject = anIter->Value();
103       _PTR(SObject) aRefSOClient;
104
105       if (aSObject->ReferencedObject(aRefSOClient)) {
106         SALOMEDS_SObject* aRefSO = _CAST(SObject,aRefSOClient);
107         aMeshShape = GEOM::GEOM_Object::_narrow(aRefSO->GetObject());
108       } else {
109         SALOMEDS_SObject* aSO = _CAST(SObject,aSObject);
110         aMeshShape = GEOM::GEOM_Object::_narrow(aSO->GetObject());
111       }
112       if ( !aMeshShape->_is_nil() )
113         return aMeshShape;
114     }
115     return aMeshShape;
116   }
117
118   GEOM::GEOM_Object_var GetGeom( Handle(SALOME_InteractiveObject) io )
119   {
120     GEOM::GEOM_Object_var go;
121     _PTR(Study) study = GetActiveStudyDocument();
122     if ( !io.IsNull() && io->hasEntry() && study )
123     {
124       _PTR(SObject) so = study->FindObjectID( io->getEntry() );
125       go = GetGeom( so );
126     }
127     return go._retn();
128   }
129
130   SMESHGUI_EXPORT char* GetGeomName( _PTR(SObject) smeshSO )
131   {
132     if (!smeshSO)
133       return 0;
134
135     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
136     if (!aStudy)
137       return 0;
138
139     _PTR(ChildIterator) anIter (aStudy->NewChildIterator( smeshSO ));
140     for ( ; anIter->More(); anIter->Next()) {
141       _PTR(SObject) aSObject = anIter->Value();
142       _PTR(SObject) aRefSOClient;
143       GEOM::GEOM_Object_var aMeshShape;
144
145       if (aSObject->ReferencedObject(aRefSOClient)) {
146         SALOMEDS_SObject* aRefSO = _CAST(SObject,aRefSOClient);
147         aMeshShape = GEOM::GEOM_Object::_narrow(aRefSO->GetObject());
148         aSObject = aRefSOClient;
149       }
150       else {
151         SALOMEDS_SObject* aSO = _CAST(SObject,aSObject);
152         aMeshShape = GEOM::GEOM_Object::_narrow(aSO->GetObject());
153       }
154
155       if (!aMeshShape->_is_nil())
156       {
157         std::string name = aSObject->GetName();
158         return CORBA::string_dup( name.c_str() );
159       }
160     }
161     return 0;
162   }
163
164   GEOM::GEOM_Object_ptr GetSubShape (GEOM::GEOM_Object_ptr theMainShape,
165                                      long                  theID)
166   {
167     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
168     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
169     if (!aStudy || geomGen->_is_nil())
170       return GEOM::GEOM_Object::_nil();
171     GEOM::GEOM_IShapesOperations_wrap aShapesOp =
172       geomGen->GetIShapesOperations(aStudy->StudyId());
173     if (aShapesOp->_is_nil())
174       return GEOM::GEOM_Object::_nil();
175     GEOM::GEOM_Object_wrap subShape = aShapesOp->GetSubShape (theMainShape,theID);
176     return subShape._retn();
177   }
178
179   //================================================================================
180   /*!
181    * \brief Return entries of sub-mesh geometry and mesh geometry by an IO of assigned
182    *        hypothesis
183    *  \param [in] hypIO - IO of hyp which is a reference SO to a hyp SO
184    *  \param [out] subGeom - found entry of a sub-mesh if any
185    *  \param [out] meshGeom - found entry of a mesh
186    *  \return bool - \c true if any geometry has been found
187    */
188   //================================================================================
189
190   bool GetGeomEntries( Handle(SALOME_InteractiveObject)& hypIO,
191                        QString&                          subGeom,
192                        QString&                          meshGeom )
193   {
194     subGeom.clear();
195     meshGeom.clear();
196     if ( hypIO.IsNull() ) return false;
197
198     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
199     if ( !aStudy ) return false;
200
201     _PTR(SObject) hypSO = aStudy->FindObjectID( hypIO->getEntry() );
202     if ( !hypSO ) return false;
203
204     // Depth() is a number of fathers
205     if ( hypSO->Depth() == 4 ) // hypSO is not a reference to a hyp but a hyp it-self
206     {
207       SMESH::SMESH_Hypothesis_var hyp =
208         SMESH::SObjectToInterface< SMESH::SMESH_Hypothesis >( hypSO );
209       SMESH::SMESH_Mesh_var mesh;
210       GEOM::GEOM_Object_var geom;
211       SMESH::SMESH_Gen_var  gen = SMESHGUI::GetSMESHGUI()->GetSMESHGen();
212       if ( !gen || !gen->GetSoleSubMeshUsingHyp( hyp, mesh.out(), geom.out() ))
213         return false;
214
215       subGeom = toQStr( geom->GetStudyEntry() );
216
217       geom  = mesh->GetShapeToMesh();
218       if ( geom->_is_nil() )
219         return false;
220       meshGeom = toQStr( geom->GetStudyEntry() );
221     }
222     else
223     {
224       _PTR(SObject) appliedSO = hypSO->GetFather(); // "Applied hypotheses" folder
225       if ( !appliedSO ) return false;
226
227       _PTR(SObject) subOrMeshSO = appliedSO->GetFather(); // mesh or sub-mesh SO
228       if ( !subOrMeshSO ) return false;
229
230       bool isMesh;
231       GEOM::GEOM_Object_var geom = GetShapeOnMeshOrSubMesh( subOrMeshSO, &isMesh );
232       if ( geom->_is_nil() )
233         return false;
234
235       if ( isMesh )
236       {
237         meshGeom = toQStr( geom->GetStudyEntry() );
238         return !meshGeom.isEmpty();
239       }
240
241       subGeom = toQStr( geom->GetStudyEntry() );
242
243       _PTR(SObject) subFolderSO = subOrMeshSO->GetFather(); // "SubMeshes on ..." folder
244       if ( !subFolderSO ) return false;
245
246       _PTR(SObject) meshSO = subFolderSO->GetFather(); // mesh SO
247       if ( !meshSO ) return false;
248
249       geom = GetShapeOnMeshOrSubMesh( meshSO );
250       if ( geom->_is_nil() )
251         return false;
252
253       meshGeom = toQStr( geom->GetStudyEntry() );
254     }
255
256     return !meshGeom.isEmpty() && !subGeom.isEmpty();
257   }
258
259
260 } // end of namespace SMESH