1 // Copyright (C) 2007-2021 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>
44 #include <SUIT_Desktop.h>
45 #include <SUIT_Session.h>
46 #include <SUIT_ViewManager.h>
47 #include <SUIT_ViewWindow.h>
48 #include <SUIT_MessageBox.h>
49 #include <SalomeApp_Application.h>
50 #include <SalomeApp_Study.h>
52 // // Open CASCADE Includes
53 #include <BRep_Tool.hxx>
54 #include <BRepAdaptor_Curve.hxx>
55 #include <BRepAdaptor_Surface.hxx>
56 #include <BRepPrimAPI_MakeCone.hxx>
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_ListOfInteractive.hxx>
63 #include <TopTools_IndexedMapOfShape.hxx>
66 #include <Precision.hxx>
68 #include <vtkRenderer.h>
72 //=====================================================================================
73 // function : GetShapeFromIOR()
74 // purpose : Get shape data by the specified IOR
75 //=====================================================================================
76 TopoDS_Shape GEOMBase::GetShapeFromIOR( const QString& IOR )
78 GEOM::GEOM_Object_var geomObj = GEOMBase::GetObjectFromIOR( IOR );
80 GetShape( geomObj, shape, TopAbs_SHAPE );
85 //=====================================================================================
86 // function : GetIndex()
87 // purpose : Get the index of a sub-shape in a main shape : index start at 1
88 //=====================================================================================
89 int GEOMBase::GetIndex( const TopoDS_Shape& subshape, const TopoDS_Shape& shape )
92 if ( !shape.IsNull() && !subshape.IsNull() ) {
93 TopTools_IndexedMapOfShape anIndices;
94 TopExp::MapShapes( shape, anIndices );
95 if ( anIndices.Contains( subshape ) )
96 idx = anIndices.FindIndex( subshape );
102 //=======================================================================
103 // function : GetTopoFromSelection()
104 // purpose : Define tds from a single selection and retuen true
105 //=======================================================================
106 TopoDS_Shape GEOMBase::GetTopoFromSelection( const SALOME_ListIO& IObjects )
109 if ( IObjects.Extent() == 1 ){
110 Handle(SALOME_InteractiveObject) IO = IObjects.First();
111 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
112 if ( IO->hasEntry() && study ) {
113 _PTR(Study) studyDS = study->studyDS();
114 _PTR(SObject) obj( studyDS->FindObjectID( IO->getEntry() ) );
115 _PTR(GenericAttribute) anAttr;
116 if ( obj && obj->FindAttribute( anAttr, "AttributeIOR" ) ) {
117 _PTR(AttributeIOR) anIOR( anAttr );
118 shape = GetShapeFromIOR( anIOR->Value().c_str() );
125 //=======================================================================
126 // function : GetNameOfSelectedIObjects()
127 // purpose : Define the name geom++ or other name of mono or multi sel.
128 //=======================================================================
129 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& IObjects,
131 const bool shapesOnly )
134 name = ""; // clear output name
137 nbSel = IObjects.Extent();
139 Handle(SALOME_InteractiveObject) anIObj = IObjects.First();
140 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
141 if ( anIObj->hasEntry() && study ) {
142 _PTR(Study) studyDS = study->studyDS();
143 _PTR(SObject) obj( studyDS->FindObjectID( anIObj->getEntry() ) );
144 _PTR(GenericAttribute) anAttr;
145 if ( obj && obj->FindAttribute( anAttr, "AttributeName" ) ) {
146 _PTR(AttributeName) aNameAttr ( anAttr );
147 name = aNameAttr->Value().c_str();
153 GEOM::ListOfGO anObjs;
154 ConvertListOfIOInListOfGO( IObjects, anObjs, shapesOnly );
155 nbSel = anObjs.length();
157 name = GetName( anObjs[ 0 ] );
161 name = QObject::tr( "%1_objects" ).arg( nbSel );
167 //=================================================================================
168 // function : GetShapeTypeString()
169 // purpose : for a single shape
170 //=================================================================================
171 QString GEOMBase::GetShapeTypeString(const TopoDS_Shape& shape)
174 if ( !shape.IsNull() ) {
175 switch ( shape.ShapeType() ) {
176 case TopAbs_COMPOUND:
178 aTypeString = QObject::tr( "GEOM_COMPOUND" );
181 case TopAbs_COMPSOLID:
183 aTypeString = QObject::tr( "GEOM_COMPOUNDSOLID" );
188 aTypeString = QObject::tr( "GEOM_SOLID" );
193 aTypeString = QObject::tr( "GEOM_SHELL" );
198 BRepAdaptor_Surface surf( TopoDS::Face( shape ) );
199 switch ( surf.GetType() ) {
202 aTypeString = QObject::tr( "GEOM_PLANE" );
205 case GeomAbs_Cylinder:
207 aTypeString = QObject::tr( "GEOM_SURFCYLINDER" );
212 aTypeString = QObject::tr( "GEOM_SURFSPHERE" );
217 aTypeString = QObject::tr( "GEOM_SURFTORUS" );
222 aTypeString = QObject::tr( "GEOM_SURFCONE" );
227 aTypeString = QObject::tr( "GEOM_FACE" );
235 aTypeString = QObject::tr( "GEOM_WIRE" );
240 BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
241 switch ( curv.GetType() ) {
244 aTypeString = ( qAbs( curv.FirstParameter() ) >= 1E6 || qAbs( curv.LastParameter() ) >= 1E6 ) ?
245 QObject::tr( "GEOM_LINE" ) : QObject::tr( "GEOM_EDGE" );
250 aTypeString = curv.IsClosed() ? QObject::tr( "GEOM_CIRCLE" ) : QObject::tr( "GEOM_ARC" );
255 aTypeString = QObject::tr( "GEOM_EDGE" );
263 aTypeString = QObject::tr( "GEOM_VERTEX" );
268 aTypeString = QObject::tr( "GEOM_SHAPE" );
281 //=======================================================================
282 // function : ConvertIORinGEOMAISShape()
284 //=======================================================================
285 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const QString& IOR, bool onlyInActiveView)
287 Handle(GEOM_AISShape) shape;
289 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
291 _PTR(Study) studyDS = study->studyDS();
292 _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
294 QList<SUIT_ViewWindow*> views;
295 if ( onlyInActiveView )
296 views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
298 views = SUIT_Session::session()->activeApplication()->desktop()->windows();
299 foreach ( SUIT_ViewWindow* view, views ) {
300 if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
301 Handle(AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)view->getViewManager()->getViewModel())->getAISContext();
303 AIS_ListOfInteractive displayed;
304 ic->DisplayedObjects( displayed );
305 AIS_ListIteratorOfListOfInteractive it( displayed );
306 while ( it.More() && shape.IsNull() ) {
307 if ( it.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
308 Handle(GEOM_AISShape) sh = Handle(GEOM_AISShape)::DownCast( it.Value() );
309 if ( !sh.IsNull() && sh->hasIO() ) {
310 Handle(SALOME_InteractiveObject) IO = sh->getIO();
311 if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
318 if ( !shape.IsNull() ) break;
326 //=======================================================================
327 // function : ConvertIORinGEOMActor()
329 //=======================================================================
330 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const QString& IOR, bool onlyInActiveView)
332 GEOM_Actor* actor = 0;
334 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
336 _PTR(Study) studyDS = study->studyDS();
337 _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
339 QList<SUIT_ViewWindow*> views;
340 if ( onlyInActiveView )
341 views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
343 views = SUIT_Session::session()->activeApplication()->desktop()->windows();
344 foreach ( SUIT_ViewWindow* view, views ) {
345 if ( view && view->getViewManager()->getType() == SVTK_Viewer::Type() ) {
346 SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( view );
347 if( !aVTKViewWindow )
349 vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
350 vtkActorCollection* theActors = Renderer->GetActors();
351 theActors->InitTraversal();
352 vtkActor* a = theActors->GetNextActor();
353 while( a && !actor ) {
354 if ( a->IsA( "GEOM_Actor" ) ) {
355 GEOM_Actor* ga = GEOM_Actor::SafeDownCast( a );
356 if ( ga && ga->hasIO() ) {
357 Handle(SALOME_InteractiveObject) IO = ga->getIO();
358 if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
362 a = theActors->GetNextActor();
372 //=======================================================================
373 // function : GetAIS()
375 //=======================================================================
376 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& IO,
377 bool onlyInActiveView, bool /*onlyGeom*/ )
379 Handle(AIS_InteractiveObject) aisObject;
381 if ( !IO.IsNull() && IO->hasEntry() ) {
382 QList<SUIT_ViewWindow*> views;
383 if ( onlyInActiveView )
384 views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
386 views = SUIT_Session::session()->activeApplication()->desktop()->windows();
388 foreach ( SUIT_ViewWindow* view, views ) {
389 if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
390 OCCViewer_Viewer* occViewer=(OCCViewer_Viewer*)view->getViewManager()->getViewModel();
391 SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
393 SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( IO->getEntry() ) );
394 if ( occPrs && !occPrs->IsNull() ) {
395 AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
396 if( !shapes.Extent() ) continue;
397 aisObject=shapes.First();
402 if ( !aisObject.IsNull() ) break;
410 //=======================================================================
411 // function : ConvertIOinGEOMAISShape()
413 //=======================================================================
414 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape( const Handle(SALOME_InteractiveObject)& IO, bool onlyInActiveView )
416 return Handle(GEOM_AISShape)::DownCast( GetAIS( IO, onlyInActiveView, true ) );
420 //=======================================================================
421 // function : ConvertListOfIOInListOfIOR()
423 //=======================================================================
424 QStringList GEOMBase::ConvertListOfIOInListOfIOR( const SALOME_ListIO& IObjects )
427 SALOME_ListIteratorOfListIO it( IObjects );
428 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
430 _PTR(Study) studyDS = study->studyDS();
431 for ( ; it.More(); it.Next() ) {
432 GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
433 if ( !CORBA::is_nil( geomObj ) )
434 iors.append( GetIORFromObject( geomObj ) );
441 //=======================================================================
442 // function : ConvertIOinGEOMObject()
444 //=======================================================================
445 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject( const Handle(SALOME_InteractiveObject)& IO )
447 GEOM::GEOM_Object_var object;
449 if ( !IO.IsNull() && IO->hasEntry() ) {
450 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
452 _PTR(Study) studyDS = study->studyDS();
453 _PTR(SObject) obj = studyDS->FindObjectID( IO->getEntry() );
454 if ( GeometryGUI::IsInGeomComponent( obj )) {
455 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( obj );
456 object = GEOM::GEOM_Object::_narrow( corbaObj );
460 return object._retn();
464 //=======================================================================
465 // function : ConvertListOfIOInListOfGO()
467 //=======================================================================
468 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& IObjects,
469 GEOM::ListOfGO& geomObjects,
472 geomObjects.length( 0 );
474 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
476 _PTR(Study) studyDS = study->studyDS();
478 geomObjects.length( IObjects.Extent() );
479 SALOME_ListIteratorOfListIO it( IObjects );
482 for ( ; it.More(); it.Next() ) {
483 GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
484 if ( !CORBA::is_nil( geomObj ) && ( !shapesOnly || IsShape( geomObj ) ) )
485 geomObjects[ i++ ] = geomObj;
487 geomObjects.length( i );
491 //=================================================================================
492 // function : CreateArrowForLinearEdge()
493 // purpose : Create a cone topology to be used to display an arrow in the middle
494 // : of an edge showing its orientation. (For simulation and Viewer OCC only)
495 //=================================================================================
496 TopoDS_Shape GEOMBase::CreateArrowForLinearEdge( const TopoDS_Shape& shape )
498 TopoDS_Shape ArrowCone;
500 SUIT_ViewWindow* view = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
501 if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() && shape.ShapeType() == TopAbs_EDGE ) {
502 Handle(V3d_View) view3d = ((OCCViewer_ViewWindow*)view)->getViewPort()->getView();
503 Standard_Real Width, Height;
504 view3d->Size( Width, Height );
505 const Standard_Real aHeight = (Width + Height) / 50.0;
508 Standard_Real first, last;
509 Handle(Geom_Curve) curv = BRep_Tool::Curve( TopoDS::Edge( shape ), first, last );
510 if ( !curv.IsNull() && curv->IsCN(1) ) {
511 const Standard_Real param = ( first+last ) / 2.0;
512 gp_Pnt middleParamPoint;
514 curv->D1( param, middleParamPoint, V1 );
515 if ( V1.Magnitude() > Precision::Confusion() ) {
516 /* Topology orientation not geom orientation */
517 if ( shape.Orientation() == TopAbs_REVERSED )
520 gp_Ax2 anAxis( middleParamPoint, gp_Dir( V1 ) );
521 const Standard_Real radius1 = aHeight / 5.0;
522 if ( radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion() )
523 ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
527 catch ( Standard_Failure& ) {
528 // OCC failures are hard to catch in GUI.
529 // This is because of the position for #include <Standard_ErrorHandler.hxx> that is very critical to find
530 // in SALOME environment : compilation error !
538 //=================================================================================
539 // function : VertexToPoint()
540 // purpose : If S can be converted in a gp_Pnt returns true and the result is P
541 //=================================================================================
542 bool GEOMBase::VertexToPoint( const TopoDS_Shape& shape, gp_Pnt& point )
544 if ( shape.IsNull() || shape.ShapeType() != TopAbs_VERTEX )
546 point = BRep_Tool::Pnt( TopoDS::Vertex( shape ) );
551 //=================================================================================
552 // function : GetBipointDxDyDz()
554 //=================================================================================
555 void GEOMBase::GetBipointDxDyDz( const gp_Pnt& point1, const gp_Pnt& point2, double& dx, double& dy, double& dz )
557 dx = point2.X() - point1.X();
558 dy = point2.Y() - point1.Y();
559 dz = point2.Z() - point1.Z();
563 //=================================================================================
564 // function : LinearEdgeExtremities()
565 // purpose : If S can be converted in a linear edge and if initial an final points
566 // : distance is sufficient, returns true else returns false.
567 // : Resulting points are respectively P1 and P2
568 //=================================================================================
569 bool GEOMBase::LinearEdgeExtremities( const TopoDS_Shape& shape, gp_Pnt& point1, gp_Pnt& point2 )
571 if ( shape.IsNull() || shape.ShapeType() != TopAbs_EDGE )
574 BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
575 if ( curv.GetType() != GeomAbs_Line )
580 curv.D0( curv.FirstParameter(), p1 );
581 curv.D0( curv.LastParameter(), p2 );
583 if ( p1.Distance( p2 ) <= Precision::Confusion() )
592 //=======================================================================
593 // function : SelectionByNameInDialogs()
594 // purpose : Called when user has entered a name of object in a LineEdit.
595 // : The selection is changed. Dialog box will receive the
596 // : corresponding signal to manage this event.
597 //=======================================================================
598 bool GEOMBase::SelectionByNameInDialogs( QWidget* widget, const QString& objectUserName, const SALOME_ListIO& /*IObjects*/ )
600 /* Find SObject with name in component GEOM */
601 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
602 if ( !study ) return false;
603 _PTR(Study) studyDS = study->studyDS();
605 std::vector<_PTR(SObject)> listSO = studyDS->FindObjectByName( objectUserName.toStdString(), "GEOM" );
607 if ( listSO.size() < 1 ) {
608 SUIT_MessageBox::critical( widget,
609 QObject::tr( "GEOM_WRN_WARNING" ),
610 QObject::tr( "GEOM_NAME_INCORRECT" ),
611 QObject::tr( "GEOM_BUT_OK" ) );
615 /* More than one object with same name */
616 if ( listSO.size() > 1 ) {
617 SUIT_MessageBox::critical( widget,
618 QObject::tr("GEOM_WRN_WARNING"),
619 QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE"),
620 QObject::tr("GEOM_BUT_OK") );
624 /* Create a SALOME_InteractiveObject with a SALOME::SObject */
625 Handle(SALOME_InteractiveObject) IO = new SALOME_InteractiveObject( listSO[0]->GetID().c_str(),
627 objectUserName.toUtf8().constData() );
629 /* Add as a selected object */
630 /* Clear any previous selection : */
631 /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
632 // Sel->ClearIObjects(); //mzn
633 // Sel->AddIObject(SI); //mzn
638 //=======================================================================
639 // function : DefineDlgPosition()
640 // purpose : Define x and y the default position for a dialog box
641 //=======================================================================
642 void GEOMBase::DefineDlgPosition( QWidget* dlg, int& x, int& y )
644 /* Here the position is on the bottom right corner - 10 */
645 SUIT_Desktop* d = SUIT_Session::session()->activeApplication()->desktop();
646 x = abs( d->x() + d->size().width() - dlg->size().width() - 10 );
647 y = abs( d->y() + d->size().height() - dlg->size().height() - 10 );
651 //=======================================================================
652 // function : GetDefaultName()
653 // purpose : Generates default names
654 //=======================================================================
655 QString GEOMBase::GetDefaultName( const QString& operation, bool extractPrefix )
659 // collect all object names of GEOM component
660 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
662 _PTR(Study) studyDS = study->studyDS();
664 std::set<std::string> names;
665 _PTR(SComponent) component( studyDS->FindComponent( "GEOM" ) );
667 _PTR(ChildIterator) it( studyDS->NewChildIterator( component ) );
668 for ( it->InitEx( true ); it->More(); it->Next() ) {
669 names.insert( it->Value()->GetName() );
673 // build a unique name
675 bool isUnique = false;
676 QString prefix = operation;
678 if ( extractPrefix ) {
679 QStringList parts = prefix.split( "_", QString::KeepEmptyParts );
680 if ( parts.count() > 1 ) {
682 aNumber = parts.last().toLong( &ok );
685 prefix = parts.join( "_" );
691 while ( !isUnique ) {
692 aName = prefix + "_" + QString::number( ++aNumber );
693 isUnique = ( names.count( aName.toStdString()) == 0 );
700 //=======================================================================
701 // function : ShowErrorMessage()
702 // purpose : Shows message box with error code and comment
703 //=======================================================================
704 void GEOMBase::ShowErrorMessage( const QString& errorCode, const QString& comment )
707 text << QObject::tr( "GEOM_PRP_ABORT" );
708 if ( !errorCode.isEmpty() )
709 text << QObject::tr( errorCode.toLatin1().constData() );
710 if ( !comment.isEmpty() )
711 text << QObject::tr( comment.toUtf8().constData() );
713 SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
714 QObject::tr( "GEOM_ERROR" ),
716 QObject::tr( "GEOM_BUT_OK" ) );
720 //=======================================================================
721 // function : GetObjectFromIOR()
722 // purpose : returns a GEOM_Object by given IOR (string)
723 //=======================================================================
724 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const QString& IOR )
726 return GeometryGUI::GetObjectFromIOR (IOR);
729 //=======================================================================
730 // function : GetIORFromObject()
731 // purpose : returns IOR of a given GEOM_Object
732 //=======================================================================
733 QString GEOMBase::GetIORFromObject( GEOM::GEOM_Object_ptr object )
735 return GeometryGUI::GetIORFromObject (object);
738 //=======================================================================
739 // function : GetShape()
740 // purpose : returns a TopoDS_Shape stored in GEOM_Object
741 //=======================================================================
742 bool GEOMBase::GetShape( GEOM::GEOM_Object_ptr object, TopoDS_Shape& shape, const TopAbs_ShapeEnum type )
744 shape = TopoDS_Shape();
745 if ( !CORBA::is_nil( object ) ) {
746 TopAbs_ShapeEnum stype = (TopAbs_ShapeEnum)( object->GetShapeType() );
747 if ( type == TopAbs_SHAPE || type == stype )
749 GEOM::GEOM_Gen_var gen = object->GetGen();
750 shape = GEOM_Client::get_client().GetShape( gen, object );
753 return !shape.IsNull();
756 //=======================================================================
757 // function : GetName()
758 // purpose : Get name of object
759 //=======================================================================
760 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr object )
763 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
765 if ( !CORBA::is_nil( object ) ) {
766 // 1. search if object is already published in the study
767 CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( object );
768 if ( study && strcmp( IOR.in(), "" ) != 0 ) {
769 _PTR(SObject) aSObj( study->studyDS()->FindObjectIOR( std::string( IOR.in() ) ) );
770 _PTR(GenericAttribute) anAttr;
771 if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") ) {
772 _PTR(AttributeName) aNameAttr( anAttr );
773 name = aNameAttr->Value().c_str();
777 // 2. if object is not found in the study, try default name
778 if ( name.isEmpty() ) {
779 if ( object->IsMainShape() ) {
780 name = GetDefaultName( "geomObj" );
783 GEOM::GEOM_Object_var mainShape = object->GetMainShape();
784 if ( !CORBA::is_nil( mainShape ) ) {
785 GEOM::ListOfLong_var indices = object->GetSubShapeIndices();
786 if ( indices->length() > 0 ) {
787 TopAbs_ShapeEnum type = (TopAbs_ShapeEnum)( object->GetShapeType() );
788 name = QString( "%1:%2_%3" ).arg( GetName( mainShape.in() ) )
789 .arg( TypeName( type ) ).arg( indices[0] );
799 //=======================================================================
800 // function : IsShape()
801 // purpose : Return TRUE if object is valid and has shape
802 //=======================================================================
803 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr object )
805 return !object->_is_nil() && object->IsShape();
808 //=======================================================================
809 // function : TypeName()
810 // purpose : Get string representation for the shape type
811 //=======================================================================
812 QString GEOMBase::TypeName( TopAbs_ShapeEnum type, bool capitalize )
814 QString name = "shape";
816 case TopAbs_COMPSOLID:
817 name = "compsolid"; break;
818 case TopAbs_COMPOUND:
819 name = "compound"; break;
821 name = "solid"; break;
823 name = "shell"; break;
825 name = "face"; break;
827 name = "wire"; break;
829 name = "edge"; break;
831 name = "vertex"; break;
835 if ( capitalize && !name.isEmpty() )
836 name = name.left(1).toUpper() + name.mid(1);
840 //================================================================
841 // Function : GetEntry
842 // Purpose : Get study entry for the given object (if it is published)
843 //================================================================
844 QString GEOMBase::GetEntry( GEOM::GEOM_Object_ptr object )
847 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
848 if ( study && !CORBA::is_nil( object ) ) {
849 QString objIOR = GetIORFromObject( object );
850 if ( !objIOR.isEmpty() ) {
851 _PTR(SObject) SO( study->studyDS()->FindObjectIOR( objIOR.toLatin1().constData() ) );
853 entry = SO->GetID().c_str();
859 //================================================================
860 // Function : PublishSubObject
861 // Purpose : Publish sub-shape under the main object
862 //================================================================
863 void GEOMBase::PublishSubObject( GEOM::GEOM_Object_ptr object, const QString& name )
865 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
866 if ( study && !CORBA::is_nil( object ) ) {
867 _PTR(Study) studyDS = study->studyDS();
868 QString entry = GetEntry( object );
869 GEOM::GEOM_Object_var father = object->GetMainShape();
870 QString fatherEntry = GetEntry( father );
871 if ( entry.isEmpty() && !CORBA::is_nil( father ) && !fatherEntry.isEmpty() ) {
872 QString aName = !name.isEmpty() ? name : GetName( object );
873 GeometryGUI::GetGeomGen()->AddInStudy( object, aName.toUtf8().data(), father.in() );
878 //================================================================
879 // Function : synchronize
881 //================================================================
882 void GEOMBase::Synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
884 // 1. remove items from the "left" list that are not in the "right" list
885 QMutableListIterator<GEOM::GeomObjPtr> it1( left );
886 while ( it1.hasNext() ) {
887 GEOM::GeomObjPtr o1 = it1.next();
889 QMutableListIterator<GEOM::GeomObjPtr> it2( right );
890 while ( it2.hasNext() && !found )
891 found = o1 == it2.next();
895 // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
897 while ( it1.hasNext() ) {
898 GEOM::GeomObjPtr o1 = it1.next();
900 QMutableListIterator<GEOM::GeomObjPtr> it2( left );
901 while ( it2.hasNext() && !found )
902 found = o1 == it2.next();