Salome HOME
Notebook: processed creation of objects dependent on another objects.
[modules/geom.git] / src / GEOMClient / GEOM_Client.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  GEOM GEOMClient : tool to transfer BREP files from GEOM server to GEOM client
23 //  File   : GEOM_Client.cxx
24 //  Author : Yves FRICAUD/Lucien PIGNOLONI
25 //  Module : GEOM
26 //  $Header$
27
28 #include <Standard_Stream.hxx>
29
30 #include <Standard_Stream.hxx>
31
32 #include <strstream>
33
34 #include "GEOM_Client.hxx"
35 #include <SALOMEconfig.h>
36 #include "Basics_Utils.hxx"
37 #include "utilities.h"
38
39 #include <BRep_Builder.hxx>
40 #include <BRepTools.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Compound.hxx>
43 #include <TCollection_AsciiString.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopExp.hxx>
46 #include <TopAbs.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48
49 #ifdef WNT
50 #include <process.h>
51 #else
52 #include <unistd.h>
53 #endif
54
55 #include CORBA_SERVER_HEADER(SALOMEDS)
56 #include CORBA_SERVER_HEADER(GEOM_Gen)
57
58 #define HST_CLIENT_LEN 256
59
60
61 //=======================================================================
62 // function : Load()
63 // purpose  :
64 //=======================================================================
65 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
66 {
67   std::string hst_client = Kernel_Utils::GetHostname();
68
69   Engines::Container_var ctn_server = geom->GetContainerRef();
70   long                   pid_server = ctn_server->getPID();
71
72   CORBA::String_var hostname = ctn_server->getHostName();
73   if ( pid_client == pid_server && !strcmp(hst_client.c_str(), hostname.in()) ) {
74     TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
75     return(*S);
76   } else {
77     /* get sequence of bytes of resulting brep shape from GEOM server */
78     TopoDS_Shape S;
79     SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
80     int sizebuf = SeqFile->length();
81     char* buf;
82     buf = (char*) &SeqFile[0];
83     std::istrstream streamBrep(buf,sizebuf);
84     BRep_Builder aBuilder;
85     BRepTools::Read(S, streamBrep, aBuilder);
86     return(S);
87   }
88 }
89
90 //=======================================================================
91 // function : Create()
92 // purpose  : Create in client not in a container
93 //=======================================================================
94 GEOM_Client::GEOM_Client()
95 {
96   pid_client =
97 #ifdef WNT
98     (long)_getpid();
99 #else
100     (long)getpid();
101 #endif
102 }
103
104 //=======================================================================
105 // function : Create()
106 // purpose  :
107 //=======================================================================
108 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
109 {
110   pid_client = client->getPID();
111 }
112
113 //=======================================================================
114 // function : Find()
115 // purpose  :
116 //=======================================================================
117 Standard_Integer GEOM_Client::Find( const TCollection_AsciiString& IOR, TopoDS_Shape& S )
118 {
119   for ( Standard_Integer i = 1; i<= myIORs.Length(); i++ ) {
120     if (myIORs.Value(i).IsEqual(IOR)) {
121       S = myShapes.Value(i);
122       return i;
123     }
124   }
125   return 0;
126 }
127
128 //=======================================================================
129 // function : Find()
130 // purpose  :
131 //=======================================================================
132 Standard_Integer GEOM_Client::Find( const TopoDS_Shape& S, TCollection_AsciiString& IOR )
133 {
134   for ( Standard_Integer i = 1; i<= myShapes.Length(); i++ ) {
135     if (myShapes.Value(i) == S) {
136       IOR = myIORs.Value(i);
137       return i;
138     }
139   }
140   return 0;
141 }
142
143 //=======================================================================
144 // function : Bind()
145 // purpose  :
146 //=======================================================================
147 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
148 {
149   myIORs.Append(IOR);
150   myShapes.Append(S);
151 }
152
153 //=======================================================================
154 // function : RemoveShapeFromBuffer()
155 // purpose  : Remove shape from Client Buffer
156 //=======================================================================
157 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
158 {
159   if( myIORs.IsEmpty() )
160     return;
161
162   TopoDS_Shape S;
163   Standard_Integer anIndex = Find( IOR, S );
164   if( anIndex != 0 ) {
165     myIORs.Remove(anIndex);
166     myShapes.Remove(anIndex);
167   }
168   return;
169 }
170
171 //=======================================================================
172 // function : ClearClientBuffer()
173 // purpose  : purge buffer
174 //=======================================================================
175 void GEOM_Client::ClearClientBuffer()
176 {
177   if( myIORs.IsEmpty() )
178     return;
179   myIORs.Clear();
180   myShapes.Clear();
181   return;
182 }
183
184 //=======================================================================
185 // function : BufferLength()
186 // purpose  :
187 //=======================================================================
188 unsigned int GEOM_Client::BufferLength()
189 {
190   return myIORs.Length();
191 }
192
193 //=======================================================================
194 // function : GetShape()
195 // purpose  :
196 //=======================================================================
197 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
198 {
199   TopoDS_Shape S;
200   CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
201   TCollection_AsciiString IOR = (char*)anIOR.in();
202   Standard_Integer anIndex = Find(IOR, S);
203
204   if (anIndex != 0) return S;
205
206   /******* in case of a MAIN GEOM::SHAPE ********/
207   if (aShape->IsMainShape()) {
208     S = Load(geom, aShape);
209     Bind(IOR, S);
210     return S;
211   }
212
213   /******* in case of SUB GEOM::SHAPE ***********/
214   // Load and Explore the Main Shape
215   TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
216   GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
217
218   TopTools_IndexedMapOfShape anIndices;
219   TopExp::MapShapes(aMainShape, anIndices);
220
221   /* Case of only one subshape */
222   if (list->length() == 1 && list[0] > 0) {
223     S = anIndices.FindKey(list[0]);
224   }
225   else {
226     BRep_Builder B;
227     TopoDS_Compound aCompound;
228     B.MakeCompound(aCompound);
229     for (int i = 0; i < list->length(); i++) {
230       if (0 < list[i] && list[i] <= anIndices.Extent()) {
231         TopoDS_Shape aSubShape = anIndices.FindKey(list[i]);
232         B.Add(aCompound, aSubShape);
233       }
234     }
235
236     S = aCompound;
237   }
238   Bind(IOR, S);
239   return S;
240 }