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
23 // GEOM GEOMClient : tool to transfer BREP files from GEOM server to GEOM client
24 // File : GEOM_Client.cxx
25 // Author : Yves FRICAUD/Lucien PIGNOLONI
29 #include <Standard_Stream.hxx>
31 #include <Standard_Stream.hxx>
35 #include "GEOM_Client.hxx"
36 #include <SALOMEconfig.h>
37 #include "Basics_Utils.hxx"
38 #include "utilities.h"
40 #include <BRep_Builder.hxx>
41 #include <BRepTools.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <TCollection_AsciiString.hxx>
45 #include <TopExp_Explorer.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
56 #include CORBA_SERVER_HEADER(SALOMEDS)
57 #include CORBA_SERVER_HEADER(GEOM_Gen)
59 #define HST_CLIENT_LEN 256
62 //=======================================================================
65 //=======================================================================
66 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
68 std::string hst_client = Kernel_Utils::GetHostname();
70 Engines::Container_var ctn_server = geom->GetContainerRef();
71 long pid_server = ctn_server->getPID();
73 CORBA::String_var hostname = ctn_server->getHostName();
74 if ( pid_client == pid_server && !strcmp(hst_client.c_str(), hostname.in()) ) {
75 TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
78 /* get sequence of bytes of resulting brep shape from GEOM server */
80 SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
81 int sizebuf = SeqFile->length();
83 buf = (char*) &SeqFile[0];
84 std::istringstream streamBrep(buf);
85 BRep_Builder aBuilder;
86 BRepTools::Read(S, streamBrep, aBuilder);
91 //=======================================================================
92 // function : Create()
93 // purpose : Create in client not in a container
94 //=======================================================================
95 GEOM_Client::GEOM_Client()
105 //=======================================================================
106 // function : Create()
108 //=======================================================================
109 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
111 pid_client = client->getPID();
114 //=======================================================================
117 //=======================================================================
118 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
120 for ( Standard_Integer i = 1; i<= myIORs.Length(); i++ ) {
121 if (myIORs.Value(i).IsEqual(IOR)) {
122 S = myShapes.Value(i);
129 //=======================================================================
132 //=======================================================================
133 Standard_Integer GEOM_Client::Find( const TopoDS_Shape& S, TCollection_AsciiString& IOR )
135 for ( Standard_Integer i = 1; i<= myShapes.Length(); i++ ) {
136 if (myShapes.Value(i) == S) {
137 IOR = myIORs.Value(i);
144 //=======================================================================
147 //=======================================================================
148 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
154 //=======================================================================
155 // function : RemoveShapeFromBuffer()
156 // purpose : Remove shape from Client Buffer
157 //=======================================================================
158 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
160 if( myIORs.IsEmpty() )
164 Standard_Integer anIndex = Find( IOR, S );
166 myIORs.Remove(anIndex);
167 myShapes.Remove(anIndex);
172 //=======================================================================
173 // function : ClearClientBuffer()
174 // purpose : purge buffer
175 //=======================================================================
176 void GEOM_Client::ClearClientBuffer()
178 if( myIORs.IsEmpty() )
185 //=======================================================================
186 // function : BufferLength()
188 //=======================================================================
189 unsigned int GEOM_Client::BufferLength()
191 return myIORs.Length();
194 //=======================================================================
195 // function : GetShape()
197 //=======================================================================
198 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
201 CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
202 TCollection_AsciiString IOR = (char*)anIOR.in();
203 Standard_Integer anIndex = Find(IOR, S);
205 if (anIndex != 0) return S;
207 /******* in case of a MAIN GEOM::SHAPE ********/
208 if (aShape->IsMainShape()) {
209 S = Load(geom, aShape);
214 /******* in case of SUB GEOM::SHAPE ***********/
215 // Load and Explore the Main Shape
216 TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
217 GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
219 TopTools_IndexedMapOfShape anIndices;
220 TopExp::MapShapes(aMainShape, anIndices);
222 /* Case of only one subshape */
223 if (list->length() == 1 && list[0] > 0) {
224 S = anIndices.FindKey(list[0]);
228 TopoDS_Compound aCompound;
229 B.MakeCompound(aCompound);
230 for (int i = 0; i < list->length(); i++) {
231 if (0 < list[i] && list[i] <= anIndices.Extent()) {
232 TopoDS_Shape aSubShape = anIndices.FindKey(list[i]);
233 B.Add(aCompound, aSubShape);