1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_Gen_i_1.cxx
23 // Created : Thu Oct 21 17:24:06 2004
24 // Author : Edward AGAPOV (eap)
27 #include "SMESH_Gen_i.hxx"
29 #include "SMESH_Algo_i.hxx"
30 #include "SMESH_Comment.hxx"
31 #include "SMESH_Group_i.hxx"
32 #include "SMESH_Hypothesis_i.hxx"
33 #include "SMESH_Mesh_i.hxx"
34 #include "SMESH_subMesh_i.hxx"
36 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
38 #include <utilities.h>
39 #include <Utils_ExceptHandlers.hxx>
40 #include <SALOMEDS_wrap.hxx>
41 #include <SALOMEDS_Attributes_wrap.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TopoDS_Solid.hxx>
47 static int MYDEBUG = 0;
48 //static int VARIABLE_DEBUG = 0;
50 static int MYDEBUG = 0;
51 //static int VARIABLE_DEBUG = 0;
54 //=============================================================================
56 * Get...Tag [ static ]
58 * Methods which determine SMESH data model structure
60 //=============================================================================
62 long SMESH_Gen_i::GetHypothesisRootTag()
64 return SMESH::Tag_HypothesisRoot;
67 long SMESH_Gen_i::GetAlgorithmsRootTag()
69 return SMESH::Tag_AlgorithmsRoot;
72 long SMESH_Gen_i::GetRefOnShapeTag()
74 return SMESH::Tag_RefOnShape;
77 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
79 return SMESH::Tag_RefOnAppliedHypothesis;
82 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
84 return SMESH::Tag_RefOnAppliedAlgorithms;
87 long SMESH_Gen_i::GetSubMeshOnVertexTag()
89 return SMESH::Tag_SubMeshOnVertex;
92 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
94 return SMESH::Tag_SubMeshOnEdge;
97 long SMESH_Gen_i::GetSubMeshOnFaceTag()
99 return SMESH::Tag_SubMeshOnFace;
102 long SMESH_Gen_i::GetSubMeshOnSolidTag()
104 return SMESH::Tag_SubMeshOnSolid;
107 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
109 return SMESH::Tag_SubMeshOnCompound;
112 long SMESH_Gen_i::GetSubMeshOnWireTag()
114 return SMESH::Tag_SubMeshOnWire;
117 long SMESH_Gen_i::GetSubMeshOnShellTag()
119 return SMESH::Tag_SubMeshOnShell;
122 long SMESH_Gen_i::GetNodeGroupsTag()
124 return SMESH::Tag_NodeGroups;
127 long SMESH_Gen_i::GetEdgeGroupsTag()
129 return SMESH::Tag_EdgeGroups;
132 long SMESH_Gen_i::GetFaceGroupsTag()
134 return SMESH::Tag_FaceGroups;
137 long SMESH_Gen_i::GetVolumeGroupsTag()
139 return SMESH::Tag_VolumeGroups;
142 long SMESH_Gen_i::Get0DElementsGroupsTag()
144 return SMESH::Tag_0DElementsGroups;
147 long SMESH_Gen_i::GetBallElementsGroupsTag()
149 return SMESH::Tag_BallElementsGroups;
152 //=============================================================================
154 * SMESH_Gen_i::CanPublishInStudy
156 * Returns true if object can be published in the study
158 //=============================================================================
160 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
162 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
163 if(CORBA::is_nil(myCurrentStudy))
166 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
167 if( !aMesh->_is_nil() )
170 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
171 if( !aSubMesh->_is_nil() )
174 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
175 if( !aHyp->_is_nil() )
178 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
179 if( !aGroup->_is_nil() )
182 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
186 //=======================================================================
187 //function : ObjectToSObject
188 //purpose : Put a result into a SALOMEDS::SObject_wrap or call UnRegister()!
189 //=======================================================================
191 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
192 CORBA::Object_ptr theObject)
194 SALOMEDS::SObject_wrap aSO;
195 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
197 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
198 aSO = theStudy->FindObjectIOR( objStr.in() );
203 //=======================================================================
204 //function : objectToServant
206 //=======================================================================
208 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
210 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
213 //=======================================================================
214 //function : ShapeToGeomObject
216 //=======================================================================
218 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
220 GEOM::GEOM_Object_var aShapeObj;
221 if ( !theShape.IsNull() ) {
222 GEOM_Client* aClient = GetShapeReader();
223 TCollection_AsciiString IOR;
224 if ( aClient && aClient->Find( theShape, IOR ))
226 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
227 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
230 return aShapeObj._retn();
233 //=======================================================================
234 //function : GeomObjectToShape
236 //=======================================================================
238 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
241 if ( !theGeomObject->_is_nil() ) {
242 GEOM_Client* aClient = GetShapeReader();
243 GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
244 if ( aClient && !aGeomEngine->_is_nil () )
245 S = aClient->GetShape( aGeomEngine, theGeomObject );
250 //=======================================================================
253 //=======================================================================
255 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr theStudy,
256 CORBA::Object_ptr theIOR,
257 SALOMEDS::SObject_ptr theFatherObject,
258 const int theTag = 0,
259 const char* thePixMap = 0,
260 const bool theSelectable = true)
262 SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
263 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
264 SALOMEDS::UseCaseBuilder_var useCaseBuilder = theStudy->GetUseCaseBuilder();
265 SALOMEDS::SObject_wrap objAfter;
266 if ( SO->_is_nil() ) {
268 SO = aStudyBuilder->NewObject( theFatherObject );
269 } else if ( !theFatherObject->FindSubObject( theTag, SO.inout() )) {
270 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
272 // define the next tag after given one in the data tree to insert SOobject
274 int last2Pnt_pos = -1;
276 CORBA::String_var entry;
277 SALOMEDS::SObject_wrap curObj;
278 SALOMEDS::UseCaseIterator_var anUseCaseIter = useCaseBuilder->GetUseCaseIterator(theFatherObject);
279 for ( ; anUseCaseIter->More(); anUseCaseIter->Next() ) {
280 curObj = anUseCaseIter->Value();
281 entry = curObj->GetID();
282 anEntry = entry.in();
283 last2Pnt_pos = anEntry.rfind( ":" );
284 tagAfter = atoi( anEntry.substr( last2Pnt_pos+1 ).c_str() );
285 if ( tagAfter > 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 && strlen( theName ) != 0 )
342 aNameAttr->SetValue( theName );
344 CORBA::String_var curName = aNameAttr->Value();
345 if ( strlen( curName.in() ) == 0 ) {
346 SMESH_Comment aName(theDefaultName);
347 aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
353 //=======================================================================
354 //function : SetPixMap
356 //=======================================================================
358 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
359 const char* thePixMap)
361 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
363 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
364 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
365 SALOMEDS::GenericAttribute_wrap anAttr =
366 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
367 SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
368 aPMAttr->SetPixMap( thePixMap );
372 //=======================================================================
373 //function : addReference
375 //=======================================================================
377 static void addReference (SALOMEDS::Study_ptr theStudy,
378 SALOMEDS::SObject_ptr theSObject,
379 CORBA::Object_ptr theToObject,
382 SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
383 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
384 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
385 SALOMEDS::SObject_wrap aReferenceSO;
387 // check if the reference to theToObject already exists
388 // and find a free label for the reference object
389 bool isReferred = false;
391 SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
392 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
393 SALOMEDS::SObject_wrap curSO = anIter->Value();
394 if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
395 CORBA::String_var refEntry = aReferenceSO->GetID();
396 CORBA::String_var toEntry = aToObjSO->GetID();
397 if ( strcmp( refEntry, toEntry ) == 0 )
400 else if ( !theTag ) {
401 SALOMEDS::GenericAttribute_wrap anAttr;
402 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
411 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
412 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
413 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
414 // add reference to the use case tree
415 // (to support tree representation customization and drag-n-drop)
416 theStudy->GetUseCaseBuilder()->AppendTo( aReferenceSO->GetFather(), aReferenceSO );
420 //=============================================================================
422 * SMESH_Gen_i::PublishInStudy
424 * Publish object in the study
426 //=============================================================================
428 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
429 SALOMEDS::SObject_ptr theSObject,
430 CORBA::Object_ptr theIOR,
432 throw (SALOME::SALOME_Exception)
434 Unexpect aCatch(SALOME_SalomeException);
435 SALOMEDS::SObject_wrap aSO;
436 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
438 if(MYDEBUG) MESSAGE("PublishInStudy");
441 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
442 if( !aMesh->_is_nil() )
443 aSO = PublishMesh( theStudy, aMesh, theName );
445 // Publishing a sub-mesh
446 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
447 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
448 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
449 aMesh = aSubMesh->GetFather();
450 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
453 // Publishing a hypothesis or algorithm
454 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
455 if ( aSO->_is_nil() && !aHyp->_is_nil() )
456 aSO = PublishHypothesis( theStudy, aHyp );
458 // Publishing a group
459 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
460 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
461 GEOM::GEOM_Object_var aShapeObject;
462 aMesh = aGroup->GetMesh();
463 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
465 if(MYDEBUG) MESSAGE("PublishInStudy_END");
470 //=======================================================================
471 //function : PublishComponent
473 //=======================================================================
475 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
477 if ( CORBA::is_nil( theStudy ))
478 return SALOMEDS::SComponent::_nil();
479 if(MYDEBUG) MESSAGE("PublishComponent");
481 CORBA::String_var compDataType = ComponentDataType();
482 SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
483 if ( !CORBA::is_nil( father ) )
484 return father._retn();
486 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
487 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
488 if ( CORBA::is_nil( aCat ) )
489 return father._retn();
491 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
492 if ( CORBA::is_nil( aComp ) )
493 return father._retn();
495 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
496 SALOMEDS::UseCaseBuilder_var useCaseBuilder = theStudy->GetUseCaseBuilder();
497 SALOMEDS::GenericAttribute_wrap anAttr;
498 SALOMEDS::AttributePixMap_wrap aPixmap;
500 father = aStudyBuilder->NewComponent( compDataType.in() );
501 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
502 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
504 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
505 CORBA::String_var userName = aComp->componentusername();
506 SetName( father, userName.in(), "MESH" );
507 // add component to the use case tree
508 // (to support tree representation customization and drag-n-drop)
509 useCaseBuilder->SetRootCurrent();
510 useCaseBuilder->Append( father ); // component object is added as the top level item
511 if(MYDEBUG) MESSAGE("PublishComponent--END");
513 return father._retn();
516 //=============================================================================
518 * findMaxChildTag [ static internal ]
520 * Finds maximum child tag for the given object
522 //=============================================================================
524 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
527 if ( !theSObject->_is_nil() ) {
528 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
529 if ( !aStudy->_is_nil() ) {
530 SALOMEDS::ChildIterator_wrap anIter = aStudy->NewChildIterator( theSObject );
531 for ( ; anIter->More(); anIter->Next() ) {
532 SALOMEDS::SObject_wrap anSO = anIter->Value();
533 long nTag = anSO->Tag();
542 //=======================================================================
543 //function : PublishMesh
545 //=======================================================================
547 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
548 SMESH::SMESH_Mesh_ptr theMesh,
551 if ( CORBA::is_nil( theStudy ) ||
552 CORBA::is_nil( theMesh ))
553 return SALOMEDS::SComponent::_nil();
554 if(MYDEBUG) MESSAGE("PublishMesh--IN");
556 // find or publish a mesh
558 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
559 if ( aMeshSO->_is_nil() )
561 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
562 if ( father->_is_nil() )
563 return aMeshSO._retn();
565 // Find correct free tag
566 long aTag = findMaxChildTag( father.in() );
567 if ( aTag <= GetAlgorithmsRootTag() )
568 aTag = GetAlgorithmsRootTag() + 1;
572 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
573 if ( aMeshSO->_is_nil() )
574 return aMeshSO._retn();
576 SetName( aMeshSO, theName, "Mesh" );
578 // Add shape reference
580 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
581 if ( !CORBA::is_nil( aShapeObject )) {
582 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
584 // Publish global hypotheses
586 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
587 for ( int i = 0; i < hypList->length(); i++ )
589 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
590 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
591 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
597 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
599 return aMeshSO._retn();
600 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
601 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
602 for ( ; subIt != subMap.end(); subIt++ ) {
603 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
604 if ( !CORBA::is_nil( aSubMesh )) {
605 aShapeObject = aSubMesh->GetSubShape();
606 SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
611 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
612 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
613 for ( ; it != grMap.end(); it++ )
615 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
616 if ( !aGroup->_is_nil() ) {
617 GEOM::GEOM_Object_var aShapeObj;
618 SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
619 if ( !aGeomGroup->_is_nil() )
620 aShapeObj = aGeomGroup->GetShape();
621 SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
625 if(MYDEBUG) MESSAGE("PublishMesh_END");
626 return aMeshSO._retn();
629 //=======================================================================
630 //function : PublishSubMesh
632 //=======================================================================
634 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
635 SMESH::SMESH_Mesh_ptr theMesh,
636 SMESH::SMESH_subMesh_ptr theSubMesh,
637 GEOM::GEOM_Object_ptr theShapeObject,
640 if (theStudy->_is_nil() || theMesh->_is_nil() ||
641 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
642 return SALOMEDS::SObject::_nil();
644 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
645 if ( aSubMeshSO->_is_nil() )
647 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
648 if ( aMeshSO->_is_nil() ) {
649 aMeshSO = PublishMesh( theStudy, theMesh );
650 if ( aMeshSO->_is_nil())
651 return SALOMEDS::SObject::_nil();
653 // Find submesh sub-tree tag
655 const char* aRootName = "";
656 switch ( theShapeObject->GetShapeType() ) {
658 aRootTag = GetSubMeshOnVertexTag();
659 aRootName = "SubMeshes on Vertex";
662 aRootTag = GetSubMeshOnEdgeTag();
663 aRootName = "SubMeshes on Edge";
666 aRootTag = GetSubMeshOnWireTag();
667 aRootName = "SubMeshes on Wire";
670 aRootTag = GetSubMeshOnFaceTag();
671 aRootName = "SubMeshes on Face";
674 aRootTag = GetSubMeshOnShellTag();
675 aRootName = "SubMeshes on Shell";
678 aRootTag = GetSubMeshOnSolidTag();
679 aRootName = "SubMeshes on Solid";
682 aRootTag = GetSubMeshOnCompoundTag();
683 aRootName = "SubMeshes on Compound";
687 // Find or create submesh root
688 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
689 aMeshSO, aRootTag, 0, false );
690 if ( aRootSO->_is_nil() )
691 return aSubMeshSO._retn();
693 SetName( aRootSO, aRootName );
695 // Add new submesh to corresponding sub-tree
696 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
697 const int isEmpty = ( elemTypes->length() == 0 );
698 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
699 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
700 if ( aSubMeshSO->_is_nil() )
701 return aSubMeshSO._retn();
703 SetName( aSubMeshSO, theName, "SubMesh" );
705 // Add reference to theShapeObject
707 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
709 // Publish hypothesis
711 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
712 for ( int i = 0; i < hypList->length(); i++ ) {
713 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
714 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
715 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
718 return aSubMeshSO._retn();
721 //=======================================================================
722 //function : PublishGroup
724 //=======================================================================
726 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
727 SMESH::SMESH_Mesh_ptr theMesh,
728 SMESH::SMESH_GroupBase_ptr theGroup,
729 GEOM::GEOM_Object_ptr theShapeObject,
732 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
733 return SALOMEDS::SObject::_nil();
735 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
736 if ( aGroupSO->_is_nil() )
738 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
739 if ( aMeshSO->_is_nil() ) {
740 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
741 if ( aMeshSO->_is_nil())
742 return SALOMEDS::SObject::_nil();
744 int aType = (int)theGroup->GetType();
745 const char* aRootNames[] = {
746 "Compound Groups", "Groups of Nodes", "Groups of Edges",
747 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
750 // Currently, groups with heterogenous content are not supported
751 if ( aType != SMESH::ALL )
753 long aRootTag = GetNodeGroupsTag() + aType - 1;
755 // Find or create groups root
756 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
757 aMeshSO, aRootTag, 0, false );
758 if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
760 if ( aType < sizeof(aRootNames)/sizeof(char*) )
761 SetName( aRootSO, aRootNames[aType] );
763 // Add new group to corresponding sub-tree
764 SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
765 int isEmpty = ( elemTypes->length() == 0 );
766 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
767 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
769 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
771 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
773 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
774 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
775 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
777 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
779 if ( aGroupSO->_is_nil() )
780 return aGroupSO._retn();
783 SetName( aGroupSO, theName, "Group" );
785 //Add reference to geometry
786 if ( !theShapeObject->_is_nil() )
787 addReference( theStudy, aGroupSO, theShapeObject, 1 );
789 return aGroupSO._retn();
792 //=======================================================================
793 //function : PublishHypothesis
795 //=======================================================================
797 SALOMEDS::SObject_ptr
798 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
799 SMESH::SMESH_Hypothesis_ptr theHyp,
802 if(MYDEBUG) MESSAGE("PublishHypothesis")
803 if (theStudy->_is_nil() || theHyp->_is_nil())
804 return SALOMEDS::SObject::_nil();
806 CORBA::String_var hypType = theHyp->GetName();
808 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
809 if ( aHypSO->_is_nil() )
811 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
812 if ( father->_is_nil() )
813 return aHypSO._retn();
815 //Find or Create Hypothesis root
816 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
817 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
818 SALOMEDS::SObject_wrap aRootSO =
819 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
820 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
821 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
823 // Add New Hypothesis
824 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
825 aPmName += hypType.in();
826 // prepend plugin name to pixmap name
827 string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
828 if ( pluginName != "StdMeshers" )
829 aPmName = pluginName + "::" + aPmName;
830 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
833 SetName( aHypSO, theName, hypType.in() );
835 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
836 return aHypSO._retn();
839 //=======================================================================
840 //function : GetMeshOrSubmeshByShape
842 //=======================================================================
844 SALOMEDS::SObject_ptr
845 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
846 SMESH::SMESH_Mesh_ptr theMesh,
847 GEOM::GEOM_Object_ptr theShape)
849 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
850 SALOMEDS::SObject_wrap aMeshOrSubMesh;
851 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
852 return aMeshOrSubMesh._retn();
855 if(theMesh->HasShapeToMesh())
856 aShape = GeomObjectToShape( theShape );
858 aShape = SMESH_Mesh::PseudoShape();
860 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
862 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
863 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
864 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
865 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
867 int shapeID = meshDS->ShapeToIndex( aShape );
868 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
869 if ( !aSubMesh->_is_nil() )
870 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
873 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
874 return aMeshOrSubMesh._retn();
877 //=======================================================================
878 //function : AddHypothesisToShape
880 //=======================================================================
882 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
883 SMESH::SMESH_Mesh_ptr theMesh,
884 GEOM::GEOM_Object_ptr theShape,
885 SMESH::SMESH_Hypothesis_ptr theHyp)
887 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
888 if (theStudy->_is_nil() || theMesh->_is_nil() ||
889 theHyp->_is_nil() || (theShape->_is_nil()
890 && theMesh->HasShapeToMesh()) )
893 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
894 if ( aMeshSO->_is_nil() )
895 aMeshSO = PublishMesh( theStudy, theMesh );
896 SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
897 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
900 // Find a mesh or submesh refering to theShape
901 SALOMEDS::SObject_wrap aMeshOrSubMesh =
902 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
903 if ( aMeshOrSubMesh->_is_nil() )
906 TopoDS_Shape aShape = GeomObjectToShape( theShape );
907 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
908 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
909 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
910 int shapeID = meshDS->ShapeToIndex( aShape );
911 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
912 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
914 if ( aMeshOrSubMesh->_is_nil() )
918 //Find or Create Applied Hypothesis root
919 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
920 SALOMEDS::SObject_wrap AHR =
921 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
922 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
923 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
924 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
926 addReference( theStudy, AHR, theHyp );
928 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
932 //=======================================================================
933 //function : RemoveHypothesisFromShape
935 //=======================================================================
937 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
938 SMESH::SMESH_Mesh_ptr theMesh,
939 GEOM::GEOM_Object_ptr theShape,
940 SMESH::SMESH_Hypothesis_ptr theHyp)
942 if (theStudy->_is_nil() || theMesh->_is_nil() ||
943 theHyp->_is_nil() || (theShape->_is_nil()
944 && theMesh->HasShapeToMesh()))
947 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
948 if ( aHypSO->_is_nil() )
951 CORBA::String_var hypEntry = aHypSO->GetID();
953 // Find a mesh or submesh refering to theShape
954 SALOMEDS::SObject_wrap aMeshOrSubMesh =
955 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
956 if ( aMeshOrSubMesh->_is_nil() )
959 // Find and remove a reference to aHypSO
960 SALOMEDS::SObject_wrap aRef, anObj;
961 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
963 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
965 if (anObj->ReferencedObject( aRef.inout() ))
967 CORBA::String_var refEntry = aRef->GetID();
968 found = ( strcmp( refEntry, hypEntry ) == 0 );
972 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
973 builder->RemoveObject( anObj );
980 //================================================================================
982 * \brief Stores names of variables that WILL be passes as parameters when calling
983 * some method of a given object.
984 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
985 * \param [in] theParameters - a string contating parameters separated by ':'.
987 //================================================================================
989 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
991 SALOMEDS::Study_var aStudy = GetCurrentStudy();
992 if ( aStudy->_is_nil() )
995 // find variable names within theParameters
998 myLastParameters.clear();
999 myLastParamIndex.clear(); /* vector holding indices of virables within the string
1000 of all varibles used for theObject */
1002 int pos = 0, prevPos = 0, len = strlen( theParameters );
1003 if ( len == 0 ) return;
1004 while ( pos <= len )
1006 if ( pos == len || theParameters[pos] == ':' )
1008 if ( prevPos < pos )
1010 string val( theParameters + prevPos, theParameters + pos );
1011 if ( !aStudy->IsVariable( val.c_str() ))
1013 myLastParameters.push_back( val );
1014 nbVars += (! myLastParameters.back().empty() );
1018 myLastParameters.push_back("");
1029 // (1) variable names in the string of all varibles used for theObject and
1030 // (2) indices of found variables in myLastParamIndex.
1032 // remember theObject
1033 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
1034 if ( aSObj->_is_nil() )
1036 CORBA::String_var anObjEntry = aSObj->GetID();
1037 myLastObj = anObjEntry.in();
1039 // get a string of variable names
1040 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1041 SALOMEDS::GenericAttribute_wrap anAttr =
1042 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1043 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1044 CORBA::String_var oldVars = aStringAttr->Value();
1045 std::string varStr = oldVars.in();
1047 // add new variables and find indices of variables
1048 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1051 if ( !myLastParameters[i].empty() )
1053 // find index of myLastParameters[i] in varStr
1055 bool varFound = false;
1057 // varStr can be "A|B::C;*=2|D"
1058 const std::string separators(":|;*=");
1059 while ( pos < varStr.size() && !varFound )
1062 while ( separators.find( varStr[ pos ]) != std::string::npos )
1063 if ( ++pos >= varStr.size() )
1065 // skip repetition number following '='
1066 if ( varStr[ pos-1 ] == '=' )
1068 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1070 continue; // to skip next separator
1072 // compare variable name
1073 if ( pos < varStr.size() )
1075 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1076 // same string begining but is length same?
1077 ( pos + myLastParameters[i].size() >= varStr.size() ||
1078 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1080 varIndex = curIndex;
1082 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1089 varStr += ":" + myLastParameters[i];
1090 varIndex = curIndex;
1093 myLastParamIndex.push_back( varIndex );
1095 aStringAttr->SetValue( varStr.c_str() );
1098 //================================================================================
1100 * \brief Return all variables used to create an object
1101 * \param [in] theObjectEntry - an object entry in the current study
1102 * \return std::vector< std::string > - all variable names (or values of removed variables)
1104 //================================================================================
1106 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1108 std::vector< std::string > varNames;
1109 if ( myCurrentStudy->_is_nil() )
1112 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1113 if ( myCurrentStudy->_is_nil() )
1116 // get a string of variable names
1117 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1118 SALOMEDS::GenericAttribute_wrap anAttr =
1119 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1120 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1121 CORBA::String_var oldVars = aStringAttr->Value();
1122 std::string varStr = oldVars.in();
1124 // separate variables within varStr;
1125 // varStr can be "A|B::C;*=2|D"
1127 const std::string separators(":|;*=");
1128 while ( pos < varStr.size() )
1131 pos = varStr.find_first_not_of( separators, pos );
1132 // while ( separators.find( varStr[ pos ]) != std::string::npos )
1133 // if ( ++pos >= varStr.size() )
1135 // skip repetition number following '='
1136 if ( varStr[ pos-1 ] == '=' )
1138 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1140 continue; // to skip next separator
1142 // store variable name
1143 if ( pos < varStr.size() )
1145 size_t pos2 = varStr.find_first_of( separators, pos );
1146 varNames.push_back( varStr.substr( pos, pos2 - pos));
1153 //=======================================================================
1154 //function : ParseParameters
1155 //purpose : Replace variables by their values
1156 //=======================================================================
1157 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1159 // //const char* aParameters = theParameters;
1160 // // const char* aParameters = CORBA::string_dup(theParameters);
1161 // TCollection_AsciiString anInputParams;
1162 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1163 // if( !aStudy->_is_nil() ) {
1164 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1165 // // for(int j=0;j<aSections->length();j++) {
1166 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1167 // // for(int i=0;i<aVars.length();i++ ) {
1168 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1169 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1170 // // if(i != aVars.length()-1)
1171 // // anInputParams+=":";
1173 // // if(j!=aSections->length()-1)
1174 // // anInputParams+="|";
1176 // TCollection_AsciiString paramStr( theParameters );
1177 // int beg = 0, end;
1178 // char sep, *pParams = (char*)paramStr.ToCString();
1179 // while ( beg < paramStr.Length() )
1182 // while ( ++end < paramStr.Length() )
1183 // if ( pParams[end] == ':' || pParams[end] == '|')
1185 // if ( end < paramStr.Length())
1187 // sep = pParams[end];
1188 // pParams[end] = '\0';
1190 // if ( aStudy->IsVariable( pParams+beg ))
1191 // anInputParams += pParams+beg;
1192 // if ( end < paramStr.Length() )
1193 // anInputParams += sep;
1199 // return CORBA::string_dup(anInputParams.ToCString());
1202 //=======================================================================
1203 //function : GetParameters
1205 //=======================================================================
1207 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1209 CORBA::String_var aResult("");
1211 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1212 if ( !aSObj->_is_nil() )
1214 SALOMEDS::GenericAttribute_wrap attr;
1215 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1217 SALOMEDS::AttributeString_wrap strAttr = attr;
1218 aResult = strAttr->Value();
1221 return aResult._retn();