Salome HOME
Imported using TkCVS
[plugins/ghs3dprlplugin.git] / src / tepal2med / ghs3dprl_mesh_wrap.cxx
1
2 #include <string>
3 #include <iostream>
4 #include <sstream>
5 #include <fstream>
6 #include <qstring.h>
7 #include <qfile.h>
8 #include "ghs3dprl_mesh_wrap.h"
9
10 using namespace std;
11 using namespace med_2_2;
12
13 //************************************
14 CVWtab::CVWtab(long nb, med_int *pmint)
15 //constructor with pmint allocated yet with new
16 {
17    //cout"***constructor med_int CVWtab***\n";
18    size=nb;
19    type=1;  //only tmint valide
20    tmint=pmint;
21    tmflo=NULL;
22 }
23
24 //************************************
25 CVWtab::CVWtab(long nb, med_float *pmflo)
26 //constructor with pmflo allocated yet with new
27 {
28    //cout<<"***constructor med_float CVWtab***\n";
29    size=nb;
30    type=2;   //only tmflo valide
31    tmint=NULL;
32    tmflo=pmflo;
33 }
34
35 //************************************
36 CVWtab::~CVWtab()
37 {
38    bool ok;
39    //cout<<"   destructor CVWtab *** "<<this->filename<<endl;
40    ok=this->CVWtab_deallocate();
41    //remove temporary file
42    if (this->filename!="_NO_FILE")
43    {
44       remove(this->filename); //#include <stdio.h>
45       //cout<<this->filename<<" successfully deleted\n";
46    }
47
48 }
49
50 //************************************
51 bool CVWtab::CVWtab_deallocate()
52 {
53    //cout<<"   deallocate CVWtab*** "<<size<<endl;
54    if (size <= 0) return FALSE;
55    if (tmint)
56    {
57       delete[] tmint;
58       size=-size; //precaution
59    }
60    if (tmflo)
61    {
62       delete[] tmflo;
63       size=-size; //precaution
64    }
65    return TRUE;
66 }
67
68 //************************************
69 bool CVWtab::is_equal(CVWtab *tab2)
70 {
71    //cout<<"is_equal tab1 tab2 type="<<this->type<<"  size="<<this->size<<" "<<tab2->size<<endl;
72    //if (this->type==1) cout<<"med_int tab1[0]="<<this->tmint[0]<<endl;
73    //if (this->type==2) cout<<"med_float tab1[0]="<<this->tmflo[0]<<endl;
74    if (this->size!=tab2->size) return FALSE;
75    if (this->type!=tab2->type) return FALSE;
76    if (this->type==1)
77    {
78       if (!this->tmint)
79       {  cout<<"***is_equal*** pb pointer NULL with tmint size="<<this->size<<endl;
80          return FALSE;
81       }
82       for (long i=0; i < this->size; i++)
83          if (this->tmint[i]!=tab2->tmint[i]) return FALSE;
84    }
85    if (this->type==2)
86    {
87       if (!this->tmflo)
88       {  cout<<"***is_equal*** pb pointer NULL with tmflo size="<<this->size<<endl;
89          return FALSE;
90       }
91       for (long i=0; i < this->size; i++)
92          if (this->tmflo[i]!=tab2->tmflo[i]) return FALSE;
93    }
94    return TRUE;
95 }
96
97 //************************************
98 bool CVW_is_equal_vertices(CVWtab *tab1, long i1,
99                        CVWtab *tab2, long i2, int verbose)
100 //verbose 0 for no prints
101 //verbose 1 for print vertices not equals
102 //verbose 2 for print also vertices equals (debug)
103 {
104    //cout<<"is_equal_vertice size="<<tab1->size<<" "<<tab2->size<<endl;
105    bool ok=FALSE;
106    med_float *p1,*p2;
107    //vertices indices from 1 not 0!
108    long di1=(i1-1)*3, di2=(i2-1)*3;
109    if (di1<0 || di1>=tab1->size)
110    {
111       cerr<<"BadIndice tab1 in is_equal_vertices "<<
112             di1<<" not in "<<tab1->size<<endl;
113       return FALSE;
114    }
115    if (di2<0 || di2>=tab2->size)
116    {
117       cerr<<"BadIndice tab2 in is_equal_vertices "<<
118             di2<<" not in "<<tab2->size<<endl;
119       return FALSE;
120    }
121    p1=(tab1->tmflo+di1);
122    p2=(tab2->tmflo+di2);
123    if (p1[0]==p2[0] && p1[1]==p2[1] && p1[2]==p2[2]) ok=TRUE ;
124    if (!ok && verbose>0) printf(
125       "Vertices differents (%.16g %.16g %.16g) (%.16g %.16g %.16g)\n",
126       p1[0],p1[1],p1[2],p2[0],p2[1],p2[2]);
127    else
128       if (verbose>1) printf(
129       "Vertices equals     (%.16g %.16g %.16g)\n",
130       p1[0],p1[1],p1[2]);
131    return ok;
132 }
133
134 //************************************
135 bool CVW_FindString(const string &str,fstream &Ff, long &count)
136 //find in file first line with string str in first position of line
137 //converts count value expected after "='" in line found
138 {
139    string line;
140    do
141    {
142       if (getline(Ff,line))
143       {
144          if (line[0]==str[0]) //faster
145          {
146             if (line.find(str)==0)
147             {
148             QString tmp=line;
149             bool ok;
150             count=tmp.section('\'',1,1).toLong(&ok);
151             return ok;
152             }
153          }
154       }
155       else
156       {
157          cerr<<"Problem line '"<<str<<"' not found in file\n"<<endl;
158          return FALSE;
159       }
160    } while (1);
161    return TRUE;
162 }
163
164 //************************************
165 bool ghs3dprl_mesh_wrap::ReadFileGLO(const QString FileName)
166 //read file .glo with no parser xml because big file (volume)
167 {
168    QString tmp;
169    fstream Ff((const char *)FileName,ios_base::in);
170    string line;
171    long count;
172    bool ok;
173
174    if (!Ff.is_open())
175    {
176       cerr<<"Problem File '"<<FileName<<"' not open\n"<<endl;
177       return FALSE;
178    }
179
180    //Lit les données :
181    if (!CVW_FindString("<vertices count=",Ff,count)) return FALSE;
182    if (this->verbose>2) cout<<"VerticesCount="<<count<<endl;
183    if (count>0)
184    {
185       med_int *tmint=new med_int[count];
186       for (int i=0; i<count; i++) Ff>>tmint[i];
187       if (this->verbose>4) cout<<"Elements "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[count-1]<<endl;
188
189       CVWtab *montab=new CVWtab(count,tmint);
190       tmp=tmp.sprintf("GL%ld VE",this->nofile);
191       ok=this->insert_key(tmp,montab);
192    }
193
194    if (!CVW_FindString("<edges count=",Ff,count)) return FALSE;
195    if (this->verbose>2) cout<<"EdgesCount="<<count<<endl;
196    if (count>0)
197    {
198       med_int *tmint=new med_int[count];
199       for (int i=0; i<count; i++) Ff>>tmint[i];
200       if (this->verbose>4) cout<<"Elements "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[count-1]<<endl;
201
202       CVWtab *montab=new CVWtab(count,tmint);
203       tmp=tmp.sprintf("GL%ld ED",this->nofile);
204       ok=this->insert_key(tmp,montab);
205    }
206
207    if (!CVW_FindString("<faces count=",Ff,count)) return FALSE;
208    if (this->verbose>2) cout<<"FacesCount="<<count<<endl;
209    if (count>0)
210    {
211       med_int *tmint=new med_int[count];
212       for (int i=0; i<count; i++) Ff>>tmint[i];
213       if (this->verbose>4) cout<<"Elements "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[count-1]<<endl;
214
215       CVWtab *montab=new CVWtab(count,tmint);
216       tmp=tmp.sprintf("GL%ld FA",this->nofile);
217       ok=this->insert_key(tmp,montab);
218    }
219
220    if (!CVW_FindString("<elements count=",Ff,count)) return FALSE;
221    if (this->verbose>2) cout<<"ElementsCount="<<count<<endl;
222    if (count>0)
223    {
224       med_int *tmint=new med_int[count];
225       for (int i=0; i<count; i++) Ff>>tmint[i];
226       if (this->verbose>4) cout<<"Elements "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[count-1]<<endl;
227
228       CVWtab *montab=new CVWtab(count,tmint);
229       tmp=tmp.sprintf("GL%ld EL",this->nofile);
230       ok=this->insert_key(tmp,montab);
231    }
232    //Ferme le fichier :
233    Ff.close();
234    this->nbfiles++;
235    return TRUE;
236 }
237
238 //************************************
239 bool ghs3dprl_mesh_wrap::ReadFileFACES(const QString FileName)
240 //read file .faces (wrap)
241 {
242    QString tmp;
243    fstream Ff((const char *)FileName,ios_base::in);
244    string line;
245    long nbelem,ntype;
246    bool ok;
247
248    if (!Ff.is_open())
249    {
250       cerr<<"Problem File '"<<FileName<<"' not open\n"<<endl;
251       return FALSE;
252    }
253
254    //Lit les données :
255    //Replace le pointeur de fichier au début :f.seekg(0);
256    if (getline(Ff,line))
257    {
258       tmp=line;
259       nbelem=tmp.section(' ',0,0).toLong(&ok);
260    }
261    else
262    {
263       cerr<<"Problem on first line of file"<<endl;
264       return FALSE;
265    }
266    if (this->verbose>2) cout<<"NumberOfElements="<<nbelem<<endl;
267    med_int *tmint=new med_int[nbelem*7];
268    for (int i=0; i<nbelem*7; i=i+7)
269    {
270       Ff>>ntype;
271       if (ntype!=3) //only triangles
272       {
273          cerr<<"Problem on ntype != 3"<<endl;
274          return FALSE;
275       }
276       for (int j=0; j<7; j++) Ff>>tmint[i+j];
277       //for (int j=0; j<7; j++) cout<<tmint[i+j]<<' '; cout<<endl;
278    }
279    if (this->verbose>4) cout<<"Elements "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[nbelem*7-1]<<endl;
280
281    CVWtab *montab=new CVWtab(nbelem*7,tmint);
282    tmp=tmp.sprintf("FC%ld",this->nofile);
283    ok=this->insert_key(tmp,montab);
284
285    Ff.close();
286    this->nbfiles++;
287    return TRUE;
288 }
289
290 //************************************
291 bool ghs3dprl_mesh_wrap::ReadFileNOBOITE(const QString FileName)
292 //read file .noboite (volume)
293 //for huge files it could be better use ReadFileNOBOITEB (B=binary format)
294 //(parameter option of ghs3d but NOT tepal)
295 {
296    QString tmp;
297    fstream Ff((const char *)FileName,ios_base::in);
298    long ne,np,npfixe,subnumber,reste;
299    bool ok;
300
301    if (!Ff.is_open())
302    {
303       cerr<<"Problem File '"<<FileName<<"' not open\n"<<endl;
304       return FALSE;
305    }
306
307    //lit les données :
308    Ff>>ne>>np>>npfixe;
309    if (this->verbose>2)
310    {
311       cout<<"NumberOfElements="<<ne<<endl;
312       cout<<"NumberOfVertices="<<np<<endl;
313       cout<<"NumberOfSpecifiedPoints="<<npfixe<<endl;
314    }
315    for (int i=1; i<=17-3; i++) Ff>>reste;
316    //printf("reste %ld\n",reste);
317    med_int *tmint=new med_int[ne*4];
318    for (int i=0; i<ne*4; i++) Ff>>tmint[i];
319    if (this->verbose>4) cout<<"Elements "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[ne*4-1]<<endl;
320
321    CVWtab *montab=new CVWtab(ne*4,tmint);
322    tmp=tmp.sprintf("NB%ld EV",this->nofile);
323    ok=this->insert_key(tmp,montab);
324
325    med_float *tmflo=new med_float[np*3];
326    for (int i=0; i<np*3; i++) Ff>>tmflo[i];
327    if (this->verbose>4) cout<<"Vertices "<<tmflo[0]<<" "<<tmflo[1]<<"... "<<tmflo[np*3-1]<<endl;
328
329    montab=new CVWtab(np*3,tmflo);
330    tmp=tmp.sprintf("NB%ld VC",this->nofile);
331    ok=this->insert_key(tmp,montab);
332
333    Ff>>subnumber;
334    if (this->verbose>2) cout<<"NumberOfSubdomains="<<subnumber<<endl;
335    tmint=new med_int[subnumber*3];
336    for (int i=0; i<subnumber*3; i++) Ff>>tmint[i];
337    if (this->verbose>4) cout<<"Subdomains "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[subnumber*3-1]<<endl;
338
339    montab=new CVWtab(subnumber*3,tmint);
340    tmp=tmp.sprintf("NB%ld SN",this->nofile);
341    ok=this->insert_key(tmp,montab);
342
343    //beware record 6 lenght 1
344    //ferme le fichier :
345    Ff.close();
346    this->nbfiles++;
347    return TRUE;
348 }
349
350 //************************************
351 bool ghs3dprl_mesh_wrap::ReadFileNOBOITEB(const QString FileName)
352 //read file .noboiteb (volume)
353 //for huge files it could be better use ReadFileNOBOITEB (B=binary format)
354 //but NOT parameter option of tepal
355 //idem ReadFileNOBOITE with read unformatted
356 {
357    bool ok;
358
359    cerr<<"Problem function ReadFileNOBOITEB\n"
360        <<"(no FORTRAN binary format files in tepal)\n\n";
361
362    QString tmp;
363    //file binary
364    FILE *Ff=fopen((const char *)FileName,"rb");
365    long ne,np,npfixe,reste,subnumber; /*,cube,npbli,
366         nbele,loele,nbelef,loelef,
367         nbpoi,lopoi,nbpoif,lopoif,
368         nbsub,losub,nbsubf,losubf,reste;*/
369
370    //http://www.math.utah.edu/software/c-with-fortran.html
371    //record 1 from format FORTRAN begins and ends with lengh of record
372    //=> 2*long(68)     (68=17*4octets)
373    long r1[17+2];
374
375    if (!Ff)
376    {
377       cerr<<"Problem File '"<<FileName<<"' not open\n"<<endl;
378       return FALSE;
379    }
380
381    //read datas :
382    fread(&r1,sizeof(long),17+2,Ff);
383    for (long i=1; i<18; i++) cout<<"R1("<<i<<")="<<r1[i]<<endl;
384
385    if (r1[0]!=68)
386    {
387       cerr<<"First FORTRAN record of File '"<<FileName<<"' not length 17*long"<<endl;
388       return FALSE;
389    }
390    ne=r1[1];
391    np=r1[2];
392    npfixe=r1[3];
393    if (this->verbose>2)
394    {
395       cout<<"NumberOfElements="<<ne<<endl;
396       cout<<"NumberOfVertices="<<np<<endl;
397       cout<<"NumberOfSpecifiedPoints="<<npfixe<<endl;
398    }
399    ///etc...could be done if necessary not debugged
400    fread(&reste,sizeof(long),1,Ff);
401    long *tlong=new long[ne*4];
402    med_int *tmint=new med_int[ne*4];
403    fread(tlong,sizeof(long),ne*4,Ff);
404    fread(&reste,sizeof(long),1,Ff);
405    for (long i=0; i<ne*4; i++) tmint[i]=tlong[i];
406    delete tlong;
407    if (this->verbose>4) cout<<"Elements "<<tmint[0]<<" "<<tmint[1]<<"... "<<tmint[ne*4-1]<<endl;
408
409    CVWtab *montab=new CVWtab(ne*4,tmint);
410    tmp=tmp.sprintf("NB%ld EV",this->nofile);
411    ok=this->insert_key(tmp,montab);
412
413    fread(&reste,sizeof(long),1,Ff);
414    //cout<<"info "<<reste<<" "<<np*3<<" "<<sizeof(med_float)<<endl;
415    float *tfloat=new float[np*3];
416    med_float *tmflo=new med_float[np*3];
417    fread(tfloat,sizeof(float),np*3,Ff);
418    fread(&reste,sizeof(long),1,Ff);
419    for (long i=0; i<np*3; i++) tmflo[i]=tfloat[i];
420    delete tfloat;
421    if (this->verbose>4) printf("Vertices %g %g ... %g \n",tmflo[0],tmflo[1],tmflo[np*3-1]);
422
423    montab=new CVWtab(np*3,tmflo);
424    tmp=tmp.sprintf("NB%ld VC",this->nofile);
425    ok=this->insert_key(tmp,montab);
426
427    fread(&reste,sizeof(long),1,Ff);
428    fread(&subnumber,sizeof(long),1,Ff);
429    fread(&reste,sizeof(long),1,Ff);
430    if (this->verbose>2) cout<<"NumberOfSubdomains="<<subnumber<<endl;
431    fread(&reste,sizeof(long),1,Ff);
432    tlong=new long[subnumber*3];
433    fread(tlong,sizeof(long),subnumber*3,Ff);
434    fread(&reste,sizeof(long),1,Ff);
435    if (this->verbose>4) printf("Subdomains %ld %ld ... %ld \n",tlong[0],tlong[1],tlong[subnumber*3-1]);
436
437    tmint=new med_int[subnumber*3];
438    for (long i=0; i<subnumber*3; i++) tmint[i]=tlong[i];
439    delete tlong;
440    montab=new CVWtab(subnumber*3,tmint);
441    tmp=tmp.sprintf("NB%ld SN",this->nofile);
442    ok=this->insert_key(tmp,montab);
443
444    //beware record 6 lenght 1
445    //ferme le fichier :
446    fclose(Ff);
447    this->nbfiles++;
448    return TRUE;
449
450 }
451
452 //************************************
453 bool ghs3dprl_mesh_wrap::ReadFilePOINTS(const QString FileName)
454 //read file .points (wrap)
455 {
456    QString tmp;
457    long nb;
458    Q_ULONG maxlen=128;
459    Q_LONG lg;
460    bool ok=TRUE;
461
462    //Lit les données :
463    QFile Ff(FileName);
464    //NOT Raw because Raw=non-buffered file access
465    ok=Ff.open(IO_ReadOnly|IO_Translate);
466    if (!ok)
467    {
468       cerr<<"Problem File '"<<FileName<<"' not open\n"<<endl;
469       return FALSE;
470    }
471
472    lg=Ff.readLine(tmp,maxlen);
473    tmp=tmp.simplifyWhiteSpace();
474    nb=tmp.toLong(&ok);
475    if (!ok)
476    {
477       cerr<<"Problem conversion File '"<<FileName<<"\n"<<endl;
478       return FALSE;
479    }
480    if (this->verbose>2) cout<<"NumberOfVertices="<<nb<<endl;
481    med_float *tmflo=new med_float[3*nb]; //coordinates
482    med_int *tmint=new med_int[nb];         //nrs (attribute of point)
483    long il3=0;
484    for ( long il=0; il<nb; il++ )
485    {
486       lg=Ff.readLine(tmp,maxlen);
487       tmp=tmp.simplifyWhiteSpace();
488       //cout<<"lu '"<<tmp<<"'"<<lg<<endl;
489       for ( int j=0; j<3; j++ )
490       {
491          tmflo[il3]=tmp.section(' ',j,j).toDouble(&ok);
492          //cout<<"cv '"<<tmflo[il3]<<"' "<<il3<<endl;
493          il3++;
494          if (!ok)
495          {
496             cerr<<"Problem conversion File '"<<FileName<<"\n"<<endl;
497             return FALSE;
498          }
499       }
500       //nrs is vertex attribute
501       tmint[il]=tmp.section(' ',3,3).toLong(&ok);
502       if (!ok)
503       {
504          cerr<<"Problem conversion File '"<<FileName<<"\n"<<endl;
505          return FALSE;
506       }
507    }
508    //beware no examples with each specified points (if any) here
509
510    {CVWtab *montab=new CVWtab(nb,tmint); //init montab->tmint nrs
511    tmp=tmp.sprintf("PO%ld NRS",this->nofile);
512    ok=this->insert_key(tmp,montab);}
513
514    {CVWtab *montab=new CVWtab(nb,tmflo); //init montab->tmflo xyz
515    tmp=tmp.sprintf("PO%ld XYZ",this->nofile);
516    ok=this->insert_key(tmp,montab);}
517
518    //Ferme le fichier :
519    Ff.close();
520    this->nbfiles++;
521    return TRUE;
522 }
523
524 //************************************
525 bool ghs3dprl_mesh_wrap::list_keys_mesh_wrap()
526 {
527    QDictIterator<CVWtab> it( this->mestab);
528    for ( ; it.current(); ++it )
529    {
530       string nom=it.currentKey();
531       nom.resize(20,' ');
532       cout<<nom<<"-> size="<<it.current()->size<<endl;
533    }
534    return TRUE;
535 }
536
537 //************************************
538 long ghs3dprl_mesh_wrap::remove_all_keys_mesh_wrap()
539 {
540    long nb=this->remove_key_mesh_wrap(QRegExp(".",TRUE,FALSE));
541    return nb;
542 }
543
544 //************************************
545 long ghs3dprl_mesh_wrap::remove_key_mesh_wrap(const QRegExp &rxp)
546 {
547    long nbremove=0;
548    bool remove;
549    QDictIterator<CVWtab> it(this->mestab);
550    for ( ; it.current(); ++it )
551    {
552      do
553      {
554        long i=it.currentKey().contains(rxp);
555        remove=FALSE;
556        if (i>0)
557        {
558           nbremove++;
559           if (this->verbose>4) cout<<"remove key "<<it.currentKey()<<endl;
560           delete it.current();
561           this->mestab.remove(it.currentKey());
562           remove=TRUE;
563        }
564        //All dictionary iterators that refer to the removed item
565        //will be set to point to the next item
566        //in the dictionary's traversal order
567      } while (remove);
568    }
569    return nbremove;
570 }
571
572 //************************************
573 bool SwapOnFile(const QString &key,const QString &path,CVWtab *tab,int verbose)
574 //
575 {
576    //return TRUE;
577    if (tab->filename=="_NO_FILE")
578    {
579       tab->filename=path+key+".tmp";
580       tab->filename.replace(" ","_"); //replace " " by "_"
581
582       //swap disque binaire
583       //montab->tmint=new long[10]; //for test
584       //for (int i=0; i<10; i++) montab->tmint[i]=i*2;
585       FILE *fichier=fopen(tab->filename,"wb");
586       long taille;
587       taille=tab->size;
588       fwrite(&taille,sizeof(taille),1,fichier);
589       if (tab->tmint)
590       {
591          if (verbose>3)
592          cout<<"SwapOnFile in binary file "<<tab->filename<<
593          " number of elements "<<taille<<
594          " size_element med_int   "<<sizeof(med_int)<<
595          " total_size_binary " <<taille*sizeof(med_int)<<endl;
596          fwrite(tab->tmint,sizeof(med_int),taille,fichier);
597          //fread(&gagnants,sizeof(gagnants),1,fichier);
598       }
599       if (tab->tmflo)
600       {
601          if (verbose>3)
602          cout<<"SwapOnFile in binary file "<<tab->filename<<
603          " number of elements "<<taille<<
604          " size_element med_float "<<sizeof(med_float)<<
605          " total_size_binary " <<taille*sizeof(med_float)<<endl;
606          fwrite(tab->tmflo,sizeof(med_float),taille,fichier);
607       }
608       fclose(fichier);
609    }
610    else
611    {
612       if (verbose>3) cout<<"SwapOnFile in binary file done yet "<<tab->filename<<endl;
613    }
614    //deallocate because swap disk binary mode
615    tab->CVWtab_deallocate(); //free memory
616    return TRUE;
617 }
618
619 //************************************
620 long ghs3dprl_mesh_wrap::SwapOutOfMemory_key_mesh_wrap(const QRegExp &rxp)
621 //
622 {
623    long nb=0;
624    bool ok;
625    QDictIterator<CVWtab> it(this->mestab);
626    for ( ; it.current(); ++it )
627    {
628      long i=it.currentKey().contains(rxp);
629      if (i>0)
630      {
631         nb++;
632         if (it.current()->size>0)
633            ok=SwapOnFile(it.currentKey(),this->path,it.current(),this->verbose); //free memory
634         //if (this->verbose) cout<<"SwapOutOfMemory key "<<it.currentKey()<<endl;
635      }
636    }
637    return nb;
638 }
639 //************************************
640 bool ghs3dprl_mesh_wrap::list_onekey_mesh_wrap(const QString &key)
641 {
642    CVWtab *montab=this->mestab[key];
643    if (montab)
644    {
645       //cout<<"key "<<key<<"trouvee -> size="<<montab->size<<endl;
646       if (montab->type==1)
647          for ( long i=0; i<montab->size; i++ )
648             cout<<montab->tmint[i]<<" ";
649       if (montab->type==2)
650          for ( long i=0; i<montab->size; i++ )
651             cout<<montab->tmflo[i]<<" ";
652       cout<<endl;
653    }
654    else
655       cout<<"key "<<key<<" not found"<<endl;
656    return TRUE;
657 }
658
659 //************************************
660 bool ghs3dprl_mesh_wrap::insert_key(const QString &key,CVWtab *tab)
661 //insertion conditionnée par limite this->nbelem_limit_swap
662 //si tableaux contenus on dimension superieure
663 //alors swap disque dans getenv(tmp) fichier temporaire binaire
664 {
665    bool ok;
666    if (this->verbose>4)
667       cout<<"InsertKey "<<key<<" size="<<tab->size<<endl;
668    tab->filename="_NO_FILE";
669    if (this->nbelem_limit_swap < tab->size)
670       ok=SwapOnFile(key,this->path,tab,this->verbose);
671    this->mestab.insert(key,tab);
672    return TRUE;
673 }
674 //************************************
675 CVWtab* ghs3dprl_mesh_wrap::restore_key(const QString &key)
676 //retauration conditionnée par limite nbelem
677 //si tableaux contenus on dimension superieure a nbelem
678 //alors swap disque dans getenv(tmp) fichier temporaire
679 //alors lecture du fichier (et reallocate memory)
680 {
681    CVWtab *tab=NULL;
682    tab=this->mestab[key];
683    /*if (tab) cout<<" -> size in proc "<<tab->size<<endl;
684    else cout<<" -> tab NULL\n";*/
685    if (!tab) //it is NOT a problem
686    {
687       if (this->verbose>6) cout<<"restore_key key not found "<<key<<endl;
688       return NULL;
689    }
690    if (tab->size > 0)
691    {
692       if (this->verbose>5) cout<<"restore_key direct from memory "<<key<<" size="<<tab->size<<endl;
693       return tab;
694    }
695    //restore from binary file
696    if ((tab->type<1)||(tab->type>2))
697    {
698       cerr<<"Problem restore_key from binary file "<<tab->filename<<
699                " type unexpexted "<<tab->type<<endl;
700       return NULL;
701    }
702    //cout<<"restore_key from binary file "<<tab->filename<<endl;
703
704    //swap disque binaire
705    FILE *fichier=fopen(tab->filename,"rb");
706    long taille;
707    fread(&taille,sizeof(long),1,fichier);
708    if (taille!=-tab->size)
709    {
710       cerr<<"Problem restore_key from binary file "<<tab->filename<<
711             " size unexpexted "<<taille<<" expected "<<-tab->size<<endl;
712       fclose(fichier);
713       return NULL;
714    }
715    if (tab->type==1)
716    {
717       if (this->verbose>5)
718       cout<<"restore_key from binary file "<<tab->filename<<
719             " number of elements "<<taille<<
720             " size_element med_float "<<sizeof(med_float)<<
721             " total_size_binary " <<taille*sizeof(med_float)<<endl;
722
723       //allocate because swap disque binaire
724       tab->tmint=new med_int[taille]; //allocate memory
725       fread(tab->tmint,sizeof(med_int),taille,fichier);
726    }
727    if (tab->type==2)
728    {
729       if (this->verbose>5)
730       cout<<"restore_key from binary file "<<tab->filename<<
731             " number of elements "<<taille<<
732             " size_element med_float "<<sizeof(med_float)<<
733             " total_size_binary " <<taille*sizeof(med_float)<<endl;
734       //allocate because swap disque binaire
735       tab->tmflo=new med_float[taille]; //allocate memory
736       for (int i=0; i<taille ; i++) tab->tmflo[i]=-1e0;
737       fread(tab->tmflo,sizeof(med_float),taille,fichier);
738       /*for (int i=0; i<taille ; i++) cout<<tab->tmflo[i]<<"/";
739       cout<<endl;*/
740    }
741    fclose(fichier);
742    tab->size=-tab->size;
743    return tab;
744 }
745
746 //************************************
747 bool ghs3dprl_mesh_wrap::test_msg_wrap()
748 //tests sur resultats fichiers msg
749 {
750    QString key1,key2,typ="FA VE ED EL"; //pour faces vertice edges elements
751    CVWtab *tab1,*tab2;
752    bool ok=TRUE;
753    //test send=receive
754    //numerotations locales sont identiques
755    long nb=typ.contains(' ',TRUE) + 1; //nb chiffres detectes
756    for (long i=0; i < nb; i++)
757    for (long ifile=1; ifile <= this->nbfiles; ifile++)
758    for (long ineig=1; ineig <= this->nbfiles; ineig++)
759    {
760       if (ifile==ineig) continue; //impossible
761       key1=key1.sprintf("MS%ld NE%ld ",ifile,ineig)+typ.section(' ',i,i)+" SE";
762       key2=key2.sprintf("MS%ld NE%ld ",ifile,ineig)+typ.section(' ',i,i)+" RE";
763       //cout<<"key "<<key1<<" et key "<<key2<<endl;
764       tab1=this->restore_key(key1);
765       //tab1=this->mestab[key1];
766       tab2=this->restore_key(key2);
767       //tab2=this->mestab[key2];
768       //cout<<"sortie key "<<key1<<" et key "<<key2<<endl;
769       if (!tab1 && !tab2) continue; //case not neighbours
770       if (!tab1)
771       {  cout<<"key "<<key1<<" inexistante avec key "<<key2<<" existante"<<endl;
772          ok=FALSE;
773       }
774       else
775       {
776        if (!tab2)
777        {  cout<<"key "<<key2<<" inexistante avec key "<<key1<<" existante"<<endl;
778           ok=FALSE;
779        }
780        else
781         if (!tab1->is_equal(tab2))
782         {  cout<<"key "<<key1<<" et key "<<key2<<" de contenu differents"<<endl;
783            ok=FALSE;
784         }
785       }
786       /*else
787          printf("key '%s' et key '%s' identiques \n",
788                            (const char *)key2,(const char *)key1);*/
789    }
790
791    //test size neighbourg=ifile
792    //numerotations locales sont differentes mais de tailles identiques
793    //pas besoin de verifier " RE " car deja fait au dessus
794    for (long i=0; i < nb; i++)
795    for (long ifile=1; ifile <= this->nbfiles; ifile++)
796    for (long ineig=ifile+1; ineig <= this->nbfiles; ineig++)
797    {
798       if (ifile==ineig) continue; //cas impossible
799       key1=key1.sprintf("MS%ld NE%ld ",ifile,ineig)+typ.section(' ',i,i)+" SE";
800       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
801       key2=key2.sprintf("MS%ld NE%ld ",ineig,ifile)+typ.section(' ',i,i)+" SE";
802       tab2=this->restore_key(key2); //tab2=this->mestab[key2];
803       if (!tab1 && !tab2) continue; //case not neighbours
804       if (!tab1)
805       {  cout<<"key "<<key1<<" inexistante avec key "<<key2<<" existante"<<endl;
806          ok=FALSE;
807       }
808       else
809       {
810        if (!tab2)
811        {  cout<<"key "<<key2<<" inexistante avec key "<<key1<<" existante"<<endl;
812           ok=FALSE;
813        }
814        else
815         if ((tab1->type!=tab2->type)||(tab1->size!=tab2->size))
816         {  cout<<"key "<<key1<<" et key "<<key2<<" de type ou tailles differents"<<endl;
817            ok=FALSE;
818         }
819       }
820    }
821    return ok;
822 }
823
824 //************************************
825 bool ghs3dprl_mesh_wrap::test_vertices_wrap()
826 //tests sur vertices
827 {
828    QString key1,key2,key11,key22,key11old,key22old;
829    CVWtab *tab1,*tab2,*tab11,*tab22;
830    bool ok=TRUE;
831    key11old="_NO_KEY";key22old="_NO_KEY";
832    //test size neighbourg=ifile
833    //numerotations locales sont differentes mais de tailles identiques
834    //pas besoin de verifier " RE " car deja fait au dessus
835    //for (int ifile=1; ifile <= this->nbfiles; ifile++)
836    //for (int ineig=ifile+1; ineig <= this->nbfiles; ineig++)
837    for (int ifile=this->nbfiles; ifile >= 1; ifile--)
838    for (int ineig=this->nbfiles; ineig >= ifile+1; ineig--)
839    {
840       if (ifile==ineig) continue; //cas impossible
841       key1=key1.sprintf("MS%d NE%d VE SE",ifile,ineig);
842       key11=key11.sprintf("NB%d VC",ifile);
843       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
844       key2=key2.sprintf("MS%d NE%d VE SE",ineig,ifile);
845       key22=key22.sprintf("NB%d VC",ineig);
846       tab2=this->restore_key(key2); //tab2=this->mestab[key2];
847       if (!tab1 && !tab2) continue; //cas non voisins
848       if (!tab1)
849       {
850          cerr<<"TestEqualityCoordinates key "<<key1<<" NOT existing but key "<<key2<<" existing"<<endl;
851          ok=FALSE; continue;
852       }
853       if (!tab2)
854       {
855          cerr<<"TestEqualityCoordinates key "<<key2<<" NOT existing but key "<<key1<<" existing"<<endl;
856          ok=FALSE; continue;
857       }
858       if (tab1->size!=tab2->size)
859       {
860          cerr<<"TestEqualityCoordinates key "<<key1<<" and key "<<key2<<" NOT same size"<<endl;
861          ok=FALSE; continue;
862       }
863       if (ok)
864       {
865          //Swap out of memory if no use
866          if ((key11old!=key11)&&(key11old!=key22))
867             this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key11old,TRUE,FALSE));
868          if ((key22old!=key11)&&(key22old!=key22))
869             this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key22old,TRUE,FALSE));
870
871          tab11=this->restore_key(key11); //tab11=this->mestab[key11];
872          tab22=this->restore_key(key22); //tab22=this->mestab[key22];
873          long i1,i2;
874          bool ok1=TRUE;
875          //test on equality of xyz_coordinates of commons vertices
876          for  (long j=0; j < tab1->size-1; j++)
877          {
878             i1=tab1->tmint[j];
879             i2=tab2->tmint[j];
880             //1 for print vertices not equals
881             if (!CVW_is_equal_vertices(tab11,i1,tab22,i2,1))
882             {
883                cerr<<j<<" Vertice "<<i1<<" != Vertice "<<i2<<"\n"<<endl;
884                ok=FALSE; ok1=FALSE;
885             }
886          }
887          if ((this->verbose>2)&&(ok1))
888             cout<<"TestEqualityCoordinates "<<tab1->size<<
889                   " Vertices "<<key1<<" and "<<key2<<" ok"<<endl;
890          if (!ok1)
891             cerr<<"TestEqualityCoordinates "<<tab1->size<<
892                   " Vertices "<<key1<<" and "<<key2<<" NO_OK"<<endl;
893          key11old=key11; key22old=key22;
894       }
895    }
896    //Swap out of memory (supposed no use?)
897    //NO because NB1&NB2 VC supposed future use
898    //YES precaution
899    this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key11old,TRUE,FALSE));
900    this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key22old,TRUE,FALSE));
901    return ok;
902 }
903
904 //************************************
905 bool ghs3dprl_mesh_wrap::Write_MEDfiles()
906 {
907    bool ok=FALSE,oklocal;
908    QString key1,tmp,filename;
909    CVWtab *tab1,*tab2;
910    med_err err;
911    char namelocal[MED_TAILLE_NOM+1];  //no more 32
912    char distfilename[MED_TAILLE_DESC+1];
913    char description[MED_TAILLE_DESC+1];
914
915    //remove path
916    //precaution because casename->med_namelocal no more 32 character
917    //if path, in this->path.
918    //20 preserve for add postfixes "_idom" etc...
919    this->casename=this->casename.section('/',-1);
920    if (this->casename.length()>20)
921    {
922       cerr<<"CaseNameMed truncated (no more 20 characters)"<<endl;
923       this->casename.truncate(20);
924    }
925    filename=this->path+this->casename;
926    ofstream file(filename); //master file
927    file<<"#MED Fichier V 2.3"<<" "<<endl;
928    file<<"# NumbersOfSubDomains"<<" "<<endl;
929    int nbdomains=this->nbfiles;
930    file<<nbdomains<<" "<<endl;
931
932    //loop on the domains
933    for (int idom=1; idom<=nbdomains; idom++)
934    {
935       oklocal=TRUE;
936       ostringstream suffix;
937       suffix<<filename<<"_"<<idom<<".med";
938       strcpy(distfilename,suffix.str().c_str());
939       //tmp=filename+tmp.sprintf("_%d.med",idom);
940       //strcpy(distfilename,tmp);
941       if (this->verbose>0)
942       {
943          if (this->verbose>2) cout<<endl;
944          cout<<"CreateMEDFile "<<idom<<" "<<distfilename<<endl;
945       }
946
947       med_idt fid=MEDouvrir(distfilename,MED_CREATION);
948       if (fid<0) cerr<<"Problem MEDouvrir "<<distfilename<<endl;
949
950       //updating the ascii master description file
951       tmp=tmp.sprintf(this->casename+"_%d",idom);
952       file<<this->casename<<" "<<idom<<" "<<
953             tmp<<" "<<"localhost "<<distfilename<<" "<<endl;
954
955       //create mesh
956       strcpy(namelocal,tmp);
957       tmp=tmp.sprintf("domain %d among %d",idom,nbdomains);
958       strcpy(description,tmp);
959       if (this->verbose>4) cout<<"File "<<distfilename<<" : "<<description<<endl;
960       //cout<<namelocal<<":"<<description<<endl
961       err=MEDmaaCr(fid,namelocal,3,MED_NON_STRUCTURE,description);
962       if (err<0) cerr<<"Problem MEDmaaCr"<<endl;
963
964       //writing node(vertices) coordinates
965       //NBx VC=files.NoBoite Vertex Coordinates
966       //                                123456789012345612345678901234561234567890123456
967       char nomcoo[3*MED_TAILLE_PNOM+1]="x               y               z               ";
968       char unicoo[3*MED_TAILLE_PNOM+1]="?               ?               ?               ";
969       key1=key1.sprintf("NB%d VC",idom); //files.NoBoite Vertex Coordinates
970       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
971       med_int nbnodes=tab1->size/3;
972
973       /*(med_idt fid, char *maa, med_int mdim, med_float *coo,
974          med_mode_switch mode_switch, med_int n,
975          med_repere type_rep, char *nom, char *unit)*/
976       err=MEDcoordEcr(fid,namelocal,3,tab1->tmflo,MED_FULL_INTERLACE,
977                                       nbnodes,MED_CART,nomcoo,unicoo);
978       if (err<0) cerr<<"Problem MEDcoordEcr"<<endl;
979       if (this->verbose>4)cout<<"NumberOfNodes="<<nbnodes<<endl;
980       this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key1,TRUE,FALSE));
981
982       //writing indices of nodes
983       med_int *arrayi=new med_int[nbnodes];
984       for (long i=0; i<nbnodes ; i++) arrayi[i]=i+1;
985       med_2_2::med_geometrie_element medgeoele0=(med_2_2::med_geometrie_element) 0;
986       err=MEDnumEcr(fid,namelocal,arrayi,nbnodes,MED_NOEUD,medgeoele0);
987       if (err<0) cerr<<"Problem MEDnumEcr of nodes"<<endl;
988       delete[] arrayi;
989
990       //writing connectivity of faces triangles of wrap by nodes
991       key1=key1.sprintf("FC%d",idom); //files.FaCes faces (wrap and triangles only)
992       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
993       med_int nbfaces=tab1->size/7;
994       if (this->verbose>4) cout<<"NumberOfTrianglesOfWrap="<<nbfaces<<endl;
995       arrayi=new med_int[nbfaces*3];
996       long ii=0,i=0 ;
997       for (long j=0; j<nbfaces ; j++)
998       {
999          arrayi[ii]=tab1->tmint[i]; ii++;
1000          arrayi[ii]=tab1->tmint[i+1]; ii++;
1001          arrayi[ii]=tab1->tmint[i+2]; ii++;
1002          i=i+7;
1003       }
1004       err=MEDconnEcr(fid,namelocal,3,arrayi,MED_FULL_INTERLACE,nbfaces,MED_FACE,MED_TRIA3,MED_NOD);
1005       if (err<0) cerr<<"Problem MEDconnEcr for triangles connectivity"<<endl;
1006       delete[] arrayi;
1007
1008       //writing indices of faces triangles of wrap
1009       //caution!
1010       //generate "overlapping of numbers of elements" in "import med file" in salome
1011       //if not in "//writing indices of tetraedes" -> arrayi[i]=!NBFACES!+i+1
1012       arrayi=new med_int[nbfaces];
1013       for (long i=0; i<nbfaces ; i++) arrayi[i]=i+1;
1014       err=MEDnumEcr(fid,namelocal,arrayi,nbfaces,MED_FACE,MED_TRIA3);
1015       if (err<0) cerr<<"Problem MEDnumEcr of triangles"<<endl;
1016       delete[] arrayi;
1017
1018       //create global family wrap default
1019       char nomfam[MED_TAILLE_NOM+1]="PART_OF_GLOBAL_WRAP";
1020       char attdes[MED_TAILLE_DESC+1]="part of wrap of global volume";
1021       char gro[MED_TAILLE_LNOM+1]="PART_OF_GLOBAL_WRAP";
1022       med_int numfam,attide,attval,natt,ngro,numfam_ini_wrap=200;
1023       //caution numfam_ini_wrap!=numfam_ini_nodes
1024       numfam=-numfam_ini_wrap; attide=1; attval=numfam; natt=1; ngro=1;
1025       err=MEDfamCr(fid,namelocal,nomfam,numfam,&attide,&attval,attdes,natt,gro,ngro);
1026       if (err<0) cerr<<"Problem MEDfamCr of "<<nomfam<<endl;
1027
1028       //for joints
1029       //init default indices of families of faces triangles of wrap = -numfam_ini_wrap
1030       //(for faces not in joints=PART_OF_GLOBAL_WRAP, why not!)
1031       //others -> -numfam_ini_wrap-indice_of_neighbourg ([1;number_of_neighbourg])
1032       //(for existing joints)
1033       int sizefamilies=nbfaces;
1034       med_int *familiesi=new med_int[sizefamilies];
1035       for (int i=0; i<sizefamilies ; i++) familiesi[i]=-numfam_ini_wrap;
1036
1037       //families known in faces in wrap PART_OF_GLOBAL_WRAP
1038       //writing indices of families of faces triangles of wrap = nsd why not?
1039       //not implemented yet because subdomain(s) of family PART_OF_GLOBAL_WRAP
1040       /*arrayi=new med_int[nbfaces];
1041       for (int i=0; i<nbfaces ; i++) arrayi[i]=tab1->tmint[(i*7)+3];
1042       err=MEDfamEcr(fid,namelocal,arrayi,nbfaces,MED_FACE,MED_TRIA3);
1043       if (err<0) cerr<<"Problem MEDfamEcr faces of wrap"<<endl;
1044       delete[] arrayi;*/
1045
1046       //writing connectivity of tetraedes by nodes
1047       key1=key1.sprintf("NB%d EV",idom); //files.NoBoite Elements Vertices (tetra only)
1048       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
1049       med_int nbtetras=tab1->size/4;
1050       if (this->verbose>4) cout<<"NumberOfTetraedes="<<nbtetras<<endl;
1051       //arrayi=new med_int[tab1->size];
1052       //for (long i=0; i<tab1->size ; i++) arrayi[i]=tab1->tmint[i];
1053       err=MEDconnEcr(fid,namelocal,3,tab1->tmint,MED_FULL_INTERLACE,nbtetras,MED_MAILLE,MED_TETRA4,MED_NOD);
1054       if (err<0) cerr<<"Problem MEDconnEcr for tetra connectivity"<<endl;
1055       //delete[] arrayi;
1056       this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key1,TRUE,FALSE));
1057
1058       //writing indices of tetraedes
1059       arrayi=new med_int[nbtetras];
1060       for (long i=0; i<nbtetras ; i++) arrayi[i]=nbfaces+i+1;
1061       err=MEDnumEcr(fid,namelocal,arrayi,nbtetras,MED_MAILLE,MED_TETRA4);
1062       if (err<0) cerr<<"Problem MEDnumEcr of tetraedes"<<endl;
1063       delete[] arrayi;
1064
1065       //writing indices of families of nodes = nrs why not?
1066       //before create families of nodes fonction of existing values of nrs of files .points
1067       arrayi=new med_int[nbnodes];
1068       key1=key1.sprintf("PO%d NRS",idom); //files.POints Vertex of wrap
1069       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
1070       med_int nbwrap=tab1->size;
1071       //families known in points in wrap
1072       //more than 30 families is stupid? (too many)?
1073       int itest,i2,ifam[30],imax=1,imess=0,numfam_ini_nodes=numfam_ini_wrap-100;
1074       //caution numfam_ini_wrap!=numfam_ini_nodes
1075       //ifam[:]<-existing values of nrs (in [0,97])
1076       //ifam[0]<-first family default=99 for new nodes IN volume (out of wrap)
1077       ifam[0]=99;
1078       for (int i=0; i<nbwrap ; i++)
1079       {
1080          itest=tab1->tmint[i];
1081          if ((itest<0)||(itest>97))
1082          {
1083             if (imess==0)
1084             {
1085                cerr<<"Problem for domain "<<idom<<" nrs="<<itest<<
1086                      " shoud be in [0;97] forced 98"<<endl;
1087                imess=1; //message only once
1088             }
1089             itest=98; //0<=nrs<=97 precaution 98=family garbage
1090          }
1091          arrayi[i]=-numfam_ini_nodes-itest;
1092          i2=0;
1093          while (1)
1094          {
1095             if (i2==imax)
1096             {
1097                ifam[imax]=itest ; imax++ ; break;
1098             }
1099             if (itest==ifam[i2]) break;
1100             i2++;
1101             if (i2>=30) break;
1102          }
1103          if (imax>=30) {
1104             cerr<<"Problem more than 30 families of nodes"<<endl;
1105             break;
1106          }
1107       }
1108       for (int i=0 ; i<imax ; i++)
1109       {
1110          //create families of nodes as nrs
1111          if (i==0)
1112             tmp=tmp.sprintf("IN_VOLUME");
1113          else
1114             tmp=tmp.sprintf("NRS_%d",ifam[i]);
1115          strcpy(nomfam,tmp);
1116          if (this->verbose>2) cout<<"CreateFamilyOfNodes_"<<nomfam<<endl;
1117          strcpy(gro,tmp);
1118          if(i==0)
1119             tmp=tmp.sprintf("nodes in local volume");
1120          else
1121             tmp=tmp.sprintf("nodes of nrs_%d on local wrap",ifam[i]);
1122          strcpy(attdes,tmp);
1123          numfam=-numfam_ini_nodes-ifam[i];
1124          //attide=1;
1125          attval=numfam;
1126          //natt=1;
1127          ngro=1;
1128          if (this->verbose>4) cout<<"MEDfamCr (nodes) of "<<nomfam<<" / "<<attdes<<" / FamilyNumber="<<numfam<<endl;
1129          err=MEDfamCr(fid,namelocal,nomfam,numfam,&attide,&attval,attdes,natt,gro,ngro);
1130          if (err<0) cerr<<"Problem MEDfamCr of "<<nomfam<<endl;
1131       }
1132       //defaults ifam[0] for new points in new volume
1133       for (int i=nbwrap; i<nbnodes ; i++) arrayi[i]=-numfam_ini_nodes-ifam[0];
1134
1135       err=MEDfamEcr(fid,namelocal,arrayi,nbnodes,MED_NOEUD,medgeoele0);
1136       if (err<0) cerr<<"Problem MEDfamEcr nodes"<<endl;
1137       delete[] arrayi;
1138
1139 /*Le nom du maillage local est une chaîne de MED_TAILLE_NOM (32) caractères.
1140   Le tableau des numéros "num" est un tableau à 1 dimension de taille égale à "n".
1141   Les numéros globaux sont obligatoirement supérieur à 1
1142   Le type de l'entite "typent" est soit MED_NOEUD,MED_MAILLE, MED_FACE ou MED_ARETE.
1143   Le type géométrique peut être :
1144   Pour les noeuds : 0.
1145   Pour les mailles : MED_POINT1, MED_SEG2, MED_SEG3, MED_TRIA3, MED_TRIA6, MED_QUAD4, MED_QUAD8, MED_POLYGONE.
1146   Pour les faces : MED_TRIA3, MED_TRIA6, MED_QUAD4, MED_QUAD8, MED_POLYGONE.
1147   Pour les arêtes : MED_SEG2 et MED_SEG3.*/
1148
1149       //writing nodes(vertices) global numbering
1150       //GLx VE=files.GLo VErtices
1151       key1=key1.sprintf("GL%d VE",idom);
1152       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
1153       nbnodes=tab1->size;
1154       if (this->verbose>2)
1155          cout<<"CreateMEDglobalNumerotation_Nodes "<<key1<<" "<<tab1->size<<endl;
1156       if (nbnodes<=0) cerr<<"Problem MEDglobalNumEcr not in memory"<<endl;
1157       //arrayi=new med_int[nbnodes];
1158       //for (int i=0; i<tab1->size ; i++) arrayi[i]=tab1->tmint[i];
1159       //med_2_2::med_geometrie_element toto=MED_POINT1;
1160       //cout<<"MED_POINT1="<<toto<<" medgeoele0="<<medgeoele0<<endl;
1161       /*MEDglobalNumEcr(med_idt fid,  char *maa, med_int *num, med_int n,
1162                med_entite_maillage type_ent, med_geometrie_element type_geo)*/
1163       err=MEDglobalNumEcr(fid,namelocal,tab1->tmint,nbnodes,MED_NOEUD,medgeoele0);
1164       if (err<0) cerr<<"Problem MEDglobalNumEcr nodes"<<endl;
1165       //cout<<"MEDglobalNumEcr vertices size="<<nbnodes<<endl;
1166       //delete[] arrayi;
1167       this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key1,TRUE,FALSE));
1168
1169       //writing faces(triangles) global numbering
1170       //GLx FA=files.GLo FAces
1171       key1=key1.sprintf("GL%d FA",idom);
1172       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
1173       nbfaces=tab1->size;
1174       //arrayi=new med_int[nbfaces];
1175       //for (int i=0; i<tab1->size ; i++) arrayi[i]=tab1->tmint[i];
1176       if (this->verbose>2)
1177          cout<<"CreateMEDglobalNumerotation_Faces "<<key1<<" "<<tab1->size<<endl;
1178       err=MEDglobalNumEcr(fid,namelocal,tab1->tmint,nbfaces,MED_FACE,MED_TRIA3);
1179       if (err<0) cerr<<"Problem MEDglobalNumEcr faces"<<endl;
1180       //cout<<"MEDglobalNumEcr faces size="<<nbfaces<<endl;
1181       //delete[] arrayi;
1182       this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key1,TRUE,FALSE));
1183
1184       //writing tetraedes global numbering
1185       //GLx EL=files.GLo ELements
1186       key1=key1.sprintf("GL%d EL",idom);
1187       tab1=this->restore_key(key1); //tab1=this->mestab[key1];
1188       med_int nbtetrasglo=tab1->size;
1189       if (nbtetrasglo!=nbtetras)
1190          cerr<<"Problem incorrect size of tetraedes global numbering"<<endl;
1191       //arrayi=new med_int[nbtetrasglo];
1192       //for (int i=0; i<tab1->size ; i++) arrayi[i]=tab1->tmint[i];
1193       if (this->verbose>2)
1194          cout<<"CreateMEDglobalNumerotation_Tetraedes "<<key1<<" "<<tab1->size<<endl;
1195       err=MEDglobalNumEcr(fid,namelocal,tab1->tmint,nbtetrasglo,MED_MAILLE,MED_TETRA4);
1196       if (err<0) cerr<<"Problem MEDglobalNumEcr tetraedes"<<endl;
1197       //cout<<"MEDglobalNumEcr tetraedes size="<<nbtetrasglo<<endl;
1198       //delete[] arrayi;
1199       this->SwapOutOfMemory_key_mesh_wrap(QRegExp(key1,TRUE,FALSE));
1200
1201       //writing joints
1202       for (int ineig=1; ineig <= this->nbfiles; ineig++)
1203       {
1204          char namejnt[MED_TAILLE_NOM+1];  //no more 32
1205          char namedist[MED_TAILLE_NOM+1];
1206          char descjnt[MED_TAILLE_DESC+1];
1207
1208          if (idom==ineig) continue; //impossible
1209          key1=key1.sprintf("MS%d NE%d VE SE",idom,ineig); //SE or RE?
1210          tab1=this->restore_key(key1);
1211          if (!tab1) continue; //case (ifile,ineig) are not neighbours=>no joints
1212          key1=key1.sprintf("MS%d NE%d VE RE",idom,ineig); //SE or RE
1213          tab2=tab1; //tab2=this->restore_key(key1); //no need because <send> equals <receive>
1214          if (!tab2) cerr<<"Problem nodes joint <send> with no <receive> in file .msg"<<endl;
1215          nbnodes=tab1->size;
1216
1217          if (this->verbose>4)
1218             cout<<"NumberOfNodesOfJoint_"<<ineig<<"="<<nbnodes<<endl;
1219          strcpy(namejnt,tmp.sprintf("joint_%d",ineig));
1220          tmp=tmp.sprintf("joint_%d among %d domains of ",ineig,nbdomains)+namelocal;
1221          strcpy(descjnt,tmp);
1222          //cout<<descjnt<<endl;
1223          strcpy(namedist,tmp.sprintf("joint_%d",idom)); //or this->casename+"_%d",ineig));
1224          err=MEDjointCr(fid,namelocal,namejnt,descjnt,ineig,namedist);
1225          if (err<0) cerr<<"Problem MEDjointCr"<<endl;
1226
1227          //writing correspondence nodes-nodes
1228          //two indices for one correspondence
1229          arrayi=new med_int[nbnodes*2];
1230          ii=0;
1231          for (int i=0; i<nbnodes ; i++)
1232          {
1233             //no need because <send> equals <receive> tab1->tmint[i]==tab2->tmint[i]
1234             arrayi[ii]=tab1->tmint[i]; ii++;
1235             arrayi[ii]=tab2->tmint[i]; ii++;
1236          }
1237          err=MEDjointEcr(fid,namelocal,namejnt,arrayi,nbnodes,
1238                            MED_NOEUD,medgeoele0,MED_NOEUD,medgeoele0);
1239          if (err<0) cerr<<"Problem MEDjointEcr nodes"<<endl;
1240          delete[] arrayi;
1241
1242          //writing correspondence triangles-triangles
1243          key1=key1.sprintf("MS%d NE%d FA SE",idom,ineig); //SE or RE?
1244          tab1=this->restore_key(key1); //tab1=this->mestab[key1];
1245          if (!tab1)
1246          {
1247             if (this->verbose>4)
1248                cout<<"NumberOfTrianglesOfJoint_"<<ineig<<"="<<0<<endl;
1249             continue; //case (ifile,ineig) are not neighbours=>no joints
1250          }
1251          key1=key1.sprintf("MS%d NE%d FA RE",idom,ineig); //SE or RE?
1252          tab2=tab1; //tab2=this->restore_key(key1); //no need because <send> equals <receive>
1253          if (!tab2) cerr<<"Problem triangles joint send with no receive"<<endl;
1254          med_int nbtriangles=tab1->size;
1255
1256          if (this->verbose>4)
1257             cout<<"NumberOfTrianglesOfJoint_"<<ineig<<"="<<nbtriangles<<endl;
1258          arrayi=new med_int[nbtriangles*2];
1259          ii=0;
1260          for (int i=0; i<nbtriangles ; i++)
1261          {
1262             //no need because <send> equals <receive> tab1->tmint[i]==tab2->tmint[i]
1263             arrayi[ii]=tab1->tmint[i]; ii++;
1264             familiesi[tab1->tmint[i]-1]=-numfam_ini_wrap-ineig;
1265             arrayi[ii]=tab2->tmint[i]; ii++;
1266             //cout<<arrayi[ii-1]<<"="<<arrayi[ii-2]<<endl;
1267          }
1268          err=MEDjointEcr(fid,namelocal,namejnt,arrayi,nbtriangles,MED_FACE,MED_TRIA3,MED_FACE,MED_TRIA3);
1269          if (err<0) cerr<<"Problem MEDjointEcr triangles"<<endl;
1270          delete[] arrayi;
1271
1272          tmp=tmp.sprintf("JOINT_%d",ineig);
1273          strcpy(nomfam,tmp);
1274
1275          //err=MEDnumEcr(fid,nomfam,arrayi,nbtriangles,MED_FACE,MED_TRIA3);
1276          //if (err<0) cerr<<"Problem MEDnumEcr of triangles of "<<nomfam<<endl;
1277
1278          //char gro[MED_TAILLE_LNOM+1]="PART_OF_GLOBAL_WRAP_PLUS_JOINTS";
1279          strcpy(gro,tmp);
1280          /*char gro[MED_TAILLE_LNOM*2+1];
1281          strcpy(gro,"PART_OF_GLOBAL_WRAP_PLUS_JOINTS    ");
1282          for (int i=32;i<MED_TAILLE_LNOM;i++) gro[i]=' ';
1283          gro[MED_TAILLE_LNOM]='\0';
1284          strcat(gro,tmp);
1285          //for (i=7;i<MED_TAILLE_LNOM;i++) gro[MED_TAILLE_LNOM+i]=' ';
1286          //gro[2*MED_TAILLE_LNOM]='\0';*/
1287
1288          tmp=tmp.sprintf("joint of neighbourg_%d on local wrap",ineig);
1289          strcpy(attdes,tmp);
1290          numfam=-numfam_ini_wrap-ineig;
1291          //attide=1;
1292          attval=numfam;
1293          //natt=1;
1294          ngro=1;
1295          if (this->verbose>2)
1296             cout<<"CreateFamilyOfFaces_"<<nomfam<<endl;
1297          if (this->verbose>4) cout<<"MEDfamCr (faces) of "<<nomfam<<" / "<<attdes<<" / FamilyNumber="<<numfam<<endl;
1298
1299          err=MEDfamCr(fid,namelocal,nomfam,numfam,&attide,&attval,attdes,natt,gro,ngro);
1300          if (err<0) cerr<<"Problem MEDfamCr of "<<nomfam<<endl;
1301
1302       }
1303       //writing indices of families of faces triangles of wrap = joint<-100 or not=100?
1304       //arrayi=new med_int[nbtriangles];
1305       //families known in faces in wrap
1306       //for (int i=0; i<nbtriangles ; i++) arrayi[i]=-100-ineig; //tab1->tlong[(i*7)+3];
1307       err=MEDfamEcr(fid,namelocal,familiesi,sizefamilies,MED_FACE,MED_TRIA3);
1308       if (err<0) cerr<<"Problem MEDfamEcr faces of all joints"<<endl;
1309       delete[] familiesi;
1310
1311       MEDfermer(fid);
1312
1313       //examples of test of med files ( ... for me!)
1314       //../Salome_321/med_231_install/bin/medconforme exemple11.med
1315       //../Salome_321/hdf5-1.6.3/bin/h5dump exemple11.med
1316       //../Salome_321/med_231_install/bin/mdump exemple11.med
1317    }
1318    return ok;
1319 }
1320