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 SALOMEDS::SObject_wrap objAfter;
269 bool isNewSO = false;
273 SO = aStudyBuilder->NewObject( theFatherObject );
276 else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
278 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
281 // define the next tag after given one in the data tree to insert SObject
282 SALOMEDS::SObject_wrap curObj;
283 if ( theFatherObject->GetLastChildTag() > theTag )
285 SALOMEDS::UseCaseIterator_wrap
286 anUseCaseIter = useCaseBuilder->GetUseCaseIterator(theFatherObject);
287 for ( ; anUseCaseIter->More(); anUseCaseIter->Next() ) {
288 curObj = anUseCaseIter->Value();
289 if ( curObj->Tag() > theTag ) {
298 SALOMEDS::GenericAttribute_wrap anAttr;
299 if ( !CORBA::is_nil( theIOR ))
301 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
302 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
303 SALOMEDS::AttributeIOR_wrap iorAttr = anAttr;
304 CORBA::String_var objStrCur = iorAttr->Value();
305 bool sameIOR = ( objStrCur.in() && strcmp( objStr.in(), objStrCur.in() ) == 0 );
308 iorAttr->SetValue( objStr.in() );
310 SALOME::GenericObj_var genObj = SALOME::GenericObj::_narrow( theIOR );
311 if ( !genObj->_is_nil() )
312 genObj->UnRegister();
317 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
318 SALOMEDS::AttributePixMap_wrap pm = anAttr;
319 pm->SetPixMap( thePixMap );
322 if ( !theSelectable ) {
323 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
324 SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
325 selAttr->SetSelectable( false );
328 // add object to the use case tree
329 // (to support tree representation customization and drag-n-drop)
332 if ( !CORBA::is_nil( objAfter ) )
333 useCaseBuilder->InsertBefore( SO, objAfter ); // insert at given tag
334 else if ( !useCaseBuilder->IsUseCaseNode( SO ) )
335 useCaseBuilder->AppendTo( theFatherObject, SO ); // append to the end of list
340 //=======================================================================
343 //=======================================================================
345 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
347 const char* theDefaultName)
349 if ( !theSObject->_is_nil() ) {
350 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
351 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
352 SALOMEDS::GenericAttribute_wrap anAttr =
353 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
354 SALOMEDS::AttributeName_wrap aNameAttr = anAttr;
355 if ( theName && theName[0] ) {
356 std::string name( theName ); // trim trailing white spaces
357 for ( size_t i = name.size()-1; i > 0; --i )
358 if ( isspace( name[i] )) name[i] = '\0';
360 aNameAttr->SetValue( name.c_str() );
364 CORBA::String_var curName = aNameAttr->Value();
365 if ( strlen( curName.in() ) == 0 ) {
366 SMESH_Comment aName(theDefaultName);
367 aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
373 //=======================================================================
374 //function : SetPixMap
376 //=======================================================================
378 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
379 const char* thePixMap)
381 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
383 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
384 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
385 SALOMEDS::GenericAttribute_wrap anAttr =
386 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
387 SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
388 aPMAttr->SetPixMap( thePixMap );
392 //=======================================================================
393 //function : addReference
395 //=======================================================================
397 static void addReference (SALOMEDS::Study_ptr theStudy,
398 SALOMEDS::SObject_ptr theSObject,
399 CORBA::Object_ptr theToObject,
402 SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
403 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
404 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
405 SALOMEDS::SObject_wrap aReferenceSO;
407 // check if the reference to theToObject already exists
408 // and find a free label for the reference object
409 bool isReferred = false;
411 SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
412 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
413 SALOMEDS::SObject_wrap curSO = anIter->Value();
414 if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
415 CORBA::String_var refEntry = aReferenceSO->GetID();
416 CORBA::String_var toEntry = aToObjSO->GetID();
417 if ( strcmp( refEntry, toEntry ) == 0 )
420 else if ( !theTag ) {
421 SALOMEDS::GenericAttribute_wrap anAttr;
422 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
431 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
432 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
434 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
436 // add reference to the use case tree
437 // (to support tree representation customization and drag-n-drop)
438 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
439 SALOMEDS::UseCaseIterator_wrap useCaseIter = useCaseBuilder->GetUseCaseIterator(theSObject);
440 for ( ; useCaseIter->More(); useCaseIter->Next() )
442 SALOMEDS::SObject_wrap curSO = useCaseIter->Value();
443 if ( curSO->Tag() == theTag )
446 useCaseBuilder->AppendTo( theSObject, aReferenceSO );
450 //=============================================================================
452 * SMESH_Gen_i::PublishInStudy
454 * Publish object in the study
456 //=============================================================================
458 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
459 SALOMEDS::SObject_ptr theSObject,
460 CORBA::Object_ptr theIOR,
462 throw (SALOME::SALOME_Exception)
464 Unexpect aCatch(SALOME_SalomeException);
465 SALOMEDS::SObject_wrap aSO;
466 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
468 if(MYDEBUG) MESSAGE("PublishInStudy");
471 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
472 if( !aMesh->_is_nil() )
473 aSO = PublishMesh( theStudy, aMesh, theName );
475 // Publishing a sub-mesh
476 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
477 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
478 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
479 aMesh = aSubMesh->GetFather();
480 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
483 // Publishing a hypothesis or algorithm
484 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
485 if ( aSO->_is_nil() && !aHyp->_is_nil() )
486 aSO = PublishHypothesis( theStudy, aHyp );
488 // Publishing a group
489 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
490 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
491 GEOM::GEOM_Object_var aShapeObject;
492 aMesh = aGroup->GetMesh();
493 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
495 if(MYDEBUG) MESSAGE("PublishInStudy_END");
500 //=======================================================================
501 //function : PublishComponent
503 //=======================================================================
505 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
507 if ( CORBA::is_nil( theStudy ))
508 return SALOMEDS::SComponent::_nil();
509 if(MYDEBUG) MESSAGE("PublishComponent");
511 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
512 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
514 CORBA::String_var compDataType = ComponentDataType();
515 SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
516 if ( !CORBA::is_nil( father ) ) {
517 // check that the component is added to the use case browser
518 if ( !useCaseBuilder->IsUseCaseNode( father ) ) {
519 useCaseBuilder->SetRootCurrent();
520 useCaseBuilder->Append( father ); // component object is added as the top level item
522 return father._retn();
525 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
526 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
527 if ( CORBA::is_nil( aCat ) )
528 return father._retn();
530 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
531 if ( CORBA::is_nil( aComp ) )
532 return father._retn();
534 SALOMEDS::GenericAttribute_wrap anAttr;
535 SALOMEDS::AttributePixMap_wrap aPixmap;
537 father = aStudyBuilder->NewComponent( compDataType.in() );
538 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
539 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
541 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
542 CORBA::String_var userName = aComp->componentusername();
543 SetName( father, userName.in(), "MESH" );
544 // add component to the use case tree
545 // (to support tree representation customization and drag-n-drop)
546 useCaseBuilder->SetRootCurrent();
547 useCaseBuilder->Append( father ); // component object is added as the top level item
548 if(MYDEBUG) MESSAGE("PublishComponent--END");
550 return father._retn();
553 //=======================================================================
554 //function : PublishMesh
556 //=======================================================================
558 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
559 SMESH::SMESH_Mesh_ptr theMesh,
562 if ( CORBA::is_nil( theStudy ) ||
563 CORBA::is_nil( theMesh ))
564 return SALOMEDS::SComponent::_nil();
565 if(MYDEBUG) MESSAGE("PublishMesh--IN");
567 // find or publish a mesh
569 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
570 if ( aMeshSO->_is_nil() )
572 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
573 if ( father->_is_nil() )
574 return aMeshSO._retn();
576 // Find correct free tag
577 long aTag = father->GetLastChildTag();
578 if ( aTag <= GetAlgorithmsRootTag() )
579 aTag = GetAlgorithmsRootTag() + 1;
583 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
584 if ( aMeshSO->_is_nil() )
585 return aMeshSO._retn();
587 SetName( aMeshSO, theName, "Mesh" );
589 // Add shape reference
591 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
592 if ( !CORBA::is_nil( aShapeObject )) {
593 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
595 // Publish global hypotheses
597 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
598 for ( CORBA::ULong i = 0; i < hypList->length(); i++ )
600 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
601 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
602 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
608 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
610 return aMeshSO._retn();
611 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
612 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
613 for ( ; subIt != subMap.end(); subIt++ ) {
614 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
615 if ( !CORBA::is_nil( aSubMesh )) {
616 aShapeObject = aSubMesh->GetSubShape();
617 SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
622 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
623 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
624 for ( ; it != grMap.end(); it++ )
626 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
627 if ( !aGroup->_is_nil() ) {
628 GEOM::GEOM_Object_var aShapeObj;
629 SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
630 if ( !aGeomGroup->_is_nil() )
631 aShapeObj = aGeomGroup->GetShape();
632 SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
636 if(MYDEBUG) MESSAGE("PublishMesh_END");
637 return aMeshSO._retn();
640 //=======================================================================
641 //function : PublishSubMesh
643 //=======================================================================
645 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
646 SMESH::SMESH_Mesh_ptr theMesh,
647 SMESH::SMESH_subMesh_ptr theSubMesh,
648 GEOM::GEOM_Object_ptr theShapeObject,
651 if (theStudy->_is_nil() || theMesh->_is_nil() ||
652 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
653 return SALOMEDS::SObject::_nil();
655 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
656 if ( aSubMeshSO->_is_nil() )
658 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
659 if ( aMeshSO->_is_nil() ) {
660 aMeshSO = PublishMesh( theStudy, theMesh );
661 if ( aMeshSO->_is_nil())
662 return SALOMEDS::SObject::_nil();
664 // Find submesh sub-tree tag
666 const char* aRootName = "";
667 switch ( theShapeObject->GetShapeType() ) {
669 aRootTag = GetSubMeshOnVertexTag();
670 aRootName = "SubMeshes on Vertex";
673 aRootTag = GetSubMeshOnEdgeTag();
674 aRootName = "SubMeshes on Edge";
677 aRootTag = GetSubMeshOnWireTag();
678 aRootName = "SubMeshes on Wire";
681 aRootTag = GetSubMeshOnFaceTag();
682 aRootName = "SubMeshes on Face";
685 aRootTag = GetSubMeshOnShellTag();
686 aRootName = "SubMeshes on Shell";
689 aRootTag = GetSubMeshOnSolidTag();
690 aRootName = "SubMeshes on Solid";
693 aRootTag = GetSubMeshOnCompoundTag();
694 aRootName = "SubMeshes on Compound";
698 // Find or create submesh root
699 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
700 aMeshSO, aRootTag, 0, false );
701 if ( aRootSO->_is_nil() )
702 return aSubMeshSO._retn();
704 SetName( aRootSO, aRootName );
706 // Add new submesh to corresponding sub-tree
707 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
708 const int isEmpty = ( elemTypes->length() == 0 );
709 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
710 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
711 if ( aSubMeshSO->_is_nil() )
712 return aSubMeshSO._retn();
714 SetName( aSubMeshSO, theName, "SubMesh" );
716 // Add reference to theShapeObject
718 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
720 // Publish hypothesis
722 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
723 for ( CORBA::ULong i = 0; i < hypList->length(); i++ ) {
724 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
725 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
726 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
729 return aSubMeshSO._retn();
732 //=======================================================================
733 //function : PublishGroup
735 //=======================================================================
737 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
738 SMESH::SMESH_Mesh_ptr theMesh,
739 SMESH::SMESH_GroupBase_ptr theGroup,
740 GEOM::GEOM_Object_ptr theShapeObject,
743 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
744 return SALOMEDS::SObject::_nil();
746 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
747 if ( aGroupSO->_is_nil() )
749 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
750 if ( aMeshSO->_is_nil() ) {
751 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
752 if ( aMeshSO->_is_nil())
753 return SALOMEDS::SObject::_nil();
755 size_t aType = (int)theGroup->GetType();
756 const char* aRootNames[] = {
757 "Compound Groups", "Groups of Nodes", "Groups of Edges",
758 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
761 // Currently, groups with heterogenous content are not supported
762 if ( aType != SMESH::ALL )
764 long aRootTag = GetNodeGroupsTag() + aType - 1;
766 // Find or create groups root
767 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
768 aMeshSO, aRootTag, 0, false );
769 if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
771 if ( aType < sizeof(aRootNames)/sizeof(char*) )
772 SetName( aRootSO, aRootNames[aType] );
774 // Add new group to corresponding sub-tree
776 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
777 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
779 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
781 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
783 if ( theGroup->GetType() == SMESH::NODE )
784 isEmpty = ( theMesh->NbNodes() == 0 );
787 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
788 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
789 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
792 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
794 if ( aGroupSO->_is_nil() )
795 return aGroupSO._retn();
798 SetName( aGroupSO, theName, "Group" );
800 //Add reference to geometry
801 if ( !theShapeObject->_is_nil() )
802 addReference( theStudy, aGroupSO, theShapeObject, 1 );
804 return aGroupSO._retn();
807 //=======================================================================
808 //function : PublishHypothesis
810 //=======================================================================
812 SALOMEDS::SObject_ptr
813 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
814 SMESH::SMESH_Hypothesis_ptr theHyp,
817 if(MYDEBUG) MESSAGE("PublishHypothesis")
818 if (theStudy->_is_nil() || theHyp->_is_nil())
819 return SALOMEDS::SObject::_nil();
821 CORBA::String_var hypType = theHyp->GetName();
823 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
824 if ( aHypSO->_is_nil() )
826 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
827 if ( father->_is_nil() )
828 return aHypSO._retn();
830 //Find or Create Hypothesis root
831 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
832 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
833 SALOMEDS::SObject_wrap aRootSO =
834 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
835 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
836 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
838 // Add New Hypothesis
839 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
840 aPmName += hypType.in();
841 // prepend plugin name to pixmap name
842 string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
843 if ( pluginName != "StdMeshers" )
844 aPmName = pluginName + "::" + aPmName;
845 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
848 SetName( aHypSO, theName, hypType.in() );
850 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
851 return aHypSO._retn();
854 //=======================================================================
855 //function : GetMeshOrSubmeshByShape
857 //=======================================================================
859 SALOMEDS::SObject_ptr
860 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
861 SMESH::SMESH_Mesh_ptr theMesh,
862 GEOM::GEOM_Object_ptr theShape)
864 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
865 SALOMEDS::SObject_wrap aMeshOrSubMesh;
866 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
867 return aMeshOrSubMesh._retn();
870 if(theMesh->HasShapeToMesh())
871 aShape = GeomObjectToShape( theShape );
873 aShape = SMESH_Mesh::PseudoShape();
875 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
877 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
878 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
879 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
880 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
882 int shapeID = meshDS->ShapeToIndex( aShape );
883 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
884 if ( !aSubMesh->_is_nil() )
885 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
888 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
889 return aMeshOrSubMesh._retn();
892 //=======================================================================
893 //function : AddHypothesisToShape
895 //=======================================================================
897 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
898 SMESH::SMESH_Mesh_ptr theMesh,
899 GEOM::GEOM_Object_ptr theShape,
900 SMESH::SMESH_Hypothesis_ptr theHyp)
902 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
903 if (theStudy->_is_nil() || theMesh->_is_nil() ||
904 theHyp->_is_nil() || (theShape->_is_nil()
905 && theMesh->HasShapeToMesh()) )
908 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
909 if ( aMeshSO->_is_nil() )
910 aMeshSO = PublishMesh( theStudy, theMesh );
911 SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
912 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
915 // Find a mesh or submesh refering to theShape
916 SALOMEDS::SObject_wrap aMeshOrSubMesh =
917 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
918 if ( aMeshOrSubMesh->_is_nil() )
921 TopoDS_Shape aShape = GeomObjectToShape( theShape );
922 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
923 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
924 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
925 int shapeID = meshDS->ShapeToIndex( aShape );
926 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
927 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
929 if ( aMeshOrSubMesh->_is_nil() )
933 //Find or Create Applied Hypothesis root
934 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
935 SALOMEDS::SObject_wrap AHR =
936 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
937 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
938 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
939 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
941 addReference( theStudy, AHR, theHyp );
943 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
947 //=======================================================================
948 //function : RemoveHypothesisFromShape
950 //=======================================================================
952 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
953 SMESH::SMESH_Mesh_ptr theMesh,
954 GEOM::GEOM_Object_ptr theShape,
955 SMESH::SMESH_Hypothesis_ptr theHyp)
957 if (theStudy->_is_nil() || theMesh->_is_nil() ||
958 theHyp->_is_nil() || (theShape->_is_nil()
959 && theMesh->HasShapeToMesh()))
962 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
963 if ( aHypSO->_is_nil() )
966 CORBA::String_var hypEntry = aHypSO->GetID();
968 // Find a mesh or sub-mesh referring to theShape
969 SALOMEDS::SObject_wrap aMeshOrSubMesh =
970 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
971 if ( aMeshOrSubMesh->_is_nil() )
974 // Find and remove a reference to aHypSO
975 SALOMEDS::SObject_wrap aRef, anObj;
976 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
978 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
980 if (anObj->ReferencedObject( aRef.inout() ))
982 CORBA::String_var refEntry = aRef->GetID();
983 found = ( strcmp( refEntry, hypEntry ) == 0 );
987 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
988 builder->RemoveObject( anObj );
995 //================================================================================
997 * \brief Stores names of variables that WILL be passes as parameters when calling
998 * some method of a given object.
999 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
1000 * \param [in] theParameters - a string contating parameters separated by ':'.
1002 //================================================================================
1004 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
1006 SALOMEDS::Study_var aStudy = GetCurrentStudy();
1007 if ( aStudy->_is_nil() )
1010 // find variable names within theParameters
1013 myLastParameters.clear();
1014 myLastParamIndex.clear(); /* vector holding indices of virables within the string
1015 of all varibles used for theObject */
1017 int pos = 0, prevPos = 0, len = strlen( theParameters );
1018 if ( len == 0 ) return;
1019 while ( pos <= len )
1021 if ( pos == len || theParameters[pos] == ':' )
1023 if ( prevPos < pos )
1025 string val( theParameters + prevPos, theParameters + pos );
1026 if ( !aStudy->IsVariable( val.c_str() ))
1028 myLastParameters.push_back( val );
1029 nbVars += (! myLastParameters.back().empty() );
1033 myLastParameters.push_back("");
1044 // (1) variable names in the string of all varibles used for theObject and
1045 // (2) indices of found variables in myLastParamIndex.
1047 // remember theObject
1048 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
1049 if ( aSObj->_is_nil() )
1051 CORBA::String_var anObjEntry = aSObj->GetID();
1052 myLastObj = anObjEntry.in();
1054 // get a string of variable names
1055 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1056 SALOMEDS::GenericAttribute_wrap anAttr =
1057 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1058 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1059 CORBA::String_var oldVars = aStringAttr->Value();
1060 std::string varStr = oldVars.in();
1062 // add new variables and find indices of variables
1063 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1066 if ( !myLastParameters[i].empty() )
1068 // find index of myLastParameters[i] in varStr
1070 bool varFound = false;
1072 // varStr can be "A|B::C;*=2|D"
1073 const std::string separators(":|;*=");
1074 while ( pos < varStr.size() && !varFound )
1077 while ( separators.find( varStr[ pos ]) != std::string::npos )
1078 if ( ++pos >= varStr.size() )
1080 // skip repetition number following '='
1081 if ( varStr[ pos-1 ] == '=' )
1083 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1085 continue; // to skip next separator
1087 // compare variable name
1088 if ( pos < varStr.size() )
1090 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1091 // same string begining but is length same?
1092 ( pos + myLastParameters[i].size() >= varStr.size() ||
1093 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1095 varIndex = curIndex;
1097 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1104 varStr += ":" + myLastParameters[i];
1105 varIndex = curIndex;
1108 myLastParamIndex.push_back( varIndex );
1110 aStringAttr->SetValue( varStr.c_str() );
1113 //================================================================================
1115 * \brief Return all variables used to create an object
1116 * \param [in] theObjectEntry - an object entry in the current study
1117 * \return std::vector< std::string > - all variable names (or values of removed variables)
1119 //================================================================================
1121 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1123 std::vector< std::string > varNames;
1124 if ( myCurrentStudy->_is_nil() )
1127 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1128 if ( myCurrentStudy->_is_nil() )
1131 // get a string of variable names
1132 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1133 SALOMEDS::GenericAttribute_wrap anAttr =
1134 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1135 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1136 CORBA::String_var oldVars = aStringAttr->Value();
1137 std::string varStr = oldVars.in();
1139 // separate variables within varStr;
1140 // varStr can be "A|B::C;*=2|D"
1142 const std::string separators(":|;*=");
1143 while ( pos < varStr.size() )
1146 pos = varStr.find_first_not_of( separators, pos );
1147 // while ( separators.find( varStr[ pos ]) != std::string::npos )
1148 // if ( ++pos >= varStr.size() )
1150 // skip repetition number following '='
1151 if ( varStr[ pos-1 ] == '=' )
1153 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1155 continue; // to skip next separator
1157 // store variable name
1158 if ( pos < varStr.size() )
1160 size_t pos2 = varStr.find_first_of( separators, pos );
1161 varNames.push_back( varStr.substr( pos, pos2 - pos));
1168 //=======================================================================
1169 //function : ParseParameters
1170 //purpose : Replace variables by their values
1171 //=======================================================================
1172 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1174 // //const char* aParameters = theParameters;
1175 // // const char* aParameters = CORBA::string_dup(theParameters);
1176 // TCollection_AsciiString anInputParams;
1177 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1178 // if( !aStudy->_is_nil() ) {
1179 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1180 // // for(int j=0;j<aSections->length();j++) {
1181 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1182 // // for(int i=0;i<aVars.length();i++ ) {
1183 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1184 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1185 // // if(i != aVars.length()-1)
1186 // // anInputParams+=":";
1188 // // if(j!=aSections->length()-1)
1189 // // anInputParams+="|";
1191 // TCollection_AsciiString paramStr( theParameters );
1192 // int beg = 0, end;
1193 // char sep, *pParams = (char*)paramStr.ToCString();
1194 // while ( beg < paramStr.Length() )
1197 // while ( ++end < paramStr.Length() )
1198 // if ( pParams[end] == ':' || pParams[end] == '|')
1200 // if ( end < paramStr.Length())
1202 // sep = pParams[end];
1203 // pParams[end] = '\0';
1205 // if ( aStudy->IsVariable( pParams+beg ))
1206 // anInputParams += pParams+beg;
1207 // if ( end < paramStr.Length() )
1208 // anInputParams += sep;
1214 // return CORBA::string_dup(anInputParams.ToCString());
1217 //=======================================================================
1218 //function : GetParameters
1220 //=======================================================================
1222 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1224 CORBA::String_var aResult("");
1226 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1227 if ( !aSObj->_is_nil() )
1229 SALOMEDS::GenericAttribute_wrap attr;
1230 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1232 SALOMEDS::AttributeString_wrap strAttr = attr;
1233 aResult = strAttr->Value();
1236 return aResult._retn();