1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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>
49 static int MYDEBUG = 0;
50 //static int VARIABLE_DEBUG = 0;
52 static int MYDEBUG = 0;
53 //static int VARIABLE_DEBUG = 0;
56 //=============================================================================
58 * Get...Tag [ static ]
60 * Methods which determine SMESH data model structure
62 //=============================================================================
64 long SMESH_Gen_i::GetHypothesisRootTag()
66 return SMESH::Tag_HypothesisRoot;
69 long SMESH_Gen_i::GetAlgorithmsRootTag()
71 return SMESH::Tag_AlgorithmsRoot;
74 long SMESH_Gen_i::GetRefOnShapeTag()
76 return SMESH::Tag_RefOnShape;
79 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
81 return SMESH::Tag_RefOnAppliedHypothesis;
84 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
86 return SMESH::Tag_RefOnAppliedAlgorithms;
89 long SMESH_Gen_i::GetSubMeshOnVertexTag()
91 return SMESH::Tag_SubMeshOnVertex;
94 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
96 return SMESH::Tag_SubMeshOnEdge;
99 long SMESH_Gen_i::GetSubMeshOnFaceTag()
101 return SMESH::Tag_SubMeshOnFace;
104 long SMESH_Gen_i::GetSubMeshOnSolidTag()
106 return SMESH::Tag_SubMeshOnSolid;
109 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
111 return SMESH::Tag_SubMeshOnCompound;
114 long SMESH_Gen_i::GetSubMeshOnWireTag()
116 return SMESH::Tag_SubMeshOnWire;
119 long SMESH_Gen_i::GetSubMeshOnShellTag()
121 return SMESH::Tag_SubMeshOnShell;
124 long SMESH_Gen_i::GetNodeGroupsTag()
126 return SMESH::Tag_NodeGroups;
129 long SMESH_Gen_i::GetEdgeGroupsTag()
131 return SMESH::Tag_EdgeGroups;
134 long SMESH_Gen_i::GetFaceGroupsTag()
136 return SMESH::Tag_FaceGroups;
139 long SMESH_Gen_i::GetVolumeGroupsTag()
141 return SMESH::Tag_VolumeGroups;
144 long SMESH_Gen_i::Get0DElementsGroupsTag()
146 return SMESH::Tag_0DElementsGroups;
149 long SMESH_Gen_i::GetBallElementsGroupsTag()
151 return SMESH::Tag_BallElementsGroups;
154 //=============================================================================
156 * SMESH_Gen_i::CanPublishInStudy
158 * Returns true if object can be published in the study
160 //=============================================================================
162 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
164 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
165 if( GetCurrentStudyID() < 0 )
168 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
169 if( !aMesh->_is_nil() )
172 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
173 if( !aSubMesh->_is_nil() )
176 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
177 if( !aHyp->_is_nil() )
180 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
181 if( !aGroup->_is_nil() )
184 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
188 //=======================================================================
189 //function : ObjectToSObject
190 //purpose : Put a result into a SALOMEDS::SObject_wrap or call UnRegister()!
191 //=======================================================================
193 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
194 CORBA::Object_ptr theObject)
196 SALOMEDS::SObject_wrap aSO;
197 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
199 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
200 aSO = theStudy->FindObjectIOR( objStr.in() );
205 //=======================================================================
206 //function : objectToServant
208 //=======================================================================
210 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
212 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
215 //=======================================================================
216 //function : ShapeToGeomObject
218 //=======================================================================
220 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
222 GEOM::GEOM_Object_var aShapeObj;
223 if ( !theShape.IsNull() ) {
224 GEOM_Client* aClient = GetShapeReader();
225 TCollection_AsciiString IOR;
226 if ( aClient && aClient->Find( theShape, IOR ))
228 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
229 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
232 return aShapeObj._retn();
235 //=======================================================================
236 //function : GeomObjectToShape
238 //=======================================================================
240 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
243 if ( !theGeomObject->_is_nil() ) {
244 GEOM_Client* aClient = GetShapeReader();
245 GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
246 if ( aClient && !aGeomEngine->_is_nil () )
247 S = aClient->GetShape( aGeomEngine, theGeomObject );
252 //=======================================================================
255 //=======================================================================
257 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr theStudy,
258 CORBA::Object_ptr theIOR,
259 SALOMEDS::SObject_ptr theFatherObject,
260 const int theTag = 0,
261 const char* thePixMap = 0,
262 const bool theSelectable = true)
264 SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
265 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
266 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
267 SALOMEDS::SObject_wrap objAfter;
271 SO = aStudyBuilder->NewObject( theFatherObject );
273 else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
275 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
277 // define the next tag after given one in the data tree to insert SObject
278 SALOMEDS::SObject_wrap curObj;
279 if ( theFatherObject->GetLastChildTag() > theTag )
281 SALOMEDS::UseCaseIterator_wrap anUseCaseIter = useCaseBuilder->GetUseCaseIterator(theFatherObject);
282 for ( ; anUseCaseIter->More(); anUseCaseIter->Next() ) {
283 curObj = anUseCaseIter->Value();
284 if ( curObj->Tag() > theTag ) {
293 SALOMEDS::GenericAttribute_wrap anAttr;
294 if ( !CORBA::is_nil( theIOR )) {
295 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
296 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
297 SALOMEDS::AttributeIOR_wrap iorAttr = anAttr;
298 iorAttr->SetValue( objStr.in() );
300 SALOME::GenericObj_var genObj = SALOME::GenericObj::_narrow( theIOR );
301 if ( !genObj->_is_nil() )
302 genObj->UnRegister();
305 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
306 SALOMEDS::AttributePixMap_wrap pm = anAttr;
307 pm->SetPixMap( thePixMap );
309 if ( !theSelectable ) {
310 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
311 SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
312 selAttr->SetSelectable( false );
315 // add object to the use case tree
316 // (to support tree representation customization and drag-n-drop)
317 if ( !CORBA::is_nil( objAfter ) ) {
318 useCaseBuilder->InsertBefore( SO, objAfter ); // insert at given tag
319 } else if ( !useCaseBuilder->IsUseCaseNode( SO ) ) {
320 useCaseBuilder->AppendTo( theFatherObject, SO ); // append to the end of list
326 //=======================================================================
329 //=======================================================================
331 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
333 const char* theDefaultName)
335 if ( !theSObject->_is_nil() ) {
336 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
337 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
338 SALOMEDS::GenericAttribute_wrap anAttr =
339 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
340 SALOMEDS::AttributeName_wrap aNameAttr = anAttr;
341 if ( theName && theName[0] ) {
342 std::string name( theName ); // trim trailing white spaces
343 for ( size_t i = name.size()-1; i > 0; --i )
344 if ( isspace( name[i] )) name[i] = '\0';
346 aNameAttr->SetValue( name.c_str() );
350 CORBA::String_var curName = aNameAttr->Value();
351 if ( strlen( curName.in() ) == 0 ) {
352 SMESH_Comment aName(theDefaultName);
353 aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
359 //=======================================================================
360 //function : SetPixMap
362 //=======================================================================
364 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
365 const char* thePixMap)
367 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
369 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
370 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
371 SALOMEDS::GenericAttribute_wrap anAttr =
372 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
373 SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
374 aPMAttr->SetPixMap( thePixMap );
378 //=======================================================================
379 //function : addReference
381 //=======================================================================
383 static void addReference (SALOMEDS::Study_ptr theStudy,
384 SALOMEDS::SObject_ptr theSObject,
385 CORBA::Object_ptr theToObject,
388 SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
389 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
390 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
391 SALOMEDS::SObject_wrap aReferenceSO;
393 // check if the reference to theToObject already exists
394 // and find a free label for the reference object
395 bool isReferred = false;
397 SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
398 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
399 SALOMEDS::SObject_wrap curSO = anIter->Value();
400 if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
401 CORBA::String_var refEntry = aReferenceSO->GetID();
402 CORBA::String_var toEntry = aToObjSO->GetID();
403 if ( strcmp( refEntry, toEntry ) == 0 )
406 else if ( !theTag ) {
407 SALOMEDS::GenericAttribute_wrap anAttr;
408 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
417 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
419 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
420 // add reference to the use case tree
421 // (to support tree representation customization and drag-n-drop)
422 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
423 useCaseBuilder->AppendTo( aReferenceSO->GetFather(), aReferenceSO );
425 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
429 //=============================================================================
431 * SMESH_Gen_i::PublishInStudy
433 * Publish object in the study
435 //=============================================================================
437 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
438 SALOMEDS::SObject_ptr theSObject,
439 CORBA::Object_ptr theIOR,
441 throw (SALOME::SALOME_Exception)
443 Unexpect aCatch(SALOME_SalomeException);
444 SALOMEDS::SObject_wrap aSO;
445 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
447 if(MYDEBUG) MESSAGE("PublishInStudy");
450 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
451 if( !aMesh->_is_nil() )
452 aSO = PublishMesh( theStudy, aMesh, theName );
454 // Publishing a sub-mesh
455 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
456 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
457 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
458 aMesh = aSubMesh->GetFather();
459 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
462 // Publishing a hypothesis or algorithm
463 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
464 if ( aSO->_is_nil() && !aHyp->_is_nil() )
465 aSO = PublishHypothesis( theStudy, aHyp );
467 // Publishing a group
468 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
469 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
470 GEOM::GEOM_Object_var aShapeObject;
471 aMesh = aGroup->GetMesh();
472 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
474 if(MYDEBUG) MESSAGE("PublishInStudy_END");
479 //=======================================================================
480 //function : PublishComponent
482 //=======================================================================
484 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
486 if ( CORBA::is_nil( theStudy ))
487 return SALOMEDS::SComponent::_nil();
488 if(MYDEBUG) MESSAGE("PublishComponent");
490 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
491 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
493 CORBA::String_var compDataType = ComponentDataType();
494 SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
495 if ( !CORBA::is_nil( father ) ) {
496 // check that the component is added to the use case browser
497 if ( !useCaseBuilder->IsUseCaseNode( father ) ) {
498 useCaseBuilder->SetRootCurrent();
499 useCaseBuilder->Append( father ); // component object is added as the top level item
501 return father._retn();
504 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
505 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
506 if ( CORBA::is_nil( aCat ) )
507 return father._retn();
509 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
510 if ( CORBA::is_nil( aComp ) )
511 return father._retn();
513 SALOMEDS::GenericAttribute_wrap anAttr;
514 SALOMEDS::AttributePixMap_wrap aPixmap;
516 father = aStudyBuilder->NewComponent( compDataType.in() );
517 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
518 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
520 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
521 CORBA::String_var userName = aComp->componentusername();
522 SetName( father, userName.in(), "MESH" );
523 // add component to the use case tree
524 // (to support tree representation customization and drag-n-drop)
525 useCaseBuilder->SetRootCurrent();
526 useCaseBuilder->Append( father ); // component object is added as the top level item
527 if(MYDEBUG) MESSAGE("PublishComponent--END");
529 return father._retn();
532 //=======================================================================
533 //function : PublishMesh
535 //=======================================================================
537 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
538 SMESH::SMESH_Mesh_ptr theMesh,
541 if ( CORBA::is_nil( theStudy ) ||
542 CORBA::is_nil( theMesh ))
543 return SALOMEDS::SComponent::_nil();
544 if(MYDEBUG) MESSAGE("PublishMesh--IN");
546 // find or publish a mesh
548 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
549 if ( aMeshSO->_is_nil() )
551 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
552 if ( father->_is_nil() )
553 return aMeshSO._retn();
555 // Find correct free tag
556 long aTag = father->GetLastChildTag();
557 if ( aTag <= GetAlgorithmsRootTag() )
558 aTag = GetAlgorithmsRootTag() + 1;
562 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
563 if ( aMeshSO->_is_nil() )
564 return aMeshSO._retn();
566 SetName( aMeshSO, theName, "Mesh" );
568 // Add shape reference
570 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
571 if ( !CORBA::is_nil( aShapeObject )) {
572 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
574 // Publish global hypotheses
576 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
577 for ( int i = 0; i < hypList->length(); i++ )
579 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
580 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
581 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
587 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
589 return aMeshSO._retn();
590 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
591 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
592 for ( ; subIt != subMap.end(); subIt++ ) {
593 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
594 if ( !CORBA::is_nil( aSubMesh )) {
595 aShapeObject = aSubMesh->GetSubShape();
596 SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
601 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
602 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
603 for ( ; it != grMap.end(); it++ )
605 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
606 if ( !aGroup->_is_nil() ) {
607 GEOM::GEOM_Object_var aShapeObj;
608 SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
609 if ( !aGeomGroup->_is_nil() )
610 aShapeObj = aGeomGroup->GetShape();
611 SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
615 if(MYDEBUG) MESSAGE("PublishMesh_END");
616 return aMeshSO._retn();
619 //=======================================================================
620 //function : PublishSubMesh
622 //=======================================================================
624 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
625 SMESH::SMESH_Mesh_ptr theMesh,
626 SMESH::SMESH_subMesh_ptr theSubMesh,
627 GEOM::GEOM_Object_ptr theShapeObject,
630 if (theStudy->_is_nil() || theMesh->_is_nil() ||
631 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
632 return SALOMEDS::SObject::_nil();
634 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
635 if ( aSubMeshSO->_is_nil() )
637 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
638 if ( aMeshSO->_is_nil() ) {
639 aMeshSO = PublishMesh( theStudy, theMesh );
640 if ( aMeshSO->_is_nil())
641 return SALOMEDS::SObject::_nil();
643 // Find submesh sub-tree tag
645 const char* aRootName = "";
646 switch ( theShapeObject->GetShapeType() ) {
648 aRootTag = GetSubMeshOnVertexTag();
649 aRootName = "SubMeshes on Vertex";
652 aRootTag = GetSubMeshOnEdgeTag();
653 aRootName = "SubMeshes on Edge";
656 aRootTag = GetSubMeshOnWireTag();
657 aRootName = "SubMeshes on Wire";
660 aRootTag = GetSubMeshOnFaceTag();
661 aRootName = "SubMeshes on Face";
664 aRootTag = GetSubMeshOnShellTag();
665 aRootName = "SubMeshes on Shell";
668 aRootTag = GetSubMeshOnSolidTag();
669 aRootName = "SubMeshes on Solid";
672 aRootTag = GetSubMeshOnCompoundTag();
673 aRootName = "SubMeshes on Compound";
677 // Find or create submesh root
678 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
679 aMeshSO, aRootTag, 0, false );
680 if ( aRootSO->_is_nil() )
681 return aSubMeshSO._retn();
683 SetName( aRootSO, aRootName );
685 // Add new submesh to corresponding sub-tree
686 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
687 const int isEmpty = ( elemTypes->length() == 0 );
688 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
689 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
690 if ( aSubMeshSO->_is_nil() )
691 return aSubMeshSO._retn();
693 SetName( aSubMeshSO, theName, "SubMesh" );
695 // Add reference to theShapeObject
697 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
699 // Publish hypothesis
701 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
702 for ( int i = 0; i < hypList->length(); i++ ) {
703 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
704 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
705 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
708 return aSubMeshSO._retn();
711 //=======================================================================
712 //function : PublishGroup
714 //=======================================================================
716 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
717 SMESH::SMESH_Mesh_ptr theMesh,
718 SMESH::SMESH_GroupBase_ptr theGroup,
719 GEOM::GEOM_Object_ptr theShapeObject,
722 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
723 return SALOMEDS::SObject::_nil();
725 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
726 if ( aGroupSO->_is_nil() )
728 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
729 if ( aMeshSO->_is_nil() ) {
730 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
731 if ( aMeshSO->_is_nil())
732 return SALOMEDS::SObject::_nil();
734 int aType = (int)theGroup->GetType();
735 const char* aRootNames[] = {
736 "Compound Groups", "Groups of Nodes", "Groups of Edges",
737 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
740 // Currently, groups with heterogenous content are not supported
741 if ( aType != SMESH::ALL )
743 long aRootTag = GetNodeGroupsTag() + aType - 1;
745 // Find or create groups root
746 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
747 aMeshSO, aRootTag, 0, false );
748 if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
750 if ( aType < sizeof(aRootNames)/sizeof(char*) )
751 SetName( aRootSO, aRootNames[aType] );
753 // Add new group to corresponding sub-tree
754 SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
755 int isEmpty = ( elemTypes->length() == 0 );
756 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
757 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
759 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
761 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
763 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
764 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
765 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
767 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
769 if ( aGroupSO->_is_nil() )
770 return aGroupSO._retn();
773 SetName( aGroupSO, theName, "Group" );
775 //Add reference to geometry
776 if ( !theShapeObject->_is_nil() )
777 addReference( theStudy, aGroupSO, theShapeObject, 1 );
779 return aGroupSO._retn();
782 //=======================================================================
783 //function : PublishHypothesis
785 //=======================================================================
787 SALOMEDS::SObject_ptr
788 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
789 SMESH::SMESH_Hypothesis_ptr theHyp,
792 if(MYDEBUG) MESSAGE("PublishHypothesis")
793 if (theStudy->_is_nil() || theHyp->_is_nil())
794 return SALOMEDS::SObject::_nil();
796 CORBA::String_var hypType = theHyp->GetName();
798 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
799 if ( aHypSO->_is_nil() )
801 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
802 if ( father->_is_nil() )
803 return aHypSO._retn();
805 //Find or Create Hypothesis root
806 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
807 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
808 SALOMEDS::SObject_wrap aRootSO =
809 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
810 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
811 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
813 // Add New Hypothesis
814 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
815 aPmName += hypType.in();
816 // prepend plugin name to pixmap name
817 string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
818 if ( pluginName != "StdMeshers" )
819 aPmName = pluginName + "::" + aPmName;
820 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
823 SetName( aHypSO, theName, hypType.in() );
825 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
826 return aHypSO._retn();
829 //=======================================================================
830 //function : GetMeshOrSubmeshByShape
832 //=======================================================================
834 SALOMEDS::SObject_ptr
835 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
836 SMESH::SMESH_Mesh_ptr theMesh,
837 GEOM::GEOM_Object_ptr theShape)
839 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
840 SALOMEDS::SObject_wrap aMeshOrSubMesh;
841 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
842 return aMeshOrSubMesh._retn();
845 if(theMesh->HasShapeToMesh())
846 aShape = GeomObjectToShape( theShape );
848 aShape = SMESH_Mesh::PseudoShape();
850 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
852 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
853 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
854 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
855 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
857 int shapeID = meshDS->ShapeToIndex( aShape );
858 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
859 if ( !aSubMesh->_is_nil() )
860 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
863 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
864 return aMeshOrSubMesh._retn();
867 //=======================================================================
868 //function : AddHypothesisToShape
870 //=======================================================================
872 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
873 SMESH::SMESH_Mesh_ptr theMesh,
874 GEOM::GEOM_Object_ptr theShape,
875 SMESH::SMESH_Hypothesis_ptr theHyp)
877 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
878 if (theStudy->_is_nil() || theMesh->_is_nil() ||
879 theHyp->_is_nil() || (theShape->_is_nil()
880 && theMesh->HasShapeToMesh()) )
883 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
884 if ( aMeshSO->_is_nil() )
885 aMeshSO = PublishMesh( theStudy, theMesh );
886 SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
887 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
890 // Find a mesh or submesh refering to theShape
891 SALOMEDS::SObject_wrap aMeshOrSubMesh =
892 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
893 if ( aMeshOrSubMesh->_is_nil() )
896 TopoDS_Shape aShape = GeomObjectToShape( theShape );
897 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
898 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
899 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
900 int shapeID = meshDS->ShapeToIndex( aShape );
901 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
902 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
904 if ( aMeshOrSubMesh->_is_nil() )
908 //Find or Create Applied Hypothesis root
909 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
910 SALOMEDS::SObject_wrap AHR =
911 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
912 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
913 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
914 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
916 addReference( theStudy, AHR, theHyp );
918 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
922 //=======================================================================
923 //function : RemoveHypothesisFromShape
925 //=======================================================================
927 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
928 SMESH::SMESH_Mesh_ptr theMesh,
929 GEOM::GEOM_Object_ptr theShape,
930 SMESH::SMESH_Hypothesis_ptr theHyp)
932 if (theStudy->_is_nil() || theMesh->_is_nil() ||
933 theHyp->_is_nil() || (theShape->_is_nil()
934 && theMesh->HasShapeToMesh()))
937 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
938 if ( aHypSO->_is_nil() )
941 CORBA::String_var hypEntry = aHypSO->GetID();
943 // Find a mesh or submesh refering to theShape
944 SALOMEDS::SObject_wrap aMeshOrSubMesh =
945 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
946 if ( aMeshOrSubMesh->_is_nil() )
949 // Find and remove a reference to aHypSO
950 SALOMEDS::SObject_wrap aRef, anObj;
951 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
953 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
955 if (anObj->ReferencedObject( aRef.inout() ))
957 CORBA::String_var refEntry = aRef->GetID();
958 found = ( strcmp( refEntry, hypEntry ) == 0 );
962 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
963 builder->RemoveObject( anObj );
970 //================================================================================
972 * \brief Stores names of variables that WILL be passes as parameters when calling
973 * some method of a given object.
974 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
975 * \param [in] theParameters - a string contating parameters separated by ':'.
977 //================================================================================
979 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
981 SALOMEDS::Study_var aStudy = GetCurrentStudy();
982 if ( aStudy->_is_nil() )
985 // find variable names within theParameters
988 myLastParameters.clear();
989 myLastParamIndex.clear(); /* vector holding indices of virables within the string
990 of all varibles used for theObject */
992 int pos = 0, prevPos = 0, len = strlen( theParameters );
993 if ( len == 0 ) return;
996 if ( pos == len || theParameters[pos] == ':' )
1000 string val( theParameters + prevPos, theParameters + pos );
1001 if ( !aStudy->IsVariable( val.c_str() ))
1003 myLastParameters.push_back( val );
1004 nbVars += (! myLastParameters.back().empty() );
1008 myLastParameters.push_back("");
1019 // (1) variable names in the string of all varibles used for theObject and
1020 // (2) indices of found variables in myLastParamIndex.
1022 // remember theObject
1023 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
1024 if ( aSObj->_is_nil() )
1026 CORBA::String_var anObjEntry = aSObj->GetID();
1027 myLastObj = anObjEntry.in();
1029 // get a string of variable names
1030 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1031 SALOMEDS::GenericAttribute_wrap anAttr =
1032 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1033 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1034 CORBA::String_var oldVars = aStringAttr->Value();
1035 std::string varStr = oldVars.in();
1037 // add new variables and find indices of variables
1038 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1041 if ( !myLastParameters[i].empty() )
1043 // find index of myLastParameters[i] in varStr
1045 bool varFound = false;
1047 // varStr can be "A|B::C;*=2|D"
1048 const std::string separators(":|;*=");
1049 while ( pos < varStr.size() && !varFound )
1052 while ( separators.find( varStr[ pos ]) != std::string::npos )
1053 if ( ++pos >= varStr.size() )
1055 // skip repetition number following '='
1056 if ( varStr[ pos-1 ] == '=' )
1058 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1060 continue; // to skip next separator
1062 // compare variable name
1063 if ( pos < varStr.size() )
1065 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1066 // same string begining but is length same?
1067 ( pos + myLastParameters[i].size() >= varStr.size() ||
1068 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1070 varIndex = curIndex;
1072 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1079 varStr += ":" + myLastParameters[i];
1080 varIndex = curIndex;
1083 myLastParamIndex.push_back( varIndex );
1085 aStringAttr->SetValue( varStr.c_str() );
1088 //================================================================================
1090 * \brief Return all variables used to create an object
1091 * \param [in] theObjectEntry - an object entry in the current study
1092 * \return std::vector< std::string > - all variable names (or values of removed variables)
1094 //================================================================================
1096 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1098 std::vector< std::string > varNames;
1099 if ( myCurrentStudy->_is_nil() )
1102 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1103 if ( myCurrentStudy->_is_nil() )
1106 // get a string of variable names
1107 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1108 SALOMEDS::GenericAttribute_wrap anAttr =
1109 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1110 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1111 CORBA::String_var oldVars = aStringAttr->Value();
1112 std::string varStr = oldVars.in();
1114 // separate variables within varStr;
1115 // varStr can be "A|B::C;*=2|D"
1117 const std::string separators(":|;*=");
1118 while ( pos < varStr.size() )
1121 pos = varStr.find_first_not_of( separators, pos );
1122 // while ( separators.find( varStr[ pos ]) != std::string::npos )
1123 // if ( ++pos >= varStr.size() )
1125 // skip repetition number following '='
1126 if ( varStr[ pos-1 ] == '=' )
1128 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1130 continue; // to skip next separator
1132 // store variable name
1133 if ( pos < varStr.size() )
1135 size_t pos2 = varStr.find_first_of( separators, pos );
1136 varNames.push_back( varStr.substr( pos, pos2 - pos));
1143 //=======================================================================
1144 //function : ParseParameters
1145 //purpose : Replace variables by their values
1146 //=======================================================================
1147 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1149 // //const char* aParameters = theParameters;
1150 // // const char* aParameters = CORBA::string_dup(theParameters);
1151 // TCollection_AsciiString anInputParams;
1152 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1153 // if( !aStudy->_is_nil() ) {
1154 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1155 // // for(int j=0;j<aSections->length();j++) {
1156 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1157 // // for(int i=0;i<aVars.length();i++ ) {
1158 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1159 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1160 // // if(i != aVars.length()-1)
1161 // // anInputParams+=":";
1163 // // if(j!=aSections->length()-1)
1164 // // anInputParams+="|";
1166 // TCollection_AsciiString paramStr( theParameters );
1167 // int beg = 0, end;
1168 // char sep, *pParams = (char*)paramStr.ToCString();
1169 // while ( beg < paramStr.Length() )
1172 // while ( ++end < paramStr.Length() )
1173 // if ( pParams[end] == ':' || pParams[end] == '|')
1175 // if ( end < paramStr.Length())
1177 // sep = pParams[end];
1178 // pParams[end] = '\0';
1180 // if ( aStudy->IsVariable( pParams+beg ))
1181 // anInputParams += pParams+beg;
1182 // if ( end < paramStr.Length() )
1183 // anInputParams += sep;
1189 // return CORBA::string_dup(anInputParams.ToCString());
1192 //=======================================================================
1193 //function : GetParameters
1195 //=======================================================================
1197 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1199 CORBA::String_var aResult("");
1201 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1202 if ( !aSObj->_is_nil() )
1204 SALOMEDS::GenericAttribute_wrap attr;
1205 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1207 SALOMEDS::AttributeString_wrap strAttr = attr;
1208 aResult = strAttr->Value();
1211 return aResult._retn();