Salome HOME
NRI : Merge from V1_2.
[modules/geom.git] / src / GEOMClient / GEOM_Client.cxx
1 //  GEOM GEOMClient : tool to transfer BREP files from GEOM server to GEOM client
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : GEOM_Client.cxx
25 //  Author : Yves FRICAUD/Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 using namespace std;
30 #include "GEOM_Client.hxx"
31 #include <SALOMEconfig.h>
32 #include "utilities.h"
33
34 #include CORBA_SERVER_HEADER(GEOM_Gen)
35
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>
42 #include <TopAbs.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>
48
49
50
51 //=======================================================================
52 // function : Load()
53 // purpose  : 
54 //=======================================================================
55 static TopoDS_Shape Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Shape_ptr aShape )
56 {
57   TopoDS_Shape S;
58   /* get sequence of bytes of resulting brep shape from GEOM server */
59   GEOM::GEOM_Shape::TMPFile_var SeqFile = aShape->GetShapeStream();
60   int sizebuf = SeqFile->length();
61   char* buf;
62   buf = (char*) &SeqFile[0];
63   istrstream streamBrep(buf,sizebuf);
64   BRep_Builder aBuilder;
65   BRepTools::Read(S, streamBrep, aBuilder);
66   return S;
67 }
68
69
70 //=======================================================================
71 // function : Create()
72 // purpose  : 
73 //=======================================================================
74 GEOM_Client::GEOM_Client()
75 {
76 }
77
78
79 //=======================================================================
80 // function : Find()
81 // purpose  : 
82 //=======================================================================
83 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
84 {
85   for ( Standard_Integer i = 1; i<= myIORs.Length(); i++ ) {
86     if (myIORs.Value(i).IsEqual(IOR)) {
87       S = myShapes.Value(i);
88       return i;
89     }
90   }
91   return 0;
92 }
93
94
95 //=======================================================================
96 // function : Bind()
97 // purpose  :
98 //=======================================================================
99 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
100 {
101   myIORs.Append(IOR);
102   myShapes.Append(S);
103 }
104
105 //=======================================================================
106 // function : RemoveShapeFromBuffer()
107 // purpose  : Remove shape from Client Buffer
108 //=======================================================================
109 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& shapeIOR )
110 {
111   if( myIORs.IsEmpty() )
112     return ;
113   
114   TopoDS_Shape S ;
115   Standard_Integer anIndex = Find( shapeIOR, S ) ;
116   if( anIndex != 0 ) {
117     myIORs.Remove(anIndex) ;
118     myShapes.Remove(anIndex) ;
119   }
120   return ;
121 }
122
123
124 //=======================================================================
125 // function : ClearClientBuffer()
126 // purpose  : purge buffer
127 //=======================================================================
128 void GEOM_Client::ClearClientBuffer()
129 {
130   if( myIORs.IsEmpty() )
131     return ;
132   myIORs.Clear() ;
133   myShapes.Clear() ;
134   return ;
135 }
136
137 //=======================================================================
138 // function : BufferLength()
139 // purpose  :
140 //=======================================================================
141 unsigned int GEOM_Client::BufferLength()
142 {
143   return myIORs.Length() ;
144 }
145
146
147 //=======================================================================
148 // function : GetShape()
149 // purpose  : 
150 //=======================================================================
151
152 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Shape_ptr aShape ) 
153
154
155   TopoDS_Shape            S; 
156   TCollection_AsciiString IOR(aShape->Name()); 
157   Standard_Integer        anIndex = Find(IOR, S); 
158   
159   BRep_Builder B; 
160
161   if (anIndex !=0 ) { 
162     return S ; 
163    } 
164
165   /******* in case of a MAIN GEOM::SHAPE ********/ 
166   if (aShape->IsMainShape()) { 
167     S = Load(geom, aShape); 
168     Bind(IOR,S); 
169     return S; 
170   } 
171
172   /******* in case of SUB GEOM::SHAPE ***********/ 
173   // Load and Explore the Main Shape 
174   TopoDS_Shape MainShape = GetShape (geom, geom->GetIORFromString(aShape->MainName())); 
175   GEOM::GEOM_Shape::ListOfSubShapeID_var list = aShape->Index(); 
176
177   Standard_Integer j = 1; 
178   TopExp_Explorer exp; 
179   TopAbs_ShapeEnum ShapeType = TopAbs_ShapeEnum(aShape->ShapeType()); 
180   
181   /* Case of only one subshape */ 
182   if (list->length() == 1) 
183   { 
184     if (ShapeType == TopAbs_COMPOUND) 
185       { 
186         TopoDS_Iterator it; 
187         TopTools_ListOfShape CL; 
188         CL.Append( MainShape ); 
189         TopTools_ListIteratorOfListOfShape itC; 
190         for (itC.Initialize( CL ); itC.More(); itC.Next()) 
191           { 
192             for (it.Initialize( itC.Value() );  it.More(); it.Next()) 
193               { 
194                 if ( it.Value().ShapeType() == TopAbs_COMPOUND) 
195                   {
196                     if (j == list[0]) 
197                       { 
198                         S = it.Value(); 
199                         Bind(IOR, S); 
200                         return S; 
201                       } 
202                     j++; 
203                     CL.Append( it.Value() ); 
204                   }
205               } 
206           } 
207       } 
208     else 
209       { 
210         TopTools_MapOfShape M; 
211         for (exp.Init(MainShape, ShapeType); exp.More(); exp.Next()) { 
212           if ( M.Add(exp.Current()) ) 
213             { 
214               if (j == list[0]) 
215                 { 
216                   S = exp.Current(); 
217                   Bind(IOR, S); 
218                   return S; 
219                 } 
220               j++; 
221             } 
222         } 
223       } 
224   } 
225   
226   /* Case of a compound containing two or more sub shapes (not a main shape compound !)   */ 
227
228   /* Warning : the compound when representing sub shapes must be explored in a sub type   */ 
229   /* that is NOT ShapeType=aShape->ShapeType()= TopAbs_COMPOUND !                         */ 
230   /* We have to retrieve the exact sub type of shapes contained in the compound first !   */ 
231   TopoDS_Iterator it ; 
232   TopAbs_ShapeEnum exactSubType ; 
233   S = Load( geom, aShape ); 
234   it.Initialize( S, true, true ) ; 
235   it.More(); 
236   exactSubType = it.Value().ShapeType() ; 
237
238   TColStd_MapOfInteger MapIndex; 
239   Standard_Integer nbSS = list->length(); 
240   TopoDS_Compound Comp; 
241   B.MakeCompound(Comp); 
242   
243   for (Standard_Integer i=1; i<=nbSS; i++) 
244     MapIndex.Add(list[i-1]); 
245   
246   for (exp.Init(MainShape, exactSubType), j=1; exp.More() ; exp.Next(), j++) { 
247     if ( MapIndex.Contains(j) ) { 
248       B.Add( Comp, exp.Current() ); 
249     } 
250   } 
251   Bind(IOR, Comp); 
252   return Comp; 
253