1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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
27 #include <Standard_Stream.hxx>
29 #include <Standard_Stream.hxx>
33 #include "GEOM_Client.hxx"
34 #include <SALOMEconfig.h>
35 #include "Basics_Utils.hxx"
36 #include "utilities.h"
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>
46 #include <TopTools_IndexedMapOfShape.hxx>
54 #include CORBA_SERVER_HEADER(SALOMEDS)
55 #include CORBA_SERVER_HEADER(GEOM_Gen)
57 #define HST_CLIENT_LEN 256
60 //=======================================================================
63 //=======================================================================
64 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
66 std::string hst_client = Kernel_Utils::GetHostname();
68 Engines::Container_var ctn_server = geom->GetContainerRef();
69 long pid_server = ctn_server->getPID();
71 CORBA::String_var hostname = ctn_server->getHostName();
72 if ( pid_client == pid_server && !strcmp(hst_client.c_str(), hostname.in()) ) {
73 TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
76 /* get sequence of bytes of resulting brep shape from GEOM server */
78 SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
79 /*int sizebuf = */SeqFile->length();
81 buf = (char*) &SeqFile[0];
82 std::istringstream streamBrep(buf);
83 BRep_Builder aBuilder;
84 BRepTools::Read(S, streamBrep, aBuilder);
89 //=======================================================================
90 // function : Create()
91 // purpose : Create in client not in a container
92 //=======================================================================
93 GEOM_Client::GEOM_Client()
103 //=======================================================================
104 // function : Create()
105 // purpose : Copy constructor
106 //=======================================================================
107 GEOM_Client::GEOM_Client(const GEOM_Client& client)
109 myIORs = client.myIORs;
110 myShapes = client.myShapes;
111 _myIndexes = client._myIndexes;
112 _mySubShapes = client._mySubShapes;
113 pid_client = client.pid_client;
116 //=======================================================================
117 // function : Create()
119 //=======================================================================
120 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
122 pid_client = client->getPID();
125 //=======================================================================
126 // function : get_client()
127 // purpose : Static method to have the only one instance of GEOM_Client
128 //=======================================================================
130 GEOM_Client& GEOM_Client::get_client()
132 static GEOM_Client a;
136 GEOM_Client GEOM_Client::get_client()
138 return GEOM_Client();
142 //=======================================================================
145 //=======================================================================
146 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
148 if (_myIndexes.count(IOR) != 0)
150 Standard_Integer i = _myIndexes[IOR];
151 S = myShapes.Value(i);
157 //=======================================================================
160 //=======================================================================
161 Standard_Integer GEOM_Client::Find( const TopoDS_Shape& S, TCollection_AsciiString& IOR )
163 for ( Standard_Integer i = 1; i<= myShapes.Length(); i++ ) {
164 if (myShapes.Value(i) == S) {
165 IOR = myIORs.Value(i);
172 //=======================================================================
175 //=======================================================================
176 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
180 _myIndexes[IOR] = myIORs.Length();
183 //=======================================================================
184 // function : RemoveShapeFromBuffer()
185 // purpose : Remove shape from Client Buffer
186 //=======================================================================
187 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
189 if( myIORs.IsEmpty() )
193 Standard_Integer anIndex = Find( IOR, S );
195 myIORs.Remove(anIndex);
196 myShapes.Remove(anIndex);
197 _myIndexes.erase(IOR);
198 _mySubShapes.erase(IOR);
202 //=======================================================================
203 // function : ClearClientBuffer()
204 // purpose : purge buffer
205 //=======================================================================
206 void GEOM_Client::ClearClientBuffer()
208 if( myIORs.IsEmpty() )
213 _mySubShapes.clear();
217 //=======================================================================
218 // function : BufferLength()
220 //=======================================================================
221 unsigned int GEOM_Client::BufferLength()
223 return myIORs.Length();
226 //=======================================================================
227 // function : GetShape()
229 //=======================================================================
230 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
233 CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
234 TCollection_AsciiString IOR = (char*)anIOR.in();
235 Standard_Integer anIndex = Find(IOR, S);
237 if (anIndex != 0) return S;
239 /******* in case of a MAIN GEOM::SHAPE ********/
240 if (aShape->IsMainShape()) {
241 S = Load(geom, aShape);
246 /******* in case of SUB GEOM::SHAPE ***********/
247 // Load and Explore the Main Shape
248 TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
249 GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
251 CORBA::String_var aMainIOR = geom->GetStringFromIOR(aShape->GetMainShape());
252 TCollection_AsciiString mainIOR = (char*)aMainIOR.in();
254 //find subshapes only one time
255 if(_mySubShapes.count(mainIOR)==0)
257 TopTools_IndexedMapOfShape anIndices;
258 TopExp::MapShapes(aMainShape, anIndices);
259 Standard_Integer ii = 1, nbSubSh = anIndices.Extent();
260 for (; ii <= nbSubSh; ii++)
262 _mySubShapes[mainIOR].push_back(anIndices.FindKey(ii));
266 /* Case of only one subshape */
267 if (list->length() == 1 && list[0] > 0) {
268 S = _mySubShapes[mainIOR][list[0]-1];
272 TopoDS_Compound aCompound;
273 B.MakeCompound(aCompound);
274 for (int i = 0; i < list->length(); i++) {
275 if (0 < list[i] && list[i] <= _mySubShapes[mainIOR].size()) {
276 TopoDS_Shape aSubShape = _mySubShapes[mainIOR][list[i]-1];
277 B.Add(aCompound, aSubShape);