1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2004 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org or email : webmaster.salome@opencascade.org
24 // File : GEOMBase_Helper.cxx
25 // Author : Sergey ANIKIN
29 #include "GEOMBase_Helper.h"
31 #include "GEOM_Operation.h"
32 #include "GeometryGUI.h"
33 #include "GEOM_Displayer.h"
34 #include "GEOMImpl_Types.hxx"
35 #include "QAD_Study.h"
36 #include "QAD_Application.h"
37 #include "QAD_Desktop.h"
38 #include "QAD_MessageBox.h"
39 #include "QAD_ViewFrame.h"
40 #include "QAD_RightFrame.h"
41 #include "QAD_WaitCursor.h"
42 #include "SALOMEGUI_QtCatchCorbaException.hxx"
44 #include <TColStd_MapOfInteger.hxx>
45 #include <TCollection_AsciiString.hxx>
49 //================================================================
50 // Function : getActiveView
51 // Purpose : Get active study frame, returns 0 if no open study frame
52 //================================================================
53 static QAD_ViewFrame* getActiveView()
55 QAD_Study* activeStudy = QAD_Application::getDesktop()->getActiveStudy();
57 QAD_StudyFrame* sf = activeStudy->getActiveStudyFrame();
59 return sf->getRightFrame()->getViewFrame();
66 //================================================================
67 // Function : getGeomEngine
68 // Purpose : Static method
69 //================================================================
70 GEOM::GEOM_Gen_ptr GEOMBase_Helper::getGeomEngine()
72 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
73 return GEOM::GEOM_Gen::_narrow(comp);
76 //================================================================
77 // Function : GEOMBase_Helper
79 //================================================================
80 GEOMBase_Helper::GEOMBase_Helper()
81 : myViewFrame( 0 ), myDisplayer( 0 ), myCommand( 0 ), isPreview( false )
85 //================================================================
86 // Function : ~GEOMBase_Helper
88 //================================================================
89 GEOMBase_Helper::~GEOMBase_Helper()
91 if ( !QAD_Application::getDesktop() )
94 if ( myPreview.size() )
99 globalSelection( GEOM_ALLOBJECTS, true );
104 //================================================================
105 // Function : display
107 //================================================================
108 void GEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
110 ObjectList::const_iterator it;
111 for ( it = objList.begin(); it != objList.end(); it++ ) {
112 display( *it, false );
114 if ( !objList.empty() && updateView )
115 getDisplayer()->UpdateViewer();
118 //================================================================
119 // Function : display
120 // Purpose : Display object.
121 // Important : Object must be already in study
122 //================================================================
123 void GEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
125 // Unset color of shape ( this color may be set during preview displaying )
126 // Default color will be used
127 getDisplayer()->UnsetColor();
128 getDisplayer()->UnsetWidth();
130 // Enable activisation of selection
131 getDisplayer()->SetToActivate( true );
134 getDisplayer()->Display( object, updateView );
137 //================================================================
140 //================================================================
141 void GEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
143 ObjectList::const_iterator it = objList.begin();
144 for ( ; it != objList.end(); it++ ) {
147 if ( !objList.empty() && updateView )
148 getDisplayer()->UpdateViewer();
151 //================================================================
154 //================================================================
155 void GEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
157 if ( !object->_is_nil() ) {
158 string entry = getEntry( object );
159 getDisplayer()->Erase( new SALOME_InteractiveObject(
160 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( object ) ) ), true, updateView );
164 //================================================================
165 // Function : redisplay
167 //================================================================
168 void GEOMBase_Helper::redisplay( const ObjectList& objList,
169 const bool withChildren,
170 const bool updateView )
172 ObjectList::const_iterator it = objList.begin();
173 for ( ; it != objList.end(); it++ ) {
174 redisplay( *it, withChildren, false );
176 if ( !objList.empty() && updateView )
177 getDisplayer()->UpdateViewer();
180 //================================================================
181 // Function : redisplay
183 //================================================================
184 void GEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
185 const bool withChildren,
186 const bool updateView )
188 if ( !object->_is_nil() ) {
189 // Unset color of shape ( this color may be set during preview displaying )
190 // Default color will be used
191 getDisplayer()->UnsetColor();
192 getDisplayer()->UnsetWidth();
194 // Enable activisation of selection
195 getDisplayer()->SetToActivate( true );
197 string entry = getEntry( object );
198 getDisplayer()->Redisplay( new SALOME_InteractiveObject( entry.c_str(), "GEOM", strdup( GEOMBase::GetName( object ) ) ), false );
201 if ( withChildren ) {
202 QAD_Study* aDoc = getStudy();
203 if ( aDoc && !CORBA::is_nil( aDoc->getStudyDocument() ) ) {
204 SALOMEDS::Study_var aStudy = aDoc->getStudyDocument();
205 SALOMEDS::SObject_var aSObj = aStudy->FindObjectIOR( GeometryGUI::GetORB()->object_to_string( object ) );
206 if ( !CORBA::is_nil( aSObj ) ) {
207 SALOMEDS::ChildIterator_var anIt = aStudy->NewChildIterator( aSObj );
208 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
209 GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow( anIt->Value()->GetObject() );
210 if ( !CORBA::is_nil( aChild ) ) {
211 if ( !aChild->_is_nil() ) {
212 string entry = getEntry( aChild );
213 getDisplayer()->Redisplay( new SALOME_InteractiveObject(
214 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( aChild ) ) ), false );
223 getDisplayer()->UpdateViewer();
226 //================================================================
227 // Function : displayPreview
228 // Purpose : Method for displaying preview based on execute() results
229 //================================================================
230 void GEOMBase_Helper::displayPreview( const bool activate,
232 const bool toRemoveFromEngine,
233 const double lineWidth )
237 if ( !isValid( msg ) )
239 erasePreview( update );
244 erasePreview( false );
249 if ( !execute( objects ) || !getOperation()->IsDone() ) {
253 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
255 displayPreview( *it, true, activate, false, lineWidth );
256 if ( toRemoveFromEngine )
257 getGeomEngine()->RemoveObject( *it );
261 catch( const SALOME::SALOME_Exception& e ) {
262 QtCatchCorbaException( e );
271 //================================================================
272 // Function : displayPreview
273 // Purpose : Method for displaying preview of resulting shape
274 //================================================================
275 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
279 const double lineWidth )
281 // Set color for preview shape
282 getDisplayer()->SetColor( Quantity_NOC_VIOLET );
284 // set width of displayed shape
285 getDisplayer()->SetWidth( lineWidth );
287 // Disable activation of selection
288 getDisplayer()->SetToActivate( activate );
290 // Make a reference to GEOM_Object
291 getDisplayer()->SetName( GeometryGUI::GetORB()->object_to_string( object ) );
294 SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
295 if ( aPrs == 0 || aPrs->IsNull() )
299 displayPreview( aPrs, append, update );
301 getDisplayer()->UnsetName();
303 // Enable activation of displayed objects
304 getDisplayer()->SetToActivate( true );
307 //================================================================
308 // Function : displayPreview
309 // Purpose : Method for displaying arbitrary preview objects (not limited to shapes)
310 //================================================================
311 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
316 erasePreview( false );
318 // remember current view frame to make correct erase preview later
319 myViewFrame = getActiveView();
321 if ( myViewFrame == 0 )
325 ( (SALOME_View*)myViewFrame )->Display( prs );
327 // Add prs to the preview list
328 myPreview.push_back( (SALOME_Prs*)prs );
332 myViewFrame->Repaint();
335 //================================================================
336 // Function : erasePreview
338 //================================================================
339 void GEOMBase_Helper::erasePreview( const bool update )
341 // check view frame where the preview was displayed
342 bool vfOK = checkViewFrame() && myViewFrame;
343 // Iterate through presentations and delete them
344 for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
346 ( (SALOME_View*)myViewFrame )->Erase( *anIter, true );
356 //================================================================
357 // Function : localSelection
358 // Purpose : Activate selection of objects of a given type
359 // IMPORTANT : Works after localSelection( ... ) method call only
360 //================================================================
361 void GEOMBase_Helper::activate( const int theType )
363 SALOMEDS::Study_var aStudy = getStudy()->getStudyDocument();
364 SALOMEDS::SComponent_var aGeom = aStudy->FindComponent( "GEOM" );
365 if ( aGeom->_is_nil() )
369 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( aGeom );
370 for ( ; anIter->More(); anIter->Next() )
372 SALOMEDS::SObject_var aSO = anIter->Value();
373 if ( !aSO->_is_nil() )
375 SALOMEDS::SObject_var aRefSO;
376 if ( !aSO->ReferencedObject( aRefSO ) )
378 GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow( aSO->GetObject() );
379 if ( !anObj->_is_nil() && anObj->GetType() == theType )
380 aList.Append( new SALOME_InteractiveObject( aSO->GetID(), "GEOM", aSO->GetName()) );
385 myDisplayer->LocalSelection( aList, 0 );
388 //================================================================
389 // Function : localSelection
390 // Purpose : Activate selection of subshapes in accordance with mode
391 // theMode is from TopAbs_ShapeEnum
392 //================================================================
393 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
395 SALOME_ListIO aListOfIO;
397 ObjectList::const_iterator anIter = theObjs.begin();
398 for ( ; anIter != theObjs.end(); ++anIter )
400 GEOM::GEOM_Object_ptr anObj = *anIter;
401 if ( anObj->_is_nil() )
403 string aEntry = getEntry( anObj );
405 aListOfIO.Append( new SALOME_InteractiveObject(
406 aEntry.c_str(), "GEOM", strdup( GEOMBase::GetName( anObj ) ) ) );
409 myDisplayer->LocalSelection( aListOfIO, theMode );
412 //================================================================
413 // Function : localSelection
414 // Purpose : Activate selection of subshapes in accordance with mode
415 // theMode is from TopAbs_ShapeEnum
416 //================================================================
417 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
419 // If object is null local selection for all objects is activated
420 if ( obj->_is_nil() ) {
421 myDisplayer->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
426 objList.push_back( obj );
427 localSelection( objList, mode );
431 //================================================================
432 // Function : globalSelection
433 // Purpose : Activate selection of subshapes. Set selection filters
434 // in accordance with mode. theMode is from GEOMImpl_Types
435 //================================================================
436 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
438 myDisplayer->GlobalSelection( theMode, update );
441 //================================================================
442 // Function : globalSelection
443 // Purpose : Activate selection of subshapes. Set selection filters
444 // in accordance with mode. theMode is from GEOMImpl_Types
445 //================================================================
446 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
449 myDisplayer->GlobalSelection( theModes, update );
452 //================================================================
453 // Function : addInStudy
454 // Purpose : Add object in study
455 //================================================================
456 void GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
461 SALOMEDS::Study_var aStudy = getStudy()->getStudyDocument();
462 if ( aStudy->_is_nil() || theObj->_is_nil() )
465 GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
467 GeometryGUI::GetGeomGUI()->GetGeomGen()->AddInStudy( aStudy, theObj, theName, aFatherObj );
470 //================================================================
471 // Function : updateObjBrowser
472 // Purpose : Update object browser
473 //================================================================
474 void GEOMBase_Helper::updateObjBrowser() const
476 getStudy()->updateObjBrowser();
479 //================================================================
480 // Function : updateViewer
481 // Purpose : Update active 3D view
482 //================================================================
483 void GEOMBase_Helper::updateViewer()
485 getDisplayer()->UpdateViewer();
488 //================================================================
489 // Function : getStudyId
490 // Purpose : Get study Id
491 //================================================================
492 int GEOMBase_Helper::getStudyId() const
496 anId = getStudy()->getStudyId();
500 //================================================================
501 // Function : getStudy
502 // Purpose : Returns the active study. It is recommended to use
503 // this method instead of direct desktop->getActiveStudy() calls
504 //================================================================
505 QAD_Study* GEOMBase_Helper::getStudy() const
507 // Probably, it would be better to rememeber the active study in helper...
508 return QAD_Application::getDesktop()->getActiveStudy();
511 //================================================================
512 // Function : getEntry
514 //================================================================
515 char* GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
517 QAD_Study* study = getStudy();
519 string IOR = GEOMBase::GetIORFromObject( object);
521 SALOMEDS::SObject_var SO = study->getStudyDocument()->FindObjectIOR( IOR.c_str() );
522 if ( !SO->_is_nil() ) {
530 //================================================================
531 // Function : getDisplayer
533 //================================================================
534 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
537 myDisplayer = new GEOM_Displayer();
541 //================================================================
542 // Function : clearShapeBuffer
544 //================================================================
545 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
547 if ( CORBA::is_nil( theObj ) )
550 string IOR = GeometryGUI::GetORB()->object_to_string( theObj );
551 TCollection_AsciiString asciiIOR( strdup( IOR.c_str() ) );
552 GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );
554 if ( !getStudy() || CORBA::is_nil( getStudy()->getStudyDocument() ) )
557 SALOMEDS::Study_var aStudy = getStudy()->getStudyDocument();
558 SALOMEDS::SObject_var aSObj = aStudy->FindObjectIOR( IOR.c_str() );
559 if ( CORBA::is_nil( aSObj ) )
562 SALOMEDS::ChildIterator_var anIt = aStudy->NewChildIterator( aSObj );
563 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
564 SALOMEDS::GenericAttribute_var anAttr;
565 if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
566 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
567 TCollection_AsciiString asciiIOR( anIOR->Value() );
568 GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );
573 //================================================================
574 // Function : openCommand
576 //================================================================
577 bool GEOMBase_Helper::openCommand()
580 if ( !getStudy() || hasCommand() )
583 GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
584 if ( !anOp->_is_nil() ) {
585 myCommand = new GEOM_Operation( getStudy(), anOp.in() );
593 //================================================================
594 // Function : abortCommand
596 //================================================================
597 bool GEOMBase_Helper::abortCommand()
608 //================================================================
609 // Function : commitCommand
611 //================================================================
612 bool GEOMBase_Helper::commitCommand( const char* )
623 //================================================================
624 // Function : hasCommand
626 //================================================================
627 bool GEOMBase_Helper::hasCommand() const
629 return (bool)myCommand;
632 //================================================================
633 // Function : getOperation
635 //================================================================
636 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
638 if ( myOperation->_is_nil() )
639 myOperation = createOperation();
646 //================================================================
647 // Function : checkViewFrame
649 //================================================================
650 bool GEOMBase_Helper::checkViewFrame()
653 QListIterator<QAD_Study> it( QAD_Application::getDesktop()->getActiveApp()->getStudies() );
654 for ( ; it.current(); ++it ) {
655 QListIterator<QAD_StudyFrame> it1( it.current()->getStudyFrames() );
656 for ( ; it1.current(); ++it1 ) {
657 if ( myViewFrame == it1.current()->getRightFrame()->getViewFrame() )
666 //================================================================
667 // Function : onAccept
668 // Purpose : This method should be called from dialog's slots onOk() and onApply()
669 // It perfroms user input validation, then it
670 // performs a proper operation and manages transactions, etc.
671 //================================================================
672 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
674 QAD_Study* aDoc = QAD_Application::getDesktop()->getActiveStudy();
675 SALOMEDS::Study_var aStudy = aDoc->getStudyDocument();
677 bool aLocked = aStudy->GetProperties()->IsLocked();
679 MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
680 QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
681 QObject::tr("WRN_WARNING"),
682 QObject::tr("WRN_STUDY_LOCKED"),
683 QObject::tr("BUT_OK") );
688 if ( !isValid( msg ) ) {
693 erasePreview( false );
696 if ( ( !publish && !useTransaction ) || openCommand() ) {
698 QAD_Application::getDesktop()->putInfo( "" );
700 if ( !execute( objects ) || !getOperation()->IsDone() ) {
706 const int nbObjs = objects.size();
707 bool withChildren = false;
708 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
712 aName = strlen( getNewObjectName() ) ? GEOMBase::GetDefaultName( getNewObjectName() ) : GEOMBase::GetDefaultName( getPrefix( *it ) );
714 aName = getNewObjectName();
715 // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
716 if ( aName.isEmpty() )
717 aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
719 addInStudy( *it, aName.latin1() );
720 withChildren = false;
721 display( *it, false );
723 else { // asv : fix of PAL6454. If publish==false, then the original shape was modified, and need to be re-cached in GEOM_Client
725 clearShapeBuffer( *it );
727 redisplay( *it, withChildren, false );
734 QAD_Application::getDesktop()->putInfo( QObject::tr("GEOM_PRP_DONE") );
741 catch( const SALOME::SALOME_Exception& e ) {
742 QtCatchCorbaException( e );
752 //================================================================
753 // Function : showError
754 // Purpose : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
755 //================================================================
756 void GEOMBase_Helper::showError()
759 if ( !getOperation()->_is_nil() )
760 msg = QObject::tr( getOperation()->GetErrorCode() );
763 msg = QObject::tr( "GEOM_PRP_ABORT" );
765 QAD_MessageBox::error1( QAD_Application::getDesktop(),
766 QObject::tr( "GEOM_ERROR_STATUS" ),
768 QObject::tr( "BUT_OK" ) );
771 //================================================================
772 // Function : showError
773 // Purpose : Shows a error message followed by <msg>
774 //================================================================
775 void GEOMBase_Helper::showError( const QString& msg )
777 QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
778 if ( !msg.isEmpty() )
780 QAD_MessageBox::error1( QAD_Application::getDesktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
783 //////////////////////////////////////////////////////////////////
784 // Virtual methods to be redefined in dialogs
785 //////////////////////////////////////////////////////////////////
787 //================================================================
788 // Function : createOperation
789 // Purpose : Redefine this method to return proper IOperation reference
790 //================================================================
791 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
793 GEOM::GEOM_IOperations_var aNilOp;
794 return aNilOp._retn();
797 //================================================================
798 // Function : isValid
799 // Purpose : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
800 //================================================================
801 bool GEOMBase_Helper::isValid( QString& )
806 //================================================================
807 // Function : execute
808 // Purpose : This method is called by onAccept().
809 // It should perform the required operation and put all new or modified objects into
810 // <objects> argument.Should return <false> if some error occurs during its execution.
811 //================================================================
812 bool GEOMBase_Helper::execute( ObjectList& objects )
817 //================================================================
818 // Function : getFather
819 // Purpose : This method is called by addInStudy(). It should return a father object
820 // for <theObj> or a nil reference if <theObj> should be published
821 // as a top-level object.
822 //================================================================
823 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
825 return GEOM::GEOM_Object::_nil();
828 //================================================================
829 // Function : getNewObjectName
830 // Purpose : Redefine this method to return proper name for a new object
831 //================================================================
832 const char* GEOMBase_Helper::getNewObjectName() const
837 //================================================================
838 // Function : getPrefix
839 // Purpose : Get prefix for name of created object
840 //================================================================
841 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
843 if ( !myPrefix.isEmpty() || theObj->_is_nil() )
847 if ( !GEOMBase::GetShape( theObj, aShape ) )
850 long aType = aShape.ShapeType();
854 case TopAbs_VERTEX : return QObject::tr( "GEOM_VERTEX" );
855 case TopAbs_EDGE : return QObject::tr( "GEOM_EDGE" );
856 case TopAbs_WIRE : return QObject::tr( "GEOM_WIRE" );
857 case TopAbs_FACE : return QObject::tr( "GEOM_FACE" );
858 case TopAbs_SHELL : return QObject::tr( "GEOM_SHELL" );
859 case TopAbs_SOLID : return QObject::tr( "GEOM_SOLID" );
860 case TopAbs_COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
861 case TopAbs_COMPOUND : return QObject::tr( "GEOM_COMPOUND" );