Salome HOME
Merge from V6_2_BR 23/12/2010
[modules/geom.git] / src / GEOMClient / GEOM_Client.cxx
1 //  Copyright (C) 2007-2010  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 //  GEOM GEOMClient : tool to transfer BREP files from GEOM server to GEOM client
23 //  File   : GEOM_Client.cxx
24 //  Author : Yves FRICAUD/Lucien PIGNOLONI
25 //  Module : GEOM
26
27 #include <Standard_Stream.hxx>
28
29 #include <Standard_Stream.hxx>
30
31 #include <sstream>
32
33 #include "GEOM_Client.hxx"
34 #include <SALOMEconfig.h>
35 #include "Basics_Utils.hxx"
36 #include "utilities.h"
37
38 #include <BRep_Builder.hxx>
39 #include <BRepTools.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <TCollection_AsciiString.hxx>
43 #include <TopExp_Explorer.hxx>
44 #include <TopExp.hxx>
45 #include <TopAbs.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47
48 #ifdef WNT
49 #include <process.h>
50 #else
51 #include <unistd.h>
52 #endif
53
54 #include CORBA_SERVER_HEADER(SALOMEDS)
55 #include CORBA_SERVER_HEADER(GEOM_Gen)
56
57 #define HST_CLIENT_LEN 256
58
59
60 //=======================================================================
61 // function : Load()
62 // purpose  :
63 //=======================================================================
64 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
65 {
66   std::string hst_client = Kernel_Utils::GetHostname();
67
68   Engines::Container_var ctn_server = geom->GetContainerRef();
69   long                   pid_server = ctn_server->getPID();
70
71   CORBA::String_var hostname = ctn_server->getHostName();
72   if ( pid_client == pid_server && !strcmp(hst_client.c_str(), hostname.in()) ) {
73     TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
74     return(*S);
75   } else {
76     /* get sequence of bytes of resulting brep shape from GEOM server */
77     TopoDS_Shape S;
78     SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
79     /*int sizebuf = */SeqFile->length();
80     char* buf;
81     buf = (char*) &SeqFile[0];
82     std::istringstream streamBrep(buf);
83     BRep_Builder aBuilder;
84     BRepTools::Read(S, streamBrep, aBuilder);
85     return(S);
86   }
87 }
88
89 //=======================================================================
90 // function : Create()
91 // purpose  : Create in client not in a container
92 //=======================================================================
93 GEOM_Client::GEOM_Client()
94 {
95   pid_client =
96 #ifdef WNT
97     (long)_getpid();
98 #else
99     (long)getpid();
100 #endif
101 }
102
103 //=======================================================================
104 // function : Create()
105 // purpose  : Copy constructor
106 //=======================================================================
107 GEOM_Client::GEOM_Client(const GEOM_Client& client)
108 {
109   myIORs = client.myIORs;
110   myShapes = client.myShapes;
111   _myIndexes = client._myIndexes;
112   _mySubShapes = client._mySubShapes;
113   pid_client = client.pid_client;
114 }
115
116 //=======================================================================
117 // function : Create()
118 // purpose  :
119 //=======================================================================
120 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
121 {
122   pid_client = client->getPID();
123 }
124
125 //=======================================================================
126 // function : get_client()
127 // purpose  : Static method to have the only one instance of GEOM_Client
128 //=======================================================================
129 #ifdef SINGLE_CLIENT
130 GEOM_Client& GEOM_Client::get_client()
131 {
132   static GEOM_Client a;
133   return a;
134 }
135 #else
136 GEOM_Client GEOM_Client::get_client()
137 {
138   return GEOM_Client();
139 }
140 #endif
141
142 //=======================================================================
143 // function : Find()
144 // purpose  :
145 //=======================================================================
146 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
147 {
148   if (_myIndexes.count(IOR) != 0)
149   {
150     Standard_Integer i = _myIndexes[IOR];
151     S = myShapes.Value(i);
152     return i;
153   }
154   return 0;
155 }
156
157 //=======================================================================
158 // function : Find()
159 // purpose  :
160 //=======================================================================
161 Standard_Integer GEOM_Client::Find( const TopoDS_Shape& S, TCollection_AsciiString& IOR )
162 {
163   for ( Standard_Integer i = 1; i<= myShapes.Length(); i++ ) {
164     if (myShapes.Value(i) == S) {
165       IOR = myIORs.Value(i);
166       return i;
167     }
168   }
169   return 0;
170 }
171
172 //=======================================================================
173 // function : Bind()
174 // purpose  :
175 //=======================================================================
176 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
177 {
178   myIORs.Append(IOR);
179   myShapes.Append(S);
180   _myIndexes[IOR] = myIORs.Length();
181 }
182
183 //=======================================================================
184 // function : RemoveShapeFromBuffer()
185 // purpose  : Remove shape from Client Buffer
186 //=======================================================================
187 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
188 {
189   if( myIORs.IsEmpty() )
190     return;
191
192   TopoDS_Shape S;
193   Standard_Integer anIndex = Find( IOR, S );
194   if( anIndex != 0 ) {
195     myIORs.Remove(anIndex);
196     myShapes.Remove(anIndex);
197     _myIndexes.erase(IOR);
198     _mySubShapes.erase(IOR);
199   }
200 }
201
202 //=======================================================================
203 // function : ClearClientBuffer()
204 // purpose  : purge buffer
205 //=======================================================================
206 void GEOM_Client::ClearClientBuffer()
207 {
208   if( myIORs.IsEmpty() )
209     return;
210   myIORs.Clear();
211   myShapes.Clear();
212   _myIndexes.clear();
213   _mySubShapes.clear();
214   return;
215 }
216
217 //=======================================================================
218 // function : BufferLength()
219 // purpose  :
220 //=======================================================================
221 unsigned int GEOM_Client::BufferLength()
222 {
223   return myIORs.Length();
224 }
225
226 //=======================================================================
227 // function : GetShape()
228 // purpose  :
229 //=======================================================================
230 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
231 {
232   TopoDS_Shape S;
233   CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
234   TCollection_AsciiString IOR = (char*)anIOR.in();
235   Standard_Integer anIndex = Find(IOR, S);
236
237   if (anIndex != 0) return S;
238
239   /******* in case of a MAIN GEOM::SHAPE ********/
240   if (aShape->IsMainShape()) {
241     S = Load(geom, aShape);
242     Bind(IOR, S);
243     return S;
244   }
245
246   /******* in case of SUB GEOM::SHAPE ***********/
247   // Load and Explore the Main Shape
248   TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
249   GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
250
251   CORBA::String_var aMainIOR = geom->GetStringFromIOR(aShape->GetMainShape());
252   TCollection_AsciiString mainIOR = (char*)aMainIOR.in();
253
254   //find subshapes only one time
255   if(_mySubShapes.count(mainIOR)==0)
256   {
257     TopTools_IndexedMapOfShape anIndices;
258     TopExp::MapShapes(aMainShape, anIndices);
259     Standard_Integer ii = 1, nbSubSh = anIndices.Extent();
260     for (; ii <= nbSubSh; ii++)
261     {
262       _mySubShapes[mainIOR].push_back(anIndices.FindKey(ii));
263     }
264   }
265
266   /* Case of only one subshape */
267   if (list->length() == 1 && list[0] > 0) {
268     S = _mySubShapes[mainIOR][list[0]-1];
269   }
270   else {
271     BRep_Builder B;
272     TopoDS_Compound aCompound;
273     B.MakeCompound(aCompound);
274     for (int i = 0; i < list->length(); i++) {
275       if (0 < list[i] && list[i] <= _mySubShapes[mainIOR].size()) {
276         TopoDS_Shape aSubShape = _mySubShapes[mainIOR][list[i]-1];
277         B.Add(aCompound, aSubShape);
278       }
279     }
280
281     S = aCompound;
282   }
283   Bind(IOR, S);
284   return S;
285 }