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