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