Salome HOME
bug #237: fatal error on profile
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportGeomObjectOp.cxx
1 // Copyright (C) 2007-2013  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 #include "HYDROGUI_ImportGeomObjectOp.h"
24
25 #include "HYDROGUI_ObstacleDlg.h"
26
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
31
32 #include <HYDROData_Obstacle.h>
33 #include <HYDROData_Iterator.h>
34
35 #include <GEOMBase.h>
36
37 #include <SalomeApp_Study.h>
38
39 #include <LightApp_Application.h>
40 #include <LightApp_UpdateFlags.h>
41
42 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,  
43                                                           const int theOpType, 
44                                                           const bool theIsToShowPanel )
45 : HYDROGUI_Operation( theModule ),
46   myOpType ( theOpType ),
47   myIsToShowPanel ( theIsToShowPanel )
48 {
49   setName( tr( "IMPORT_GEOM_OBJECT" ) );
50 }
51
52 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
53 {
54 }
55
56 void HYDROGUI_ImportGeomObjectOp::startOperation()
57 {
58   HYDROGUI_Operation::startOperation();
59
60   // Get GEOM objects to import
61   myGeomObjects.clear();
62   if ( myOpType == ImportCreated ) {
63     myGeomObjects = module()->GetGeomObjectsToImport();
64   } else if ( myOpType == ImportSelected ) {
65     myGeomObjects = HYDROGUI_Tool::GetSelectedGeomObjects( module() );
66   }
67
68   HYDROGUI_ObstacleDlg* aPanel = 0;
69
70   if ( myGeomObjects.count() == 1 ) {
71     // Get panel
72     aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
73
74     if ( aPanel ) {
75       // Reset the panel state
76       aPanel->reset();
77
78       // Set default name
79       SalomeApp_Study* aStudy = 
80         dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
81       if ( aStudy ) {
82         QString anEntry = myGeomObjects.first();
83         _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
84         if ( aSObject ) {
85           aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
86         }
87       }
88
89       // Pass the existing obstacle names to the panel
90       QStringList anObstacles = 
91         HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
92
93       aPanel->setObstacleNames( anObstacles );
94     }
95   }
96
97   if ( !aPanel ) {
98     onApply();
99   }
100 }
101
102 void HYDROGUI_ImportGeomObjectOp::abortOperation()
103 {
104   HYDROGUI_Operation::abortOperation();
105 }
106
107 void HYDROGUI_ImportGeomObjectOp::commitOperation()
108 {
109   HYDROGUI_Operation::commitOperation();
110 }
111
112 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
113                                                 QString& theErrorMsg )
114 {
115   // Get active SalomeApp_Study
116   SalomeApp_Study* aStudy = 
117     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
118   if ( !aStudy ) {
119     return false;
120   }
121
122   QString anObstacleName;
123   Handle(HYDROData_Obstacle) anObstacleToEdit;  
124
125   if ( myGeomObjects.count() == 1 ) {
126     // Get panel
127     HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
128     if ( aPanel ) {
129       // Check obstacle name
130       anObstacleName = aPanel->getObstacleName().simplified();
131       if ( anObstacleName.isEmpty() ) {
132         theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
133         return false;
134       }
135
136       // Get obstacle to edit
137       QString anEditedName = aPanel->getEditedObstacleName().simplified();
138
139       if ( !anEditedName.isEmpty() ) {
140         anObstacleToEdit = Handle(HYDROData_Obstacle)::DownCast(
141           HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
142       }
143     }
144
145     if( anObstacleToEdit.IsNull() || anObstacleToEdit->GetName() != anObstacleName ) {
146       // check that there are no other objects with the same name in the document
147       Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObstacleName );
148       if( !anObject.IsNull() ) {
149         theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObstacleName );
150         return false;
151       }
152     }
153   }
154
155   bool anIsOk = false;
156
157   // Get the created object as SObject
158   foreach ( const QString& anEntry, myGeomObjects ) {
159     _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
160     if (aSObject) {
161       // Get the corresponding TopoDS_Shape
162       TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
163       if ( !aShape.IsNull() ) {
164         // Create/edit an obstacle object
165         Handle(HYDROData_Obstacle) anObstacle; 
166
167         if ( anObstacleToEdit.IsNull() ) {
168           anObstacle = 
169             Handle(HYDROData_Obstacle)::DownCast( doc()->CreateObject(KIND_OBSTACLE) );
170           
171           anObstacle->SetFillingColor( HYDROData_Obstacle::DefaultFillingColor() );
172           anObstacle->SetBorderColor( HYDROData_Obstacle::DefaultBorderColor() );
173         } else {
174           anObstacle = anObstacleToEdit;
175         }
176
177         // Set name
178         if ( anObstacleName.isEmpty() ) {
179           QString aName = QString::fromStdString( aSObject->GetName() );
180           anObstacleName = HYDROGUI_Tool::GenerateObjectName( 
181             module(), aName, QStringList(), true );
182         }
183         if ( anObstacle->GetName() != anObstacleName ) {
184           anObstacle->SetName( anObstacleName );
185         }
186
187         anObstacleName.clear();
188
189         // Set shape
190         anObstacle->SetShape3D( aShape );
191
192         // Set operation status
193         anIsOk = true;
194         theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
195       }
196     }
197   }
198
199   return anIsOk;
200 }
201
202 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
203 {
204   HYDROGUI_InputPanel* aPanel = 0;
205   if ( myIsToShowPanel ) {
206     aPanel = new HYDROGUI_ObstacleDlg( module(), getName() );
207   }
208
209   return aPanel;
210 }