1 // Copyright (C) 2007-2011 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 // File : GEOM_Client.cxx
23 // Author : Yves FRICAUD/Lucien PIGNOLONI
26 #include <Standard_Stream.hxx>
28 #include <Standard_Stream.hxx>
32 #include "GEOM_Client.hxx"
33 #include <SALOMEconfig.h>
34 #include "Basics_Utils.hxx"
35 #include "utilities.h"
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>
45 #include <TopTools_IndexedMapOfShape.hxx>
53 #include CORBA_SERVER_HEADER(SALOMEDS)
54 #include CORBA_SERVER_HEADER(GEOM_Gen)
56 #define HST_CLIENT_LEN 256
59 //=======================================================================
62 //=======================================================================
63 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
65 std::string hst_client = Kernel_Utils::GetHostname();
67 Engines::Container_var ctn_server = geom->GetContainerRef();
68 long pid_server = ctn_server->getPID();
70 CORBA::String_var hostname = ctn_server->getHostName();
71 if ( pid_client == pid_server && !strcmp(hst_client.c_str(), hostname.in()) ) {
72 TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
75 /* get sequence of bytes of resulting brep shape from GEOM server */
77 SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
78 /*int sizebuf = */SeqFile->length();
80 buf = (char*) &SeqFile[0];
81 std::istringstream streamBrep(buf);
82 BRep_Builder aBuilder;
83 BRepTools::Read(S, streamBrep, aBuilder);
88 //=======================================================================
89 // function : Create()
90 // purpose : Create in client not in a container
91 //=======================================================================
92 GEOM_Client::GEOM_Client()
102 //=======================================================================
103 // function : Create()
104 // purpose : Copy constructor
105 //=======================================================================
106 GEOM_Client::GEOM_Client(const GEOM_Client& client)
108 myIORs = client.myIORs;
109 myShapes = client.myShapes;
110 _myIndexes = client._myIndexes;
111 _mySubShapes = client._mySubShapes;
112 pid_client = client.pid_client;
115 //=======================================================================
116 // function : Create()
118 //=======================================================================
119 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
121 pid_client = client->getPID();
124 //=======================================================================
125 // function : get_client()
126 // purpose : Static method to have the only one instance of GEOM_Client
127 //=======================================================================
129 GEOM_Client& GEOM_Client::get_client()
131 static GEOM_Client a;
135 GEOM_Client GEOM_Client::get_client()
137 return GEOM_Client();
141 //=======================================================================
144 //=======================================================================
145 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
147 if (_myIndexes.count(IOR) != 0)
149 Standard_Integer i = _myIndexes[IOR];
150 S = myShapes.Value(i);
156 //=======================================================================
159 //=======================================================================
160 Standard_Integer GEOM_Client::Find( const TopoDS_Shape& S, TCollection_AsciiString& IOR )
162 for ( Standard_Integer i = 1; i<= myShapes.Length(); i++ ) {
163 if (myShapes.Value(i) == S) {
164 IOR = myIORs.Value(i);
171 //=======================================================================
174 //=======================================================================
175 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
179 _myIndexes[IOR] = myIORs.Length();
182 //=======================================================================
183 // function : RemoveShapeFromBuffer()
184 // purpose : Remove shape from Client Buffer
185 //=======================================================================
186 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
188 if( myIORs.IsEmpty() )
192 Standard_Integer anIndex = Find( IOR, S );
194 myIORs.Remove(anIndex);
195 myShapes.Remove(anIndex);
196 _myIndexes.erase(IOR);
197 _mySubShapes.erase(IOR);
201 //=======================================================================
202 // function : ClearClientBuffer()
203 // purpose : purge buffer
204 //=======================================================================
205 void GEOM_Client::ClearClientBuffer()
207 if( myIORs.IsEmpty() )
212 _mySubShapes.clear();
216 //=======================================================================
217 // function : BufferLength()
219 //=======================================================================
220 unsigned int GEOM_Client::BufferLength()
222 return myIORs.Length();
225 //=======================================================================
226 // function : GetShape()
228 //=======================================================================
229 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
232 CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
233 TCollection_AsciiString IOR = (char*)anIOR.in();
234 Standard_Integer anIndex = Find(IOR, S);
236 if (anIndex != 0) return S;
238 /******* in case of a MAIN GEOM::SHAPE ********/
239 if (aShape->IsMainShape()) {
240 S = Load(geom, aShape);
245 /******* in case of SUB GEOM::SHAPE ***********/
246 // Load and Explore the Main Shape
247 TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
248 GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
250 CORBA::String_var aMainIOR = geom->GetStringFromIOR(aShape->GetMainShape());
251 TCollection_AsciiString mainIOR = (char*)aMainIOR.in();
253 //find subshapes only one time
254 if(_mySubShapes.count(mainIOR)==0)
256 TopTools_IndexedMapOfShape anIndices;
257 TopExp::MapShapes(aMainShape, anIndices);
258 Standard_Integer ii = 1, nbSubSh = anIndices.Extent();
259 for (; ii <= nbSubSh; ii++)
261 _mySubShapes[mainIOR].push_back(anIndices.FindKey(ii));
265 /* Case of only one subshape */
266 if (list->length() == 1 && list[0] > 0) {
267 S = _mySubShapes[mainIOR][list[0]-1];
271 TopoDS_Compound aCompound;
272 B.MakeCompound(aCompound);
273 for (int i = 0; i < list->length(); i++) {
274 if (0 < list[i] && list[i] <= _mySubShapes[mainIOR].size()) {
275 TopoDS_Shape aSubShape = _mySubShapes[mainIOR][list[i]-1];
276 B.Add(aCompound, aSubShape);