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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : GEOMBase.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
29 #include <GeometryGUI.h>
30 #include <GEOM_Client.hxx>
33 #include <GEOM_Actor.h>
34 #include <SVTK_ViewModel.h>
35 #include <SVTK_ViewWindow.h>
36 #include <OCCViewer_ViewPort3d.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <SOCC_ViewModel.h>
42 #include <SALOME_ListIO.hxx>
43 #include <SALOME_ListIteratorOfListIO.hxx>
45 #include <SUIT_Desktop.h>
46 #include <SUIT_Session.h>
47 #include <SUIT_ViewManager.h>
48 #include <SUIT_ViewWindow.h>
49 #include <SUIT_MessageBox.h>
50 #include <SalomeApp_Application.h>
51 #include <SalomeApp_Study.h>
53 // // Open CASCADE Includes
54 #include <BRep_Tool.hxx>
55 #include <BRepAdaptor_Curve.hxx>
56 #include <BRepAdaptor_Surface.hxx>
57 #include <BRepPrimAPI_MakeCone.hxx>
59 #include <AIS_ListIteratorOfListOfInteractive.hxx>
60 #include <AIS_ListOfInteractive.hxx>
64 #include <TopTools_IndexedMapOfShape.hxx>
67 #include <Precision.hxx>
69 #include <vtkRenderer.h>
73 //=====================================================================================
74 // function : GetShapeFromIOR()
75 // purpose : Get shape data by the specified IOR
76 //=====================================================================================
77 TopoDS_Shape GEOMBase::GetShapeFromIOR( const QString& IOR )
79 GEOM::GEOM_Object_var geomObj = GEOMBase::GetObjectFromIOR( IOR );
81 GetShape( geomObj, shape, TopAbs_SHAPE );
86 //=====================================================================================
87 // function : GetIndex()
88 // purpose : Get the index of a sub-shape in a main shape : index start at 1
89 //=====================================================================================
90 int GEOMBase::GetIndex( const TopoDS_Shape& subshape, const TopoDS_Shape& shape )
93 if ( !shape.IsNull() && !subshape.IsNull() ) {
94 TopTools_IndexedMapOfShape anIndices;
95 TopExp::MapShapes( shape, anIndices );
96 if ( anIndices.Contains( subshape ) )
97 idx = anIndices.FindIndex( subshape );
103 //=======================================================================
104 // function : GetTopoFromSelection()
105 // purpose : Define tds from a single selection and retuen true
106 //=======================================================================
107 TopoDS_Shape GEOMBase::GetTopoFromSelection( const SALOME_ListIO& IObjects )
110 if ( IObjects.Extent() == 1 ){
111 Handle(SALOME_InteractiveObject) IO = IObjects.First();
112 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
113 if ( IO->hasEntry() && study ) {
114 _PTR(Study) studyDS = study->studyDS();
115 _PTR(SObject) obj( studyDS->FindObjectID( IO->getEntry() ) );
116 _PTR(GenericAttribute) anAttr;
117 if ( obj && obj->FindAttribute( anAttr, "AttributeIOR" ) ) {
118 _PTR(AttributeIOR) anIOR( anAttr );
119 shape = GetShapeFromIOR( anIOR->Value().c_str() );
126 //=======================================================================
127 // function : GetNameOfSelectedIObjects()
128 // purpose : Define the name geom++ or other name of mono or multi sel.
129 //=======================================================================
130 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& IObjects,
132 const bool shapesOnly )
135 name = ""; // clear output name
138 nbSel = IObjects.Extent();
140 Handle(SALOME_InteractiveObject) anIObj = IObjects.First();
141 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
142 if ( anIObj->hasEntry() && study ) {
143 _PTR(Study) studyDS = study->studyDS();
144 _PTR(SObject) obj( studyDS->FindObjectID( anIObj->getEntry() ) );
145 _PTR(GenericAttribute) anAttr;
146 if ( obj && obj->FindAttribute( anAttr, "AttributeName" ) ) {
147 _PTR(AttributeName) aNameAttr ( anAttr );
148 name = aNameAttr->Value().c_str();
154 GEOM::ListOfGO anObjs;
155 ConvertListOfIOInListOfGO( IObjects, anObjs, shapesOnly );
156 nbSel = anObjs.length();
158 name = GetName( anObjs[ 0 ] );
162 name = QObject::tr( "%1_objects" ).arg( nbSel );
168 //=================================================================================
169 // function : GetShapeTypeString()
170 // purpose : for a single shape
171 //=================================================================================
172 QString GEOMBase::GetShapeTypeString(const TopoDS_Shape& shape)
175 if ( !shape.IsNull() ) {
176 switch ( shape.ShapeType() ) {
177 case TopAbs_COMPOUND:
179 aTypeString = QObject::tr( "GEOM_COMPOUND" );
182 case TopAbs_COMPSOLID:
184 aTypeString = QObject::tr( "GEOM_COMPOUNDSOLID" );
189 aTypeString = QObject::tr( "GEOM_SOLID" );
194 aTypeString = QObject::tr( "GEOM_SHELL" );
199 BRepAdaptor_Surface surf( TopoDS::Face( shape ) );
200 switch ( surf.GetType() ) {
203 aTypeString = QObject::tr( "GEOM_PLANE" );
206 case GeomAbs_Cylinder:
208 aTypeString = QObject::tr( "GEOM_SURFCYLINDER" );
213 aTypeString = QObject::tr( "GEOM_SURFSPHERE" );
218 aTypeString = QObject::tr( "GEOM_SURFTORUS" );
223 aTypeString = QObject::tr( "GEOM_SURFCONE" );
228 aTypeString = QObject::tr( "GEOM_FACE" );
236 aTypeString = QObject::tr( "GEOM_WIRE" );
241 BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
242 switch ( curv.GetType() ) {
245 aTypeString = ( qAbs( curv.FirstParameter() ) >= 1E6 || qAbs( curv.LastParameter() ) >= 1E6 ) ?
246 QObject::tr( "GEOM_LINE" ) : QObject::tr( "GEOM_EDGE" );
251 aTypeString = curv.IsClosed() ? QObject::tr( "GEOM_CIRCLE" ) : QObject::tr( "GEOM_ARC" );
256 aTypeString = QObject::tr( "GEOM_EDGE" );
264 aTypeString = QObject::tr( "GEOM_VERTEX" );
269 aTypeString = QObject::tr( "GEOM_SHAPE" );
282 //=======================================================================
283 // function : ConvertIORinGEOMAISShape()
285 //=======================================================================
286 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const QString& IOR, bool onlyInActiveView)
288 Handle(GEOM_AISShape) shape;
290 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
292 _PTR(Study) studyDS = study->studyDS();
293 _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
295 QList<SUIT_ViewWindow*> views;
296 if ( onlyInActiveView )
297 views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
299 views = SUIT_Session::session()->activeApplication()->desktop()->windows();
300 foreach ( SUIT_ViewWindow* view, views ) {
301 if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
302 Handle(AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)view->getViewManager()->getViewModel())->getAISContext();
304 AIS_ListOfInteractive displayed;
305 ic->DisplayedObjects( displayed );
306 #if OCC_VERSION_LARGE <= 0x06060000
307 ic->ObjectsInCollector( displayed );
309 AIS_ListIteratorOfListOfInteractive it( displayed );
310 while ( it.More() && shape.IsNull() ) {
311 if ( it.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
312 Handle(GEOM_AISShape) sh = Handle(GEOM_AISShape)::DownCast( it.Value() );
313 if ( !sh.IsNull() && sh->hasIO() ) {
314 Handle(SALOME_InteractiveObject) IO = Handle(SALOME_InteractiveObject)::DownCast( sh->getIO() );
315 if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
322 if ( !shape.IsNull() ) break;
330 //=======================================================================
331 // function : ConvertIORinGEOMActor()
333 //=======================================================================
334 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const QString& IOR, bool onlyInActiveView)
336 GEOM_Actor* actor = 0;
338 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
340 _PTR(Study) studyDS = study->studyDS();
341 _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
343 QList<SUIT_ViewWindow*> views;
344 if ( onlyInActiveView )
345 views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
347 views = SUIT_Session::session()->activeApplication()->desktop()->windows();
348 foreach ( SUIT_ViewWindow* view, views ) {
349 if ( view && view->getViewManager()->getType() == SVTK_Viewer::Type() ) {
350 SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( view );
351 if( !aVTKViewWindow )
353 vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
354 vtkActorCollection* theActors = Renderer->GetActors();
355 theActors->InitTraversal();
356 vtkActor* a = theActors->GetNextActor();
357 while( a && !actor ) {
358 if ( a->IsA( "GEOM_Actor" ) ) {
359 GEOM_Actor* ga = GEOM_Actor::SafeDownCast( a );
360 if ( ga && ga->hasIO() ) {
361 Handle(SALOME_InteractiveObject) IO = Handle(SALOME_InteractiveObject)::DownCast( ga->getIO() );
362 if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
366 a = theActors->GetNextActor();
376 //=======================================================================
377 // function : GetAIS()
379 //=======================================================================
380 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& IO,
381 bool onlyInActiveView, bool onlyGeom )
383 Handle(AIS_InteractiveObject) aisObject;
385 if ( !IO.IsNull() && IO->hasEntry() ) {
386 QList<SUIT_ViewWindow*> views;
387 if ( onlyInActiveView )
388 views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
390 views = SUIT_Session::session()->activeApplication()->desktop()->windows();
392 foreach ( SUIT_ViewWindow* view, views ) {
393 if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
394 OCCViewer_Viewer* occViewer=(OCCViewer_Viewer*)view->getViewManager()->getViewModel();
395 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
397 SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( IO->getEntry() ) );
398 if ( occPrs && !occPrs->IsNull() ) {
399 AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
400 if( !shapes.Extent() ) continue;
401 aisObject=shapes.First();
406 if ( !aisObject.IsNull() ) break;
414 //=======================================================================
415 // function : ConvertIOinGEOMAISShape()
417 //=======================================================================
418 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape( const Handle(SALOME_InteractiveObject)& IO, bool onlyInActiveView )
420 return Handle(GEOM_AISShape)::DownCast( GetAIS( IO, onlyInActiveView, true ) );
424 //=======================================================================
425 // function : ConvertListOfIOInListOfIOR()
427 //=======================================================================
428 QStringList GEOMBase::ConvertListOfIOInListOfIOR( const SALOME_ListIO& IObjects )
431 SALOME_ListIteratorOfListIO it( IObjects );
432 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
434 _PTR(Study) studyDS = study->studyDS();
435 for ( ; it.More(); it.Next() ) {
436 GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
437 if ( !CORBA::is_nil( geomObj ) )
438 iors.append( GetIORFromObject( geomObj ) );
445 //=======================================================================
446 // function : ConvertIOinGEOMObject()
448 //=======================================================================
449 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject( const Handle(SALOME_InteractiveObject)& IO )
451 GEOM::GEOM_Object_var object;
453 if ( !IO.IsNull() && IO->hasEntry() ) {
454 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
456 _PTR(Study) studyDS = study->studyDS();
457 _PTR(SObject) obj( studyDS->FindObjectID( IO->getEntry() ) );
459 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( obj );
460 if ( !CORBA::is_nil( corbaObj ) )
461 object = GEOM::GEOM_Object::_narrow( corbaObj );
465 return object._retn();
469 //=======================================================================
470 // function : ConvertListOfIOInListOfGO()
472 //=======================================================================
473 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& IObjects,
474 GEOM::ListOfGO& geomObjects,
477 geomObjects.length( 0 );
479 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
481 _PTR(Study) studyDS = study->studyDS();
483 geomObjects.length( IObjects.Extent() );
484 SALOME_ListIteratorOfListIO it( IObjects );
487 for ( ; it.More(); it.Next() ) {
488 GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
489 if ( !CORBA::is_nil( geomObj ) && ( !shapesOnly || IsShape( geomObj ) ) )
490 geomObjects[ i++ ] = geomObj;
492 geomObjects.length( i );
497 //=================================================================================
498 // function : CreateArrowForLinearEdge()
499 // purpose : Create a cone topology to be used to display an arrow in the middle
500 // : of an edge showing its orientation. (For simulation and Viewer OCC only)
501 //=================================================================================
502 TopoDS_Shape GEOMBase::CreateArrowForLinearEdge( const TopoDS_Shape& shape )
504 TopoDS_Shape ArrowCone;
506 SUIT_ViewWindow* view = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
507 if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() && shape.ShapeType() == TopAbs_EDGE ) {
508 Handle(V3d_View) view3d = ((OCCViewer_ViewWindow*)view)->getViewPort()->getView();
509 Standard_Real Width, Height;
510 view3d->Size( Width, Height );
511 const Standard_Real aHeight = (Width + Height) / 50.0;
514 Standard_Real first, last;
515 Handle(Geom_Curve) curv = BRep_Tool::Curve( TopoDS::Edge( shape ), first, last );
516 if ( !curv.IsNull() && curv->IsCN(1) ) {
517 const Standard_Real param = ( first+last ) / 2.0;
518 gp_Pnt middleParamPoint;
520 curv->D1( param, middleParamPoint, V1 );
521 if ( V1.Magnitude() > Precision::Confusion() ) {
522 /* Topology orientation not geom orientation */
523 if ( shape.Orientation() == TopAbs_REVERSED )
526 gp_Ax2 anAxis( middleParamPoint, gp_Dir( V1 ) );
527 const Standard_Real radius1 = aHeight / 5.0;
528 if ( radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion() )
529 ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
533 catch ( Standard_Failure ) {
534 // OCC failures are hard to catch in GUI.
535 // This is because of the position for #include <Standard_ErrorHandler.hxx> that is very critical to find
536 // in SALOME environment : compilation error !
544 //=================================================================================
545 // function : VertexToPoint()
546 // purpose : If S can be converted in a gp_Pnt returns true and the result is P
547 //=================================================================================
548 bool GEOMBase::VertexToPoint( const TopoDS_Shape& shape, gp_Pnt& point )
550 if ( shape.IsNull() || shape.ShapeType() != TopAbs_VERTEX )
552 point = BRep_Tool::Pnt( TopoDS::Vertex( shape ) );
557 //=================================================================================
558 // function : GetBipointDxDyDz()
560 //=================================================================================
561 void GEOMBase::GetBipointDxDyDz( const gp_Pnt& point1, const gp_Pnt& point2, double& dx, double& dy, double& dz )
563 dx = point2.X() - point1.X();
564 dy = point2.Y() - point1.Y();
565 dz = point2.Z() - point1.Z();
569 //=================================================================================
570 // function : LinearEdgeExtremities()
571 // purpose : If S can be converted in a linear edge and if initial an final points
572 // : distance is sufficient, returns true else returns false.
573 // : Resulting points are respectively P1 and P2
574 //=================================================================================
575 bool GEOMBase::LinearEdgeExtremities( const TopoDS_Shape& shape, gp_Pnt& point1, gp_Pnt& point2 )
577 if ( shape.IsNull() || shape.ShapeType() != TopAbs_EDGE )
580 BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
581 if ( curv.GetType() != GeomAbs_Line )
586 curv.D0( curv.FirstParameter(), p1 );
587 curv.D0( curv.LastParameter(), p2 );
589 if ( p1.Distance( p2 ) <= Precision::Confusion() )
598 //=======================================================================
599 // function : SelectionByNameInDialogs()
600 // purpose : Called when user has entered a name of object in a LineEdit.
601 // : The selection is changed. Dialog box will receive the
602 // : corresponding signal to manage this event.
603 //=======================================================================
604 bool GEOMBase::SelectionByNameInDialogs( QWidget* widget, const QString& objectUserName, const SALOME_ListIO& /*IObjects*/ )
606 /* Find SObject with name in component GEOM */
607 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
608 if ( !study ) return false;
609 _PTR(Study) studyDS = study->studyDS();
611 std::vector<_PTR(SObject)> listSO = studyDS->FindObjectByName( objectUserName.toStdString(), "GEOM" );
613 if ( listSO.size() < 1 ) {
614 SUIT_MessageBox::critical( widget,
615 QObject::tr( "GEOM_WRN_WARNING" ),
616 QObject::tr( "GEOM_NAME_INCORRECT" ),
617 QObject::tr( "GEOM_BUT_OK" ) );
621 /* More than one object with same name */
622 if ( listSO.size() > 1 ) {
623 SUIT_MessageBox::critical( widget,
624 QObject::tr("GEOM_WRN_WARNING"),
625 QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE"),
626 QObject::tr("GEOM_BUT_OK") );
630 /* Create a SALOME_InteractiveObject with a SALOME::SObject */
631 Handle(SALOME_InteractiveObject) IO = new SALOME_InteractiveObject( listSO[0]->GetID().c_str(),
633 objectUserName.toLatin1().constData() );
635 /* Add as a selected object */
636 /* Clear any previous selection : */
637 /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
638 // Sel->ClearIObjects(); //mzn
639 // Sel->AddIObject(SI); //mzn
644 //=======================================================================
645 // function : DefineDlgPosition()
646 // purpose : Define x and y the default position for a dialog box
647 //=======================================================================
648 void GEOMBase::DefineDlgPosition( QWidget* dlg, int& x, int& y )
650 /* Here the position is on the bottom right corner - 10 */
651 SUIT_Desktop* d = SUIT_Session::session()->activeApplication()->desktop();
652 x = abs( d->x() + d->size().width() - dlg->size().width() - 10 );
653 y = abs( d->y() + d->size().height() - dlg->size().height() - 10 );
657 //=======================================================================
658 // function : GetDefaultName()
659 // purpose : Generates default names
660 //=======================================================================
661 QString GEOMBase::GetDefaultName( const QString& operation, bool extractPrefix )
665 // collect all object names of GEOM component
666 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
668 _PTR(Study) studyDS = study->studyDS();
670 std::set<std::string> names;
671 _PTR(SComponent) component( studyDS->FindComponent( "GEOM" ) );
673 _PTR(ChildIterator) it( studyDS->NewChildIterator( component ) );
674 for ( it->InitEx( true ); it->More(); it->Next() ) {
675 names.insert( it->Value()->GetName() );
679 // build a unique name
681 bool isUnique = false;
682 QString prefix = operation;
684 if ( extractPrefix ) {
685 QStringList parts = prefix.split( "_", QString::KeepEmptyParts );
686 if ( parts.count() > 1 ) {
688 aNumber = parts.last().toLong( &ok );
691 prefix = parts.join( "_" );
697 while ( !isUnique ) {
698 aName = prefix + "_" + QString::number( ++aNumber );
699 isUnique = ( names.count( aName.toStdString()) == 0 );
706 //=======================================================================
707 // function : ShowErrorMessage()
708 // purpose : Shows message box with error code and comment
709 //=======================================================================
710 void GEOMBase::ShowErrorMessage( const QString& errorCode, const QString& comment )
713 text << QObject::tr( "GEOM_PRP_ABORT" );
714 if ( !errorCode.isEmpty() )
715 text << QObject::tr( errorCode.toLatin1().constData() );
716 if ( !comment.isEmpty() )
717 text << QObject::tr( comment.toLatin1().constData() );
719 SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
720 QObject::tr( "GEOM_ERROR" ),
722 QObject::tr( "GEOM_BUT_OK" ) );
726 //=======================================================================
727 // function : GetObjectFromIOR()
728 // purpose : returns a GEOM_Object by given IOR (string)
729 //=======================================================================
730 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const QString& IOR )
732 return GeometryGUI::GetObjectFromIOR (IOR);
735 //=======================================================================
736 // function : GetIORFromObject()
737 // purpose : returns IOR of a given GEOM_Object
738 //=======================================================================
739 QString GEOMBase::GetIORFromObject( GEOM::GEOM_Object_ptr object )
741 return GeometryGUI::GetIORFromObject (object);
744 //=======================================================================
745 // function : GetShape()
746 // purpose : returns a TopoDS_Shape stored in GEOM_Object
747 //=======================================================================
748 bool GEOMBase::GetShape( GEOM::GEOM_Object_ptr object, TopoDS_Shape& shape, const TopAbs_ShapeEnum type )
750 shape = TopoDS_Shape();
751 if ( !CORBA::is_nil( object ) ) {
752 TopAbs_ShapeEnum stype = (TopAbs_ShapeEnum)( object->GetShapeType() );
753 if ( type == TopAbs_SHAPE || type == stype )
754 shape = GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), object );
756 return !shape.IsNull();
759 //=======================================================================
760 // function : GetName()
761 // purpose : Get name of object
762 //=======================================================================
763 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr object )
766 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
768 if ( !CORBA::is_nil( object ) ) {
769 // 1. search if object is already published in the study
770 CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( object );
771 if ( study && strcmp( IOR.in(), "" ) != 0 ) {
772 _PTR(SObject) aSObj( study->studyDS()->FindObjectIOR( std::string( IOR.in() ) ) );
773 _PTR(GenericAttribute) anAttr;
774 if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") ) {
775 _PTR(AttributeName) aNameAttr( anAttr );
776 name = aNameAttr->Value().c_str();
780 // 2. if object is not found in the study, try default name
781 if ( name.isEmpty() ) {
782 if ( object->IsMainShape() ) {
783 name = GetDefaultName( "geomObj" );
786 GEOM::GEOM_Object_var mainShape = object->GetMainShape();
787 if ( !CORBA::is_nil( mainShape ) ) {
788 GEOM::ListOfLong_var indices = object->GetSubShapeIndices();
789 if ( indices->length() > 0 ) {
790 TopAbs_ShapeEnum type = (TopAbs_ShapeEnum)( object->GetShapeType() );
791 name = QString( "%1:%2_%3" ).arg( GetName( mainShape.in() ) )
792 .arg( TypeName( type ) ).arg( indices[0] );
802 //=======================================================================
803 // function : IsShape()
804 // purpose : Return TRUE if object is valid and has shape
805 //=======================================================================
806 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr object )
808 return !object->_is_nil() && object->IsShape();
811 //=======================================================================
812 // function : TypeName()
813 // purpose : Get string representation for the shape type
814 //=======================================================================
815 QString GEOMBase::TypeName( TopAbs_ShapeEnum type )
817 QString name = "shape";
819 case TopAbs_COMPSOLID:
820 name = "compsolid"; break;
821 case TopAbs_COMPOUND:
822 name = "compound"; break;
824 name = "solid"; break;
826 name = "shell"; break;
828 name = "face"; break;
830 name = "wire"; break;
832 name = "edge"; break;
834 name = "vertex"; break;
841 //================================================================
842 // Function : GetEntry
843 // Purpose : Get study entry for the given object (if it is published)
844 //================================================================
845 QString GEOMBase::GetEntry( GEOM::GEOM_Object_ptr object )
848 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
849 if ( study && !CORBA::is_nil( object ) ) {
850 QString objIOR = GetIORFromObject( object );
851 if ( !objIOR.isEmpty() ) {
852 _PTR(SObject) SO( study->studyDS()->FindObjectIOR( objIOR.toLatin1().constData() ) );
854 entry = SO->GetID().c_str();
860 //================================================================
861 // Function : PublishSubObject
862 // Purpose : Publish sub-shape under the main object
863 //================================================================
864 void GEOMBase::PublishSubObject( GEOM::GEOM_Object_ptr object )
866 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
867 if ( study && !CORBA::is_nil( object ) ) {
868 _PTR(Study) studyDS = study->studyDS();
869 QString entry = GetEntry( object );
870 GEOM::GEOM_Object_var father = object->GetMainShape();
871 QString fatherEntry = GetEntry( father );
872 if ( entry.isEmpty() && !CORBA::is_nil( father ) && !fatherEntry.isEmpty() ) {
873 QString name = GetName( object );
874 GeometryGUI::GetGeomGen()->AddInStudy( GeometryGUI::ClientStudyToStudy( studyDS ),
875 object, name.toLatin1().data(), father.in() );
880 //================================================================
881 // Function : synchronize
883 //================================================================
884 void GEOMBase::Synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
886 // 1. remove items from the "left" list that are not in the "right" list
887 QMutableListIterator<GEOM::GeomObjPtr> it1( left );
888 while ( it1.hasNext() ) {
889 GEOM::GeomObjPtr o1 = it1.next();
891 QMutableListIterator<GEOM::GeomObjPtr> it2( right );
892 while ( it2.hasNext() && !found )
893 found = o1 == it2.next();
897 // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
899 while ( it1.hasNext() ) {
900 GEOM::GeomObjPtr o1 = it1.next();
902 QMutableListIterator<GEOM::GeomObjPtr> it2( left );
903 while ( it2.hasNext() && !found )
904 found = o1 == it2.next();