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