Salome HOME
Update of CheckDone
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_GEOMGenUtils.cxx
1 // Copyright (C) 2007-2021  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( GEOM::GEOM_Object_ptr go )
48   {
49     GEOM::GEOM_Gen_ptr gen = GEOM::GEOM_Gen::_nil();
50     if ( !CORBA::is_nil( go ))
51       gen = go->GetGen();
52     return gen;
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_var GetGeom (_PTR(SObject) theSO)
82   {
83     GEOM::GEOM_Object_var aMeshShape;
84     if (!theSO)
85       return aMeshShape;
86
87     CORBA::Object_var obj = _CAST( SObject,theSO )->GetObject();
88     aMeshShape = GEOM::GEOM_Object::_narrow( obj );
89     if ( !aMeshShape->_is_nil() )
90       return aMeshShape;
91
92     _PTR(ChildIterator) anIter (SMESH::getStudy()->NewChildIterator(theSO));
93     for ( ; anIter->More(); anIter->Next()) {
94       _PTR(SObject) aSObject = anIter->Value();
95       _PTR(SObject) aRefSOClient;
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       if ( !aMeshShape->_is_nil() )
105         return aMeshShape;
106     }
107     return aMeshShape;
108   }
109
110   GEOM::GEOM_Object_var GetGeom( Handle(SALOME_InteractiveObject) io )
111   {
112     GEOM::GEOM_Object_var go;
113     if ( !io.IsNull() && io->hasEntry() )
114     {
115       _PTR(SObject) so = SMESH::getStudy()->FindObjectID( io->getEntry() );
116       go = GetGeom( so );
117     }
118     return go._retn();
119   }
120
121   SMESHGUI_EXPORT char* GetGeomName( _PTR(SObject) smeshSO )
122   {
123     if (!smeshSO)
124       return 0;
125
126     _PTR(ChildIterator) anIter (SMESH::getStudy()->NewChildIterator( smeshSO ));
127     for ( ; anIter->More(); anIter->Next()) {
128       _PTR(SObject) aSObject = anIter->Value();
129       _PTR(SObject) aRefSOClient;
130       GEOM::GEOM_Object_var aMeshShape;
131
132       if (aSObject->ReferencedObject(aRefSOClient)) {
133         SALOMEDS_SObject* aRefSO = _CAST(SObject,aRefSOClient);
134         aMeshShape = GEOM::GEOM_Object::_narrow(aRefSO->GetObject());
135         aSObject = aRefSOClient;
136       }
137       else {
138         SALOMEDS_SObject* aSO = _CAST(SObject,aSObject);
139         aMeshShape = GEOM::GEOM_Object::_narrow(aSO->GetObject());
140       }
141
142       if (!aMeshShape->_is_nil())
143       {
144         std::string name = aSObject->GetName();
145         return CORBA::string_dup( name.c_str() );
146       }
147     }
148     return 0;
149   }
150
151   GEOM::GEOM_Object_ptr GetSubShape (GEOM::GEOM_Object_ptr theMainShape,
152                                      long                  theID)
153   {
154     if ( CORBA::is_nil( theMainShape ))
155       return GEOM::GEOM_Object::_nil();
156
157     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen( theMainShape );
158     if (geomGen->_is_nil())
159       return GEOM::GEOM_Object::_nil();
160
161     GEOM::GEOM_IShapesOperations_wrap aShapesOp = geomGen->GetIShapesOperations();
162     if (aShapesOp->_is_nil())
163       return GEOM::GEOM_Object::_nil();
164
165     GEOM::GEOM_Object_wrap subShape = aShapesOp->GetSubShape( theMainShape, theID );
166     return subShape._retn();
167   }
168
169   //================================================================================
170   /*!
171    * \brief Return entries of sub-mesh geometry and mesh geometry by an IO of assigned
172    *        hypothesis
173    *  \param [in] hypIO - IO of hyp which is a reference SO to a hyp SO
174    *  \param [out] subGeom - found entry of a sub-mesh if any
175    *  \param [out] meshGeom - found entry of a mesh
176    *  \return bool - \c true if any geometry has been found
177    */
178   //================================================================================
179
180   bool GetGeomEntries( Handle(SALOME_InteractiveObject)& hypIO,
181                        QString&                          subGeom,
182                        QString&                          meshGeom )
183   {
184     subGeom.clear();
185     meshGeom.clear();
186     if ( hypIO.IsNull() ) return false;
187
188     _PTR(SObject) hypSO = SMESH::getStudy()->FindObjectID( hypIO->getEntry() );
189     if ( !hypSO ) return false;
190
191     // Depth() is a number of fathers
192     if ( hypSO->Depth() == 4 ) // hypSO is not a reference to a hyp but a hyp it-self
193     {
194       SMESH::SMESH_Hypothesis_var hyp =
195         SMESH::SObjectToInterface< SMESH::SMESH_Hypothesis >( hypSO );
196       SMESH::SMESH_Mesh_var mesh;
197       GEOM::GEOM_Object_var geom;
198       SMESH::SMESH_Gen_var  gen = SMESHGUI::GetSMESHGUI()->GetSMESHGen();
199       if ( !gen || !gen->GetSoleSubMeshUsingHyp( hyp, mesh.out(), geom.out() ))
200         return false;
201
202       subGeom = toQStr( geom->GetStudyEntry() );
203
204       geom  = mesh->GetShapeToMesh();
205       if ( geom->_is_nil() )
206         return false;
207       meshGeom = toQStr( geom->GetStudyEntry() );
208     }
209     else
210     {
211       _PTR(SObject) appliedSO = hypSO->GetFather(); // "Applied hypotheses" folder
212       if ( !appliedSO ) return false;
213
214       _PTR(SObject) subOrMeshSO = appliedSO->GetFather(); // mesh or sub-mesh SO
215       if ( !subOrMeshSO ) return false;
216
217       bool isMesh;
218       GEOM::GEOM_Object_var geom = GetShapeOnMeshOrSubMesh( subOrMeshSO, &isMesh );
219       if ( geom->_is_nil() )
220         return false;
221
222       if ( isMesh )
223       {
224         meshGeom = toQStr( geom->GetStudyEntry() );
225         return !meshGeom.isEmpty();
226       }
227
228       subGeom = toQStr( geom->GetStudyEntry() );
229
230       _PTR(SObject) subFolderSO = subOrMeshSO->GetFather(); // "SubMeshes on ..." folder
231       if ( !subFolderSO ) return false;
232
233       _PTR(SObject) meshSO = subFolderSO->GetFather(); // mesh SO
234       if ( !meshSO ) return false;
235
236       geom = GetShapeOnMeshOrSubMesh( meshSO );
237       if ( geom->_is_nil() )
238         return false;
239
240       meshGeom = toQStr( geom->GetStudyEntry() );
241     }
242
243     return !meshGeom.isEmpty() && !subGeom.isEmpty();
244   }
245
246
247 } // end of namespace SMESH