Salome HOME
porting on linux
[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>& 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   TopExp_Explorer exp (theShape, TopAbs_FACE);
179   QVector<QString> Names;
180   for (; exp.More(); exp.Next())
181   {
182     const TopoDS_Shape& csh = exp.Current();
183     if (mapShape.Add(csh))
184     {
185       //listShape.Append(csh);
186       QString Qstr = aFacesToNameModif.FindFromKey(csh);
187       Names.push_back(Qstr);
188     }
189   }
190
191   for (size_t i = 0; i < aFc->length(); i++)
192     GEOMBase::PublishSubObject(aFc->operator[](i), Names[i]);  
193
194   return aGeomObj;
195 }
196
197
198  GEOM::GEOM_Object_ptr HYDROData_GeomTool::createFaceInGEOM( GEOM::GEOM_Gen_var theGeomEngine,
199                                                              SALOMEDS::Study_ptr theStudy,
200                                                              const int theWidth,
201                                                              const int theHeight,
202                                                              const QString& theName,
203                                                              QString& theFaceEntry )
204 {
205   theFaceEntry = "";
206   GEOM::GEOM_Object_var aGeomObj;
207
208   if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ) {
209     return aGeomObj._retn();
210   }
211
212   GEOM::GEOM_IBasicOperations_var aBasicOperations = theGeomEngine->GetIBasicOperations( theStudy->StudyId() );
213   GEOM::GEOM_IBlocksOperations_var aBlocksOperations = theGeomEngine->GetIBlocksOperations( theStudy->StudyId() );
214          
215   GEOM::GEOM_Object_var P1 = aBasicOperations->MakePointXYZ( -0.5 * theWidth, -0.5 * theHeight, 0 );
216   GEOM::GEOM_Object_var P2 = aBasicOperations->MakePointXYZ(  0.5 * theWidth, -0.5 * theHeight, 0 );
217   GEOM::GEOM_Object_var P3 = aBasicOperations->MakePointXYZ(  0.5 * theWidth,  0.5 * theHeight, 0 );
218   GEOM::GEOM_Object_var P4 = aBasicOperations->MakePointXYZ( -0.5 * theWidth,  0.5 * theHeight, 0 );
219   
220   GEOM::GEOM_Object_var aFace = aBlocksOperations->MakeQuad4Vertices( P1, P2 ,P3, P4 );
221     
222   // Publish the face
223   theFaceEntry = publishGEOMObject( theGeomEngine, theStudy, aFace, theName );
224
225   return aFace._retn();
226 }
227
228 QString HYDROData_GeomTool::publishGEOMObject( GEOM::GEOM_Gen_var theGeomEngine,
229                                                SALOMEDS::Study_ptr theStudy,
230                                                GEOM::GEOM_Object_ptr theGeomObj,
231                                                const QString& theName )
232 {
233   QString anEntry;
234
235   if ( !theGeomObj->_is_nil() ) {
236     QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
237
238     SALOMEDS::SObject_var aResultSO = 
239       theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), 
240                                      theGeomObj, qPrintable( aName ) );
241     if ( !aResultSO->_is_nil() ) {
242       anEntry = aResultSO->GetID();
243     }
244   }
245
246   return anEntry;
247 }
248
249 #endif