]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESHClient/SMESH_Client.cxx
Salome HOME
0022098: EDF 2036 SMESH: Create groups from none conected parts of a mesh
[modules/smesh.git] / src / SMESHClient / SMESH_Client.cxx
1 // Copyright (C) 2007-2013  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 //  File   : SMESH_Client.cxx
23 //  Author : Pavel TELKOV
24 //  Module : SMESH
25
26 #include "SMESH_Client.hxx"
27 #include "SMESH_Mesh.hxx"
28 #include "SMESHDS_Script.hxx"
29
30 #include "SALOME_NamingService.hxx"
31 #include "SALOME_LifeCycleCORBA.hxx"
32
33 #include <SALOMEconfig.h>
34 #include CORBA_SERVER_HEADER(SALOME_Component)
35 #include CORBA_SERVER_HEADER(SALOME_Exception)
36
37 #include "Basics_Utils.hxx"
38 #include "utilities.h"
39
40 #ifdef WNT
41 #include <process.h>
42 #else
43 #include <unistd.h>
44 #endif
45
46 #include <stdexcept>
47
48 #ifndef EXCEPTION
49 #define EXCEPTION(TYPE, MSG) {\
50   std::ostringstream aStream;\
51   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
52   throw TYPE(aStream.str());\
53 }
54 #endif
55
56 #ifdef _DEBUG_
57 static int MYDEBUG = 1;
58 #else
59 static int MYDEBUG = 0;
60 #endif
61
62 namespace
63 {
64
65   //=======================================================================
66   //function : FindNode
67   //=======================================================================
68   inline const SMDS_MeshNode* FindNode(const SMDS_Mesh* theMesh, int theId){
69     if(const SMDS_MeshNode* anElem = theMesh->FindNode(theId)) return anElem;
70     EXCEPTION(runtime_error,"SMDS_Mesh::FindNode - cannot find a SMDS_MeshNode for ID = "<<theId);
71   }
72
73
74   //=======================================================================
75   //function : FindElement
76   //=======================================================================
77   inline const SMDS_MeshElement* FindElement(const SMDS_Mesh* theMesh, int theId){
78     if(const SMDS_MeshElement* anElem = theMesh->FindElement(theId)) return anElem;
79     EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot find a SMDS_MeshElement for ID = "<<theId);
80   }
81
82
83   //=======================================================================
84   //function : AddNodesWithID
85   //=======================================================================
86   inline void AddNodesWithID(SMDS_Mesh* theMesh,
87                              SMESH::log_array_var& theSeq,
88                              CORBA::Long theId)
89   {
90     const SMESH::double_array& aCoords = theSeq[theId].coords;
91     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
92     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
93     if(3*aNbElems != aCoords.length())
94       EXCEPTION(runtime_error,"AddNodesWithID - 3*aNbElems != aCoords.length()");
95     for(CORBA::Long aCoordId = 0; anElemId < aNbElems; anElemId++, aCoordId+=3){
96       SMDS_MeshElement* anElem = theMesh->AddNodeWithID(aCoords[aCoordId],
97                                                         aCoords[aCoordId+1],
98                                                         aCoords[aCoordId+2],
99                                                         anIndexes[anElemId]);
100       if(!anElem)
101         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddNodeWithID for ID = "<<anElemId);
102     }
103   }
104
105
106   //=======================================================================
107   //function : Add0DElementsWithID
108   //=======================================================================
109   inline void Add0DElementsWithID(SMDS_Mesh* theMesh,
110                                   SMESH::log_array_var& theSeq,
111                                   CORBA::Long theId)
112   {
113     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
114     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
115     if (2*aNbElems != anIndexes.length())
116       EXCEPTION(runtime_error,"AddEdgeWithID - 2*aNbElems != aCoords.length()");
117     CORBA::Long anIndexId = 0;
118     for (; anElemId < aNbElems; anElemId++, anIndexId+=2)
119     {
120       SMDS_MeshElement* anElem = theMesh->Add0DElementWithID(anIndexes[anIndexId+1],
121                                                              anIndexes[anIndexId]);
122       if (!anElem)
123         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot Add0DElementWithID for ID = "<<anElemId);
124     }
125   }
126
127
128   //=======================================================================
129   //function : AddBallsWithID
130   //=======================================================================
131   inline void AddBallsWithID(SMDS_Mesh*            theMesh,
132                              SMESH::log_array_var& theSeq,
133                              CORBA::Long           theId)
134   {
135     const SMESH::double_array& aDiameter = theSeq[theId].coords;
136     const SMESH::long_array& anIndexes   = theSeq[theId].indexes;
137     CORBA::Long anElemId = 0, aNbElems   = theSeq[theId].number;
138     if (2*aNbElems != anIndexes.length() )
139       EXCEPTION(runtime_error,"AddEdgeWithID - 2*aNbElems != anIndexes.length()");
140     if (aNbElems != aDiameter.length())
141       EXCEPTION(runtime_error,"AddEdgeWithID - aNbElems != aDiameter.length()");
142     CORBA::Long anIndexId = 0;
143     for (; anElemId < aNbElems; anElemId++, anIndexId+=2)
144     {
145       SMDS_MeshElement* anElem = theMesh->AddBallWithID(anIndexes[anIndexId+1],
146                                                         aDiameter[anElemId],
147                                                         anIndexes[anIndexId]);
148       if (!anElem)
149         EXCEPTION(runtime_error,"cannot SMDS_Mesh::AddBallsWithID for ID = "<<anElemId);
150     }
151   }
152
153
154   //=======================================================================
155   //function : AddEdgesWithID
156   //=======================================================================
157   inline void AddEdgesWithID(SMDS_Mesh* theMesh,
158                              SMESH::log_array_var& theSeq,
159                              CORBA::Long theId)
160   {
161     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
162     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
163     if(3*aNbElems != anIndexes.length())
164       EXCEPTION(runtime_error,"AddEdgeWithID - 3*aNbElems != aCoords.length()");
165     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=3){
166       SMDS_MeshElement* anElem = theMesh->AddEdgeWithID(anIndexes[anIndexId+1],
167                                                         anIndexes[anIndexId+2],
168                                                         anIndexes[anIndexId]);
169       if(!anElem)
170         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddEdgeWithID for ID = "<<anElemId);
171     }
172   }
173
174
175   //=======================================================================
176   //function : AddTriasWithID
177   //=======================================================================
178   inline void AddTriasWithID(SMDS_Mesh* theMesh,
179                              SMESH::log_array_var& theSeq,
180                              CORBA::Long theId)
181   {
182     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
183     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
184     if(4*aNbElems != anIndexes.length())
185       EXCEPTION(runtime_error,"AddTriasWithID - 4*aNbElems != anIndexes.length()");
186     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=4){
187       SMDS_MeshElement* anElem = theMesh->AddFaceWithID(anIndexes[anIndexId+1],
188                                                         anIndexes[anIndexId+2],
189                                                         anIndexes[anIndexId+3],
190                                                         anIndexes[anIndexId]);
191       if(!anElem)
192         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddFaceWithID for ID = "<<anElemId);
193     }
194   }
195
196
197   //=======================================================================
198   //function : AddQuadsWithID
199   //=======================================================================
200   inline void AddQuadsWithID(SMDS_Mesh* theMesh,
201                              SMESH::log_array_var theSeq,
202                              CORBA::Long theId)
203   {
204     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
205     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
206     if(5*aNbElems != anIndexes.length())
207       EXCEPTION(runtime_error,"AddQuadsWithID - 4*aNbElems != anIndexes.length()");
208     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=5){
209       SMDS_MeshElement* anElem = theMesh->AddFaceWithID(anIndexes[anIndexId+1],
210                                                         anIndexes[anIndexId+2],
211                                                         anIndexes[anIndexId+3],
212                                                         anIndexes[anIndexId+4],
213                                                         anIndexes[anIndexId]);
214       if(!anElem)
215         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddFaceWithID for ID = "<<anElemId);
216     }
217   }
218
219
220   //=======================================================================
221   //function : AddPolygonsWithID
222   //=======================================================================
223   inline void AddPolygonsWithID(SMDS_Mesh* theMesh,
224                                 SMESH::log_array_var& theSeq,
225                                 CORBA::Long theId)
226   {
227     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
228     CORBA::Long anIndexId = 0, aNbElems = theSeq[theId].number;
229
230     for (CORBA::Long anElemId = 0; anElemId < aNbElems; anElemId++) {
231       int aFaceId = anIndexes[anIndexId++];
232
233       int aNbNodes = anIndexes[anIndexId++];
234       std::vector<int> nodes_ids (aNbNodes);
235       for (int i = 0; i < aNbNodes; i++) {
236         nodes_ids[i] = anIndexes[anIndexId++];
237       }
238
239       SMDS_MeshElement* anElem = theMesh->AddPolygonalFaceWithID(nodes_ids, aFaceId);
240       if (!anElem)
241         EXCEPTION(runtime_error, "SMDS_Mesh::FindElement - cannot AddPolygonalFaceWithID for ID = "
242                   << anElemId);
243     }
244   }
245
246
247   //=======================================================================
248   //function : AddTetrasWithID
249   //=======================================================================
250   inline void AddTetrasWithID(SMDS_Mesh* theMesh,
251                               SMESH::log_array_var& theSeq,
252                               CORBA::Long theId)
253   {
254     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
255     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
256     if(5*aNbElems != anIndexes.length())
257       EXCEPTION(runtime_error,"AddTetrasWithID - 5*aNbElems != anIndexes.length()");
258     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=5){
259       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
260                                                           anIndexes[anIndexId+2],
261                                                           anIndexes[anIndexId+3],
262                                                           anIndexes[anIndexId+4],
263                                                           anIndexes[anIndexId]);
264       if(!anElem)
265         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
266     }
267   }
268
269
270   //=======================================================================
271   //function : AddPiramidsWithID
272   //=======================================================================
273   inline void AddPiramidsWithID(SMDS_Mesh* theMesh,
274                                 SMESH::log_array_var& theSeq,
275                                 CORBA::Long theId)
276   {
277     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
278     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
279     if(6*aNbElems != anIndexes.length())
280       EXCEPTION(runtime_error,"AddPiramidsWithID - 6*aNbElems != anIndexes.length()");
281     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=6){
282       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
283                                                           anIndexes[anIndexId+2],
284                                                           anIndexes[anIndexId+3],
285                                                           anIndexes[anIndexId+4],
286                                                           anIndexes[anIndexId+5],
287                                                           anIndexes[anIndexId]);
288       if(!anElem)
289         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
290     }
291   }
292
293
294   //=======================================================================
295   //function : AddPrismsWithID
296   //=======================================================================
297   inline void AddPrismsWithID(SMDS_Mesh* theMesh,
298                               SMESH::log_array_var& theSeq,
299                               CORBA::Long theId)
300   {
301     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
302     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
303     if(7*aNbElems != anIndexes.length())
304       EXCEPTION(runtime_error,"AddPrismsWithID - 7*aNbElems != anIndexes.length()");
305     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=7){
306       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
307                                                           anIndexes[anIndexId+2],
308                                                           anIndexes[anIndexId+3],
309                                                           anIndexes[anIndexId+4],
310                                                           anIndexes[anIndexId+5],
311                                                           anIndexes[anIndexId+6],
312                                                           anIndexes[anIndexId]);
313       if(!anElem)
314         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
315     }
316   }
317
318
319   //=======================================================================
320   //function : AddHexasWithID
321   //=======================================================================
322   inline void AddHexasWithID(SMDS_Mesh* theMesh,
323                              SMESH::log_array_var& theSeq,
324                              CORBA::Long theId)
325   {
326     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
327     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
328     if(9*aNbElems != anIndexes.length())
329       EXCEPTION(runtime_error,"AddHexasWithID - 9*aNbElems != anIndexes.length()");
330     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=9){
331       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
332                                                           anIndexes[anIndexId+2],
333                                                           anIndexes[anIndexId+3],
334                                                           anIndexes[anIndexId+4],
335                                                           anIndexes[anIndexId+5],
336                                                           anIndexes[anIndexId+6],
337                                                           anIndexes[anIndexId+7],
338                                                           anIndexes[anIndexId+8],
339                                                           anIndexes[anIndexId]);
340       if(!anElem)
341         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
342     }
343   }
344
345   //=======================================================================
346   //function : AddHexPrismWithID
347   //=======================================================================
348   inline void AddHexPrismWithID(SMDS_Mesh* theMesh,
349                                 SMESH::log_array_var& theSeq,
350                                 CORBA::Long theId)
351   {
352     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
353     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
354     if(13*aNbElems != anIndexes.length())
355       EXCEPTION(runtime_error,"AddHexPrismWithID - 13*aNbElems != anIndexes.length()");
356     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=13){
357       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
358                                                           anIndexes[anIndexId+2],
359                                                           anIndexes[anIndexId+3],
360                                                           anIndexes[anIndexId+4],
361                                                           anIndexes[anIndexId+5],
362                                                           anIndexes[anIndexId+6],
363                                                           anIndexes[anIndexId+7],
364                                                           anIndexes[anIndexId+8],
365                                                           anIndexes[anIndexId+9],
366                                                           anIndexes[anIndexId+10],
367                                                           anIndexes[anIndexId+11],
368                                                           anIndexes[anIndexId+12],
369                                                           anIndexes[anIndexId]);
370       if(!anElem)
371         EXCEPTION(runtime_error,"AddHexPrismWithID - cannot AddVolumeWithID for ID = "<<anElemId);
372     }
373   }
374
375
376   //=======================================================================
377   //function : AddPolyhedronsWithID
378   //=======================================================================
379   inline void AddPolyhedronsWithID (SMDS_Mesh* theMesh,
380                                     SMESH::log_array_var& theSeq,
381                                     CORBA::Long theId)
382   {
383     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
384     CORBA::Long anIndexId = 0, aNbElems = theSeq[theId].number;
385
386     for (CORBA::Long anElemId = 0; anElemId < aNbElems; anElemId++) {
387       int aFaceId = anIndexes[anIndexId++];
388
389       int aNbNodes = anIndexes[anIndexId++];
390       std::vector<int> nodes_ids (aNbNodes);
391       for (int i = 0; i < aNbNodes; i++) {
392         nodes_ids[i] = anIndexes[anIndexId++];
393       }
394
395       int aNbFaces = anIndexes[anIndexId++];
396       std::vector<int> quantities (aNbFaces);
397       for (int i = 0; i < aNbFaces; i++) {
398         quantities[i] = anIndexes[anIndexId++];
399       }
400
401       SMDS_MeshElement* anElem =
402         theMesh->AddPolyhedralVolumeWithID(nodes_ids, quantities, aFaceId);
403       if (!anElem)
404         EXCEPTION(runtime_error, "SMDS_Mesh::FindElement - cannot AddPolyhedralVolumeWithID for ID = "
405                   << anElemId);
406     }
407   }
408
409
410   //=======================================================================
411   //function : AddQuadEdgesWithID
412   //=======================================================================
413   inline void AddQuadEdgesWithID(SMDS_Mesh* theMesh,
414                                  SMESH::log_array_var& theSeq,
415                                  CORBA::Long theId)
416   {
417     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
418     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
419     if(4*aNbElems != anIndexes.length())
420       EXCEPTION(runtime_error,"AddQuadEdgeWithID - 4*aNbElems != aCoords.length()");
421     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=4){
422       SMDS_MeshElement* anElem = theMesh->AddEdgeWithID(anIndexes[anIndexId+1],
423                                                         anIndexes[anIndexId+2],
424                                                         anIndexes[anIndexId+3],
425                                                         anIndexes[anIndexId]);
426       if(!anElem)
427         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddEdgeWithID for ID = "<<anElemId);
428     }
429   }
430
431
432   //=======================================================================
433   //function : AddQuadTriasWithID
434   //=======================================================================
435   inline void AddQuadTriasWithID(SMDS_Mesh* theMesh,
436                                  SMESH::log_array_var& theSeq,
437                                  CORBA::Long theId)
438   {
439     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
440     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
441     if(7*aNbElems != anIndexes.length())
442       EXCEPTION(runtime_error,"AddQuadTriasWithID - 7*aNbElems != anIndexes.length()");
443     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=7){
444       SMDS_MeshElement* anElem = theMesh->AddFaceWithID(anIndexes[anIndexId+1],
445                                                         anIndexes[anIndexId+2],
446                                                         anIndexes[anIndexId+3],
447                                                         anIndexes[anIndexId+4],
448                                                         anIndexes[anIndexId+5],
449                                                         anIndexes[anIndexId+6],
450                                                         anIndexes[anIndexId]);
451       if(!anElem)
452         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddFaceWithID for ID = "<<anElemId);
453     }
454   }
455
456
457   //=======================================================================
458   //function : AddQuadQuadsWithID
459   //=======================================================================
460   inline void AddQuadQuadsWithID(SMDS_Mesh* theMesh,
461                                  SMESH::log_array_var theSeq,
462                                  CORBA::Long theId)
463   {
464     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
465     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
466     if(9*aNbElems != anIndexes.length())
467       EXCEPTION(runtime_error,"AddQuadQuadsWithID - 9*aNbElems != anIndexes.length()");
468     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=9){
469       SMDS_MeshElement* anElem = theMesh->AddFaceWithID(anIndexes[anIndexId+1],
470                                                         anIndexes[anIndexId+2],
471                                                         anIndexes[anIndexId+3],
472                                                         anIndexes[anIndexId+4],
473                                                         anIndexes[anIndexId+5],
474                                                         anIndexes[anIndexId+6],
475                                                         anIndexes[anIndexId+7],
476                                                         anIndexes[anIndexId+8],
477                                                         anIndexes[anIndexId]);
478       if(!anElem)
479         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddFaceWithID for ID = "<<anElemId);
480     }
481   }
482
483   //=======================================================================
484   //function : AddBiQuadQuadsWithID
485   //=======================================================================
486   inline void AddBiQuadQuadsWithID(SMDS_Mesh* theMesh,
487                                    SMESH::log_array_var theSeq,
488                                    CORBA::Long theId)
489   {
490     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
491     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
492     if(10*aNbElems != anIndexes.length())
493       EXCEPTION(runtime_error,"AddBiQuadQuadsWithID - 10*aNbElems != anIndexes.length()");
494     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=10){
495       SMDS_MeshElement* anElem = theMesh->AddFaceWithID(anIndexes[anIndexId+1],
496                                                         anIndexes[anIndexId+2],
497                                                         anIndexes[anIndexId+3],
498                                                         anIndexes[anIndexId+4],
499                                                         anIndexes[anIndexId+5],
500                                                         anIndexes[anIndexId+6],
501                                                         anIndexes[anIndexId+7],
502                                                         anIndexes[anIndexId+8],
503                                                         anIndexes[anIndexId+9],
504                                                         anIndexes[anIndexId]);
505       if(!anElem)
506         EXCEPTION(runtime_error,"AddBiQuadQuadsWithID() - cannot AddFaceWithID for ID = "<<anElemId);
507     }
508   }
509
510   //=======================================================================
511   //function : AddBiQuadTriasWithID
512   //=======================================================================
513   inline void AddBiQuadTriasWithID(SMDS_Mesh* theMesh,
514                                    SMESH::log_array_var theSeq,
515                                    CORBA::Long theId)
516   {
517     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
518     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
519     if(8*aNbElems != anIndexes.length())
520       EXCEPTION(runtime_error,"AddBiQuadTriasWithID - 8*aNbElems != anIndexes.length()");
521     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=8){
522       SMDS_MeshElement* anElem = theMesh->AddFaceWithID(anIndexes[anIndexId+1],
523                                                         anIndexes[anIndexId+2],
524                                                         anIndexes[anIndexId+3],
525                                                         anIndexes[anIndexId+4],
526                                                         anIndexes[anIndexId+5],
527                                                         anIndexes[anIndexId+6],
528                                                         anIndexes[anIndexId+7],
529                                                         anIndexes[anIndexId]);
530       if(!anElem)
531         EXCEPTION(runtime_error,"AddBiQuadTriasWithID() - cannot AddFaceWithID for ID = "<<anElemId);
532     }
533   }
534
535
536   //=======================================================================
537   //function : AddQuadTetrasWithID
538   //=======================================================================
539   inline void AddQuadTetrasWithID(SMDS_Mesh* theMesh,
540                                   SMESH::log_array_var& theSeq,
541                                   CORBA::Long theId)
542   {
543     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
544     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
545     if(11*aNbElems != anIndexes.length())
546       EXCEPTION(runtime_error,"AddQuadTetrasWithID - 11*aNbElems != anIndexes.length()");
547     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=11){
548       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
549                                                           anIndexes[anIndexId+2],
550                                                           anIndexes[anIndexId+3],
551                                                           anIndexes[anIndexId+4],
552                                                           anIndexes[anIndexId+5],
553                                                           anIndexes[anIndexId+6],
554                                                           anIndexes[anIndexId+7],
555                                                           anIndexes[anIndexId+8],
556                                                           anIndexes[anIndexId+9],
557                                                           anIndexes[anIndexId+10],
558                                                           anIndexes[anIndexId]);
559       if(!anElem)
560         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
561     }
562   }
563
564
565   //=======================================================================
566   //function : AddQuadPiramidsWithID
567   //=======================================================================
568   inline void AddQuadPiramidsWithID(SMDS_Mesh* theMesh,
569                                     SMESH::log_array_var& theSeq,
570                                     CORBA::Long theId)
571   {
572     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
573     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
574     if(14*aNbElems != anIndexes.length())
575       EXCEPTION(runtime_error,"AddQuadPiramidsWithID - 14*aNbElems != anIndexes.length()");
576     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=14){
577       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
578                                                           anIndexes[anIndexId+2],
579                                                           anIndexes[anIndexId+3],
580                                                           anIndexes[anIndexId+4],
581                                                           anIndexes[anIndexId+5],
582                                                           anIndexes[anIndexId+6],
583                                                           anIndexes[anIndexId+7],
584                                                           anIndexes[anIndexId+8],
585                                                           anIndexes[anIndexId+9],
586                                                           anIndexes[anIndexId+10],
587                                                           anIndexes[anIndexId+11],
588                                                           anIndexes[anIndexId+12],
589                                                           anIndexes[anIndexId+13],
590                                                           anIndexes[anIndexId]);
591       if(!anElem)
592         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
593     }
594   }
595
596
597   //=======================================================================
598   //function : AddQuadPentasWithID
599   //=======================================================================
600   inline void AddQuadPentasWithID(SMDS_Mesh* theMesh,
601                                   SMESH::log_array_var& theSeq,
602                                   CORBA::Long theId)
603   {
604     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
605     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
606     if(16*aNbElems != anIndexes.length())
607       EXCEPTION(runtime_error,"AddQuadPentasWithID - 16*aNbElems != anIndexes.length()");
608     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=16){
609       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
610                                                           anIndexes[anIndexId+2],
611                                                           anIndexes[anIndexId+3],
612                                                           anIndexes[anIndexId+4],
613                                                           anIndexes[anIndexId+5],
614                                                           anIndexes[anIndexId+6],
615                                                           anIndexes[anIndexId+7],
616                                                           anIndexes[anIndexId+8],
617                                                           anIndexes[anIndexId+9],
618                                                           anIndexes[anIndexId+10],
619                                                           anIndexes[anIndexId+11],
620                                                           anIndexes[anIndexId+12],
621                                                           anIndexes[anIndexId+13],
622                                                           anIndexes[anIndexId+14],
623                                                           anIndexes[anIndexId+15],
624                                                           anIndexes[anIndexId]);
625       if(!anElem)
626         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
627     }
628   }
629
630
631   //=======================================================================
632   //function : AddQuadHexasWithID
633   //=======================================================================
634   inline void AddQuadHexasWithID(SMDS_Mesh* theMesh,
635                                  SMESH::log_array_var& theSeq,
636                                  CORBA::Long theId)
637   {
638     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
639     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
640     if(21*aNbElems != anIndexes.length())
641       EXCEPTION(runtime_error,"AddQuadHexasWithID - 21*aNbElems != anIndexes.length()");
642     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=21){
643       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
644                                                           anIndexes[anIndexId+2],
645                                                           anIndexes[anIndexId+3],
646                                                           anIndexes[anIndexId+4],
647                                                           anIndexes[anIndexId+5],
648                                                           anIndexes[anIndexId+6],
649                                                           anIndexes[anIndexId+7],
650                                                           anIndexes[anIndexId+8],
651                                                           anIndexes[anIndexId+9],
652                                                           anIndexes[anIndexId+10],
653                                                           anIndexes[anIndexId+11],
654                                                           anIndexes[anIndexId+12],
655                                                           anIndexes[anIndexId+13],
656                                                           anIndexes[anIndexId+14],
657                                                           anIndexes[anIndexId+15],
658                                                           anIndexes[anIndexId+16],
659                                                           anIndexes[anIndexId+17],
660                                                           anIndexes[anIndexId+18],
661                                                           anIndexes[anIndexId+19],
662                                                           anIndexes[anIndexId+20],
663                                                           anIndexes[anIndexId]);
664       if(!anElem)
665         EXCEPTION(runtime_error,"SMDS_Mesh::FindElement - cannot AddVolumeWithID for ID = "<<anElemId);
666     }
667   }
668
669   //=======================================================================
670   //function : AddTriQuadHexasWithID
671   //=======================================================================
672   inline void AddTriQuadHexasWithID(SMDS_Mesh* theMesh,
673                                     SMESH::log_array_var& theSeq,
674                                     CORBA::Long theId)
675   {
676     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
677     CORBA::Long anElemId = 0, aNbElems = theSeq[theId].number;
678     if(28*aNbElems != anIndexes.length())
679       EXCEPTION(runtime_error,"AddTriQuadHexasWithID - 28*aNbElems != anIndexes.length()");
680     for(CORBA::Long anIndexId = 0; anElemId < aNbElems; anElemId++, anIndexId+=28){
681       SMDS_MeshElement* anElem = theMesh->AddVolumeWithID(anIndexes[anIndexId+1],
682                                                           anIndexes[anIndexId+2],
683                                                           anIndexes[anIndexId+3],
684                                                           anIndexes[anIndexId+4],
685                                                           anIndexes[anIndexId+5],
686                                                           anIndexes[anIndexId+6],
687                                                           anIndexes[anIndexId+7],
688                                                           anIndexes[anIndexId+8],
689                                                           anIndexes[anIndexId+9],
690                                                           anIndexes[anIndexId+10],
691                                                           anIndexes[anIndexId+11],
692                                                           anIndexes[anIndexId+12],
693                                                           anIndexes[anIndexId+13],
694                                                           anIndexes[anIndexId+14],
695                                                           anIndexes[anIndexId+15],
696                                                           anIndexes[anIndexId+16],
697                                                           anIndexes[anIndexId+17],
698                                                           anIndexes[anIndexId+18],
699                                                           anIndexes[anIndexId+19],
700                                                           anIndexes[anIndexId+20],
701                                                           anIndexes[anIndexId+21],
702                                                           anIndexes[anIndexId+22],
703                                                           anIndexes[anIndexId+23],
704                                                           anIndexes[anIndexId+24],
705                                                           anIndexes[anIndexId+25],
706                                                           anIndexes[anIndexId+26],
707                                                           anIndexes[anIndexId+27],
708                                                           anIndexes[anIndexId]);
709       if(!anElem)
710         EXCEPTION(runtime_error,"AddTriQuadHexasWithID() - cannot AddVolumeWithID for ID = "<<anElemId);
711     }
712   }
713
714
715   //=======================================================================
716   //function : ChangePolyhedronNodes
717   //=======================================================================
718   inline void ChangePolyhedronNodes (SMDS_Mesh* theMesh,
719                                      SMESH::log_array_var& theSeq,
720                                      CORBA::Long theId)
721   {
722     const SMESH::long_array& anIndexes = theSeq[theId].indexes;
723     CORBA::Long iind = 0, aNbElems = theSeq[theId].number;
724
725     for (CORBA::Long anElemId = 0; anElemId < aNbElems; anElemId++)
726     {
727       // find element
728       const SMDS_MeshElement* elem = FindElement(theMesh, anIndexes[iind++]);
729       // nb nodes
730       int nbNodes = anIndexes[iind++];
731       // nodes
732       std::vector<const SMDS_MeshNode*> aNodes (nbNodes);
733       for (int iNode = 0; iNode < nbNodes; iNode++) {
734         aNodes[iNode] = FindNode(theMesh, anIndexes[iind++]);
735       }
736       // nb faces
737       int nbFaces = anIndexes[iind++];
738       // quantities
739       std::vector<int> quantities (nbFaces);
740       for (int iFace = 0; iFace < nbFaces; iFace++) {
741         quantities[iFace] = anIndexes[iind++];
742       }
743       // change
744       theMesh->ChangePolyhedronNodes(elem, aNodes, quantities);
745     }
746   }
747 }
748
749 //=======================================================================
750 SMESH::SMESH_Gen_var
751 SMESH_Client::GetSMESHGen(CORBA::ORB_ptr theORB,
752                           CORBA::Boolean& theIsEmbeddedMode)
753 {
754   static SMESH::SMESH_Gen_var aMeshGen;
755
756   if(CORBA::is_nil(aMeshGen.in())){
757 #ifdef WNT
758     long aClientPID = (long)_getpid();
759 #else
760     long aClientPID =  (long)getpid();
761 #endif
762
763     SALOME_NamingService aNamingService(theORB);
764     SALOME_LifeCycleCORBA aLifeCycleCORBA(&aNamingService);
765     Engines::EngineComponent_var aComponent = aLifeCycleCORBA.FindOrLoad_Component("FactoryServer","SMESH");
766     aMeshGen = SMESH::SMESH_Gen::_narrow(aComponent);
767
768     std::string aClientHostName = Kernel_Utils::GetHostname();
769     Engines::Container_var aServerContainer = aMeshGen->GetContainerRef();
770     CORBA::String_var aServerHostName = aServerContainer->getHostName();
771     CORBA::Long aServerPID = aServerContainer->getPID();
772     aMeshGen->SetEmbeddedMode((aClientPID == aServerPID) && (aClientHostName == aServerHostName.in()));
773   }
774   theIsEmbeddedMode = aMeshGen->IsEmbeddedMode();
775
776   return aMeshGen;
777 }
778
779
780 //=======================================================================
781 // function : Create()
782 // purpose  :
783 //=======================================================================
784 SMESH_Client::SMESH_Client(CORBA::ORB_ptr theORB,
785                            SMESH::SMESH_Mesh_ptr theMesh):
786   myMeshServer(SMESH::SMESH_Mesh::_duplicate(theMesh)),
787   mySMESHDSMesh(NULL),
788   mySMDSMesh(NULL)
789 {
790   MESSAGE("SMESH_Client::SMESH_Client");
791   myMeshServer->Register();
792
793   CORBA::Boolean anIsEmbeddedMode;
794   GetSMESHGen(theORB,anIsEmbeddedMode);
795   if(anIsEmbeddedMode){
796     if ( MYDEBUG )
797       MESSAGE("Info: The same process, update mesh by pointer ");
798     // just set client mesh pointer to server mesh pointer
799     //SMESH_Mesh* aMesh = reinterpret_cast<SMESH_Mesh*>(theMesh->GetMeshPtr());
800     CORBA::LongLong pointeur = theMesh->GetMeshPtr();
801     if( MYDEBUG )
802       MESSAGE("SMESH_Client::SMESH_Client pointeur "<<pointeur);
803     SMESH_Mesh* aMesh = reinterpret_cast<SMESH_Mesh*> (pointeur);
804     if ( MYDEBUG )
805       MESSAGE("SMESH_Client::SMESH_Client aMesh "<<aMesh);
806     //if(aMesh->GetMeshDS()->IsEmbeddedMode()){
807     if(anIsEmbeddedMode){
808       mySMESHDSMesh = aMesh->GetMeshDS();
809       mySMDSMesh = mySMESHDSMesh;
810     }
811   }
812   if(!mySMDSMesh)
813     mySMDSMesh = new SMDS_Mesh();
814 }
815
816
817 //=================================================================================
818 // function : ~SMESH_Client
819 // purpose  : Destructor
820 //=================================================================================
821 SMESH_Client::~SMESH_Client()
822 {
823   myMeshServer->UnRegister();
824   if(!mySMESHDSMesh)
825     delete mySMDSMesh;
826 }
827
828
829 //=================================================================================
830 SMDS_Mesh*
831 SMESH_Client::GetMesh() const
832 {
833   return mySMDSMesh;
834 }
835
836
837 //=================================================================================
838 SMDS_Mesh*
839 SMESH_Client::operator->() const
840 {
841   return GetMesh();
842 }
843
844
845 //=================================================================================
846 SMESH::SMESH_Mesh_ptr
847 SMESH_Client::GetMeshServer()
848 {
849   return myMeshServer.in();
850 }
851
852
853 //=================================================================================
854 // function : SMESH_Client
855 // purpose  : Update mesh
856 //=================================================================================
857 bool
858 SMESH_Client::Update(bool theIsClear)
859 {
860   bool anIsModified = true;
861   if(mySMESHDSMesh){
862         MESSAGE("Update mySMESHDSMesh");
863     SMESHDS_Script* aScript = mySMESHDSMesh->GetScript();
864     anIsModified = aScript->IsModified();
865     aScript->SetModified(false);
866   }else{
867         MESSAGE("Update CORBA");
868     SMESH::log_array_var aSeq = myMeshServer->GetLog( theIsClear );
869     CORBA::Long aLength = aSeq->length();
870     anIsModified = aLength > 0;
871     if( MYDEBUG )
872       MESSAGE( "Update: length of the script is "<<aLength );
873
874     if(!anIsModified)
875       return false;
876
877     // update client mesh structure by logged changes commands
878     try
879     {
880       for ( CORBA::Long anId = 0; anId < aLength; anId++)
881       {
882         const SMESH::double_array& aCoords = aSeq[anId].coords;
883         const SMESH::long_array& anIndexes = aSeq[anId].indexes;
884         CORBA::Long anElemId = 0, aNbElems = aSeq[anId].number;
885         CORBA::Long aCommand = aSeq[anId].commandType;
886
887         switch(aCommand)
888         {
889         case SMESH::ADD_NODE             : AddNodesWithID      ( mySMDSMesh, aSeq, anId ); break;
890         case SMESH::ADD_EDGE             : AddEdgesWithID      ( mySMDSMesh, aSeq, anId ); break;
891         case SMESH::ADD_TRIANGLE         : AddTriasWithID      ( mySMDSMesh, aSeq, anId ); break;
892         case SMESH::ADD_QUADRANGLE       : AddQuadsWithID      ( mySMDSMesh, aSeq, anId ); break;
893         case SMESH::ADD_POLYGON          : AddPolygonsWithID   ( mySMDSMesh, aSeq, anId ); break;
894         case SMESH::ADD_TETRAHEDRON      : AddTetrasWithID     ( mySMDSMesh, aSeq, anId ); break;
895         case SMESH::ADD_PYRAMID          : AddPiramidsWithID   ( mySMDSMesh, aSeq, anId ); break;
896         case SMESH::ADD_PRISM            : AddPrismsWithID     ( mySMDSMesh, aSeq, anId ); break;
897         case SMESH::ADD_HEXAHEDRON       : AddHexasWithID      ( mySMDSMesh, aSeq, anId ); break;
898         case SMESH::ADD_HEXAGONAL_PRISM  : AddHexPrismWithID   ( mySMDSMesh, aSeq, anId ); break;
899         case SMESH::ADD_POLYHEDRON       : AddPolyhedronsWithID( mySMDSMesh, aSeq, anId ); break;
900         case SMESH::ADD_ELEM0D           : Add0DElementsWithID ( mySMDSMesh, aSeq, anId ); break;
901         case SMESH::ADD_BALL             : AddBallsWithID      ( mySMDSMesh, aSeq, anId ); break;
902
903         case SMESH::ADD_QUADEDGE         : AddQuadEdgesWithID   ( mySMDSMesh, aSeq, anId ); break;
904         case SMESH::ADD_QUADTRIANGLE     : AddQuadTriasWithID   ( mySMDSMesh, aSeq, anId ); break;
905         case SMESH::ADD_QUADQUADRANGLE   : AddQuadQuadsWithID   ( mySMDSMesh, aSeq, anId ); break;
906         case SMESH::ADD_QUADTETRAHEDRON  : AddQuadTetrasWithID  ( mySMDSMesh, aSeq, anId ); break;
907         case SMESH::ADD_QUADPYRAMID      : AddQuadPiramidsWithID( mySMDSMesh, aSeq, anId ); break;
908         case SMESH::ADD_QUADPENTAHEDRON  : AddQuadPentasWithID  ( mySMDSMesh, aSeq, anId ); break;
909         case SMESH::ADD_QUADHEXAHEDRON   : AddQuadHexasWithID   ( mySMDSMesh, aSeq, anId ); break;
910
911         case SMESH::ADD_BIQUAD_QUADRANGLE: AddBiQuadQuadsWithID ( mySMDSMesh, aSeq, anId ); break;
912         case SMESH::ADD_BIQUAD_TRIANGLE  : AddBiQuadTriasWithID ( mySMDSMesh, aSeq, anId ); break;
913         case SMESH::ADD_TRIQUAD_HEXA     : AddTriQuadHexasWithID( mySMDSMesh, aSeq, anId ); break;
914
915         case SMESH::CLEAR_MESH:
916           mySMDSMesh->Clear();
917           break;
918
919         case SMESH::REMOVE_NODE:
920           for( ; anElemId < aNbElems; anElemId++ )
921             mySMDSMesh->RemoveNode( FindNode( mySMDSMesh, anIndexes[anElemId] ) );
922         break;
923
924         case SMESH::REMOVE_ELEMENT:
925           for( ; anElemId < aNbElems; anElemId++ )
926             mySMDSMesh->RemoveElement( FindElement( mySMDSMesh, anIndexes[anElemId] ) );
927         break;
928
929         case SMESH::MOVE_NODE:
930           for(CORBA::Long aCoordId=0; anElemId < aNbElems; anElemId++, aCoordId+=3)
931           {
932             SMDS_MeshNode* node =
933               const_cast<SMDS_MeshNode*>( FindNode( mySMDSMesh, anIndexes[anElemId] ));
934             node->setXYZ( aCoords[aCoordId], aCoords[aCoordId+1], aCoords[aCoordId+2] );
935           }
936         break;
937
938         case SMESH::CHANGE_ELEMENT_NODES:
939           for ( CORBA::Long i = 0; anElemId < aNbElems; anElemId++ )
940           {
941             // find element
942             const SMDS_MeshElement* elem = FindElement( mySMDSMesh, anIndexes[i++] );
943             // nb nodes
944             int nbNodes = anIndexes[i++];
945             // nodes
946             //ASSERT( nbNodes < 9 );
947             vector<const SMDS_MeshNode*> aNodes( nbNodes );
948             for ( int iNode = 0; iNode < nbNodes; iNode++ )
949               aNodes[ iNode ] = FindNode( mySMDSMesh, anIndexes[i++] );
950             // change
951             mySMDSMesh->ChangeElementNodes( elem, &aNodes[0], nbNodes );
952           }
953           break;
954
955         case SMESH::CHANGE_POLYHEDRON_NODES:
956           ChangePolyhedronNodes(mySMDSMesh, aSeq, anId);
957           break;
958         case SMESH::RENUMBER:
959           for(CORBA::Long i=0; anElemId < aNbElems; anElemId++, i+=3)
960           {
961             mySMDSMesh->Renumber( anIndexes[i], anIndexes[i+1], anIndexes[i+2] );
962           }
963           break;
964
965         default:;
966         }
967       }
968     }
969     catch ( SALOME::SALOME_Exception& exc )
970     {
971       INFOS("Following exception was cought:\n\t"<<exc.details.text);
972     }
973     catch( const std::exception& exc)
974     {
975       INFOS("Following exception was cought:\n\t"<<exc.what());
976     }
977     catch(...)
978     {
979       INFOS("Unknown exception was cought !!!");
980     }
981
982     if ( MYDEBUG && mySMDSMesh )
983     {
984       MESSAGE("Update - mySMDSMesh->NbNodes() = "<<mySMDSMesh->NbNodes());
985       MESSAGE("Update - mySMDSMesh->Nb0DElements() = "<<mySMDSMesh->Nb0DElements());
986       MESSAGE("Update - mySMDSMesh->NbEdges() = "<<mySMDSMesh->NbEdges());
987       MESSAGE("Update - mySMDSMesh->NbFaces() = "<<mySMDSMesh->NbFaces());
988       MESSAGE("Update - mySMDSMesh->NbVolumes() = "<<mySMDSMesh->NbVolumes());
989     }
990   } // end of update mesh by log script
991
992   return anIsModified;
993 }