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;
268 bool isNewSO = false;
272 SO = aStudyBuilder->NewObject( theFatherObject );
275 else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
277 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
280 // define the next tag after given one in the data tree to insert SObject
281 SALOMEDS::SObject_wrap curObj;
282 if ( theFatherObject->GetLastChildTag() > theTag )
284 SALOMEDS::UseCaseIterator_wrap anUseCaseIter = useCaseBuilder->GetUseCaseIterator(theFatherObject);
285 for ( ; anUseCaseIter->More(); anUseCaseIter->Next() ) {
286 curObj = anUseCaseIter->Value();
287 if ( curObj->Tag() > theTag ) {
296 SALOMEDS::GenericAttribute_wrap anAttr;
297 if ( !CORBA::is_nil( theIOR )) {
298 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
299 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
300 SALOMEDS::AttributeIOR_wrap iorAttr = anAttr;
301 iorAttr->SetValue( objStr.in() );
303 SALOME::GenericObj_var genObj = SALOME::GenericObj::_narrow( theIOR );
304 if ( !genObj->_is_nil() )
305 genObj->UnRegister();
308 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
309 SALOMEDS::AttributePixMap_wrap pm = anAttr;
310 pm->SetPixMap( thePixMap );
312 if ( !theSelectable ) {
313 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
314 SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
315 selAttr->SetSelectable( false );
318 // add object to the use case tree
319 // (to support tree representation customization and drag-n-drop)
322 if ( !CORBA::is_nil( objAfter ) )
323 useCaseBuilder->InsertBefore( SO, objAfter ); // insert at given tag
324 else if ( !useCaseBuilder->IsUseCaseNode( SO ) )
325 useCaseBuilder->AppendTo( theFatherObject, SO ); // append to the end of list
330 //=======================================================================
333 //=======================================================================
335 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
337 const char* theDefaultName)
339 if ( !theSObject->_is_nil() ) {
340 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
341 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
342 SALOMEDS::GenericAttribute_wrap anAttr =
343 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
344 SALOMEDS::AttributeName_wrap aNameAttr = anAttr;
345 if ( theName && theName[0] ) {
346 std::string name( theName ); // trim trailing white spaces
347 for ( size_t i = name.size()-1; i > 0; --i )
348 if ( isspace( name[i] )) name[i] = '\0';
350 aNameAttr->SetValue( name.c_str() );
354 CORBA::String_var curName = aNameAttr->Value();
355 if ( strlen( curName.in() ) == 0 ) {
356 SMESH_Comment aName(theDefaultName);
357 aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
363 //=======================================================================
364 //function : SetPixMap
366 //=======================================================================
368 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
369 const char* thePixMap)
371 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
373 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
374 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
375 SALOMEDS::GenericAttribute_wrap anAttr =
376 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
377 SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
378 aPMAttr->SetPixMap( thePixMap );
382 //=======================================================================
383 //function : addReference
385 //=======================================================================
387 static void addReference (SALOMEDS::Study_ptr theStudy,
388 SALOMEDS::SObject_ptr theSObject,
389 CORBA::Object_ptr theToObject,
392 SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
393 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
394 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
395 SALOMEDS::SObject_wrap aReferenceSO;
397 // check if the reference to theToObject already exists
398 // and find a free label for the reference object
399 bool isReferred = false;
401 SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
402 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
403 SALOMEDS::SObject_wrap curSO = anIter->Value();
404 if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
405 CORBA::String_var refEntry = aReferenceSO->GetID();
406 CORBA::String_var toEntry = aToObjSO->GetID();
407 if ( strcmp( refEntry, toEntry ) == 0 )
410 else if ( !theTag ) {
411 SALOMEDS::GenericAttribute_wrap anAttr;
412 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
421 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
422 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
424 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
426 // add reference to the use case tree
427 // (to support tree representation customization and drag-n-drop)
428 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
429 SALOMEDS::UseCaseIterator_wrap useCaseIter = useCaseBuilder->GetUseCaseIterator(theSObject);
430 for ( ; useCaseIter->More(); useCaseIter->Next() )
432 SALOMEDS::SObject_wrap curSO = useCaseIter->Value();
433 if ( curSO->Tag() == theTag )
436 useCaseBuilder->AppendTo( theSObject, aReferenceSO );
440 //=============================================================================
442 * SMESH_Gen_i::PublishInStudy
444 * Publish object in the study
446 //=============================================================================
448 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
449 SALOMEDS::SObject_ptr theSObject,
450 CORBA::Object_ptr theIOR,
452 throw (SALOME::SALOME_Exception)
454 Unexpect aCatch(SALOME_SalomeException);
455 SALOMEDS::SObject_wrap aSO;
456 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
458 if(MYDEBUG) MESSAGE("PublishInStudy");
461 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
462 if( !aMesh->_is_nil() )
463 aSO = PublishMesh( theStudy, aMesh, theName );
465 // Publishing a sub-mesh
466 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
467 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
468 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
469 aMesh = aSubMesh->GetFather();
470 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
473 // Publishing a hypothesis or algorithm
474 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
475 if ( aSO->_is_nil() && !aHyp->_is_nil() )
476 aSO = PublishHypothesis( theStudy, aHyp );
478 // Publishing a group
479 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
480 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
481 GEOM::GEOM_Object_var aShapeObject;
482 aMesh = aGroup->GetMesh();
483 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
485 if(MYDEBUG) MESSAGE("PublishInStudy_END");
490 //=======================================================================
491 //function : PublishComponent
493 //=======================================================================
495 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
497 if ( CORBA::is_nil( theStudy ))
498 return SALOMEDS::SComponent::_nil();
499 if(MYDEBUG) MESSAGE("PublishComponent");
501 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
502 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
504 CORBA::String_var compDataType = ComponentDataType();
505 SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
506 if ( !CORBA::is_nil( father ) ) {
507 // check that the component is added to the use case browser
508 if ( !useCaseBuilder->IsUseCaseNode( father ) ) {
509 useCaseBuilder->SetRootCurrent();
510 useCaseBuilder->Append( father ); // component object is added as the top level item
512 return father._retn();
515 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
516 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
517 if ( CORBA::is_nil( aCat ) )
518 return father._retn();
520 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
521 if ( CORBA::is_nil( aComp ) )
522 return father._retn();
524 SALOMEDS::GenericAttribute_wrap anAttr;
525 SALOMEDS::AttributePixMap_wrap aPixmap;
527 father = aStudyBuilder->NewComponent( compDataType.in() );
528 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
529 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
531 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
532 CORBA::String_var userName = aComp->componentusername();
533 SetName( father, userName.in(), "MESH" );
534 // add component to the use case tree
535 // (to support tree representation customization and drag-n-drop)
536 useCaseBuilder->SetRootCurrent();
537 useCaseBuilder->Append( father ); // component object is added as the top level item
538 if(MYDEBUG) MESSAGE("PublishComponent--END");
540 return father._retn();
543 //=======================================================================
544 //function : PublishMesh
546 //=======================================================================
548 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
549 SMESH::SMESH_Mesh_ptr theMesh,
552 if ( CORBA::is_nil( theStudy ) ||
553 CORBA::is_nil( theMesh ))
554 return SALOMEDS::SComponent::_nil();
555 if(MYDEBUG) MESSAGE("PublishMesh--IN");
557 // find or publish a mesh
559 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
560 if ( aMeshSO->_is_nil() )
562 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
563 if ( father->_is_nil() )
564 return aMeshSO._retn();
566 // Find correct free tag
567 long aTag = father->GetLastChildTag();
568 if ( aTag <= GetAlgorithmsRootTag() )
569 aTag = GetAlgorithmsRootTag() + 1;
573 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
574 if ( aMeshSO->_is_nil() )
575 return aMeshSO._retn();
577 SetName( aMeshSO, theName, "Mesh" );
579 // Add shape reference
581 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
582 if ( !CORBA::is_nil( aShapeObject )) {
583 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
585 // Publish global hypotheses
587 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
588 for ( int i = 0; i < hypList->length(); i++ )
590 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
591 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
592 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
598 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
600 return aMeshSO._retn();
601 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
602 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
603 for ( ; subIt != subMap.end(); subIt++ ) {
604 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
605 if ( !CORBA::is_nil( aSubMesh )) {
606 aShapeObject = aSubMesh->GetSubShape();
607 SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
612 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
613 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
614 for ( ; it != grMap.end(); it++ )
616 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
617 if ( !aGroup->_is_nil() ) {
618 GEOM::GEOM_Object_var aShapeObj;
619 SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
620 if ( !aGeomGroup->_is_nil() )
621 aShapeObj = aGeomGroup->GetShape();
622 SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
626 if(MYDEBUG) MESSAGE("PublishMesh_END");
627 return aMeshSO._retn();
630 //=======================================================================
631 //function : PublishSubMesh
633 //=======================================================================
635 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
636 SMESH::SMESH_Mesh_ptr theMesh,
637 SMESH::SMESH_subMesh_ptr theSubMesh,
638 GEOM::GEOM_Object_ptr theShapeObject,
641 if (theStudy->_is_nil() || theMesh->_is_nil() ||
642 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
643 return SALOMEDS::SObject::_nil();
645 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
646 if ( aSubMeshSO->_is_nil() )
648 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
649 if ( aMeshSO->_is_nil() ) {
650 aMeshSO = PublishMesh( theStudy, theMesh );
651 if ( aMeshSO->_is_nil())
652 return SALOMEDS::SObject::_nil();
654 // Find submesh sub-tree tag
656 const char* aRootName = "";
657 switch ( theShapeObject->GetShapeType() ) {
659 aRootTag = GetSubMeshOnVertexTag();
660 aRootName = "SubMeshes on Vertex";
663 aRootTag = GetSubMeshOnEdgeTag();
664 aRootName = "SubMeshes on Edge";
667 aRootTag = GetSubMeshOnWireTag();
668 aRootName = "SubMeshes on Wire";
671 aRootTag = GetSubMeshOnFaceTag();
672 aRootName = "SubMeshes on Face";
675 aRootTag = GetSubMeshOnShellTag();
676 aRootName = "SubMeshes on Shell";
679 aRootTag = GetSubMeshOnSolidTag();
680 aRootName = "SubMeshes on Solid";
683 aRootTag = GetSubMeshOnCompoundTag();
684 aRootName = "SubMeshes on Compound";
688 // Find or create submesh root
689 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
690 aMeshSO, aRootTag, 0, false );
691 if ( aRootSO->_is_nil() )
692 return aSubMeshSO._retn();
694 SetName( aRootSO, aRootName );
696 // Add new submesh to corresponding sub-tree
697 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
698 const int isEmpty = ( elemTypes->length() == 0 );
699 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
700 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
701 if ( aSubMeshSO->_is_nil() )
702 return aSubMeshSO._retn();
704 SetName( aSubMeshSO, theName, "SubMesh" );
706 // Add reference to theShapeObject
708 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
710 // Publish hypothesis
712 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
713 for ( int i = 0; i < hypList->length(); i++ ) {
714 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
715 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
716 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
719 return aSubMeshSO._retn();
722 //=======================================================================
723 //function : PublishGroup
725 //=======================================================================
727 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
728 SMESH::SMESH_Mesh_ptr theMesh,
729 SMESH::SMESH_GroupBase_ptr theGroup,
730 GEOM::GEOM_Object_ptr theShapeObject,
733 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
734 return SALOMEDS::SObject::_nil();
736 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
737 if ( aGroupSO->_is_nil() )
739 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
740 if ( aMeshSO->_is_nil() ) {
741 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
742 if ( aMeshSO->_is_nil())
743 return SALOMEDS::SObject::_nil();
745 int aType = (int)theGroup->GetType();
746 const char* aRootNames[] = {
747 "Compound Groups", "Groups of Nodes", "Groups of Edges",
748 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
751 // Currently, groups with heterogenous content are not supported
752 if ( aType != SMESH::ALL )
754 long aRootTag = GetNodeGroupsTag() + aType - 1;
756 // Find or create groups root
757 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
758 aMeshSO, aRootTag, 0, false );
759 if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
761 if ( aType < sizeof(aRootNames)/sizeof(char*) )
762 SetName( aRootSO, aRootNames[aType] );
764 // Add new group to corresponding sub-tree
765 SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
766 int isEmpty = ( elemTypes->length() == 0 );
767 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
768 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
770 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
772 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
774 if ( theGroup->GetType() == SMESH::NODE )
775 isEmpty = ( theMesh->NbNodes() == 0 );
778 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
779 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
780 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
783 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
785 if ( aGroupSO->_is_nil() )
786 return aGroupSO._retn();
789 SetName( aGroupSO, theName, "Group" );
791 //Add reference to geometry
792 if ( !theShapeObject->_is_nil() )
793 addReference( theStudy, aGroupSO, theShapeObject, 1 );
795 return aGroupSO._retn();
798 //=======================================================================
799 //function : PublishHypothesis
801 //=======================================================================
803 SALOMEDS::SObject_ptr
804 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
805 SMESH::SMESH_Hypothesis_ptr theHyp,
808 if(MYDEBUG) MESSAGE("PublishHypothesis")
809 if (theStudy->_is_nil() || theHyp->_is_nil())
810 return SALOMEDS::SObject::_nil();
812 CORBA::String_var hypType = theHyp->GetName();
814 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
815 if ( aHypSO->_is_nil() )
817 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
818 if ( father->_is_nil() )
819 return aHypSO._retn();
821 //Find or Create Hypothesis root
822 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
823 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
824 SALOMEDS::SObject_wrap aRootSO =
825 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
826 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
827 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
829 // Add New Hypothesis
830 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
831 aPmName += hypType.in();
832 // prepend plugin name to pixmap name
833 string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
834 if ( pluginName != "StdMeshers" )
835 aPmName = pluginName + "::" + aPmName;
836 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
839 SetName( aHypSO, theName, hypType.in() );
841 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
842 return aHypSO._retn();
845 //=======================================================================
846 //function : GetMeshOrSubmeshByShape
848 //=======================================================================
850 SALOMEDS::SObject_ptr
851 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
852 SMESH::SMESH_Mesh_ptr theMesh,
853 GEOM::GEOM_Object_ptr theShape)
855 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
856 SALOMEDS::SObject_wrap aMeshOrSubMesh;
857 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
858 return aMeshOrSubMesh._retn();
861 if(theMesh->HasShapeToMesh())
862 aShape = GeomObjectToShape( theShape );
864 aShape = SMESH_Mesh::PseudoShape();
866 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
868 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
869 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
870 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
871 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
873 int shapeID = meshDS->ShapeToIndex( aShape );
874 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
875 if ( !aSubMesh->_is_nil() )
876 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
879 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
880 return aMeshOrSubMesh._retn();
883 //=======================================================================
884 //function : AddHypothesisToShape
886 //=======================================================================
888 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
889 SMESH::SMESH_Mesh_ptr theMesh,
890 GEOM::GEOM_Object_ptr theShape,
891 SMESH::SMESH_Hypothesis_ptr theHyp)
893 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
894 if (theStudy->_is_nil() || theMesh->_is_nil() ||
895 theHyp->_is_nil() || (theShape->_is_nil()
896 && theMesh->HasShapeToMesh()) )
899 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
900 if ( aMeshSO->_is_nil() )
901 aMeshSO = PublishMesh( theStudy, theMesh );
902 SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
903 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
906 // Find a mesh or submesh refering to theShape
907 SALOMEDS::SObject_wrap aMeshOrSubMesh =
908 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
909 if ( aMeshOrSubMesh->_is_nil() )
912 TopoDS_Shape aShape = GeomObjectToShape( theShape );
913 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
914 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
915 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
916 int shapeID = meshDS->ShapeToIndex( aShape );
917 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
918 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
920 if ( aMeshOrSubMesh->_is_nil() )
924 //Find or Create Applied Hypothesis root
925 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
926 SALOMEDS::SObject_wrap AHR =
927 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
928 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
929 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
930 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
932 addReference( theStudy, AHR, theHyp );
934 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
938 //=======================================================================
939 //function : RemoveHypothesisFromShape
941 //=======================================================================
943 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
944 SMESH::SMESH_Mesh_ptr theMesh,
945 GEOM::GEOM_Object_ptr theShape,
946 SMESH::SMESH_Hypothesis_ptr theHyp)
948 if (theStudy->_is_nil() || theMesh->_is_nil() ||
949 theHyp->_is_nil() || (theShape->_is_nil()
950 && theMesh->HasShapeToMesh()))
953 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
954 if ( aHypSO->_is_nil() )
957 CORBA::String_var hypEntry = aHypSO->GetID();
959 // Find a mesh or sub-mesh referring to theShape
960 SALOMEDS::SObject_wrap aMeshOrSubMesh =
961 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
962 if ( aMeshOrSubMesh->_is_nil() )
965 // Find and remove a reference to aHypSO
966 SALOMEDS::SObject_wrap aRef, anObj;
967 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
969 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
971 if (anObj->ReferencedObject( aRef.inout() ))
973 CORBA::String_var refEntry = aRef->GetID();
974 found = ( strcmp( refEntry, hypEntry ) == 0 );
978 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
979 builder->RemoveObject( anObj );
986 //================================================================================
988 * \brief Stores names of variables that WILL be passes as parameters when calling
989 * some method of a given object.
990 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
991 * \param [in] theParameters - a string contating parameters separated by ':'.
993 //================================================================================
995 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
997 SALOMEDS::Study_var aStudy = GetCurrentStudy();
998 if ( aStudy->_is_nil() )
1001 // find variable names within theParameters
1004 myLastParameters.clear();
1005 myLastParamIndex.clear(); /* vector holding indices of virables within the string
1006 of all varibles used for theObject */
1008 int pos = 0, prevPos = 0, len = strlen( theParameters );
1009 if ( len == 0 ) return;
1010 while ( pos <= len )
1012 if ( pos == len || theParameters[pos] == ':' )
1014 if ( prevPos < pos )
1016 string val( theParameters + prevPos, theParameters + pos );
1017 if ( !aStudy->IsVariable( val.c_str() ))
1019 myLastParameters.push_back( val );
1020 nbVars += (! myLastParameters.back().empty() );
1024 myLastParameters.push_back("");
1035 // (1) variable names in the string of all varibles used for theObject and
1036 // (2) indices of found variables in myLastParamIndex.
1038 // remember theObject
1039 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
1040 if ( aSObj->_is_nil() )
1042 CORBA::String_var anObjEntry = aSObj->GetID();
1043 myLastObj = anObjEntry.in();
1045 // get a string of variable names
1046 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1047 SALOMEDS::GenericAttribute_wrap anAttr =
1048 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1049 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1050 CORBA::String_var oldVars = aStringAttr->Value();
1051 std::string varStr = oldVars.in();
1053 // add new variables and find indices of variables
1054 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1057 if ( !myLastParameters[i].empty() )
1059 // find index of myLastParameters[i] in varStr
1061 bool varFound = false;
1063 // varStr can be "A|B::C;*=2|D"
1064 const std::string separators(":|;*=");
1065 while ( pos < varStr.size() && !varFound )
1068 while ( separators.find( varStr[ pos ]) != std::string::npos )
1069 if ( ++pos >= varStr.size() )
1071 // skip repetition number following '='
1072 if ( varStr[ pos-1 ] == '=' )
1074 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1076 continue; // to skip next separator
1078 // compare variable name
1079 if ( pos < varStr.size() )
1081 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1082 // same string begining but is length same?
1083 ( pos + myLastParameters[i].size() >= varStr.size() ||
1084 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1086 varIndex = curIndex;
1088 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1095 varStr += ":" + myLastParameters[i];
1096 varIndex = curIndex;
1099 myLastParamIndex.push_back( varIndex );
1101 aStringAttr->SetValue( varStr.c_str() );
1104 //================================================================================
1106 * \brief Return all variables used to create an object
1107 * \param [in] theObjectEntry - an object entry in the current study
1108 * \return std::vector< std::string > - all variable names (or values of removed variables)
1110 //================================================================================
1112 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1114 std::vector< std::string > varNames;
1115 if ( myCurrentStudy->_is_nil() )
1118 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1119 if ( myCurrentStudy->_is_nil() )
1122 // get a string of variable names
1123 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1124 SALOMEDS::GenericAttribute_wrap anAttr =
1125 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1126 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1127 CORBA::String_var oldVars = aStringAttr->Value();
1128 std::string varStr = oldVars.in();
1130 // separate variables within varStr;
1131 // varStr can be "A|B::C;*=2|D"
1133 const std::string separators(":|;*=");
1134 while ( pos < varStr.size() )
1137 pos = varStr.find_first_not_of( separators, pos );
1138 // while ( separators.find( varStr[ pos ]) != std::string::npos )
1139 // if ( ++pos >= varStr.size() )
1141 // skip repetition number following '='
1142 if ( varStr[ pos-1 ] == '=' )
1144 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1146 continue; // to skip next separator
1148 // store variable name
1149 if ( pos < varStr.size() )
1151 size_t pos2 = varStr.find_first_of( separators, pos );
1152 varNames.push_back( varStr.substr( pos, pos2 - pos));
1159 //=======================================================================
1160 //function : ParseParameters
1161 //purpose : Replace variables by their values
1162 //=======================================================================
1163 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1165 // //const char* aParameters = theParameters;
1166 // // const char* aParameters = CORBA::string_dup(theParameters);
1167 // TCollection_AsciiString anInputParams;
1168 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1169 // if( !aStudy->_is_nil() ) {
1170 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1171 // // for(int j=0;j<aSections->length();j++) {
1172 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1173 // // for(int i=0;i<aVars.length();i++ ) {
1174 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1175 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1176 // // if(i != aVars.length()-1)
1177 // // anInputParams+=":";
1179 // // if(j!=aSections->length()-1)
1180 // // anInputParams+="|";
1182 // TCollection_AsciiString paramStr( theParameters );
1183 // int beg = 0, end;
1184 // char sep, *pParams = (char*)paramStr.ToCString();
1185 // while ( beg < paramStr.Length() )
1188 // while ( ++end < paramStr.Length() )
1189 // if ( pParams[end] == ':' || pParams[end] == '|')
1191 // if ( end < paramStr.Length())
1193 // sep = pParams[end];
1194 // pParams[end] = '\0';
1196 // if ( aStudy->IsVariable( pParams+beg ))
1197 // anInputParams += pParams+beg;
1198 // if ( end < paramStr.Length() )
1199 // anInputParams += sep;
1205 // return CORBA::string_dup(anInputParams.ToCString());
1208 //=======================================================================
1209 //function : GetParameters
1211 //=======================================================================
1213 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1215 CORBA::String_var aResult("");
1217 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1218 if ( !aSObj->_is_nil() )
1220 SALOMEDS::GenericAttribute_wrap attr;
1221 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1223 SALOMEDS::AttributeString_wrap strAttr = attr;
1224 aResult = strAttr->Value();
1227 return aResult._retn();