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)
27 #include "SMESH_Gen_i.hxx"
29 #include "SMESH_Mesh_i.hxx"
30 #include "SMESH_Hypothesis_i.hxx"
31 #include "SMESH_Algo_i.hxx"
32 #include "SMESH_Group_i.hxx"
33 #include "SMESH_subMesh_i.hxx"
35 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
37 #include "utilities.h"
38 #include "Utils_ExceptHandlers.hxx"
40 #include <TCollection_AsciiString.hxx>
43 static int MYDEBUG = 0;
44 //static int VARIABLE_DEBUG = 0;
46 static int MYDEBUG = 0;
47 //static int VARIABLE_DEBUG = 0;
50 //=============================================================================
52 * Get...Tag [ static ]
54 * Methods which determine SMESH data model structure
56 //=============================================================================
58 long SMESH_Gen_i::GetHypothesisRootTag()
60 return SMESH::Tag_HypothesisRoot;
63 long SMESH_Gen_i::GetAlgorithmsRootTag()
65 return SMESH::Tag_AlgorithmsRoot;
68 long SMESH_Gen_i::GetRefOnShapeTag()
70 return SMESH::Tag_RefOnShape;
73 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
75 return SMESH::Tag_RefOnAppliedHypothesis;
78 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
80 return SMESH::Tag_RefOnAppliedAlgorithms;
83 long SMESH_Gen_i::GetSubMeshOnVertexTag()
85 return SMESH::Tag_SubMeshOnVertex;
88 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
90 return SMESH::Tag_SubMeshOnEdge;
93 long SMESH_Gen_i::GetSubMeshOnFaceTag()
95 return SMESH::Tag_SubMeshOnFace;
98 long SMESH_Gen_i::GetSubMeshOnSolidTag()
100 return SMESH::Tag_SubMeshOnSolid;
103 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
105 return SMESH::Tag_SubMeshOnCompound;
108 long SMESH_Gen_i::GetSubMeshOnWireTag()
110 return SMESH::Tag_SubMeshOnWire;
113 long SMESH_Gen_i::GetSubMeshOnShellTag()
115 return SMESH::Tag_SubMeshOnShell;
118 long SMESH_Gen_i::GetNodeGroupsTag()
120 return SMESH::Tag_NodeGroups;
123 long SMESH_Gen_i::GetEdgeGroupsTag()
125 return SMESH::Tag_EdgeGroups;
128 long SMESH_Gen_i::GetFaceGroupsTag()
130 return SMESH::Tag_FaceGroups;
133 long SMESH_Gen_i::GetVolumeGroupsTag()
135 return SMESH::Tag_VolumeGroups;
138 long SMESH_Gen_i::Get0DElementsGroupsTag()
140 return SMESH::Tag_0DElementsGroups;
143 //=============================================================================
145 * SMESH_Gen_i::CanPublishInStudy
147 * Returns true if object can be published in the study
149 //=============================================================================
151 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
153 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
154 if(CORBA::is_nil(myCurrentStudy))
157 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
158 if( !aMesh->_is_nil() )
161 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
162 if( !aSubMesh->_is_nil() )
165 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
166 if( !aHyp->_is_nil() )
169 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
170 if( !aGroup->_is_nil() )
173 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
177 //=======================================================================
178 //function : ObjectToSObject
180 //=======================================================================
182 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
183 CORBA::Object_ptr theObject)
185 SALOMEDS::SObject_var aSO;
186 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
188 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
189 aSO = theStudy->FindObjectIOR( objStr.in() );
194 //=======================================================================
195 //function : objectToServant
197 //=======================================================================
199 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
201 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
204 //=======================================================================
205 //function : ShapeToGeomObject
207 //=======================================================================
209 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
211 GEOM::GEOM_Object_var aShapeObj;
212 if ( !theShape.IsNull() ) {
213 GEOM_Client* aClient = GetShapeReader();
214 TCollection_AsciiString IOR;
215 if ( aClient && aClient->Find( theShape, IOR ))
217 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
218 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
221 return aShapeObj._retn();
224 //=======================================================================
225 //function : GeomObjectToShape
227 //=======================================================================
229 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
232 if ( !theGeomObject->_is_nil() ) {
233 GEOM_Client* aClient = GetShapeReader();
234 GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
235 if ( aClient && !aGeomEngine->_is_nil () )
236 S = aClient->GetShape( aGeomEngine, theGeomObject );
241 //=======================================================================
244 //=======================================================================
246 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr theStudy,
247 CORBA::Object_ptr theIOR,
248 SALOMEDS::SObject_ptr theFatherObject,
249 const int theTag = 0,
250 const char* thePixMap = 0,
251 const bool theSelectable = true)
253 SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
254 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
255 if ( SO->_is_nil() ) {
257 SO = aStudyBuilder->NewObject( theFatherObject );
258 else if ( !theFatherObject->FindSubObject( theTag, SO ))
259 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
262 SALOMEDS::GenericAttribute_var anAttr;
263 if ( !CORBA::is_nil( theIOR )) {
264 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
265 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
266 SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
269 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
270 SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
271 pm->SetPixMap( thePixMap );
273 if ( !theSelectable ) {
274 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
275 SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
280 //=======================================================================
283 //=======================================================================
285 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
287 const char* theDefaultName)
289 if ( !theSObject->_is_nil() ) {
290 SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
291 SALOMEDS::GenericAttribute_var anAttr =
292 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
293 SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
294 if ( theName && strlen( theName ) != 0 )
295 aNameAttr->SetValue( theName );
297 CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
298 if ( strlen( curName ) == 0 ) {
299 TCollection_AsciiString aName( (char*) theDefaultName );
300 aName += TCollection_AsciiString("_") + TCollection_AsciiString( theSObject->Tag() );
301 aNameAttr->SetValue( aName.ToCString() );
307 //=======================================================================
308 //function : SetPixMap
310 //=======================================================================
312 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
313 const char* thePixMap)
315 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
317 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
318 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
319 SALOMEDS::GenericAttribute_var anAttr =
320 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
321 SALOMEDS::AttributePixMap_var aPMAttr = SALOMEDS::AttributePixMap::_narrow( anAttr );
322 aPMAttr->SetPixMap( thePixMap );
326 //=======================================================================
327 //function : addReference
329 //=======================================================================
331 static void addReference (SALOMEDS::Study_ptr theStudy,
332 SALOMEDS::SObject_ptr theSObject,
333 CORBA::Object_ptr theToObject,
336 SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
337 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
338 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
339 SALOMEDS::SObject_var aReferenceSO;
341 // check if the reference to theToObject already exists
342 // and find a free label for the reference object
343 bool isReferred = false;
345 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
346 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
347 if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
348 if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
351 else if ( !theTag ) {
352 SALOMEDS::GenericAttribute_var anAttr;
353 if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
362 if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
363 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
364 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
368 //=============================================================================
370 * SMESH_Gen_i::PublishInStudy
372 * Publish object in the study
374 //=============================================================================
376 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
377 SALOMEDS::SObject_ptr theSObject,
378 CORBA::Object_ptr theIOR,
380 throw (SALOME::SALOME_Exception)
382 Unexpect aCatch(SALOME_SalomeException);
383 SALOMEDS::SObject_var aSO;
384 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
386 if(MYDEBUG) MESSAGE("PublishInStudy");
389 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
390 if( !aMesh->_is_nil() )
391 aSO = PublishMesh( theStudy, aMesh, theName );
393 // Publishing a sub-mesh
394 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
395 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
396 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
397 aMesh = aSubMesh->GetFather();
398 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
401 // Publishing a hypothesis or algorithm
402 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
403 if ( aSO->_is_nil() && !aHyp->_is_nil() )
404 aSO = PublishHypothesis( theStudy, aHyp );
406 // Publishing a group
407 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
408 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
409 GEOM::GEOM_Object_var aShapeObject;
410 aMesh = aGroup->GetMesh();
411 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
413 if(MYDEBUG) MESSAGE("PublishInStudy_END");
418 //=======================================================================
419 //function : PublishComponent
421 //=======================================================================
423 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
425 if ( CORBA::is_nil( theStudy ))
426 return SALOMEDS::SComponent::_nil();
427 if(MYDEBUG) MESSAGE("PublishComponent");
429 SALOMEDS::SComponent_var father =
430 SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
431 if ( !CORBA::is_nil( father ) )
432 return father._retn();
434 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
435 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
436 if ( CORBA::is_nil( aCat ) )
437 return father._retn();
439 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
440 if ( CORBA::is_nil( aComp ) )
441 return father._retn();
443 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
444 SALOMEDS::GenericAttribute_var anAttr;
445 SALOMEDS::AttributePixMap_var aPixmap;
447 father = aStudyBuilder->NewComponent( ComponentDataType() );
448 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
449 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
450 aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
451 aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
452 SetName( father, aComp->componentusername(), "MESH" );
453 if(MYDEBUG) MESSAGE("PublishComponent--END");
455 return father._retn();
458 //=============================================================================
460 * findMaxChildTag [ static internal ]
462 * Finds maximum child tag for the given object
464 //=============================================================================
466 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
469 if ( !theSObject->_is_nil() ) {
470 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
471 if ( !aStudy->_is_nil() ) {
472 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
473 for ( ; anIter->More(); anIter->Next() ) {
474 long nTag = anIter->Value()->Tag();
483 //=======================================================================
484 //function : PublishMesh
486 //=======================================================================
488 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
489 SMESH::SMESH_Mesh_ptr theMesh,
492 if ( CORBA::is_nil( theStudy ) ||
493 CORBA::is_nil( theMesh ))
494 return SALOMEDS::SComponent::_nil();
495 if(MYDEBUG) MESSAGE("PublishMesh--IN");
497 // find or publish a mesh
499 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
500 if ( aMeshSO->_is_nil() )
502 SALOMEDS::SComponent_var father = PublishComponent( theStudy );
503 if ( father->_is_nil() )
504 return aMeshSO._retn();
506 // Find correct free tag
507 long aTag = findMaxChildTag( father.in() );
508 if ( aTag <= GetAlgorithmsRootTag() )
509 aTag = GetAlgorithmsRootTag() + 1;
513 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
514 if ( aMeshSO->_is_nil() )
515 return aMeshSO._retn();
517 SetName( aMeshSO, theName, "Mesh" );
519 // Add shape reference
521 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
522 if ( !CORBA::is_nil( aShapeObject )) {
523 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
525 // Publish global hypotheses
527 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
528 for ( int i = 0; i < hypList->length(); i++ ) {
529 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
530 PublishHypothesis( theStudy, aHyp );
531 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
537 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
539 return aMeshSO._retn();
540 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
541 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
542 for ( ; subIt != subMap.end(); subIt++ ) {
543 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
544 if ( !CORBA::is_nil( aSubMesh )) {
545 aShapeObject = aSubMesh->GetSubShape();
546 PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
551 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
552 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
553 for ( ; it != grMap.end(); it++ )
555 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
556 if ( !aGroup->_is_nil() ) {
557 GEOM::GEOM_Object_var aShapeObj;
558 SMESH::SMESH_GroupOnGeom_var aGeomGroup =
559 SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
560 if ( !aGeomGroup->_is_nil() )
561 aShapeObj = aGeomGroup->GetShape();
562 PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
566 if(MYDEBUG) MESSAGE("PublishMesh_END");
567 return aMeshSO._retn();
570 //=======================================================================
571 //function : PublishSubMesh
573 //=======================================================================
575 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
576 SMESH::SMESH_Mesh_ptr theMesh,
577 SMESH::SMESH_subMesh_ptr theSubMesh,
578 GEOM::GEOM_Object_ptr theShapeObject,
581 if (theStudy->_is_nil() || theMesh->_is_nil() ||
582 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
583 return SALOMEDS::SObject::_nil();
585 SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
586 if ( aSubMeshSO->_is_nil() )
588 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
589 if ( aMeshSO->_is_nil() ) {
590 aMeshSO = PublishMesh( theStudy, theMesh );
591 if ( aMeshSO->_is_nil())
592 return SALOMEDS::SObject::_nil();
594 // Find submesh sub-tree tag
596 const char* aRootName = "";
597 switch ( theShapeObject->GetShapeType() ) {
599 aRootTag = GetSubMeshOnVertexTag();
600 aRootName = "SubMeshes on Vertex";
603 aRootTag = GetSubMeshOnEdgeTag();
604 aRootName = "SubMeshes on Edge";
607 aRootTag = GetSubMeshOnWireTag();
608 aRootName = "SubMeshes on Wire";
611 aRootTag = GetSubMeshOnFaceTag();
612 aRootName = "SubMeshes on Face";
615 aRootTag = GetSubMeshOnShellTag();
616 aRootName = "SubMeshes on Shell";
619 aRootTag = GetSubMeshOnSolidTag();
620 aRootName = "SubMeshes on Solid";
623 aRootTag = GetSubMeshOnCompoundTag();
624 aRootName = "SubMeshes on Compound";
628 // Find or create submesh root
629 SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
630 aMeshSO, aRootTag, 0, false );
631 SetName( aRootSO, aRootName );
633 // Add new submesh to corresponding sub-tree
634 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
635 const int isEmpty = ( elemTypes->length() == 0 );
636 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
637 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
638 if ( aSubMeshSO->_is_nil() )
639 return aSubMeshSO._retn();
641 SetName( aSubMeshSO, theName, "SubMesh" );
643 // Add reference to theShapeObject
645 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
647 // Publish hypothesis
649 SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
651 for ( int i = 0; i < hypList->length(); i++ ) {
652 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
653 PublishHypothesis( theStudy, aHyp );
654 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
657 return aSubMeshSO._retn();
660 //=======================================================================
661 //function : PublishGroup
663 //=======================================================================
665 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
666 SMESH::SMESH_Mesh_ptr theMesh,
667 SMESH::SMESH_GroupBase_ptr theGroup,
668 GEOM::GEOM_Object_ptr theShapeObject,
671 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
672 return SALOMEDS::SObject::_nil();
674 SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
675 if ( aGroupSO->_is_nil() )
677 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
678 if ( aMeshSO->_is_nil() ) {
679 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
680 if ( aMeshSO->_is_nil())
681 return SALOMEDS::SObject::_nil();
683 int aType = (int)theGroup->GetType();
684 const char* aRootNames[] = {
685 "Compound Groups", "Groups of Nodes", "Groups of Edges",
686 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements" };
688 // Currently, groups with heterogenous content are not supported
689 if ( aType != SMESH::ALL ) {
690 long aRootTag = GetNodeGroupsTag() + aType - 1;
692 // Find or create groups root
693 SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
694 aMeshSO, aRootTag, 0, false );
696 SetName( aRootSO, aRootNames[aType] );
698 // Add new group to corresponding sub-tree
699 SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
700 int isEmpty = ( elemTypes->length() == 0 );
701 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
702 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
704 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
706 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
708 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
709 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
710 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
712 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
714 if ( aGroupSO->_is_nil() )
715 return aGroupSO._retn();
718 SetName( aGroupSO, theName, "Group" );
720 //Add reference to geometry
721 if ( !theShapeObject->_is_nil() )
722 addReference( theStudy, aGroupSO, theShapeObject, 1 );
724 return aGroupSO._retn();
727 //=======================================================================
728 //function : PublishHypothesis
730 //=======================================================================
732 SALOMEDS::SObject_ptr
733 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
734 SMESH::SMESH_Hypothesis_ptr theHyp,
737 if(MYDEBUG) MESSAGE("PublishHypothesis")
738 if (theStudy->_is_nil() || theHyp->_is_nil())
739 return SALOMEDS::SObject::_nil();
741 SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
742 if ( aHypSO->_is_nil() )
744 SALOMEDS::SComponent_var father = PublishComponent( theStudy );
745 if ( father->_is_nil() )
746 return aHypSO._retn();
748 //Find or Create Hypothesis root
749 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
750 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
751 SALOMEDS::SObject_var aRootSO =
752 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
753 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
754 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
756 // Add New Hypothesis
757 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
758 aPmName += theHyp->GetName();
759 // prepend plugin name to pixmap name
760 string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
761 if ( pluginName != "StdMeshers" )
762 aPmName = pluginName + "::" + aPmName;
763 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
766 if ( !aHypSO->_is_nil() ) {
767 CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
768 SetName( aHypSO, theName, aHypName );
771 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
772 return aHypSO._retn();
775 //=======================================================================
776 //function : GetMeshOrSubmeshByShape
778 //=======================================================================
780 SALOMEDS::SObject_ptr
781 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
782 SMESH::SMESH_Mesh_ptr theMesh,
783 GEOM::GEOM_Object_ptr theShape)
785 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
786 SALOMEDS::SObject_var aMeshOrSubMesh;
787 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
788 return aMeshOrSubMesh._retn();
791 if(theMesh->HasShapeToMesh())
792 aShape = GeomObjectToShape( theShape );
794 aShape = SMESH_Mesh::PseudoShape();
796 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
798 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
799 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
800 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
801 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
803 int shapeID = meshDS->ShapeToIndex( aShape );
804 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
805 if ( !aSubMesh->_is_nil() )
806 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
809 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
810 return aMeshOrSubMesh._retn();
813 //=======================================================================
814 //function : AddHypothesisToShape
816 //=======================================================================
818 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
819 SMESH::SMESH_Mesh_ptr theMesh,
820 GEOM::GEOM_Object_ptr theShape,
821 SMESH::SMESH_Hypothesis_ptr theHyp)
823 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
824 if (theStudy->_is_nil() || theMesh->_is_nil() ||
825 theHyp->_is_nil() || (theShape->_is_nil()
826 && theMesh->HasShapeToMesh()) )
829 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
830 if ( aMeshSO->_is_nil() )
831 aMeshSO = PublishMesh( theStudy, theMesh );
832 SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
833 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
836 // Find a mesh or submesh refering to theShape
837 SALOMEDS::SObject_var aMeshOrSubMesh =
838 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
839 if ( aMeshOrSubMesh->_is_nil() )
842 TopoDS_Shape aShape = GeomObjectToShape( theShape );
843 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
844 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
845 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
846 int shapeID = meshDS->ShapeToIndex( aShape );
847 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
848 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
850 if ( aMeshOrSubMesh->_is_nil() )
854 //Find or Create Applied Hypothesis root
855 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
856 SALOMEDS::SObject_var AHR =
857 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
858 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
859 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
860 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
861 if ( AHR->_is_nil() )
864 addReference( theStudy, AHR, theHyp );
865 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
869 //=======================================================================
870 //function : RemoveHypothesisFromShape
872 //=======================================================================
874 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
875 SMESH::SMESH_Mesh_ptr theMesh,
876 GEOM::GEOM_Object_ptr theShape,
877 SMESH::SMESH_Hypothesis_ptr theHyp)
879 if (theStudy->_is_nil() || theMesh->_is_nil() ||
880 theHyp->_is_nil() || (theShape->_is_nil()
881 && theMesh->HasShapeToMesh()))
884 SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
885 if ( aHypSO->_is_nil() )
888 // Find a mesh or submesh refering to theShape
889 SALOMEDS::SObject_var aMeshOrSubMesh =
890 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
891 if ( aMeshOrSubMesh->_is_nil() )
894 // Find and remove a reference to aHypSO
895 SALOMEDS::SObject_var aRef, anObj;
896 CORBA::String_var anID = CORBA::string_dup( aHypSO->GetID() );
897 SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
898 for ( it->InitEx( true ); it->More(); it->Next() ) {
900 if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
901 theStudy->NewBuilder()->RemoveObject( anObj );
908 //=======================================================================
909 //function : UpdateParameters
911 //=======================================================================
912 void SMESH_Gen_i::UpdateParameters(/*CORBA::Object_ptr theObject,*/ const char* theParameters)
914 SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
915 if ( aStudy->_is_nil() )
917 myLastParameters.clear();
918 int pos = 0, prevPos = 0, len = strlen( theParameters );
919 //if ( len == 0 ) return;
922 if ( pos == len || theParameters[pos] == ':' )
926 string val(theParameters + prevPos, theParameters + pos );
927 if ( !aStudy->IsVariable( val.c_str() ))
929 myLastParameters.push_back( val );
933 myLastParameters.push_back("");
943 // if(VARIABLE_DEBUG)
944 // cout<<"UpdateParameters : "<<theParameters<<endl;
945 // //SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
946 // if(aStudy->_is_nil() || CORBA::is_nil(theObject))
949 // SALOMEDS::SObject_var aSObj = ObjectToSObject(aStudy,theObject);
950 // if(aSObj->_is_nil())
953 // SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
955 // SALOMEDS::GenericAttribute_var aFindAttr;
956 // bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
957 // if(VARIABLE_DEBUG)
958 // cout<<"Find Attribute "<<hasAttr<<endl;
960 // SALOMEDS::GenericAttribute_var anAttr;
961 // anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
962 // SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
964 // CORBA::String_var oldparVar = aStringAttr->Value();
965 // CORBA::String_var inpparVar = ParseParameters(theParameters);
966 // TCollection_AsciiString aNewParams;
967 // TCollection_AsciiString aOldParameters(oldparVar.inout());
968 // TCollection_AsciiString anInputParams(inpparVar.inout());
970 // aNewParams = anInputParams;
973 // int pos = aOldParameters.SearchFromEnd("|");
974 // if(pos==-1) pos = 0;
975 // TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
976 // TCollection_AsciiString previousParam(previousParamFull);
977 // TCollection_AsciiString theRepet("1");
978 // pos = previousParam.SearchFromEnd(";*=");
981 // theRepet = previousParam.Split(pos+2);
983 // if(pos==-1) pos = 0;
984 // previousParam.Split(pos);
986 // if(previousParam == anInputParams)
988 // theRepet = theRepet.IntegerValue()+1;
989 // aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
993 // aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
997 // if(VARIABLE_DEBUG)
999 // cout<<"Input Parameters : "<<anInputParams<<endl;
1000 // cout<<"Old Parameters : "<<aOldParameters<<endl;
1001 // cout<<"New Parameters : "<<aNewParams<<endl;
1004 // aStringAttr->SetValue( aNewParams.ToCString() );
1007 //=======================================================================
1008 //function : ParseParameters
1009 //purpose : Replace variables by their values
1010 //=======================================================================
1011 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1013 //const char* aParameters = theParameters;
1014 // const char* aParameters = CORBA::string_dup(theParameters);
1015 TCollection_AsciiString anInputParams;
1016 SALOMEDS::Study_var aStudy = GetCurrentStudy();
1017 if( !aStudy->_is_nil() ) {
1018 // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1019 // for(int j=0;j<aSections->length();j++) {
1020 // SALOMEDS::ListOfStrings aVars= aSections[j];
1021 // for(int i=0;i<aVars.length();i++ ) {
1022 // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1023 // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1024 // if(i != aVars.length()-1)
1025 // anInputParams+=":";
1027 // if(j!=aSections->length()-1)
1028 // anInputParams+="|";
1030 TCollection_AsciiString paramStr( theParameters );
1031 static TCollection_AsciiString separators(":|");
1033 char sep, *pParams = (char*)paramStr.ToCString();
1034 while ( beg < paramStr.Length() )
1037 while ( ++end < paramStr.Length() )
1038 if ( pParams[end] == ':' || pParams[end] == '|')
1040 if ( end < paramStr.Length())
1043 pParams[end] = '\0';
1045 if ( aStudy->IsVariable( pParams+beg ))
1046 anInputParams += pParams+beg;
1047 if ( end < paramStr.Length() )
1048 anInputParams += sep;
1054 return CORBA::string_dup(anInputParams.ToCString());
1057 //=======================================================================
1058 //function : GetParameters
1060 //=======================================================================
1061 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1063 TCollection_AsciiString aResult;
1065 SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
1066 SALOMEDS::SObject_var aSObj = ObjectToSObject(aStudy,theObject);
1068 if(!aStudy->_is_nil() &&
1069 !CORBA::is_nil(theObject) &&
1072 SALOMEDS::GenericAttribute_var anAttr;
1073 if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
1074 aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
1078 return CORBA::string_dup( aResult.ToCString() );