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