]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_GeomTool.cxx
Salome HOME
Merge branch 'BR_H2018_2' of https://codev-tuleap.cea.fr/plugins/git/salome/hydro...
[modules/hydro.git] / src / HYDROData / HYDROData_GeomTool.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_GeomTool.h"
20
21 #ifndef LIGHT_MODE
22
23 #include <TopoDS_Shape.hxx>
24
25 #include <BRepTools.hxx>
26 #include <GEOMBase.h>
27
28 #ifdef WIN32
29   #pragma warning ( disable: 4251 )
30 #endif
31
32 #include <SALOME_NamingService.hxx>
33 #include <SALOME_LifeCycleCORBA.hxx>
34
35 #include <TopTools_MapOfShape.hxx>
36 #include <TopExp_Explorer.hxx>
37
38 #ifdef WIN32
39   #pragma warning ( default: 4251 )
40 #endif
41
42 #include <QStringList>
43 #include <QVector>
44 #include <QSet>
45
46 static int _argc = 0;
47 static CORBA::ORB_var _ORB = CORBA::ORB_init( _argc, 0, "omniORB4"/*CORBA::ORB_ID*/ );
48 static SALOME_NamingService _NS( _ORB );
49 static SALOME_LifeCycleCORBA _LCC( &_NS );
50
51
52 TopoDS_Shape HYDROData_GeomTool::GetShapeFromIOR( const int theStudyId, const QString& theIOR )
53 {
54   // Note that GEOMBase::GetShape() cause crash in batch mode
55
56   TopoDS_Shape aResShape;
57    
58   CORBA::Object_var aCorbaObj = _ORB->string_to_object( qPrintable( theIOR ) );
59   if ( !CORBA::is_nil( aCorbaObj ) ) {
60     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aCorbaObj );
61
62     Engines::EngineComponent_var aComp =
63       _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
64     GEOM::GEOM_Gen_var aComponentGeom = GEOM::GEOM_Gen::_narrow( aComp );
65
66     aResShape = GEOM_Client::get_client().GetShape( aComponentGeom, aGeomObj );
67   }
68
69   return aResShape;
70 }
71
72 GEOM::GEOM_Gen_var HYDROData_GeomTool::GetGeomGen()
73 {
74   Engines::EngineComponent_var aComponent = _LCC.FindOrLoad_Component( "FactoryServer", "GEOM" );
75   GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
76
77   return aGEOMEngine._retn();
78 }
79
80 SALOMEDS::Study_var HYDROData_GeomTool::GetStudyByID( const int theStudyId )
81 {
82   CORBA::Object_var aSMObject = _NS.Resolve("/myStudyManager");
83   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
84   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( theStudyId );
85
86   return aDSStudy._retn();
87 }
88
89 QString HYDROData_GeomTool::GetFreeName( SALOMEDS::Study_ptr theStudy, const QString& theBaseName )
90 {
91   QString aName = theBaseName;
92
93   if ( !theStudy->_is_nil() ) {
94     // collect all object names of GEOM component
95     QSet<QString> anUsedNames;
96     SALOMEDS::SComponent_var aComponent = theStudy->FindComponent( "GEOM" );
97     if ( !aComponent->_is_nil() ) {
98       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( aComponent );
99       for ( anIter->InitEx( true ); anIter->More(); anIter->Next() ) {
100         anUsedNames.insert( anIter->Value()->GetName() );
101       }
102     }
103
104     // build an unique name
105     int aNumber = 0;
106     bool isUnique = false;
107     QString aPrefix = theBaseName;
108     QStringList aParts = aPrefix.split( "_", QString::KeepEmptyParts );
109     if ( aParts.count() > 1 ) {
110       bool isOk;
111       aNumber = aParts.last().toLong( &isOk );
112       if ( isOk ) {
113         aParts.removeLast();
114         aPrefix = aParts.join( "_" );
115         aNumber--;
116       }
117     }
118     
119     while ( !isUnique ) {
120       aName = aPrefix + "_" + QString::number( ++aNumber );
121       isUnique = ( !anUsedNames.contains( aName ) );
122     }
123   }
124
125   return aName;
126 }
127
128 GEOM::GEOM_Object_ptr HYDROData_GeomTool::publishShapeInGEOM( 
129   GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
130   const TopoDS_Shape& theShape, const QString& theName,
131   QString& theGeomObjEntry )
132 {
133   theGeomObjEntry = "";
134   GEOM::GEOM_Object_var aGeomObj;
135
136   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
137        theShape.IsNull() ) {
138     return aGeomObj._retn();
139   }
140
141   std::ostringstream aStreamShape;
142   // Write TopoDS_Shape in ASCII format to the stream
143   BRepTools::Write( theShape, aStreamShape );
144   // Returns the number of bytes that have been stored in the stream's buffer.
145   int aSize = aStreamShape.str().size();
146   // Allocate octect buffer of required size
147   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
148   // Copy ostrstream content to the octect buffer
149   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
150   // Create TMPFile
151   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
152
153   // Restore shape from the stream and get the GEOM object
154   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
155   aGeomObj = anInsOp->RestoreShape( aSeqFile );
156   
157   // Publish the GEOM object
158   theGeomObjEntry = publishGEOMObject( theGeomEngine, theStudy, aGeomObj, theName );
159   
160   return aGeomObj._retn();
161 }
162
163
164 GEOM::GEOM_Object_ptr HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( GEOM::GEOM_Gen_var theGeomEngine, 
165   SALOMEDS::Study_ptr theStudy, 
166   const TopoDS_Shape& theShape,   
167   const NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher>& aFacesToNameModif,  
168   const QString& theName,
169   QString& theGeomObjEntry)
170
171   GEOM::GEOM_Object_ptr aGeomObj = HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theStudy, theShape, theName, theGeomObjEntry );
172
173   GEOM::GEOM_IShapesOperations_var anExpOp = theGeomEngine->GetIShapesOperations( theStudy->StudyId() );
174   GEOM::ListOfGO* aFc = anExpOp->MakeExplode(aGeomObj, TopAbs_FACE, false);
175
176   TopTools_MapOfShape mapShape;
177
178   NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFF2N;
179   for (int i = 1; i <= aFacesToNameModif.Extent(); i++)
180   {
181     TopExp_Explorer exp (aFacesToNameModif.FindKey(i), TopAbs_FACE);
182     for (;exp.More(); exp.Next())
183       aFF2N.Add(exp.Current(), aFacesToNameModif.FindFromIndex(i));
184   }
185
186   TopExp_Explorer exp (theShape, TopAbs_FACE);
187   QVector<QString> Names;
188   for (; exp.More(); exp.Next())
189   {
190     const TopoDS_Shape& csh = exp.Current();
191     if (mapShape.Add(csh))
192     {
193       //listShape.Append(csh);
194       QString Qstr = aFF2N.FindFromKey(csh);
195       Names.push_back(Qstr);
196     }
197   }
198
199   for (size_t i = 0; i < aFc->length(); i++)
200   {
201     std::string name = Names[i].toStdString();
202     GEOM::GEOM_Object_ptr anObj = aFc->operator[](i);
203     //GEOMBase::PublishSubObject( anObj, name.c_str() );
204     theGeomEngine->AddInStudy( theStudy, anObj, name.c_str(), aGeomObj );
205   }
206
207   return aGeomObj;
208 }
209
210
211  GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
212                                                              SALOMEDS::Study_ptr theStudy,
213                                                              const int theWidth,
214                                                              const int theHeight,
215                                                              const QString& theName,
216                                                              QString& theFaceEntry )
217 {
218   theFaceEntry = "";
219   GEOM::GEOM_Object_var aGeomObj;
220
221   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ) {
222     return aGeomObj._retn();
223   }
224
225   GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations( theStudy->StudyId() );
226   GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations( theStudy->StudyId() );
227          
228   GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
229   GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ(  0.5 * theWidth, -0.5 * theHeight, 0 );
230   GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ(  0.5 * theWidth,  0.5 * theHeight, 0 );
231   GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth,  0.5 * theHeight, 0 );
232   
233   GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
234     
235   // Publish the face
236   theFaceEntry = publishGEOMObject( theGeomEngine, theStudy, aFace, theName );
237
238   return aFace._retn();
239 }
240
241 QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
242                                                SALOMEDS::Study_ptr theStudy,
243                                                GEOM::GEOM_Object_ptr theGeomObj,
244                                                const QString& theName )
245 {
246   QString anEntry;
247
248   if ( !theGeomObj->_is_nil() ) {
249     QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
250
251     SALOMEDS::SObject_var aResultSO = 
252       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
253                                      theGeomObj, qPrintable( aName ) );
254     if ( !aResultSO->_is_nil() ) {
255       anEntry = aResultSO->GetID();
256     }
257   }
258
259   return anEntry;
260 }
261
262 #endif