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