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