Back to index

salome-geom  6.5.0
GEOM_Client.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : GEOM_Client.cxx
00023 //  Author : Yves FRICAUD/Lucien PIGNOLONI
00024 //  Module : GEOM
00025 
00026 #include <Standard_Stream.hxx>
00027 
00028 #include <Standard_Stream.hxx>
00029 
00030 #include <sstream>
00031 
00032 #include "GEOM_Client.hxx"
00033 #include <SALOMEconfig.h>
00034 #include "Basics_Utils.hxx"
00035 #include "utilities.h"
00036 
00037 #include <BRep_Builder.hxx>
00038 #include <BRepTools.hxx>
00039 #include <TopoDS_Shape.hxx>
00040 #include <TopoDS_Compound.hxx>
00041 #include <TCollection_AsciiString.hxx>
00042 #include <TopExp_Explorer.hxx>
00043 #include <TopExp.hxx>
00044 #include <TopAbs.hxx>
00045 #include <TopTools_IndexedMapOfShape.hxx>
00046 
00047 #ifdef WNT
00048 #include <process.h>
00049 #else
00050 #include <unistd.h>
00051 #endif
00052 
00053 #include CORBA_SERVER_HEADER(SALOMEDS)
00054 #include CORBA_SERVER_HEADER(GEOM_Gen)
00055 
00056 #define HST_CLIENT_LEN 256
00057 
00058 //=======================================================================
00059 // function : Load()
00060 // purpose  :
00061 //=======================================================================
00062 TopoDS_Shape GEOM_Client::Load( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
00063 {
00064   std::string hst_client = Kernel_Utils::GetHostname();
00065 
00066   Engines::Container_var ctn_server = geom->GetContainerRef();
00067   long                   pid_server = ctn_server->getPID();
00068 
00069   CORBA::String_var hostname = ctn_server->getHostName();
00070   if ( pid_client == pid_server && !strcmp(hst_client.c_str(), hostname.in()) ) {
00071     TopoDS_Shape* S = (TopoDS_Shape*)(aShape->getShape());
00072     return(*S);
00073   } else {
00074     /* get sequence of bytes of resulting brep shape from GEOM server */
00075     TopoDS_Shape S;
00076     SALOMEDS::TMPFile_var SeqFile = aShape->GetShapeStream();
00077     /*int sizebuf = */SeqFile->length();
00078     char* buf;
00079     buf = (char*) &SeqFile[0];
00080     std::istringstream streamBrep(buf);
00081     BRep_Builder aBuilder;
00082     BRepTools::Read(S, streamBrep, aBuilder);
00083     return(S);
00084   }
00085 }
00086 
00087 //=======================================================================
00088 // function : Create()
00089 // purpose  : Create in client not in a container
00090 //=======================================================================
00091 GEOM_Client::GEOM_Client()
00092 {
00093   pid_client =
00094 #ifdef WNT
00095     (long)_getpid();
00096 #else
00097     (long)getpid();
00098 #endif
00099 }
00100 
00101 //=======================================================================
00102 // function : Create()
00103 // purpose  : Copy constructor
00104 //=======================================================================
00105 GEOM_Client::GEOM_Client(const GEOM_Client& client)
00106 {
00107   _mySubShapes = client._mySubShapes;
00108   myShapesMap = client.myShapesMap;
00109   pid_client = client.pid_client;
00110 }
00111 
00112 //=======================================================================
00113 // function : Create()
00114 // purpose  :
00115 //=======================================================================
00116 GEOM_Client::GEOM_Client(Engines::Container_ptr client)
00117 {
00118   pid_client = client->getPID();
00119 }
00120 
00121 //=======================================================================
00122 // function : get_client()
00123 // purpose  : Static method to have the only one instance of GEOM_Client
00124 //=======================================================================
00125 #ifdef SINGLE_CLIENT
00126 GEOM_Client& GEOM_Client::get_client()
00127 {
00128   static GEOM_Client a;
00129   return a;
00130 }
00131 #else
00132 GEOM_Client GEOM_Client::get_client()
00133 {
00134   return GEOM_Client();
00135 }
00136 #endif
00137 
00138 //=======================================================================
00139 // function : Find()
00140 // purpose  :
00141 //=======================================================================
00142 Standard_Boolean GEOM_Client::Find (const TCollection_AsciiString& IOR, TopoDS_Shape& S)
00143 {
00144   if (myShapesMap.count(IOR) != 0) {
00145     S = myShapesMap[IOR];
00146     return Standard_True;
00147   }
00148   return Standard_False;
00149 }
00150 
00151 //=======================================================================
00152 // function : Find()
00153 // purpose  :
00154 //=======================================================================
00155 Standard_Boolean GEOM_Client::Find (const TopoDS_Shape& S, TCollection_AsciiString& IOR)
00156 {
00157   std::map< TCollection_AsciiString, TopoDS_Shape >::const_iterator it;
00158   for (it = myShapesMap.begin(); it != myShapesMap.end(); ++it) {
00159     if ((*it).second == S) {
00160       IOR = (*it).first;
00161       return Standard_True;
00162     }
00163   }
00164   return Standard_False;
00165 }
00166 
00167 //=======================================================================
00168 // function : Bind()
00169 // purpose  :
00170 //=======================================================================
00171 void GEOM_Client::Bind( const TCollection_AsciiString& IOR, const TopoDS_Shape& S )
00172 {
00173   myShapesMap[IOR] = S;
00174 }
00175 
00176 //=======================================================================
00177 // function : RemoveShapeFromBuffer()
00178 // purpose  : Remove shape from Client Buffer
00179 //=======================================================================
00180 void GEOM_Client::RemoveShapeFromBuffer( const TCollection_AsciiString& IOR)
00181 {
00182   if (myShapesMap.size() == 0)
00183     return;
00184 
00185   if (myShapesMap.count(IOR) != 0) {
00186     myShapesMap.erase(IOR);
00187     _mySubShapes.erase(IOR);
00188   }
00189 }
00190 
00191 //=======================================================================
00192 // function : ClearClientBuffer()
00193 // purpose  : purge buffer
00194 //=======================================================================
00195 void GEOM_Client::ClearClientBuffer()
00196 {
00197   if (myShapesMap.size() == 0)
00198     return;
00199 
00200   _mySubShapes.clear();
00201   myShapesMap.clear();
00202 }
00203 
00204 //=======================================================================
00205 // function : BufferLength()
00206 // purpose  :
00207 //=======================================================================
00208 unsigned int GEOM_Client::BufferLength()
00209 {
00210   return myShapesMap.size();
00211 }
00212 
00213 //=======================================================================
00214 // function : GetShape()
00215 // purpose  :
00216 //=======================================================================
00217 TopoDS_Shape GEOM_Client::GetShape( GEOM::GEOM_Gen_ptr geom, GEOM::GEOM_Object_ptr aShape )
00218 {
00219   TopoDS_Shape S;
00220   CORBA::String_var anIOR = geom->GetStringFromIOR(aShape);
00221   TCollection_AsciiString IOR = (char*)anIOR.in();
00222   Standard_Boolean anIndex = Find(IOR, S);
00223 
00224   if (anIndex) return S;
00225 
00226   /******* in case of a MAIN GEOM::SHAPE ********/
00227   if (aShape->IsMainShape()) {
00228     S = Load(geom, aShape);
00229     Bind(IOR, S);
00230     return S;
00231   }
00232 
00233   /******* in case of SUB GEOM::SHAPE ***********/
00234   // Load and Explore the Main Shape
00235   TopoDS_Shape aMainShape = GetShape (geom, aShape->GetMainShape());
00236   GEOM::ListOfLong_var list = aShape->GetSubShapeIndices();
00237 
00238   CORBA::String_var aMainIOR = geom->GetStringFromIOR(aShape->GetMainShape());
00239   TCollection_AsciiString mainIOR = (char*)aMainIOR.in();
00240 
00241   //find subshapes only one time
00242   if (_mySubShapes.count(mainIOR) == 0)
00243   {
00244     TopTools_IndexedMapOfShape anIndices;
00245     TopExp::MapShapes(aMainShape, anIndices);
00246     Standard_Integer ii = 1, nbSubSh = anIndices.Extent();
00247     for (; ii <= nbSubSh; ii++)
00248     {
00249       _mySubShapes[mainIOR].push_back(anIndices.FindKey(ii));
00250     }
00251   }
00252 
00253   /* Case of only one subshape */
00254   if (list->length() == 1 && list[0] > 0) {
00255     S = _mySubShapes[mainIOR][list[0]-1];
00256   }
00257   else {
00258     BRep_Builder B;
00259     TopoDS_Compound aCompound;
00260     B.MakeCompound(aCompound);
00261     for (int i = 0; i < list->length(); i++) {
00262       if (0 < list[i] && list[i] <= _mySubShapes[mainIOR].size()) {
00263         TopoDS_Shape aSubShape = _mySubShapes[mainIOR][list[i]-1];
00264         B.Add(aCompound, aSubShape);
00265       }
00266     }
00267 
00268     S = aCompound;
00269   }
00270   Bind(IOR, S);
00271   return S;
00272 }