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