1 // Copyright (C) 2007-2013 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 SALOMEDS::UseCaseBuilder_var useCaseBuilder = theStudy->GetUseCaseBuilder();
265 if ( SO->_is_nil() ) {
267 SO = aStudyBuilder->NewObject( theFatherObject );
268 else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
269 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
272 SALOMEDS::GenericAttribute_wrap anAttr;
273 if ( !CORBA::is_nil( theIOR )) {
274 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
275 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
276 SALOMEDS::AttributeIOR_wrap iorAttr = anAttr;
277 iorAttr->SetValue( objStr.in() );
279 SALOME::GenericObj_var genObj = SALOME::GenericObj::_narrow( theIOR );
280 if ( !genObj->_is_nil() )
281 genObj->UnRegister();
284 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
285 SALOMEDS::AttributePixMap_wrap pm = anAttr;
286 pm->SetPixMap( thePixMap );
288 if ( !theSelectable ) {
289 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
290 SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
291 selAttr->SetSelectable( false );
294 // add object to the use case tree
295 // (to support tree representation customization and drag-n-drop)
296 useCaseBuilder->AppendTo( SO->GetFather(), SO );
301 //=======================================================================
304 //=======================================================================
306 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
308 const char* theDefaultName)
310 if ( !theSObject->_is_nil() ) {
311 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
312 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
313 SALOMEDS::GenericAttribute_wrap anAttr =
314 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
315 SALOMEDS::AttributeName_wrap aNameAttr = anAttr;
316 if ( theName && strlen( theName ) != 0 )
317 aNameAttr->SetValue( theName );
319 CORBA::String_var curName = aNameAttr->Value();
320 if ( strlen( curName.in() ) == 0 ) {
321 SMESH_Comment aName(theDefaultName);
322 aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
328 //=======================================================================
329 //function : SetPixMap
331 //=======================================================================
333 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
334 const char* thePixMap)
336 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
338 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
339 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
340 SALOMEDS::GenericAttribute_wrap anAttr =
341 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
342 SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
343 aPMAttr->SetPixMap( thePixMap );
347 //=======================================================================
348 //function : addReference
350 //=======================================================================
352 static void addReference (SALOMEDS::Study_ptr theStudy,
353 SALOMEDS::SObject_ptr theSObject,
354 CORBA::Object_ptr theToObject,
357 SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
358 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
359 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
360 SALOMEDS::SObject_wrap aReferenceSO;
362 // check if the reference to theToObject already exists
363 // and find a free label for the reference object
364 bool isReferred = false;
366 SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
367 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
368 SALOMEDS::SObject_wrap curSO = anIter->Value();
369 if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
370 CORBA::String_var refEntry = aReferenceSO->GetID();
371 CORBA::String_var toEntry = aToObjSO->GetID();
372 if ( strcmp( refEntry, toEntry ) == 0 )
375 else if ( !theTag ) {
376 SALOMEDS::GenericAttribute_wrap anAttr;
377 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
386 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
387 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
388 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
389 // add reference to the use case tree
390 // (to support tree representation customization and drag-n-drop)
391 theStudy->GetUseCaseBuilder()->AppendTo( aReferenceSO->GetFather(), aReferenceSO );
395 //=============================================================================
397 * SMESH_Gen_i::PublishInStudy
399 * Publish object in the study
401 //=============================================================================
403 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
404 SALOMEDS::SObject_ptr theSObject,
405 CORBA::Object_ptr theIOR,
407 throw (SALOME::SALOME_Exception)
409 Unexpect aCatch(SALOME_SalomeException);
410 SALOMEDS::SObject_wrap aSO;
411 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
413 if(MYDEBUG) MESSAGE("PublishInStudy");
416 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
417 if( !aMesh->_is_nil() )
418 aSO = PublishMesh( theStudy, aMesh, theName );
420 // Publishing a sub-mesh
421 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
422 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
423 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
424 aMesh = aSubMesh->GetFather();
425 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
428 // Publishing a hypothesis or algorithm
429 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
430 if ( aSO->_is_nil() && !aHyp->_is_nil() )
431 aSO = PublishHypothesis( theStudy, aHyp );
433 // Publishing a group
434 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
435 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
436 GEOM::GEOM_Object_var aShapeObject;
437 aMesh = aGroup->GetMesh();
438 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
440 if(MYDEBUG) MESSAGE("PublishInStudy_END");
445 //=======================================================================
446 //function : PublishComponent
448 //=======================================================================
450 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
452 if ( CORBA::is_nil( theStudy ))
453 return SALOMEDS::SComponent::_nil();
454 if(MYDEBUG) MESSAGE("PublishComponent");
456 CORBA::String_var compDataType = ComponentDataType();
457 SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
458 if ( !CORBA::is_nil( father ) )
459 return father._retn();
461 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
462 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
463 if ( CORBA::is_nil( aCat ) )
464 return father._retn();
466 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
467 if ( CORBA::is_nil( aComp ) )
468 return father._retn();
470 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
471 SALOMEDS::UseCaseBuilder_var useCaseBuilder = theStudy->GetUseCaseBuilder();
472 SALOMEDS::GenericAttribute_wrap anAttr;
473 SALOMEDS::AttributePixMap_wrap aPixmap;
475 father = aStudyBuilder->NewComponent( compDataType.in() );
476 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
477 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
479 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
480 CORBA::String_var userName = aComp->componentusername();
481 SetName( father, userName.in(), "MESH" );
482 // add component to the use case tree
483 // (to support tree representation customization and drag-n-drop)
484 useCaseBuilder->SetRootCurrent();
485 useCaseBuilder->Append( father ); // component object is added as the top level item
486 if(MYDEBUG) MESSAGE("PublishComponent--END");
488 return father._retn();
491 //=============================================================================
493 * findMaxChildTag [ static internal ]
495 * Finds maximum child tag for the given object
497 //=============================================================================
499 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
502 if ( !theSObject->_is_nil() ) {
503 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
504 if ( !aStudy->_is_nil() ) {
505 SALOMEDS::ChildIterator_wrap anIter = aStudy->NewChildIterator( theSObject );
506 for ( ; anIter->More(); anIter->Next() ) {
507 SALOMEDS::SObject_wrap anSO = anIter->Value();
508 long nTag = anSO->Tag();
517 //=======================================================================
518 //function : PublishMesh
520 //=======================================================================
522 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
523 SMESH::SMESH_Mesh_ptr theMesh,
526 if ( CORBA::is_nil( theStudy ) ||
527 CORBA::is_nil( theMesh ))
528 return SALOMEDS::SComponent::_nil();
529 if(MYDEBUG) MESSAGE("PublishMesh--IN");
531 // find or publish a mesh
533 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
534 if ( aMeshSO->_is_nil() )
536 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
537 if ( father->_is_nil() )
538 return aMeshSO._retn();
540 // Find correct free tag
541 long aTag = findMaxChildTag( father.in() );
542 if ( aTag <= GetAlgorithmsRootTag() )
543 aTag = GetAlgorithmsRootTag() + 1;
547 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
548 if ( aMeshSO->_is_nil() )
549 return aMeshSO._retn();
551 SetName( aMeshSO, theName, "Mesh" );
553 // Add shape reference
555 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
556 if ( !CORBA::is_nil( aShapeObject )) {
557 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
559 // Publish global hypotheses
561 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
562 for ( int i = 0; i < hypList->length(); i++ )
564 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
565 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
566 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
572 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
574 return aMeshSO._retn();
575 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
576 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
577 for ( ; subIt != subMap.end(); subIt++ ) {
578 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
579 if ( !CORBA::is_nil( aSubMesh )) {
580 aShapeObject = aSubMesh->GetSubShape();
581 SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
586 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
587 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
588 for ( ; it != grMap.end(); it++ )
590 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
591 if ( !aGroup->_is_nil() ) {
592 GEOM::GEOM_Object_var aShapeObj;
593 SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
594 if ( !aGeomGroup->_is_nil() )
595 aShapeObj = aGeomGroup->GetShape();
596 SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
600 if(MYDEBUG) MESSAGE("PublishMesh_END");
601 return aMeshSO._retn();
604 //=======================================================================
605 //function : PublishSubMesh
607 //=======================================================================
609 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
610 SMESH::SMESH_Mesh_ptr theMesh,
611 SMESH::SMESH_subMesh_ptr theSubMesh,
612 GEOM::GEOM_Object_ptr theShapeObject,
615 if (theStudy->_is_nil() || theMesh->_is_nil() ||
616 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
617 return SALOMEDS::SObject::_nil();
619 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
620 if ( aSubMeshSO->_is_nil() )
622 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
623 if ( aMeshSO->_is_nil() ) {
624 aMeshSO = PublishMesh( theStudy, theMesh );
625 if ( aMeshSO->_is_nil())
626 return SALOMEDS::SObject::_nil();
628 // Find submesh sub-tree tag
630 const char* aRootName = "";
631 switch ( theShapeObject->GetShapeType() ) {
633 aRootTag = GetSubMeshOnVertexTag();
634 aRootName = "SubMeshes on Vertex";
637 aRootTag = GetSubMeshOnEdgeTag();
638 aRootName = "SubMeshes on Edge";
641 aRootTag = GetSubMeshOnWireTag();
642 aRootName = "SubMeshes on Wire";
645 aRootTag = GetSubMeshOnFaceTag();
646 aRootName = "SubMeshes on Face";
649 aRootTag = GetSubMeshOnShellTag();
650 aRootName = "SubMeshes on Shell";
653 aRootTag = GetSubMeshOnSolidTag();
654 aRootName = "SubMeshes on Solid";
657 aRootTag = GetSubMeshOnCompoundTag();
658 aRootName = "SubMeshes on Compound";
662 // Find or create submesh root
663 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
664 aMeshSO, aRootTag, 0, false );
665 if ( aRootSO->_is_nil() )
666 return aSubMeshSO._retn();
668 SetName( aRootSO, aRootName );
670 // Add new submesh to corresponding sub-tree
671 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
672 const int isEmpty = ( elemTypes->length() == 0 );
673 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
674 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
675 if ( aSubMeshSO->_is_nil() )
676 return aSubMeshSO._retn();
678 SetName( aSubMeshSO, theName, "SubMesh" );
680 // Add reference to theShapeObject
682 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
684 // Publish hypothesis
686 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
687 for ( int i = 0; i < hypList->length(); i++ ) {
688 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
689 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
690 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
693 return aSubMeshSO._retn();
696 //=======================================================================
697 //function : PublishGroup
699 //=======================================================================
701 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
702 SMESH::SMESH_Mesh_ptr theMesh,
703 SMESH::SMESH_GroupBase_ptr theGroup,
704 GEOM::GEOM_Object_ptr theShapeObject,
707 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
708 return SALOMEDS::SObject::_nil();
710 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
711 if ( aGroupSO->_is_nil() )
713 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
714 if ( aMeshSO->_is_nil() ) {
715 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
716 if ( aMeshSO->_is_nil())
717 return SALOMEDS::SObject::_nil();
719 int aType = (int)theGroup->GetType();
720 const char* aRootNames[] = {
721 "Compound Groups", "Groups of Nodes", "Groups of Edges",
722 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
725 // Currently, groups with heterogenous content are not supported
726 if ( aType != SMESH::ALL )
728 long aRootTag = GetNodeGroupsTag() + aType - 1;
730 // Find or create groups root
731 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
732 aMeshSO, aRootTag, 0, false );
733 if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
735 if ( aType < sizeof(aRootNames)/sizeof(char*) )
736 SetName( aRootSO, aRootNames[aType] );
738 // Add new group to corresponding sub-tree
739 SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
740 int isEmpty = ( elemTypes->length() == 0 );
741 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
742 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
744 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
746 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
748 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
749 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
750 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
752 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
754 if ( aGroupSO->_is_nil() )
755 return aGroupSO._retn();
758 SetName( aGroupSO, theName, "Group" );
760 //Add reference to geometry
761 if ( !theShapeObject->_is_nil() )
762 addReference( theStudy, aGroupSO, theShapeObject, 1 );
764 return aGroupSO._retn();
767 //=======================================================================
768 //function : PublishHypothesis
770 //=======================================================================
772 SALOMEDS::SObject_ptr
773 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
774 SMESH::SMESH_Hypothesis_ptr theHyp,
777 if(MYDEBUG) MESSAGE("PublishHypothesis")
778 if (theStudy->_is_nil() || theHyp->_is_nil())
779 return SALOMEDS::SObject::_nil();
781 CORBA::String_var hypType = theHyp->GetName();
783 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
784 if ( aHypSO->_is_nil() )
786 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
787 if ( father->_is_nil() )
788 return aHypSO._retn();
790 //Find or Create Hypothesis root
791 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
792 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
793 SALOMEDS::SObject_wrap aRootSO =
794 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
795 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
796 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
798 // Add New Hypothesis
799 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
800 aPmName += hypType.in();
801 // prepend plugin name to pixmap name
802 string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
803 if ( pluginName != "StdMeshers" )
804 aPmName = pluginName + "::" + aPmName;
805 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
808 SetName( aHypSO, theName, hypType.in() );
810 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
811 return aHypSO._retn();
814 //=======================================================================
815 //function : GetMeshOrSubmeshByShape
817 //=======================================================================
819 SALOMEDS::SObject_ptr
820 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
821 SMESH::SMESH_Mesh_ptr theMesh,
822 GEOM::GEOM_Object_ptr theShape)
824 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
825 SALOMEDS::SObject_wrap aMeshOrSubMesh;
826 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
827 return aMeshOrSubMesh._retn();
830 if(theMesh->HasShapeToMesh())
831 aShape = GeomObjectToShape( theShape );
833 aShape = SMESH_Mesh::PseudoShape();
835 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
837 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
838 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
839 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
840 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
842 int shapeID = meshDS->ShapeToIndex( aShape );
843 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
844 if ( !aSubMesh->_is_nil() )
845 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
848 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
849 return aMeshOrSubMesh._retn();
852 //=======================================================================
853 //function : AddHypothesisToShape
855 //=======================================================================
857 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
858 SMESH::SMESH_Mesh_ptr theMesh,
859 GEOM::GEOM_Object_ptr theShape,
860 SMESH::SMESH_Hypothesis_ptr theHyp)
862 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
863 if (theStudy->_is_nil() || theMesh->_is_nil() ||
864 theHyp->_is_nil() || (theShape->_is_nil()
865 && theMesh->HasShapeToMesh()) )
868 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
869 if ( aMeshSO->_is_nil() )
870 aMeshSO = PublishMesh( theStudy, theMesh );
871 SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
872 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
875 // Find a mesh or submesh refering to theShape
876 SALOMEDS::SObject_wrap aMeshOrSubMesh =
877 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
878 if ( aMeshOrSubMesh->_is_nil() )
881 TopoDS_Shape aShape = GeomObjectToShape( theShape );
882 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
883 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
884 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
885 int shapeID = meshDS->ShapeToIndex( aShape );
886 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
887 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
889 if ( aMeshOrSubMesh->_is_nil() )
893 //Find or Create Applied Hypothesis root
894 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
895 SALOMEDS::SObject_wrap AHR =
896 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
897 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
898 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
899 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
901 addReference( theStudy, AHR, theHyp );
903 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
907 //=======================================================================
908 //function : RemoveHypothesisFromShape
910 //=======================================================================
912 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
913 SMESH::SMESH_Mesh_ptr theMesh,
914 GEOM::GEOM_Object_ptr theShape,
915 SMESH::SMESH_Hypothesis_ptr theHyp)
917 if (theStudy->_is_nil() || theMesh->_is_nil() ||
918 theHyp->_is_nil() || (theShape->_is_nil()
919 && theMesh->HasShapeToMesh()))
922 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
923 if ( aHypSO->_is_nil() )
926 CORBA::String_var hypEntry = aHypSO->GetID();
928 // Find a mesh or submesh refering to theShape
929 SALOMEDS::SObject_wrap aMeshOrSubMesh =
930 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
931 if ( aMeshOrSubMesh->_is_nil() )
934 // Find and remove a reference to aHypSO
935 SALOMEDS::SObject_wrap aRef, anObj;
936 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
938 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
940 if (anObj->ReferencedObject( aRef.inout() ))
942 CORBA::String_var refEntry = aRef->GetID();
943 found = ( strcmp( refEntry, hypEntry ) == 0 );
947 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
948 builder->RemoveObject( anObj );
955 //================================================================================
957 * \brief Stores names of variables that WILL be passes as parameters when calling
958 * some method of a given object.
959 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
960 * \param [in] theParameters - a string contating parameters separated by ':'.
962 //================================================================================
964 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
966 SALOMEDS::Study_var aStudy = GetCurrentStudy();
967 if ( aStudy->_is_nil() )
970 // find variable names within theParameters
973 myLastParameters.clear();
974 myLastParamIndex.clear(); /* vector holding indices of virables within the string
975 of all varibles used for theObject */
977 int pos = 0, prevPos = 0, len = strlen( theParameters );
978 if ( len == 0 ) return;
981 if ( pos == len || theParameters[pos] == ':' )
985 string val( theParameters + prevPos, theParameters + pos );
986 if ( !aStudy->IsVariable( val.c_str() ))
988 myLastParameters.push_back( val );
989 nbVars += (! myLastParameters.back().empty() );
993 myLastParameters.push_back("");
1004 // (1) variable names in the string of all varibles used for theObject and
1005 // (2) indices of found variables in myLastParamIndex.
1007 // remember theObject
1008 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
1009 if ( aSObj->_is_nil() )
1011 CORBA::String_var anObjEntry = aSObj->GetID();
1012 myLastObj = anObjEntry.in();
1014 // get a string of variable names
1015 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1016 SALOMEDS::GenericAttribute_wrap anAttr =
1017 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1018 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1019 CORBA::String_var oldVars = aStringAttr->Value();
1020 std::string varStr = oldVars.in();
1022 // add new variables and find indices of variables
1023 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1026 if ( !myLastParameters[i].empty() )
1028 // find index of myLastParameters[i] in varStr
1030 bool varFound = false;
1032 // varStr can be "A|B::C;*=2|D"
1033 const std::string separators(":|;*=");
1034 while ( pos < varStr.size() && !varFound )
1037 while ( separators.find( varStr[ pos ]) != std::string::npos )
1038 if ( ++pos >= varStr.size() )
1040 // skip repetition number following '='
1041 if ( varStr[ pos-1 ] == '=' )
1043 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1045 continue; // to skip next separator
1047 // compare variable name
1048 if ( pos < varStr.size() )
1050 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1051 // same string begining but is length same?
1052 ( pos + myLastParameters[i].size() >= varStr.size() ||
1053 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1055 varIndex = curIndex;
1057 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1064 varStr += ":" + myLastParameters[i];
1065 varIndex = curIndex;
1068 myLastParamIndex.push_back( varIndex );
1070 aStringAttr->SetValue( varStr.c_str() );
1073 //================================================================================
1075 * \brief Return all variables used to create an object
1076 * \param [in] theObjectEntry - an object entry in the current study
1077 * \return std::vector< std::string > - all variable names (or values of removed variables)
1079 //================================================================================
1081 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1083 std::vector< std::string > varNames;
1084 if ( myCurrentStudy->_is_nil() )
1087 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1088 if ( myCurrentStudy->_is_nil() )
1091 // get a string of variable names
1092 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1093 SALOMEDS::GenericAttribute_wrap anAttr =
1094 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1095 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1096 CORBA::String_var oldVars = aStringAttr->Value();
1097 std::string varStr = oldVars.in();
1099 // separate variables within varStr;
1100 // varStr can be "A|B::C;*=2|D"
1102 const std::string separators(":|;*=");
1103 while ( pos < varStr.size() )
1106 pos = varStr.find_first_not_of( separators, pos );
1107 // while ( separators.find( varStr[ pos ]) != std::string::npos )
1108 // if ( ++pos >= varStr.size() )
1110 // skip repetition number following '='
1111 if ( varStr[ pos-1 ] == '=' )
1113 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1115 continue; // to skip next separator
1117 // store variable name
1118 if ( pos < varStr.size() )
1120 size_t pos2 = varStr.find_first_of( separators, pos );
1121 varNames.push_back( varStr.substr( pos, pos2 - pos));
1128 //=======================================================================
1129 //function : ParseParameters
1130 //purpose : Replace variables by their values
1131 //=======================================================================
1132 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1134 // //const char* aParameters = theParameters;
1135 // // const char* aParameters = CORBA::string_dup(theParameters);
1136 // TCollection_AsciiString anInputParams;
1137 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1138 // if( !aStudy->_is_nil() ) {
1139 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1140 // // for(int j=0;j<aSections->length();j++) {
1141 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1142 // // for(int i=0;i<aVars.length();i++ ) {
1143 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1144 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1145 // // if(i != aVars.length()-1)
1146 // // anInputParams+=":";
1148 // // if(j!=aSections->length()-1)
1149 // // anInputParams+="|";
1151 // TCollection_AsciiString paramStr( theParameters );
1152 // int beg = 0, end;
1153 // char sep, *pParams = (char*)paramStr.ToCString();
1154 // while ( beg < paramStr.Length() )
1157 // while ( ++end < paramStr.Length() )
1158 // if ( pParams[end] == ':' || pParams[end] == '|')
1160 // if ( end < paramStr.Length())
1162 // sep = pParams[end];
1163 // pParams[end] = '\0';
1165 // if ( aStudy->IsVariable( pParams+beg ))
1166 // anInputParams += pParams+beg;
1167 // if ( end < paramStr.Length() )
1168 // anInputParams += sep;
1174 // return CORBA::string_dup(anInputParams.ToCString());
1177 //=======================================================================
1178 //function : GetParameters
1180 //=======================================================================
1182 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1184 CORBA::String_var aResult("");
1186 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1187 if ( !aSObj->_is_nil() )
1189 SALOMEDS::GenericAttribute_wrap attr;
1190 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1192 SALOMEDS::AttributeString_wrap strAttr = attr;
1193 aResult = strAttr->Value();
1196 return aResult._retn();