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