Salome HOME
Revert "Synchronize adm files"
[modules/geom.git] / src / GEOMClient / GEOM_Client.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 //  File   : GEOM_Client.cxx
23 //  Author : Yves FRICAUD/Lucien PIGNOLONI
24 //  Module : GEOM
25
26 #include <Standard_Stream.hxx>
27
28 #include <Standard_Stream.hxx>
29
30 #include <sstream>
31
32 #include "GEOM_Client.hxx"
33 #include <SALOMEconfig.h>
34 #include "Basics_Utils.hxx"
35 #include "utilities.h"
36
37 #include <BRep_Builder.hxx>
38 #include <BRepTools.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Compound.hxx>
41 #include <TCollection_AsciiString.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopExp.hxx>
44 #include <TopAbs.hxx>
45 #include <TopTools_IndexedMapOfShape.hxx>
46
47 #ifdef WIN32
48 #include <process.h>
49 #else
50 #include <unistd.h>
51 #endif
52
53 #include CORBA_SERVER_HEADER(SALOMEDS)
54 #include CORBA_SERVER_HEADER(GEOM_Gen)
55
56 #define HST_CLIENT_LEN 256
57
58 //=======================================================================
59 // function : Load()
60 // purpose  :
61 //=======================================================================
62 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
63 {
64   std::string hst_client = Kernel_Utils::GetHostname();
65
66   Engines::Container_var ctn_server = geom->GetContainerRef();
67   long                   pid_server = ctn_server->getPID();
68
69   CORBA::String_var hostname = ctn_server->getHostName();
70   if ( pid_client == pid_server && !strcmp(hst_client.c_str(), hostname.in()) ) {
71     TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
72     return(*S);
73   } else {
74     /* get sequence of bytes of resulting brep shape from GEOM server */
75     TopoDS_Shape S;
76     SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
77     /*int sizebuf = */SeqFile->length();
78     char* buf;
79     buf = (char*) &SeqFile[0];
80     std::istringstream streamBrep(buf);
81     BRep_Builder aBuilder;
82     BRepTools::Read(S, streamBrep, aBuilder);
83     return(S);
84   }
85 }
86
87 //=======================================================================
88 // function : Create()
89 // purpose  : Create in client not in a container
90 //=======================================================================
91 GEOM_Client::GEOM_Client()
92 {
93   pid_client =
94 #ifdef WIN32
95     (long)_getpid();
96 #else
97     (long)getpid();
98 #endif
99 }
100
101 //=======================================================================
102 // function : Create()
103 // purpose  : Copy constructor
104 //=======================================================================
105 GEOM_Client::GEOM_Client(const GEOM_Client& client)
106 {
107   _mySubShapes = client._mySubShapes;
108   myShapesMap = client.myShapesMap;
109   pid_client = client.pid_client;
110 }
111
112 //=======================================================================
113 // function : Create()
114 // purpose  :
115 //=======================================================================
116 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
117 {
118   pid_client = client->getPID();
119 }
120
121 //=======================================================================
122 // function : get_client()
123 // purpose  : Static method to have the only one instance of GEOM_Client
124 //=======================================================================
125 #ifdef SINGLE_CLIENT
126 GEOM_Client& GEOM_Client::get_client()
127 {
128   static GEOM_Client a;
129   return a;
130 }
131 #else
132 GEOM_Client GEOM_Client::get_client()
133 {
134   return GEOM_Client();
135 }
136 #endif
137
138 //=======================================================================
139 // function : Find()
140 // purpose  :
141 //=======================================================================
142 Standard_Boolean GEOM_Client::Find (const TCollection_AsciiString& IOR, TopoDS_Shape& S)
143 {
144   if (myShapesMap.count(IOR) != 0) {
145     S = myShapesMap[IOR];
146     return Standard_True;
147   }
148   return Standard_False;
149 }
150
151 //=======================================================================
152 // function : Find()
153 // purpose  :
154 //=======================================================================
155 Standard_Boolean GEOM_Client::Find (const TopoDS_Shape& S, TCollection_AsciiString& IOR)
156 {
157   std::map< TCollection_AsciiString, TopoDS_Shape >::const_iterator it;
158   for (it = myShapesMap.begin(); it != myShapesMap.end(); ++it) {
159     if ((*it).second == S) {
160       IOR = (*it).first;
161       return Standard_True;
162     }
163   }
164   return Standard_False;
165 }
166
167 //=======================================================================
168 // function : Bind()
169 // purpose  :
170 //=======================================================================
171 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
172 {
173   myShapesMap[IOR] = S;
174 }
175
176 //=======================================================================
177 // function : RemoveShapeFromBuffer()
178 // purpose  : Remove shape from Client Buffer
179 //=======================================================================
180 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
181 {
182   if (myShapesMap.size() == 0)
183     return;
184
185   if (myShapesMap.count(IOR) != 0) {
186     myShapesMap.erase(IOR);
187     _mySubShapes.erase(IOR);
188   }
189 }
190
191 //=======================================================================
192 // function : ClearClientBuffer()
193 // purpose  : purge buffer
194 //=======================================================================
195 void GEOM_Client::ClearClientBuffer()
196 {
197   if (myShapesMap.size() == 0)
198     return;
199
200   _mySubShapes.clear();
201   myShapesMap.clear();
202 }
203
204 //=======================================================================
205 // function : BufferLength()
206 // purpose  :
207 //=======================================================================
208 unsigned int GEOM_Client::BufferLength()
209 {
210   return myShapesMap.size();
211 }
212
213 //=======================================================================
214 // function : GetShape()
215 // purpose  :
216 //=======================================================================
217 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
218 {
219   TopoDS_Shape S;
220   CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
221   TCollection_AsciiString IOR = (char*)anIOR.in();
222   Standard_Boolean anIndex = Find(IOR, S);
223
224   if (anIndex) return S;
225
226   /******* in case of a MAIN GEOM::SHAPE ********/
227   if (aShape->IsMainShape()) {
228     S = Load(geom, aShape);
229     Bind(IOR, S);
230     return S;
231   }
232
233   /******* in case of SUB GEOM::SHAPE ***********/
234   // Load and Explore the Main Shape
235   TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
236   GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
237
238   CORBA::String_var aMainIOR = geom->GetStringFromIOR(aShape->GetMainShape());
239   TCollection_AsciiString mainIOR = (char*)aMainIOR.in();
240
241   //find subshapes only one time
242   if (_mySubShapes.count(mainIOR) == 0)
243   {
244     TopTools_IndexedMapOfShape anIndices;
245     TopExp::MapShapes(aMainShape, anIndices);
246     Standard_Integer ii = 1, nbSubSh = anIndices.Extent();
247     for (; ii <= nbSubSh; ii++)
248     {
249       _mySubShapes[mainIOR].push_back(anIndices.FindKey(ii));
250     }
251   }
252
253   /* Case of only one subshape */
254   if (list->length() == 1 && list[0] > 0) {
255     S = _mySubShapes[mainIOR][list[0]-1];
256   }
257   else {
258     BRep_Builder B;
259     TopoDS_Compound aCompound;
260     B.MakeCompound(aCompound);
261     for (int i = 0; i < list->length(); i++) {
262       if (0 < list[i] && list[i] <= _mySubShapes[mainIOR].size()) {
263         TopoDS_Shape aSubShape = _mySubShapes[mainIOR][list[i]-1];
264         B.Add(aCompound, aSubShape);
265       }
266     }
267
268     S = aCompound;
269   }
270   Bind(IOR, S);
271   return S;
272 }