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