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