1 // Copyright (C) 2007-2012 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_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"
36 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
38 #include <utilities.h>
39 #include <Utils_ExceptHandlers.hxx>
40 #include <SALOMEDS_wrap.hxx>
41 #include <SALOMEDS_Attributes_wrap.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TopoDS_Solid.hxx>
47 static int MYDEBUG = 0;
48 //static int VARIABLE_DEBUG = 0;
50 static int MYDEBUG = 0;
51 //static int VARIABLE_DEBUG = 0;
54 //=============================================================================
56 * Get...Tag [ static ]
58 * Methods which determine SMESH data model structure
60 //=============================================================================
62 long SMESH_Gen_i::GetHypothesisRootTag()
64 return SMESH::Tag_HypothesisRoot;
67 long SMESH_Gen_i::GetAlgorithmsRootTag()
69 return SMESH::Tag_AlgorithmsRoot;
72 long SMESH_Gen_i::GetRefOnShapeTag()
74 return SMESH::Tag_RefOnShape;
77 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
79 return SMESH::Tag_RefOnAppliedHypothesis;
82 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
84 return SMESH::Tag_RefOnAppliedAlgorithms;
87 long SMESH_Gen_i::GetSubMeshOnVertexTag()
89 return SMESH::Tag_SubMeshOnVertex;
92 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
94 return SMESH::Tag_SubMeshOnEdge;
97 long SMESH_Gen_i::GetSubMeshOnFaceTag()
99 return SMESH::Tag_SubMeshOnFace;
102 long SMESH_Gen_i::GetSubMeshOnSolidTag()
104 return SMESH::Tag_SubMeshOnSolid;
107 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
109 return SMESH::Tag_SubMeshOnCompound;
112 long SMESH_Gen_i::GetSubMeshOnWireTag()
114 return SMESH::Tag_SubMeshOnWire;
117 long SMESH_Gen_i::GetSubMeshOnShellTag()
119 return SMESH::Tag_SubMeshOnShell;
122 long SMESH_Gen_i::GetNodeGroupsTag()
124 return SMESH::Tag_NodeGroups;
127 long SMESH_Gen_i::GetEdgeGroupsTag()
129 return SMESH::Tag_EdgeGroups;
132 long SMESH_Gen_i::GetFaceGroupsTag()
134 return SMESH::Tag_FaceGroups;
137 long SMESH_Gen_i::GetVolumeGroupsTag()
139 return SMESH::Tag_VolumeGroups;
142 long SMESH_Gen_i::Get0DElementsGroupsTag()
144 return SMESH::Tag_0DElementsGroups;
147 long SMESH_Gen_i::GetBallElementsGroupsTag()
149 return SMESH::Tag_BallElementsGroups;
152 //=============================================================================
154 * SMESH_Gen_i::CanPublishInStudy
156 * Returns true if object can be published in the study
158 //=============================================================================
160 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
162 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
163 if(CORBA::is_nil(myCurrentStudy))
166 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
167 if( !aMesh->_is_nil() )
170 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
171 if( !aSubMesh->_is_nil() )
174 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
175 if( !aHyp->_is_nil() )
178 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
179 if( !aGroup->_is_nil() )
182 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
186 //=======================================================================
187 //function : ObjectToSObject
188 //purpose : Put a result into a SALOMEDS::SObject_wrap or call UnRegister()!
189 //=======================================================================
191 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
192 CORBA::Object_ptr theObject)
194 SALOMEDS::SObject_wrap aSO;
195 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
197 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
198 aSO = theStudy->FindObjectIOR( objStr.in() );
203 //=======================================================================
204 //function : objectToServant
206 //=======================================================================
208 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
210 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
213 //=======================================================================
214 //function : ShapeToGeomObject
216 //=======================================================================
218 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
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 ))
226 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
227 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
230 return aShapeObj._retn();
233 //=======================================================================
234 //function : GeomObjectToShape
236 //=======================================================================
238 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
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 );
250 //=======================================================================
253 //=======================================================================
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)
262 SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
263 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
264 if ( SO->_is_nil() ) {
266 SO = aStudyBuilder->NewObject( theFatherObject );
267 else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
268 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
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() );
279 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
280 SALOMEDS::AttributePixMap_wrap pm = anAttr;
281 pm->SetPixMap( thePixMap );
283 if ( !theSelectable ) {
284 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
285 SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
286 selAttr->SetSelectable( false );
292 //=======================================================================
295 //=======================================================================
297 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
299 const char* theDefaultName)
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 );
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() );
319 //=======================================================================
320 //function : SetPixMap
322 //=======================================================================
324 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
325 const char* thePixMap)
327 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
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 );
338 //=======================================================================
339 //function : addReference
341 //=======================================================================
343 static void addReference (SALOMEDS::Study_ptr theStudy,
344 SALOMEDS::SObject_ptr theSObject,
345 CORBA::Object_ptr theToObject,
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;
353 // check if the reference to theToObject already exists
354 // and find a free label for the reference object
355 bool isReferred = false;
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 )
366 else if ( !theTag ) {
367 SALOMEDS::GenericAttribute_wrap anAttr;
368 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
377 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
378 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
379 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
383 //=============================================================================
385 * SMESH_Gen_i::PublishInStudy
387 * Publish object in the study
389 //=============================================================================
391 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
392 SALOMEDS::SObject_ptr theSObject,
393 CORBA::Object_ptr theIOR,
395 throw (SALOME::SALOME_Exception)
397 Unexpect aCatch(SALOME_SalomeException);
398 SALOMEDS::SObject_wrap aSO;
399 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
401 if(MYDEBUG) MESSAGE("PublishInStudy");
404 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
405 if( !aMesh->_is_nil() )
406 aSO = PublishMesh( theStudy, aMesh, theName );
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 );
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 );
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 );
428 if(MYDEBUG) MESSAGE("PublishInStudy_END");
433 //=======================================================================
434 //function : PublishComponent
436 //=======================================================================
438 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
440 if ( CORBA::is_nil( theStudy ))
441 return SALOMEDS::SComponent::_nil();
442 if(MYDEBUG) MESSAGE("PublishComponent");
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();
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();
454 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
455 if ( CORBA::is_nil( aComp ) )
456 return father._retn();
458 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
459 SALOMEDS::GenericAttribute_wrap anAttr;
460 SALOMEDS::AttributePixMap_wrap aPixmap;
462 father = aStudyBuilder->NewComponent( compDataType.in() );
463 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
464 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
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");
471 return father._retn();
474 //=============================================================================
476 * findMaxChildTag [ static internal ]
478 * Finds maximum child tag for the given object
480 //=============================================================================
482 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
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();
500 //=======================================================================
501 //function : PublishMesh
503 //=======================================================================
505 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
506 SMESH::SMESH_Mesh_ptr theMesh,
509 if ( CORBA::is_nil( theStudy ) ||
510 CORBA::is_nil( theMesh ))
511 return SALOMEDS::SComponent::_nil();
512 if(MYDEBUG) MESSAGE("PublishMesh--IN");
514 // find or publish a mesh
516 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
517 if ( aMeshSO->_is_nil() )
519 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
520 if ( father->_is_nil() )
521 return aMeshSO._retn();
523 // Find correct free tag
524 long aTag = findMaxChildTag( father.in() );
525 if ( aTag <= GetAlgorithmsRootTag() )
526 aTag = GetAlgorithmsRootTag() + 1;
530 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
531 if ( aMeshSO->_is_nil() )
532 return aMeshSO._retn();
534 SetName( aMeshSO, theName, "Mesh" );
536 // Add shape reference
538 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
539 if ( !CORBA::is_nil( aShapeObject )) {
540 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
542 // Publish global hypotheses
544 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
545 for ( int i = 0; i < hypList->length(); i++ )
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 );
555 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
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 ));
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++ )
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 ));
583 if(MYDEBUG) MESSAGE("PublishMesh_END");
584 return aMeshSO._retn();
587 //=======================================================================
588 //function : PublishSubMesh
590 //=======================================================================
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,
598 if (theStudy->_is_nil() || theMesh->_is_nil() ||
599 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
600 return SALOMEDS::SObject::_nil();
602 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
603 if ( aSubMeshSO->_is_nil() )
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();
611 // Find submesh sub-tree tag
613 const char* aRootName = "";
614 switch ( theShapeObject->GetShapeType() ) {
616 aRootTag = GetSubMeshOnVertexTag();
617 aRootName = "SubMeshes on Vertex";
620 aRootTag = GetSubMeshOnEdgeTag();
621 aRootName = "SubMeshes on Edge";
624 aRootTag = GetSubMeshOnWireTag();
625 aRootName = "SubMeshes on Wire";
628 aRootTag = GetSubMeshOnFaceTag();
629 aRootName = "SubMeshes on Face";
632 aRootTag = GetSubMeshOnShellTag();
633 aRootName = "SubMeshes on Shell";
636 aRootTag = GetSubMeshOnSolidTag();
637 aRootName = "SubMeshes on Solid";
640 aRootTag = GetSubMeshOnCompoundTag();
641 aRootName = "SubMeshes on Compound";
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();
651 SetName( aRootSO, aRootName );
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();
661 SetName( aSubMeshSO, theName, "SubMesh" );
663 // Add reference to theShapeObject
665 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
667 // Publish hypothesis
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 );
676 return aSubMeshSO._retn();
679 //=======================================================================
680 //function : PublishGroup
682 //=======================================================================
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,
690 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
691 return SALOMEDS::SObject::_nil();
693 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
694 if ( aGroupSO->_is_nil() )
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();
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",
708 // Currently, groups with heterogenous content are not supported
709 if ( aType != SMESH::ALL )
711 long aRootTag = GetNodeGroupsTag() + aType - 1;
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();
718 if ( aType < sizeof(aRootNames)/sizeof(char*) )
719 SetName( aRootSO, aRootNames[aType] );
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 ))
727 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
729 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
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() );
735 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
737 if ( aGroupSO->_is_nil() )
738 return aGroupSO._retn();
741 SetName( aGroupSO, theName, "Group" );
743 //Add reference to geometry
744 if ( !theShapeObject->_is_nil() )
745 addReference( theStudy, aGroupSO, theShapeObject, 1 );
747 return aGroupSO._retn();
750 //=======================================================================
751 //function : PublishHypothesis
753 //=======================================================================
755 SALOMEDS::SObject_ptr
756 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
757 SMESH::SMESH_Hypothesis_ptr theHyp,
760 if(MYDEBUG) MESSAGE("PublishHypothesis")
761 if (theStudy->_is_nil() || theHyp->_is_nil())
762 return SALOMEDS::SObject::_nil();
764 CORBA::String_var hypType = theHyp->GetName();
766 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
767 if ( aHypSO->_is_nil() )
769 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
770 if ( father->_is_nil() )
771 return aHypSO._retn();
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" );
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() );
791 SetName( aHypSO, theName, hypType.in() );
793 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
794 return aHypSO._retn();
797 //=======================================================================
798 //function : GetMeshOrSubmeshByShape
800 //=======================================================================
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)
807 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
808 SALOMEDS::SObject_wrap aMeshOrSubMesh;
809 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
810 return aMeshOrSubMesh._retn();
813 if(theMesh->HasShapeToMesh())
814 aShape = GeomObjectToShape( theShape );
816 aShape = SMESH_Mesh::PseudoShape();
818 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
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 );
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 );
831 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
832 return aMeshOrSubMesh._retn();
835 //=======================================================================
836 //function : AddHypothesisToShape
838 //=======================================================================
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)
845 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
846 if (theStudy->_is_nil() || theMesh->_is_nil() ||
847 theHyp->_is_nil() || (theShape->_is_nil()
848 && theMesh->HasShapeToMesh()) )
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())
858 // Find a mesh or submesh refering to theShape
859 SALOMEDS::SObject_wrap aMeshOrSubMesh =
860 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
861 if ( aMeshOrSubMesh->_is_nil() )
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 );
872 if ( aMeshOrSubMesh->_is_nil() )
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" );
884 addReference( theStudy, AHR, theHyp );
886 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
890 //=======================================================================
891 //function : RemoveHypothesisFromShape
893 //=======================================================================
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)
900 if (theStudy->_is_nil() || theMesh->_is_nil() ||
901 theHyp->_is_nil() || (theShape->_is_nil()
902 && theMesh->HasShapeToMesh()))
905 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
906 if ( aHypSO->_is_nil() )
909 CORBA::String_var hypEntry = aHypSO->GetID();
911 // Find a mesh or submesh refering to theShape
912 SALOMEDS::SObject_wrap aMeshOrSubMesh =
913 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
914 if ( aMeshOrSubMesh->_is_nil() )
917 // Find and remove a reference to aHypSO
918 SALOMEDS::SObject_wrap aRef, anObj;
919 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
921 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
923 if (anObj->ReferencedObject( aRef.inout() ))
925 CORBA::String_var refEntry = aRef->GetID();
926 found = ( strcmp( refEntry, hypEntry ) == 0 );
930 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
931 builder->RemoveObject( anObj );
938 //================================================================================
940 * \brief Stores names of variables that WILL be passes as parameters when calling
941 * some method of a given object.
942 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
943 * \param [in] theParameters - a string contating parameters separated by ':'.
945 //================================================================================
947 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
949 SALOMEDS::Study_var aStudy = GetCurrentStudy();
950 if ( aStudy->_is_nil() )
953 // find variable names within theParameters
955 myLastParameters.clear();
956 myLastParamIndex.clear(); /* vector holding indices of virables within the string
957 of all varibles used for theObject */
959 int pos = 0, prevPos = 0, len = strlen( theParameters );
960 if ( len == 0 ) return;
963 if ( pos == len || theParameters[pos] == ':' )
967 string val( theParameters + prevPos, theParameters + pos );
968 if ( !aStudy->IsVariable( val.c_str() ))
970 myLastParameters.push_back( val );
971 nbVars += (! myLastParameters.back().empty() );
975 myLastParameters.push_back("");
986 // (1) variable names in the string of all varibles used for theObject and
987 // (2) indices of found variables in myLastParamIndex.
989 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
990 if ( aSObj->_is_nil() )
993 // get a string of variable names
994 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
995 SALOMEDS::GenericAttribute_wrap anAttr =
996 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
997 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
998 CORBA::String_var oldVars = aStringAttr->Value();
999 std::string varStr = oldVars.in();
1001 // add new variables and find indices of variables
1002 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1005 if ( !myLastParameters[i].empty() )
1007 // find index of myLastParameters[i] in varStr
1009 bool varFound = false;
1011 // varStr can be "A|B::C;*=2|D"
1012 const std::string separators(":|;*=");
1013 while ( pos < varStr.size() && !varFound )
1016 while ( separators.find( varStr[ pos ]) != std::string::npos )
1017 if ( ++pos >= varStr.size() )
1019 // skip repetition number following '='
1020 if ( varStr[ pos-1 ] == '=' )
1022 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1024 continue; // to skip next separator
1026 // compare variable name
1027 if ( pos < varStr.size() )
1029 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1030 // same string begining but is length same?
1031 ( pos + myLastParameters[i].size() >= varStr.size() ||
1032 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1034 varIndex = curIndex;
1036 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1043 varStr += ":" + myLastParameters[i];
1044 varIndex = curIndex;
1047 myLastParamIndex.push_back( varIndex );
1049 aStringAttr->SetValue( varStr.c_str() );
1052 //================================================================================
1054 * \brief Return all variables used to create an object
1055 * \param [in] theObjectEntry - an object entry in the current study
1056 * \return std::vector< std::string > - all variable names (or values of removed variables)
1058 //================================================================================
1060 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1062 std::vector< std::string > varNames;
1063 if ( myCurrentStudy->_is_nil() )
1066 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1067 if ( myCurrentStudy->_is_nil() )
1070 // get a string of variable names
1071 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1072 SALOMEDS::GenericAttribute_wrap anAttr =
1073 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1074 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1075 CORBA::String_var oldVars = aStringAttr->Value();
1076 std::string varStr = oldVars.in();
1078 // separate variables within varStr;
1079 // varStr can be "A|B::C;*=2|D"
1081 const std::string separators(":|;*=");
1082 while ( pos < varStr.size() )
1085 while ( separators.find( varStr[ pos ]) != std::string::npos )
1086 if ( ++pos >= varStr.size() )
1088 // skip repetition number following '='
1089 if ( varStr[ pos-1 ] == '=' )
1091 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1093 continue; // to skip next separator
1095 // store variable name
1096 if ( pos < varStr.size() )
1098 size_t pos2 = varStr.find_first_of( separators, pos );
1099 varNames.push_back( varStr.substr( pos, pos2-1 ));
1106 //=======================================================================
1107 //function : ParseParameters
1108 //purpose : Replace variables by their values
1109 //=======================================================================
1110 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1112 // //const char* aParameters = theParameters;
1113 // // const char* aParameters = CORBA::string_dup(theParameters);
1114 // TCollection_AsciiString anInputParams;
1115 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1116 // if( !aStudy->_is_nil() ) {
1117 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1118 // // for(int j=0;j<aSections->length();j++) {
1119 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1120 // // for(int i=0;i<aVars.length();i++ ) {
1121 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1122 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1123 // // if(i != aVars.length()-1)
1124 // // anInputParams+=":";
1126 // // if(j!=aSections->length()-1)
1127 // // anInputParams+="|";
1129 // TCollection_AsciiString paramStr( theParameters );
1130 // int beg = 0, end;
1131 // char sep, *pParams = (char*)paramStr.ToCString();
1132 // while ( beg < paramStr.Length() )
1135 // while ( ++end < paramStr.Length() )
1136 // if ( pParams[end] == ':' || pParams[end] == '|')
1138 // if ( end < paramStr.Length())
1140 // sep = pParams[end];
1141 // pParams[end] = '\0';
1143 // if ( aStudy->IsVariable( pParams+beg ))
1144 // anInputParams += pParams+beg;
1145 // if ( end < paramStr.Length() )
1146 // anInputParams += sep;
1152 // return CORBA::string_dup(anInputParams.ToCString());
1155 //=======================================================================
1156 //function : GetParameters
1158 //=======================================================================
1160 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1162 CORBA::String_var aResult("");
1164 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy,theObject);
1165 if ( !aSObj->_is_nil() )
1167 SALOMEDS::GenericAttribute_wrap attr;
1168 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1170 SALOMEDS::AttributeString_wrap strAttr = attr;
1171 aResult = strAttr->Value();
1175 return CORBA::string_dup( aResult.in() );