Salome HOME
0021096: EDF 1729 SMESH: Create a Projection 1D-2D algorithm
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i_1.cxx
1 // Copyright (C) 2007-2011  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 //  File      : SMESH_Gen_i_1.cxx
23 //  Created   : Thu Oct 21 17:24:06 2004
24 //  Author    : Edward AGAPOV (eap)
25 //  Module    : SMESH
26 //  $Header   : $
27
28 #include "SMESH_Gen_i.hxx"
29
30 #include "SMESH_Mesh_i.hxx"
31 #include "SMESH_Hypothesis_i.hxx"
32 #include "SMESH_Algo_i.hxx"
33 #include "SMESH_Group_i.hxx"
34 #include "SMESH_subMesh_i.hxx"
35
36 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
37
38 #include "utilities.h"
39 #include "Utils_ExceptHandlers.hxx"
40
41 #include <TCollection_AsciiString.hxx>
42
43 #ifdef _DEBUG_
44 static int MYDEBUG = 0;
45 static int VARIABLE_DEBUG = 0;
46 #else
47 static int MYDEBUG = 0;
48 static int VARIABLE_DEBUG = 0;
49 #endif
50
51 //=============================================================================
52 /*!
53  *  Get...Tag [ static ]
54  *
55  *  Methods which determine SMESH data model structure
56  */
57 //=============================================================================
58
59 long SMESH_Gen_i::GetHypothesisRootTag()
60 {
61   return SMESH::Tag_HypothesisRoot;
62 }
63
64 long SMESH_Gen_i::GetAlgorithmsRootTag()
65 {
66   return SMESH::Tag_AlgorithmsRoot;
67 }
68
69 long SMESH_Gen_i::GetRefOnShapeTag()
70 {
71   return SMESH::Tag_RefOnShape;
72 }
73
74 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
75 {
76   return SMESH::Tag_RefOnAppliedHypothesis;
77 }
78
79 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
80 {
81   return SMESH::Tag_RefOnAppliedAlgorithms;
82 }
83
84 long SMESH_Gen_i::GetSubMeshOnVertexTag()
85 {
86   return SMESH::Tag_SubMeshOnVertex;
87 }
88
89 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
90 {
91   return SMESH::Tag_SubMeshOnEdge;
92 }
93
94 long SMESH_Gen_i::GetSubMeshOnFaceTag()
95 {
96   return SMESH::Tag_SubMeshOnFace;
97 }
98
99 long SMESH_Gen_i::GetSubMeshOnSolidTag()
100 {
101   return SMESH::Tag_SubMeshOnSolid;
102 }
103
104 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
105 {
106   return SMESH::Tag_SubMeshOnCompound;
107 }
108
109 long SMESH_Gen_i::GetSubMeshOnWireTag()
110 {
111   return SMESH::Tag_SubMeshOnWire;
112 }
113
114 long SMESH_Gen_i::GetSubMeshOnShellTag()
115 {
116   return SMESH::Tag_SubMeshOnShell;
117 }
118
119 long SMESH_Gen_i::GetNodeGroupsTag()
120 {
121   return SMESH::Tag_NodeGroups;
122 }
123
124 long SMESH_Gen_i::GetEdgeGroupsTag()
125 {
126   return SMESH::Tag_EdgeGroups;
127 }
128
129 long SMESH_Gen_i::GetFaceGroupsTag()
130 {
131   return SMESH::Tag_FaceGroups;
132 }
133
134 long SMESH_Gen_i::GetVolumeGroupsTag()
135 {
136   return SMESH::Tag_VolumeGroups;
137 }
138
139 long SMESH_Gen_i::Get0DElementsGroupsTag()
140 {
141   return SMESH::Tag_0DElementsGroups;
142 }
143
144 //=============================================================================
145 /*!
146  *  SMESH_Gen_i::CanPublishInStudy
147  *
148  *  Returns true if object can be published in the study
149  */
150 //=============================================================================
151
152 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
153 {
154   if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
155   if(CORBA::is_nil(myCurrentStudy))
156     return false;
157   
158   SMESH::SMESH_Mesh_var aMesh       = SMESH::SMESH_Mesh::_narrow(theIOR);
159   if( !aMesh->_is_nil() )
160     return true;
161
162   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
163   if( !aSubMesh->_is_nil() )
164     return true;
165
166   SMESH::SMESH_Hypothesis_var aHyp  = SMESH::SMESH_Hypothesis::_narrow(theIOR);
167   if( !aHyp->_is_nil() )
168     return true;
169
170   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
171   if( !aGroup->_is_nil() )
172     return true;
173
174   if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
175   return false;
176 }
177
178 //=======================================================================
179 //function : ObjectToSObject
180 //purpose  : 
181 //=======================================================================
182
183 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
184                                                    CORBA::Object_ptr   theObject)
185 {
186   SALOMEDS::SObject_var aSO;
187   if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
188   {
189     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
190     aSO = theStudy->FindObjectIOR( objStr.in() );
191   }
192   return aSO._retn();
193 }
194
195 //=======================================================================
196 //function : objectToServant
197 //purpose  : 
198 //=======================================================================
199
200 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
201 {
202   return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
203 }
204
205 //=======================================================================
206 //function : ShapeToGeomObject
207 //purpose  : 
208 //=======================================================================
209
210 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
211 {
212   GEOM::GEOM_Object_var aShapeObj;
213   if ( !theShape.IsNull() ) {
214     GEOM_Client* aClient = GetShapeReader();
215     TCollection_AsciiString IOR;
216     if ( aClient && aClient->Find( theShape, IOR ))
217     {
218       CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
219       aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
220     }
221   }
222   return aShapeObj._retn();
223 }
224
225 //=======================================================================
226 //function : GeomObjectToShape
227 //purpose  : 
228 //=======================================================================
229
230 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
231 {
232   TopoDS_Shape S;
233   if ( !theGeomObject->_is_nil() ) {
234     GEOM_Client* aClient = GetShapeReader();
235     GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
236     if ( aClient && !aGeomEngine->_is_nil () )
237       S = aClient->GetShape( aGeomEngine, theGeomObject );
238   }
239   return S;
240 }
241
242 //=======================================================================
243 //function : publish
244 //purpose  : 
245 //=======================================================================
246
247 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr   theStudy,
248                                      CORBA::Object_ptr     theIOR,
249                                      SALOMEDS::SObject_ptr theFatherObject,
250                                      const int             theTag = 0,
251                                      const char*           thePixMap = 0,
252                                      const bool            theSelectable = true)
253 {
254   SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
255   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
256   if ( SO->_is_nil() ) {
257     if ( theTag == 0 )
258       SO = aStudyBuilder->NewObject( theFatherObject );
259     else if ( !theFatherObject->FindSubObject( theTag, SO ))
260       SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
261   }
262
263   SALOMEDS::GenericAttribute_var anAttr;
264   if ( !CORBA::is_nil( theIOR )) {
265     anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
266     CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
267     SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
268   }
269   if ( thePixMap ) {
270     anAttr  = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
271     SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
272     pm->SetPixMap( thePixMap );
273   }
274   if ( !theSelectable ) {
275     anAttr   = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
276     SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
277   }
278   return SO._retn();
279 }
280
281 //=======================================================================
282 //function : setName
283 //purpose  : 
284 //=======================================================================
285
286 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
287                           const char*           theName,
288                           const char*           theDefaultName)
289 {
290   if ( !theSObject->_is_nil() ) {
291     SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
292     SALOMEDS::GenericAttribute_var anAttr =
293       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
294     SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
295     if ( theName && strlen( theName ) != 0 )
296       aNameAttr->SetValue( theName );
297     else {
298       CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
299       if ( strlen( curName ) == 0 ) {
300         TCollection_AsciiString aName( (char*) theDefaultName );
301         aName += TCollection_AsciiString("_") + TCollection_AsciiString( theSObject->Tag() );
302         aNameAttr->SetValue( aName.ToCString() );
303       }
304     }
305   }
306 }
307
308 //=======================================================================
309 //function : SetPixMap
310 //purpose  : 
311 //=======================================================================
312
313 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
314                             const char*           thePixMap)
315 {
316   if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
317   {
318     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
319     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
320     SALOMEDS::GenericAttribute_var anAttr =
321       aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
322     SALOMEDS::AttributePixMap_var aPMAttr = SALOMEDS::AttributePixMap::_narrow( anAttr );
323     aPMAttr->SetPixMap( thePixMap );
324   }
325 }
326
327 //=======================================================================
328 //function : addReference
329 //purpose  : 
330 //=======================================================================
331
332 static void addReference (SALOMEDS::Study_ptr   theStudy,
333                           SALOMEDS::SObject_ptr theSObject,
334                           CORBA::Object_ptr     theToObject,
335                           int                   theTag = 0)
336 {
337   SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
338   if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
339     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
340     SALOMEDS::SObject_var aReferenceSO;
341     if ( !theTag ) {
342       // check if the reference to theToObject already exists
343       // and find a free label for the reference object
344       bool isReferred = false;
345       int tag = 1;
346       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
347       for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
348         if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
349           if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
350             isReferred = true;
351         }
352         else if ( !theTag ) {
353           SALOMEDS::GenericAttribute_var anAttr;
354           if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
355             theTag = tag;
356         }
357       }
358       if ( isReferred )
359         return;
360       if ( !theTag )
361         theTag = tag;
362     }
363     if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
364       aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
365     aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
366   }
367 }
368
369 //=============================================================================
370 /*!
371  *  SMESH_Gen_i::PublishInStudy
372  *
373  *  Publish object in the study
374  */
375 //=============================================================================
376
377 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr   theStudy,
378                                                   SALOMEDS::SObject_ptr theSObject,
379                                                   CORBA::Object_ptr     theIOR,
380                                                   const char*           theName)
381      throw (SALOME::SALOME_Exception)
382 {
383   Unexpect aCatch(SALOME_SalomeException);
384   SALOMEDS::SObject_var aSO;
385   if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
386     return aSO._retn();
387   if(MYDEBUG) MESSAGE("PublishInStudy");
388
389   // Publishing a mesh
390   SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
391   if( !aMesh->_is_nil() )
392     aSO = PublishMesh( theStudy, aMesh, theName );
393
394   // Publishing a sub-mesh
395   SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
396   if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
397     GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
398     aMesh = aSubMesh->GetFather();
399     aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
400   }
401
402   // Publishing a hypothesis or algorithm
403   SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
404   if ( aSO->_is_nil() && !aHyp->_is_nil() )
405     aSO = PublishHypothesis( theStudy, aHyp );
406
407   // Publishing a group
408   SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
409   if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
410     GEOM::GEOM_Object_var aShapeObject;
411     aMesh = aGroup->GetMesh();
412     aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
413   }
414   if(MYDEBUG) MESSAGE("PublishInStudy_END");
415
416   return aSO._retn();
417 }
418
419 //=======================================================================
420 //function : PublishComponent
421 //purpose  : 
422 //=======================================================================
423
424 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
425 {
426   if ( CORBA::is_nil( theStudy ))
427     return SALOMEDS::SComponent::_nil();
428   if(MYDEBUG) MESSAGE("PublishComponent");
429
430   SALOMEDS::SComponent_var father =
431     SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
432   if ( !CORBA::is_nil( father ) )
433     return father._retn();
434
435   SALOME_ModuleCatalog::ModuleCatalog_var aCat =
436     SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
437   if ( CORBA::is_nil( aCat ) )
438     return father._retn();
439
440   SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
441   if ( CORBA::is_nil( aComp ) )
442     return father._retn();
443
444   SALOMEDS::StudyBuilder_var     aStudyBuilder = theStudy->NewBuilder(); 
445   SALOMEDS::GenericAttribute_var anAttr;
446   SALOMEDS::AttributePixMap_var  aPixmap;
447
448   father  = aStudyBuilder->NewComponent( ComponentDataType() );
449   aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
450   anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
451   aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
452   aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
453   SetName( father, aComp->componentusername(), "MESH" );
454   if(MYDEBUG) MESSAGE("PublishComponent--END");
455
456   return father._retn();
457 }
458
459 //=============================================================================
460 /*!
461  *  findMaxChildTag [ static internal ]
462  *
463  *  Finds maximum child tag for the given object
464  */
465 //=============================================================================
466
467 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
468 {
469   long aTag = 0;
470   if ( !theSObject->_is_nil() ) {
471     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
472     if ( !aStudy->_is_nil() ) {
473       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
474       for ( ; anIter->More(); anIter->Next() ) {
475         long nTag = anIter->Value()->Tag();
476         if ( nTag > aTag )
477           aTag = nTag;
478       }
479     }
480   }
481   return aTag;
482 }
483
484 //=======================================================================
485 //function : PublishMesh
486 //purpose  : 
487 //=======================================================================
488
489 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr   theStudy,
490                                                 SMESH::SMESH_Mesh_ptr theMesh,
491                                                 const char*           theName)
492 {
493   if ( CORBA::is_nil( theStudy ) ||
494        CORBA::is_nil( theMesh ))
495     return SALOMEDS::SComponent::_nil();
496   if(MYDEBUG) MESSAGE("PublishMesh--IN");
497
498   // find or publish a mesh
499
500   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
501   if ( aMeshSO->_is_nil() )
502   {
503     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
504     if ( father->_is_nil() )
505       return aMeshSO._retn();
506
507     // Find correct free tag
508     long aTag = findMaxChildTag( father.in() );
509     if ( aTag <= GetAlgorithmsRootTag() )
510       aTag = GetAlgorithmsRootTag() + 1;
511     else
512       aTag++;
513
514     aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
515     if ( aMeshSO->_is_nil() )
516       return aMeshSO._retn();
517   }
518   SetName( aMeshSO, theName, "Mesh" );
519
520   // Add shape reference
521
522   GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
523   if ( !CORBA::is_nil( aShapeObject )) {
524     addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
525
526     // Publish global hypotheses
527
528     SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
529     for ( int i = 0; i < hypList->length(); i++ ) {
530       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
531       PublishHypothesis( theStudy, aHyp );
532       AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
533     }
534   }
535
536   // Publish submeshes
537
538   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
539   if ( !mesh_i )
540     return aMeshSO._retn();
541   map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
542   map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
543   for ( ; subIt != subMap.end(); subIt++ ) {
544     SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
545     if ( !CORBA::is_nil( aSubMesh )) {
546       aShapeObject = aSubMesh->GetSubShape();
547       PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
548     }
549   }
550
551   // Publish groups
552   const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
553   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
554   for ( ; it != grMap.end(); it++ )
555   {
556     SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
557     if ( !aGroup->_is_nil() ) {
558       GEOM::GEOM_Object_var  aShapeObj;
559       SMESH::SMESH_GroupOnGeom_var aGeomGroup =
560         SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
561       if ( !aGeomGroup->_is_nil() )
562         aShapeObj = aGeomGroup->GetShape();
563       PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
564     }
565   }
566
567   if(MYDEBUG) MESSAGE("PublishMesh_END");
568   return aMeshSO._retn();
569 }
570
571 //=======================================================================
572 //function : PublishSubMesh
573 //purpose  : 
574 //=======================================================================
575
576 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr      theStudy,
577                                                    SMESH::SMESH_Mesh_ptr    theMesh,
578                                                    SMESH::SMESH_subMesh_ptr theSubMesh,
579                                                    GEOM::GEOM_Object_ptr    theShapeObject,
580                                                    const char*              theName)
581 {
582   if (theStudy->_is_nil() || theMesh->_is_nil() ||
583       theSubMesh->_is_nil() || theShapeObject->_is_nil() )
584     return SALOMEDS::SObject::_nil();
585
586   SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
587   if ( aSubMeshSO->_is_nil() )
588   {
589     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
590     if ( aMeshSO->_is_nil() ) {
591       aMeshSO = PublishMesh( theStudy, theMesh );
592       if ( aMeshSO->_is_nil())
593         return SALOMEDS::SObject::_nil();
594     }
595     // Find submesh sub-tree tag
596     long aRootTag;
597     const char* aRootName = "";
598     switch ( theShapeObject->GetShapeType() ) {
599     case GEOM::VERTEX:
600       aRootTag  = GetSubMeshOnVertexTag();
601       aRootName = "SubMeshes on Vertex";
602       break;
603     case GEOM::EDGE:
604       aRootTag  = GetSubMeshOnEdgeTag();
605       aRootName = "SubMeshes on Edge";
606       break;
607     case GEOM::WIRE:
608       aRootTag  = GetSubMeshOnWireTag();
609       aRootName = "SubMeshes on Wire";
610       break;
611     case GEOM::FACE:
612       aRootTag  = GetSubMeshOnFaceTag();
613       aRootName = "SubMeshes on Face";    
614       break;
615     case GEOM::SHELL:
616       aRootTag  = GetSubMeshOnShellTag();
617       aRootName = "SubMeshes on Shell";   
618       break;
619     case GEOM::SOLID:
620       aRootTag  = GetSubMeshOnSolidTag();
621       aRootName = "SubMeshes on Solid";
622       break;
623     default:
624       aRootTag  = GetSubMeshOnCompoundTag();
625       aRootName = "SubMeshes on Compound";
626       break;
627     }
628
629     // Find or create submesh root
630     SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
631                                              aMeshSO, aRootTag, 0, false );
632     SetName( aRootSO, aRootName );
633
634     // Add new submesh to corresponding sub-tree
635     SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
636     const int isEmpty = ( elemTypes->length() == 0 );
637     const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
638     aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
639     if ( aSubMeshSO->_is_nil() )
640       return aSubMeshSO._retn();
641   }
642   SetName( aSubMeshSO, theName, "SubMesh" );
643
644   // Add reference to theShapeObject
645
646   addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
647
648   // Publish hypothesis
649
650   SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
651   if ( hypList )
652     for ( int i = 0; i < hypList->length(); i++ ) {
653       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
654       PublishHypothesis( theStudy, aHyp );
655       AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
656     }
657
658   return aSubMeshSO._retn();
659 }
660
661 //=======================================================================
662 //function : PublishGroup
663 //purpose  : 
664 //=======================================================================
665
666 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr    theStudy,
667                                                  SMESH::SMESH_Mesh_ptr  theMesh,
668                                                  SMESH::SMESH_GroupBase_ptr theGroup,
669                                                  GEOM::GEOM_Object_ptr  theShapeObject,
670                                                  const char*            theName)
671 {
672   if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
673     return SALOMEDS::SObject::_nil();
674
675   SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
676   if ( aGroupSO->_is_nil() )
677   {
678     SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
679     if ( aMeshSO->_is_nil() ) {
680       aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
681       if ( aMeshSO->_is_nil())
682         return SALOMEDS::SObject::_nil();
683     }
684     int aType = (int)theGroup->GetType();
685     const char* aRootNames[] = {
686       "Compound Groups", "Groups of Nodes", "Groups of Edges",
687       "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements" };
688
689     // Currently, groups with heterogenous content are not supported
690     if ( aType != SMESH::ALL ) {
691       long aRootTag = GetNodeGroupsTag() + aType - 1;
692
693       // Find or create groups root
694       SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
695                                                aMeshSO, aRootTag, 0, false );
696       if ( aType < 6 )
697         SetName( aRootSO, aRootNames[aType] );
698
699       // Add new group to corresponding sub-tree
700       SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
701       const int isEmpty = ( elemTypes->length() == 0 );
702       std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
703       if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
704         pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
705       aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
706     }
707     if ( aGroupSO->_is_nil() )
708       return aGroupSO._retn();
709   }
710
711   SetName( aGroupSO, theName, "Group" );
712
713   //Add reference to geometry
714   if ( !theShapeObject->_is_nil() )
715     addReference( theStudy, aGroupSO, theShapeObject, 1 );
716
717   return aGroupSO._retn();
718 }
719
720 //=======================================================================
721 //function : PublishHypothesis
722 //purpose  : 
723 //=======================================================================
724
725 SALOMEDS::SObject_ptr
726   SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr         theStudy,
727                                   SMESH::SMESH_Hypothesis_ptr theHyp,
728                                   const char*                 theName)
729 {
730   if(MYDEBUG) MESSAGE("PublishHypothesis")
731   if (theStudy->_is_nil() || theHyp->_is_nil())
732     return SALOMEDS::SObject::_nil();
733
734   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
735   if ( aHypSO->_is_nil() )
736   {
737     SALOMEDS::SComponent_var father = PublishComponent( theStudy );
738     if ( father->_is_nil() )
739       return aHypSO._retn();
740
741     //Find or Create Hypothesis root
742     bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
743     int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
744     SALOMEDS::SObject_var aRootSO =
745       publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
746                isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
747     SetName( aRootSO, isAlgo ?  "Algorithms" : "Hypotheses" );
748
749     // Add New Hypothesis
750     string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
751     aPmName += theHyp->GetName();
752     // prepend plugin name to pixmap name
753     string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
754     if ( pluginName != "StdMeshers" )
755       aPmName = pluginName + "::" + aPmName;
756     aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
757   }
758
759   if ( !aHypSO->_is_nil() ) {
760     CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
761     SetName( aHypSO, theName, aHypName );
762   }
763
764   if(MYDEBUG) MESSAGE("PublishHypothesis--END")
765   return aHypSO._retn();
766 }
767
768 //=======================================================================
769 //function : GetMeshOrSubmeshByShape
770 //purpose  : 
771 //=======================================================================
772
773 SALOMEDS::SObject_ptr
774   SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr   theStudy,
775                                         SMESH::SMESH_Mesh_ptr theMesh,
776                                         GEOM::GEOM_Object_ptr theShape)
777 {
778   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
779   SALOMEDS::SObject_var aMeshOrSubMesh;
780   if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
781     return aMeshOrSubMesh._retn();
782   
783   TopoDS_Shape aShape;
784   if(theMesh->HasShapeToMesh())
785     aShape = GeomObjectToShape( theShape );
786   else
787     aShape = SMESH_Mesh::PseudoShape();
788
789   SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
790
791   if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
792     SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
793     if ( aShape.IsSame( meshDS->ShapeToMesh() ))
794       aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
795     else {
796       int shapeID = meshDS->ShapeToIndex( aShape );
797       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
798       if ( !aSubMesh->_is_nil() )
799         aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
800     }
801   }
802   if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
803   return aMeshOrSubMesh._retn();
804 }
805
806 //=======================================================================
807 //function : AddHypothesisToShape
808 //purpose  : 
809 //=======================================================================
810
811 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr         theStudy,
812                                        SMESH::SMESH_Mesh_ptr       theMesh,
813                                        GEOM::GEOM_Object_ptr       theShape,
814                                        SMESH::SMESH_Hypothesis_ptr theHyp)
815 {
816   if(MYDEBUG) MESSAGE("AddHypothesisToShape")
817   if (theStudy->_is_nil() || theMesh->_is_nil() ||
818       theHyp->_is_nil() || (theShape->_is_nil()
819                             && theMesh->HasShapeToMesh()) )
820     return false;
821
822   SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
823   if ( aMeshSO->_is_nil() )
824     aMeshSO = PublishMesh( theStudy, theMesh );
825   SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
826   if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
827     return false;
828
829   // Find a mesh or submesh refering to theShape
830   SALOMEDS::SObject_var aMeshOrSubMesh =
831     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
832   if ( aMeshOrSubMesh->_is_nil() )
833   {
834     // publish submesh
835     TopoDS_Shape aShape = GeomObjectToShape( theShape );
836     SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
837     if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
838       SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
839       int shapeID = meshDS->ShapeToIndex( aShape );
840       SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
841       aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
842     }
843     if ( aMeshOrSubMesh->_is_nil() )
844       return false;
845   }
846
847   //Find or Create Applied Hypothesis root
848   bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
849   SALOMEDS::SObject_var AHR =
850     publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
851              aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
852              aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
853   SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
854   if ( AHR->_is_nil() )
855     return false;
856
857   addReference( theStudy, AHR, theHyp );
858   if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
859   return true;
860 }
861
862 //=======================================================================
863 //function : RemoveHypothesisFromShape
864 //purpose  : 
865 //=======================================================================
866
867 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr         theStudy,
868                                             SMESH::SMESH_Mesh_ptr       theMesh,
869                                             GEOM::GEOM_Object_ptr       theShape,
870                                             SMESH::SMESH_Hypothesis_ptr theHyp)
871 {
872   if (theStudy->_is_nil() || theMesh->_is_nil() ||
873       theHyp->_is_nil() || (theShape->_is_nil()
874                             && theMesh->HasShapeToMesh()))
875     return false;
876
877   SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
878   if ( aHypSO->_is_nil() )
879     return false;
880
881   // Find a mesh or submesh refering to theShape
882   SALOMEDS::SObject_var aMeshOrSubMesh =
883     GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
884   if ( aMeshOrSubMesh->_is_nil() )
885     return false;
886
887   // Find and remove a reference to aHypSO
888   SALOMEDS::SObject_var aRef, anObj;
889   CORBA::String_var     anID = CORBA::string_dup( aHypSO->GetID() );
890   SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
891   for ( it->InitEx( true ); it->More(); it->Next() ) {
892     anObj = it->Value();
893     if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
894       theStudy->NewBuilder()->RemoveObject( anObj );
895       break;
896     }
897   }
898   return true;
899 }
900
901 //=======================================================================
902 //function : UpdateParameters
903 //purpose  : 
904 //=======================================================================
905 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
906 {
907
908   if(VARIABLE_DEBUG)
909     cout<<"UpdateParameters : "<<theParameters<<endl;
910   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
911   if(aStudy->_is_nil() || CORBA::is_nil(theObject)) 
912     return;
913
914   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
915   if(aSObj->_is_nil())  
916     return;
917
918   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
919
920   SALOMEDS::GenericAttribute_var aFindAttr;
921   bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
922   if(VARIABLE_DEBUG)
923     cout<<"Find Attribute "<<hasAttr<<endl;
924
925   SALOMEDS::GenericAttribute_var anAttr;
926   anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
927   SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
928
929   CORBA::String_var oldparVar = aStringAttr->Value();
930   CORBA::String_var inpparVar = ParseParameters(theParameters);
931   TCollection_AsciiString aNewParams;
932   TCollection_AsciiString aOldParameters(oldparVar.inout());
933   TCollection_AsciiString anInputParams(inpparVar.inout());
934   if(!hasAttr)
935     aNewParams = anInputParams;
936   else 
937     {
938       int pos = aOldParameters.SearchFromEnd("|");
939       if(pos==-1) pos = 0;
940       TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
941       TCollection_AsciiString previousParam(previousParamFull);
942       TCollection_AsciiString theRepet("1");
943       pos = previousParam.SearchFromEnd(";*=");
944       if(pos >= 0)
945         {
946           theRepet = previousParam.Split(pos+2);
947           pos = pos-1;
948           if(pos==-1) pos = 0;
949           previousParam.Split(pos);
950         }
951       if(previousParam == anInputParams)
952         {
953           theRepet = theRepet.IntegerValue()+1;
954           aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
955         }
956       else
957         {
958           aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
959         }
960     }
961
962   if(VARIABLE_DEBUG)
963   {
964     cout<<"Input Parameters : "<<anInputParams<<endl;
965     cout<<"Old Parameters : "<<aOldParameters<<endl;
966     cout<<"New Parameters : "<<aNewParams<<endl;
967   }
968
969   aStringAttr->SetValue( aNewParams.ToCString() );
970 }
971
972 //=======================================================================
973 //function : ParseParameters
974 //purpose  : 
975 //=======================================================================
976 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
977 {
978   //const char* aParameters = theParameters;
979 //   const char* aParameters = CORBA::string_dup(theParameters);
980   TCollection_AsciiString anInputParams;
981   SALOMEDS::Study_var aStudy = GetCurrentStudy();
982   if( !aStudy->_is_nil() ) {
983 //     SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
984 //     for(int j=0;j<aSections->length();j++) {
985 //       SALOMEDS::ListOfStrings aVars= aSections[j];
986 //       for(int i=0;i<aVars.length();i++ ) {
987 //         anInputParams += aStudy->IsVariable(aVars[i].in()) ? 
988 //           TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
989 //         if(i != aVars.length()-1)
990 //           anInputParams+=":";
991 //       }
992 //       if(j!=aSections->length()-1)
993 //         anInputParams+="|";
994 //     }
995     TCollection_AsciiString paramStr( theParameters );
996     static TCollection_AsciiString separators(":|");
997     int beg = 0, end;
998     char sep, *pParams = (char*)paramStr.ToCString();
999     while ( beg < paramStr.Length() )
1000     {
1001       end = beg-1;
1002       while ( ++end < paramStr.Length() )
1003         if ( pParams[end] == ':' || pParams[end] == '|')
1004           break;
1005       if ( end < paramStr.Length())
1006       {
1007         sep = pParams[end];
1008         pParams[end] = '\0';
1009       }
1010       if ( aStudy->IsVariable( pParams+beg ))
1011         anInputParams += pParams+beg;
1012       if ( end < paramStr.Length() )
1013         anInputParams += sep;
1014       else
1015         break;
1016       beg = end + 1;
1017     }
1018   }
1019   return CORBA::string_dup(anInputParams.ToCString());
1020 }
1021
1022 //=======================================================================
1023 //function : GetParameters
1024 //purpose  : 
1025 //=======================================================================
1026 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1027 {
1028   TCollection_AsciiString aResult;
1029
1030   SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
1031   SALOMEDS::SObject_var aSObj =  ObjectToSObject(aStudy,theObject);
1032
1033   if(!aStudy->_is_nil() && 
1034      !CORBA::is_nil(theObject) && 
1035      !aSObj->_is_nil()){
1036     
1037     SALOMEDS::GenericAttribute_var anAttr;
1038     if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
1039       aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
1040     }
1041   }
1042   
1043   return CORBA::string_dup( aResult.ToCString() );
1044 }