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