Salome HOME
Merging with WPdev
[modules/geom.git] / src / GEOMBase / GEOMBase_Helper.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : GEOMBase_Helper.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : GEOM
27 //  $Header$
28
29 #include <SUIT_ViewModel.h>
30
31 #include "GEOMBase_Helper.h"
32 #include "GEOMBase.h"
33 #include "GEOM_Operation.h"
34 #include "GeometryGUI.h"
35 #include "GEOM_Displayer.h"
36 #include "GEOMImpl_Types.hxx"
37
38 #include <SUIT_Session.h>
39 #include <SUIT_ViewWindow.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_OverrideCursor.h>
42
43 #include <SalomeApp_Module.h>
44 #include <SalomeApp_Application.h>
45 #include <SalomeApp_Study.h>
46 #include <LightApp_SelectionMgr.h>
47 #include <SalomeApp_Tools.h>
48 #include <SalomeApp_DataModel.h>
49 #include <SalomeApp_Module.h>
50
51 #include <OCCViewer_ViewModel.h>
52 #include <SVTK_ViewModel.h>
53
54 #include <OB_Browser.h>
55
56 #include <TColStd_MapOfInteger.hxx>
57 #include <TCollection_AsciiString.hxx>
58
59 using namespace std;
60
61 #include <SALOMEDSClient.hxx>
62
63
64 //================================================================
65 // Function : getActiveView
66 // Purpose  : Get active view window, returns 0 if no open study frame
67 //================================================================
68 static SUIT_ViewWindow* getActiveView()
69 {
70   SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
71   if ( activeStudy )
72     return SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
73
74   return 0;
75 }
76
77
78 //================================================================
79 // Function : getGeomEngine
80 // Purpose  : Static method
81 //================================================================
82 GEOM::GEOM_Gen_ptr GEOMBase_Helper::getGeomEngine()
83 {
84   return GeometryGUI::GetGeomGen();
85 }
86
87 //================================================================
88 // Function : GEOMBase_Helper
89 // Purpose  :
90 //================================================================
91 GEOMBase_Helper::GEOMBase_Helper( SUIT_Desktop* desktop )
92   : myDesktop( desktop ), myViewWindow( 0 ), myDisplayer( 0 ), myCommand( 0 ), isPreview( false )
93 {
94 }
95
96 //================================================================
97 // Function : ~GEOMBase_Helper
98 // Purpose  :
99 //================================================================
100 GEOMBase_Helper::~GEOMBase_Helper()
101 {
102   if ( !SUIT_Session::session()->activeApplication()->desktop() )
103     return;
104
105   if ( myPreview.size() )
106     erasePreview();
107   if ( hasCommand() )
108     abortCommand();
109
110   globalSelection( GEOM_ALLOBJECTS, true );
111
112   if (myDisplayer)
113     delete myDisplayer;
114 }
115
116 //================================================================
117 // Function : display
118 // Purpose  :
119 //================================================================
120 void GEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
121 {
122   ObjectList::const_iterator it;
123   for ( it = objList.begin(); it != objList.end(); it++ ) {
124     display( *it, false );
125   }
126   if ( !objList.empty() && updateView )
127     getDisplayer()->UpdateViewer();
128 }
129
130 //================================================================
131 // Function  : display
132 // Purpose   : Display object.
133 // Important : Object must be already in study
134 //================================================================
135 void GEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
136 {
137   // Unset color of shape ( this color may be set during preview displaying )
138   // Default color will be used
139   getDisplayer()->UnsetColor();
140   getDisplayer()->UnsetWidth();
141
142   // Enable activisation of selection
143   getDisplayer()->SetToActivate( true );
144
145   // Display object
146   getDisplayer()->Display( object, updateView );
147 }
148
149 //================================================================
150 // Function : erase
151 // Purpose  :
152 //================================================================
153 void GEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
154 {
155   ObjectList::const_iterator it = objList.begin();
156   for ( ; it != objList.end(); it++ ) {
157     erase( *it, false );
158   }
159   if ( !objList.empty() && updateView )
160     getDisplayer()->UpdateViewer();
161 }
162
163 //================================================================
164 // Function : erase
165 // Purpose  :
166 //================================================================
167 void GEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
168 {
169   if ( !object->_is_nil() ) {
170     string entry = getEntry( object );
171     getDisplayer()->Erase( new SALOME_InteractiveObject(
172       entry.c_str(), "GEOM", strdup( GEOMBase::GetName( object ) ) ), true, updateView );
173   }
174 }
175
176 //================================================================
177 // Function : redisplay
178 // Purpose  :
179 //================================================================
180 void GEOMBase_Helper::redisplay( const ObjectList& objList,
181                                  const bool withChildren,
182                                  const bool updateView )
183 {
184   ObjectList::const_iterator it = objList.begin();
185   for ( ; it != objList.end(); it++ ) {
186     redisplay( *it, withChildren, false );
187   }
188   if ( !objList.empty() && updateView )
189     getDisplayer()->UpdateViewer();
190 }
191
192 //================================================================
193 // Function : redisplay
194 // Purpose  :
195 //================================================================
196 void GEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
197                                  const bool withChildren,
198                                  const bool updateView )
199 {
200   if ( !object->_is_nil() ) {
201     // Unset color of shape ( this color may be set during preview displaying )
202     // Default color will be used
203     getDisplayer()->UnsetColor();
204     getDisplayer()->UnsetWidth();
205
206     // Enable activisation of selection
207     getDisplayer()->SetToActivate( true );
208
209     string entry = getEntry( object );
210     getDisplayer()->Redisplay(new SALOME_InteractiveObject
211                               (entry.c_str(), "GEOM", strdup(GEOMBase::GetName(object))), false);
212   }
213
214   if ( withChildren ) {
215     SalomeApp_Study* aDoc = getStudy();
216     if ( aDoc && aDoc->studyDS() ) {
217       _PTR(Study) aStudy = aDoc->studyDS();
218       CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(object);
219       _PTR(SObject) aSObj (aStudy->FindObjectIOR(string(objStr.in())));
220       if ( aSObj  ) {
221         _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
222         for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
223           GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow
224             (GeometryGUI::ClientSObjectToObject(anIt->Value()));
225           if ( !CORBA::is_nil( aChild ) ) {
226             if ( !aChild->_is_nil() ) {
227               string entry = getEntry( aChild );
228               getDisplayer()->Redisplay( new SALOME_InteractiveObject(
229                 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( aChild ) ) ), false );
230             }
231           }
232         }
233       }
234     }
235   }
236
237   if ( updateView )
238     getDisplayer()->UpdateViewer();
239 }
240
241 //================================================================
242 // Function : displayPreview
243 // Purpose  : Method for displaying preview based on execute() results
244 //================================================================
245 void GEOMBase_Helper::displayPreview( const bool   activate,
246                                       const bool   update,
247                                       const bool   toRemoveFromEngine,
248                                       const double lineWidth )
249 {
250   isPreview = true;
251   QString msg;
252   if ( !isValid( msg ) )
253   {
254     erasePreview( update );
255     isPreview = false;
256     return;
257   }
258
259   erasePreview( false );
260
261   try {
262     SUIT_OverrideCursor wc;
263     ObjectList objects;
264     if ( !execute( objects ) || !getOperation()->IsDone() ) {
265       wc.suspend();
266     }
267     else {
268       for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
269       {
270         displayPreview( *it, true, activate, false, lineWidth );
271         if ( toRemoveFromEngine )
272           getGeomEngine()->RemoveObject( *it );
273       }
274     }
275   }
276   catch( const SALOME::SALOME_Exception& e ) {
277     SalomeApp_Tools::QtCatchCorbaException( e );
278   }
279
280   isPreview = false;
281
282   if ( update )
283     updateViewer();
284 }
285
286 //================================================================
287 // Function : displayPreview
288 // Purpose  : Method for displaying preview of resulting shape
289 //================================================================
290 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
291                                       const bool            append,
292                                       const bool            activate,
293                                       const bool            update,
294                                       const double          lineWidth )
295 {
296   // Set color for preview shape
297   getDisplayer()->SetColor( Quantity_NOC_VIOLET );
298
299   // set width of displayed shape
300   getDisplayer()->SetWidth( lineWidth );
301
302   // Disable activation of selection
303   getDisplayer()->SetToActivate( activate );
304
305   // Make a reference to GEOM_Object
306   CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string( object );
307   getDisplayer()->SetName( objStr.in() );
308
309   // Build prs
310   SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
311   if ( aPrs == 0 || aPrs->IsNull() )
312     return;
313
314   // Display prs
315   displayPreview( aPrs, append, update );
316
317   getDisplayer()->UnsetName();
318
319   // Enable activation of displayed objects
320   getDisplayer()->SetToActivate( true );
321 }
322
323 //================================================================
324 // Function : displayPreview
325 // Purpose  : Method for displaying arbitrary preview objects (not limited to shapes)
326 //================================================================
327 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
328                                       const bool        append,
329                                       const bool        update )
330 {
331   if ( !append )
332     erasePreview( false );
333
334   // remember current view frame to make correct erase preview later
335   myViewWindow = getActiveView();
336
337   if ( myViewWindow == 0 )
338     return;
339
340   // Display prs
341   SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
342   if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
343        aViewManager->getType() == SVTK_Viewer::Type() )
344     {
345       SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
346       SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
347       if (aView)
348         aView->Display( prs );
349     }
350
351   // Add prs to the preview list
352   myPreview.push_back( (SALOME_Prs*)prs );
353
354   // Update viewer
355   if ( update )
356     getDisplayer()->UpdateViewer();
357 }
358
359 //================================================================
360 // Function : erasePreview
361 // Purpose  :
362 //================================================================
363 void GEOMBase_Helper::erasePreview( const bool update )
364 {
365   // check view frame where the preview was displayed
366   bool vfOK = checkViewWindow() && myViewWindow;
367   // Iterate through presentations and delete them
368   for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
369     if ( vfOK )
370       {
371          SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
372          if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
373               aViewManager->getType() == SVTK_Viewer::Type() )
374            {
375              SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
376              SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
377              if (aView)
378                aView->Erase( *anIter, true );
379            }
380       }
381     delete *anIter;
382   }
383   myPreview.clear();
384
385   // Update viewer
386   if ( update )
387     updateViewer();
388 }
389
390 //================================================================
391 // Function  : localSelection
392 // Purpose   : Activate selection of objects of a given type
393 // IMPORTANT : Works after localSelection( ... ) method call only
394 //================================================================
395 void GEOMBase_Helper::activate( const int theType )
396 {
397   if (!getStudy()) return;
398   _PTR(Study) aStudy = getStudy()->studyDS();
399   _PTR(SComponent) aGeom ( aStudy->FindComponent( "GEOM" ) );
400   if ( !aGeom )
401     return;
402
403   SALOME_ListIO aList;
404   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( aGeom ) );
405   for ( ; anIter->More(); anIter->Next() )
406   {
407     _PTR(SObject) aSO ( anIter->Value() );
408     if ( aSO )
409     {
410       _PTR(SObject) aRefSO;
411       if ( !aSO->ReferencedObject( aRefSO ) )
412       {
413         GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow
414           (GeometryGUI::ClientSObjectToObject(aSO));
415         if ( !anObj->_is_nil() && anObj->GetType() == theType )
416           aList.Append( new SALOME_InteractiveObject( aSO->GetID().c_str(), "GEOM", aSO->GetName().c_str()) );
417       }
418     }
419   }
420
421   getDisplayer()->LocalSelection( aList, 0 );
422 }
423
424 //================================================================
425 // Function : localSelection
426 // Purpose  : Activate selection of subshapes in accordance with mode
427 //            theMode is from TopAbs_ShapeEnum
428 //================================================================
429 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
430 {
431   SALOME_ListIO aListOfIO;
432
433   ObjectList::const_iterator anIter = theObjs.begin();
434   for ( ; anIter != theObjs.end(); ++anIter )
435   {
436     GEOM::GEOM_Object_ptr anObj = *anIter;
437     if ( anObj->_is_nil() )
438       continue;
439     string aEntry = getEntry( anObj );
440     if ( aEntry != "" )
441       aListOfIO.Append( new SALOME_InteractiveObject(
442         aEntry.c_str(), "GEOM", strdup( GEOMBase::GetName( anObj ) ) ) );
443   }
444
445   getDisplayer()->LocalSelection( aListOfIO, theMode );
446 }
447
448 //================================================================
449 // Function : localSelection
450 // Purpose  : Activate selection of subshapes in accordance with mode
451 //            theMode is from TopAbs_ShapeEnum
452 //================================================================
453 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
454 {
455   // If object is null local selection for all objects is activated
456   if ( obj->_is_nil() ) {
457     getDisplayer()->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
458     return;
459   }
460
461   ObjectList objList;
462   objList.push_back( obj );
463   localSelection( objList, mode );
464 }
465
466
467 //================================================================
468 // Function : globalSelection
469 // Purpose  : Activate selection of subshapes. Set selection filters
470 //            in accordance with mode. theMode is from GEOMImpl_Types
471 //================================================================
472 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
473 {
474   getDisplayer()->GlobalSelection( theMode, update );
475 }
476
477 //================================================================
478 // Function : globalSelection
479 // Purpose  : Activate selection of subshapes. Set selection filters
480 //            in accordance with mode. theMode is from GEOMImpl_Types
481 //================================================================
482 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
483                                        const bool update )
484 {
485   getDisplayer()->GlobalSelection( theModes, update );
486 }
487
488 //================================================================
489 // Function : addInStudy
490 // Purpose  : Add object in study
491 //================================================================
492 void GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
493 {
494   if ( !hasCommand() )
495     return;
496
497   _PTR(Study) aStudy = getStudy()->studyDS();
498   if ( !aStudy || theObj->_is_nil() )
499     return;
500
501   GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
502
503   getGeomEngine()->AddInStudy(GeometryGUI::ClientStudyToStudy(aStudy),
504                               theObj, theName, aFatherObj);
505 }
506
507 //================================================================
508 // Function : updateObjBrowser
509 // Purpose  : Update object browser
510 //================================================================
511 void GEOMBase_Helper::updateObjBrowser() const
512 {
513   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
514   if (app) {
515     CAM_Module* module = app->module( "Geometry" );
516     SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
517     if ( appMod ) {
518       appMod->updateObjBrowser( true );
519     }
520   }
521 }
522
523 //================================================================
524 // Function : updateViewer
525 // Purpose  : Update active 3D view
526 //================================================================
527 void GEOMBase_Helper::updateViewer()
528 {
529   getDisplayer()->UpdateViewer();
530 }
531
532 //================================================================
533 // Function : getStudyId
534 // Purpose  : Get study Id
535 //================================================================
536 int GEOMBase_Helper::getStudyId() const
537 {
538   int anId = -1;
539   if ( getStudy() )
540     anId = getStudy()->id();
541   return anId;
542 }
543
544 //================================================================
545 // Function : getStudy
546 // Purpose  : Returns the active study. It is recommended to use
547 //            this method instead of direct desktop->getActiveStudy() calls
548 //================================================================
549 SalomeApp_Study* GEOMBase_Helper::getStudy() const
550 {
551   SUIT_Desktop* aDesktop = getDesktop();
552   if (!aDesktop)
553     return 0;
554
555   QPtrList<SUIT_Application> anAppList = SUIT_Session::session()->applications();
556
557   SUIT_Application* anApp = 0;
558   for ( QPtrListIterator<SUIT_Application> it( anAppList ); it.current() ; ++it )
559     {
560       anApp = it.current();
561       if ( anApp->desktop() == aDesktop )
562         break;
563     }
564
565   return dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
566 }
567
568 //================================================================
569 // Function : getEntry
570 // Purpose  :
571 //================================================================
572 char* GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
573 {
574   SalomeApp_Study* study = getStudy();
575   if ( study )  {
576     char * objIOR = GEOMBase::GetIORFromObject( object );
577     string IOR( objIOR );
578     free( objIOR );
579     if ( IOR != "" ) {
580       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
581       if ( SO ) {
582               return TCollection_AsciiString((char*)SO->GetID().c_str()).ToCString();
583       }
584     }
585   }
586   return "";
587 }
588
589 //================================================================
590 // Function : getDisplayer
591 // Purpose  :
592 //================================================================
593 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
594 {
595   if ( !myDisplayer )
596     myDisplayer = new GEOM_Displayer( getStudy() );
597   return myDisplayer;
598 }
599
600 //================================================================
601 // Function : clearShapeBuffer
602 // Purpose  :
603 //================================================================
604 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
605 {
606   if ( CORBA::is_nil( theObj ) )
607     return;
608
609   CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( theObj );
610   TCollection_AsciiString asciiIOR( (char *)IOR.in() );
611   GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
612
613   if ( !getStudy() || !getStudy()->studyDS() )
614     return;
615
616   _PTR(Study) aStudy = getStudy()->studyDS();
617   _PTR(SObject) aSObj ( aStudy->FindObjectIOR( string( IOR ) ) );
618   if ( !aSObj )
619     return;
620
621   _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
622   for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
623     _PTR(GenericAttribute) anAttr;
624     if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
625       _PTR(AttributeIOR) anIOR ( anAttr );
626       TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
627       GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
628     }
629   }
630 }
631
632 //================================================================
633 // Function : openCommand
634 // Purpose  :
635 //================================================================
636 bool GEOMBase_Helper::openCommand()
637 {
638   bool res = false;
639   if ( !getStudy() || hasCommand() )
640     return res;
641
642   GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
643   if ( !anOp->_is_nil() ) {
644     myCommand = new GEOM_Operation( SUIT_Session::session()->activeApplication(), anOp.in() );
645     myCommand->start();
646     res = true;
647   }
648
649   return res;
650 }
651
652 //================================================================
653 // Function : abortCommand
654 // Purpose  :
655 //================================================================
656 bool GEOMBase_Helper::abortCommand()
657 {
658   if ( !hasCommand() )
659     return false;
660
661   myCommand->abort();
662   myCommand = 0;
663
664   return true;
665 }
666
667 //================================================================
668 // Function : commitCommand
669 // Purpose  :
670 //================================================================
671 bool GEOMBase_Helper::commitCommand( const char* )
672 {
673   if ( !hasCommand() )
674     return false;
675
676   myCommand->commit();
677   myCommand = 0;
678
679   return true;
680 }
681
682 //================================================================
683 // Function : hasCommand
684 // Purpose  :
685 //================================================================
686 bool GEOMBase_Helper::hasCommand() const
687 {
688   return (bool)myCommand;
689 }
690
691 //================================================================
692 // Function : getOperation
693 // Purpose  :
694 //================================================================
695 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
696 {
697   if ( myOperation->_is_nil() )
698     myOperation = createOperation();
699
700   return myOperation;
701 }
702
703
704
705 //================================================================
706 // Function : checkViewWindow
707 // Purpose  :
708 //================================================================
709 bool GEOMBase_Helper::checkViewWindow()
710 {
711   if ( myViewWindow ){
712     QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
713     for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it )
714       {
715         if ( myViewWindow == it.current() )
716           return true;
717       }
718   }
719   myViewWindow = 0;
720   return false;
721 }
722
723 //================================================================
724 // Function : onAccept
725 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
726 //            It perfroms user input validation, then it
727 //            performs a proper operation and manages transactions, etc.
728 //================================================================
729 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
730 {
731   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
732   if ( !appStudy ) return false;
733   _PTR(Study) aStudy = appStudy->studyDS();
734
735   bool aLocked = (_PTR(AttributeStudyProperties) (aStudy->GetProperties()))->IsLocked();
736   if ( aLocked ) {
737     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
738     SUIT_MessageBox::warn1 ( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
739                            QObject::tr("WRN_WARNING"),
740                            QObject::tr("WRN_STUDY_LOCKED"),
741                            QObject::tr("BUT_OK") );
742     return false;
743   }
744
745   QString msg;
746   if ( !isValid( msg ) ) {
747     showError( msg );
748     return false;
749   }
750
751   erasePreview( false );
752
753   try {
754     if ( ( !publish && !useTransaction ) || openCommand() ) {
755       SUIT_OverrideCursor wc;
756       SUIT_Session::session()->activeApplication()->putInfo( "" );
757       ObjectList objects;
758       if ( !execute( objects ) || !getOperation()->IsDone() ) {
759         wc.suspend();
760         abortCommand();
761         showError();
762       }
763       else {
764         const int nbObjs = objects.size();
765         bool withChildren = false;
766         int aNumber = 1;
767         for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
768           if ( publish ) {
769             QString aName = getNewObjectName();
770             if ( nbObjs > 1 ) {
771               if (aName.isEmpty())
772                 aName = getPrefix(*it);
773               if (nbObjs <= 30) {
774                 // Try to find a unique name
775                 aName = GEOMBase::GetDefaultName(aName);
776               } else {
777                 // Don't check name uniqueness in case of numerous objects
778                 aName = aName + "_" + QString::number(aNumber++);
779               }
780             } else {
781               // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
782               if ( aName.isEmpty() )
783                 aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
784             }
785             addInStudy( *it, aName.latin1() );
786             withChildren = false;
787             display( *it, false );
788           }
789           else { // asv : fix of PAL6454. If publish==false, then the original shape was modified, and need to be re-cached in GEOM_Client
790                  // before redisplay
791             clearShapeBuffer( *it );
792             withChildren = true;
793             redisplay( *it, withChildren, false );
794           }
795         }
796
797         if ( nbObjs ) {
798           commitCommand();
799           updateObjBrowser();
800           SUIT_Session::session()->activeApplication()->putInfo( QObject::tr("GEOM_PRP_DONE") );
801         }
802         else
803           abortCommand();
804       }
805     }
806   }
807   catch( const SALOME::SALOME_Exception& e ) {
808     SalomeApp_Tools::QtCatchCorbaException( e );
809     abortCommand();
810   }
811
812   updateViewer();
813
814   return true;
815 }
816
817
818 //================================================================
819 // Function : showError
820 // Purpose  : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
821 //================================================================
822 void GEOMBase_Helper::showError()
823 {
824   QString msg;
825   if ( !getOperation()->_is_nil() )
826     msg = QObject::tr( getOperation()->GetErrorCode() );
827
828   if ( msg.isEmpty() )
829     msg = QObject::tr( "GEOM_PRP_ABORT" );
830
831   SUIT_MessageBox::error1( SUIT_Session::session()->activeApplication()->desktop(),
832                            QObject::tr( "GEOM_ERROR_STATUS" ),
833                            msg,
834                            QObject::tr( "BUT_OK" ) );
835 }
836
837 //================================================================
838 // Function : showError
839 // Purpose  : Shows a error message followed by <msg>
840 //================================================================
841 void GEOMBase_Helper::showError( const QString& msg )
842 {
843   QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
844   if ( !msg.isEmpty() )
845     str += "\n" + msg;
846   SUIT_MessageBox::error1(SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
847 }
848
849 //////////////////////////////////////////////////////////////////
850 // Virtual methods to be redefined in dialogs
851 //////////////////////////////////////////////////////////////////
852
853 //================================================================
854 // Function : createOperation
855 // Purpose  : Redefine this method to return proper IOperation reference
856 //================================================================
857 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
858 {
859   GEOM::GEOM_IOperations_var aNilOp;
860   return aNilOp._retn();
861 }
862
863 //================================================================
864 // Function : isValid
865 // Purpose  : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
866 //================================================================
867 bool GEOMBase_Helper::isValid( QString& )
868 {
869   return true;
870 }
871
872 //================================================================
873 // Function : execute
874 // Purpose  : This method is called by onAccept().
875 //            It should perform the required operation and put all new or modified objects into
876 //            <objects> argument.Should return <false> if some error occurs during its execution.
877 //================================================================
878 bool GEOMBase_Helper::execute( ObjectList& objects )
879 {
880   return false;
881 }
882
883 //================================================================
884 // Function : getFather
885 // Purpose  : This method is called by addInStudy(). It should return a father object
886 //            for <theObj> or a nil reference if <theObj> should be published
887 //            as a top-level object.
888 //================================================================
889 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
890 {
891   return GEOM::GEOM_Object::_nil();
892 }
893
894 //================================================================
895 // Function : getNewObjectName
896 // Purpose  : Redefine this method to return proper name for a new object
897 //================================================================
898 const char* GEOMBase_Helper::getNewObjectName() const
899 {
900   return "";
901 }
902
903 //================================================================
904 // Function : getPrefix
905 // Purpose  : Get prefix for name of created object
906 //================================================================
907 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
908 {
909   if ( !myPrefix.isEmpty() || theObj->_is_nil() )
910     return myPrefix;
911
912   TopoDS_Shape aShape;
913   if ( !GEOMBase::GetShape( theObj, aShape ) )
914     return "";
915
916   long aType = aShape.ShapeType();
917
918   switch ( aType )
919   {
920     case TopAbs_VERTEX   : return QObject::tr( "GEOM_VERTEX" );
921     case TopAbs_EDGE     : return QObject::tr( "GEOM_EDGE" );
922     case TopAbs_WIRE     : return QObject::tr( "GEOM_WIRE" );
923     case TopAbs_FACE     : return QObject::tr( "GEOM_FACE" );
924     case TopAbs_SHELL    : return QObject::tr( "GEOM_SHELL" );
925     case TopAbs_SOLID    : return QObject::tr( "GEOM_SOLID" );
926     case TopAbs_COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
927     case TopAbs_COMPOUND : return QObject::tr( "GEOM_COMPOUND" );
928     default : return "";
929   }
930 }
931
932 //================================================================
933 // Function : selectedIO
934 // Purpose  : Return the list of selected SALOME_InteractiveObject's
935 //================================================================
936 const SALOME_ListIO& GEOMBase_Helper::selectedIO()
937 {
938   mySelected.Clear();
939
940   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
941   if ( app ) {
942     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
943     if ( aSelMgr )
944       aSelMgr->selectedObjects( mySelected );
945   }
946
947   return mySelected;
948 }
949
950 //================================================================
951 // Function : IObjectCount
952 // Purpose  : Return the number of selected objects
953 //================================================================
954 int GEOMBase_Helper::IObjectCount()
955 {
956   return selectedIO().Extent();
957 }
958
959 //================================================================
960 // Function : firstIObject
961 // Purpose  :  Return the first selected object in the selected object list
962 //================================================================
963 Handle(SALOME_InteractiveObject) GEOMBase_Helper::firstIObject()
964 {
965   const SALOME_ListIO& aList = selectedIO();
966   return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
967 }
968
969 //================================================================
970 // Function : lastIObject
971 // Purpose  : Return the last selected object in the selected object list
972 //================================================================
973 Handle(SALOME_InteractiveObject) GEOMBase_Helper::lastIObject()
974 {
975   const SALOME_ListIO& aList = selectedIO();
976   return aList.Extent() > 0 ? aList.Last() : Handle(SALOME_InteractiveObject)();
977 }
978
979 //================================================================
980 // Function : getDesktop
981 // Purpose  : Returns myDesktop field.  Initialized in constructor, usually as dynamic_cast<SUIT_Desktop*>(parentWidget())
982 //================================================================
983 SUIT_Desktop* GEOMBase_Helper::getDesktop() const
984 {
985   return myDesktop;
986 }
987