Salome HOME
a977323fc3e5b4b3445bec24033f3d119d2154f4
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GEOMBase_Tools.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header: 
28
29 using namespace std;
30 #include "GEOMToolsGUI.h"
31
32 #include "QAD_Config.h"
33 #include "QAD_FileDlg.h"
34 #include "QAD_Tools.h"
35 #include "QAD_MessageBox.h"
36 #include "QAD_RightFrame.h"
37
38 #include "OCCViewer_Viewer3d.h"
39 #include "VTKViewer_ViewFrame.h"
40
41 #include "SALOME_ListIteratorOfListIO.hxx"
42 #include "SALOMEGUI_NameDlg.h"
43 #include "SALOMEGUI_ImportOperation.h"
44 #include "SALOMEGUI_QtCatchCorbaException.hxx"
45
46 #include <AIS_Drawer.hxx>
47 #include <AIS_ListIteratorOfListOfInteractive.hxx>
48 #include <Prs3d_IsoAspect.hxx>
49
50 #include <vtkBMPReader.h>
51 #include <vtkTexture.h>
52 #include <vtkTextureMapToPlane.h>
53 #include <vtkTransformTextureCoords.h>
54 #include <vtkDataSetMapper.h>
55
56 #include <qfileinfo.h>
57 #include <qcolordialog.h>
58 #include <qspinbox.h>
59
60 #include "GEOMToolsGUI_NbIsosDlg.h"        // Method ISOS adjustement
61 #include "GEOMToolsGUI_TransparencyDlg.h"  // Method TRANSPARENCY adjustement
62
63 //=======================================================================
64 // function : GEOMToolsGUI()
65 // purpose  : Constructor
66 //=======================================================================
67 GEOMToolsGUI::GEOMToolsGUI() :
68   QObject()
69 {
70   myGeomBase = new GEOMBase();
71   myGeomGUI = GEOMContext::GetGeomGUI();
72   myGeom = myGeomGUI->myComponentGeom;
73 }
74
75
76 //=======================================================================
77 // function : ~GEOMToolsGUI()
78 // purpose  : Destructor
79 //=======================================================================
80 GEOMToolsGUI::~GEOMToolsGUI()
81 {
82 }
83
84
85 //=======================================================================
86 // function : OnGUIEvent()
87 // purpose  : 
88 //=======================================================================
89 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, QAD_Desktop* parent)
90 {
91   GEOMToolsGUI* myGEOMToolsGUI = new GEOMToolsGUI();
92   myGEOMToolsGUI->myGeomGUI->EmitSignalDeactivateDialog();
93   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
94
95   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
96   switch (theCommandID)
97     {
98     case 31: // COPY
99       {
100         myGEOMToolsGUI->OnEditCopy();
101         break;
102       }
103     case 33: // DELETE
104       {
105         myGEOMToolsGUI->OnEditDelete();
106         break;
107       }
108     case 111: // IMPORT BREP
109       {
110         myGEOMToolsGUI->Import(theCommandID);
111         break;
112       }
113     case 112: // IMPORT IGES
114       {
115         myGEOMToolsGUI->Import(theCommandID);
116         break;
117       }
118     case 113: // IMPORT STEP
119       {
120         myGEOMToolsGUI->Import(theCommandID);
121         break;
122       }
123     case 121: // EXPORT BREP
124       {
125         myGEOMToolsGUI->Export(theCommandID);
126         break;
127       }
128     case 122: // EXPORT IGES
129       {
130         myGEOMToolsGUI->Export(theCommandID);
131         break;
132       }
133     case 123: // EXPORT STEP
134       {
135         myGEOMToolsGUI->Export(theCommandID);
136         break;
137       }
138     case 411: // SETTINGS - ADD IN STUDY
139       {
140         QMenuBar* Mb = QAD_Application::getDesktop()->getMainMenuBar();
141         QMenuData* pp;
142         QMenuItem* item = parent->menuBar()->findItem(411, &pp);
143         bool check = !pp->isItemChecked(411);
144         pp->setItemChecked(411,check);
145         //myGEOMToolsGUI->myGeomGUI->GetSettings_AddInStudy() = check;
146         QAD_CONFIG->addSetting("Geometry:SettingsAddInStudy", check);
147         break;
148       }
149     case 412: // SETTINGS - SHADING COLOR
150       {
151         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
152           break;
153
154         OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
155         Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
156
157         QString SCr = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorRed");
158         QString SCg = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorGreen");
159         QString SCb = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorBlue");
160         QColor color;
161         if(!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty()) {          
162           color = QColor (SCr.toInt(), SCg.toInt(), SCb.toInt());
163         } 
164         else {
165           Quantity_Color Default = Quantity_Color();
166           color = QColor ((int)Default.Red()  * 255.0, (int)Default.Green()* 255.0, (int)Default.Blue() * 255.0);
167         }
168         
169         QColor c = QColorDialog::getColor(color, QAD_Application::getDesktop());
170         if(c.isValid()) {
171           //myGEOMToolsGUI->myGeomGUI->GetShadingColor() = Quantity_Color(c.red() / 255.0, c.green() / 255.0, c.blue() / 255.0, Quantity_TOC_RGB);
172           
173           AIS_ListOfInteractive List;
174           ic->DisplayedObjects(List);
175           AIS_ListOfInteractive List1;
176           ic->ObjectsInCollector(List1);
177           List.Append(List1);
178           
179           AIS_ListIteratorOfListOfInteractive ite(List);
180           while(ite.More()) {
181             if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
182               Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
183               aSh->SetShadingColor(Quantity_Color(c.red() / 255.0, c.green() / 255.0, c.blue() / 255.0, Quantity_TOC_RGB));
184               ic->Redisplay(aSh, Standard_True, Standard_True);
185             }
186             ite.Next();
187           }
188           
189           ic->UpdateCurrentViewer();
190                 
191           QAD_CONFIG->addSetting("Geometry:SettingsShadingColorRed", c.red());
192           QAD_CONFIG->addSetting("Geometry:SettingsShadingColorGreen", c.green());
193           QAD_CONFIG->addSetting("Geometry:SettingsShadingColorBlue", c.blue());        
194         }
195         break;
196       }
197     case 413: // SETTINGS - ISOS
198       {
199         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
200           break;
201
202         OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
203         Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
204
205         QString IsoU = QAD_CONFIG->getSetting("Geometry:SettingsIsoU");
206         QString IsoV = QAD_CONFIG->getSetting("Geometry:SettingsIsoV");
207         if(!IsoU.isEmpty())
208           ic->DefaultDrawer()->UIsoAspect()->SetNumber(IsoU.toInt());
209         else
210           IsoU = "1";
211         if (!IsoV.isEmpty())
212           ic->DefaultDrawer()->VIsoAspect()->SetNumber(IsoV.toInt());
213         else
214           IsoV = "1";
215         
216         GEOMBase_NbIsosDlg* NbIsosDlg = new GEOMBase_NbIsosDlg(QAD_Application::getDesktop(), tr("GEOM_MEN_ISOS"), TRUE);       
217         int UIso = IsoU.toInt();
218         int VIso = IsoV.toInt();
219         
220         NbIsosDlg->SpinBoxU->setValue(UIso);
221         NbIsosDlg->SpinBoxV->setValue(VIso);
222
223         if(NbIsosDlg->exec()) {
224           UIso = NbIsosDlg->SpinBoxU->text().toInt();
225           VIso = NbIsosDlg->SpinBoxV->text().toInt();
226           
227           ic->DefaultDrawer()->UIsoAspect()->SetNumber(UIso);
228           ic->DefaultDrawer()->VIsoAspect()->SetNumber(VIso);
229           QAD_CONFIG->addSetting("Geometry:SettingsIsoU", NbIsosDlg->SpinBoxU->text()); /* text format */
230           QAD_CONFIG->addSetting("Geometry:SettingsIsoV", NbIsosDlg->SpinBoxV->text()); /* text format */
231         }
232
233         AIS_ListOfInteractive List;
234         ic->DisplayedObjects(List);
235         AIS_ListOfInteractive List1;
236         ic->ObjectsInCollector(List1);
237         List.Append(List1);
238         
239         AIS_ListIteratorOfListOfInteractive ite(List);
240         ic->InitCurrent();
241         if(ic->NbCurrents())
242           while(ite.More()) {
243             if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
244               Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
245               Handle (AIS_Drawer) CurDrawer;
246               CurDrawer = aSh->Attributes();
247               CurDrawer->UIsoAspect()->SetNumber(UIso);
248               CurDrawer->VIsoAspect()->SetNumber(VIso);
249               ic->SetLocalAttributes(aSh, CurDrawer);
250               ic->Redisplay(aSh);       
251             }
252             ite.Next();
253           }
254         ic->UpdateCurrentViewer();
255         break;
256       }
257     case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
258       {
259         QString step = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
260         if(step.isEmpty())
261           step = "100.0";
262
263         Standard_Boolean res = false;
264         double dd = myGEOMToolsGUI->myGeomBase->Parameter(res, QString("%1").arg(step), tr("GEOM_MEN_STEP_LABEL"), tr("GEOM_STEP_TITLE"), 0.001, 10000.0, 3);
265         if(res) {
266           QAD_CONFIG->addSetting("Geometry:SettingsGeomStep", QString("%1").arg(dd));
267
268           /* Emit signal to GeometryGUI_SpinBoxes */
269           myGEOMToolsGUI->myGeomGUI->EmitSignalDefaultStepValueChanged(dd);
270         }
271         else
272           parent->putInfo(tr("GEOM_PRP_ABORT"));
273         break;
274       }
275     case 804: // ADD IN STUDY - POPUP VIEWER
276       {
277         const SALOME_ListIO& ListSelected = Sel->StoredIObjects();
278         SALOME_ListIteratorOfListIO It(ListSelected);
279         for(; It.More(); It.Next()) {
280           Handle(SALOME_InteractiveObject) IObject = It.Value();
281           myGEOMToolsGUI->myGeomBase->AddInStudy(true, IObject);
282         }
283         /* Is set on the dialog box */
284         QApplication::restoreOverrideCursor();
285         break;
286       }
287     case 901: // RENAME
288       {
289         SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
290         for(; It.More(); It.Next()) {
291           Handle(SALOME_InteractiveObject) IObject = It.Value();
292
293           SALOMEDS::SObject_var obj = aStudy->FindObjectID(IObject->getEntry());
294           SALOMEDS::GenericAttribute_var anAttr;
295           SALOMEDS::AttributeName_var aName;
296           if(!obj->_is_nil()) {
297             if(obj->FindAttribute(anAttr, "AttributeName")) {
298               aName = SALOMEDS::AttributeName::_narrow(anAttr);
299
300               QString nm = QString(aName->Value());
301               nm = SALOMEGUI_NameDlg::getName(QAD_Application::getDesktop(), nm);
302               if(!nm.isEmpty()) {
303                 QApplication::setOverrideCursor(Qt::waitCursor);
304                 QAD_Application::getDesktop()->getActiveStudy()->renameIObject(IObject, nm);
305                 QApplication::restoreOverrideCursor();
306               }
307             }
308           }
309         }
310         break;
311       }
312     case 5103: // CHECK GEOMETRY
313       {
314         QAD_PyEditor* PyEditor = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getPyEditor();
315         PyEditor->setText("from GEOM_usinggeom import *\n");
316         //PyEditor->setText(">>> ");
317         PyEditor->handleReturn();
318         break;
319       }
320     case 5104: // LOAD SCRIPT
321       {
322         QStringList filtersList;
323         filtersList.append(tr("GEOM_MEN_LOAD_SCRIPT"));
324         filtersList.append(tr("GEOM_MEN_ALL_FILES"));
325
326         QString aFile = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), "", filtersList, tr("GEOM_MEN_IMPORT"), true);
327         if(!aFile.isEmpty()) {
328           QFileInfo file = aFile;
329           QApplication::setOverrideCursor(Qt::waitCursor);
330           QAD_PyEditor* PyEditor = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getPyEditor();
331
332           PyEditor->setText("import geompy\n");
333           PyEditor->handleReturn();
334
335           QStringList aTextList = QStringList::split(".", file.fileName());
336           PyEditor->setText("geompy.Path('" + file.dirPath() + "')\n");
337           PyEditor->handleReturn();
338
339           PyEditor->setText("from " + aTextList.first() + " import *\n");
340           PyEditor->handleReturn();
341         }
342         QApplication::restoreOverrideCursor();
343         break;
344       }
345     case 8032: // COLOR - POPUP VIEWER
346       {
347         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
348           // VTK
349           VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
350           
351           SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
352           Handle(SALOME_InteractiveObject) FirstIOS =  Sel->firstIObject();
353           if(!FirstIOS.IsNull()) {
354             QColor initcolor = myRenderInter->GetColor(FirstIOS);
355             QColor c = QColorDialog::getColor( initcolor, QAD_Application::getDesktop());
356             
357             if(c.isValid()) {
358               QApplication::setOverrideCursor(Qt::waitCursor);
359               for(;It.More();It.Next()) {
360                 Handle(SALOME_InteractiveObject) IOS = It.Value();
361                 myRenderInter->SetColor(IOS,c);
362               }
363             }
364             QApplication::restoreOverrideCursor();
365           }
366         }
367         else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
368           Handle(SALOME_InteractiveObject) IO = Sel->firstIObject();
369           Standard_Boolean found;
370           Handle(GEOM_AISShape) Shape = myGEOMToolsGUI->myGeomBase->ConvertIOinGEOMAISShape(IO, found, true);
371           if(found) {
372             Quantity_Color CSFColor;
373             Shape->Color(CSFColor);
374             
375             QColor c = QColorDialog::getColor(QColor(CSFColor.Red()  * 255.0, CSFColor.Green()* 255.0, CSFColor.Blue() * 255.0), QAD_Application::getDesktop());
376             
377             if(c.isValid()) {
378               CSFColor = Quantity_Color (c.red()/255., c.green()/255., c.blue()/255., Quantity_TOC_RGB);
379               QApplication::setOverrideCursor( Qt::waitCursor );
380             
381               SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
382               for(;It.More();It.Next()) {
383                 Handle(SALOME_InteractiveObject) IObject = It.Value();
384                 Standard_Boolean found;
385                 Handle(GEOM_AISShape) Shape = myGEOMToolsGUI->myGeomBase->ConvertIOinGEOMAISShape(IObject, found, true);
386                 if(found) {
387                   Shape->SetColor(CSFColor);
388                   Shape->SetShadingColor(CSFColor);
389                 }
390               }
391             }
392           }
393         }
394         QApplication::restoreOverrideCursor();
395         break;
396       }
397     case 8033: // TRANSPARENCY - POPUP VIEWER
398       {
399         Handle(AIS_InteractiveContext) ic;
400         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
401           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
402           ic = v3d->getAISContext();
403         }
404         GEOMBase_TransparencyDlg *aDlg = new GEOMBase_TransparencyDlg(parent, "", Sel, ic);
405         break;
406       }
407     case 8034: // ISOS - POPUP VIEWER
408       {
409         if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
410           break;
411
412         OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
413         Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
414
415         ic->InitCurrent();
416         if(ic->MoreCurrent()) {
417           Handle(GEOM_AISShape) CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
418           QString IsoU = QAD_CONFIG->getSetting("Geometry:SettingsIsoU");
419           QString IsoV = QAD_CONFIG->getSetting("Geometry:SettingsIsoV");
420
421           if(!IsoU.isEmpty())
422             ic->DefaultDrawer()->UIsoAspect()->SetNumber(IsoU.toInt());
423           else
424             IsoU = "1";
425           if (!IsoV.isEmpty())
426             ic->DefaultDrawer()->VIsoAspect()->SetNumber(IsoV.toInt());
427           else
428             IsoV = "1";
429             
430           GEOMBase_NbIsosDlg * NbIsosDlg =
431             new GEOMBase_NbIsosDlg(QAD_Application::getDesktop(), tr("GEOM_MEN_ISOS"), TRUE);
432     
433           NbIsosDlg->SpinBoxU->setValue(IsoU.toInt());
434           NbIsosDlg->SpinBoxV->setValue(IsoV.toInt());
435
436           if(NbIsosDlg->exec()) {
437             QApplication::setOverrideCursor(Qt::waitCursor);
438             for(; ic->MoreCurrent(); ic->NextCurrent()) {
439               Handle(AIS_Drawer) CurDrawer;
440               
441               CurDrawer = ic->Current()->Attributes();
442               CurDrawer->UIsoAspect()->SetNumber(NbIsosDlg->SpinBoxU->text().toInt());
443               CurDrawer->VIsoAspect()->SetNumber(NbIsosDlg->SpinBoxV->text().toInt());
444                 
445               ic->SetLocalAttributes(CurObject, CurDrawer);
446               ic->Redisplay(CurObject);
447             }
448           }
449         }
450         QApplication::restoreOverrideCursor();
451         break;
452       }
453     case 9024 : // OPEN - OBJBROSER POPUP
454       {
455         SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
456         Handle(SALOME_InteractiveObject) anIObject;
457         for(;It.More();It.Next()) {
458           anIObject = It.Value();
459           SALOMEDS::SObject_var obj = aStudy->FindObjectID(anIObject->getEntry());
460           SALOMEDS::AttributePersistentRef_var aPersist;
461           SALOMEDS::AttributeIOR_var anIOR;
462           if(!obj->_is_nil()) {
463             // this SObject may be GEOM module root SObject
464             SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(obj);
465             SALOMEDS::GenericAttribute_var anAttr;
466             bool useSubItems = false;
467             while (anIter->More() && !useSubItems) {
468               SALOMEDS::SObject_var subobj = anIter->Value();
469               if (subobj->FindAttribute(anAttr, "AttributePersistentRef")) {
470                 useSubItems = true;
471                 obj = subobj;
472               }
473               else 
474                 anIter->Next();
475             }
476             obj->FindAttribute(anAttr, "AttributePersistentRef");
477               
478             while(useSubItems?anIter->More():!anAttr->_is_nil()) { 
479               if(!obj->FindAttribute(anAttr, "AttributeIOR") &&
480                   obj->FindAttribute(anAttr, "AttributePersistentRef")) {
481                 // load
482                 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer","GEOM");
483                 if (!CORBA::is_nil(comp)) {
484                   SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(comp);
485                   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
486                   aStudyBuilder->LoadWith(aStudy->FindComponent("GEOM"),driver);
487                 } 
488                 else {
489                   MESSAGE("Component is null");
490                 }
491               }
492               if(useSubItems) {
493                 anIter->Next();
494                 obj = anIter->Value();
495               } 
496               else 
497                 anAttr = SALOMEDS::GenericAttribute::_nil();
498             }
499           }
500         }
501         break;
502       }
503     default:
504       {
505         parent->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
506         break;
507       }
508     }
509   return true;
510 }
511
512
513 //===============================================================================
514 // function : OnEditDelete()
515 // purpose  :
516 //===============================================================================
517 void GEOMToolsGUI::OnEditDelete()
518 {
519   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
520   if(Sel->IObjectCount() == 0)
521     return;
522
523   if(QAD_MessageBox::warn2(QAD_Application::getDesktop(), tr("GEOM_WRN_WARNING"), tr("GEOM_REALLY_DELETE"), tr("GEOM_BUT_YES"), tr("GEOM_BUT_NO"), 1, 0, 0) != 1)
524     return;
525        
526   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
527     
528   Standard_Boolean found;
529   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
530   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
531   SALOMEDS::GenericAttribute_var anAttr;
532   SALOMEDS::AttributeIOR_var anIOR;
533   
534   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
535   QAD_Operation* op = new SALOMEGUI_ImportOperation( QAD_Application::getDesktop()->getActiveStudy() );
536   op->start();
537   Standard_Boolean deleted = false;
538   for(;It.More();It.Next()) {
539     Handle(SALOME_InteractiveObject) IObject = It.Value();
540     if(IObject->hasEntry()) {
541       SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
542       SALOMEDS::SObject_var SO = aStudy->FindObjectID(IObject->getEntry());
543       
544       /* Erase child graphical objects */
545       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(SO);
546       for(; it->More();it->Next()) {
547         SALOMEDS::SObject_var CSO= it->Value();
548         if(CSO->FindAttribute(anAttr, "AttributeIOR") ) {
549           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
550           /* Delete child(s) shape in Client : */
551           const TCollection_AsciiString ASCior(anIOR->Value()) ;
552           myGeomGUI->GetShapeReader().RemoveShapeFromBuffer(ASCior);
553
554           for(int i = 0; i < nbSf; i++) {
555             QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
556             if(sf->getTypeView() == VIEW_OCC) {
557               OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getViewer();
558               Handle (AIS_InteractiveContext) myContext = v3d->getAISContext();
559               Handle(GEOM_AISShape) Result = myGeomBase->ConvertIORinGEOMAISShape(anIOR->Value(), found);
560               if(found)
561                 myContext->Erase(Result, true, false);
562             }
563             else if(sf->getTypeView() == VIEW_VTK) {
564               //vtkRenderer* Renderer = ((VTKViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getRenderer();
565               VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getRWInteractor();
566               GEOM_Actor* ac = myGeomBase->ConvertIORinGEOMActor(anIOR->Value(), found);
567               if(found) {
568                 //Renderer->RemoveActor(ac);
569                 if(ac->hasIO()) 
570                   myRenderInter->Remove(ac->getIO());
571               }
572             }
573           }
574         }
575       }
576       
577       /* Erase main graphical object */
578       for(int i = 0; i < nbSf; i++) {
579         QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
580         if(sf->getTypeView() == VIEW_OCC) {
581           OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getViewer();
582           Handle(AIS_InteractiveContext) myContext = v3d->getAISContext();
583           Handle(GEOM_AISShape) Result = myGeomBase->ConvertIOinGEOMAISShape(IObject, found );
584           if(found)
585             myContext->Erase(Result, true, false);
586         }
587         else if(sf->getTypeView() == VIEW_VTK) {
588           VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getRWInteractor();
589           myRenderInter->Remove( IObject );
590         }
591       }
592       
593       /* Delete main shape in Client : */
594       if(SO->FindAttribute(anAttr, "AttributeIOR")) {
595         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
596         const TCollection_AsciiString ASCIor(anIOR->Value()) ;
597         myGeomGUI->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
598       }
599
600       /* Erase objects in Study */
601       SALOMEDS::SObject_var obj = aStudy->FindObjectID(IObject->getEntry());
602       if(!obj->_is_nil()) {
603         aStudyBuilder->RemoveObject(obj);
604         deleted = true;
605       }
606
607     } /* IObject->hasEntry() */
608   }   /* more/next           */
609
610   if (deleted) op->finish();
611   else op->abort();
612
613   /* Clear any previous selection */
614   Sel->ClearIObjects(); 
615   QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
616 }
617
618
619 //==============================================================================
620 // function : OnEditCopy()
621 // purpose  :
622 //==============================================================================
623 void GEOMToolsGUI::OnEditCopy()
624 {
625   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
626   GEOM::GEOM_Gen::ListOfIOR_var listIOR = new GEOM::GEOM_Gen::ListOfIOR;
627
628   const SALOME_ListIO& List = Sel->StoredIObjects();
629
630   myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
631
632   Sel->ClearIObjects();
633
634   for (unsigned int ind = 0; ind < listIOR->length();ind++) {
635     GEOM::GEOM_Shape_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
636     try {
637       GEOM::GEOM_Shape_var result = myGeom->MakeCopy(aShapeInit) ;
638       result->NameType( aShapeInit->NameType() );
639       myGeomBase->Display(result);
640     }
641     catch  (const SALOME::SALOME_Exception& S_ex) {
642       QtCatchCorbaException(S_ex);
643     }
644   }
645   
646   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
647 }
648
649
650 //=====================================================================================
651 // function : Import
652 // purpose  : BRep, Iges, Step
653 //=====================================================================================
654 bool GEOMToolsGUI::Import(int aState)
655 {
656   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
657   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
658   
659   GEOM::GEOM_Shape_var aShape;
660   QString file;
661   QStringList filtersList;
662   
663   switch(aState)
664     {
665     case 111 :  // Import BREP
666       {
667         filtersList.append(tr("GEOM_MEN_IMPORT_BREP"));
668         filtersList.append(tr("GEOM_MEN_ALL_FILES"));
669
670         file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), "", filtersList, tr("GEOM_MEN_IMPORT"), true);
671         if(!file.isEmpty()) {
672           QApplication::setOverrideCursor(Qt::waitCursor);
673           try {
674             aShape = myGeom->ImportBREP(file.latin1());
675           }
676           catch(const SALOME::SALOME_Exception& S_ex) {
677             QtCatchCorbaException(S_ex);
678           }
679         }
680         break;
681       }
682     case 112 :  // Import IGES
683       {
684         filtersList.append( tr("GEOM_MEN_IMPORT_IGES") ) ;
685         filtersList.append( tr("GEOM_MEN_ALL_FILES") ) ;
686
687         file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
688                                         "",
689                                         filtersList,
690                                         tr("GEOM_MEN_IMPORT"),
691                                         true);
692         if ( !file.isEmpty() ) {
693           QApplication::setOverrideCursor( Qt::waitCursor );
694           try {
695             aShape = myGeom->ImportIGES( file.latin1() );
696           }
697           catch (const SALOME::SALOME_Exception& S_ex) {
698             QtCatchCorbaException(S_ex);
699           }
700         }
701         break;
702       }
703     case 113 : // Import STEP
704       {
705         filtersList.append( tr("GEOM_MEN_IMPORT_STEP") ) ;
706         filtersList.append( tr("GEOM_MEN_ALL_FILES") ) ;
707
708         file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(),
709                                         "",
710                                         filtersList,
711                                         tr("GEOM_MEN_IMPORT"),
712                                         true);
713         if ( !file.isEmpty() ) {
714           QApplication::setOverrideCursor( Qt::waitCursor );
715           try {
716             aShape = myGeom->ImportSTEP( file.latin1() );
717           }
718           catch  (const SALOME::SALOME_Exception& S_ex) {
719             QtCatchCorbaException(S_ex);
720           }
721         }
722         break;
723       }
724     }
725   
726   if ( !file.isEmpty() ) {
727     QAD_Application::getDesktop()->putInfo( tr("GEOM_PRP_LOADING").arg(QAD_Tools::getFileNameFromPath( file )) );
728
729     SALOMEDS::SComponent_var father = aStudy->FindComponent("GEOM");
730     SALOMEDS::SObject_var fatherSF = aStudy->FindObjectID(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry());
731     SALOMEDS::GenericAttribute_var anAttr;
732     SALOMEDS::AttributeName_var    aName;
733     SALOMEDS::AttributePixMap_var  aPixmap;
734     int aLocked = false;
735     if (father->_is_nil()) {
736       QAD_Operation* op = new SALOMEGUI_ImportOperation(QAD_Application::getDesktop()->getActiveStudy() );
737       op->start();
738       aLocked = aStudy->GetProperties()->IsLocked();
739       if (aLocked) aStudy->GetProperties()->SetLocked(false);
740       father = aStudyBuilder->NewComponent("GEOM");
741       anAttr = aStudyBuilder->FindOrCreateAttribute(father, "AttributeName");
742       aName = SALOMEDS::AttributeName::_narrow(anAttr);
743       //      aName->SetValue( tr("GEOM_MEN_COMPONENT") );
744       aName->SetValue(QAD_Application::getDesktop()->getComponentUserName( "GEOM" ) );
745       anAttr = aStudyBuilder->FindOrCreateAttribute(father, "AttributePixMap");
746       aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
747       aPixmap->SetPixMap( "ICON_OBJBROWSER_Geometry" );
748       aStudyBuilder->DefineComponentInstance( father, myGeom );
749       if (aLocked) aStudy->GetProperties()->SetLocked(true);
750       op->finish();
751     }
752 //      if (aLocked) return false;
753     father->ComponentIOR(myGeomGUI->GetFatherior());
754
755     QString nameShape = QAD_Tools::getFileNameFromPath(file,false) +  QString("_%1").arg(myGeomGUI->GetNbGeom()++);
756
757     if(myGeomBase->Display(aShape, strdup(nameShape.latin1()))) {
758       QAD_Application::getDesktop()->getActiveStudy()->setMessage( tr("GEOM_INF_LOADED").arg(QAD_Tools::getFileNameFromPath( file )) );
759       QAD_Application::getDesktop()->putInfo( tr("GEOM_PRP_READY"));
760     }
761   }
762   QApplication::restoreOverrideCursor();
763   return true;
764 }
765
766
767 //=====================================================================================
768 // function : Export
769 // purpose  : BRep, Iges, Step
770 //=====================================================================================
771 bool GEOMToolsGUI::Export(int aState)
772 {
773   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
774   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
775
776   GEOM::GEOM_Shape_var aShape;
777
778   static QString filters[] = { tr("GEOM_MEN_IMPORT_BREP"),
779                                tr("GEOM_MEN_IMPORT_IGES"),
780                                tr("GEOM_MEN_IMPORT_STEP") };
781   
782   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
783   SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
784
785   switch (aState)
786     {
787     case 121 :
788       {
789         for(;It.More();It.Next()) {
790           QApplication::restoreOverrideCursor();
791           Handle(SALOME_InteractiveObject) IObject = It.Value();
792           Standard_Boolean found;
793           GEOM::GEOM_Shape_var aShape = myGeomBase->ConvertIOinGEOMShape(IObject, found);
794           //      Handle(GEOM_AISShape) Shape = myGeomGUI->ConvertIOinGEOMAISShape(IObject, found, true);
795           if ( found ) {
796             QString file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), 
797                                                     QString( IObject->getName() ) + ".brep",
798                                                     tr("GEOM_MEN_IMPORT_BREP"),
799                                                     tr("GEOM_MEN_EXPORT"),
800                                                     false);
801             if ( !file.isEmpty() && !aShape->_is_nil() ) {
802               QApplication::setOverrideCursor( Qt::waitCursor );
803               //      Standard_Boolean result = BRepTools::Write(Shape->Shape(), strdup(file.latin1()) );
804               try {
805                 myGeom->ExportBREP(strdup( file.latin1()), aShape);
806               }  
807               catch (const SALOME::SALOME_Exception& S_ex) {
808                 QtCatchCorbaException(S_ex);
809               }
810             }
811           }
812         }
813         break;
814       }
815     case 122 :
816       {
817         for(;It.More();It.Next()) {
818           QApplication::restoreOverrideCursor();
819           Handle(SALOME_InteractiveObject) IObject = It.Value();
820           Standard_Boolean found;
821           GEOM::GEOM_Shape_var aShape = myGeomBase->ConvertIOinGEOMShape(IObject, found);
822           //      Handle(GEOM_AISShape) Shape = myGeomGUI->ConvertIOinGEOMAISShape(IObject, found, true);
823           if ( found ) {
824             QString file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), 
825                                                     QString( IObject->getName() ) + ".igs",
826                                                     tr("GEOM_MEN_IMPORT_IGES"),
827                                                     tr("GEOM_MEN_EXPORT"),
828                                                     false);
829             if ( !file.isEmpty() && !aShape->_is_nil() ) {
830               QApplication::setOverrideCursor( Qt::waitCursor );
831               try {
832                 myGeom->ExportIGES(strdup( file.latin1()), aShape);
833               }  
834               catch (const SALOME::SALOME_Exception& S_ex) {
835                 QtCatchCorbaException(S_ex);
836               }
837 //            //VRV: OCC 4.0 migration
838 //            IGESControl_Controller::Init();
839 //            IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
840 //                                       Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
841 //            //VRV: OCC 4.0 migration
842
843 //            ICW.AddShape (Shape->Shape());
844 //            ICW.ComputeModel();
845 //            Standard_Boolean result = ICW.Write( strdup(file.latin1()) );
846             }
847           }
848         }
849         break;
850       }
851
852     case 123 :
853       {
854 //      bool test = false ;
855 //      IFSelect_ReturnStatus status ;
856 //      //VRV: OCC 4.0 migration
857 //      STEPControl_Writer aWriter;
858 //      //VRV: OCC 4.0 migration
859         QString file;
860
861         for( ; It.More(); It.Next() ) {
862 //        GEOM::GEOM_Shape_var aShape = myGeomBase->ConvertIOinGEOMShape(IObject, found);
863           QApplication::restoreOverrideCursor();
864           Handle(SALOME_InteractiveObject) IObject = It.Value();
865           Standard_Boolean found;
866           GEOM::GEOM_Shape_var aShape = myGeomBase->ConvertIOinGEOMShape(IObject, found);
867           //      Handle(GEOM_AISShape) Shape = myGeomBase->ConvertIOinGEOMAISShape(IObject, found, true);
868           if ( found ) {
869             file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), 
870                                             QString( IObject->getName() ) + ".stp",
871                                             tr("GEOM_MEN_IMPORT_STEP"),
872                                             tr("GEOM_MEN_EXPORT"),
873                                             false);
874             if ( !file.isEmpty() && !aShape->_is_nil() ) {
875
876               QApplication::setOverrideCursor( Qt::waitCursor ) ;       
877               try {   
878                 myGeom->ExportSTEP(strdup( file.latin1()), aShape);   
879               }  
880               catch (const SALOME::SALOME_Exception& S_ex) {
881                 QtCatchCorbaException(S_ex);
882               }
883 //            //VRV: OCC 4.0 migration
884 //            status = aWriter.Transfer( Shape->Shape(), STEPControl_ManifoldSolidBrep ) ;
885 //            //VRV: OCC 4.0 migration
886 //            test = true ;
887 //            if ( status != IFSelect_RetDone ) {
888 //              QApplication::restoreOverrideCursor() ;
889 //              return false ;
890 //            }
891             }
892           }
893         }
894 //      if(test) {
895 //        status = aWriter.Write( strdup(file.latin1()) ) ;
896 //        QApplication::restoreOverrideCursor() ;
897 //        return status ;
898 //      }
899         break;
900       }
901
902     }
903   QApplication::restoreOverrideCursor() ;
904 }
905
906
907 //=====================================================================================
908 // EXPORTED METHODS
909 //=====================================================================================
910 extern "C"
911 {
912   bool OnGUIEvent(int theCommandID, QAD_Desktop* parent)
913   {return GEOMToolsGUI::OnGUIEvent(theCommandID, parent);}
914 }