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