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