Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[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 "utilities.h"
33
34 #include CORBA_SERVER_HEADER(GEOM_Gen)
35
36 #include <BRep_Builder.hxx>
37 #include <BRepTools.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <TCollection_AsciiString.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopAbs.hxx>
43 #include <TColStd_MapOfInteger.hxx>
44 #include <TopoDS_Iterator.hxx>
45 #include <TopTools_MapOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
48
49
50 #include <unistd.h>
51
52 #define HST_CLIENT_LEN 256
53
54
55 //=======================================================================
56 // function : Load()
57 // purpose  : 
58 //=======================================================================
59 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Shape_ptr aShape )
60 {
61     char hst_client[HST_CLIENT_LEN];
62     gethostname(hst_client, HST_CLIENT_LEN);
63
64     Engines::Container_var ctn_server = geom->GetContainerRef();
65     long                   pid_server = ctn_server->getPID();
66
67     if ( (pid_client==pid_server) && (strcmp(hst_client, ctn_server->getHostName())==0) ) {
68         TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
69         return(*S);
70     } else {
71         /* get sequence of bytes of resulting brep shape from GEOM server */
72         TopoDS_Shape S;
73         GEOM::GEOM_Shape::TMPFile_var SeqFile = aShape->GetShapeStream();
74         int sizebuf = SeqFile->length();
75         char* buf;
76         buf = (char*) &SeqFile[0];
77         istrstream streamBrep(buf,sizebuf);
78         BRep_Builder aBuilder;
79         BRepTools::Read(S, streamBrep, aBuilder);
80         return(S);
81     };
82 }
83
84
85 //=======================================================================
86 // function : Create()
87 // purpose  : Create in client not in a container
88 //=======================================================================
89 GEOM_Client::GEOM_Client()
90 {
91   pid_client = (-1);
92 }
93
94 //=======================================================================
95 // function : Create()
96 // purpose  : 
97 //=======================================================================
98 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
99 {
100   pid_client = client->getPID();
101 }
102
103 //=======================================================================
104 // function : Find()
105 // purpose  : 
106 //=======================================================================
107 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
108 {
109   for ( Standard_Integer i = 1; i<= myIORs.Length(); i++ ) {
110     if (myIORs.Value(i).IsEqual(IOR)) {
111       S = myShapes.Value(i);
112       return i;
113     }
114   }
115   return 0;
116 }
117
118
119 //=======================================================================
120 // function : Bind()
121 // purpose  :
122 //=======================================================================
123 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
124 {
125   myIORs.Append(IOR);
126   myShapes.Append(S);
127 }
128
129 //=======================================================================
130 // function : RemoveShapeFromBuffer()
131 // purpose  : Remove shape from Client Buffer
132 //=======================================================================
133 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& shapeIOR )
134 {
135   if( myIORs.IsEmpty() )
136     return ;
137   
138   TopoDS_Shape S ;
139   Standard_Integer anIndex = Find( shapeIOR, S ) ;
140   if( anIndex != 0 ) {
141     myIORs.Remove(anIndex) ;
142     myShapes.Remove(anIndex) ;
143   }
144   return ;
145 }
146
147
148 //=======================================================================
149 // function : ClearClientBuffer()
150 // purpose  : purge buffer
151 //=======================================================================
152 void GEOM_Client::ClearClientBuffer()
153 {
154   if( myIORs.IsEmpty() )
155     return ;
156   myIORs.Clear() ;
157   myShapes.Clear() ;
158   return ;
159 }
160
161 //=======================================================================
162 // function : BufferLength()
163 // purpose  :
164 //=======================================================================
165 unsigned int GEOM_Client::BufferLength()
166 {
167   return myIORs.Length() ;
168 }
169
170
171 //=======================================================================
172 // function : GetShape()
173 // purpose  : 
174 //=======================================================================
175
176 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Shape_ptr aShape ) 
177
178   
179   TopoDS_Shape            S; 
180   TCollection_AsciiString IOR(aShape->Name()); 
181   Standard_Integer        anIndex = Find(IOR, S); 
182   
183   BRep_Builder B; 
184
185   if (anIndex !=0 ) { 
186     return S ; 
187    } 
188
189   /******* in case of a MAIN GEOM::SHAPE ********/ 
190   if (aShape->IsMainShape()) { 
191     S = Load(geom, aShape); 
192     Bind(IOR,S); 
193     return S; 
194   } 
195
196   /******* in case of SUB GEOM::SHAPE ***********/ 
197   // Load and Explore the Main Shape 
198   TopoDS_Shape MainShape = GetShape (geom, geom->GetIORFromString(aShape->MainName())); 
199   GEOM::GEOM_Shape::ListOfSubShapeID_var list = aShape->Index(); 
200
201   Standard_Integer j = 1; 
202   TopExp_Explorer exp; 
203   TopAbs_ShapeEnum ShapeType = TopAbs_ShapeEnum(aShape->ShapeType()); 
204   
205   /* Case of only one subshape */ 
206   if (list->length() == 1) 
207   { 
208     if (ShapeType == TopAbs_COMPOUND) 
209       { 
210         TopoDS_Iterator it; 
211         TopTools_ListOfShape CL; 
212         CL.Append( MainShape ); 
213         TopTools_ListIteratorOfListOfShape itC; 
214         for (itC.Initialize( CL ); itC.More(); itC.Next()) 
215           { 
216             for (it.Initialize( itC.Value() );  it.More(); it.Next()) 
217               { 
218                 if ( it.Value().ShapeType() == TopAbs_COMPOUND) 
219                   {
220                     if (j == list[0]) 
221                       { 
222                         S = it.Value(); 
223                         Bind(IOR, S); 
224                         return S; 
225                       } 
226                     j++; 
227                     CL.Append( it.Value() ); 
228                   }
229               } 
230           } 
231       } 
232     else 
233       { 
234         TopTools_MapOfShape M; 
235         for (exp.Init(MainShape, ShapeType); exp.More(); exp.Next()) { 
236           if ( M.Add(exp.Current()) ) 
237             { 
238               if (j == list[0]) 
239                 { 
240                   S = exp.Current(); 
241                   Bind(IOR, S); 
242                   return S; 
243                 } 
244               j++; 
245             } 
246         } 
247       } 
248   } 
249   
250   /* Case of a compound containing two or more sub shapes (not a main shape compound !)   */ 
251
252   /* Warning : the compound when representing sub shapes must be explored in a sub type   */ 
253   /* that is NOT ShapeType=aShape->ShapeType()= TopAbs_COMPOUND !                         */ 
254   /* We have to retrieve the exact sub type of shapes contained in the compound first !   */ 
255   TopoDS_Iterator it ; 
256   TopAbs_ShapeEnum exactSubType ; 
257   S = Load( geom, aShape ); 
258   it.Initialize( S, true, true ) ; 
259   it.More(); 
260   exactSubType = it.Value().ShapeType() ; 
261
262   TColStd_MapOfInteger MapIndex; 
263   Standard_Integer nbSS = list->length(); 
264   TopoDS_Compound Comp; 
265   B.MakeCompound(Comp); 
266   
267   for (Standard_Integer i=1; i<=nbSS; i++) 
268     MapIndex.Add(list[i-1]); 
269   
270   for (exp.Init(MainShape, exactSubType), j=1; exp.More() ; exp.Next(), j++) { 
271     if ( MapIndex.Contains(j) ) { 
272       B.Add( Comp, exp.Current() ); 
273     } 
274   } 
275   Bind(IOR, Comp); 
276   return Comp; 
277