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 ( myPreview.size() )
96 globalSelection( GEOM_ALLOBJECTS, true );
101 //================================================================
102 // Function : display
104 //================================================================
105 void GEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
107 ObjectList::const_iterator it;
108 for ( it = objList.begin(); it != objList.end(); it++ ) {
109 display( *it, false );
111 if ( !objList.empty() && updateView )
112 getDisplayer()->UpdateViewer();
115 //================================================================
116 // Function : display
117 // Purpose : Display object.
118 // Important : Object must be already in study
119 //================================================================
120 void GEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
122 // Unset color of shape ( this color may be set during preview displaying )
123 // Default color will be used
124 getDisplayer()->UnsetColor();
125 getDisplayer()->UnsetWidth();
127 // Enable activisation of selection
128 getDisplayer()->SetToActivate( true );
131 getDisplayer()->Display( object, updateView );
134 //================================================================
137 //================================================================
138 void GEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
140 ObjectList::const_iterator it = objList.begin();
141 for ( ; it != objList.end(); it++ ) {
144 if ( !objList.empty() && updateView )
145 getDisplayer()->UpdateViewer();
148 //================================================================
151 //================================================================
152 void GEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
154 if ( !object->_is_nil() ) {
155 string entry = getEntry( object );
156 getDisplayer()->Erase( new SALOME_InteractiveObject(
157 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( object ) ) ), true, updateView );
161 //================================================================
162 // Function : redisplay
164 //================================================================
165 void GEOMBase_Helper::redisplay( const ObjectList& objList,
166 const bool withChildren,
167 const bool updateView )
169 ObjectList::const_iterator it = objList.begin();
170 for ( ; it != objList.end(); it++ ) {
171 redisplay( *it, withChildren, false );
173 if ( !objList.empty() && updateView )
174 getDisplayer()->UpdateViewer();
177 //================================================================
178 // Function : redisplay
180 //================================================================
181 void GEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
182 const bool withChildren,
183 const bool updateView )
185 if ( !object->_is_nil() ) {
186 // Unset color of shape ( this color may be set during preview displaying )
187 // Default color will be used
188 getDisplayer()->UnsetColor();
189 getDisplayer()->UnsetWidth();
191 // Enable activisation of selection
192 getDisplayer()->SetToActivate( true );
194 string entry = getEntry( object );
195 getDisplayer()->Redisplay( new SALOME_InteractiveObject( entry.c_str(), "GEOM", strdup( GEOMBase::GetName( object ) ) ), false );
198 if ( withChildren ) {
199 QAD_Study* aDoc = getStudy();
200 if ( aDoc && !CORBA::is_nil( aDoc->getStudyDocument() ) ) {
201 SALOMEDS::Study_var aStudy = aDoc->getStudyDocument();
202 SALOMEDS::SObject_var aSObj = aStudy->FindObjectIOR( GeometryGUI::GetORB()->object_to_string( object ) );
203 if ( !CORBA::is_nil( aSObj ) ) {
204 SALOMEDS::ChildIterator_var anIt = aStudy->NewChildIterator( aSObj );
205 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
206 GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow( anIt->Value()->GetObject() );
207 if ( !CORBA::is_nil( aChild ) ) {
208 if ( !aChild->_is_nil() ) {
209 string entry = getEntry( aChild );
210 getDisplayer()->Redisplay( new SALOME_InteractiveObject(
211 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( aChild ) ) ), false );
220 getDisplayer()->UpdateViewer();
223 //================================================================
224 // Function : displayPreview
225 // Purpose : Method for displaying preview based on execute() results
226 //================================================================
227 void GEOMBase_Helper::displayPreview( const bool activate,
229 const bool toRemoveFromEngine,
230 const double lineWidth )
234 if ( !isValid( msg ) )
236 erasePreview( update );
241 erasePreview( false );
246 if ( !execute( objects ) || !getOperation()->IsDone() ) {
250 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
252 displayPreview( *it, true, activate, false, lineWidth );
253 if ( toRemoveFromEngine )
254 getGeomEngine()->RemoveObject( *it );
258 catch( const SALOME::SALOME_Exception& e ) {
259 QtCatchCorbaException( e );
268 //================================================================
269 // Function : displayPreview
270 // Purpose : Method for displaying preview of resulting shape
271 //================================================================
272 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
276 const double lineWidth )
278 // Set color for preview shape
279 getDisplayer()->SetColor( Quantity_NOC_VIOLET );
281 // set width of displayed shape
282 getDisplayer()->SetWidth( lineWidth );
284 // Disable activation of selection
285 getDisplayer()->SetToActivate( activate );
287 // Make a reference to GEOM_Object
288 getDisplayer()->SetName( GeometryGUI::GetORB()->object_to_string( object ) );
291 SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
292 if ( aPrs == 0 || aPrs->IsNull() )
296 displayPreview( aPrs, append, update );
298 getDisplayer()->UnsetName();
300 // Enable activation of displayed objects
301 getDisplayer()->SetToActivate( true );
304 //================================================================
305 // Function : displayPreview
306 // Purpose : Method for displaying arbitrary preview objects (not limited to shapes)
307 //================================================================
308 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
313 erasePreview( false );
315 // remember current view frame to make correct erase preview later
316 myViewFrame = getActiveView();
318 if ( myViewFrame == 0 )
322 ( (SALOME_View*)myViewFrame )->Display( prs );
324 // Add prs to the preview list
325 myPreview.push_back( (SALOME_Prs*)prs );
329 myViewFrame->Repaint();
332 //================================================================
333 // Function : erasePreview
335 //================================================================
336 void GEOMBase_Helper::erasePreview( const bool update )
338 // check view frame where the preview was displayed
339 bool vfOK = checkViewFrame() && myViewFrame;
340 // Iterate through presentations and delete them
341 for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
343 ( (SALOME_View*)myViewFrame )->Erase( *anIter, true );
353 //================================================================
354 // Function : localSelection
355 // Purpose : Activate selection of objects of a given type
356 // IMPORTANT : Works after localSelection( ... ) method call only
357 //================================================================
358 void GEOMBase_Helper::activate( const int theType )
360 SALOMEDS::Study_var aStudy = getStudy()->getStudyDocument();
361 SALOMEDS::SComponent_var aGeom = aStudy->FindComponent( "GEOM" );
362 if ( aGeom->_is_nil() )
366 SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( aGeom );
367 for ( ; anIter->More(); anIter->Next() )
369 SALOMEDS::SObject_var aSO = anIter->Value();
370 if ( !aSO->_is_nil() )
372 SALOMEDS::SObject_var aRefSO;
373 if ( !aSO->ReferencedObject( aRefSO ) )
375 GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow( aSO->GetObject() );
376 if ( !anObj->_is_nil() && anObj->GetType() == theType )
377 aList.Append( new SALOME_InteractiveObject( aSO->GetID(), "GEOM", aSO->GetName()) );
382 myDisplayer->LocalSelection( aList, 0 );
385 //================================================================
386 // Function : localSelection
387 // Purpose : Activate selection of subshapes in accordance with mode
388 // theMode is from TopAbs_ShapeEnum
389 //================================================================
390 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
392 SALOME_ListIO aListOfIO;
394 ObjectList::const_iterator anIter = theObjs.begin();
395 for ( ; anIter != theObjs.end(); ++anIter )
397 GEOM::GEOM_Object_ptr anObj = *anIter;
398 if ( anObj->_is_nil() )
400 string aEntry = getEntry( anObj );
402 aListOfIO.Append( new SALOME_InteractiveObject(
403 aEntry.c_str(), "GEOM", strdup( GEOMBase::GetName( anObj ) ) ) );
406 myDisplayer->LocalSelection( aListOfIO, theMode );
409 //================================================================
410 // Function : localSelection
411 // Purpose : Activate selection of subshapes in accordance with mode
412 // theMode is from TopAbs_ShapeEnum
413 //================================================================
414 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
416 // If object is null local selection for all objects is activated
417 if ( obj->_is_nil() ) {
418 myDisplayer->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
423 objList.push_back( obj );
424 localSelection( objList, mode );
428 //================================================================
429 // Function : globalSelection
430 // Purpose : Activate selection of subshapes. Set selection filters
431 // in accordance with mode. theMode is from GEOMImpl_Types
432 //================================================================
433 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
435 myDisplayer->GlobalSelection( theMode, update );
438 //================================================================
439 // Function : globalSelection
440 // Purpose : Activate selection of subshapes. Set selection filters
441 // in accordance with mode. theMode is from GEOMImpl_Types
442 //================================================================
443 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
446 myDisplayer->GlobalSelection( theModes, update );
449 //================================================================
450 // Function : addInStudy
451 // Purpose : Add object in study
452 //================================================================
453 void GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
458 SALOMEDS::Study_var aStudy = getStudy()->getStudyDocument();
459 if ( aStudy->_is_nil() || theObj->_is_nil() )
462 GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
464 GeometryGUI::GetGeomGUI()->GetGeomGen()->AddInStudy( aStudy, theObj, theName, aFatherObj );
467 //================================================================
468 // Function : updateObjBrowser
469 // Purpose : Update object browser
470 //================================================================
471 void GEOMBase_Helper::updateObjBrowser() const
473 getStudy()->updateObjBrowser();
476 //================================================================
477 // Function : updateViewer
478 // Purpose : Update active 3D view
479 //================================================================
480 void GEOMBase_Helper::updateViewer()
482 getDisplayer()->UpdateViewer();
485 //================================================================
486 // Function : getStudyId
487 // Purpose : Get study Id
488 //================================================================
489 int GEOMBase_Helper::getStudyId() const
493 anId = getStudy()->getStudyId();
497 //================================================================
498 // Function : getStudy
499 // Purpose : Returns the active study. It is recommended to use
500 // this method instead of direct desktop->getActiveStudy() calls
501 //================================================================
502 QAD_Study* GEOMBase_Helper::getStudy() const
504 // Probably, it would be better to rememeber the active study in helper...
505 return QAD_Application::getDesktop()->getActiveStudy();
508 //================================================================
509 // Function : getEntry
511 //================================================================
512 char* GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
514 QAD_Study* study = getStudy();
516 string IOR = GEOMBase::GetIORFromObject( object);
518 SALOMEDS::SObject_var SO = study->getStudyDocument()->FindObjectIOR( IOR.c_str() );
519 if ( !SO->_is_nil() ) {
527 //================================================================
528 // Function : getDisplayer
530 //================================================================
531 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
534 myDisplayer = new GEOM_Displayer();
538 //================================================================
539 // Function : clearShapeBuffer
541 //================================================================
542 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
544 if ( CORBA::is_nil( theObj ) )
547 string IOR = GeometryGUI::GetORB()->object_to_string( theObj );
548 TCollection_AsciiString asciiIOR( strdup( IOR.c_str() ) );
549 GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );
551 if ( !getStudy() || CORBA::is_nil( getStudy()->getStudyDocument() ) )
554 SALOMEDS::Study_var aStudy = getStudy()->getStudyDocument();
555 SALOMEDS::SObject_var aSObj = aStudy->FindObjectIOR( IOR.c_str() );
556 if ( CORBA::is_nil( aSObj ) )
559 SALOMEDS::ChildIterator_var anIt = aStudy->NewChildIterator( aSObj );
560 for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
561 SALOMEDS::GenericAttribute_var anAttr;
562 if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
563 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
564 TCollection_AsciiString asciiIOR( anIOR->Value() );
565 GeometryGUI::GetGeomGUI()->GetShapeReader().RemoveShapeFromBuffer( asciiIOR );
570 //================================================================
571 // Function : openCommand
573 //================================================================
574 bool GEOMBase_Helper::openCommand()
577 if ( !getStudy() || hasCommand() )
580 GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
581 if ( !anOp->_is_nil() ) {
582 myCommand = new GEOM_Operation( getStudy(), anOp.in() );
590 //================================================================
591 // Function : abortCommand
593 //================================================================
594 bool GEOMBase_Helper::abortCommand()
605 //================================================================
606 // Function : commitCommand
608 //================================================================
609 bool GEOMBase_Helper::commitCommand( const char* )
620 //================================================================
621 // Function : hasCommand
623 //================================================================
624 bool GEOMBase_Helper::hasCommand() const
626 return (bool)myCommand;
629 //================================================================
630 // Function : getOperation
632 //================================================================
633 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
635 if ( myOperation->_is_nil() )
636 myOperation = createOperation();
643 //================================================================
644 // Function : checkViewFrame
646 //================================================================
647 bool GEOMBase_Helper::checkViewFrame()
650 QListIterator<QAD_Study> it( QAD_Application::getDesktop()->getActiveApp()->getStudies() );
651 for ( ; it.current(); ++it ) {
652 QListIterator<QAD_StudyFrame> it1( it.current()->getStudyFrames() );
653 for ( ; it1.current(); ++it1 ) {
654 if ( myViewFrame == it1.current()->getRightFrame()->getViewFrame() )
663 //================================================================
664 // Function : onAccept
665 // Purpose : This method should be called from dialog's slots onOk() and onApply()
666 // It perfroms user input validation, then it
667 // performs a proper operation and manages transactions, etc.
668 //================================================================
669 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
671 QAD_Study* aDoc = QAD_Application::getDesktop()->getActiveStudy();
672 SALOMEDS::Study_var aStudy = aDoc->getStudyDocument();
674 bool aLocked = aStudy->GetProperties()->IsLocked();
676 MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
677 QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
678 QObject::tr("WRN_WARNING"),
679 QObject::tr("WRN_STUDY_LOCKED"),
680 QObject::tr("BUT_OK") );
685 if ( !isValid( msg ) ) {
690 erasePreview( false );
693 if ( ( !publish && !useTransaction ) || openCommand() ) {
695 QAD_Application::getDesktop()->putInfo( "" );
697 if ( !execute( objects ) || !getOperation()->IsDone() ) {
703 const int nbObjs = objects.size();
704 bool withChildren = false;
705 for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
709 aName = strlen( getNewObjectName() ) ? GEOMBase::GetDefaultName( getNewObjectName() ) : GEOMBase::GetDefaultName( getPrefix( *it ) );
711 aName = getNewObjectName();
712 // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
713 if ( aName.isEmpty() )
714 aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
716 addInStudy( *it, aName.latin1() );
717 withChildren = false;
718 display( *it, false );
720 else { // asv : fix of PAL6454. If publish==false, then the original shape was modified, and need to be re-cached in GEOM_Client
722 clearShapeBuffer( *it );
724 redisplay( *it, withChildren, false );
731 QAD_Application::getDesktop()->putInfo( QObject::tr("GEOM_PRP_DONE") );
738 catch( const SALOME::SALOME_Exception& e ) {
739 QtCatchCorbaException( e );
749 //================================================================
750 // Function : showError
751 // Purpose : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
752 //================================================================
753 void GEOMBase_Helper::showError()
756 if ( !getOperation()->_is_nil() )
757 msg = QObject::tr( getOperation()->GetErrorCode() );
760 msg = QObject::tr( "GEOM_PRP_ABORT" );
762 QAD_MessageBox::error1( QAD_Application::getDesktop(),
763 QObject::tr( "GEOM_ERROR_STATUS" ),
765 QObject::tr( "BUT_OK" ) );
768 //================================================================
769 // Function : showError
770 // Purpose : Shows a error message followed by <msg>
771 //================================================================
772 void GEOMBase_Helper::showError( const QString& msg )
774 QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
775 if ( !msg.isEmpty() )
777 QAD_MessageBox::error1( QAD_Application::getDesktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
780 //////////////////////////////////////////////////////////////////
781 // Virtual methods to be redefined in dialogs
782 //////////////////////////////////////////////////////////////////
784 //================================================================
785 // Function : createOperation
786 // Purpose : Redefine this method to return proper IOperation reference
787 //================================================================
788 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
790 GEOM::GEOM_IOperations_var aNilOp;
791 return aNilOp._retn();
794 //================================================================
795 // Function : isValid
796 // Purpose : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
797 //================================================================
798 bool GEOMBase_Helper::isValid( QString& )
803 //================================================================
804 // Function : execute
805 // Purpose : This method is called by onAccept().
806 // It should perform the required operation and put all new or modified objects into
807 // <objects> argument.Should return <false> if some error occurs during its execution.
808 //================================================================
809 bool GEOMBase_Helper::execute( ObjectList& objects )
814 //================================================================
815 // Function : getFather
816 // Purpose : This method is called by addInStudy(). It should return a father object
817 // for <theObj> or a nil reference if <theObj> should be published
818 // as a top-level object.
819 //================================================================
820 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
822 return GEOM::GEOM_Object::_nil();
825 //================================================================
826 // Function : getNewObjectName
827 // Purpose : Redefine this method to return proper name for a new object
828 //================================================================
829 const char* GEOMBase_Helper::getNewObjectName() const
834 //================================================================
835 // Function : getPrefix
836 // Purpose : Get prefix for name of created object
837 //================================================================
838 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
840 if ( !myPrefix.isEmpty() || theObj->_is_nil() )
844 if ( !GEOMBase::GetShape( theObj, aShape ) )
847 long aType = aShape.ShapeType();
851 case TopAbs_VERTEX : return QObject::tr( "GEOM_VERTEX" );
852 case TopAbs_EDGE : return QObject::tr( "GEOM_EDGE" );
853 case TopAbs_WIRE : return QObject::tr( "GEOM_WIRE" );
854 case TopAbs_FACE : return QObject::tr( "GEOM_FACE" );
855 case TopAbs_SHELL : return QObject::tr( "GEOM_SHELL" );
856 case TopAbs_SOLID : return QObject::tr( "GEOM_SOLID" );
857 case TopAbs_COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
858 case TopAbs_COMPOUND : return QObject::tr( "GEOM_COMPOUND" );