1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_Gen_i_1.cxx
23 // Created : Thu Oct 21 17:24:06 2004
24 // Author : Edward AGAPOV (eap)
27 #include "SMESH_Gen_i.hxx"
29 #include "SMESH_Algo_i.hxx"
30 #include "SMESH_Comment.hxx"
31 #include "SMESH_Group_i.hxx"
32 #include "SMESH_Hypothesis_i.hxx"
33 #include "SMESH_Mesh_i.hxx"
34 #include "SMESH_subMesh_i.hxx"
36 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
38 #include <utilities.h>
39 #include <Utils_ExceptHandlers.hxx>
40 #include <SALOMEDS_wrap.hxx>
41 #include <SALOMEDS_Attributes_wrap.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TopoDS_Solid.hxx>
49 static int MYDEBUG = 0;
50 //static int VARIABLE_DEBUG = 0;
52 static int MYDEBUG = 0;
53 //static int VARIABLE_DEBUG = 0;
56 //=============================================================================
58 * Get...Tag [ static ]
60 * Methods which determine SMESH data model structure
62 //=============================================================================
64 long SMESH_Gen_i::GetHypothesisRootTag()
66 return SMESH::Tag_HypothesisRoot;
69 long SMESH_Gen_i::GetAlgorithmsRootTag()
71 return SMESH::Tag_AlgorithmsRoot;
74 long SMESH_Gen_i::GetRefOnShapeTag()
76 return SMESH::Tag_RefOnShape;
79 long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
81 return SMESH::Tag_RefOnAppliedHypothesis;
84 long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
86 return SMESH::Tag_RefOnAppliedAlgorithms;
89 long SMESH_Gen_i::GetSubMeshOnVertexTag()
91 return SMESH::Tag_SubMeshOnVertex;
94 long SMESH_Gen_i::GetSubMeshOnEdgeTag()
96 return SMESH::Tag_SubMeshOnEdge;
99 long SMESH_Gen_i::GetSubMeshOnFaceTag()
101 return SMESH::Tag_SubMeshOnFace;
104 long SMESH_Gen_i::GetSubMeshOnSolidTag()
106 return SMESH::Tag_SubMeshOnSolid;
109 long SMESH_Gen_i::GetSubMeshOnCompoundTag()
111 return SMESH::Tag_SubMeshOnCompound;
114 long SMESH_Gen_i::GetSubMeshOnWireTag()
116 return SMESH::Tag_SubMeshOnWire;
119 long SMESH_Gen_i::GetSubMeshOnShellTag()
121 return SMESH::Tag_SubMeshOnShell;
124 long SMESH_Gen_i::GetNodeGroupsTag()
126 return SMESH::Tag_NodeGroups;
129 long SMESH_Gen_i::GetEdgeGroupsTag()
131 return SMESH::Tag_EdgeGroups;
134 long SMESH_Gen_i::GetFaceGroupsTag()
136 return SMESH::Tag_FaceGroups;
139 long SMESH_Gen_i::GetVolumeGroupsTag()
141 return SMESH::Tag_VolumeGroups;
144 long SMESH_Gen_i::Get0DElementsGroupsTag()
146 return SMESH::Tag_0DElementsGroups;
149 long SMESH_Gen_i::GetBallElementsGroupsTag()
151 return SMESH::Tag_BallElementsGroups;
154 //=============================================================================
156 * SMESH_Gen_i::CanPublishInStudy
158 * Returns true if object can be published in the study
160 //=============================================================================
162 bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
164 if(MYDEBUG) MESSAGE("CanPublishInStudy - "<<!CORBA::is_nil(myCurrentStudy));
165 if( GetCurrentStudyID() < 0 )
168 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(theIOR);
169 if( !aMesh->_is_nil() )
172 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
173 if( !aSubMesh->_is_nil() )
176 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(theIOR);
177 if( !aHyp->_is_nil() )
180 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
181 if( !aGroup->_is_nil() )
184 if(MYDEBUG) MESSAGE("CanPublishInStudy--CANT");
188 //=======================================================================
189 //function : ObjectToSObject
190 //purpose : Put a result into a SALOMEDS::SObject_wrap or call UnRegister()!
191 //=======================================================================
193 SALOMEDS::SObject_ptr SMESH_Gen_i::ObjectToSObject(SALOMEDS::Study_ptr theStudy,
194 CORBA::Object_ptr theObject)
196 SALOMEDS::SObject_wrap aSO;
197 if ( !CORBA::is_nil( theStudy ) && !CORBA::is_nil( theObject ))
199 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theObject );
200 aSO = theStudy->FindObjectIOR( objStr.in() );
205 //=======================================================================
206 //function : objectToServant
208 //=======================================================================
210 template<typename T> static inline T* objectToServant( CORBA::Object_ptr theIOR )
212 return dynamic_cast<T*>( SMESH_Gen_i::GetServant( theIOR ).in() );
215 //=======================================================================
216 //function : ShapeToGeomObject
218 //=======================================================================
220 GEOM::GEOM_Object_ptr SMESH_Gen_i::ShapeToGeomObject (const TopoDS_Shape& theShape )
222 GEOM::GEOM_Object_var aShapeObj;
223 if ( !theShape.IsNull() ) {
224 GEOM_Client* aClient = GetShapeReader();
225 TCollection_AsciiString IOR;
226 if ( aClient && aClient->Find( theShape, IOR ))
228 CORBA::Object_var obj = GetORB()->string_to_object( IOR.ToCString() );
229 aShapeObj = GEOM::GEOM_Object::_narrow ( obj );
232 return aShapeObj._retn();
235 //=======================================================================
236 //function : GeomObjectToShape
238 //=======================================================================
240 TopoDS_Shape SMESH_Gen_i::GeomObjectToShape(GEOM::GEOM_Object_ptr theGeomObject)
243 if ( !theGeomObject->_is_nil() ) {
244 GEOM_Client* aClient = GetShapeReader();
245 GEOM::GEOM_Gen_ptr aGeomEngine = GetGeomEngine();
246 if ( aClient && !aGeomEngine->_is_nil () )
247 S = aClient->GetShape( aGeomEngine, theGeomObject );
252 //=======================================================================
255 //=======================================================================
257 static SALOMEDS::SObject_ptr publish(SALOMEDS::Study_ptr theStudy,
258 CORBA::Object_ptr theIOR,
259 SALOMEDS::SObject_ptr theFatherObject,
260 const int theTag = 0,
261 const char* thePixMap = 0,
262 const bool theSelectable = true)
264 SALOMEDS::SObject_wrap SO = SMESH_Gen_i::ObjectToSObject( theStudy, theIOR );
265 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
266 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
267 SALOMEDS::SObject_wrap objAfter;
271 SO = aStudyBuilder->NewObject( theFatherObject );
273 else if ( !theFatherObject->FindSubObject( theTag, SO.inout() ))
275 SO = aStudyBuilder->NewObjectToTag( theFatherObject, theTag );
277 // define the next tag after given one in the data tree to insert SObject
279 int last2Pnt_pos = -1;
281 CORBA::String_var entry;
282 SALOMEDS::SObject_wrap curObj;
283 SALOMEDS::UseCaseIterator_wrap anUseCaseIter = useCaseBuilder->GetUseCaseIterator(theFatherObject);
284 for ( ; anUseCaseIter->More(); anUseCaseIter->Next() ) {
285 curObj = anUseCaseIter->Value();
286 entry = curObj->GetID();
287 anEntry = entry.in();
288 last2Pnt_pos = anEntry.rfind( ":" );
289 tagAfter = atoi( anEntry.substr( last2Pnt_pos+1 ).c_str() );
290 if ( tagAfter > theTag ) {
298 SALOMEDS::GenericAttribute_wrap anAttr;
299 if ( !CORBA::is_nil( theIOR )) {
300 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeIOR" );
301 CORBA::String_var objStr = SMESH_Gen_i::GetORB()->object_to_string( theIOR );
302 SALOMEDS::AttributeIOR_wrap iorAttr = anAttr;
303 iorAttr->SetValue( objStr.in() );
305 SALOME::GenericObj_var genObj = SALOME::GenericObj::_narrow( theIOR );
306 if ( !genObj->_is_nil() )
307 genObj->UnRegister();
310 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributePixMap" );
311 SALOMEDS::AttributePixMap_wrap pm = anAttr;
312 pm->SetPixMap( thePixMap );
314 if ( !theSelectable ) {
315 anAttr = aStudyBuilder->FindOrCreateAttribute( SO, "AttributeSelectable" );
316 SALOMEDS::AttributeSelectable_wrap selAttr = anAttr;
317 selAttr->SetSelectable( false );
320 // add object to the use case tree
321 // (to support tree representation customization and drag-n-drop)
322 if ( !CORBA::is_nil( objAfter ) ) {
323 useCaseBuilder->InsertBefore( SO, objAfter ); // insert at given tag
324 } else if ( !useCaseBuilder->IsUseCaseNode( SO ) ) {
325 useCaseBuilder->AppendTo( theFatherObject, SO ); // append to the end of list
331 //=======================================================================
334 //=======================================================================
336 void SMESH_Gen_i::SetName(SALOMEDS::SObject_ptr theSObject,
338 const char* theDefaultName)
340 if ( !theSObject->_is_nil() ) {
341 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
342 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
343 SALOMEDS::GenericAttribute_wrap anAttr =
344 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributeName" );
345 SALOMEDS::AttributeName_wrap aNameAttr = anAttr;
346 if ( theName && theName[0] ) {
347 std::string name( theName ); // trim trailing white spaces
348 for ( size_t i = name.size()-1; i > 0; --i )
349 if ( isspace( name[i] )) name[i] = '\0';
351 aNameAttr->SetValue( name.c_str() );
355 CORBA::String_var curName = aNameAttr->Value();
356 if ( strlen( curName.in() ) == 0 ) {
357 SMESH_Comment aName(theDefaultName);
358 aNameAttr->SetValue( ( aName<< "_" << theSObject->Tag()).c_str() );
364 //=======================================================================
365 //function : SetPixMap
367 //=======================================================================
369 void SMESH_Gen_i::SetPixMap(SALOMEDS::SObject_ptr theSObject,
370 const char* thePixMap)
372 if ( !theSObject->_is_nil() && thePixMap && strlen( thePixMap ))
374 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
375 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
376 SALOMEDS::GenericAttribute_wrap anAttr =
377 aStudyBuilder->FindOrCreateAttribute( theSObject, "AttributePixMap" );
378 SALOMEDS::AttributePixMap_wrap aPMAttr = anAttr;
379 aPMAttr->SetPixMap( thePixMap );
383 //=======================================================================
384 //function : addReference
386 //=======================================================================
388 static void addReference (SALOMEDS::Study_ptr theStudy,
389 SALOMEDS::SObject_ptr theSObject,
390 CORBA::Object_ptr theToObject,
393 SALOMEDS::SObject_wrap aToObjSO = SMESH_Gen_i::ObjectToSObject( theStudy, theToObject );
394 if ( !aToObjSO->_is_nil() && !theSObject->_is_nil() ) {
395 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
396 SALOMEDS::SObject_wrap aReferenceSO;
398 // check if the reference to theToObject already exists
399 // and find a free label for the reference object
400 bool isReferred = false;
402 SALOMEDS::ChildIterator_wrap anIter = theStudy->NewChildIterator( theSObject );
403 for ( ; !isReferred && anIter->More(); anIter->Next(), ++tag ) {
404 SALOMEDS::SObject_wrap curSO = anIter->Value();
405 if ( curSO->ReferencedObject( aReferenceSO.inout() )) {
406 CORBA::String_var refEntry = aReferenceSO->GetID();
407 CORBA::String_var toEntry = aToObjSO->GetID();
408 if ( strcmp( refEntry, toEntry ) == 0 )
411 else if ( !theTag ) {
412 SALOMEDS::GenericAttribute_wrap anAttr;
413 if ( !curSO->FindAttribute( anAttr.inout(), "AttributeIOR" ))
422 if ( !theSObject->FindSubObject( theTag, aReferenceSO.inout() ))
424 aReferenceSO = aStudyBuilder->NewObjectToTag( theSObject, theTag );
425 // add reference to the use case tree
426 // (to support tree representation customization and drag-n-drop)
427 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
428 useCaseBuilder->AppendTo( aReferenceSO->GetFather(), aReferenceSO );
430 aStudyBuilder->Addreference( aReferenceSO, aToObjSO );
434 //=============================================================================
436 * SMESH_Gen_i::PublishInStudy
438 * Publish object in the study
440 //=============================================================================
442 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
443 SALOMEDS::SObject_ptr theSObject,
444 CORBA::Object_ptr theIOR,
446 throw (SALOME::SALOME_Exception)
448 Unexpect aCatch(SALOME_SalomeException);
449 SALOMEDS::SObject_wrap aSO;
450 if ( CORBA::is_nil( theStudy ) || CORBA::is_nil( theIOR ))
452 if(MYDEBUG) MESSAGE("PublishInStudy");
455 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
456 if( !aMesh->_is_nil() )
457 aSO = PublishMesh( theStudy, aMesh, theName );
459 // Publishing a sub-mesh
460 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
461 if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
462 GEOM::GEOM_Object_var aShapeObject = aSubMesh->GetSubShape();
463 aMesh = aSubMesh->GetFather();
464 aSO = PublishSubMesh( theStudy, aMesh, aSubMesh, aShapeObject, theName );
467 // Publishing a hypothesis or algorithm
468 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
469 if ( aSO->_is_nil() && !aHyp->_is_nil() )
470 aSO = PublishHypothesis( theStudy, aHyp );
472 // Publishing a group
473 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(theIOR);
474 if ( aSO->_is_nil() && !aGroup->_is_nil() ) {
475 GEOM::GEOM_Object_var aShapeObject;
476 aMesh = aGroup->GetMesh();
477 aSO = PublishGroup( theStudy, aMesh, aGroup, aShapeObject, theName );
479 if(MYDEBUG) MESSAGE("PublishInStudy_END");
484 //=======================================================================
485 //function : PublishComponent
487 //=======================================================================
489 SALOMEDS::SComponent_ptr SMESH_Gen_i::PublishComponent(SALOMEDS::Study_ptr theStudy)
491 if ( CORBA::is_nil( theStudy ))
492 return SALOMEDS::SComponent::_nil();
493 if(MYDEBUG) MESSAGE("PublishComponent");
495 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
496 SALOMEDS::UseCaseBuilder_wrap useCaseBuilder = theStudy->GetUseCaseBuilder();
498 CORBA::String_var compDataType = ComponentDataType();
499 SALOMEDS::SComponent_wrap father = theStudy->FindComponent( compDataType.in() );
500 if ( !CORBA::is_nil( father ) ) {
501 // check that the component is added to the use case browser
502 if ( !useCaseBuilder->IsUseCaseNode( father ) ) {
503 useCaseBuilder->SetRootCurrent();
504 useCaseBuilder->Append( father ); // component object is added as the top level item
506 return father._retn();
509 SALOME_ModuleCatalog::ModuleCatalog_var aCat =
510 SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
511 if ( CORBA::is_nil( aCat ) )
512 return father._retn();
514 SALOME_ModuleCatalog::Acomponent_var aComp = aCat->GetComponent( compDataType.in() );
515 if ( CORBA::is_nil( aComp ) )
516 return father._retn();
518 SALOMEDS::GenericAttribute_wrap anAttr;
519 SALOMEDS::AttributePixMap_wrap aPixmap;
521 father = aStudyBuilder->NewComponent( compDataType.in() );
522 aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
523 anAttr = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
525 aPixmap->SetPixMap( "ICON_OBJBROWSER_SMESH" );
526 CORBA::String_var userName = aComp->componentusername();
527 SetName( father, userName.in(), "MESH" );
528 // add component to the use case tree
529 // (to support tree representation customization and drag-n-drop)
530 useCaseBuilder->SetRootCurrent();
531 useCaseBuilder->Append( father ); // component object is added as the top level item
532 if(MYDEBUG) MESSAGE("PublishComponent--END");
534 return father._retn();
537 //=============================================================================
539 * findMaxChildTag [ static internal ]
541 * Finds maximum child tag for the given object
543 //=============================================================================
545 static long findMaxChildTag( SALOMEDS::SObject_ptr theSObject )
548 if ( !theSObject->_is_nil() ) {
549 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
550 if ( !aStudy->_is_nil() ) {
551 SALOMEDS::ChildIterator_wrap anIter = aStudy->NewChildIterator( theSObject );
552 for ( ; anIter->More(); anIter->Next() ) {
553 SALOMEDS::SObject_wrap anSO = anIter->Value();
554 long nTag = anSO->Tag();
563 //=======================================================================
564 //function : PublishMesh
566 //=======================================================================
568 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishMesh (SALOMEDS::Study_ptr theStudy,
569 SMESH::SMESH_Mesh_ptr theMesh,
572 if ( CORBA::is_nil( theStudy ) ||
573 CORBA::is_nil( theMesh ))
574 return SALOMEDS::SComponent::_nil();
575 if(MYDEBUG) MESSAGE("PublishMesh--IN");
577 // find or publish a mesh
579 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
580 if ( aMeshSO->_is_nil() )
582 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
583 if ( father->_is_nil() )
584 return aMeshSO._retn();
586 // Find correct free tag
587 long aTag = findMaxChildTag( father.in() );
588 if ( aTag <= GetAlgorithmsRootTag() )
589 aTag = GetAlgorithmsRootTag() + 1;
593 aMeshSO = publish (theStudy, theMesh, father, aTag, "ICON_SMESH_TREE_MESH_WARN" );
594 if ( aMeshSO->_is_nil() )
595 return aMeshSO._retn();
597 SetName( aMeshSO, theName, "Mesh" );
599 // Add shape reference
601 GEOM::GEOM_Object_var aShapeObject = theMesh->GetShapeToMesh();
602 if ( !CORBA::is_nil( aShapeObject )) {
603 addReference( theStudy, aMeshSO, aShapeObject, GetRefOnShapeTag() );
605 // Publish global hypotheses
607 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( aShapeObject );
608 for ( int i = 0; i < hypList->length(); i++ )
610 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
611 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
612 AddHypothesisToShape( theStudy, theMesh, aShapeObject, aHyp );
618 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
620 return aMeshSO._retn();
621 map<int, SMESH_subMesh_i*>& subMap = mesh_i->_mapSubMesh_i;
622 map<int, SMESH_subMesh_i*>::iterator subIt = subMap.begin();
623 for ( ; subIt != subMap.end(); subIt++ ) {
624 SMESH::SMESH_subMesh_ptr aSubMesh = (*subIt).second->_this();
625 if ( !CORBA::is_nil( aSubMesh )) {
626 aShapeObject = aSubMesh->GetSubShape();
627 SALOMEDS::SObject_wrap( PublishSubMesh( theStudy, theMesh, aSubMesh, aShapeObject ));
632 const map<int, SMESH::SMESH_GroupBase_ptr>& grMap = mesh_i->getGroups();
633 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator it = grMap.begin();
634 for ( ; it != grMap.end(); it++ )
636 SMESH::SMESH_GroupBase_ptr aGroup = (*it).second;
637 if ( !aGroup->_is_nil() ) {
638 GEOM::GEOM_Object_var aShapeObj;
639 SMESH::SMESH_GroupOnGeom_var aGeomGroup = SMESH::SMESH_GroupOnGeom::_narrow( aGroup );
640 if ( !aGeomGroup->_is_nil() )
641 aShapeObj = aGeomGroup->GetShape();
642 SALOMEDS::SObject_wrap( PublishGroup( theStudy, theMesh, aGroup, aShapeObj ));
646 if(MYDEBUG) MESSAGE("PublishMesh_END");
647 return aMeshSO._retn();
650 //=======================================================================
651 //function : PublishSubMesh
653 //=======================================================================
655 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishSubMesh (SALOMEDS::Study_ptr theStudy,
656 SMESH::SMESH_Mesh_ptr theMesh,
657 SMESH::SMESH_subMesh_ptr theSubMesh,
658 GEOM::GEOM_Object_ptr theShapeObject,
661 if (theStudy->_is_nil() || theMesh->_is_nil() ||
662 theSubMesh->_is_nil() || theShapeObject->_is_nil() )
663 return SALOMEDS::SObject::_nil();
665 SALOMEDS::SObject_wrap aSubMeshSO = ObjectToSObject( theStudy, theSubMesh );
666 if ( aSubMeshSO->_is_nil() )
668 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
669 if ( aMeshSO->_is_nil() ) {
670 aMeshSO = PublishMesh( theStudy, theMesh );
671 if ( aMeshSO->_is_nil())
672 return SALOMEDS::SObject::_nil();
674 // Find submesh sub-tree tag
676 const char* aRootName = "";
677 switch ( theShapeObject->GetShapeType() ) {
679 aRootTag = GetSubMeshOnVertexTag();
680 aRootName = "SubMeshes on Vertex";
683 aRootTag = GetSubMeshOnEdgeTag();
684 aRootName = "SubMeshes on Edge";
687 aRootTag = GetSubMeshOnWireTag();
688 aRootName = "SubMeshes on Wire";
691 aRootTag = GetSubMeshOnFaceTag();
692 aRootName = "SubMeshes on Face";
695 aRootTag = GetSubMeshOnShellTag();
696 aRootName = "SubMeshes on Shell";
699 aRootTag = GetSubMeshOnSolidTag();
700 aRootName = "SubMeshes on Solid";
703 aRootTag = GetSubMeshOnCompoundTag();
704 aRootName = "SubMeshes on Compound";
708 // Find or create submesh root
709 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
710 aMeshSO, aRootTag, 0, false );
711 if ( aRootSO->_is_nil() )
712 return aSubMeshSO._retn();
714 SetName( aRootSO, aRootName );
716 // Add new submesh to corresponding sub-tree
717 SMESH::array_of_ElementType_var elemTypes = theSubMesh->GetTypes();
718 const int isEmpty = ( elemTypes->length() == 0 );
719 const char* pm[2] = { "ICON_SMESH_TREE_MESH", "ICON_SMESH_TREE_MESH_WARN" };
720 aSubMeshSO = publish (theStudy, theSubMesh, aRootSO, 0, pm[isEmpty] );
721 if ( aSubMeshSO->_is_nil() )
722 return aSubMeshSO._retn();
724 SetName( aSubMeshSO, theName, "SubMesh" );
726 // Add reference to theShapeObject
728 addReference( theStudy, aSubMeshSO, theShapeObject, 1 );
730 // Publish hypothesis
732 SMESH::ListOfHypothesis_var hypList = theMesh->GetHypothesisList( theShapeObject );
733 for ( int i = 0; i < hypList->length(); i++ ) {
734 SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( hypList[ i ]);
735 SALOMEDS::SObject_wrap so = PublishHypothesis( theStudy, aHyp );
736 AddHypothesisToShape( theStudy, theMesh, theShapeObject, aHyp );
739 return aSubMeshSO._retn();
742 //=======================================================================
743 //function : PublishGroup
745 //=======================================================================
747 SALOMEDS::SObject_ptr SMESH_Gen_i::PublishGroup (SALOMEDS::Study_ptr theStudy,
748 SMESH::SMESH_Mesh_ptr theMesh,
749 SMESH::SMESH_GroupBase_ptr theGroup,
750 GEOM::GEOM_Object_ptr theShapeObject,
753 if (theStudy->_is_nil() || theMesh->_is_nil() || theGroup->_is_nil() )
754 return SALOMEDS::SObject::_nil();
756 SALOMEDS::SObject_wrap aGroupSO = ObjectToSObject( theStudy, theGroup );
757 if ( aGroupSO->_is_nil() )
759 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
760 if ( aMeshSO->_is_nil() ) {
761 aMeshSO = PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), theMesh, "");
762 if ( aMeshSO->_is_nil())
763 return SALOMEDS::SObject::_nil();
765 int aType = (int)theGroup->GetType();
766 const char* aRootNames[] = {
767 "Compound Groups", "Groups of Nodes", "Groups of Edges",
768 "Groups of Faces", "Groups of Volumes", "Groups of 0D Elements",
771 // Currently, groups with heterogenous content are not supported
772 if ( aType != SMESH::ALL )
774 long aRootTag = GetNodeGroupsTag() + aType - 1;
776 // Find or create groups root
777 SALOMEDS::SObject_wrap aRootSO = publish (theStudy, CORBA::Object::_nil(),
778 aMeshSO, aRootTag, 0, false );
779 if ( aRootSO->_is_nil() ) return SALOMEDS::SObject::_nil();
781 if ( aType < sizeof(aRootNames)/sizeof(char*) )
782 SetName( aRootSO, aRootNames[aType] );
784 // Add new group to corresponding sub-tree
785 SMESH::array_of_ElementType_var elemTypes = theGroup->GetTypes();
786 int isEmpty = ( elemTypes->length() == 0 );
787 std::string pm[2] = { "ICON_SMESH_TREE_GROUP", "ICON_SMESH_TREE_MESH_WARN" };
788 if ( SMESH::DownCast< SMESH_GroupOnFilter_i* > ( theGroup ))
790 pm[0] = "ICON_SMESH_TREE_GROUP_ON_FILTER";
792 else if ( SMESH::DownCast< SMESH_Group_i* > ( theGroup ))
794 SMESH::array_of_ElementType_var allElemTypes = theMesh->GetTypes();
795 for ( size_t i =0; i < allElemTypes->length() && isEmpty; ++i )
796 isEmpty = ( allElemTypes[i] != theGroup->GetType() );
798 aGroupSO = publish (theStudy, theGroup, aRootSO, 0, pm[isEmpty].c_str() );
800 if ( aGroupSO->_is_nil() )
801 return aGroupSO._retn();
804 SetName( aGroupSO, theName, "Group" );
806 //Add reference to geometry
807 if ( !theShapeObject->_is_nil() )
808 addReference( theStudy, aGroupSO, theShapeObject, 1 );
810 return aGroupSO._retn();
813 //=======================================================================
814 //function : PublishHypothesis
816 //=======================================================================
818 SALOMEDS::SObject_ptr
819 SMESH_Gen_i::PublishHypothesis (SALOMEDS::Study_ptr theStudy,
820 SMESH::SMESH_Hypothesis_ptr theHyp,
823 if(MYDEBUG) MESSAGE("PublishHypothesis")
824 if (theStudy->_is_nil() || theHyp->_is_nil())
825 return SALOMEDS::SObject::_nil();
827 CORBA::String_var hypType = theHyp->GetName();
829 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
830 if ( aHypSO->_is_nil() )
832 SALOMEDS::SComponent_wrap father = PublishComponent( theStudy );
833 if ( father->_is_nil() )
834 return aHypSO._retn();
836 //Find or Create Hypothesis root
837 bool isAlgo = ( !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil() );
838 int aRootTag = isAlgo ? GetAlgorithmsRootTag() : GetHypothesisRootTag();
839 SALOMEDS::SObject_wrap aRootSO =
840 publish (theStudy, CORBA::Object::_nil(),father, aRootTag,
841 isAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
842 SetName( aRootSO, isAlgo ? "Algorithms" : "Hypotheses" );
844 // Add New Hypothesis
845 string aPmName = isAlgo ? "ICON_SMESH_TREE_ALGO_" : "ICON_SMESH_TREE_HYPO_";
846 aPmName += hypType.in();
847 // prepend plugin name to pixmap name
848 string pluginName = myHypCreatorMap[ hypType.in() ]->GetModuleName();
849 if ( pluginName != "StdMeshers" )
850 aPmName = pluginName + "::" + aPmName;
851 aHypSO = publish( theStudy, theHyp, aRootSO, 0, aPmName.c_str() );
854 SetName( aHypSO, theName, hypType.in() );
856 if(MYDEBUG) MESSAGE("PublishHypothesis--END")
857 return aHypSO._retn();
860 //=======================================================================
861 //function : GetMeshOrSubmeshByShape
863 //=======================================================================
865 SALOMEDS::SObject_ptr
866 SMESH_Gen_i::GetMeshOrSubmeshByShape (SALOMEDS::Study_ptr theStudy,
867 SMESH::SMESH_Mesh_ptr theMesh,
868 GEOM::GEOM_Object_ptr theShape)
870 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape")
871 SALOMEDS::SObject_wrap aMeshOrSubMesh;
872 if (theMesh->_is_nil() || ( theShape->_is_nil() && theMesh->HasShapeToMesh()))
873 return aMeshOrSubMesh._retn();
876 if(theMesh->HasShapeToMesh())
877 aShape = GeomObjectToShape( theShape );
879 aShape = SMESH_Mesh::PseudoShape();
881 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
883 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
884 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
885 if ( aShape.IsSame( meshDS->ShapeToMesh() ))
886 aMeshOrSubMesh = ObjectToSObject( theStudy, theMesh );
888 int shapeID = meshDS->ShapeToIndex( aShape );
889 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
890 if ( !aSubMesh->_is_nil() )
891 aMeshOrSubMesh = ObjectToSObject( theStudy, aSubMesh );
894 if(MYDEBUG) MESSAGE("GetMeshOrSubmeshByShape--END")
895 return aMeshOrSubMesh._retn();
898 //=======================================================================
899 //function : AddHypothesisToShape
901 //=======================================================================
903 bool SMESH_Gen_i::AddHypothesisToShape(SALOMEDS::Study_ptr theStudy,
904 SMESH::SMESH_Mesh_ptr theMesh,
905 GEOM::GEOM_Object_ptr theShape,
906 SMESH::SMESH_Hypothesis_ptr theHyp)
908 if(MYDEBUG) MESSAGE("AddHypothesisToShape")
909 if (theStudy->_is_nil() || theMesh->_is_nil() ||
910 theHyp->_is_nil() || (theShape->_is_nil()
911 && theMesh->HasShapeToMesh()) )
914 SALOMEDS::SObject_wrap aMeshSO = ObjectToSObject( theStudy, theMesh );
915 if ( aMeshSO->_is_nil() )
916 aMeshSO = PublishMesh( theStudy, theMesh );
917 SALOMEDS::SObject_wrap aHypSO = PublishHypothesis( theStudy, theHyp );
918 if ( aMeshSO->_is_nil() || aHypSO->_is_nil())
921 // Find a mesh or submesh refering to theShape
922 SALOMEDS::SObject_wrap aMeshOrSubMesh =
923 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
924 if ( aMeshOrSubMesh->_is_nil() )
927 TopoDS_Shape aShape = GeomObjectToShape( theShape );
928 SMESH_Mesh_i* mesh_i = objectToServant<SMESH_Mesh_i>( theMesh );
929 if ( !aShape.IsNull() && mesh_i && mesh_i->GetImpl().GetMeshDS() ) {
930 SMESHDS_Mesh* meshDS = mesh_i->GetImpl().GetMeshDS();
931 int shapeID = meshDS->ShapeToIndex( aShape );
932 SMESH::SMESH_subMesh_var aSubMesh = mesh_i->getSubMesh(shapeID);
933 aMeshOrSubMesh = PublishSubMesh( theStudy, theMesh, aSubMesh, theShape );
935 if ( aMeshOrSubMesh->_is_nil() )
939 //Find or Create Applied Hypothesis root
940 bool aIsAlgo = !SMESH::SMESH_Algo::_narrow( theHyp )->_is_nil();
941 SALOMEDS::SObject_wrap AHR =
942 publish (theStudy, CORBA::Object::_nil(), aMeshOrSubMesh,
943 aIsAlgo ? GetRefOnAppliedAlgorithmsTag() : GetRefOnAppliedHypothesisTag(),
944 aIsAlgo ? "ICON_SMESH_TREE_ALGO" : "ICON_SMESH_TREE_HYPO", false);
945 SetName( AHR, aIsAlgo ? "Applied algorithms" : "Applied hypotheses" );
947 addReference( theStudy, AHR, theHyp );
949 if(MYDEBUG) MESSAGE("AddHypothesisToShape--END")
953 //=======================================================================
954 //function : RemoveHypothesisFromShape
956 //=======================================================================
958 bool SMESH_Gen_i::RemoveHypothesisFromShape(SALOMEDS::Study_ptr theStudy,
959 SMESH::SMESH_Mesh_ptr theMesh,
960 GEOM::GEOM_Object_ptr theShape,
961 SMESH::SMESH_Hypothesis_ptr theHyp)
963 if (theStudy->_is_nil() || theMesh->_is_nil() ||
964 theHyp->_is_nil() || (theShape->_is_nil()
965 && theMesh->HasShapeToMesh()))
968 SALOMEDS::SObject_wrap aHypSO = ObjectToSObject( theStudy, theHyp );
969 if ( aHypSO->_is_nil() )
972 CORBA::String_var hypEntry = aHypSO->GetID();
974 // Find a mesh or submesh refering to theShape
975 SALOMEDS::SObject_wrap aMeshOrSubMesh =
976 GetMeshOrSubmeshByShape( theStudy, theMesh, theShape );
977 if ( aMeshOrSubMesh->_is_nil() )
980 // Find and remove a reference to aHypSO
981 SALOMEDS::SObject_wrap aRef, anObj;
982 SALOMEDS::ChildIterator_wrap it = theStudy->NewChildIterator( aMeshOrSubMesh );
984 for ( it->InitEx( true ); ( it->More() && !found ); it->Next() ) {
986 if (anObj->ReferencedObject( aRef.inout() ))
988 CORBA::String_var refEntry = aRef->GetID();
989 found = ( strcmp( refEntry, hypEntry ) == 0 );
993 SALOMEDS::StudyBuilder_var builder = theStudy->NewBuilder();
994 builder->RemoveObject( anObj );
1001 //================================================================================
1003 * \brief Stores names of variables that WILL be passes as parameters when calling
1004 * some method of a given object.
1005 * \param [in] theObject - the object whose a method WILL be called with \a theParameters.
1006 * \param [in] theParameters - a string contating parameters separated by ':'.
1008 //================================================================================
1010 void SMESH_Gen_i::UpdateParameters(CORBA::Object_ptr theObject, const char* theParameters)
1012 SALOMEDS::Study_var aStudy = GetCurrentStudy();
1013 if ( aStudy->_is_nil() )
1016 // find variable names within theParameters
1019 myLastParameters.clear();
1020 myLastParamIndex.clear(); /* vector holding indices of virables within the string
1021 of all varibles used for theObject */
1023 int pos = 0, prevPos = 0, len = strlen( theParameters );
1024 if ( len == 0 ) return;
1025 while ( pos <= len )
1027 if ( pos == len || theParameters[pos] == ':' )
1029 if ( prevPos < pos )
1031 string val( theParameters + prevPos, theParameters + pos );
1032 if ( !aStudy->IsVariable( val.c_str() ))
1034 myLastParameters.push_back( val );
1035 nbVars += (! myLastParameters.back().empty() );
1039 myLastParameters.push_back("");
1050 // (1) variable names in the string of all varibles used for theObject and
1051 // (2) indices of found variables in myLastParamIndex.
1053 // remember theObject
1054 SALOMEDS::SObject_wrap aSObj = ObjectToSObject(aStudy,theObject);
1055 if ( aSObj->_is_nil() )
1057 CORBA::String_var anObjEntry = aSObj->GetID();
1058 myLastObj = anObjEntry.in();
1060 // get a string of variable names
1061 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1062 SALOMEDS::GenericAttribute_wrap anAttr =
1063 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1064 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1065 CORBA::String_var oldVars = aStringAttr->Value();
1066 std::string varStr = oldVars.in();
1068 // add new variables and find indices of variables
1069 for ( size_t i = 0; i < myLastParameters.size(); ++i )
1072 if ( !myLastParameters[i].empty() )
1074 // find index of myLastParameters[i] in varStr
1076 bool varFound = false;
1078 // varStr can be "A|B::C;*=2|D"
1079 const std::string separators(":|;*=");
1080 while ( pos < varStr.size() && !varFound )
1083 while ( separators.find( varStr[ pos ]) != std::string::npos )
1084 if ( ++pos >= varStr.size() )
1086 // skip repetition number following '='
1087 if ( varStr[ pos-1 ] == '=' )
1089 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1091 continue; // to skip next separator
1093 // compare variable name
1094 if ( pos < varStr.size() )
1096 varFound = ( varStr.compare( pos, myLastParameters[i].size(), myLastParameters[i] ) == 0 &&
1097 // same string begining but is length same?
1098 ( pos + myLastParameters[i].size() >= varStr.size() ||
1099 separators.find( varStr[ pos+1 ]) != std::string::npos ));
1101 varIndex = curIndex;
1103 pos = varStr.find_first_of( separators, pos ); // goto the next separator
1110 varStr += ":" + myLastParameters[i];
1111 varIndex = curIndex;
1114 myLastParamIndex.push_back( varIndex );
1116 aStringAttr->SetValue( varStr.c_str() );
1119 //================================================================================
1121 * \brief Return all variables used to create an object
1122 * \param [in] theObjectEntry - an object entry in the current study
1123 * \return std::vector< std::string > - all variable names (or values of removed variables)
1125 //================================================================================
1127 std::vector< std::string > SMESH_Gen_i::GetAllParameters(const std::string& theObjectEntry) const
1129 std::vector< std::string > varNames;
1130 if ( myCurrentStudy->_is_nil() )
1133 SALOMEDS::SObject_wrap aSObj = myCurrentStudy->FindObjectID( theObjectEntry.c_str() );
1134 if ( myCurrentStudy->_is_nil() )
1137 // get a string of variable names
1138 SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
1139 SALOMEDS::GenericAttribute_wrap anAttr =
1140 aStudyBuilder->FindOrCreateAttribute( aSObj, "AttributeString" );
1141 SALOMEDS::AttributeString_wrap aStringAttr = anAttr;
1142 CORBA::String_var oldVars = aStringAttr->Value();
1143 std::string varStr = oldVars.in();
1145 // separate variables within varStr;
1146 // varStr can be "A|B::C;*=2|D"
1148 const std::string separators(":|;*=");
1149 while ( pos < varStr.size() )
1152 pos = varStr.find_first_not_of( separators, pos );
1153 // while ( separators.find( varStr[ pos ]) != std::string::npos )
1154 // if ( ++pos >= varStr.size() )
1156 // skip repetition number following '='
1157 if ( varStr[ pos-1 ] == '=' )
1159 while ( '0' <= varStr[ pos ] && varStr[ pos ] <= '9' )
1161 continue; // to skip next separator
1163 // store variable name
1164 if ( pos < varStr.size() )
1166 size_t pos2 = varStr.find_first_of( separators, pos );
1167 varNames.push_back( varStr.substr( pos, pos2 - pos));
1174 //=======================================================================
1175 //function : ParseParameters
1176 //purpose : Replace variables by their values
1177 //=======================================================================
1178 // char* SMESH_Gen_i::ParseParameters(const char* theParameters)
1180 // //const char* aParameters = theParameters;
1181 // // const char* aParameters = CORBA::string_dup(theParameters);
1182 // TCollection_AsciiString anInputParams;
1183 // SALOMEDS::Study_var aStudy = GetCurrentStudy();
1184 // if( !aStudy->_is_nil() ) {
1185 // // SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(theParameters);
1186 // // for(int j=0;j<aSections->length();j++) {
1187 // // SALOMEDS::ListOfStrings aVars= aSections[j];
1188 // // for(int i=0;i<aVars.length();i++ ) {
1189 // // anInputParams += aStudy->IsVariable(aVars[i].in()) ?
1190 // // TCollection_AsciiString(aVars[i].in()) : TCollection_AsciiString("");
1191 // // if(i != aVars.length()-1)
1192 // // anInputParams+=":";
1194 // // if(j!=aSections->length()-1)
1195 // // anInputParams+="|";
1197 // TCollection_AsciiString paramStr( theParameters );
1198 // int beg = 0, end;
1199 // char sep, *pParams = (char*)paramStr.ToCString();
1200 // while ( beg < paramStr.Length() )
1203 // while ( ++end < paramStr.Length() )
1204 // if ( pParams[end] == ':' || pParams[end] == '|')
1206 // if ( end < paramStr.Length())
1208 // sep = pParams[end];
1209 // pParams[end] = '\0';
1211 // if ( aStudy->IsVariable( pParams+beg ))
1212 // anInputParams += pParams+beg;
1213 // if ( end < paramStr.Length() )
1214 // anInputParams += sep;
1220 // return CORBA::string_dup(anInputParams.ToCString());
1223 //=======================================================================
1224 //function : GetParameters
1226 //=======================================================================
1228 char* SMESH_Gen_i::GetParameters(CORBA::Object_ptr theObject)
1230 CORBA::String_var aResult("");
1232 SALOMEDS::SObject_wrap aSObj = ObjectToSObject( myCurrentStudy, theObject );
1233 if ( !aSObj->_is_nil() )
1235 SALOMEDS::GenericAttribute_wrap attr;
1236 if ( aSObj->FindAttribute( attr.inout(), "AttributeString"))
1238 SALOMEDS::AttributeString_wrap strAttr = attr;
1239 aResult = strAttr->Value();
1242 return aResult._retn();