1 // GEOM GEOMClient : tool to transfer BREP files from GEOM server to GEOM client
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GEOM_Client.cxx
25 // Author : Yves FRICAUD/Lucien PIGNOLONI
30 #include "GEOM_Client.hxx"
31 #include <SALOMEconfig.h>
32 #include "utilities.h"
34 #include CORBA_SERVER_HEADER(GEOM_Gen)
36 #include <BRep_Builder.hxx>
37 #include <BRepTools.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Compound.hxx>
40 #include <TCollection_AsciiString.hxx>
41 #include <TopExp_Explorer.hxx>
43 #include <TColStd_MapOfInteger.hxx>
44 #include <TopoDS_Iterator.hxx>
45 #include <TopTools_MapOfShape.hxx>
46 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
52 #define HST_CLIENT_LEN 256
55 //=======================================================================
58 //=======================================================================
59 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Shape_ptr aShape )
61 char hst_client[HST_CLIENT_LEN];
62 gethostname(hst_client, HST_CLIENT_LEN);
64 Engines::Container_var ctn_server = geom->GetContainerRef();
65 long pid_server = ctn_server->getPID();
67 if ( (pid_client==pid_server) && (strcmp(hst_client, ctn_server->getHostName())==0) ) {
68 TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
71 /* get sequence of bytes of resulting brep shape from GEOM server */
73 GEOM::GEOM_Shape::TMPFile_var SeqFile = aShape->GetShapeStream();
74 int sizebuf = SeqFile->length();
76 buf = (char*) &SeqFile[0];
77 istrstream streamBrep(buf,sizebuf);
78 BRep_Builder aBuilder;
79 BRepTools::Read(S, streamBrep, aBuilder);
85 //=======================================================================
86 // function : Create()
87 // purpose : Create in client not in a container
88 //=======================================================================
89 GEOM_Client::GEOM_Client()
94 //=======================================================================
95 // function : Create()
97 //=======================================================================
98 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
100 pid_client = client->getPID();
103 //=======================================================================
106 //=======================================================================
107 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
109 for ( Standard_Integer i = 1; i<= myIORs.Length(); i++ ) {
110 if (myIORs.Value(i).IsEqual(IOR)) {
111 S = myShapes.Value(i);
119 //=======================================================================
122 //=======================================================================
123 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
129 //=======================================================================
130 // function : RemoveShapeFromBuffer()
131 // purpose : Remove shape from Client Buffer
132 //=======================================================================
133 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& shapeIOR )
135 if( myIORs.IsEmpty() )
139 Standard_Integer anIndex = Find( shapeIOR, S ) ;
141 myIORs.Remove(anIndex) ;
142 myShapes.Remove(anIndex) ;
148 //=======================================================================
149 // function : ClearClientBuffer()
150 // purpose : purge buffer
151 //=======================================================================
152 void GEOM_Client::ClearClientBuffer()
154 if( myIORs.IsEmpty() )
161 //=======================================================================
162 // function : BufferLength()
164 //=======================================================================
165 unsigned int GEOM_Client::BufferLength()
167 return myIORs.Length() ;
171 //=======================================================================
172 // function : GetShape()
174 //=======================================================================
176 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Shape_ptr aShape )
180 TCollection_AsciiString IOR(aShape->Name());
181 Standard_Integer anIndex = Find(IOR, S);
189 /******* in case of a MAIN GEOM::SHAPE ********/
190 if (aShape->IsMainShape()) {
191 S = Load(geom, aShape);
196 /******* in case of SUB GEOM::SHAPE ***********/
197 // Load and Explore the Main Shape
198 TopoDS_Shape MainShape = GetShape (geom, geom->GetIORFromString(aShape->MainName()));
199 GEOM::GEOM_Shape::ListOfSubShapeID_var list = aShape->Index();
201 Standard_Integer j = 1;
203 TopAbs_ShapeEnum ShapeType = TopAbs_ShapeEnum(aShape->ShapeType());
205 /* Case of only one subshape */
206 if (list->length() == 1)
208 if (ShapeType == TopAbs_COMPOUND)
211 TopTools_ListOfShape CL;
212 CL.Append( MainShape );
213 TopTools_ListIteratorOfListOfShape itC;
214 for (itC.Initialize( CL ); itC.More(); itC.Next())
216 for (it.Initialize( itC.Value() ); it.More(); it.Next())
218 if ( it.Value().ShapeType() == TopAbs_COMPOUND)
227 CL.Append( it.Value() );
234 TopTools_MapOfShape M;
235 for (exp.Init(MainShape, ShapeType); exp.More(); exp.Next()) {
236 if ( M.Add(exp.Current()) )
250 /* Case of a compound containing two or more sub shapes (not a main shape compound !) */
252 /* Warning : the compound when representing sub shapes must be explored in a sub type */
253 /* that is NOT ShapeType=aShape->ShapeType()= TopAbs_COMPOUND ! */
254 /* We have to retrieve the exact sub type of shapes contained in the compound first ! */
256 TopAbs_ShapeEnum exactSubType ;
257 S = Load( geom, aShape );
258 it.Initialize( S, true, true ) ;
260 exactSubType = it.Value().ShapeType() ;
262 TColStd_MapOfInteger MapIndex;
263 Standard_Integer nbSS = list->length();
264 TopoDS_Compound Comp;
265 B.MakeCompound(Comp);
267 for (Standard_Integer i=1; i<=nbSS; i++)
268 MapIndex.Add(list[i-1]);
270 for (exp.Init(MainShape, exactSubType), j=1; exp.More() ; exp.Next(), j++) {
271 if ( MapIndex.Contains(j) ) {
272 B.Add( Comp, exp.Current() );