Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMClient / GEOM_Client.cxx
1 //  Copyright (C) 2007-2008  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 //  $Header$
27 //
28 #include <Standard_Stream.hxx>
29
30 #include <Standard_Stream.hxx>
31
32 #include <strstream>
33
34 #include "GEOM_Client.hxx"
35 #include <SALOMEconfig.h>
36 #include "Basics_Utils.hxx"
37 #include "utilities.h"
38
39 #include <BRep_Builder.hxx>
40 #include <BRepTools.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopExp.hxx>
46 #include <TopAbs.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48
49 #ifdef WNT
50 #include <process.h>
51 #else
52 #include <unistd.h>
53 #endif
54
55 #include CORBA_SERVER_HEADER(SALOMEDS)
56 #include CORBA_SERVER_HEADER(GEOM_Gen)
57
58 #define HST_CLIENT_LEN 256
59
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    if ( (pid_client==pid_server) && (strcmp(hst_client.c_str(), ctn_server->getHostName())==0) ) {
74         TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
75         return(*S);
76     } else {
77         /* get sequence of bytes of resulting brep shape from GEOM server */
78         TopoDS_Shape S;
79         SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
80         int sizebuf = SeqFile->length();
81         char* buf;
82         buf = (char*) &SeqFile[0];
83         std::istrstream streamBrep(buf,sizebuf);
84         BRep_Builder aBuilder;
85         BRepTools::Read(S, streamBrep, aBuilder);
86         return(S);
87     };
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   for ( Standard_Integer i = 1; i<= myIORs.Length(); i++ ) {
121     if (myIORs.Value(i).IsEqual(IOR)) {
122       S = myShapes.Value(i);
123       return i;
124     }
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 }
153
154 //=======================================================================
155 // function : RemoveShapeFromBuffer()
156 // purpose  : Remove shape from Client Buffer
157 //=======================================================================
158 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
159 {
160   if( myIORs.IsEmpty() )
161     return ;
162   
163   TopoDS_Shape S ;
164   Standard_Integer anIndex = Find( IOR, S ) ;
165   if( anIndex != 0 ) {
166     myIORs.Remove(anIndex) ;
167     myShapes.Remove(anIndex) ;
168   }
169   return ;
170 }
171
172
173 //=======================================================================
174 // function : ClearClientBuffer()
175 // purpose  : purge buffer
176 //=======================================================================
177 void GEOM_Client::ClearClientBuffer()
178 {
179   if( myIORs.IsEmpty() )
180     return ;
181   myIORs.Clear() ;
182   myShapes.Clear() ;
183   return ;
184 }
185
186 //=======================================================================
187 // function : BufferLength()
188 // purpose  :
189 //=======================================================================
190 unsigned int GEOM_Client::BufferLength()
191 {
192   return myIORs.Length() ;
193 }
194
195
196 //=======================================================================
197 // function : GetShape()
198 // purpose  : 
199 //=======================================================================
200 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
201 {
202   TopoDS_Shape S;
203   TCollection_AsciiString IOR = geom->GetStringFromIOR(aShape);
204   Standard_Integer anIndex = Find(IOR, S);
205
206   if (anIndex != 0) return S;
207
208   /******* in case of a MAIN GEOM::SHAPE ********/
209   if (aShape->IsMainShape()) {
210     S = Load(geom, aShape);
211     Bind(IOR, S);
212     return S;
213   }
214
215   /******* in case of SUB GEOM::SHAPE ***********/
216   // Load and Explore the Main Shape
217   TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
218   GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
219
220   TopTools_IndexedMapOfShape anIndices;
221   TopExp::MapShapes(aMainShape, anIndices);
222
223   /* Case of only one subshape */
224   if (list->length() == 1 && list[0] > 0) {
225     S = anIndices.FindKey(list[0]);
226   }
227   else {
228     BRep_Builder B;
229     TopoDS_Compound aCompound;
230     B.MakeCompound(aCompound);
231     for (int i = 0; i < list->length(); i++) {
232       if (0 < list[i] && list[i] <= anIndices.Extent()) {
233         TopoDS_Shape aSubShape = anIndices.FindKey(list[i]);
234         B.Add(aCompound, aSubShape);
235       }
236     }
237
238     S = aCompound;
239   }
240   Bind(IOR, S);
241   return S;
242 }