Salome HOME
Update mail address
[modules/geom.git] / src / GEOMClient / GEOM_Client.cxx
1 //  GEOM GEOMClient : tool to transfer BREP files from GEOM server to GEOM client
2 //
3 //  Copyright (C) 2003  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 //
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 <strstream>
34
35 #include "GEOM_Client.hxx"
36 #include <SALOMEconfig.h>
37 #include "OpUtil.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 //=======================================================================
64 // function : Load()
65 // purpose  : 
66 //=======================================================================
67 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
68 {
69     string hst_client = GetHostname();
70
71     Engines::Container_var ctn_server = geom->GetContainerRef();
72     long                   pid_server = ctn_server->getPID();
73  
74    if ( (pid_client==pid_server) && (strcmp(hst_client.c_str(), ctn_server->getHostName())==0) ) {
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         istrstream streamBrep(buf,sizebuf);
85         BRep_Builder aBuilder;
86         BRepTools::Read(S, streamBrep, aBuilder);
87         return(S);
88     };
89 }
90
91
92 //=======================================================================
93 // function : Create()
94 // purpose  : Create in client not in a container
95 //=======================================================================
96 GEOM_Client::GEOM_Client()
97 {
98   pid_client = 
99 #ifdef WNT
100     (long)_getpid();
101 #else
102     (long)getpid();
103 #endif
104 }
105
106 //=======================================================================
107 // function : Create()
108 // purpose  : 
109 //=======================================================================
110 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
111 {
112   pid_client = client->getPID();
113 }
114
115 //=======================================================================
116 // function : Find()
117 // purpose  : 
118 //=======================================================================
119 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
120 {
121   for ( Standard_Integer i = 1; i<= myIORs.Length(); i++ ) {
122     if (myIORs.Value(i).IsEqual(IOR)) {
123       S = myShapes.Value(i);
124       return i;
125     }
126   }
127   return 0;
128 }
129
130 //=======================================================================
131 // function : Find()
132 // purpose  : 
133 //=======================================================================
134 Standard_Integer GEOM_Client::Find( const TopoDS_Shape& S, TCollection_AsciiString& IOR )
135 {
136   for ( Standard_Integer i = 1; i<= myShapes.Length(); i++ ) {
137     if (myShapes.Value(i) == S) {
138       IOR = myIORs.Value(i);
139       return i;
140     }
141   }
142   return 0;
143 }
144
145 //=======================================================================
146 // function : Bind()
147 // purpose  :
148 //=======================================================================
149 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
150 {
151   myIORs.Append(IOR);
152   myShapes.Append(S);
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   }
170   return ;
171 }
172
173
174 //=======================================================================
175 // function : ClearClientBuffer()
176 // purpose  : purge buffer
177 //=======================================================================
178 void GEOM_Client::ClearClientBuffer()
179 {
180   if( myIORs.IsEmpty() )
181     return ;
182   myIORs.Clear() ;
183   myShapes.Clear() ;
184   return ;
185 }
186
187 //=======================================================================
188 // function : BufferLength()
189 // purpose  :
190 //=======================================================================
191 unsigned int GEOM_Client::BufferLength()
192 {
193   return myIORs.Length() ;
194 }
195
196
197 //=======================================================================
198 // function : GetShape()
199 // purpose  : 
200 //=======================================================================
201 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
202 {
203   TopoDS_Shape S;
204   TCollection_AsciiString IOR = geom->GetStringFromIOR(aShape);
205   Standard_Integer anIndex = Find(IOR, S);
206
207   if (anIndex != 0) return S;
208
209   /******* in case of a MAIN GEOM::SHAPE ********/
210   if (aShape->IsMainShape()) {
211     S = Load(geom, aShape);
212     Bind(IOR, S);
213     return S;
214   }
215
216   /******* in case of SUB GEOM::SHAPE ***********/
217   // Load and Explore the Main Shape
218   TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
219   GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
220
221   TopTools_IndexedMapOfShape anIndices;
222   TopExp::MapShapes(aMainShape, anIndices);
223
224   /* Case of only one subshape */
225   if (list->length() == 1) {
226     S = anIndices.FindKey(list[0]);
227   }
228   else {
229     BRep_Builder B;
230     TopoDS_Compound aCompound;
231     B.MakeCompound(aCompound);
232     for (int i = 0; i < list->length(); i++) {
233       if (0 < list[i] && list[i] <= anIndices.Extent()) {
234         TopoDS_Shape aSubShape = anIndices.FindKey(list[i]);
235         B.Add(aCompound, aSubShape);
236       }
237     }
238
239     S = aCompound;
240   }
241   Bind(IOR, S);
242   return S;
243 }