1 // Copyright (C) 2007-2012 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
58 //=======================================================================
61 //=======================================================================
62 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
64 std::string hst_client = Kernel_Utils::GetHostname();
66 Engines::Container_var ctn_server = geom->GetContainerRef();
67 long pid_server = ctn_server->getPID();
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());
74 /* get sequence of bytes of resulting brep shape from GEOM server */
76 SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
77 /*int sizebuf = */SeqFile->length();
79 buf = (char*) &SeqFile[0];
80 std::istringstream streamBrep(buf);
81 BRep_Builder aBuilder;
82 BRepTools::Read(S, streamBrep, aBuilder);
87 //=======================================================================
88 // function : Create()
89 // purpose : Create in client not in a container
90 //=======================================================================
91 GEOM_Client::GEOM_Client()
101 //=======================================================================
102 // function : Create()
103 // purpose : Copy constructor
104 //=======================================================================
105 GEOM_Client::GEOM_Client(const GEOM_Client& client)
107 _mySubShapes = client._mySubShapes;
108 myShapesMap = client.myShapesMap;
109 pid_client = client.pid_client;
112 //=======================================================================
113 // function : Create()
115 //=======================================================================
116 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
118 pid_client = client->getPID();
121 //=======================================================================
122 // function : get_client()
123 // purpose : Static method to have the only one instance of GEOM_Client
124 //=======================================================================
126 GEOM_Client& GEOM_Client::get_client()
128 static GEOM_Client a;
132 GEOM_Client GEOM_Client::get_client()
134 return GEOM_Client();
138 //=======================================================================
141 //=======================================================================
142 Standard_Boolean GEOM_Client::Find (const TCollection_AsciiString& IOR, TopoDS_Shape& S)
144 if (myShapesMap.count(IOR) != 0) {
145 S = myShapesMap[IOR];
146 return Standard_True;
148 return Standard_False;
151 //=======================================================================
154 //=======================================================================
155 Standard_Boolean GEOM_Client::Find (const TopoDS_Shape& S, TCollection_AsciiString& IOR)
157 std::map< TCollection_AsciiString, TopoDS_Shape >::const_iterator it;
158 for (it = myShapesMap.begin(); it != myShapesMap.end(); ++it) {
159 if ((*it).second == S) {
161 return Standard_True;
164 return Standard_False;
167 //=======================================================================
170 //=======================================================================
171 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
173 myShapesMap[IOR] = S;
176 //=======================================================================
177 // function : RemoveShapeFromBuffer()
178 // purpose : Remove shape from Client Buffer
179 //=======================================================================
180 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
182 if (myShapesMap.size() == 0)
185 if (myShapesMap.count(IOR) != 0) {
186 myShapesMap.erase(IOR);
187 _mySubShapes.erase(IOR);
191 //=======================================================================
192 // function : ClearClientBuffer()
193 // purpose : purge buffer
194 //=======================================================================
195 void GEOM_Client::ClearClientBuffer()
197 if (myShapesMap.size() == 0)
200 _mySubShapes.clear();
204 //=======================================================================
205 // function : BufferLength()
207 //=======================================================================
208 unsigned int GEOM_Client::BufferLength()
210 return myShapesMap.size();
213 //=======================================================================
214 // function : GetShape()
216 //=======================================================================
217 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
220 CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
221 TCollection_AsciiString IOR = (char*)anIOR.in();
222 Standard_Boolean anIndex = Find(IOR, S);
224 if (anIndex) return S;
226 /******* in case of a MAIN GEOM::SHAPE ********/
227 if (aShape->IsMainShape()) {
228 S = Load(geom, aShape);
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();
238 CORBA::String_var aMainIOR = geom->GetStringFromIOR(aShape->GetMainShape());
239 TCollection_AsciiString mainIOR = (char*)aMainIOR.in();
241 //find subshapes only one time
242 if (_mySubShapes.count(mainIOR) == 0)
244 TopTools_IndexedMapOfShape anIndices;
245 TopExp::MapShapes(aMainShape, anIndices);
246 Standard_Integer ii = 1, nbSubSh = anIndices.Extent();
247 for (; ii <= nbSubSh; ii++)
249 _mySubShapes[mainIOR].push_back(anIndices.FindKey(ii));
253 /* Case of only one subshape */
254 if (list->length() == 1 && list[0] > 0) {
255 S = _mySubShapes[mainIOR][list[0]-1];
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);