Salome HOME
[fix] After merge with BR_PY3
[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 static SALOMEDS::Study_var _STUDY = SALOMEDS::Study::_nil();
51
52 QString HYDROData_GeomTool::myContainerName = "FactoryServer";
53
54 void HYDROData_GeomTool::SetContainerName( const QString& theContainerName )
55 {
56   myContainerName = theContainerName;
57 }
58
59 TopoDS_Shape HYDROData_GeomTool::GetShapeFromIOR( const QString& theIOR )
60 {
61   // Note that GEOMBase::GetShape() cause crash in batch mode
62
63   TopoDS_Shape aResShape;
64    
65   CORBA::Object_var aCorbaObj = _ORB->string_to_object( qPrintable( theIOR ) );
66   if ( !CORBA::is_nil( aCorbaObj ) ) {
67     GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow( aCorbaObj );
68
69     Engines::EngineComponent_var aComp =
70       _LCC.FindOrLoad_Component( myContainerName.toStdString().c_str(), "GEOM" );
71     GEOM::GEOM_Gen_var aComponentGeom = GEOM::GEOM_Gen::_narrow( aComp );
72
73     aResShape = GEOM_Client::get_client().GetShape( aComponentGeom, aGeomObj );
74   }
75
76   return aResShape;
77 }
78
79 GEOM::GEOM_Gen_var HYDROData_GeomTool::GetGeomGen()
80 {
81   Engines::EngineComponent_var aComponent = _LCC.FindOrLoad_Component( myContainerName.toStdString().c_str(), "GEOM" );
82   GEOM::GEOM_Gen_var aGEOMEngine = GEOM::GEOM_Gen::_narrow( aComponent );
83
84   return aGEOMEngine._retn();
85 }
86
87 SALOMEDS::Study_var HYDROData_GeomTool::GetStudy()
88 {
89   if (_STUDY->_is_nil())
90   {
91     CORBA::Object_var aStudyObject = _NS.Resolve("/Study");
92     _STUDY = SALOMEDS::Study::_narrow(aStudyObject);
93   }
94   return _STUDY._retn();
95 }
96
97 QString HYDROData_GeomTool::GetFreeName( const QString& theBaseName )
98 {
99   QString aName = theBaseName;
100
101   SALOMEDS::Study_var theStudy = GetStudy();
102   if ( !theStudy->_is_nil() ) {
103     // collect all object names of GEOM component
104     QSet<QString> anUsedNames;
105     SALOMEDS::SComponent_var aComponent = theStudy->FindComponent( "GEOM" );
106     if ( !aComponent->_is_nil() ) {
107       SALOMEDS::ChildIterator_var anIter = theStudy->NewChildIterator( aComponent );
108       for ( anIter->InitEx( true ); anIter->More(); anIter->Next() ) {
109         anUsedNames.insert( anIter->Value()->GetName() );
110       }
111     }
112
113     // build an unique name
114     int aNumber = 0;
115     bool isUnique = false;
116     QString aPrefix = theBaseName;
117     QStringList aParts = aPrefix.split( "_", QString::KeepEmptyParts );
118     if ( aParts.count() > 1 ) {
119       bool isOk;
120       aNumber = aParts.last().toLong( &isOk );
121       if ( isOk ) {
122         aParts.removeLast();
123         aPrefix = aParts.join( "_" );
124         aNumber--;
125       }
126     }
127     
128     while ( !isUnique ) {
129       aName = aPrefix + "_" + QString::number( ++aNumber );
130       isUnique = ( !anUsedNames.contains( aName ) );
131     }
132   }
133
134   return aName;
135 }
136
137 GEOM::GEOM_Object_ptr HYDROData_GeomTool::publishShapeInGEOM(
138   GEOM::GEOM_Gen_var theGeomEngine,
139   const TopoDS_Shape& theShape, const QString& theName,
140   QString& theGeomObjEntry )
141 {
142   theGeomObjEntry = "";
143   GEOM::GEOM_Object_var aGeomObj;
144
145   if ( theGeomEngine->_is_nil() || 
146        theShape.IsNull() ) {
147     return aGeomObj._retn();
148   }
149
150   std::ostringstream aStreamShape;
151   // Write TopoDS_Shape in ASCII format to the stream
152   BRepTools::Write( theShape, aStreamShape );
153   // Returns the number of bytes that have been stored in the stream's buffer.
154   int aSize = aStreamShape.str().size();
155   // Allocate octect buffer of required size
156   CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
157   // Copy ostrstream content to the octect buffer
158   memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
159   // Create TMPFile
160   SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
161
162   // Restore shape from the stream and get the GEOM object
163   GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations();
164   aGeomObj = anInsOp->RestoreShape( aSeqFile );
165   
166   // Publish the GEOM object
167   theGeomObjEntry = publishGEOMObject( theGeomEngine, aGeomObj, theName );
168   
169   return aGeomObj._retn();
170 }
171
172
173 GEOM::GEOM_Object_ptr HYDROData_GeomTool::ExplodeShapeInGEOMandPublish( GEOM::GEOM_Gen_var theGeomEngine, 
174   const TopoDS_Shape& theShape,   
175   const NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher>& aFacesToNameModif,  
176   const QString& theName,
177   QString& theGeomObjEntry)
178
179   GEOM::GEOM_Object_ptr aGeomObj = HYDROData_GeomTool::publishShapeInGEOM( theGeomEngine, theShape, theName, theGeomObjEntry );
180
181   GEOM::GEOM_IShapesOperations_var anExpOp = theGeomEngine->GetIShapesOperations();
182   GEOM::ListOfGO* aFc = anExpOp->MakeExplode(aGeomObj, TopAbs_FACE, false);
183
184   TopTools_MapOfShape mapShape;
185
186   NCollection_IndexedDataMap<TopoDS_Shape, QString, TopTools_ShapeMapHasher> aFF2N;
187   for (int i = 1; i <= aFacesToNameModif.Extent(); i++)
188   {
189     TopExp_Explorer exp (aFacesToNameModif.FindKey(i), TopAbs_FACE);
190     for (;exp.More(); exp.Next())
191       aFF2N.Add(exp.Current(), aFacesToNameModif.FindFromIndex(i));
192   }
193
194   TopExp_Explorer exp (theShape, TopAbs_FACE);
195   QVector<QString> Names;
196   for (; exp.More(); exp.Next())
197   {
198     const TopoDS_Shape& csh = exp.Current();
199     if (mapShape.Add(csh))
200     {
201       //listShape.Append(csh);
202       QString Qstr = aFF2N.FindFromKey(csh);
203       Names.push_back(Qstr);
204     }
205   }
206
207   for (size_t i = 0; i < aFc->length(); i++)
208   {
209     std::string name = Names[i].toStdString();
210     GEOM::GEOM_Object_ptr anObj = aFc->operator[](i);
211     //GEOMBase::PublishSubObject( anObj, name.c_str() );
212     theGeomEngine->AddInStudy( anObj, name.c_str(), aGeomObj );
213   }
214
215   return aGeomObj;
216 }
217
218
219  GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
220                                                              const int theWidth,
221                                                              const int theHeight,
222                                                              const QString& theName,
223                                                              QString& theFaceEntry )
224 {
225   theFaceEntry = "";
226   GEOM::GEOM_Object_var aGeomObj;
227
228   if ( theGeomEngine->_is_nil() ) {
229     return aGeomObj._retn();
230   }
231
232   GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations();
233   GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations();
234          
235   GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
236   GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ(  0.5 * theWidth, -0.5 * theHeight, 0 );
237   GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ(  0.5 * theWidth,  0.5 * theHeight, 0 );
238   GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth,  0.5 * theHeight, 0 );
239   
240   GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
241     
242   // Publish the face
243   theFaceEntry = publishGEOMObject( theGeomEngine, aFace, theName );
244
245   return aFace._retn();
246 }
247
248 QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
249                                                GEOM::GEOM_Object_ptr theGeomObj,
250                                                const QString& theName )
251 {
252   QString anEntry;
253
254   if ( !theGeomObj->_is_nil() ) {
255     QString aName = HYDROData_GeomTool::GetFreeName( theName );
256
257     SALOMEDS::SObject_var aResultSO = 
258       theGeomEngine->PublishInStudy( SALOMEDS::SObject::_nil(), 
259                                      theGeomObj, qPrintable( aName ) );
260     if ( !aResultSO->_is_nil() ) {
261       anEntry = aResultSO->GetID();
262     }
263   }
264
265   return anEntry;
266 }
267
268 #endif