1 // Copyright (C) 2007-2012 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_Mesh_i.hxx"
30 #include "SMESH_Hypothesis_i.hxx"
31 #include "SMESH_Algo_i.hxx"
32 #include "SMESH_Group_i.hxx"
33 #include "SMESH_subMesh_i.hxx"
35 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
37 #include "utilities.h"
38 #include "Utils_ExceptHandlers.hxx"
40 #include <TCollection_AsciiString.hxx>
41 #include <TopoDS_Solid.hxx>
44 static int MYDEBUG = 0;
45 //static int VARIABLE_DEBUG = 0;
47 static int MYDEBUG = 0;
48 //static int VARIABLE_DEBUG = 0;
51 //=============================================================================
53 * Get...Tag [ static ]
55 * Methods which determine SMESH data model structure
57 //=============================================================================
59 long SMESH_Gen_i::GetHypothesisRootTag()
61 return SMESH::Tag_HypothesisRoot;
64 long SMESH_Gen_i::GetAlgorithmsRootTag()
66 return SMESH::Tag_AlgorithmsRoot;
69 long SMESH_Gen_i::GetRefOnShapeTag()
71 return SMESH::Tag_RefOnShape;
74 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
76 return SMESH::Tag_RefOnAppliedHypothesis;
79 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
81 return SMESH::Tag_RefOnAppliedAlgorithms;
84 long SMESH_Gen_i::GetSubMeshOnVertexTag()
86 return SMESH::Tag_SubMeshOnVertex;
89 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
91 return SMESH::Tag_SubMeshOnEdge;
94 long SMESH_Gen_i::GetSubMeshOnFaceTag()
96 return SMESH::Tag_SubMeshOnFace;
99 long SMESH_Gen_i::GetSubMeshOnSolidTag()
101 return SMESH::Tag_SubMeshOnSolid;
104 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
106 return SMESH::Tag_SubMeshOnCompound;
109 long SMESH_Gen_i::GetSubMeshOnWireTag()
111 return SMESH::Tag_SubMeshOnWire;
114 long SMESH_Gen_i::GetSubMeshOnShellTag()
116 return SMESH::Tag_SubMeshOnShell;
119 long SMESH_Gen_i::GetNodeGroupsTag()
121 return SMESH::Tag_NodeGroups;
124 long SMESH_Gen_i::GetEdgeGroupsTag()
126 return SMESH::Tag_EdgeGroups;
129 long SMESH_Gen_i::GetFaceGroupsTag()
131 return SMESH::Tag_FaceGroups;
134 long SMESH_Gen_i::GetVolumeGroupsTag()
136 return SMESH::Tag_VolumeGroups;
139 long SMESH_Gen_i::Get0DElementsGroupsTag()
141 return SMESH::Tag_0DElementsGroups;
144 long SMESH_Gen_i::GetBallElementsGroupsTag()
146 return SMESH::Tag_BallElementsGroups;
149 //=============================================================================
151 * SMESH_Gen_i::CanPublishInStudy
153 * Returns true if object can be published in the study
155 //=============================================================================
157 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
159 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
160 if(CORBA::is_nil(myCurrentStudy))
163 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
164 if( !aMesh->_is_nil() )
167 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
168 if( !aSubMesh->_is_nil() )
171 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
172 if( !aHyp->_is_nil() )
175 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
176 if( !aGroup->_is_nil() )
179 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
183 //=======================================================================
184 //function : ObjectToSObject
186 //=======================================================================
188 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
189 CORBA::Object_ptr theObject)
191 SALOMEDS::SObject_var aSO;
192 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
194 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
195 aSO = theStudy->FindObjectIOR( objStr.in() );
200 //=======================================================================
201 //function : objectToServant
203 //=======================================================================
205 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
207 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
210 //=======================================================================
211 //function : ShapeToGeomObject
213 //=======================================================================
215 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
217 GEOM::GEOM_Object_var aShapeObj;
218 if ( !theShape.IsNull() ) {
219 GEOM_Client* aClient = GetShapeReader();
220 TCollection_AsciiString IOR;
221 if ( aClient && aClient->Find( theShape, IOR ))
223 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
224 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
227 return aShapeObj._retn();
230 //=======================================================================
231 //function : GeomObjectToShape
233 //=======================================================================
235 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
238 if ( !theGeomObject->_is_nil() ) {
239 GEOM_Client* aClient = GetShapeReader();
240 GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
241 if ( aClient && !aGeomEngine->_is_nil () )
242 S = aClient->GetShape( aGeomEngine, theGeomObject );
247 //=======================================================================
250 //=======================================================================
252 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr theStudy,
253 CORBA::Object_ptr theIOR,
254 SALOMEDS::SObject_ptr theFatherObject,
255 const int theTag = 0,
256 const char* thePixMap = 0,
257 const bool theSelectable = true)
259 SALOMEDS::SObject_var SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
260 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
261 if ( SO->_is_nil() ) {
263 SO = aStudyBuilder->NewObject( theFatherObject );
264 else if ( !theFatherObject->FindSubObject( theTag, SO ))
265 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
268 SALOMEDS::GenericAttribute_var anAttr;
269 if ( !CORBA::is_nil( theIOR )) {
270 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
271 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
272 SALOMEDS::AttributeIOR::_narrow(anAttr)->SetValue( objStr.in() );
275 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
276 SALOMEDS::AttributePixMap_var pm = SALOMEDS::AttributePixMap::_narrow( anAttr );
277 pm->SetPixMap( thePixMap );
279 if ( !theSelectable ) {
280 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
281 SALOMEDS::AttributeSelectable::_narrow( anAttr )->SetSelectable( false );
286 //=======================================================================
289 //=======================================================================
291 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
293 const char* theDefaultName)
295 if ( !theSObject->_is_nil() ) {
296 SALOMEDS::StudyBuilder_var aStudyBuilder = theSObject->GetStudy()->NewBuilder();
297 SALOMEDS::GenericAttribute_var anAttr =
298 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
299 SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow( anAttr );
300 if ( theName && strlen( theName ) != 0 )
301 aNameAttr->SetValue( theName );
303 CORBA::String_var curName = CORBA::string_dup( aNameAttr->Value() );
304 if ( strlen( curName ) == 0 ) {
305 TCollection_AsciiString aName( (char*) theDefaultName );
306 aName += TCollection_AsciiString("_") + TCollection_AsciiString( theSObject->Tag() );
307 aNameAttr->SetValue( aName.ToCString() );
313 //=======================================================================
314 //function : SetPixMap
316 //=======================================================================
318 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
319 const char* thePixMap)
321 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
323 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
324 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
325 SALOMEDS::GenericAttribute_var anAttr =
326 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
327 SALOMEDS::AttributePixMap_var aPMAttr = SALOMEDS::AttributePixMap::_narrow( anAttr );
328 aPMAttr->SetPixMap( thePixMap );
332 //=======================================================================
333 //function : addReference
335 //=======================================================================
337 static void addReference (SALOMEDS::Study_ptr theStudy,
338 SALOMEDS::SObject_ptr theSObject,
339 CORBA::Object_ptr theToObject,
342 SALOMEDS::SObject_var aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
343 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
344 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
345 SALOMEDS::SObject_var aReferenceSO;
347 // check if the reference to theToObject already exists
348 // and find a free label for the reference object
349 bool isReferred = false;
351 SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( theSObject );
352 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
353 if ( anIter->Value()->ReferencedObject( aReferenceSO )) {
354 if ( strcmp( aReferenceSO->GetID(), aToObjSO->GetID() ) == 0 )
357 else if ( !theTag ) {
358 SALOMEDS::GenericAttribute_var anAttr;
359 if ( !anIter->Value()->FindAttribute( anAttr, "AttributeIOR" ))
368 if ( !theSObject->FindSubObject( theTag, aReferenceSO ))
369 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
370 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
374 //=============================================================================
376 * SMESH_Gen_i::PublishInStudy
378 * Publish object in the study
380 //=============================================================================
382 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
383 SALOMEDS::SObject_ptr theSObject,
384 CORBA::Object_ptr theIOR,
386 throw (SALOME::SALOME_Exception)
388 Unexpect aCatch(SALOME_SalomeException);
389 SALOMEDS::SObject_var aSO;
390 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
392 if(MYDEBUG) MESSAGE("PublishInStudy");
395 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
396 if( !aMesh->_is_nil() )
397 aSO = PublishMesh( theStudy, aMesh, theName );
399 // Publishing a sub-mesh
400 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
401 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
402 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
403 aMesh = aSubMesh->GetFather();
404 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
407 // Publishing a hypothesis or algorithm
408 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
409 if ( aSO->_is_nil() && !aHyp->_is_nil() )
410 aSO = PublishHypothesis( theStudy, aHyp );
412 // Publishing a group
413 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
414 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
415 GEOM::GEOM_Object_var aShapeObject;
416 aMesh = aGroup->GetMesh();
417 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
419 if(MYDEBUG) MESSAGE("PublishInStudy_END");
424 //=======================================================================
425 //function : PublishComponent
427 //=======================================================================
429 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
431 if ( CORBA::is_nil( theStudy ))
432 return SALOMEDS::SComponent::_nil();
433 if(MYDEBUG) MESSAGE("PublishComponent");
435 SALOMEDS::SComponent_var father =
436 SALOMEDS::SComponent::_narrow( theStudy->FindComponent( ComponentDataType() ) );
437 if ( !CORBA::is_nil( father ) )
438 return father._retn();
440 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
441 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
442 if ( CORBA::is_nil( aCat ) )
443 return father._retn();
445 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( ComponentDataType() );
446 if ( CORBA::is_nil( aComp ) )
447 return father._retn();
449 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
450 SALOMEDS::GenericAttribute_var anAttr;
451 SALOMEDS::AttributePixMap_var aPixmap;
453 father = aStudyBuilder->NewComponent( ComponentDataType() );
454 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
455 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
456 aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
457 aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
458 SetName( father, aComp->componentusername(), "MESH" );
459 if(MYDEBUG) MESSAGE("PublishComponent--END");
461 return father._retn();
464 //=============================================================================
466 * findMaxChildTag [ static internal ]
468 * Finds maximum child tag for the given object
470 //=============================================================================
472 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
475 if ( !theSObject->_is_nil() ) {
476 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
477 if ( !aStudy->_is_nil() ) {
478 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
479 for ( ; anIter->More(); anIter->Next() ) {
480 long nTag = anIter->Value()->Tag();
489 //=======================================================================
490 //function : PublishMesh
492 //=======================================================================
494 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
495 SMESH::SMESH_Mesh_ptr theMesh,
498 if ( CORBA::is_nil( theStudy ) ||
499 CORBA::is_nil( theMesh ))
500 return SALOMEDS::SComponent::_nil();
501 if(MYDEBUG) MESSAGE("PublishMesh--IN");
503 // find or publish a mesh
505 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
506 if ( aMeshSO->_is_nil() )
508 SALOMEDS::SComponent_var father = PublishComponent( theStudy );
509 if ( father->_is_nil() )
510 return aMeshSO._retn();
512 // Find correct free tag
513 long aTag = findMaxChildTag( father.in() );
514 if ( aTag <= GetAlgorithmsRootTag() )
515 aTag = GetAlgorithmsRootTag() + 1;
519 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
520 if ( aMeshSO->_is_nil() )
521 return aMeshSO._retn();
523 SetName( aMeshSO, theName, "Mesh" );
525 // Add shape reference
527 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
528 if ( !CORBA::is_nil( aShapeObject )) {
529 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
531 // Publish global hypotheses
533 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
534 for ( int i = 0; i < hypList->length(); i++ ) {
535 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
536 PublishHypothesis( theStudy, aHyp );
537 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
543 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
545 return aMeshSO._retn();
546 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
547 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
548 for ( ; subIt != subMap.end(); subIt++ ) {
549 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
550 if ( !CORBA::is_nil( aSubMesh )) {
551 aShapeObject = aSubMesh->GetSubShape();
552 PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject );
557 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
558 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
559 for ( ; it != grMap.end(); it++ )
561 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
562 if ( !aGroup->_is_nil() ) {
563 GEOM::GEOM_Object_var aShapeObj;
564 SMESH::SMESH_GroupOnGeom_var aGeomGroup =
565 SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
566 if ( !aGeomGroup->_is_nil() )
567 aShapeObj = aGeomGroup->GetShape();
568 PublishGroup( theStudy, theMesh, aGroup, aShapeObj );
572 if(MYDEBUG) MESSAGE("PublishMesh_END");
573 return aMeshSO._retn();
576 //=======================================================================
577 //function : PublishSubMesh
579 //=======================================================================
581 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
582 SMESH::SMESH_Mesh_ptr theMesh,
583 SMESH::SMESH_subMesh_ptr theSubMesh,
584 GEOM::GEOM_Object_ptr theShapeObject,
587 if (theStudy->_is_nil() || theMesh->_is_nil() ||
588 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
589 return SALOMEDS::SObject::_nil();
591 SALOMEDS::SObject_var aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
592 if ( aSubMeshSO->_is_nil() )
594 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
595 if ( aMeshSO->_is_nil() ) {
596 aMeshSO = PublishMesh( theStudy, theMesh );
597 if ( aMeshSO->_is_nil())
598 return SALOMEDS::SObject::_nil();
600 // Find submesh sub-tree tag
602 const char* aRootName = "";
603 switch ( theShapeObject->GetShapeType() ) {
605 aRootTag = GetSubMeshOnVertexTag();
606 aRootName = "SubMeshes on Vertex";
609 aRootTag = GetSubMeshOnEdgeTag();
610 aRootName = "SubMeshes on Edge";
613 aRootTag = GetSubMeshOnWireTag();
614 aRootName = "SubMeshes on Wire";
617 aRootTag = GetSubMeshOnFaceTag();
618 aRootName = "SubMeshes on Face";
621 aRootTag = GetSubMeshOnShellTag();
622 aRootName = "SubMeshes on Shell";
625 aRootTag = GetSubMeshOnSolidTag();
626 aRootName = "SubMeshes on Solid";
629 aRootTag = GetSubMeshOnCompoundTag();
630 aRootName = "SubMeshes on Compound";
634 // Find or create submesh root
635 SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
636 aMeshSO, aRootTag, 0, false );
637 SetName( aRootSO, aRootName );
639 // Add new submesh to corresponding sub-tree
640 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
641 const int isEmpty = ( elemTypes->length() == 0 );
642 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
643 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
644 if ( aSubMeshSO->_is_nil() )
645 return aSubMeshSO._retn();
647 SetName( aSubMeshSO, theName, "SubMesh" );
649 // Add reference to theShapeObject
651 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
653 // Publish hypothesis
655 SMESH::ListOfHypothesis * hypList = theMesh->GetHypothesisList( theShapeObject );
657 for ( int i = 0; i < hypList->length(); i++ ) {
658 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( (*hypList)[ i ]);
659 PublishHypothesis( theStudy, aHyp );
660 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
663 return aSubMeshSO._retn();
666 //=======================================================================
667 //function : PublishGroup
669 //=======================================================================
671 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
672 SMESH::SMESH_Mesh_ptr theMesh,
673 SMESH::SMESH_GroupBase_ptr theGroup,
674 GEOM::GEOM_Object_ptr theShapeObject,
677 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
678 return SALOMEDS::SObject::_nil();
680 SALOMEDS::SObject_var aGroupSO = ObjectToSObject( theStudy, theGroup );
681 if ( aGroupSO->_is_nil() )
683 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
684 if ( aMeshSO->_is_nil() ) {
685 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
686 if ( aMeshSO->_is_nil())
687 return SALOMEDS::SObject::_nil();
689 int aType = (int)theGroup->GetType();
690 const char* aRootNames[] = {
691 "Compound Groups", "Groups of Nodes", "Groups of Edges",
692 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
695 // Currently, groups with heterogenous content are not supported
696 if ( aType != SMESH::ALL )
698 long aRootTag = GetNodeGroupsTag() + aType - 1;
700 // Find or create groups root
701 SALOMEDS::SObject_var aRootSO = publish (theStudy, CORBA::Object::_nil(),
702 aMeshSO, aRootTag, 0, false );
703 if ( aType < sizeof(aRootNames)/sizeof(char*) )
704 SetName( aRootSO, aRootNames[aType] );
706 // Add new group to corresponding sub-tree
707 SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
708 int isEmpty = ( elemTypes->length() == 0 );
709 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
710 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
712 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
714 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
716 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
717 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
718 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
720 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
722 if ( aGroupSO->_is_nil() )
723 return aGroupSO._retn();
726 SetName( aGroupSO, theName, "Group" );
728 //Add reference to geometry
729 if ( !theShapeObject->_is_nil() )
730 addReference( theStudy, aGroupSO, theShapeObject, 1 );
732 return aGroupSO._retn();
735 //=======================================================================
736 //function : PublishHypothesis
738 //=======================================================================
740 SALOMEDS::SObject_ptr
741 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
742 SMESH::SMESH_Hypothesis_ptr theHyp,
745 if(MYDEBUG) MESSAGE("PublishHypothesis")
746 if (theStudy->_is_nil() || theHyp->_is_nil())
747 return SALOMEDS::SObject::_nil();
749 SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
750 if ( aHypSO->_is_nil() )
752 SALOMEDS::SComponent_var father = PublishComponent( theStudy );
753 if ( father->_is_nil() )
754 return aHypSO._retn();
756 //Find or Create Hypothesis root
757 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
758 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
759 SALOMEDS::SObject_var aRootSO =
760 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
761 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
762 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
764 // Add New Hypothesis
765 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
766 aPmName += theHyp->GetName();
767 // prepend plugin name to pixmap name
768 string pluginName = myHypCreatorMap[string(theHyp->GetName())]->GetModuleName();
769 if ( pluginName != "StdMeshers" )
770 aPmName = pluginName + "::" + aPmName;
771 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
774 if ( !aHypSO->_is_nil() ) {
775 CORBA::String_var aHypName = CORBA::string_dup( theHyp->GetName() );
776 SetName( aHypSO, theName, aHypName );
779 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
780 return aHypSO._retn();
783 //=======================================================================
784 //function : GetMeshOrSubmeshByShape
786 //=======================================================================
788 SALOMEDS::SObject_ptr
789 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
790 SMESH::SMESH_Mesh_ptr theMesh,
791 GEOM::GEOM_Object_ptr theShape)
793 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
794 SALOMEDS::SObject_var aMeshOrSubMesh;
795 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
796 return aMeshOrSubMesh._retn();
799 if(theMesh->HasShapeToMesh())
800 aShape = GeomObjectToShape( theShape );
802 aShape = SMESH_Mesh::PseudoShape();
804 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
806 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
807 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
808 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
809 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
811 int shapeID = meshDS->ShapeToIndex( aShape );
812 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
813 if ( !aSubMesh->_is_nil() )
814 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
817 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
818 return aMeshOrSubMesh._retn();
821 //=======================================================================
822 //function : AddHypothesisToShape
824 //=======================================================================
826 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
827 SMESH::SMESH_Mesh_ptr theMesh,
828 GEOM::GEOM_Object_ptr theShape,
829 SMESH::SMESH_Hypothesis_ptr theHyp)
831 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
832 if (theStudy->_is_nil() || theMesh->_is_nil() ||
833 theHyp->_is_nil() || (theShape->_is_nil()
834 && theMesh->HasShapeToMesh()) )
837 SALOMEDS::SObject_var aMeshSO = ObjectToSObject( theStudy, theMesh );
838 if ( aMeshSO->_is_nil() )
839 aMeshSO = PublishMesh( theStudy, theMesh );
840 SALOMEDS::SObject_var aHypSO = PublishHypothesis( theStudy, theHyp );
841 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
844 // Find a mesh or submesh refering to theShape
845 SALOMEDS::SObject_var aMeshOrSubMesh =
846 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
847 if ( aMeshOrSubMesh->_is_nil() )
850 TopoDS_Shape aShape = GeomObjectToShape( theShape );
851 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
852 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
853 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
854 int shapeID = meshDS->ShapeToIndex( aShape );
855 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
856 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
858 if ( aMeshOrSubMesh->_is_nil() )
862 //Find or Create Applied Hypothesis root
863 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
864 SALOMEDS::SObject_var AHR =
865 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
866 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
867 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
868 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
869 if ( AHR->_is_nil() )
872 addReference( theStudy, AHR, theHyp );
873 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
877 //=======================================================================
878 //function : RemoveHypothesisFromShape
880 //=======================================================================
882 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
883 SMESH::SMESH_Mesh_ptr theMesh,
884 GEOM::GEOM_Object_ptr theShape,
885 SMESH::SMESH_Hypothesis_ptr theHyp)
887 if (theStudy->_is_nil() || theMesh->_is_nil() ||
888 theHyp->_is_nil() || (theShape->_is_nil()
889 && theMesh->HasShapeToMesh()))
892 SALOMEDS::SObject_var aHypSO = ObjectToSObject( theStudy, theHyp );
893 if ( aHypSO->_is_nil() )
896 // Find a mesh or submesh refering to theShape
897 SALOMEDS::SObject_var aMeshOrSubMesh =
898 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
899 if ( aMeshOrSubMesh->_is_nil() )
902 // Find and remove a reference to aHypSO
903 SALOMEDS::SObject_var aRef, anObj;
904 CORBA::String_var anID = CORBA::string_dup( aHypSO->GetID() );
905 SALOMEDS::ChildIterator_var it = theStudy->NewChildIterator( aMeshOrSubMesh );
906 for ( it->InitEx( true ); it->More(); it->Next() ) {
908 if (anObj->ReferencedObject( aRef ) && strcmp( aRef->GetID(), anID ) == 0 ) {
909 theStudy->NewBuilder()->RemoveObject( anObj );
916 //=======================================================================
917 //function : UpdateParameters
919 //=======================================================================
920 void SMESH_Gen_i::UpdateParameters(/*CORBA::Object_ptr theObject,*/ const char* theParameters)
922 SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
923 if ( aStudy->_is_nil() )
925 myLastParameters.clear();
926 int pos = 0, prevPos = 0, len = strlen( theParameters );
927 if ( len == 0 ) return;
930 if ( pos == len || theParameters[pos] == ':' )
934 string val(theParameters + prevPos, theParameters + pos );
935 if ( !aStudy->IsVariable( val.c_str() ))
937 myLastParameters.push_back( val );
941 myLastParameters.push_back("");
951 // if(VARIABLE_DEBUG)
952 // cout<<"UpdateParameters : "<<theParameters<<endl;
953 // //SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
954 // if(aStudy->_is_nil() || CORBA::is_nil(theObject))
957 // SALOMEDS::SObject_var aSObj = ObjectToSObject(aStudy,theObject);
958 // if(aSObj->_is_nil())
961 // SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
963 // SALOMEDS::GenericAttribute_var aFindAttr;
964 // bool hasAttr = aSObj->FindAttribute(aFindAttr, "AttributeString");
965 // if(VARIABLE_DEBUG)
966 // cout<<"Find Attribute "<<hasAttr<<endl;
968 // SALOMEDS::GenericAttribute_var anAttr;
969 // anAttr = aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString");
970 // SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
972 // CORBA::String_var oldparVar = aStringAttr->Value();
973 // CORBA::String_var inpparVar = ParseParameters(theParameters);
974 // TCollection_AsciiString aNewParams;
975 // TCollection_AsciiString aOldParameters(oldparVar.inout());
976 // TCollection_AsciiString anInputParams(inpparVar.inout());
978 // aNewParams = anInputParams;
981 // int pos = aOldParameters.SearchFromEnd("|");
982 // if(pos==-1) pos = 0;
983 // TCollection_AsciiString previousParamFull(aOldParameters.Split(pos));
984 // TCollection_AsciiString previousParam(previousParamFull);
985 // TCollection_AsciiString theRepet("1");
986 // pos = previousParam.SearchFromEnd(";*=");
989 // theRepet = previousParam.Split(pos+2);
991 // if(pos==-1) pos = 0;
992 // previousParam.Split(pos);
994 // if(previousParam == anInputParams)
996 // theRepet = theRepet.IntegerValue()+1;
997 // aNewParams = aOldParameters + previousParam + ";*=" + theRepet;
1001 // aNewParams = aOldParameters + previousParamFull + "|" + anInputParams;
1005 // if(VARIABLE_DEBUG)
1007 // cout<<"Input Parameters : "<<anInputParams<<endl;
1008 // cout<<"Old Parameters : "<<aOldParameters<<endl;
1009 // cout<<"New Parameters : "<<aNewParams<<endl;
1012 // aStringAttr->SetValue( aNewParams.ToCString() );
1015 //=======================================================================
1016 //function : ParseParameters
1017 //purpose : Replace variables by their values
1018 //=======================================================================
1019 char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1021 //const char* aParameters = theParameters;
1022 // const char* aParameters = CORBA::string_dup(theParameters);
1023 TCollection_AsciiString anInputParams;
1024 SALOMEDS::Study_var aStudy = GetCurrentStudy();
1025 if( !aStudy->_is_nil() ) {
1026 // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1027 // for(int j=0;j<aSections->length();j++) {
1028 // SALOMEDS::ListOfStrings aVars= aSections[j];
1029 // for(int i=0;i<aVars.length();i++ ) {
1030 // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1031 // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1032 // if(i != aVars.length()-1)
1033 // anInputParams+=":";
1035 // if(j!=aSections->length()-1)
1036 // anInputParams+="|";
1038 TCollection_AsciiString paramStr( theParameters );
1039 static TCollection_AsciiString separators(":|");
1041 char sep, *pParams = (char*)paramStr.ToCString();
1042 while ( beg < paramStr.Length() )
1045 while ( ++end < paramStr.Length() )
1046 if ( pParams[end] == ':' || pParams[end] == '|')
1048 if ( end < paramStr.Length())
1051 pParams[end] = '\0';
1053 if ( aStudy->IsVariable( pParams+beg ))
1054 anInputParams += pParams+beg;
1055 if ( end < paramStr.Length() )
1056 anInputParams += sep;
1062 return CORBA::string_dup(anInputParams.ToCString());
1065 //=======================================================================
1066 //function : GetParameters
1068 //=======================================================================
1069 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1071 TCollection_AsciiString aResult;
1073 SALOMEDS::Study_ptr aStudy = GetCurrentStudy();
1074 SALOMEDS::SObject_var aSObj = ObjectToSObject(aStudy,theObject);
1076 if(!aStudy->_is_nil() &&
1077 !CORBA::is_nil(theObject) &&
1080 SALOMEDS::GenericAttribute_var anAttr;
1081 if ( aSObj->FindAttribute(anAttr, "AttributeString")) {
1082 aResult = TCollection_AsciiString(SALOMEDS::AttributeString::_narrow(anAttr)->Value());
1086 return CORBA::string_dup( aResult.ToCString() );