1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_Gen_i_1.cxx
23 // Created : Thu Oct 21 17:24:06 2004
24 // Author : Edward AGAPOV (eap)
28 #include "SMESH_Gen_i.hxx"
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"
36 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
38 #include "utilities.h"
39 #include "Utils_ExceptHandlers.hxx"
41 #include <TCollection_AsciiString.hxx>
44 static int MYDEBUG = 0;
45 static int VARIABLE_DEBUG = 0;
47 static int MYDEBUG = 0;
48 static int VARIABLE_DEBUG = 0;
51 //=============================================================================
53 * Get...Tag [ static ]
55 * Methods which determine SMESH data model structure
57 //=============================================================================
59 long SMESH_Gen_i::GetHypothesisRootTag()
61 return SMESH::Tag_HypothesisRoot;
64 long SMESH_Gen_i::GetAlgorithmsRootTag()
66 return SMESH::Tag_AlgorithmsRoot;
69 long SMESH_Gen_i::GetRefOnShapeTag()
71 return SMESH::Tag_RefOnShape;
74 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
76 return SMESH::Tag_RefOnAppliedHypothesis;
79 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
81 return SMESH::Tag_RefOnAppliedAlgorithms;
84 long SMESH_Gen_i::GetSubMeshOnVertexTag()
86 return SMESH::Tag_SubMeshOnVertex;
89 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
91 return SMESH::Tag_SubMeshOnEdge;
94 long SMESH_Gen_i::GetSubMeshOnFaceTag()
96 return SMESH::Tag_SubMeshOnFace;
99 long SMESH_Gen_i::GetSubMeshOnSolidTag()
101 return SMESH::Tag_SubMeshOnSolid;
104 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
106 return SMESH::Tag_SubMeshOnCompound;
109 long SMESH_Gen_i::GetSubMeshOnWireTag()
111 return SMESH::Tag_SubMeshOnWire;
114 long SMESH_Gen_i::GetSubMeshOnShellTag()
116 return SMESH::Tag_SubMeshOnShell;
119 long SMESH_Gen_i::GetNodeGroupsTag()
121 return SMESH::Tag_NodeGroups;
124 long SMESH_Gen_i::GetEdgeGroupsTag()
126 return SMESH::Tag_EdgeGroups;
129 long SMESH_Gen_i::GetFaceGroupsTag()
131 return SMESH::Tag_FaceGroups;
134 long SMESH_Gen_i::GetVolumeGroupsTag()
136 return SMESH::Tag_VolumeGroups;
139 long SMESH_Gen_i::Get0DElementsGroupsTag()
141 return SMESH::Tag_0DElementsGroups;
144 //=============================================================================
146 * SMESH_Gen_i::CanPublishInStudy
148 * Returns true if object can be published in the study
150 //=============================================================================
152 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
154 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
155 if(CORBA::is_nil(myCurrentStudy))
158 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
159 if( !aMesh->_is_nil() )
162 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
163 if( !aSubMesh->_is_nil() )
166 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
167 if( !aHyp->_is_nil() )
170 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
171 if( !aGroup->_is_nil() )
174 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
178 //=======================================================================
179 //function : ObjectToSObject
181 //=======================================================================
183 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
184 CORBA::Object_ptr theObject)
186 SALOMEDS::SObject_var aSO;
187 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
189 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
190 aSO = theStudy->FindObjectIOR( objStr.in() );
195 //=======================================================================
196 //function : objectToServant
198 //=======================================================================
200 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
202 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
205 //=======================================================================
206 //function : ShapeToGeomObject
208 //=======================================================================
210 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
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 ))
218 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
219 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
222 return aShapeObj._retn();
225 //=======================================================================
226 //function : GeomObjectToShape
228 //=======================================================================
230 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
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 );
242 //=======================================================================
245 //=======================================================================
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)
254 SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
255 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
256 if ( SO->_is_nil() ) {
258 SO = aStudyBuilder->NewObject( theFatherObject );
259 else if ( !theFatherObject->FindSubObject( theTag, SO ))
260 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
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() );
270 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
271 SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
272 pm->SetPixMap( thePixMap );
274 if ( !theSelectable ) {
275 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
276 SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
281 //=======================================================================
284 //=======================================================================
286 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
288 const char* theDefaultName)
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 );
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() );
308 //=======================================================================
309 //function : SetPixMap
311 //=======================================================================
313 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
314 const char* thePixMap)
316 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
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 );
327 //=======================================================================
328 //function : addReference
330 //=======================================================================
332 static void addReference (SALOMEDS::Study_ptr theStudy,
333 SALOMEDS::SObject_ptr theSObject,
334 CORBA::Object_ptr theToObject,
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;
342 // check if the reference to theToObject already exists
343 // and find a free label for the reference object
344 bool isReferred = false;
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 )
352 else if ( !theTag ) {
353 SALOMEDS::GenericAttribute_var anAttr;
354 if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
363 if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
364 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
365 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
369 //=============================================================================
371 * SMESH_Gen_i::PublishInStudy
373 * Publish object in the study
375 //=============================================================================
377 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
378 SALOMEDS::SObject_ptr theSObject,
379 CORBA::Object_ptr theIOR,
381 throw (SALOME::SALOME_Exception)
383 Unexpect aCatch(SALOME_SalomeException);
384 SALOMEDS::SObject_var aSO;
385 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
387 if(MYDEBUG) MESSAGE("PublishInStudy");
390 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
391 if( !aMesh->_is_nil() )
392 aSO = PublishMesh( theStudy, aMesh, theName );
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 );
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 );
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 );
414 if(MYDEBUG) MESSAGE("PublishInStudy_END");
419 //=======================================================================
420 //function : PublishComponent
422 //=======================================================================
424 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
426 if ( CORBA::is_nil( theStudy ))
427 return SALOMEDS::SComponent::_nil();
428 if(MYDEBUG) MESSAGE("PublishComponent");
430 SALOMEDS::SComponent_var father =
431 SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
432 if ( !CORBA::is_nil( father ) )
433 return father._retn();
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();
440 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
441 if ( CORBA::is_nil( aComp ) )
442 return father._retn();
444 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
445 SALOMEDS::GenericAttribute_var anAttr;
446 SALOMEDS::AttributePixMap_var aPixmap;
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");
456 return father._retn();
459 //=============================================================================
461 * findMaxChildTag [ static internal ]
463 * Finds maximum child tag for the given object
465 //=============================================================================
467 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
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();
484 //=======================================================================
485 //function : PublishMesh
487 //=======================================================================
489 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
490 SMESH::SMESH_Mesh_ptr theMesh,
493 if ( CORBA::is_nil( theStudy ) ||
494 CORBA::is_nil( theMesh ))
495 return SALOMEDS::SComponent::_nil();
496 if(MYDEBUG) MESSAGE("PublishMesh--IN");
498 // find or publish a mesh
500 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
501 if ( aMeshSO->_is_nil() )
503 SALOMEDS::SComponent_var father = PublishComponent( theStudy );
504 if ( father->_is_nil() )
505 return aMeshSO._retn();
507 // Find correct free tag
508 long aTag = findMaxChildTag( father.in() );
509 if ( aTag <= GetAlgorithmsRootTag() )
510 aTag = GetAlgorithmsRootTag() + 1;
514 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
515 if ( aMeshSO->_is_nil() )
516 return aMeshSO._retn();
518 SetName( aMeshSO, theName, "Mesh" );
520 // Add shape reference
522 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
523 if ( !CORBA::is_nil( aShapeObject )) {
524 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
526 // Publish global hypotheses
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 );
538 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
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 );
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++ )
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 );
567 if(MYDEBUG) MESSAGE("PublishMesh_END");
568 return aMeshSO._retn();
571 //=======================================================================
572 //function : PublishSubMesh
574 //=======================================================================
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,
582 if (theStudy->_is_nil() || theMesh->_is_nil() ||
583 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
584 return SALOMEDS::SObject::_nil();
586 SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
587 if ( aSubMeshSO->_is_nil() )
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();
595 // Find submesh sub-tree tag
597 const char* aRootName = "";
598 switch ( theShapeObject->GetShapeType() ) {
600 aRootTag = GetSubMeshOnVertexTag();
601 aRootName = "SubMeshes on Vertex";
604 aRootTag = GetSubMeshOnEdgeTag();
605 aRootName = "SubMeshes on Edge";
608 aRootTag = GetSubMeshOnWireTag();
609 aRootName = "SubMeshes on Wire";
612 aRootTag = GetSubMeshOnFaceTag();
613 aRootName = "SubMeshes on Face";
616 aRootTag = GetSubMeshOnShellTag();
617 aRootName = "SubMeshes on Shell";
620 aRootTag = GetSubMeshOnSolidTag();
621 aRootName = "SubMeshes on Solid";
624 aRootTag = GetSubMeshOnCompoundTag();
625 aRootName = "SubMeshes on Compound";
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 );
634 // Add new submesh to corresponding sub-tree
635 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, "ICON_SMESH_TREE_MESH_WARN");
636 if ( aSubMeshSO->_is_nil() )
637 return aSubMeshSO._retn();
639 SetName( aSubMeshSO, theName, "SubMesh" );
641 // Add reference to theShapeObject
643 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
645 // Publish hypothesis
647 SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
649 for ( int i = 0; i < hypList->length(); i++ ) {
650 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
651 PublishHypothesis( theStudy, aHyp );
652 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
655 return aSubMeshSO._retn();
658 //=======================================================================
659 //function : PublishGroup
661 //=======================================================================
663 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
664 SMESH::SMESH_Mesh_ptr theMesh,
665 SMESH::SMESH_GroupBase_ptr theGroup,
666 GEOM::GEOM_Object_ptr theShapeObject,
669 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
670 return SALOMEDS::SObject::_nil();
672 SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
673 if ( aGroupSO->_is_nil() )
675 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
676 if ( aMeshSO->_is_nil() ) {
677 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
678 if ( aMeshSO->_is_nil())
679 return SALOMEDS::SObject::_nil();
681 int aType = (int)theGroup->GetType();
682 const char* aRootNames[] = {
683 "Compound Groups", "Groups of Nodes", "Groups of Edges",
684 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements" };
686 // Currently, groups with heterogenous content are not supported
687 if ( aType != SMESH::ALL ) {
688 long aRootTag = GetNodeGroupsTag() + aType - 1;
690 // Find or create groups root
691 SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
692 aMeshSO, aRootTag, 0, false );
694 SetName( aRootSO, aRootNames[aType] );
696 // Add new group to corresponding sub-tree
697 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, "ICON_SMESH_TREE_GROUP" );
699 if ( aGroupSO->_is_nil() )
700 return aGroupSO._retn();
703 SetName( aGroupSO, theName, "Group" );
705 //Add reference to geometry
706 if ( !theShapeObject->_is_nil() )
707 addReference( theStudy, aGroupSO, theShapeObject, 1 );
709 return aGroupSO._retn();
712 //=======================================================================
713 //function : PublishHypothesis
715 //=======================================================================
717 SALOMEDS::SObject_ptr
718 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
719 SMESH::SMESH_Hypothesis_ptr theHyp,
722 if(MYDEBUG) MESSAGE("PublishHypothesis")
723 if (theStudy->_is_nil() || theHyp->_is_nil())
724 return SALOMEDS::SObject::_nil();
726 SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
727 if ( aHypSO->_is_nil() )
729 SALOMEDS::SComponent_var father = PublishComponent( theStudy );
730 if ( father->_is_nil() )
731 return aHypSO._retn();
733 //Find or Create Hypothesis root
734 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
735 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
736 SALOMEDS::SObject_var aRootSO =
737 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
738 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
739 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
741 // Add New Hypothesis
742 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
743 aPmName += theHyp->GetName();
744 // prepend plugin name to pixmap name
745 string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
746 if ( pluginName != "StdMeshers" )
747 aPmName = pluginName + "::" + aPmName;
748 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
751 if ( !aHypSO->_is_nil() ) {
752 CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
753 SetName( aHypSO, theName, aHypName );
756 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
757 return aHypSO._retn();
760 //=======================================================================
761 //function : GetMeshOrSubmeshByShape
763 //=======================================================================
765 SALOMEDS::SObject_ptr
766 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
767 SMESH::SMESH_Mesh_ptr theMesh,
768 GEOM::GEOM_Object_ptr theShape)
770 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
771 SALOMEDS::SObject_var aMeshOrSubMesh;
772 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
773 return aMeshOrSubMesh._retn();
776 if(theMesh->HasShapeToMesh())
777 aShape = GeomObjectToShape( theShape );
779 aShape = SMESH_Mesh::PseudoShape();
781 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
783 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
784 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
785 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
786 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
788 int shapeID = meshDS->ShapeToIndex( aShape );
789 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
790 if ( !aSubMesh->_is_nil() )
791 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
794 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
795 return aMeshOrSubMesh._retn();
798 //=======================================================================
799 //function : AddHypothesisToShape
801 //=======================================================================
803 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
804 SMESH::SMESH_Mesh_ptr theMesh,
805 GEOM::GEOM_Object_ptr theShape,
806 SMESH::SMESH_Hypothesis_ptr theHyp)
808 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
809 if (theStudy->_is_nil() || theMesh->_is_nil() ||
810 theHyp->_is_nil() || (theShape->_is_nil()
811 && theMesh->HasShapeToMesh()) )
814 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
815 if ( aMeshSO->_is_nil() )
816 aMeshSO = PublishMesh( theStudy, theMesh );
817 SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
818 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
821 // Find a mesh or submesh refering to theShape
822 SALOMEDS::SObject_var aMeshOrSubMesh =
823 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
824 if ( aMeshOrSubMesh->_is_nil() )
827 TopoDS_Shape aShape = GeomObjectToShape( theShape );
828 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
829 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
830 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
831 int shapeID = meshDS->ShapeToIndex( aShape );
832 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
833 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
835 if ( aMeshOrSubMesh->_is_nil() )
839 //Find or Create Applied Hypothesis root
840 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
841 SALOMEDS::SObject_var AHR =
842 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
843 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
844 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
845 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
846 if ( AHR->_is_nil() )
849 addReference( theStudy, AHR, theHyp );
850 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
854 //=======================================================================
855 //function : RemoveHypothesisFromShape
857 //=======================================================================
859 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
860 SMESH::SMESH_Mesh_ptr theMesh,
861 GEOM::GEOM_Object_ptr theShape,
862 SMESH::SMESH_Hypothesis_ptr theHyp)
864 if (theStudy->_is_nil() || theMesh->_is_nil() ||
865 theHyp->_is_nil() || (theShape->_is_nil()
866 && theMesh->HasShapeToMesh()))
869 SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
870 if ( aHypSO->_is_nil() )
873 // Find a mesh or submesh refering to theShape
874 SALOMEDS::SObject_var aMeshOrSubMesh =
875 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
876 if ( aMeshOrSubMesh->_is_nil() )
879 // Find and remove a reference to aHypSO
880 SALOMEDS::SObject_var aRef, anObj;
881 CORBA::String_var anID = CORBA::string_dup( aHypSO->GetID() );
882 SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
883 for ( it->InitEx( true ); it->More(); it->Next() ) {
885 if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
886 theStudy->NewBuilder()->RemoveObject( anObj );
893 //=======================================================================
894 //function : UpdateParameters
896 //=======================================================================
897 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
901 cout<<"UpdateParameters : "<<theParameters<<endl;
902 SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
903 if(aStudy->_is_nil() || CORBA::is_nil(theObject))
906 SALOMEDS::SObject_var aSObj = ObjectToSObject(aStudy,theObject);
910 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
912 SALOMEDS::GenericAttribute_var aFindAttr;
913 bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
915 cout<<"Find Attribute "<<hasAttr<<endl;
917 SALOMEDS::GenericAttribute_var anAttr;
918 anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
919 SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
921 CORBA::String_var oldparVar = aStringAttr->Value();
922 CORBA::String_var inpparVar = ParseParameters(theParameters);
923 TCollection_AsciiString aNewParams;
924 TCollection_AsciiString aOldParameters(oldparVar.inout());
925 TCollection_AsciiString anInputParams(inpparVar.inout());
927 aNewParams = anInputParams;
930 int pos = aOldParameters.SearchFromEnd("|");
932 TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
933 TCollection_AsciiString previousParam(previousParamFull);
934 TCollection_AsciiString theRepet("1");
935 pos = previousParam.SearchFromEnd(";*=");
938 theRepet = previousParam.Split(pos+2);
941 previousParam.Split(pos);
943 if(previousParam == anInputParams)
945 theRepet = theRepet.IntegerValue()+1;
946 aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
950 aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
956 cout<<"Input Parameters : "<<anInputParams<<endl;
957 cout<<"Old Parameters : "<<aOldParameters<<endl;
958 cout<<"New Parameters : "<<aNewParams<<endl;
961 aStringAttr->SetValue( aNewParams.ToCString() );
964 //=======================================================================
965 //function : ParseParameters
967 //=======================================================================
968 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
970 //const char* aParameters = theParameters;
971 // const char* aParameters = CORBA::string_dup(theParameters);
972 TCollection_AsciiString anInputParams;
973 SALOMEDS::Study_var aStudy = GetCurrentStudy();
974 if( !aStudy->_is_nil() ) {
975 // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
976 // for(int j=0;j<aSections->length();j++) {
977 // SALOMEDS::ListOfStrings aVars= aSections[j];
978 // for(int i=0;i<aVars.length();i++ ) {
979 // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
980 // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
981 // if(i != aVars.length()-1)
982 // anInputParams+=":";
984 // if(j!=aSections->length()-1)
985 // anInputParams+="|";
987 TCollection_AsciiString paramStr( theParameters );
988 static TCollection_AsciiString separators(":|");
990 char sep, *pParams = (char*)paramStr.ToCString();
991 while ( beg < paramStr.Length() )
994 while ( ++end < paramStr.Length() )
995 if ( pParams[end] == ':' || pParams[end] == '|')
997 if ( end < paramStr.Length())
1000 pParams[end] = '\0';
1002 if ( aStudy->IsVariable( pParams+beg ))
1003 anInputParams += pParams+beg;
1004 if ( end < paramStr.Length() )
1005 anInputParams += sep;
1011 return CORBA::string_dup(anInputParams.ToCString());
1014 //=======================================================================
1015 //function : GetParameters
1017 //=======================================================================
1018 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1020 TCollection_AsciiString aResult;
1022 SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
1023 SALOMEDS::SObject_var aSObj = ObjectToSObject(aStudy,theObject);
1025 if(!aStudy->_is_nil() &&
1026 !CORBA::is_nil(theObject) &&
1029 SALOMEDS::GenericAttribute_var anAttr;
1030 if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
1031 aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
1035 return CORBA::string_dup( aResult.ToCString() );