1 // Copyright (C) 2007-2016 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 "SMESHDS_Mesh.hxx"
30 #include "SMESH_Algo_i.hxx"
31 #include "SMESH_Comment.hxx"
32 #include "SMESH_Group_i.hxx"
33 #include "SMESH_Hypothesis_i.hxx"
34 #include "SMESH_Mesh_i.hxx"
35 #include "SMESH_subMesh_i.hxx"
37 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
39 #include <utilities.h>
40 #include <Utils_ExceptHandlers.hxx>
41 #include <SALOMEDS_wrap.hxx>
42 #include <SALOMEDS_Attributes_wrap.hxx>
44 #include <TCollection_AsciiString.hxx>
45 #include <TopoDS_Solid.hxx>
50 static int MYDEBUG = 0;
52 static int MYDEBUG = 0;
57 //=============================================================================
59 * Get...Tag [ static ]
61 * Methods which determine SMESH data model structure
63 //=============================================================================
65 long SMESH_Gen_i::GetHypothesisRootTag()
67 return SMESH::Tag_HypothesisRoot;
70 long SMESH_Gen_i::GetAlgorithmsRootTag()
72 return SMESH::Tag_AlgorithmsRoot;
75 long SMESH_Gen_i::GetRefOnShapeTag()
77 return SMESH::Tag_RefOnShape;
80 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
82 return SMESH::Tag_RefOnAppliedHypothesis;
85 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
87 return SMESH::Tag_RefOnAppliedAlgorithms;
90 long SMESH_Gen_i::GetSubMeshOnVertexTag()
92 return SMESH::Tag_SubMeshOnVertex;
95 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
97 return SMESH::Tag_SubMeshOnEdge;
100 long SMESH_Gen_i::GetSubMeshOnFaceTag()
102 return SMESH::Tag_SubMeshOnFace;
105 long SMESH_Gen_i::GetSubMeshOnSolidTag()
107 return SMESH::Tag_SubMeshOnSolid;
110 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
112 return SMESH::Tag_SubMeshOnCompound;
115 long SMESH_Gen_i::GetSubMeshOnWireTag()
117 return SMESH::Tag_SubMeshOnWire;
120 long SMESH_Gen_i::GetSubMeshOnShellTag()
122 return SMESH::Tag_SubMeshOnShell;
125 long SMESH_Gen_i::GetNodeGroupsTag()
127 return SMESH::Tag_NodeGroups;
130 long SMESH_Gen_i::GetEdgeGroupsTag()
132 return SMESH::Tag_EdgeGroups;
135 long SMESH_Gen_i::GetFaceGroupsTag()
137 return SMESH::Tag_FaceGroups;
140 long SMESH_Gen_i::GetVolumeGroupsTag()
142 return SMESH::Tag_VolumeGroups;
145 long SMESH_Gen_i::Get0DElementsGroupsTag()
147 return SMESH::Tag_0DElementsGroups;
150 long SMESH_Gen_i::GetBallElementsGroupsTag()
152 return SMESH::Tag_BallElementsGroups;
155 //=============================================================================
157 * SMESH_Gen_i::CanPublishInStudy
159 * Returns true if object can be published in the study
161 //=============================================================================
163 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
165 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
166 if( GetCurrentStudyID() < 0 )
169 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
170 if( !aMesh->_is_nil() )
173 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
174 if( !aSubMesh->_is_nil() )
177 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
178 if( !aHyp->_is_nil() )
181 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
182 if( !aGroup->_is_nil() )
185 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
189 //=======================================================================
190 //function : ObjectToSObject
191 //purpose : Put a result into a SALOMEDS::SObject_wrap or call UnRegister()!
192 //=======================================================================
194 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
195 CORBA::Object_ptr theObject)
197 SALOMEDS::SObject_wrap aSO;
198 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
200 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
201 aSO = theStudy->FindObjectIOR( objStr.in() );
206 //=======================================================================
207 //function : objectToServant
209 //=======================================================================
211 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
213 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
216 //=======================================================================
217 //function : ShapeToGeomObject
219 //=======================================================================
221 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
223 GEOM::GEOM_Object_var aShapeObj;
224 if ( !theShape.IsNull() ) {
225 GEOM_Client* aClient = GetShapeReader();
226 TCollection_AsciiString IOR;
227 if ( aClient && aClient->Find( theShape, IOR ))
229 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
230 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
233 return aShapeObj._retn();
236 //=======================================================================
237 //function : GeomObjectToShape
239 //=======================================================================
241 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
244 if ( !theGeomObject->_is_nil() ) {
245 GEOM_Client* aClient = GetShapeReader();
246 GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
247 if ( aClient && !aGeomEngine->_is_nil () )
248 S = aClient->GetShape( aGeomEngine, theGeomObject );
253 //=======================================================================
256 //=======================================================================
258 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr theStudy,
259 CORBA::Object_ptr theIOR,
260 SALOMEDS::SObject_ptr theFatherObject,
261 const int theTag = 0,
262 const char* thePixMap = 0,
263 const bool theSelectable = true)
265 SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
266 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
267 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
268 bool isNewSO = false, isInUseCaseTree = false;
272 SO = aStudyBuilder->NewObject( theFatherObject );
275 else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
277 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
283 isInUseCaseTree = useCaseBuilder->IsUseCaseNode( SO );
286 SALOMEDS::GenericAttribute_wrap anAttr;
287 if ( !CORBA::is_nil( theIOR ))
289 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
290 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
291 SALOMEDS::AttributeIOR_wrap iorAttr = anAttr;
292 CORBA::String_var objStrCur = iorAttr->Value();
293 bool sameIOR = ( objStrCur.in() && strcmp( objStr.in(), objStrCur.in() ) == 0 );
296 iorAttr->SetValue( objStr.in() );
298 SALOME::GenericObj_var genObj = SALOME::GenericObj::_narrow( theIOR );
299 if ( !genObj->_is_nil() )
300 genObj->UnRegister();
305 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
306 SALOMEDS::AttributePixMap_wrap pm = anAttr;
307 pm->SetPixMap( thePixMap );
310 if ( !theSelectable ) {
311 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
312 SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
313 selAttr->SetSelectable( false );
317 aStudyBuilder->RemoveReference( SO );// remove garbage reference (23336)
319 // add object to the use case tree
320 // (to support tree representation customization and drag-n-drop)
321 if ( isNewSO || !isInUseCaseTree )
323 // define the next tag after given one in the data tree to insert SObject
324 SALOMEDS::SObject_wrap curObj, objAfter;
325 if ( theFatherObject->GetLastChildTag() > theTag )
327 SALOMEDS::UseCaseIterator_wrap
328 anUseCaseIter = useCaseBuilder->GetUseCaseIterator(theFatherObject);
329 for ( ; anUseCaseIter->More(); anUseCaseIter->Next() ) {
330 curObj = anUseCaseIter->Value();
331 if ( curObj->Tag() > theTag ) {
337 if ( !CORBA::is_nil( objAfter ))
338 useCaseBuilder->InsertBefore( SO, objAfter ); // insert at given tag
339 else if ( !isInUseCaseTree )
340 useCaseBuilder->AppendTo( theFatherObject, SO ); // append to the end of list
345 //=======================================================================
348 //=======================================================================
350 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
352 const char* theDefaultName)
354 if ( !theSObject->_is_nil() ) {
355 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
356 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
357 SALOMEDS::GenericAttribute_wrap anAttr =
358 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
359 SALOMEDS::AttributeName_wrap aNameAttr = anAttr;
360 if ( theName && theName[0] ) {
361 std::string name( theName ); // trim trailing white spaces
362 for ( size_t i = name.size()-1; i > 0; --i )
363 if ( isspace( name[i] )) name[i] = '\0';
365 aNameAttr->SetValue( name.c_str() );
369 CORBA::String_var curName = aNameAttr->Value();
370 if ( strlen( curName.in() ) == 0 ) {
371 SMESH_Comment aName(theDefaultName);
372 aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
378 //=======================================================================
379 //function : SetPixMap
381 //=======================================================================
383 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
384 const char* thePixMap)
386 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
388 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
389 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
390 SALOMEDS::GenericAttribute_wrap anAttr =
391 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
392 SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
393 aPMAttr->SetPixMap( thePixMap );
397 //=======================================================================
398 //function : addReference
400 //=======================================================================
402 static void addReference (SALOMEDS::Study_ptr theStudy,
403 SALOMEDS::SObject_ptr theSObject,
404 CORBA::Object_ptr theToObject,
407 SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
408 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
409 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
410 SALOMEDS::SObject_wrap aReferenceSO;
412 // check if the reference to theToObject already exists
413 // and find a free label for the reference object
414 bool isReferred = false;
416 SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
417 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
418 SALOMEDS::SObject_wrap curSO = anIter->Value();
419 if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
420 CORBA::String_var refEntry = aReferenceSO->GetID();
421 CORBA::String_var toEntry = aToObjSO->GetID();
422 if ( strcmp( refEntry, toEntry ) == 0 )
425 else if ( !theTag ) {
426 SALOMEDS::GenericAttribute_wrap anAttr;
427 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
436 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
437 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
439 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
441 // add reference to the use case tree
442 // (to support tree representation customization and drag-n-drop)
443 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
444 SALOMEDS::UseCaseIterator_wrap useCaseIter = useCaseBuilder->GetUseCaseIterator(theSObject);
445 for ( ; useCaseIter->More(); useCaseIter->Next() )
447 SALOMEDS::SObject_wrap curSO = useCaseIter->Value();
448 if ( curSO->Tag() == theTag )
451 useCaseBuilder->AppendTo( theSObject, aReferenceSO );
455 //=============================================================================
457 * SMESH_Gen_i::PublishInStudy
459 * Publish object in the study
461 //=============================================================================
463 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
464 SALOMEDS::SObject_ptr theSObject,
465 CORBA::Object_ptr theIOR,
467 throw (SALOME::SALOME_Exception)
469 Unexpect aCatch(SALOME_SalomeException);
470 SALOMEDS::SObject_wrap aSO;
471 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
473 if(MYDEBUG) MESSAGE("PublishInStudy");
476 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
477 if( !aMesh->_is_nil() )
478 aSO = PublishMesh( theStudy, aMesh, theName );
480 // Publishing a sub-mesh
481 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
482 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
483 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
484 aMesh = aSubMesh->GetFather();
485 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
488 // Publishing a hypothesis or algorithm
489 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
490 if ( aSO->_is_nil() && !aHyp->_is_nil() )
491 aSO = PublishHypothesis( theStudy, aHyp );
493 // Publishing a group
494 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
495 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
496 GEOM::GEOM_Object_var aShapeObject;
497 aMesh = aGroup->GetMesh();
498 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
500 if(MYDEBUG) MESSAGE("PublishInStudy_END");
505 //=======================================================================
506 //function : PublishComponent
508 //=======================================================================
510 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
512 if ( CORBA::is_nil( theStudy ))
513 return SALOMEDS::SComponent::_nil();
514 if(MYDEBUG) MESSAGE("PublishComponent");
516 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
517 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
519 CORBA::String_var compDataType = ComponentDataType();
520 SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
521 if ( !CORBA::is_nil( father ) ) {
522 // check that the component is added to the use case browser
523 if ( !useCaseBuilder->IsUseCaseNode( father ) ) {
524 useCaseBuilder->SetRootCurrent();
525 useCaseBuilder->Append( father ); // component object is added as the top level item
527 return father._retn();
530 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
531 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
532 if ( CORBA::is_nil( aCat ) )
533 return father._retn();
535 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
536 if ( CORBA::is_nil( aComp ) )
537 return father._retn();
539 SALOMEDS::GenericAttribute_wrap anAttr;
540 SALOMEDS::AttributePixMap_wrap aPixmap;
542 father = aStudyBuilder->NewComponent( compDataType.in() );
543 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
544 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
546 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
547 CORBA::String_var userName = aComp->componentusername();
548 SetName( father, userName.in(), "MESH" );
549 // add component to the use case tree
550 // (to support tree representation customization and drag-n-drop)
551 useCaseBuilder->SetRootCurrent();
552 useCaseBuilder->Append( father ); // component object is added as the top level item
553 if(MYDEBUG) MESSAGE("PublishComponent--END");
555 return father._retn();
558 //=======================================================================
559 //function : PublishMesh
561 //=======================================================================
563 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
564 SMESH::SMESH_Mesh_ptr theMesh,
567 if ( CORBA::is_nil( theStudy ) ||
568 CORBA::is_nil( theMesh ))
569 return SALOMEDS::SComponent::_nil();
570 if(MYDEBUG) MESSAGE("PublishMesh--IN");
572 // find or publish a mesh
574 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
575 if ( aMeshSO->_is_nil() )
577 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
578 if ( father->_is_nil() )
579 return aMeshSO._retn();
581 // Find correct free tag
582 long aTag = father->GetLastChildTag();
583 if ( aTag <= GetAlgorithmsRootTag() )
584 aTag = GetAlgorithmsRootTag() + 1;
588 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
589 if ( aMeshSO->_is_nil() )
590 return aMeshSO._retn();
592 SetName( aMeshSO, theName, "Mesh" );
594 // Add shape reference
596 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
597 if ( !CORBA::is_nil( aShapeObject )) {
598 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
600 // Publish global hypotheses
602 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
603 for ( CORBA::ULong i = 0; i < hypList->length(); i++ )
605 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
606 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
607 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
613 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
615 return aMeshSO._retn();
616 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
617 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
618 for ( ; subIt != subMap.end(); subIt++ ) {
619 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
620 if ( !CORBA::is_nil( aSubMesh )) {
621 aShapeObject = aSubMesh->GetSubShape();
622 SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
627 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
628 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
629 for ( ; it != grMap.end(); it++ )
631 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
632 if ( !aGroup->_is_nil() ) {
633 GEOM::GEOM_Object_var aShapeObj;
634 SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
635 if ( !aGeomGroup->_is_nil() )
636 aShapeObj = aGeomGroup->GetShape();
637 SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
641 if(MYDEBUG) MESSAGE("PublishMesh_END");
642 return aMeshSO._retn();
645 //=======================================================================
646 //function : PublishSubMesh
648 //=======================================================================
650 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
651 SMESH::SMESH_Mesh_ptr theMesh,
652 SMESH::SMESH_subMesh_ptr theSubMesh,
653 GEOM::GEOM_Object_ptr theShapeObject,
656 if (theStudy->_is_nil() || theMesh->_is_nil() ||
657 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
658 return SALOMEDS::SObject::_nil();
660 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
661 if ( aSubMeshSO->_is_nil() )
663 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
664 if ( aMeshSO->_is_nil() ) {
665 aMeshSO = PublishMesh( theStudy, theMesh );
666 if ( aMeshSO->_is_nil())
667 return SALOMEDS::SObject::_nil();
669 // Find submesh sub-tree tag
671 const char* aRootName = "";
672 switch ( theShapeObject->GetShapeType() ) {
674 aRootTag = GetSubMeshOnVertexTag();
675 aRootName = "SubMeshes on Vertex";
678 aRootTag = GetSubMeshOnEdgeTag();
679 aRootName = "SubMeshes on Edge";
682 aRootTag = GetSubMeshOnWireTag();
683 aRootName = "SubMeshes on Wire";
686 aRootTag = GetSubMeshOnFaceTag();
687 aRootName = "SubMeshes on Face";
690 aRootTag = GetSubMeshOnShellTag();
691 aRootName = "SubMeshes on Shell";
694 aRootTag = GetSubMeshOnSolidTag();
695 aRootName = "SubMeshes on Solid";
698 aRootTag = GetSubMeshOnCompoundTag();
699 aRootName = "SubMeshes on Compound";
703 // Find or create submesh root
704 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
705 aMeshSO, aRootTag, 0, false );
706 if ( aRootSO->_is_nil() )
707 return aSubMeshSO._retn();
709 SetName( aRootSO, aRootName );
711 // Add new submesh to corresponding sub-tree
712 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
713 const int isEmpty = ( elemTypes->length() == 0 );
714 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
715 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
716 if ( aSubMeshSO->_is_nil() )
717 return aSubMeshSO._retn();
719 SetName( aSubMeshSO, theName, "SubMesh" );
721 // Add reference to theShapeObject
723 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
725 // Publish hypothesis
727 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
728 for ( CORBA::ULong i = 0; i < hypList->length(); i++ ) {
729 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
730 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
731 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
734 return aSubMeshSO._retn();
737 //=======================================================================
738 //function : PublishGroup
740 //=======================================================================
742 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
743 SMESH::SMESH_Mesh_ptr theMesh,
744 SMESH::SMESH_GroupBase_ptr theGroup,
745 GEOM::GEOM_Object_ptr theShapeObject,
748 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
749 return SALOMEDS::SObject::_nil();
751 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
752 if ( aGroupSO->_is_nil() )
754 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
755 if ( aMeshSO->_is_nil() ) {
756 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
757 if ( aMeshSO->_is_nil())
758 return SALOMEDS::SObject::_nil();
760 size_t aType = (int)theGroup->GetType();
761 const char* aRootNames[] = {
762 "Compound Groups", "Groups of Nodes", "Groups of Edges",
763 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
766 // Currently, groups with heterogenous content are not supported
767 if ( aType != SMESH::ALL )
769 long aRootTag = GetNodeGroupsTag() + aType - 1;
771 // Find or create groups root
772 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
773 aMeshSO, aRootTag, 0, false );
774 if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
776 if ( aType < sizeof(aRootNames)/sizeof(char*) )
777 SetName( aRootSO, aRootNames[aType] );
779 // Add new group to corresponding sub-tree
781 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
782 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
784 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
786 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
788 if ( theGroup->GetType() == SMESH::NODE )
789 isEmpty = ( theMesh->NbNodes() == 0 );
792 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
793 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
794 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
797 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
799 if ( aGroupSO->_is_nil() )
800 return aGroupSO._retn();
803 SetName( aGroupSO, theName, "Group" );
805 //Add reference to geometry
806 if ( !theShapeObject->_is_nil() )
807 addReference( theStudy, aGroupSO, theShapeObject, 1 );
809 return aGroupSO._retn();
812 //=======================================================================
813 //function : PublishHypothesis
815 //=======================================================================
817 SALOMEDS::SObject_ptr
818 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
819 SMESH::SMESH_Hypothesis_ptr theHyp,
822 if(MYDEBUG) MESSAGE("PublishHypothesis")
823 if (theStudy->_is_nil() || theHyp->_is_nil())
824 return SALOMEDS::SObject::_nil();
826 CORBA::String_var hypType = theHyp->GetName();
828 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
829 if ( aHypSO->_is_nil() )
831 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
832 if ( father->_is_nil() )
833 return aHypSO._retn();
835 //Find or Create Hypothesis root
836 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
837 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
838 SALOMEDS::SObject_wrap aRootSO =
839 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
840 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
841 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
843 // Add New Hypothesis
844 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
845 aPmName += hypType.in();
846 // prepend plugin name to pixmap name
847 string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
848 if ( pluginName != "StdMeshers" )
849 aPmName = pluginName + "::" + aPmName;
850 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
853 SetName( aHypSO, theName, hypType.in() );
855 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
856 return aHypSO._retn();
859 //=======================================================================
860 //function : GetMeshOrSubmeshByShape
862 //=======================================================================
864 SALOMEDS::SObject_ptr
865 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
866 SMESH::SMESH_Mesh_ptr theMesh,
867 GEOM::GEOM_Object_ptr theShape)
869 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
870 SALOMEDS::SObject_wrap aMeshOrSubMesh;
871 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
872 return aMeshOrSubMesh._retn();
875 if(theMesh->HasShapeToMesh())
876 aShape = GeomObjectToShape( theShape );
878 aShape = SMESH_Mesh::PseudoShape();
880 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
882 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
883 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
884 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
885 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
887 int shapeID = meshDS->ShapeToIndex( aShape );
888 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
889 if ( !aSubMesh->_is_nil() )
890 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
893 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
894 return aMeshOrSubMesh._retn();
897 //=======================================================================
898 //function : AddHypothesisToShape
900 //=======================================================================
902 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
903 SMESH::SMESH_Mesh_ptr theMesh,
904 GEOM::GEOM_Object_ptr theShape,
905 SMESH::SMESH_Hypothesis_ptr theHyp)
907 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
908 if (theStudy->_is_nil() || theMesh->_is_nil() ||
909 theHyp->_is_nil() || (theShape->_is_nil()
910 && theMesh->HasShapeToMesh()) )
913 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
914 if ( aMeshSO->_is_nil() )
915 aMeshSO = PublishMesh( theStudy, theMesh );
916 SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
917 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
920 // Find a mesh or submesh refering to theShape
921 SALOMEDS::SObject_wrap aMeshOrSubMesh =
922 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
923 if ( aMeshOrSubMesh->_is_nil() )
926 TopoDS_Shape aShape = GeomObjectToShape( theShape );
927 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
928 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
929 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
930 int shapeID = meshDS->ShapeToIndex( aShape );
931 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
932 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
934 if ( aMeshOrSubMesh->_is_nil() )
938 //Find or Create Applied Hypothesis root
939 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
940 SALOMEDS::SObject_wrap AHR =
941 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
942 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
943 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
944 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
946 addReference( theStudy, AHR, theHyp );
948 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
952 //=======================================================================
953 //function : RemoveHypothesisFromShape
955 //=======================================================================
957 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
958 SMESH::SMESH_Mesh_ptr theMesh,
959 GEOM::GEOM_Object_ptr theShape,
960 SMESH::SMESH_Hypothesis_ptr theHyp)
962 if (theStudy->_is_nil() || theMesh->_is_nil() ||
963 theHyp->_is_nil() || (theShape->_is_nil()
964 && theMesh->HasShapeToMesh()))
967 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
968 if ( aHypSO->_is_nil() )
971 CORBA::String_var hypEntry = aHypSO->GetID();
973 // Find a mesh or sub-mesh referring to theShape
974 SALOMEDS::SObject_wrap aMeshOrSubMesh =
975 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
976 if ( aMeshOrSubMesh->_is_nil() )
979 // Find and remove a reference to aHypSO
980 SALOMEDS::SObject_wrap aRef, anObj;
981 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
983 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
985 if (anObj->ReferencedObject( aRef.inout() ))
987 CORBA::String_var refEntry = aRef->GetID();
988 found = ( strcmp( refEntry, hypEntry ) == 0 );
992 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
993 builder->RemoveObject( anObj );
1000 //================================================================================
1002 * \brief Stores names of variables that WILL be passes as parameters when calling
1003 * some method of a given object.
1004 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
1005 * \param [in] theParameters - a string contating parameters separated by ':'.
1007 //================================================================================
1009 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
1011 SALOMEDS::Study_var aStudy = GetCurrentStudy();
1012 if ( aStudy->_is_nil() )
1015 // find variable names within theParameters
1018 myLastParameters.clear();
1019 myLastParamIndex.clear(); /* vector holding indices of virables within the string
1020 of all varibles used for theObject */
1022 int pos = 0, prevPos = 0, len = strlen( theParameters );
1023 if ( len == 0 ) return;
1024 while ( pos <= len )
1026 if ( pos == len || theParameters[pos] == ':' )
1028 if ( prevPos < pos )
1030 string val( theParameters + prevPos, theParameters + pos );
1031 if ( !aStudy->IsVariable( val.c_str() ))
1033 myLastParameters.push_back( val );
1034 nbVars += (! myLastParameters.back().empty() );
1038 myLastParameters.push_back("");
1049 // (1) variable names in the string of all varibles used for theObject and
1050 // (2) indices of found variables in myLastParamIndex.
1052 // remember theObject
1053 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
1054 if ( aSObj->_is_nil() )
1056 CORBA::String_var anObjEntry = aSObj->GetID();
1057 myLastObj = anObjEntry.in();
1059 // get a string of variable names
1060 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1061 SALOMEDS::GenericAttribute_wrap anAttr =
1062 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1063 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1064 CORBA::String_var oldVars = aStringAttr->Value();
1065 std::string varStr = oldVars.in();
1067 // add new variables and find indices of variables
1068 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1071 if ( !myLastParameters[i].empty() )
1073 // find index of myLastParameters[i] in varStr
1075 bool varFound = false;
1077 // varStr can be "A|B::C;*=2|D"
1078 const std::string separators(":|;*=");
1079 while ( pos < varStr.size() && !varFound )
1082 while ( separators.find( varStr[ pos ]) != std::string::npos )
1083 if ( ++pos >= varStr.size() )
1085 // skip repetition number following '='
1086 if ( varStr[ pos-1 ] == '=' )
1088 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1090 continue; // to skip next separator
1092 // compare variable name
1093 if ( pos < varStr.size() )
1095 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1096 // same string begining but is length same?
1097 ( pos + myLastParameters[i].size() >= varStr.size() ||
1098 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1100 varIndex = curIndex;
1102 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1109 varStr += ":" + myLastParameters[i];
1110 varIndex = curIndex;
1113 myLastParamIndex.push_back( varIndex );
1115 aStringAttr->SetValue( varStr.c_str() );
1118 //================================================================================
1120 * \brief Return all variables used to create an object
1121 * \param [in] theObjectEntry - an object entry in the current study
1122 * \return std::vector< std::string > - all variable names (or values of removed variables)
1124 //================================================================================
1126 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1128 std::vector< std::string > varNames;
1129 if ( myCurrentStudy->_is_nil() )
1132 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1133 if ( myCurrentStudy->_is_nil() )
1136 // get a string of variable names
1137 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1138 SALOMEDS::GenericAttribute_wrap anAttr =
1139 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1140 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1141 CORBA::String_var oldVars = aStringAttr->Value();
1142 std::string varStr = oldVars.in();
1144 // separate variables within varStr;
1145 // varStr can be "A|B::C;*=2|D"
1147 const std::string separators(":|;*=");
1148 while ( pos < varStr.size() )
1151 pos = varStr.find_first_not_of( separators, pos );
1152 // while ( separators.find( varStr[ pos ]) != std::string::npos )
1153 // if ( ++pos >= varStr.size() )
1155 // skip repetition number following '='
1156 if ( varStr[ pos-1 ] == '=' )
1158 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1160 continue; // to skip next separator
1162 // store variable name
1163 if ( pos < varStr.size() )
1165 size_t pos2 = varStr.find_first_of( separators, pos );
1166 varNames.push_back( varStr.substr( pos, pos2 - pos));
1173 //=======================================================================
1174 //function : ParseParameters
1175 //purpose : Replace variables by their values
1176 //=======================================================================
1177 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1179 // //const char* aParameters = theParameters;
1180 // // const char* aParameters = CORBA::string_dup(theParameters);
1181 // TCollection_AsciiString anInputParams;
1182 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1183 // if( !aStudy->_is_nil() ) {
1184 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1185 // // for(int j=0;j<aSections->length();j++) {
1186 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1187 // // for(int i=0;i<aVars.length();i++ ) {
1188 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1189 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1190 // // if(i != aVars.length()-1)
1191 // // anInputParams+=":";
1193 // // if(j!=aSections->length()-1)
1194 // // anInputParams+="|";
1196 // TCollection_AsciiString paramStr( theParameters );
1197 // int beg = 0, end;
1198 // char sep, *pParams = (char*)paramStr.ToCString();
1199 // while ( beg < paramStr.Length() )
1202 // while ( ++end < paramStr.Length() )
1203 // if ( pParams[end] == ':' || pParams[end] == '|')
1205 // if ( end < paramStr.Length())
1207 // sep = pParams[end];
1208 // pParams[end] = '\0';
1210 // if ( aStudy->IsVariable( pParams+beg ))
1211 // anInputParams += pParams+beg;
1212 // if ( end < paramStr.Length() )
1213 // anInputParams += sep;
1219 // return CORBA::string_dup(anInputParams.ToCString());
1222 //=======================================================================
1223 //function : GetParameters
1225 //=======================================================================
1227 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1229 CORBA::String_var aResult("");
1231 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1232 if ( !aSObj->_is_nil() )
1234 SALOMEDS::GenericAttribute_wrap attr;
1235 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1237 SALOMEDS::AttributeString_wrap strAttr = attr;
1238 aResult = strAttr->Value();
1241 return aResult._retn();