Salome HOME
Create goups for stream.
[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_GeomObjectDlg.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_PolylineXY.h>
34 #include <HYDROData_Iterator.h>
35
36 #include <GEOMBase.h>
37
38 #include <SalomeApp_Study.h>
39
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
42
43 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,  
44                                                           const int theOpType, 
45                                                           const bool theIsToShowPanel )
46 : HYDROGUI_Operation( theModule ),
47   myOpType ( theOpType ),
48   myIsToShowPanel ( theIsToShowPanel )
49 {
50   if ( myOpType == ImportSelectedAsPolyline ) {
51     setName( tr( "IMPORT_GEOM_OBJECT_AS_POLYLINE" ) );
52   } else {
53     setName( tr( "IMPORT_GEOM_OBJECT_AS_OBSTACLE" ) );
54   }
55 }
56
57 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
58 {
59 }
60
61 void HYDROGUI_ImportGeomObjectOp::startOperation()
62 {
63   HYDROGUI_Operation::startOperation();
64
65   // Get GEOM objects to import
66   myGeomObjects.clear();
67   if ( myOpType == ImportCreatedAsObstacle ) {
68     myGeomObjects = module()->GetGeomObjectsToImport();
69   } else if ( myOpType == ImportSelectedAsObstacle ) {
70     myGeomObjects = 
71       HYDROGUI_Tool::GetSelectedGeomObjects( module(), getObstacleTypes() );
72   } else if ( myOpType == ImportSelectedAsPolyline ) {
73     myGeomObjects = 
74       HYDROGUI_Tool::GetSelectedGeomObjects( module(), getPolylineTypes() );
75   }
76
77   HYDROGUI_GeomObjectDlg* aPanel = 0;
78
79   if ( myGeomObjects.count() == 1 ) {
80     // Get panel
81     aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
82
83     if ( aPanel ) {
84       // Reset the panel state
85       aPanel->reset();
86
87       // Set default name
88       SalomeApp_Study* aStudy = 
89         dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
90       if ( aStudy ) {
91         QString anEntry = myGeomObjects.first();
92         _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
93         if ( aSObject ) {
94           aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
95         }
96       }
97
98       // Pass the existing object names to the panel
99       QStringList anExistingNames;
100       if ( myOpType == ImportCreatedAsObstacle || 
101            myOpType == ImportSelectedAsObstacle ) {
102         anExistingNames = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
103       } else if ( myOpType == ImportSelectedAsPolyline ) {
104         anExistingNames = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINEXY );
105       }
106
107       aPanel->setObjectNames( anExistingNames );
108     }
109   }
110
111   if ( !aPanel ) {
112     onApply();
113   }
114 }
115
116 void HYDROGUI_ImportGeomObjectOp::abortOperation()
117 {
118   HYDROGUI_Operation::abortOperation();
119 }
120
121 void HYDROGUI_ImportGeomObjectOp::commitOperation()
122 {
123   HYDROGUI_Operation::commitOperation();
124 }
125
126 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
127                                                 QString& theErrorMsg )
128 {
129   // Get active SalomeApp_Study
130   SalomeApp_Study* aStudy = 
131     dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
132   if ( !aStudy ) {
133     return false;
134   }
135
136   QString anObjectName;
137   Handle(HYDROData_Entity) anObjectToEdit;  
138   ObjectKind anObjectKind = 
139     myOpType == ImportSelectedAsPolyline ? KIND_POLYLINEXY : KIND_OBSTACLE;
140
141   if ( myGeomObjects.count() == 1 ) {
142     // Get panel
143     HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
144     if ( aPanel ) {
145       // Check object name
146       anObjectName = aPanel->getObjectName().simplified();
147       if ( anObjectName.isEmpty() ) {
148         theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
149         return false;
150       }
151
152       // Get object to edit
153       QString anEditedName = aPanel->getEditedObjectName().simplified();
154
155       if ( !anEditedName.isEmpty() ) {
156         anObjectToEdit = HYDROGUI_Tool::FindObjectByName( module(), anEditedName, anObjectKind );
157       }
158     }
159
160     if( anObjectToEdit.IsNull() || anObjectToEdit->GetName() != anObjectName ) {
161       // check that there are no other objects with the same name in the document
162       Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName/*, anObjectKind*/ );
163       if( !anObject.IsNull() ) {
164         theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
165         return false;
166       }
167     }
168   }
169
170   bool anIsOk = false;
171
172   // Get the GEOM object as SObject
173   foreach ( const QString& anEntry, myGeomObjects ) {
174     _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
175     if ( aSObject ) {
176       // Get the corresponding TopoDS_Shape
177       TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
178       if ( aShape.IsNull() ) {
179         continue;
180       }
181       
182       // Create/edit an object
183       Handle(HYDROData_Entity) anObject; 
184
185       if ( anObjectToEdit.IsNull() ) {
186         if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
187           anObject = 
188             Handle(HYDROData_Obstacle)::DownCast( doc()->CreateObject(KIND_OBSTACLE) );
189           Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
190           anObstacle->SetFillingColor( HYDROData_Obstacle::DefaultFillingColor() );
191           anObstacle->SetBorderColor( HYDROData_Obstacle::DefaultBorderColor() );
192         } else if ( myOpType == ImportSelectedAsPolyline ) {
193           anObject = 
194             Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject(KIND_POLYLINEXY) );
195         }
196       } else {
197         anObject = anObjectToEdit;
198       }
199
200       // Set name
201       if ( anObjectName.isEmpty() ) {
202         QString aName = QString::fromStdString( aSObject->GetName() );
203         anObjectName = HYDROGUI_Tool::GenerateObjectName( 
204           module(), aName, QStringList(), true );
205       }
206       if ( anObject->GetName() != anObjectName ) {
207         anObject->SetName( anObjectName );
208       }
209
210       anObjectName.clear();
211
212       // Set shape
213       if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
214         Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
215         anObstacle->SetShape3D( aShape );
216         anIsOk = true;
217       } else if ( myOpType == ImportSelectedAsPolyline ) {
218         Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast( anObject );
219         // TODO ISSUE #228: set the shape ("aShape") to the polyline
220         // anIsOk = aPolyline->setShape( aShape );
221       }
222
223       // Check operation status
224       if ( anIsOk ) {
225         anObject->Update();
226         theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
227       }
228     }
229   }
230
231   return anIsOk;
232 }
233
234 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
235 {
236   HYDROGUI_InputPanel* aPanel = 0;
237   if ( myIsToShowPanel ) {
238     QString anObjectTypeName = 
239       myOpType == ImportSelectedAsPolyline ? tr("DEFAULT_POLYLINE_NAME") :
240                   tr("DEFAULT_OBSTACLE_NAME");
241     aPanel = new HYDROGUI_GeomObjectDlg( module(), getName(), anObjectTypeName );
242   }
243
244   return aPanel;
245 }
246
247 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getObstacleTypes()
248 {
249   QList<GEOM::shape_type> aTypes;
250
251   aTypes << GEOM::COMPOUND << GEOM::COMPOUND << GEOM::SOLID << 
252             GEOM::SHELL << GEOM::FACE << GEOM::SHAPE;
253
254   return aTypes;
255 }
256
257 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getPolylineTypes()
258 {
259   QList<GEOM::shape_type> aTypes;
260
261   aTypes << GEOM::WIRE << GEOM::EDGE;
262
263   return aTypes;
264 }