1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_ImportGeomObjectOp.h"
25 #include "HYDROGUI_GeomObjectDlg.h"
27 #include "HYDROGUI_DataModel.h"
28 #include "HYDROGUI_Module.h"
29 #include "HYDROGUI_Tool.h"
30 #include "HYDROGUI_UpdateFlags.h"
32 #include <HYDROData_Obstacle.h>
33 #include <HYDROData_PolylineXY.h>
34 #include <HYDROData_Iterator.h>
38 #include <SalomeApp_Study.h>
40 #include <LightApp_Application.h>
41 #include <LightApp_UpdateFlags.h>
43 HYDROGUI_ImportGeomObjectOp::HYDROGUI_ImportGeomObjectOp( HYDROGUI_Module* theModule,
45 const bool theIsToShowPanel )
46 : HYDROGUI_Operation( theModule ),
47 myOpType ( theOpType ),
48 myIsToShowPanel ( theIsToShowPanel )
50 if ( myOpType == ImportSelectedAsPolyline ) {
51 setName( tr( "IMPORT_GEOM_OBJECT_AS_POLYLINE" ) );
53 setName( tr( "IMPORT_GEOM_OBJECT_AS_OBSTACLE" ) );
57 HYDROGUI_ImportGeomObjectOp::~HYDROGUI_ImportGeomObjectOp()
61 void HYDROGUI_ImportGeomObjectOp::startOperation()
63 HYDROGUI_Operation::startOperation();
65 // Get GEOM objects to import
66 myGeomObjects.clear();
67 if ( myOpType == ImportCreatedAsObstacle ) {
68 myGeomObjects = module()->GetGeomObjectsToImport();
69 } else if ( myOpType == ImportSelectedAsObstacle ) {
71 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getObstacleTypes() );
72 } else if ( myOpType == ImportSelectedAsPolyline ) {
74 HYDROGUI_Tool::GetSelectedGeomObjects( module(), getPolylineTypes() );
77 HYDROGUI_GeomObjectDlg* aPanel = 0;
79 if ( myGeomObjects.count() == 1 ) {
81 aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
84 // Reset the panel state
88 SalomeApp_Study* aStudy =
89 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
91 QString anEntry = myGeomObjects.first();
92 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
94 aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
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 );
107 aPanel->setObjectNames( anExistingNames );
116 void HYDROGUI_ImportGeomObjectOp::abortOperation()
118 HYDROGUI_Operation::abortOperation();
121 void HYDROGUI_ImportGeomObjectOp::commitOperation()
123 HYDROGUI_Operation::commitOperation();
126 bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
127 QString& theErrorMsg )
129 // Get active SalomeApp_Study
130 SalomeApp_Study* aStudy =
131 dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
136 QString anObjectName;
137 Handle(HYDROData_Entity) anObjectToEdit;
138 ObjectKind anObjectKind =
139 myOpType == ImportSelectedAsPolyline ? KIND_POLYLINEXY : KIND_OBSTACLE;
141 if ( myGeomObjects.count() == 1 ) {
143 HYDROGUI_GeomObjectDlg* aPanel = ::qobject_cast<HYDROGUI_GeomObjectDlg*>( inputPanel() );
146 anObjectName = aPanel->getObjectName().simplified();
147 if ( anObjectName.isEmpty() ) {
148 theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
152 // Get object to edit
153 QString anEditedName = aPanel->getEditedObjectName().simplified();
155 if ( !anEditedName.isEmpty() ) {
156 anObjectToEdit = HYDROGUI_Tool::FindObjectByName( module(), anEditedName, anObjectKind );
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 );
172 // Get the GEOM object as SObject
173 foreach ( const QString& anEntry, myGeomObjects ) {
174 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry)) );
176 // Get the corresponding TopoDS_Shape
177 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
178 if ( aShape.IsNull() ) {
182 // Create/edit an object
183 Handle(HYDROData_Entity) anObject;
185 if ( anObjectToEdit.IsNull() ) {
186 if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
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 ) {
194 Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject(KIND_POLYLINEXY) );
197 anObject = anObjectToEdit;
201 if ( anObjectName.isEmpty() ) {
202 QString aName = QString::fromStdString( aSObject->GetName() );
203 anObjectName = HYDROGUI_Tool::GenerateObjectName(
204 module(), aName, QStringList(), true );
206 if ( anObject->GetName() != anObjectName ) {
207 anObject->SetName( anObjectName );
210 anObjectName.clear();
213 if ( myOpType == ImportCreatedAsObstacle || myOpType == ImportSelectedAsObstacle ) {
214 Handle(HYDROData_Obstacle) anObstacle = Handle(HYDROData_Obstacle)::DownCast( anObject );
215 anObstacle->SetShape3D( aShape );
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 );
223 // Check operation status
226 theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
234 HYDROGUI_InputPanel* HYDROGUI_ImportGeomObjectOp::createInputPanel() const
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 );
247 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getObstacleTypes()
249 QList<GEOM::shape_type> aTypes;
251 aTypes << GEOM::COMPOUND << GEOM::COMPOUND << GEOM::SOLID <<
252 GEOM::SHELL << GEOM::FACE << GEOM::SHAPE;
257 QList<GEOM::shape_type> HYDROGUI_ImportGeomObjectOp::getPolylineTypes()
259 QList<GEOM::shape_type> aTypes;
261 aTypes << GEOM::WIRE << GEOM::EDGE;