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