Salome HOME
Porting HEXABLOCK module on WIN32 platform.
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_DocumentModel.cxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //CS_TODO: relever les fonctions qui nécessitent updateData().
21 //        addGroupElement à tester
22 #include <algorithm>
23 #include <string>
24
25 #include "utilities.h"
26
27 #include "HEXABLOCKGUI_DocumentModel.hxx"
28 #include "HEXABLOCKGUI_DocumentItem.hxx"
29
30 #include "HEXABLOCKGUI.hxx"
31 #include "HEXABLOCKGUI_Trace.hxx"
32
33 #include "HexVertex.hxx"
34 #include "HexShape.hxx"
35 #include "HexSubShape.hxx"
36 #include "HexAssoEdge.hxx"
37 #include "HexBiCylinder.hxx"
38
39 #include <TopoDS.hxx>
40 #include <BRepBuilderAPI_MakeVertex.hxx>
41
42 #include "HEXABLOCKGUI_SalomeTools.hxx"
43
44 #include <SALOME_ListIO.hxx>
45 #include <SALOMEconfig.h>
46 #include <SUIT_ViewWindow.h>
47
48 #include <qglobal.h>
49
50 #include "HEXABLOCKGUI_VtkDocumentGraphicView.hxx"
51
52
53 //#define _DEVDEBUG_
54
55
56 using namespace std;
57 //using namespace HEXA_NS;
58 using namespace HEXABLOCK::GUI;
59
60 /*****************************************************************
61                       DocumentModel
62  *****************************************************************/
63 // DocumentModel::DocumentModel(QObject * parent):
64 DocumentModel::DocumentModel(HEXA_NS::Document* docIn, const QString& entryIn, QObject * parent):
65                               QStandardItemModel(parent),
66                               _hexaFile( new QTemporaryFile() ),
67                               _hexaDocument( docIn ),
68                               _entry( entryIn ),
69                               //   _hexaDocument(  new HEXA_NS::Document("/tmp/doc.hex") ), //CS_TODO
70
71                               _vertexDirItem( new QStandardItem(tr("TREE_ITEM_VERTEX")) ),
72                               _edgeDirItem( new QStandardItem(tr("TREE_ITEM_EDGE")) ),
73                               _quadDirItem( new QStandardItem(tr("TREE_ITEM_QUAD")) ),
74                               _hexaDirItem( new QStandardItem(tr("TREE_ITEM_HEXA")) ),
75
76                               _vectorDirItem( new QStandardItem(tr("TREE_ITEM_VECTOR")) ),
77                               _elementsDirItem( new QStandardItem(tr("TREE_ITEM_ELEMENT")) ),
78                               _crossElementsDirItem( new QStandardItem(tr("TREE_ITEM_CROSSELEMENT")) ),
79
80                               _explicitShapesDirItem( new QStandardItem(tr("TREE_ITEM_EXPSHAPE")) ),
81                               _implicitShapesDirItem( new QStandardItem(tr("TREE_ITEM_IMPSHAPE")) ),
82                               _cloudOfPointsDirItem( new QStandardItem(tr("TREE_ITEM_CLOUD_OF_POINT")) ),
83
84                               _groupDirItem( new QStandardItem(tr("TREE_ITEM_GROUP")) ),
85
86                               _lawDirItem( new QStandardItem(tr("TREE_ITEM_LAW")) ),
87                               _propagationDirItem( new QStandardItem(tr("TREE_ITEM_PROPAGATION")) ),
88
89                               _vertexItemFlags( Qt::NoItemFlags ),
90                               _edgeItemFlags( Qt::NoItemFlags ),
91                               _quadItemFlags( Qt::NoItemFlags ),
92                               _hexaItemFlags( Qt::NoItemFlags ),
93                               _vectorItemFlags( Qt::NoItemFlags ),
94                               _elementsItemFlags( Qt::NoItemFlags ),
95                               _crossElementsItemFlags( Qt::NoItemFlags ),
96
97                               _disallowEdition( false )
98 {
99     //   setColumnCount( 4 ); //CS_TEST
100     if ( !_hexaDocument && _hexaFile->open() ){
101         _hexaDocument =  new HEXA_NS::Document( _hexaFile->fileName().toLatin1() );
102         _hexaDocument->reorderFaces();
103     }
104
105     QStandardItem *parentItem = invisibleRootItem();
106     //   parentItem->setData( QString::number( reinterpret_cast<intptr_t>(_hexaDocument) ), HEXA_ENTRY_ROLE );
107     parentItem->setData( _entry, HEXA_ENTRY_ROLE );
108
109     _vertexDirItem->setData( VERTEX_DIR_TREE,           HEXA_TREE_ROLE );
110     _edgeDirItem->setData( EDGE_DIR_TREE,               HEXA_TREE_ROLE );
111     _quadDirItem->setData( QUAD_DIR_TREE,               HEXA_TREE_ROLE );
112     _hexaDirItem->setData( HEXA_DIR_TREE,               HEXA_TREE_ROLE );
113
114     _vectorDirItem->setData( VECTOR_DIR_TREE,           HEXA_TREE_ROLE );
115     _elementsDirItem->setData( ELEMENTS_DIR_TREE,           HEXA_TREE_ROLE );
116     _crossElementsDirItem->setData( CROSSELEMENTS_DIR_TREE, HEXA_TREE_ROLE );
117
118     _explicitShapesDirItem->setData( EXPLICIT_SHAPES_DIR_TREE, HEXA_TREE_ROLE );
119     _implicitShapesDirItem->setData( IMPLICIT_SHAPES_DIR_TREE, HEXA_TREE_ROLE );
120     _cloudOfPointsDirItem->setData( CLOUD_OF_POINTS_DIR_TREE, HEXA_TREE_ROLE );
121
122     _groupDirItem->setData( GROUP_DIR_TREE, HEXA_TREE_ROLE );
123
124     //CS_TODO associations
125
126     _lawDirItem->setData( LAW_DIR_TREE, HEXA_TREE_ROLE );
127     _propagationDirItem->setData( PROPAGATION_DIR_TREE, HEXA_TREE_ROLE );
128
129     parentItem->appendRow(_vertexDirItem);
130     parentItem->appendRow(_edgeDirItem);
131     parentItem->appendRow(_quadDirItem);
132     parentItem->appendRow(_hexaDirItem);
133
134     parentItem->appendRow(_vectorDirItem);
135     parentItem->appendRow(_elementsDirItem);
136     parentItem->appendRow(_crossElementsDirItem);
137
138     parentItem->appendRow(_explicitShapesDirItem);
139     parentItem->appendRow(_implicitShapesDirItem);
140
141     parentItem->appendRow(_groupDirItem);
142     parentItem->appendRow(_lawDirItem);
143     parentItem->appendRow(_propagationDirItem);
144 }
145
146 DocumentModel::~DocumentModel()
147 {
148     if (_hexaDocument != NULL)
149         delete _hexaDocument;
150
151     if (_hexaFile != NULL)
152         delete _hexaFile;
153 }
154
155 void DocumentModel::setName(const QString& name)
156 {
157     _hexaDocument->setName( name.toLocal8Bit().constData() );
158     emit nameChanged(QString(_hexaDocument->getName()));
159 }
160
161 HEXA_NS::EltBase* DocumentModel::getHexaPtr(const QModelIndex& iElt)
162 {
163     HEXA_NS::EltBase *elt = NULL;
164     switch ( /*data(iElt, HEXA_TREE_ROLE).toInt()*/iElt.data(HEXA_TREE_ROLE).toInt() ){
165     case VERTEX_TREE : elt = getHexaPtr<HEXA_NS::Vertex*>(iElt); break;
166     case EDGE_TREE : elt = getHexaPtr<HEXA_NS::Edge*>(iElt); break;
167     case QUAD_TREE : elt = getHexaPtr<HEXA_NS::Quad*>(iElt); break;
168     case HEXA_TREE : elt = getHexaPtr<HEXA_NS::Hexa*>(iElt); break;
169     case VECTOR_TREE : elt = getHexaPtr<HEXA_NS::Vector*>(iElt); break;
170     case ELEMENTS_TREE : elt = getHexaPtr<HEXA_NS::Elements*>(iElt); break;
171     case CROSSELEMENTS_TREE : elt = getHexaPtr<HEXA_NS::CrossElements*>(iElt); break;
172     case GEOMSHAPE_TREE: elt = getHexaPtr<HEXA_NS::NewShape*>(iElt); break;
173     case GEOMPOINT_TREE: elt = getHexaPtr<HEXA_NS::VertexShape*>(iElt); break;
174     case GEOMEDGE_TREE:  elt = getHexaPtr<HEXA_NS::EdgeShape*>(iElt); break;
175     case GEOMFACE_TREE:  elt = getHexaPtr<HEXA_NS::FaceShape*>(iElt); break;
176     }
177     return elt;
178 }
179
180 //get the number of elements of type 'eltType' in the document
181 int DocumentModel::getNbrElt(HEXA_NS::EnumElt eltType)
182 {
183     if (_hexaDocument == NULL) return 0;
184
185    return _hexaDocument->getNbrElt(eltType);
186 }
187
188 //get the number of elements of type 'eltType' used in the document
189 int DocumentModel::getNbrUsedElt(HEXA_NS::EnumElt eltType)
190 {
191     if (_hexaDocument == NULL) return 0;
192
193     switch(eltType) {
194     case HEXA_NS::EL_VERTEX:
195         return _hexaDocument->countUsedVertex();
196     case HEXA_NS::EL_EDGE:
197         return _hexaDocument->countUsedEdge();
198     case HEXA_NS::EL_QUAD:
199         return _hexaDocument->countUsedQuad();
200     case HEXA_NS::EL_HEXA:
201         return _hexaDocument->countUsedHexa();
202     default: return 0;
203     }
204 }
205
206 //associate a shape to the current document
207 bool DocumentModel::addShape(TopoDS_Shape& aShape, QString& name, bool publish)
208 {
209     if (aShape.IsNull() || name.isEmpty())
210         return false;
211     bool ok = (_hexaDocument != NULL ? _hexaDocument->addShape(aShape, name.toStdString().c_str()) != NULL : false);
212
213     // * publish the shape in the study
214     if (ok && publish)
215     {
216         HEXABLOCKGUI* module = HEXABLOCKGUI::getInstance();
217         if (module != NULL)
218         {
219             QMap<QString, TopoDS_Shape> topo_shapes;
220             topo_shapes[name] = aShape;
221             module->addInStudy(topo_shapes, docShapesEntry, docShapesName);
222         }
223     }
224
225     // * update data
226     if (ok)
227         updateGeomTree();
228
229     return ok;
230 }
231
232 //get the number of unused elements of type 'eltType' in the document
233 int DocumentModel::getNbrUnusedElt(HEXA_NS::EnumElt eltType)
234 {
235     if (_hexaDocument == NULL)
236         return 0;
237
238     return getNbrElt(eltType) - getNbrUsedElt(eltType);
239 }
240
241
242 //Load the current Document
243 void DocumentModel::load()
244 {
245     if (_hexaDocument == NULL)
246         return;
247
248     load(_hexaDocument->getName());
249 }
250
251
252 void DocumentModel::load( const QString& xmlFileName ) // Fill Data
253 {
254     _hexaDocument->loadXml(xmlFileName.toLocal8Bit().constData() );
255     clearAll();
256
257     fillData();
258     fillBuilder();
259     fillGeometry();
260     fillAssociation();
261     fillGroups();
262     fillMesh();
263
264     emit patternDataChanged();
265 }
266
267 bool DocumentModel::isEmpty() const
268 {
269     if (_hexaDocument == NULL) return true;
270     return _hexaDocument->isEmpty();
271 }
272
273 void DocumentModel::save( const QString& xmlFileName )
274 {
275     _hexaDocument->save( xmlFileName.toLocal8Bit().constData() );
276 }
277
278 QString DocumentModel::getName()
279 {
280     return _hexaDocument->getName();
281 }
282
283 void DocumentModel::updateData()
284 {
285     clearData();
286     fillData();
287     clearMesh();
288     fillMesh();
289     emit patternDataChanged();
290 }
291
292 void DocumentModel::updateGeomTree()
293 {
294     clearGeometry();
295     fillGeometry();
296 }
297
298 QModelIndex DocumentModel::addElementsToTree(HEXA_NS::Elements* helts, QStandardItem* tree)
299 {
300     QModelIndex eltIndex;
301     if (helts == NULL || tree == NULL)
302         return eltIndex;
303
304     updateData();
305     ElementsItem* eltsItem = new ElementsItem(helts);
306     tree->appendRow(eltsItem);
307
308     return eltsItem->index();
309 }
310
311 void DocumentModel::refresh()
312 {
313     clearAll();
314
315     fillData();
316     fillBuilder();
317     fillGeometry();
318     fillAssociation();
319     fillGroups();
320     fillMesh();
321
322     emit patternDataChanged();
323 }
324
325 void DocumentModel::clearAll()
326 {
327     clearData();
328     clearBuilder();
329     clearGeometry();
330     clearAssociation();
331     clearGroups();
332     clearMesh();
333 }
334
335 void DocumentModel::clearData()
336 {
337     _vertexDirItem->removeRows( 0, _vertexDirItem->rowCount() );
338     _edgeDirItem->removeRows(0, _edgeDirItem->rowCount() );
339     _quadDirItem->removeRows(0, _quadDirItem->rowCount() );
340     _hexaDirItem->removeRows(0, _hexaDirItem->rowCount() );
341 }
342
343
344 void DocumentModel::clearBuilder()
345 {
346     _vectorDirItem->removeRows(0, _vectorDirItem->rowCount() );
347     _elementsDirItem->removeRows(0, _elementsDirItem->rowCount() );
348     _crossElementsDirItem->removeRows(0, _crossElementsDirItem->rowCount() );
349 }
350
351 void DocumentModel::clearGeometry()
352 {
353     _explicitShapesDirItem->removeRows(0, _explicitShapesDirItem->rowCount() );
354     _implicitShapesDirItem->removeRows(0, _implicitShapesDirItem->rowCount() );
355     _cloudOfPointsDirItem->removeRows(0, _cloudOfPointsDirItem->rowCount() );
356 }
357
358 void DocumentModel::clearAssociation()
359 {
360     //CS_TODO
361 }
362
363 void DocumentModel::clearGroups()
364 {
365     _groupDirItem->removeRows(0, _groupDirItem->rowCount() );
366 }
367
368 void DocumentModel::clearMesh()
369 {
370     _lawDirItem->removeRows(0, _lawDirItem->rowCount() );
371     _propagationDirItem->removeRows(0, _propagationDirItem->rowCount() );
372 }
373
374
375 void DocumentModel::fillData()
376 {
377     PatternDataSelectionModel* pdsm = HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel();
378     if (pdsm == NULL)
379         return;
380     pdsm->clearHighlightedItems();
381
382     // DATA
383     HEXA_NS::Vertex *v     = NULL;
384     VertexItem      *vItem = NULL;
385     int nbVertex = _hexaDocument->countVertex();
386     for ( int i=0; i<nbVertex; ++i ){
387         v = _hexaDocument->getVertex(i);
388         vItem = new VertexItem(v, _entry);
389         _vertexDirItem->appendRow(vItem);
390     }
391
392     HEXA_NS::Edge *e     = NULL;
393     EdgeItem      *eItem = NULL;
394     int nbEdge = _hexaDocument->countEdge();
395     for ( int i=0; i<nbEdge; ++i ){
396         e = _hexaDocument->getEdge(i);
397         eItem = new EdgeItem(e, _entry);
398         _edgeDirItem->appendRow(eItem);
399     }
400
401     HEXA_NS::Quad *q     = NULL;
402     QuadItem      *qItem = NULL;
403     int nbQuad = _hexaDocument->countQuad();
404     for ( int i=0; i<nbQuad; ++i ){
405         q = _hexaDocument->getQuad(i);
406         qItem = new QuadItem(q, _entry);
407         _quadDirItem->appendRow(qItem);
408     }
409
410     HEXA_NS::Hexa *h     = NULL;
411     HexaItem      *hItem = NULL;
412     int nbHexa = _hexaDocument->countHexa();
413     for ( int i=0; i<nbHexa; ++i ){
414         h = _hexaDocument->getHexa(i);
415         hItem = new HexaItem(h, _entry);
416         _hexaDirItem->appendRow(hItem);
417     }
418 }
419
420
421 void DocumentModel::fillBuilder()
422 {
423     HEXA_NS::Vector *v     = NULL;
424     VectorItem      *vItem = NULL;
425     int nbVector = _hexaDocument->countVector();
426     for ( int i=0; i<nbVector; ++i ){
427         v = _hexaDocument->getVector(i);
428         vItem = new VectorItem(v);
429         _vectorDirItem->appendRow(vItem);
430     }
431
432     //******* A VOIR AVEC ALAIN POUR LE REMPLISSAGE DE L'ARBRE ELEMENTS ET CROSSELEMENTS (ByCylinder) *******/
433     //******* OU SONT STOCKES LES ELEMENTS ET LES CROSSELEMENTS DANS LE DOCUMENT?                     ******/
434
435     //   _cylinderDirItem
436 //    HEXA_NS::Cylinder *c     = NULL;
437 //    HEXA_NS::Elements* c = NULL;
438 //    CylinderItem      *cItem = NULL;
439 //    ElementsItem* cItem = NULL;
440 //    int nbCylinder = _hexaDocument->countCylinder();
441 //    for ( int i=0; i<nbCylinder; ++i ){
442 //        c = _hexaDocument->getCylinder(i);
443 //        cItem = new CylinderItem(c, _entry);
444 //        cItem = new ElementsItem(c, _entry);
445 //        _cylinderDirItem->appendRow(cItem);
446 //        _elementsDirItem->appendRow(cItem);
447 //    }
448
449     //   _pipeDirItem
450 //    HEXA_NS::Pipe *p     = NULL;
451 //    HEXA_NS::Elements* p = NULL;
452 //    PipeItem      *pItem = NULL;
453 //    ElementsItem* pItem = NULL;
454 //    int nbPipe = _hexaDocument->countPipe();
455 //    for ( int i=0; i<nbPipe; ++i ){
456 //        p = _hexaDocument->getPipe(i);
457 //        pItem = new PipeItem(p);
458 //        pItem = new ElementsItem(p, _entry);
459 //        _pipeDirItem->appendRow(pItem);
460 //        _elementsDirItem->appendRow(pItem);
461 //    }
462     //********************************************************************************************************
463 }
464
465 void DocumentModel::fillGeometry()
466 {
467     PatternGeomSelectionModel* pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
468     if (_hexaDocument == NULL || isEmpty() || pgsm == NULL)
469         return;
470
471     HEXA_NS::NewShape* shape;
472     HEXA_NS::VertexShape* node;
473     HEXA_NS::EdgeShape* line;
474     HEXA_NS::FaceShape* face;
475     TopoDS_Compound vertexCompound;
476     BRep_Builder compoundBuilder;
477
478     // * fill the shape tree
479
480     shapeById.clear();
481     pgsm->clearHighlightedItems();
482     QMap<QString, TopoDS_Shape> topo_shapes;
483     int nbShapes = _hexaDocument->countShape();
484     for (int i=0; i < nbShapes; ++i)
485     {
486         shape = _hexaDocument->getShape(i);
487         QString shapeName = shape->getName();
488         if (!docShapesEntry.contains(shapeName))
489         {
490             if (shape->getOrigin() == HEXA_NS::SH_CLOUD)
491             {
492                 compoundBuilder.MakeCompound(vertexCompound);
493                 topo_shapes[shapeName] = vertexCompound;
494             }
495             else
496                 topo_shapes[shapeName] = shape->getShape();
497         }
498
499         GeomShapeItem* shItem = new GeomShapeItem(shape);
500         if (shape->getOrigin() == HEXA_NS::SH_IMPORT)
501             // * explicit shapes
502             _explicitShapesDirItem->appendRow(shItem);
503         else
504             // * implicit shapes
505             _implicitShapesDirItem->appendRow(shItem);
506
507         //add vertices to the tree
508         QStandardItem* geomPointDirItem = NULL;
509         if (shape->getOrigin() != HEXA_NS::SH_CLOUD)
510         {
511             geomPointDirItem = new QStandardItem(tr("TREE_ITEM_VERTEX"));
512             geomPointDirItem->setData( GEOMPOINT_DIR_TREE, HEXA_TREE_ROLE );
513         }
514         if (geomPointDirItem != NULL)
515             shItem->appendRow(geomPointDirItem);
516         int nbVertexShape = shape->countVertex();
517         for (int j=0; j < nbVertexShape; ++j)
518         {
519             node = shape->getVertexShape(j);
520             GeomPointItem* gPointItem = new GeomPointItem(node);
521             if (geomPointDirItem != NULL)
522                 geomPointDirItem->appendRow(gPointItem);
523             else
524                 shItem->appendRow(gPointItem);
525             shapeById[shapeName+","+QString::number(node->getIdent())] = node;
526
527             // * update the compound of vertices
528             if (shape->getOrigin() == HEXA_NS::SH_CLOUD && !vertexCompound.IsNull())
529                 compoundBuilder.Add(topo_shapes[shapeName], node->getShape());
530         }
531
532         if (shape->getOrigin() == HEXA_NS::SH_CLOUD)
533             continue; // * it's a cloud of points, there's no edges and no quads to fill
534
535         //add edges to the tree
536         QStandardItem* geomEdgeDirItem = new QStandardItem(tr("TREE_ITEM_EDGE"));
537         geomEdgeDirItem->setData( GEOMEDGE_DIR_TREE, HEXA_TREE_ROLE );
538         shItem->appendRow(geomEdgeDirItem);
539         int nbEdgeShape = shape->countEdge();
540         for (int j = 0; j < nbEdgeShape; ++j)
541         {
542             line = shape->getEdgeShape(j);
543             GeomEdgeItem* gEdgeItem = new GeomEdgeItem(line);
544             geomEdgeDirItem->appendRow(gEdgeItem);
545             shapeById[shapeName+","+QString::number(line->getIdent())] = line;
546         }
547
548         //add faces to the tree
549         QStandardItem* geomFaceDirItem = new QStandardItem(tr("TREE_ITEM_QUAD"));
550         geomFaceDirItem->setData( GEOMFACE_DIR_TREE, HEXA_TREE_ROLE );
551         shItem->appendRow(geomFaceDirItem);
552         int nbFaceShape = shape->countFace();
553         for (int j = 0; j < nbFaceShape; ++j)
554         {
555             face = shape->getFaceShape(j);
556             GeomFaceItem* gFaceItem = new GeomFaceItem(face);
557             geomFaceDirItem->appendRow(gFaceItem);
558             shapeById[shapeName+","+QString::number(face->getIdent())] = face;
559         }
560     }
561
562     if (topo_shapes.size() == 0)
563         return;
564
565     // * register shapes
566     HEXABLOCKGUI* module = HEXABLOCKGUI::getInstance();
567     if (module != NULL)
568         module->addInStudy(topo_shapes, docShapesEntry, docShapesName);
569 }
570
571 void DocumentModel::fillAssociation()
572 {
573 }
574
575 //------------------------------------------- NEW ITEMS
576 //void DocumentModel::fillGroups()
577 //{
578 //  HEXA_NS::Group *g     = NULL;
579 //  GroupItem      *gItem = NULL;
580 //  int nbGroup = _hexaDocument->countGroup();
581 //  for ( int i=0; i<nbGroup; ++i ){
582 //    g = _hexaDocument->getGroup(i);
583 //    gItem = new GroupItem(g, _entry);
584 //    _groupDirItem->appendRow(gItem);
585 //  }
586 //}
587 //
588 //void DocumentModel::fillMesh()
589 //{
590 //  HEXA_NS::Law *l     = NULL;
591 //  LawItem      *lItem = NULL;
592 //  int nbLaw = _hexaDocument->countLaw();
593 //  for ( int i=0; i<nbLaw; ++i ){
594 //    l = _hexaDocument->getLaw(i);
595 //    lItem = new LawItem(l);
596 //    _lawDirItem->appendRow(lItem);
597 //  }
598 //
599 //  HEXA_NS::Propagation *p     = NULL;
600 //  PropagationItem      *pItem = NULL;
601 //  int nbPropagation = _hexaDocument->countPropagation();
602 //  for ( int i=0; i<nbPropagation; ++i ){
603 //    p = _hexaDocument->getPropagation(i);
604 //    pItem = new PropagationItem(p, _entry);
605 //    pItem->setText(QString("Propagation%1").arg(i) );
606 //    _propagationDirItem->appendRow(pItem);
607 //  }
608 //}
609 //----------------------------------------------- END NEW ITEMS
610
611 void DocumentModel::fillGroups()
612 {
613     HEXA_NS::Group *g     = NULL;
614     GroupItem      *gItem = NULL;
615     int nbGroup = _hexaDocument->countGroup();
616     for ( int i=0; i<nbGroup; ++i ){
617         g = _hexaDocument->getGroup(i);
618         //std::cout<<"getGroup => "<< i << std::endl;
619         gItem = new GroupItem(g);
620         gItem->setData( _entry, HEXA_DOC_ENTRY_ROLE );
621         _groupDirItem->appendRow(gItem);
622     }
623 }
624
625 void DocumentModel::fillMesh()
626 {
627     //   _lawDirItem
628     HEXA_NS::Law *l     = NULL;
629     LawItem      *lItem = NULL;
630     int nbLaw = _hexaDocument->countLaw();
631     for ( int i=0; i<nbLaw; ++i ){
632         l = _hexaDocument->getLaw(i);
633         lItem = new LawItem(l);
634         _lawDirItem->appendRow(lItem);
635     }
636
637     //   _propagationDirItem
638     HEXA_NS::Propagation *p     = NULL;
639     PropagationItem      *pItem = NULL;
640     int nbPropagation = _hexaDocument->countPropagation();
641     for ( int i=0; i<nbPropagation; ++i ){
642         p = _hexaDocument->getPropagation(i);
643         pItem = new PropagationItem(p);
644         pItem->setText(QString("Propagation%1").arg(i) );
645         pItem->setData( _entry, HEXA_DOC_ENTRY_ROLE );
646         _propagationDirItem->appendRow(pItem);
647     }
648 }
649
650 HEXA_NS::Hexa* DocumentModel::getQuadHexa(HEXA_NS::Quad* quad)
651 {
652     HEXA_NS::Hexa* hexa;
653     int nbHexa = _hexaDocument->countHexa();
654     for ( int i=0; i<nbHexa; ++i ){
655         hexa = _hexaDocument->getHexa(i);
656         if (hexa->findQuad(quad) > -1) return hexa;
657     }
658     return NULL;
659 }
660
661
662 Qt::ItemFlags DocumentModel::flags(const QModelIndex &index) const
663 {
664     Qt::ItemFlags flags;
665
666     if (!index.isValid()) return Qt::ItemIsEnabled;
667
668     if ( _disallowEdition ){
669         return QAbstractItemModel::flags(index) | Qt::ItemFlags( ~Qt::ItemIsEditable );
670     } else {
671         return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
672     }
673 }
674
675 void DocumentModel::allowEdition()
676 {
677     _disallowEdition = false;
678 }
679
680 void DocumentModel::disallowEdition()
681 {
682     _disallowEdition = true;
683 }
684
685 void DocumentModel::allowDataSelectionOnly()
686 {
687     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
688     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
689     _quadItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
690     _hexaItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
691
692     _vectorItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
693     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
694     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
695
696     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
697     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
698     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
699 }
700
701 void DocumentModel::allowVertexSelectionOnly()
702 {
703     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable);
704     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
705     _quadItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
706     _hexaItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
707
708     _vectorItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
709     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
710     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
711
712     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
713     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
714     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
715
716     //     emit layoutChanged();
717 }
718
719 void DocumentModel::allowEdgeSelectionOnly()
720 {
721     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
722     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
723     _quadItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
724     _hexaItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
725
726     _vectorItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
727     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
728     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
729
730     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
731     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
732     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
733 }
734
735 void DocumentModel::allowQuadSelectionOnly()
736 {
737     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
738     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
739     _quadItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
740     _hexaItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
741
742     _vectorItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
743     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
744     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
745
746     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
747     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
748     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
749 }
750
751 void DocumentModel::allowHexaSelectionOnly()
752 {
753     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
754     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
755     _quadItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
756     _hexaItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
757
758     _vectorItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
759     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
760     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
761
762     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
763     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
764     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
765 }
766
767 void DocumentModel::allowVectorSelectionOnly()
768 {
769     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
770     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
771     _quadItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
772     _hexaItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
773
774     _vectorItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
775     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
776     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
777
778     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
779     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
780     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
781 }
782
783 void DocumentModel::allowCylinderSelectionOnly()
784 {
785     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
786     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
787     _quadItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
788     _hexaItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
789
790     _vectorItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
791     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
792     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
793
794     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
795     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
796     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
797 }
798
799 void DocumentModel::allowPipeSelectionOnly()
800 {
801     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
802     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
803     _quadItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
804     _hexaItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
805
806     _vectorItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
807     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
808     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
809
810     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
811     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
812     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
813 }
814
815 void DocumentModel::allowElementsSelectionOnly()
816 {
817     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
818     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
819     _quadItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
820     _hexaItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
821
822     _vectorItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
823     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
824     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
825
826     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
827     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
828     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
829 }
830
831 void DocumentModel::allowCrossElementsSelectionOnly()
832 {
833     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
834     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
835     _quadItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
836     _hexaItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
837
838     _vectorItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
839     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
840     _crossElementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEditable );
841
842     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
843     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
844     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
845 }
846
847 void DocumentModel::allowLawSelectionOnly()
848 {
849     _vertexItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
850     _edgeItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
851     _quadItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
852     _hexaItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
853
854     _vectorItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEnabled );
855     _elementsItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
856     _crossElementsItemFlags = Qt::ItemFlags( Qt::ItemIsEnabled );
857
858     _groupItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
859     _lawItemFlags   = Qt::ItemFlags( ~Qt::ItemIsEditable );
860     _propagationItemFlags = Qt::ItemFlags( ~Qt::ItemIsEnabled );
861 }
862
863
864 void DocumentModel::setName( const QModelIndex& iElt, const QString& name )
865 {
866     HEXA_NS::EltBase *elt = NULL;
867
868     switch ( data(iElt, HEXA_TREE_ROLE).toInt() ){
869     case GROUP_TREE :
870     {
871         HEXA_NS::Group* grp = iElt.data( HEXA_DATA_ROLE ).value< HEXA_NS::Group* >();
872         grp->setName( name.toLatin1().data() );
873         break;
874     }
875     case LAW_TREE :
876     {
877         HEXA_NS::Law* l = iElt.data( HEXA_DATA_ROLE ).value< HEXA_NS::Law* >();
878         l->setName( name.toLatin1().data() );
879         break;
880     }
881     //   case PROPAGATION_TREE : elt = iElt.data( HEXA_DATA_ROLE ).value< HEXA_NS::Propagation* >(); break;
882     default: elt = getHexaPtr(iElt);
883     }
884
885     if ( elt != NULL ) elt->setName( name.toStdString() );
886     setData( iElt, name );
887
888 }
889
890 bool DocumentModel::clearEltAssociations( const QModelIndex& iElt )
891 {
892     bool isOk = false;
893     HEXA_NS::EltBase *elt = getHexaPtr(iElt);
894
895     if ( elt != NULL ){
896         elt->clearAssociation();
897         setData( iElt, QVariant(), HEXA_ASSOC_ENTRY_ROLE );
898         isOk = true;
899     }
900
901     return isOk;
902 }
903
904 void DocumentModel::clearAssociation(HEXA_NS::EnumElt& eltType)
905 {
906     if (_hexaDocument != NULL)
907         _hexaDocument->clearAssociation(eltType);
908
909     QModelIndex iElt;
910     HEXA_NS::Vertex* vertex;
911     HEXA_NS::Edge* edge;
912     HEXA_NS::Quad* quad;
913     int nbElts;
914
915     if (HEXABLOCKGUI::currentDocGView == NULL) return;
916     PatternDataSelectionModel* pdsm = HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel();
917     PatternDataModel* pdm = HEXABLOCKGUI::currentDocGView->getPatternDataModel();
918     if (pdsm == NULL || pdm == NULL) return;
919
920     //update associations in the model
921     if (eltType == HEXA_NS::EL_VERTEX)
922     {
923         nbElts = _hexaDocument->countVertex();
924         for (int i = 0; i < nbElts; ++i)
925         {
926             vertex = _hexaDocument->getVertex(i);
927             iElt = pdm->mapToSource(pdsm->indexBy(HEXA_DATA_ROLE, QVariant::fromValue(vertex)));
928             if (iElt.isValid())
929                 setData( iElt, QVariant(), HEXA_ASSOC_ENTRY_ROLE );
930         }
931     }
932     else if (eltType == HEXA_NS::EL_EDGE)
933     {
934         nbElts = _hexaDocument->countEdge();
935         for (int i = 0; i < nbElts; ++i)
936         {
937             edge = _hexaDocument->getEdge(i);
938             iElt = pdm->mapToSource(pdsm->indexBy(HEXA_DATA_ROLE, QVariant::fromValue(edge)));
939             if (iElt.isValid())
940                 setData( iElt, QVariant(), HEXA_ASSOC_ENTRY_ROLE );
941         }
942     }
943     else if (eltType == HEXA_NS::EL_QUAD)
944     {
945         nbElts = _hexaDocument->countQuad();
946         for (int i = 0; i < nbElts; ++i)
947         {
948             quad = _hexaDocument->getQuad(i);
949             iElt = pdm->mapToSource(pdsm->indexBy(HEXA_DATA_ROLE, QVariant::fromValue(quad)));
950             if (iElt.isValid())
951                 setData( iElt, QVariant(), HEXA_ASSOC_ENTRY_ROLE );
952         }
953     }
954 }
955
956
957 QModelIndex DocumentModel::addVertex( double x, double y, double z )
958 {
959     QModelIndex vertexIndex;
960
961     HEXA_NS::Vertex* hv = _hexaDocument->addVertex(x, y, z);
962     if (BadElement(hv)) return vertexIndex;
963
964     VertexItem* v = new VertexItem(hv, _entry);
965     _vertexDirItem->appendRow(v);
966     vertexIndex = v->index();
967     emit patternDataChanged();
968
969     return vertexIndex;
970 }
971
972 QModelIndex DocumentModel::addEdgeVertices (const QModelIndex &i_v0, const QModelIndex &i_v1 )
973 {
974     QModelIndex edgeIndex;
975
976     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(i_v0);
977     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(i_v1);
978
979     HEXA_NS::Edge* he = _hexaDocument->addEdge( hv0, hv1 );
980     if ( BadElement(he) ) return edgeIndex;
981
982     EdgeItem* e = new EdgeItem(he, _entry);
983     _edgeDirItem->appendRow(e);
984     edgeIndex = e->index();
985     emit patternDataChanged();
986
987     return edgeIndex;
988 }
989
990 QModelIndex DocumentModel::addEdgeVector ( const QModelIndex &i_v, const QModelIndex &i_vec )
991 {
992     QModelIndex edgeIndex;
993
994     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(i_v);
995     HEXA_NS::Vector* hvec = getHexaPtr<HEXA_NS::Vector*>(i_vec);
996
997     HEXA_NS::Edge* he = _hexaDocument->addEdgeVector( hv0, hvec );
998     if ( BadElement(he) ) return edgeIndex;
999
1000     EdgeItem* e = new EdgeItem(he, _entry);
1001     _vectorDirItem->appendRow(e);
1002     edgeIndex = e->index();
1003     emit patternDataChanged();
1004
1005     return edgeIndex;
1006 }
1007
1008 QModelIndex DocumentModel::addQuadVertices( const QModelIndex &i_v0, const QModelIndex &i_v1,
1009         const QModelIndex &i_v2, const QModelIndex &i_v3 )
1010 { //CS_TODO : gestion erreur
1011     QModelIndex quadIndex;
1012
1013     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(i_v0);
1014     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(i_v1);
1015     HEXA_NS::Vertex* hv2 = getHexaPtr<HEXA_NS::Vertex*>(i_v2);
1016     HEXA_NS::Vertex* hv3 = getHexaPtr<HEXA_NS::Vertex*>(i_v3);
1017
1018     if ( hv0 && hv1 && hv2 && hv3 ){
1019         HEXA_NS::Quad* hq = _hexaDocument->addQuadVertices( hv0, hv1, hv2, hv3 );
1020         if ( BadElement(hq) ) return quadIndex;
1021
1022         QuadItem* q = new QuadItem(hq, _entry);
1023         _quadDirItem->appendRow(q);
1024         quadIndex = q->index();
1025         emit patternDataChanged();
1026     }
1027
1028     return quadIndex;
1029 }
1030
1031 QModelIndex DocumentModel::addQuadEdges( const QModelIndex &e0, const QModelIndex &e1,
1032         const QModelIndex &e2, const QModelIndex &e3 )
1033 { //CS_TODO
1034     QModelIndex quadIndex;
1035
1036     HEXA_NS::Edge* he0 = getHexaPtr<HEXA_NS::Edge*>(e0);
1037     HEXA_NS::Edge* he1 = getHexaPtr<HEXA_NS::Edge*>(e1);
1038     HEXA_NS::Edge* he2 = getHexaPtr<HEXA_NS::Edge*>(e2);
1039     HEXA_NS::Edge* he3 = getHexaPtr<HEXA_NS::Edge*>(e3);
1040
1041     if ( he0 && he1 && he2 && he3 ){
1042
1043         HEXA_NS::Quad* hq = _hexaDocument->addQuad( he0, he1, he2, he3 );
1044         if ( BadElement(hq) ) return quadIndex;
1045
1046         QuadItem* q = new QuadItem(hq, _entry);
1047         _quadDirItem->appendRow(q);
1048         quadIndex = q->index();
1049         emit patternDataChanged();
1050     }
1051
1052     return quadIndex;
1053 }
1054
1055 QModelIndex DocumentModel::addHexaVertices(
1056         const QModelIndex &iv0, const QModelIndex &iv1,
1057         const QModelIndex &iv2, const QModelIndex &iv3,
1058         const QModelIndex &iv4, const QModelIndex &iv5,
1059         const QModelIndex &iv6, const QModelIndex &iv7 )
1060 {
1061     QModelIndex iHexa;
1062
1063     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(iv0);
1064     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(iv1);
1065     HEXA_NS::Vertex* hv2 = getHexaPtr<HEXA_NS::Vertex*>(iv2);
1066     HEXA_NS::Vertex* hv3 = getHexaPtr<HEXA_NS::Vertex*>(iv3);
1067     HEXA_NS::Vertex* hv4 = getHexaPtr<HEXA_NS::Vertex*>(iv4);
1068     HEXA_NS::Vertex* hv5 = getHexaPtr<HEXA_NS::Vertex*>(iv5);
1069     HEXA_NS::Vertex* hv6 = getHexaPtr<HEXA_NS::Vertex*>(iv6);
1070     HEXA_NS::Vertex* hv7 = getHexaPtr<HEXA_NS::Vertex*>(iv7);
1071
1072     HEXA_NS::Hexa* hh = _hexaDocument->addHexaVertices( hv0, hv1, hv2, hv3,
1073             hv4, hv5, hv6, hv7 );
1074
1075     if ( BadElement(hh) ) return iHexa;
1076
1077     HexaItem* h = new HexaItem(hh, _entry);
1078     _hexaDirItem->appendRow(h);
1079     iHexa = h->index();
1080     emit patternDataChanged();
1081
1082     return iHexa;
1083 }
1084
1085 QModelIndex DocumentModel::addHexaQuad( const QModelIndex &i_q0, const QModelIndex &i_q1,
1086         const QModelIndex &i_q2,const QModelIndex &i_q3, const QModelIndex &i_q4, const QModelIndex &i_q5 )
1087 { //CS_TODO : gestion erreur
1088     QModelIndex hexaIndex;
1089
1090     HEXA_NS::Quad* hq0 = getHexaPtr<HEXA_NS::Quad*>(i_q0);
1091     HEXA_NS::Quad* hq1 = getHexaPtr<HEXA_NS::Quad*>(i_q1);
1092     HEXA_NS::Quad* hq2 = getHexaPtr<HEXA_NS::Quad*>(i_q2);
1093     HEXA_NS::Quad* hq3 = getHexaPtr<HEXA_NS::Quad*>(i_q3);
1094     HEXA_NS::Quad* hq4 = getHexaPtr<HEXA_NS::Quad*>(i_q4);
1095     HEXA_NS::Quad* hq5 = getHexaPtr<HEXA_NS::Quad*>(i_q5);
1096
1097     HEXA_NS::Hexa* hh = _hexaDocument->addHexa( hq0, hq1, hq2, hq3, hq4, hq5 );
1098     if ( BadElement(hh) ) return hexaIndex;
1099
1100     HexaItem* h = new HexaItem(hh, _entry);
1101     _hexaDirItem->appendRow(h);
1102     hexaIndex = h->index();
1103     emit patternDataChanged();
1104
1105     return hexaIndex;
1106 }
1107
1108 QModelIndex DocumentModel::addHexaQuads( const QModelIndexList &iquads)
1109 {
1110     QModelIndex hexaIndex;
1111
1112     HEXA_NS::Hexa* hh = NULL;
1113     HEXA_NS::Quad* hq0, *hq1, *hq2, *hq3, *hq4, *hq5 = NULL;
1114
1115     hq0 = getHexaPtr<HEXA_NS::Quad*>(iquads.value(0));
1116     hq1 = getHexaPtr<HEXA_NS::Quad*>(iquads.value(1));
1117     hq2 = getHexaPtr<HEXA_NS::Quad*>(iquads.value(2));
1118     hq3 = getHexaPtr<HEXA_NS::Quad*>(iquads.value(3));
1119     hq4 = getHexaPtr<HEXA_NS::Quad*>(iquads.value(4));
1120     hq5 = getHexaPtr<HEXA_NS::Quad*>(iquads.value(5));
1121
1122     if ( hq0 && hq1 && hq2 && hq3 && hq4 && hq5 )
1123         hh = _hexaDocument->addHexa( hq0, hq1, hq2, hq3, hq4, hq5 );
1124     else if ( hq0 && hq1 && hq2 && hq3 && hq4 )
1125         hh = _hexaDocument->addHexa5Quads( hq0, hq1, hq2, hq3, hq4 );
1126     else if ( hq0 && hq1 && hq2 && hq3 )
1127         hh = _hexaDocument->addHexa4Quads( hq0, hq1, hq2, hq3 );
1128     else if ( hq0 && hq1 && hq2)
1129         hh = _hexaDocument->addHexa3Quads( hq0, hq1, hq2 );
1130     else if ( hq0 && hq1 )
1131         hh = _hexaDocument->addHexa2Quads( hq0, hq1);
1132
1133     if ( BadElement(hh) ) return hexaIndex;
1134
1135     updateData();
1136     unsigned int nbRows = _hexaDirItem->rowCount();
1137     if (nbRows > 0)
1138         hexaIndex = _hexaDirItem->child(nbRows-1)->index();
1139
1140     return hexaIndex;
1141 }
1142
1143 QModelIndex DocumentModel::addVector( double dx, double dy, double dz )
1144 {
1145     QModelIndex vectorIndex;
1146
1147     HEXA_NS::Vector* hv = _hexaDocument->addVector(dx, dy, dz);
1148     if ( BadElement(hv) ) return vectorIndex; //Invalid index
1149
1150     VectorItem* v = new VectorItem(hv);
1151     _vectorDirItem->appendRow(v);
1152     vectorIndex = v->index();
1153
1154     return vectorIndex;
1155 }
1156
1157 QModelIndex DocumentModel::addVectorVertices( const QModelIndex &iv0, const QModelIndex &iv1 )
1158 {
1159     QModelIndex iVec;
1160
1161     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(iv0);
1162     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(iv1);
1163
1164     HEXA_NS::Vector* hvec = _hexaDocument->addVectorVertices( hv0, hv1 );
1165     if ( BadElement(hvec) ) return iVec;
1166
1167     VectorItem* vec = new VectorItem(hvec);
1168     _vectorDirItem->appendRow(vec);
1169     iVec = vec->index();
1170
1171     return iVec;
1172 }
1173
1174
1175 // ************  EDIT HEXABLOCK MODEL ************
1176
1177 bool DocumentModel::updateVertex( const QModelIndex& ivertex, double x, double y, double z )
1178 {
1179     bool ret = false;
1180
1181     HEXA_NS::Vertex* hVertex = getHexaPtr<HEXA_NS::Vertex*>(ivertex);
1182
1183     if ( hVertex ){
1184 //        hVertex->setName( name.toStdString() );
1185         hVertex->setX ( x );
1186         hVertex->setY ( y );
1187         hVertex->setZ ( z );
1188         emit patternDataChanged();
1189         ret = true;
1190     }
1191
1192     return ret;
1193 }
1194
1195 bool DocumentModel::removeHexa( const QModelIndex& ihexa )
1196 {
1197     HEXA_NS::Hexa* hHexa = getHexaPtr<HEXA_NS::Hexa*>(ihexa);
1198
1199     int r = _hexaDocument->removeHexa( hHexa );
1200     if ( r == HOK ){
1201         updateData();
1202         return true;
1203     }
1204
1205     return false;
1206 }
1207
1208 bool DocumentModel::removeConnectedHexa( const QModelIndex& ihexa )
1209 {
1210     HEXA_NS::Hexa* hHexa = getHexaPtr<HEXA_NS::Hexa*>(ihexa);
1211
1212     int r = _hexaDocument->removeConnectedHexa( hHexa );
1213     if ( r == HOK ){
1214         updateData();
1215         return true;
1216     }
1217
1218     return false;
1219 }
1220
1221 bool DocumentModel::mergeVertices( const QModelIndex &iv0, const QModelIndex &iv1 ) //CS_TODO : impact sur le model?
1222 {
1223     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(iv0);
1224     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(iv1);
1225
1226     int r = _hexaDocument->mergeVertices( hv0, hv1 );
1227     if ( r == HOK ){
1228         updateData(); //CS_TODO more or less?
1229         return true;
1230     }
1231
1232     return false;
1233 }
1234
1235 bool DocumentModel::mergeEdges( const QModelIndex &ie0, const QModelIndex &ie1,
1236         const QModelIndex &iv0, const QModelIndex &iv1 )
1237 //CS_TODO : impact sur le model?
1238 {
1239     HEXA_NS::Edge* he0 = getHexaPtr<HEXA_NS::Edge*>(ie0);
1240     HEXA_NS::Edge* he1 = getHexaPtr<HEXA_NS::Edge*>(ie1);
1241
1242     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(iv0);
1243     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(iv1);
1244
1245     //Merge
1246     int r = _hexaDocument->mergeEdges( he0, he1, hv0, hv1 );
1247     if ( r == HOK ){
1248         updateData();
1249         return true;
1250     }
1251
1252     return false;
1253 }
1254
1255 bool DocumentModel::mergeQuads( const QModelIndex& iquad0, const QModelIndex& iquad1,
1256         const QModelIndex& iv0, const QModelIndex& iv1,
1257         const QModelIndex& iv2, const QModelIndex& iv3 )
1258 {
1259     HEXA_NS::Quad* hquad0 = data(iquad0, HEXA_DATA_ROLE).value<HEXA_NS::Quad *>();
1260     HEXA_NS::Quad* hquad1 = data(iquad1, HEXA_DATA_ROLE).value<HEXA_NS::Quad *>();
1261
1262     HEXA_NS::Vertex* hv0 = getHexaPtr<HEXA_NS::Vertex*>(iv0);
1263     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(iv1);
1264     HEXA_NS::Vertex* hv2 = getHexaPtr<HEXA_NS::Vertex*>(iv2);
1265     HEXA_NS::Vertex* hv3 = getHexaPtr<HEXA_NS::Vertex*>(iv3);
1266
1267     int r = _hexaDocument->mergeQuads( hquad0, hquad1, hv0, hv1, hv2, hv3 );
1268     if ( r == HOK ){
1269         updateData();
1270         return true;
1271     }
1272
1273     return false;
1274 }
1275
1276 //
1277 QModelIndex DocumentModel::disconnectVertex( const QModelIndex& ihexa, const QModelIndex& ivertex )
1278 {
1279     QModelIndex iElts;
1280
1281     HEXA_NS::Hexa*   hHexa   = getHexaPtr<HEXA_NS::Hexa*>(ihexa);
1282     HEXA_NS::Vertex* hVertex = getHexaPtr<HEXA_NS::Vertex*>(ivertex);
1283
1284     HEXA_NS::Elements* hElts = _hexaDocument->disconnectVertex( hHexa, hVertex );
1285     if ( BadElement(hElts) ) return iElts;
1286
1287     updateData(); //CS_TO_CHECK
1288     ElementsItem* elts = new ElementsItem(hElts);
1289     _elementsDirItem->appendRow(elts);
1290     iElts = elts->index();
1291
1292     return iElts;
1293 }
1294
1295 QModelIndex DocumentModel::disconnectEdge( const QModelIndex& ihexa, const QModelIndex& iedge )
1296 {
1297     QModelIndex iElts;
1298
1299     HEXA_NS::Hexa* hHexa = getHexaPtr<HEXA_NS::Hexa*>(ihexa);
1300     HEXA_NS::Edge* hEdge = getHexaPtr<HEXA_NS::Edge*>(iedge);
1301
1302     HEXA_NS::Elements* hElts = _hexaDocument->disconnectEdge( hHexa, hEdge );
1303     if ( BadElement(hElts) ) return iElts;
1304
1305     updateData(); //CS_TO_CHECK
1306     ElementsItem* elts = new ElementsItem(hElts);
1307     _elementsDirItem->appendRow(elts);
1308     iElts = elts->index();
1309
1310     return iElts;
1311 }
1312
1313
1314 QModelIndex DocumentModel::disconnectEdges( const QModelIndexList& ihexas, const QModelIndexList& iedges )
1315 {
1316     QModelIndex iElts;
1317
1318     HEXA_NS::Hexas hHexas;
1319     HEXA_NS::Edges hEdges;
1320
1321     //edges list
1322     HEXA_NS::Edge* hedge = NULL;
1323     foreach( const QModelIndex& iedge, iedges ){
1324         hedge = getHexaPtr<HEXA_NS::Edge*>(iedge);
1325         hEdges.push_back( hedge );
1326     }
1327
1328     //hexas list
1329     HEXA_NS::Hexa* hhexa = NULL;
1330     foreach( const QModelIndex& ihexa, ihexas ){
1331         hhexa = getHexaPtr<HEXA_NS::Hexa*>(ihexa);
1332         hHexas.push_back( hhexa );
1333     }
1334
1335
1336     HEXA_NS::Elements* hElts = _hexaDocument->disconnectEdges( hHexas, hEdges );
1337     if ( BadElement(hElts) ) return iElts;
1338
1339     updateData(); //CS_TO_CHECK
1340     ElementsItem* elts = new ElementsItem(hElts);
1341     _elementsDirItem->appendRow(elts);
1342     iElts = elts->index();
1343
1344     return iElts;
1345 }
1346
1347
1348 QModelIndex DocumentModel::disconnectQuad( const QModelIndex& ihexa, const QModelIndex& iquad )
1349 {
1350     QModelIndex iElts;
1351
1352     HEXA_NS::Hexa* hHexa = getHexaPtr<HEXA_NS::Hexa*>(ihexa);
1353     HEXA_NS::Quad* hQuad = getHexaPtr<HEXA_NS::Quad*>(iquad);
1354
1355     HEXA_NS::Elements* hElts = _hexaDocument->disconnectQuad( hHexa, hQuad );
1356     if ( BadElement(hElts) ) return iElts;
1357
1358     updateData(); //CS_TO_CHECK
1359     ElementsItem* elts = new ElementsItem(hElts);
1360     _elementsDirItem->appendRow(elts);
1361     iElts = elts->index();
1362
1363     return iElts;
1364 }
1365
1366
1367 QModelIndex DocumentModel::makeTranslation( const QModelIndex& ielts, const QModelIndex& ivec )
1368 {
1369     QModelIndex iElts;
1370
1371     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1372     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1373
1374     HEXA_NS::Elements* hNewElts = _hexaDocument->makeTranslation( hElts, hVec );
1375     if ( BadElement(hNewElts) ) return iElts;
1376
1377     updateData(); //CS_TODO  more or less?
1378     ElementsItem* eltsItem = new ElementsItem(hNewElts);
1379     _elementsDirItem->appendRow(eltsItem);
1380     iElts = eltsItem->index();
1381
1382     return iElts;
1383 }
1384
1385
1386
1387 QModelIndex DocumentModel::makeScale( const QModelIndex& ielts, const QModelIndex& ivex, double k )
1388 {
1389     QModelIndex iElts;
1390
1391     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1392     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(ivex);
1393
1394     HEXA_NS::Elements* hNewElts = _hexaDocument->makeScale( hElts, hVex, k );
1395     if ( BadElement(hNewElts) ) return iElts;
1396
1397     updateData(); //CS_TODO  more or less?
1398     ElementsItem* eltsItem = new ElementsItem(hNewElts);
1399     _elementsDirItem->appendRow(eltsItem);
1400     iElts = eltsItem->index();
1401
1402     return iElts;
1403 }
1404
1405
1406
1407 QModelIndex DocumentModel::makeRotation( const QModelIndex& ielts,
1408         const QModelIndex& iv,
1409         const QModelIndex& ivec, double angle )
1410 {
1411     QModelIndex iElts;
1412
1413     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1414     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(iv);
1415     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1416
1417     HEXA_NS::Elements* hNewElts = _hexaDocument->makeRotation( hElts, hVex, hVec, angle );
1418     if ( BadElement(hNewElts) ) return iElts;
1419
1420     updateData(); //CS_TODO  more or less?
1421     ElementsItem* eltsItem = new ElementsItem(hNewElts);
1422     _elementsDirItem->appendRow(eltsItem);
1423     iElts = eltsItem->index();
1424
1425     return iElts;
1426 }
1427
1428
1429 QModelIndex DocumentModel::makeSymmetryPoint( const QModelIndex& ielts, const QModelIndex& iv )
1430 {
1431     QModelIndex iElts;
1432
1433     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1434     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(iv);
1435
1436     HEXA_NS::Elements* hNewElts = _hexaDocument->makeSymmetryPoint (hElts, hVex);
1437     if ( BadElement(hNewElts) ) return iElts;
1438
1439     updateData(); //CS_TODO  more or less?
1440     ElementsItem* eltsItem = new ElementsItem(hNewElts);
1441     _elementsDirItem->appendRow(eltsItem);
1442     iElts = eltsItem->index();
1443
1444     return iElts;
1445 }
1446
1447
1448 QModelIndex DocumentModel::makeSymmetryLine( const QModelIndex& ielts,
1449         const QModelIndex& iv,
1450         const QModelIndex& ivec )
1451 {
1452     QModelIndex iElts;
1453
1454     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1455     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(iv);
1456     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1457
1458
1459     HEXA_NS::Elements* hNewElts = _hexaDocument->makeSymmetryLine( hElts, hVex, hVec );
1460     if ( BadElement(hNewElts) ) return iElts;
1461
1462     updateData(); //CS_TODO  more or less?
1463     ElementsItem* eltsItem = new ElementsItem(hNewElts);
1464     _elementsDirItem->appendRow(eltsItem);
1465     iElts = eltsItem->index();
1466
1467     return iElts;
1468 }
1469
1470
1471 QModelIndex DocumentModel::makeSymmetryPlane( const QModelIndex& ielts, const QModelIndex& iv, const QModelIndex& ivec )
1472 {
1473     QModelIndex iElts;
1474
1475     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1476     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(iv);
1477     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1478
1479     HEXA_NS::Elements* hNewElts = _hexaDocument->makeSymmetryPlane( hElts, hVex, hVec );
1480     if ( BadElement(hNewElts) ) return iElts;
1481
1482     updateData(); //CS_TODO  more or less?
1483     ElementsItem* eltsItem = new ElementsItem(hNewElts);
1484     _elementsDirItem->appendRow(eltsItem);
1485     iElts = eltsItem->index();
1486
1487     return iElts;
1488 }
1489
1490
1491 bool DocumentModel::performTranslation( const QModelIndex& ielts, const QModelIndex& ivec )
1492 {
1493     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1494     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1495
1496     int r = _hexaDocument->performTranslation (hElts, hVec);
1497     if ( r == HOK ){ //Job well done?
1498         updateData();
1499         return true;
1500     }
1501
1502     return false;
1503 }
1504
1505
1506 //
1507 bool DocumentModel::performScale( const QModelIndex& ielts, const QModelIndex& ivex, double k )
1508 {
1509     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1510     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(ivex);
1511
1512     int r = _hexaDocument->performScale (hElts, hVex, k);
1513     if ( r == HOK ){
1514         updateData();
1515         return true;
1516     }
1517
1518     return false;
1519 }
1520
1521 //
1522 bool DocumentModel::performRotation( const QModelIndex& ielts, const QModelIndex& ivex, const QModelIndex& ivec, double angle )
1523 {
1524     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1525     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(ivex);
1526     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1527
1528     int r = _hexaDocument-> performRotation( hElts, hVex, hVec, angle );
1529     if ( r == HOK ){
1530         updateData();
1531         return true;
1532     }
1533
1534     return false;
1535 }
1536
1537
1538 //
1539 bool DocumentModel::performSymmetryPoint( const QModelIndex& ielts, const QModelIndex& ivex )
1540 {
1541     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1542     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(ivex);
1543
1544     int r = _hexaDocument->performSymmetryPoint( hElts, hVex );
1545     if ( r == HOK ){
1546         updateData();
1547         return true;
1548     }
1549
1550     return false;
1551 }
1552
1553
1554 bool DocumentModel::performSymmetryLine( const QModelIndex& ielts, const QModelIndex& ivex, const QModelIndex& ivec )
1555 {
1556     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1557     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(ivex);
1558     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1559
1560     int r = _hexaDocument->performSymmetryLine( hElts, hVex, hVec );
1561     if ( r == HOK ){
1562         updateData();
1563         return true;
1564     }
1565
1566     return false;
1567 }
1568
1569
1570 bool DocumentModel::performSymmetryPlane( const QModelIndex& ielts,
1571         const QModelIndex& ivex,
1572         const QModelIndex& ivec )
1573 {
1574     HEXA_NS::Elements* hElts = getHexaPtr<HEXA_NS::Elements*>(ielts);
1575     HEXA_NS::Vertex*   hVex  = getHexaPtr<HEXA_NS::Vertex*>(ivex);
1576     HEXA_NS::Vector*   hVec  = getHexaPtr<HEXA_NS::Vector*>(ivec);
1577
1578     int r = _hexaDocument->performSymmetryPlane( hElts, hVex, hVec );
1579     if ( r == HOK ){
1580         updateData();
1581         return true;
1582     }
1583
1584     return false;
1585 }
1586
1587 QModelIndex DocumentModel::replace( const QModelIndexList& iquadsPattern,
1588         const QModelIndex& ip1, const QModelIndex& ic1,
1589         const QModelIndex& ip2, const QModelIndex& ic2,
1590         const QModelIndex& ip3, const QModelIndex& ic3 )
1591 {
1592     QModelIndex ielts;
1593
1594     HEXA_NS::Vertex* hp1 = getHexaPtr<HEXA_NS::Vertex*>(ip1);
1595     HEXA_NS::Vertex* hc1 = getHexaPtr<HEXA_NS::Vertex*>(ic1);
1596     HEXA_NS::Vertex* hp2 = getHexaPtr<HEXA_NS::Vertex*>(ip2);
1597     HEXA_NS::Vertex* hc2 = getHexaPtr<HEXA_NS::Vertex*>(ic2);
1598     HEXA_NS::Vertex* hp3 = getHexaPtr<HEXA_NS::Vertex*>(ip3);
1599     HEXA_NS::Vertex* hc3 = getHexaPtr<HEXA_NS::Vertex*>(ic3);
1600
1601     HEXA_NS::Quads   hquads;
1602     HEXA_NS::Quad*   hquad = NULL;
1603     foreach( const QModelIndex& iquad, iquadsPattern ){
1604         hquad = getHexaPtr<HEXA_NS::Quad*>(iquad);
1605         hquads.push_back( hquad );
1606     }
1607
1608     HEXA_NS::Elements* helts = _hexaDocument->replace( hquads,
1609             hp1, hc1, hp2, hc2, hp3, hc3 );
1610     if ( BadElement(helts) ) return ielts;
1611
1612     updateData();
1613     ElementsItem* eltsItem = new ElementsItem(helts);
1614     _elementsDirItem->appendRow(eltsItem);
1615     ielts = eltsItem->index();
1616
1617     return ielts;
1618 }
1619
1620 QModelIndex DocumentModel::getGeomModelIndex(QString& id) const
1621 {
1622     QModelIndex result;
1623     PatternGeomSelectionModel* pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
1624
1625     // ** get the model index of the geom element having this ID
1626     HEXA_NS::SubShape* eltPtr = getGeomPtr(id);
1627     HEXA_NS::VertexShape* geomVertex = dynamic_cast<HEXA_NS::VertexShape*>(eltPtr);
1628     HEXA_NS::EdgeShape* geomEdge = dynamic_cast<HEXA_NS::EdgeShape*>(eltPtr);
1629     HEXA_NS::FaceShape* geomFace = dynamic_cast<HEXA_NS::FaceShape*>(eltPtr);
1630     if (geomVertex != NULL)
1631         result = pgsm->indexBy( HEXA_DATA_ROLE, QVariant::fromValue(geomVertex));
1632     if (geomEdge != NULL)
1633         result = pgsm->indexBy( HEXA_DATA_ROLE, QVariant::fromValue(geomEdge));
1634     if (geomFace != NULL)
1635         result = pgsm->indexBy( HEXA_DATA_ROLE, QVariant::fromValue(geomFace));
1636
1637     return result;
1638 }
1639
1640 QMultiMap< QString, int >     DocumentModel::getAssocShapesIds(const QModelIndex& dataIndex)
1641 {
1642    QMultiMap< QString, int > result;
1643    HEXA_NS::NewShape* mainShape;
1644    HEXA_NS::VertexShape* node;
1645    HEXA_NS::EdgeShape* line;
1646    HEXA_NS::FaceShape* face;
1647    QString shapeName;
1648    int subid;
1649
1650    PatternDataSelectionModel* pdsm = HEXABLOCKGUI::currentDocGView->getPatternDataSelectionModel();
1651    QModelIndexList assocsInd = pdsm->getGeomAssociations(dataIndex);
1652    foreach( const QModelIndex& anAssoc, assocsInd )
1653    {
1654       node = getHexaPtr<HEXA_NS::VertexShape*>(anAssoc);
1655       line = getHexaPtr<HEXA_NS::EdgeShape*>(anAssoc);
1656       face = getHexaPtr<HEXA_NS::FaceShape*>(anAssoc);
1657
1658       if (node != NULL)
1659          {
1660             mainShape = node->getParentShape();
1661             if (mainShape != NULL)
1662                {
1663                   shapeName = mainShape->getName();
1664                   subid = node->getIdent();
1665                }
1666          }
1667       else if (line != NULL)
1668          {
1669             mainShape = line->getParentShape();
1670             if (mainShape != NULL)
1671                {
1672                   shapeName = mainShape->getName();
1673                   subid = line->getIdent();
1674                }
1675          }
1676       else if (face != NULL)
1677          {
1678             mainShape = face->getParentShape();
1679             if (mainShape != NULL)
1680                {
1681                   shapeName = mainShape->getName();
1682                   subid = face->getIdent();
1683                }
1684          }
1685
1686       if (!shapeName.isEmpty())
1687          result.insert( shapeName, subid );
1688
1689       shapeName = QString();
1690    }
1691    return result;
1692 }
1693
1694 bool DocumentModel::setVertexAssociation( const QModelIndex& iVertex, double x, double y, double z)
1695 {
1696     HEXA_NS::Vertex* vertex = getHexaPtr<HEXA_NS::Vertex *>(iVertex);
1697
1698     if (vertex == NULL || _hexaDocument == NULL || vertex->setAssociation(x, y, z) != HOK)
1699         return false;
1700
1701     HEXA_NS::VertexShape* assoc = vertex->getAssociation();
1702     if (assoc != NULL)
1703     {
1704         HEXA_NS::NewShape* mainSh = assoc->getParentShape();
1705         setData( iVertex, QVariant::fromValue(QString(mainSh->getName())+","+QString::number(assoc->getIdent())+";"), HEXA_ASSOC_ENTRY_ROLE );
1706     }
1707
1708     return true;
1709 }
1710
1711 bool DocumentModel::setVertexAssociation( const QModelIndex& iVertex, const QModelIndex& iGeomVertex)
1712 {
1713     //parameters control
1714     QString newAssoc;
1715     HEXA_NS::Vertex* vertex = getHexaPtr<HEXA_NS::Vertex *>(iVertex);
1716     HEXA_NS::VertexShape* geomVertex = getHexaPtr<HEXA_NS::VertexShape*>(iGeomVertex);
1717     if (_hexaDocument == NULL || vertex == NULL || geomVertex == NULL) return false;
1718     HEXA_NS::NewShape* mainShape = geomVertex->getParentShape();
1719     if (mainShape == NULL) return false;
1720
1721     //add association
1722     if (vertex->setAssociation(geomVertex) != HOK)
1723         return false;
1724
1725     //update association state in the model
1726     newAssoc =  QString(mainShape->getName()) + "," + QString::number(geomVertex->getIdent()) + ";";
1727     setData( iVertex, QVariant::fromValue(newAssoc), HEXA_ASSOC_ENTRY_ROLE );
1728
1729     return true;
1730 }
1731
1732 bool DocumentModel::addEdgeAssociation( const QModelIndex& iEdge, const QModelIndex& iGeomEdge, double start, double end)
1733 {
1734     //parameters control
1735     QString currentAssoc, newAssoc;
1736     HEXA_NS::Edge* edge = getHexaPtr<HEXA_NS::Edge*>(iEdge);
1737     HEXA_NS::EdgeShape* geomEdge = getHexaPtr<HEXA_NS::EdgeShape*>(iGeomEdge);
1738     if (_hexaDocument == NULL || edge == NULL ||
1739             geomEdge == NULL || start > end) return false;
1740     HEXA_NS::NewShape* mainShape = geomEdge->getParentShape();
1741     if (mainShape == NULL) return false;
1742
1743     //add association in the engine side
1744     if (edge->addAssociation(geomEdge, start, end) != HOK)
1745         return false;
1746
1747     //add/update association in the model side (UI)
1748     currentAssoc = data(iEdge, HEXA_ASSOC_ENTRY_ROLE).toString();
1749     newAssoc = QString(mainShape->getName()) + "," + QString::number(geomEdge->getIdent()) + ";";
1750     if (!currentAssoc.isEmpty()) newAssoc = currentAssoc + newAssoc;
1751
1752     setData( iEdge, QVariant::fromValue(newAssoc), HEXA_ASSOC_ENTRY_ROLE );
1753
1754     return true;
1755 }
1756
1757
1758 bool DocumentModel::addQuadAssociation (const QModelIndex& iQuad, const QModelIndex& iGeomFace)
1759 {
1760     //parameters control
1761     QString currentAssoc, newAssoc;
1762     HEXA_NS::Quad* quad = getHexaPtr<HEXA_NS::Quad*>(iQuad);
1763     HEXA_NS::FaceShape* geomFace = getHexaPtr<HEXA_NS::FaceShape*>(iGeomFace);
1764     if (_hexaDocument == NULL || quad == NULL || geomFace == NULL)
1765         return false;
1766     HEXA_NS::NewShape* mainShape = geomFace->getParentShape();
1767     if (mainShape == NULL) return false;
1768
1769     //add association
1770     if (quad->addAssociation(geomFace) != HOK)
1771         return false;
1772
1773     //update association
1774     currentAssoc = data( iQuad, HEXA_ASSOC_ENTRY_ROLE).toString();
1775     newAssoc = QString(mainShape->getName()) + "," + QString::number(geomFace->getIdent()) + ";";
1776     if (!currentAssoc.isEmpty()) newAssoc = currentAssoc + newAssoc;
1777     setData( iQuad, QVariant::fromValue(newAssoc), HEXA_ASSOC_ENTRY_ROLE );
1778
1779     return true;
1780 }
1781
1782
1783 QModelIndex DocumentModel::getVertexAssociation(const QModelIndex& iVertex)
1784 {
1785     HEXA_NS::Vertex* vertex = getHexaPtr<HEXA_NS::Vertex*>(iVertex);
1786     if (vertex == NULL)
1787         return QModelIndex();
1788
1789     //get the associated geom vertex
1790     HEXA_NS::VertexShape* geomVertex = vertex->getAssociation();
1791     if (geomVertex == NULL)
1792         return QModelIndex();
1793
1794     //convert geom vertex to qmodelindex
1795     PatternGeomSelectionModel* pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
1796     if (pgsm == NULL)
1797         return QModelIndex();
1798
1799     return pgsm->indexBy(HEXA_DATA_ROLE, QVariant::fromValue(geomVertex));
1800 }
1801
1802
1803 QModelIndexList DocumentModel::getEdgeAssociations(const QModelIndex& iEdge)
1804 {
1805     QModelIndexList result;
1806     HEXA_NS::Edge* edge = getHexaPtr<HEXA_NS::Edge*>(iEdge);
1807     if (edge == NULL) return result;
1808
1809     HEXA_NS::EdgeShape* geomEdge;
1810     HEXA_NS::AssoEdge* anEdgeAssoc;
1811
1812     PatternGeomSelectionModel* pGSModel = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
1813     int nbAssocs = edge->countAssociation();
1814     for (int i = 0; i < nbAssocs; ++i)
1815     {
1816         anEdgeAssoc = edge->getAssociation(i);
1817         if (anEdgeAssoc == NULL) continue;
1818         geomEdge = anEdgeAssoc->getEdgeShape();
1819         if (geomEdge == NULL) continue;
1820         result << pGSModel->indexBy( HEXA_DATA_ROLE, QVariant::fromValue(geomEdge));
1821     }
1822
1823     return result;
1824 }
1825
1826
1827 QModelIndexList DocumentModel::getQuadAssociations(const QModelIndex& iQuad)
1828 {
1829     QModelIndexList result;
1830     QModelIndex geomQuadIndex;
1831     HEXA_NS::Quad* quad = getHexaPtr<HEXA_NS::Quad*>(iQuad);
1832     if (quad == NULL) return result;
1833
1834     if (HEXABLOCKGUI::currentDocGView == NULL) return result;
1835     PatternGeomSelectionModel* pgsm = HEXABLOCKGUI::currentDocGView->getPatternGeomSelectionModel();
1836     PatternGeomModel* pgm = HEXABLOCKGUI::currentDocGView->getPatternGeomModel();
1837     if (pgsm == NULL || pgm == NULL) return result;
1838
1839     HEXA_NS::FaceShape* geomFace;
1840
1841     int nbAssocs = quad->countAssociation();
1842     for (int i = 0; i < nbAssocs; ++i)
1843     {
1844         geomFace = quad->getAssociation(i);
1845         if (geomFace == NULL) continue;
1846
1847         geomQuadIndex = pgsm->indexBy( HEXA_DATA_ROLE, QVariant::fromValue(geomFace) );
1848         result << geomQuadIndex;
1849     }
1850
1851     return result;
1852 }
1853
1854
1855 bool DocumentModel::associateOpenedLine( const QModelIndexList& iedges,
1856         HEXA_NS::NewShapes shapes,
1857         HEXA_NS::IntVector subIds,
1858         double pstart,
1859         double pend )
1860 {
1861
1862     HEXA_NS::Edges  mline;
1863
1864     HEXA_NS::Edge* hedge = NULL;
1865     foreach( const QModelIndex& iedge, iedges ){
1866         hedge = getHexaPtr<HEXA_NS::Edge*>(iedge);
1867         if (hedge != NULL)
1868             mline.push_back( hedge );
1869     }
1870
1871     int err = _hexaDocument->associateOpenedLine( mline, shapes, subIds, pstart, pend );
1872     if ( err == HOK ){
1873         updateData();
1874         return true;
1875     }
1876     return false;
1877 }
1878
1879 bool DocumentModel::associateClosedLine( const  QModelIndex& ivertex,
1880         const  QModelIndexList& iedges,
1881         HEXA_NS::NewShapes shapes,
1882         HEXA_NS::IntVector subIds,
1883         double pstart,
1884         bool   inv )
1885 {
1886     HEXA_NS::Vertex* mfirst = getHexaPtr<HEXA_NS::Vertex*>(ivertex);
1887     HEXA_NS::Edges   mline;
1888
1889     HEXA_NS::Edge* hedge = NULL;
1890     foreach( const QModelIndex& iedge, iedges ){
1891         hedge = getHexaPtr<HEXA_NS::Edge*>(iedge);
1892         if (hedge != NULL)
1893             mline.push_back( hedge );
1894     }
1895
1896     int err = _hexaDocument->associateClosedLine( mfirst, mline, shapes, subIds, pstart, inv);
1897     if ( err == HOK ){
1898         updateData();
1899         return true;
1900     }
1901     return false;
1902 }
1903
1904 // ************  GROUPS  ************
1905 //
1906 QModelIndex DocumentModel::addGroup( const QString& name, Group kind )
1907 {
1908     QModelIndex iGroup;
1909
1910     HEXA_NS::Group* hGroup = _hexaDocument->addGroup( name.toLocal8Bit().constData(), kind );
1911     if ( hGroup == NULL ) return iGroup;
1912
1913     GroupItem* groupItem = new GroupItem(hGroup);
1914     groupItem->setData( _entry, HEXA_DOC_ENTRY_ROLE );
1915     _groupDirItem->appendRow(groupItem);
1916     iGroup = groupItem->index();
1917
1918     return iGroup;
1919 }
1920
1921
1922 bool DocumentModel::removeGroup( const QModelIndex& igrp )
1923 {
1924     HEXA_NS::Group* hGroup = data(igrp, HEXA_DATA_ROLE).value<HEXA_NS::Group *>();
1925     int r = _hexaDocument->removeGroup ( hGroup );
1926
1927     if ( r == HOK ){
1928         removeRow( igrp.row(), igrp.parent());
1929         return true;
1930     }
1931
1932     return false;
1933 }
1934
1935
1936 QModelIndexList DocumentModel::getGroupElements( const QModelIndex& iGroup, DocumentModel::Group& kind ) const
1937 {
1938     QModelIndexList iElements;
1939
1940     HEXA_NS::Group* g = iGroup.data(HEXA_DATA_ROLE).value<HEXA_NS::Group *>();
1941     if ( g == NULL ) return iElements;
1942
1943     QModelIndexList iFound;
1944     QVariant q;
1945     HEXA_NS::EltBase* eltBase = NULL;
1946     int nbElement = g->countElement();
1947     for ( int nr = 0; nr < nbElement; ++nr ){
1948         eltBase = g->getElement( nr );
1949         kind = g->getKind();
1950         switch ( kind ){
1951         case HEXA_NS::HexaCell: case HEXA_NS::HexaNode: q = QVariant::fromValue( (HEXA_NS::Hexa *)eltBase ); break;
1952         case HEXA_NS::QuadCell: case HEXA_NS::QuadNode: q = QVariant::fromValue( (HEXA_NS::Quad *)eltBase ); break;
1953         case HEXA_NS::EdgeCell: case HEXA_NS::EdgeNode: q = QVariant::fromValue( (HEXA_NS::Edge *)eltBase ); break;
1954         case HEXA_NS::VertexNode: q = QVariant::fromValue( (HEXA_NS::Vertex *)eltBase ); break;
1955         }
1956         iFound = match( index(0, 0),
1957                 HEXA_DATA_ROLE,
1958                 q,
1959                 1,
1960                 Qt::MatchRecursive );
1961         if ( !iFound.isEmpty() )
1962             iElements << iFound[0];
1963     }
1964     return iElements;
1965 }
1966
1967 // 7.4 Boite: éditer un groupe
1968 void DocumentModel::setGroupName( const QModelIndex& igrp, const QString& name )
1969 {
1970     HEXA_NS::Group* hGroup = data(igrp, HEXA_DATA_ROLE).value<HEXA_NS::Group *>();
1971
1972     if ( hGroup ){
1973         hGroup->setName( name.toLocal8Bit().constData() );
1974         setData(igrp, QVariant::fromValue( name ) );
1975     }
1976 }
1977
1978 bool DocumentModel::addGroupElement( const QModelIndex& igrp, const QModelIndex& ielt )
1979 {
1980     HEXA_NS::Group*   hGroup = data(igrp, HEXA_DATA_ROLE).value<HEXA_NS::Group *>();
1981     if (hGroup == NULL) return false;
1982
1983     HEXA_NS::EltBase* hElt   = NULL;
1984     switch ( hGroup->getKind() ){
1985     case HEXA_NS::HexaCell: case HEXA_NS::HexaNode: hElt = getHexaPtr<HEXA_NS::Hexa*>(ielt); break;
1986     case HEXA_NS::QuadCell: case HEXA_NS::QuadNode: hElt = getHexaPtr<HEXA_NS::Quad*>(ielt); break;
1987     case HEXA_NS::EdgeCell: case HEXA_NS::EdgeNode: hElt = getHexaPtr<HEXA_NS::Edge*>(ielt); break;
1988     case HEXA_NS::VertexNode: hElt = getHexaPtr<HEXA_NS::Vertex*>(ielt); break;
1989     }
1990
1991     int res = HERR;
1992     if ( hElt != NULL )
1993         res = hGroup->addElement( hElt );
1994
1995     if ( res == HOK ) return true;
1996
1997     return false;
1998 }
1999
2000 bool DocumentModel::removeGroupElement( const QModelIndex& igrp, int nro )
2001 { //CS_TODO : remove child?
2002     HEXA_NS::Group* hGroup = data(igrp, HEXA_DATA_ROLE).value<HEXA_NS::Group *>();
2003     if (hGroup == NULL) return false;
2004
2005     if (hGroup->removeElement( nro ) == HOK) return true;
2006
2007     return false;
2008 }
2009
2010 bool DocumentModel::clearGroupElement( const QModelIndex& igrp )
2011 {
2012     HEXA_NS::Group* hGroup = data(igrp, HEXA_DATA_ROLE).value<HEXA_NS::Group *>();
2013
2014     if ( hGroup != NULL)
2015     {
2016         hGroup->clearElement();
2017         return true;
2018     }
2019     return false;
2020 }
2021
2022 // ************  LAWS  ************
2023
2024 //
2025 QModelIndex DocumentModel::addLaw( const QString& name, int nbnodes )
2026 {
2027     QModelIndex iLaw;
2028
2029     HEXA_NS::Law* hLaw = _hexaDocument->addLaw( name.toLocal8Bit().constData(), nbnodes );
2030     if ( BadElement(hLaw) ) return iLaw;
2031
2032     LawItem* lawItem = new LawItem(hLaw);
2033     _lawDirItem->appendRow(lawItem);
2034     iLaw = lawItem->index();
2035
2036     return iLaw;
2037 }
2038
2039 bool DocumentModel::setLaw( const QModelIndex& ilaw, int nbnodes, double coeff, KindLaw type )
2040 {
2041     bool ret = false;
2042
2043     HEXA_NS::Law* hLaw = data(ilaw, HEXA_DATA_ROLE).value<HEXA_NS::Law *>();
2044
2045     if ( hLaw ){
2046         int ok;
2047         ok = hLaw->setNodes( nbnodes );
2048         ( ok == HOK) ? ret = true : ret = false;
2049         ok = hLaw->setCoefficient( coeff );
2050         hLaw->setKind(type);
2051     }
2052
2053     return ret;
2054 }
2055
2056 //
2057 bool  DocumentModel::removeLaw( const QModelIndex& ilaw )
2058 {
2059     HEXA_NS::Law* hLaw = data(ilaw, HEXA_DATA_ROLE).value<HEXA_NS::Law *>();
2060     int r = _hexaDocument->removeLaw( hLaw );
2061
2062     if ( r == HOK ){
2063         removeRow( ilaw.row(),  ilaw.parent());
2064         return true;
2065     }
2066
2067     return false;
2068 }
2069
2070 bool DocumentModel::setPropagation( const QModelIndex& iPropagation, const QModelIndex& iLaw, bool way )
2071 {
2072     HEXA_NS::Propagation* hPropagation = data(iPropagation, HEXA_DATA_ROLE).value<HEXA_NS::Propagation *>();
2073     HEXA_NS::Law* hLaw = data(iLaw, HEXA_DATA_ROLE).value<HEXA_NS::Law *>();
2074
2075     int r = hPropagation->setLaw( hLaw );
2076     hPropagation->setWay( way );
2077
2078     if ( r == HOK ) return true;
2079
2080     return false;
2081 }
2082
2083
2084 QModelIndexList DocumentModel::getPropagation( const QModelIndex& iPropagation ) const
2085 {
2086     QModelIndexList iEdges;
2087
2088     QModelIndexList iFound;
2089     HEXA_NS::Propagation* propa = iPropagation.data(HEXA_DATA_ROLE).value<HEXA_NS::Propagation *>();
2090     if ( propa == NULL ) return iEdges;
2091
2092     const HEXA_NS::Edges& edges = propa->getEdges();
2093     for ( HEXA_NS::Edges::const_iterator anEdge = edges.begin(), endIt = edges.end();
2094             anEdge != endIt;
2095             ++anEdge ){
2096         iFound = match( index(0, 0),
2097                 HEXA_DATA_ROLE,
2098                 QVariant::fromValue( *anEdge ),
2099                 1,
2100                 Qt::MatchRecursive);
2101         if ( !iFound.isEmpty() )
2102             iEdges << iFound[0];
2103     }
2104
2105     return iEdges;
2106 }
2107
2108 HEXA_NS::Document* DocumentModel::documentImpl()
2109 {
2110     return _hexaDocument;
2111 }
2112
2113 QString DocumentModel::documentEntry()
2114 {
2115     return _entry;
2116 }
2117
2118 // 8.3 Boite: éditer une loi  CS_TODO
2119 // (idem création)
2120
2121 /*****************************************************************
2122                       PatternDataModel
2123  *****************************************************************/
2124
2125 PatternDataModel::PatternDataModel( QObject * parent ) :
2126                               QSortFilterProxyModel( parent )
2127 {
2128     QString dataRegExp = QString("(%1|%2|%3|%4|%5|%6|%7|%8)").
2129             arg(VERTEX_TREE).arg(EDGE_TREE).arg(QUAD_TREE).arg(HEXA_TREE).
2130             arg(VERTEX_DIR_TREE).arg(EDGE_DIR_TREE).arg(QUAD_DIR_TREE).arg(HEXA_DIR_TREE);
2131
2132     setFilterRole(HEXA_TREE_ROLE);
2133     setFilterRegExp ( QRegExp(dataRegExp) );
2134 }
2135
2136 // *** Pour suppression des panneaux "Builder" et "Geometry" ****/
2137
2138 //PatternDataModel::PatternDataModel( QObject * parent ) :
2139 //                              QSortFilterProxyModel( parent )
2140 //{
2141 //    QString dataRegExp = QString("(%1|%2|%3|%4|%5|%6|%7|%8|%9|%10|%11|%12|%13|%14|%15|%16|%17|%18|%19|%20|%21|%22|%23|%24|%25|%26)").
2142 //            arg(VERTEX_TREE).arg(EDGE_TREE).arg(QUAD_TREE).arg(HEXA_TREE).
2143 //            arg(VERTEX_DIR_TREE).arg(EDGE_DIR_TREE).arg(QUAD_DIR_TREE).
2144 //            arg(HEXA_DIR_TREE).arg(VECTOR_TREE).arg(ELEMENTS_TREE).
2145 //            arg(VECTOR_DIR_TREE).arg(ELEMENTS_DIR_TREE).
2146 //            arg(IMPLICIT_SHAPES_TREE).arg(EXPLICIT_SHAPES_TREE).arg(CLOUD_OF_POINTS_TREE).
2147 //            arg(GEOMSHAPE_TREE).arg(GEOMPOINT_TREE).arg(GEOMEDGE_TREE).arg(GEOMFACE_TREE).
2148 //            arg(IMPLICIT_SHAPES_DIR_TREE).arg(EXPLICIT_SHAPES_DIR_TREE).arg(CLOUD_OF_POINTS_DIR_TREE).
2149 //            arg(GEOMSHAPE_DIR_TREE).arg(GEOMPOINT_DIR_TREE).arg(GEOMEDGE_DIR_TREE).arg(GEOMFACE_DIR_TREE);
2150 //
2151 //    setFilterRole(HEXA_TREE_ROLE);
2152 //    setFilterRegExp ( QRegExp(dataRegExp) );
2153 //}
2154
2155 // ****************
2156
2157 PatternDataModel::~PatternDataModel()
2158 {
2159 }
2160
2161 HEXA_NS::Document* PatternDataModel::documentImpl()
2162 {
2163     HEXA_NS::Document* doc = NULL;
2164     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2165     if (m) doc = m->documentImpl();
2166     return doc;
2167 }
2168
2169 QString PatternDataModel::documentEntry()
2170 {
2171     QString entry;
2172     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2173     if (m) entry = m->documentEntry();
2174     return entry;
2175 }
2176
2177 Qt::ItemFlags PatternDataModel::flags(const QModelIndex &index) const
2178 {
2179     Qt::ItemFlags flags;
2180
2181     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2182     if ( m != NULL ){
2183         flags = m->flags( mapToSource(index) );
2184     }
2185     return flags;
2186 }
2187
2188
2189 QVariant PatternDataModel::headerData ( int section, Qt::Orientation orientation, int role ) const
2190 {
2191     if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ){
2192         return QVariant( "Data" );
2193     } else {
2194         return QSortFilterProxyModel::headerData ( section, orientation, role );
2195     }
2196 }
2197
2198
2199 QStandardItem* PatternDataModel::itemFromIndex ( const QModelIndex & index ) const
2200 {
2201     QStandardItem *item = NULL;
2202     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2203     if ( m != NULL ){
2204         item = m->itemFromIndex( mapToSource(index) );
2205     }
2206     return item;
2207 }
2208
2209 PatternBuilderModel::PatternBuilderModel( QObject * parent ) :
2210                               QSortFilterProxyModel( parent )
2211 {
2212     QString builderRegExp =QString("(%1|%2|%3|%4|%5|%6)").
2213             arg(VECTOR_TREE).arg(ELEMENTS_TREE).
2214             arg(CROSSELEMENTS_TREE).arg(VECTOR_DIR_TREE).arg(ELEMENTS_DIR_TREE).arg(CROSSELEMENTS_DIR_TREE);
2215
2216     setFilterRole( HEXA_TREE_ROLE );
2217     setFilterRegExp ( QRegExp( builderRegExp ) );
2218 }
2219
2220 PatternBuilderModel::~PatternBuilderModel()
2221 {
2222 }
2223
2224 Qt::ItemFlags PatternBuilderModel::flags(const QModelIndex &index) const
2225 {
2226     Qt::ItemFlags flags;
2227
2228     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2229     if ( m != NULL ){
2230         flags = m->flags( mapToSource(index) );
2231     }
2232     return flags;
2233 }
2234
2235 QVariant PatternBuilderModel::headerData ( int section, Qt::Orientation orientation, int role ) const
2236 {
2237     if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ){
2238         return QVariant( "Builder" );
2239     } else {
2240         return QSortFilterProxyModel::headerData ( section, orientation, role );
2241     }
2242 }
2243
2244 QStandardItem* PatternBuilderModel::itemFromIndex ( const QModelIndex & index ) const
2245 {
2246     QStandardItem *item = NULL;
2247     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2248     if ( m != NULL ){
2249         item = m->itemFromIndex( mapToSource(index) );
2250     }
2251     return item;
2252 }
2253
2254 //====================================================================
2255 //              PatternGeomModel
2256 //====================================================================
2257 PatternGeomModel::PatternGeomModel( QObject * parent ) :
2258                               QSortFilterProxyModel( parent )
2259 {
2260     QString builderRegExp =QString("(%1|%2|%3|%4|%5|%6|%7|%8|%9|%10|%11|%12|%13|%14)").
2261             arg(EXPLICIT_SHAPES_TREE).arg(IMPLICIT_SHAPES_TREE).arg(CLOUD_OF_POINTS_TREE).
2262             arg(GEOMSHAPE_TREE).arg(GEOMPOINT_TREE).arg(GEOMEDGE_TREE).arg(GEOMFACE_TREE).
2263             arg(EXPLICIT_SHAPES_DIR_TREE).arg(IMPLICIT_SHAPES_DIR_TREE).arg(CLOUD_OF_POINTS_DIR_TREE).
2264             arg(GEOMSHAPE_DIR_TREE).arg(GEOMPOINT_DIR_TREE).arg(GEOMEDGE_DIR_TREE).arg(GEOMFACE_DIR_TREE);
2265
2266     setFilterRole( HEXA_TREE_ROLE );
2267     setFilterRegExp ( QRegExp(builderRegExp ) );
2268 }
2269
2270 PatternGeomModel::~PatternGeomModel()
2271 {
2272 }
2273
2274 Qt::ItemFlags PatternGeomModel::flags(const QModelIndex &index) const
2275 {
2276     Qt::ItemFlags flags;
2277
2278     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2279     if ( m != NULL ){
2280         flags = m->flags( mapToSource(index) );
2281     }
2282     return flags;
2283 }
2284
2285 QVariant PatternGeomModel::headerData ( int section, Qt::Orientation orientation, int role ) const
2286 {
2287     if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ){
2288         return QVariant( "Geometry" );
2289     } else {
2290         return QSortFilterProxyModel::headerData ( section, orientation, role );
2291     }
2292 }
2293
2294 QStandardItem* PatternGeomModel::itemFromIndex ( const QModelIndex & index ) const
2295 {
2296     QStandardItem *item = NULL;
2297     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2298     if ( m != NULL ){
2299         item = m->itemFromIndex( mapToSource(index) );
2300     }
2301     return item;
2302 }
2303 //==============================================================
2304
2305
2306 AssociationsModel::AssociationsModel( QObject * parent ) :
2307                               QSortFilterProxyModel( parent )
2308 {
2309     QString assocRegExp;
2310
2311     setFilterRole( HEXA_TREE_ROLE );
2312     setFilterRegExp ( QRegExp(assocRegExp) );
2313 }
2314
2315 AssociationsModel::~AssociationsModel()
2316 {
2317 }
2318
2319 Qt::ItemFlags AssociationsModel::flags(const QModelIndex &index) const
2320 {
2321     Qt::ItemFlags flags;
2322
2323     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2324     if ( m != NULL ){
2325         flags = m->flags( mapToSource(index) );
2326     }
2327     return flags;
2328 }
2329
2330 QVariant AssociationsModel::headerData ( int section, Qt::Orientation orientation, int role ) const
2331 {
2332     if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ){
2333         return QVariant( "Associations" );
2334     } else {
2335         return QSortFilterProxyModel::headerData ( section, orientation, role );
2336     }
2337 }
2338
2339 QStandardItem* AssociationsModel::itemFromIndex ( const QModelIndex & index ) const
2340 {
2341     QStandardItem *item = NULL;
2342     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2343     if ( m != NULL ){
2344         item = m->itemFromIndex( mapToSource(index) );
2345     }
2346     return item;
2347 }
2348
2349 GroupsModel::GroupsModel( QObject * parent ) :
2350                               QSortFilterProxyModel( parent )
2351 {
2352     QString groupsRegExp =QString("(%1|%2)").arg(GROUP_TREE).arg(GROUP_DIR_TREE);
2353
2354     setFilterRole( HEXA_TREE_ROLE );
2355     setFilterRegExp ( QRegExp(groupsRegExp ) );
2356 }
2357
2358 GroupsModel::~GroupsModel()
2359 {
2360 }
2361
2362 Qt::ItemFlags GroupsModel::flags(const QModelIndex &index) const
2363 {
2364     Qt::ItemFlags flags;
2365
2366     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2367     if ( m != NULL ){
2368         flags = m->flags( mapToSource(index) );
2369     }
2370     return flags;
2371 }
2372
2373 QVariant GroupsModel::headerData ( int section, Qt::Orientation orientation, int role ) const
2374 {
2375     if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ){
2376         return QVariant( "Groups" );
2377     } else {
2378         return QSortFilterProxyModel::headerData ( section, orientation, role );
2379     }
2380 }
2381
2382 QStandardItem* GroupsModel::itemFromIndex ( const QModelIndex & index ) const
2383 {
2384     QStandardItem *item = NULL;
2385     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2386     if ( m != NULL ){
2387         item = m->itemFromIndex( mapToSource(index) );
2388     }
2389     return item;
2390 }
2391
2392 QModelIndexList GroupsModel::getGroupElements( const QModelIndex& iGroup, DocumentModel::Group& kind ) const
2393 {
2394     QModelIndexList elements;
2395     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2396     if ( m != NULL ){
2397         elements = m->getGroupElements( mapToSource(iGroup), kind );
2398     }
2399     return elements;
2400 }
2401
2402 MeshModel::MeshModel( QObject * parent ) :
2403                               QSortFilterProxyModel( parent )
2404 {
2405     QString meshRegExp =QString("(%1|%2|%3|%4)").arg(LAW_TREE).arg(LAW_DIR_TREE)                            .arg(PROPAGATION_TREE).arg(PROPAGATION_DIR_TREE);
2406
2407     setFilterRole( HEXA_TREE_ROLE );
2408     setFilterRegExp ( QRegExp(meshRegExp) );
2409 }
2410
2411 MeshModel::~MeshModel()
2412 {
2413 }
2414
2415 Qt::ItemFlags MeshModel::flags(const QModelIndex &index) const
2416 {
2417     Qt::ItemFlags flags;
2418
2419     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2420     if ( m != NULL ){
2421         flags = m->flags( mapToSource(index) );
2422     }
2423     return flags;
2424 }
2425
2426 QVariant MeshModel::headerData ( int section, Qt::Orientation orientation, int role ) const
2427 {
2428     if ( section == 0 && orientation == Qt::Horizontal && role == Qt::DisplayRole ){
2429         return QVariant( "Mesh" );
2430     } else {
2431         return QSortFilterProxyModel::headerData ( section, orientation, role );
2432     }
2433 }
2434
2435 QStandardItem* MeshModel::itemFromIndex ( const QModelIndex & index ) const
2436 {
2437     QStandardItem *item = NULL;
2438     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2439     if ( m != NULL ){
2440         item = m->itemFromIndex( mapToSource(index) );
2441     }
2442     return item;
2443 }
2444
2445 QModelIndexList MeshModel::getPropagation( const QModelIndex& iPropagation ) const
2446 {
2447     QModelIndexList edges;
2448     DocumentModel *m = dynamic_cast<DocumentModel *>( sourceModel() );
2449     if ( m != NULL ){
2450         edges = m->getPropagation( mapToSource(iPropagation) );
2451     }
2452     return edges;
2453 }
2454
2455
2456 // ================================== NEW ======================================
2457
2458 // ===== CARTESIAN GRID
2459
2460 QModelIndex DocumentModel::makeCartesianTop(int nx, int ny, int nz)
2461 {
2462     QModelIndex result;
2463
2464     HEXA_NS::Elements* helts = _hexaDocument->makeCartesianTop( nx, ny, nz );
2465     if ( BadElement(helts) )
2466         return result;
2467
2468     result = addElementsToTree(helts, _elementsDirItem);
2469
2470     return result;
2471 }
2472
2473 QModelIndex DocumentModel::makeCartesianUni(const QModelIndex& icenter,
2474                             const QModelIndex& ibase, const QModelIndex& ivec, const QModelIndex& iaxis,
2475                             double lx, double ly, double lz, int nx, int ny, int nz)
2476 {
2477     QModelIndex result;
2478
2479     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2480     HEXA_NS::Vector* hbase   = getHexaPtr<HEXA_NS::Vector*>(ibase);
2481     HEXA_NS::Vector* hvec    = getHexaPtr<HEXA_NS::Vector*>(ivec);
2482     HEXA_NS::Vector* haxis   = getHexaPtr<HEXA_NS::Vector*>(iaxis);
2483
2484     HEXA_NS::Elements* helts = _hexaDocument->makeCartesianUni( hcenter, hbase, hvec, haxis,
2485                                                                 lx, ly, lz, nx, ny, nz);
2486     if ( BadElement(helts) )
2487         return result;
2488
2489     result = addElementsToTree(helts, _elementsDirItem);
2490
2491     return result;
2492 }
2493
2494 QModelIndex DocumentModel::makeCartesian(const QModelIndex& icenter,
2495                                          const QModelIndex& ibase, const QModelIndex& ivec, const QModelIndex& iaxis,
2496                                          vector<double>& radius, vector<double>& angles, vector<double>& heights)
2497 {
2498     QModelIndex result;
2499
2500     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2501     HEXA_NS::Vector* hbase   = getHexaPtr<HEXA_NS::Vector*>(ibase);
2502     HEXA_NS::Vector* hvec    = getHexaPtr<HEXA_NS::Vector*>(ivec);
2503     HEXA_NS::Vector* haxis   = getHexaPtr<HEXA_NS::Vector*>(iaxis);
2504
2505     HEXA_NS::Elements* helts = _hexaDocument->makeCartesian( hcenter, hbase, hvec, haxis,
2506                                                              radius, angles, heights);
2507     if ( BadElement(helts) )
2508         return result;
2509
2510     result = addElementsToTree(helts, _elementsDirItem);
2511
2512     return result;
2513 }
2514
2515
2516 // ===== SPHERE
2517
2518 QModelIndex DocumentModel::makeSphereTop (int nr, int na, int nh)
2519 {
2520     QModelIndex result;
2521
2522     HEXA_NS::Elements* helts = _hexaDocument->makeSphereTop( nr, na, nh );
2523     if ( BadElement(helts) )
2524         return result;
2525
2526     result = addElementsToTree(helts, _elementsDirItem);
2527
2528     return result;
2529 }
2530
2531 QModelIndex DocumentModel::makeSphereUni (QModelIndex& icenter,
2532                                           QModelIndex& ivec_x, QModelIndex& ivec_z,
2533                                           double rtrou, double rext, double ang,
2534                                           QModelIndex& ivplan,
2535                                           int nr, int na, int nh)
2536 {
2537     QModelIndex result;
2538
2539     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2540     HEXA_NS::Vector* hvec_x  = getHexaPtr<HEXA_NS::Vector*>(ivec_x);
2541     HEXA_NS::Vector* hvec_z  = getHexaPtr<HEXA_NS::Vector*>(ivec_z);
2542     HEXA_NS::Vertex* hvplan  = getHexaPtr<HEXA_NS::Vertex*>(ivplan);
2543
2544     HEXA_NS::Elements* helts = _hexaDocument->makeSphereUni( hcenter, hvec_x, hvec_z,
2545                                                              rtrou, rext, ang, hvplan,
2546                                                              nr, na, nh);
2547     result = addElementsToTree(helts, _elementsDirItem);
2548
2549     return result;
2550 }
2551
2552 QModelIndex DocumentModel::makeSphere    (QModelIndex& icenter,
2553                                           QModelIndex& ivec_x, QModelIndex& ivec_z,
2554                                           vector<double>& tray, vector<double>& tang, vector<double>& thaut)
2555 {
2556     QModelIndex result;
2557
2558     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2559     HEXA_NS::Vector* hvec_x  = getHexaPtr<HEXA_NS::Vector*>(ivec_x);
2560     HEXA_NS::Vector* hvec_z  = getHexaPtr<HEXA_NS::Vector*>(ivec_z);
2561
2562     HEXA_NS::Elements* helts = _hexaDocument->makeSphere( hcenter, hvec_x, hvec_z,
2563                                                           tray, tang, thaut);
2564
2565     result = addElementsToTree(helts, _elementsDirItem);
2566
2567     return result;
2568 }
2569
2570 // ====== SPHERICAL
2571
2572 QModelIndex DocumentModel::makeSphericalTop (int nbre, int crit)
2573 {
2574     QModelIndex result;
2575
2576     HEXA_NS::Elements* helts = _hexaDocument->makeSphericalTop(nbre, crit);
2577     if (BadElement(helts))
2578         return result;
2579
2580     result = addElementsToTree(helts, _elementsDirItem);
2581
2582     return result;
2583 }
2584
2585 QModelIndex DocumentModel::makeSphericalUni (QModelIndex& icenter,
2586                                              QModelIndex& ivec_x, QModelIndex& ivec_z,
2587                                              double rayon,
2588                                              int nbre, int crit)
2589 {
2590     QModelIndex result;
2591
2592     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2593     HEXA_NS::Vector* hvec_x  = getHexaPtr<HEXA_NS::Vector*>(ivec_x);
2594     HEXA_NS::Vector* hvec_z  = getHexaPtr<HEXA_NS::Vector*>(ivec_z);
2595
2596     HEXA_NS::Elements* helts = _hexaDocument->makeSphericalUni(hcenter, hvec_x, hvec_z, rayon, nbre, crit);
2597     if (BadElement(helts))
2598         return result;
2599
2600     result = addElementsToTree(helts, _elementsDirItem);
2601
2602     return result;
2603 }
2604
2605 QModelIndex DocumentModel::makeSpherical    (QModelIndex& icenter,
2606                                              QModelIndex& ivec_x, QModelIndex& ivec_z,
2607                                              vector<double>& rayon,
2608                                              int crit)
2609 {
2610     QModelIndex result;
2611
2612     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2613     HEXA_NS::Vector* hvec_x = getHexaPtr<HEXA_NS::Vector*>(ivec_x);
2614     HEXA_NS::Vector* hvec_z = getHexaPtr<HEXA_NS::Vector*>(ivec_z);
2615
2616     HEXA_NS::Elements* helts = _hexaDocument->makeSpherical(hcenter, hvec_x, hvec_z, rayon, crit);
2617     if (BadElement(helts))
2618         return result;
2619
2620     result = addElementsToTree(helts, _elementsDirItem);
2621
2622     return result;
2623 }
2624
2625 // ===== RIND
2626 QModelIndex DocumentModel::makeRindTop (int nr, int na, int nh)
2627 {
2628     QModelIndex result;
2629
2630     HEXA_NS::Elements* helts = _hexaDocument->makeRindTop(nr, na, nh);
2631     if (BadElement(helts))
2632         return result;
2633
2634     result = addElementsToTree(helts, _elementsDirItem);
2635
2636     return result;
2637 }
2638
2639 QModelIndex DocumentModel::makeRindUni (QModelIndex& icenter,
2640                                         QModelIndex& ivec_x, QModelIndex& ivec_z,
2641                                         double raytrou, double rint, double rext, double ang,
2642                                         QModelIndex& ivplan,
2643                                         int nr, int na, int nh)
2644 {
2645     QModelIndex result;
2646
2647     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2648     HEXA_NS::Vector* hvec_x = getHexaPtr<HEXA_NS::Vector*>(ivec_x);
2649     HEXA_NS::Vector* hvec_z = getHexaPtr<HEXA_NS::Vector*>(ivec_z);
2650     HEXA_NS::Vertex* hvplan = getHexaPtr<HEXA_NS::Vertex*>(ivplan);
2651
2652     HEXA_NS::Elements* helts = _hexaDocument->makeRindUni(hcenter, hvec_x, hvec_z, raytrou, rint, rext, ang,
2653                                                           hvplan, nr, na, nh);
2654     if (BadElement(helts))
2655         return result;
2656
2657     result = addElementsToTree(helts, _elementsDirItem);
2658
2659     return result;
2660 }
2661
2662 QModelIndex DocumentModel::makeRind    (QModelIndex& icenter,
2663                                         QModelIndex& ivec_x, QModelIndex& ivec_z,
2664                                         vector<double>& tray, vector<double>& tang, vector<double>& thaut)
2665 {
2666     QModelIndex result;
2667
2668     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2669     HEXA_NS::Vector* hvec_x = getHexaPtr<HEXA_NS::Vector*>(ivec_x);
2670     HEXA_NS::Vector* hvec_z = getHexaPtr<HEXA_NS::Vector*>(ivec_z);
2671
2672     HEXA_NS::Elements* helts = _hexaDocument->makeRind(hcenter, hvec_x, hvec_z, tray, tang, thaut);
2673     if (BadElement(helts))
2674         return result;
2675
2676     result = addElementsToTree(helts, _elementsDirItem);
2677
2678     return result;
2679 }
2680
2681 // ======== Cylinder
2682 QModelIndex DocumentModel::makeCylinderTop(int nr, int na, int nh)
2683 {
2684     QModelIndex result;
2685
2686     HEXA_NS::Elements* helts = _hexaDocument->makeCylinderTop(nr, na, nh);
2687     if (BadElement(helts))
2688         return result;
2689
2690     result = addElementsToTree(helts, _elementsDirItem);
2691
2692     return result;
2693 }
2694
2695 QModelIndex DocumentModel::makeCylinderUni(QModelIndex& iorig, QModelIndex& ivecx, QModelIndex& ivecz,
2696                                            double rint, double rext, double angle, double haut,
2697                                            int nr, int na, int nh)
2698 {
2699     QModelIndex result;
2700
2701     HEXA_NS::Vertex* horig = getHexaPtr<HEXA_NS::Vertex*>(iorig);
2702     HEXA_NS::Vector* hvecx = getHexaPtr<HEXA_NS::Vector*>(ivecx);
2703     HEXA_NS::Vector* hvecz = getHexaPtr<HEXA_NS::Vector*>(ivecz);
2704
2705     HEXA_NS::Elements* helts = _hexaDocument->makeCylinderUni(horig, hvecx, hvecz,
2706                                                               rint, rext, angle, haut,
2707                                                               nr, na, nh);
2708     if (BadElement(helts))
2709         return result;
2710
2711     result = addElementsToTree(helts, _elementsDirItem);
2712
2713     return result;
2714 }
2715
2716 QModelIndex DocumentModel::makeCylinder(QModelIndex& iorig, QModelIndex& ivecx, QModelIndex& ivecz,
2717                                         vector<double>& tray, vector<double>& tang, vector<double>& thaut)
2718 {
2719     QModelIndex result;
2720
2721     HEXA_NS::Vertex* horig = getHexaPtr<HEXA_NS::Vertex*>(iorig);
2722     HEXA_NS::Vector* hvecx = getHexaPtr<HEXA_NS::Vector*>(ivecx);
2723     HEXA_NS::Vector* hvecz = getHexaPtr<HEXA_NS::Vector*>(ivecz);
2724
2725     HEXA_NS::Elements* helts = _hexaDocument->makeCylinder(horig, hvecx, hvecz, tray, tang, thaut);
2726     if (BadElement(helts))
2727         return result;
2728
2729     result = addElementsToTree(helts, _elementsDirItem);
2730
2731     return result;
2732 }
2733
2734
2735 // ======== Cylinders
2736 QModelIndex DocumentModel::makeCylinders(QModelIndex& iorig1, QModelIndex& ivecz1,  double r1, double h1,
2737                                          QModelIndex& iorig2, QModelIndex& ivecz2, double r2, double h2)
2738 {
2739     QModelIndex result;
2740
2741     HEXA_NS::Vertex* horig1 = getHexaPtr<HEXA_NS::Vertex*>(iorig1);
2742     HEXA_NS::Vertex* horig2 = getHexaPtr<HEXA_NS::Vertex*>(iorig2);
2743     HEXA_NS::Vector* hvecz1 = getHexaPtr<HEXA_NS::Vector*>(ivecz1);
2744     HEXA_NS::Vector* hvecz2 = getHexaPtr<HEXA_NS::Vector*>(ivecz2);
2745
2746     HEXA_NS::BiCylinder* helts = _hexaDocument->makeCylinders(horig1, hvecz1, r1, h1,
2747                                                               horig2, hvecz2, r2, h2);
2748     if (BadElement(helts))
2749         return result;
2750
2751     result = addElementsToTree(helts, _crossElementsDirItem);
2752
2753     return result;
2754 }
2755
2756 // =========== PIPE
2757 QModelIndex DocumentModel::makePipeTop(int nr, int na, int nh)
2758 {
2759     QModelIndex result;
2760
2761     HEXA_NS::Elements* helts = _hexaDocument->makePipeTop(nr, na, nh);
2762     if (BadElement(helts))
2763         return result;
2764
2765     result = addElementsToTree(helts, _elementsDirItem);
2766
2767     return result;
2768 }
2769
2770 QModelIndex DocumentModel::makePipeUni(QModelIndex& iorig, QModelIndex& ivecx, QModelIndex& ivecz,
2771                                        double rint, double rext, double angle, double haut,
2772                                        int nr, int na, int nh)
2773 {
2774     QModelIndex result;
2775
2776     HEXA_NS::Vertex* horig = getHexaPtr<HEXA_NS::Vertex*>(iorig);
2777     HEXA_NS::Vector* hvecx = getHexaPtr<HEXA_NS::Vector*>(ivecx);
2778     HEXA_NS::Vector* hvecz = getHexaPtr<HEXA_NS::Vector*>(ivecz);
2779
2780     HEXA_NS::Elements* helts = _hexaDocument->makePipeUni(horig, hvecx, hvecz, rint, rext, angle,
2781                                                           haut, nr, na, nh);
2782     if (BadElement(helts))
2783         return result;
2784
2785     result = addElementsToTree(helts, _elementsDirItem);
2786
2787     return result;
2788 }
2789
2790 QModelIndex DocumentModel::makePipe(QModelIndex& iorig, QModelIndex& ivecx, QModelIndex& ivecz,
2791                                     vector<double>& tray, vector<double>& tang, vector<double>& thaut)
2792 {
2793     QModelIndex result;
2794
2795     HEXA_NS::Vertex* horig = getHexaPtr<HEXA_NS::Vertex*>(iorig);
2796     HEXA_NS::Vector* hvecx = getHexaPtr<HEXA_NS::Vector*>(ivecx);
2797     HEXA_NS::Vector* hvecz = getHexaPtr<HEXA_NS::Vector*>(ivecz);
2798
2799     HEXA_NS::Elements* helts = _hexaDocument->makePipe(horig, hvecx, hvecz, tray, tang, thaut);
2800     if (BadElement(helts))
2801         return result;
2802
2803     result = addElementsToTree(helts, _elementsDirItem);
2804
2805     return result;
2806 }
2807
2808 // ======== Pipes
2809 QModelIndex DocumentModel::makePipes(QModelIndex& iorig1, QModelIndex& ivecz1, double rint1, double rex1, double h1,
2810                                      QModelIndex& iorig2, QModelIndex& ivecz2, double rint2, double rex2, double h2)
2811 {
2812     QModelIndex result;
2813
2814     HEXA_NS::Vertex* horig1 = getHexaPtr<HEXA_NS::Vertex*>(iorig1);
2815     HEXA_NS::Vertex* horig2 = getHexaPtr<HEXA_NS::Vertex*>(iorig2);
2816     HEXA_NS::Vector* hvecz1 = getHexaPtr<HEXA_NS::Vector*>(ivecz1);
2817     HEXA_NS::Vector* hvecz2 = getHexaPtr<HEXA_NS::Vector*>(ivecz2);
2818
2819     HEXA_NS::BiCylinder* helts = _hexaDocument->makePipes(horig1, hvecz1, rint1, rex1, h1,
2820                                                         horig2, hvecz2, rint2, rex2, h2);
2821     if (BadElement(helts))
2822         return result;
2823
2824     result = addElementsToTree(helts, _crossElementsDirItem);
2825
2826     return result;
2827 }
2828
2829 // ======== Join Quads
2830 QModelIndex DocumentModel::joinQuadUni(QModelIndex&  istart, QModelIndex& idest, QModelIndex& iv1, QModelIndex& iv2,
2831                                        QModelIndex& iv3, QModelIndex& iv4, int nb)
2832 {
2833     QModelIndex result;
2834
2835     HEXA_NS::Quad* hstart = getHexaPtr<HEXA_NS::Quad*>(istart);
2836     HEXA_NS::Quad* hdest = getHexaPtr<HEXA_NS::Quad*>(idest);
2837     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(iv1);
2838     HEXA_NS::Vertex* hv2 = getHexaPtr<HEXA_NS::Vertex*>(iv2);
2839     HEXA_NS::Vertex* hv3 = getHexaPtr<HEXA_NS::Vertex*>(iv3);
2840     HEXA_NS::Vertex* hv4 = getHexaPtr<HEXA_NS::Vertex*>(iv4);
2841
2842     HEXA_NS::Elements* helts = _hexaDocument->joinQuadUni(hstart, hdest, hv1, hv2, hv3, hv4, nb);
2843     if (BadElement(helts))
2844         return result;
2845
2846     result = addElementsToTree(helts, _elementsDirItem);
2847
2848     return result;
2849 }
2850
2851 QModelIndex DocumentModel::joinQuad(QModelIndex&  istart, QModelIndex& idest, QModelIndex& iva1, QModelIndex& ivb1,
2852                      QModelIndex& iva2, QModelIndex& ivb2, vector<double>& tlen)
2853 {
2854     QModelIndex result;
2855
2856     HEXA_NS::Quad* hstart = getHexaPtr<HEXA_NS::Quad*>(istart);
2857     HEXA_NS::Quad* hdest = getHexaPtr<HEXA_NS::Quad*>(idest);
2858     HEXA_NS::Vertex* hva1 = getHexaPtr<HEXA_NS::Vertex*>(iva1);
2859     HEXA_NS::Vertex* hvb1 = getHexaPtr<HEXA_NS::Vertex*>(ivb1);
2860     HEXA_NS::Vertex* hva2 = getHexaPtr<HEXA_NS::Vertex*>(iva2);
2861     HEXA_NS::Vertex* hvb2 = getHexaPtr<HEXA_NS::Vertex*>(ivb2);
2862
2863     HEXA_NS::Elements* helts = _hexaDocument->joinQuad(hstart, hdest, hva1, hvb1, hva2, hvb2, tlen);
2864     if (BadElement(helts))
2865         return result;
2866
2867     result = addElementsToTree(helts, _elementsDirItem);
2868
2869     return result;
2870 }
2871
2872 QModelIndex DocumentModel::joinQuadsUni(QModelIndexList& istarts, QModelIndex& idest, QModelIndex& iv1, QModelIndex& iv2,
2873                                         QModelIndex& iv3, QModelIndex& iv4, int nb)
2874 {
2875     QModelIndex result;
2876
2877     HEXA_NS::Quads hstarts;
2878     int nbQuads = istarts.size();
2879     for (int i = 0; i < nbQuads; ++i)
2880         hstarts.push_back(getHexaPtr<HEXA_NS::Quad*>(istarts[i]));
2881
2882     HEXA_NS::Quad* hdest = getHexaPtr<HEXA_NS::Quad*>(idest);
2883     HEXA_NS::Vertex* hv1 = getHexaPtr<HEXA_NS::Vertex*>(iv1);
2884     HEXA_NS::Vertex* hv2 = getHexaPtr<HEXA_NS::Vertex*>(iv2);
2885     HEXA_NS::Vertex* hv3 = getHexaPtr<HEXA_NS::Vertex*>(iv3);
2886     HEXA_NS::Vertex* hv4 = getHexaPtr<HEXA_NS::Vertex*>(iv4);
2887
2888     HEXA_NS::Elements* helts = _hexaDocument->joinQuadsUni(hstarts, hdest, hv1, hv2, hv3, hv4, nb);
2889     if (BadElement(helts))
2890         return result;
2891
2892     result = addElementsToTree(helts, _elementsDirItem);
2893
2894     return result;
2895 }
2896
2897 QModelIndex DocumentModel::joinQuads(QModelIndexList& istarts, QModelIndex& idest, QModelIndex& iva1, QModelIndex& ivb1,
2898                                      QModelIndex& iva2, QModelIndex& ivb2, vector<double>& tlen)
2899 {
2900     QModelIndex result;
2901
2902     HEXA_NS::Quads hstarts;
2903     int nbQuads = istarts.size();
2904     for (int i = 0; i < nbQuads; ++i)
2905         hstarts.push_back(getHexaPtr<HEXA_NS::Quad*>(istarts[i]));
2906
2907     HEXA_NS::Quad* hdest = getHexaPtr<HEXA_NS::Quad*>(idest);
2908     HEXA_NS::Vertex* hva1 = getHexaPtr<HEXA_NS::Vertex*>(iva1);
2909     HEXA_NS::Vertex* hvb1 = getHexaPtr<HEXA_NS::Vertex*>(ivb1);
2910     HEXA_NS::Vertex* hva2 = getHexaPtr<HEXA_NS::Vertex*>(iva2);
2911     HEXA_NS::Vertex* hvb2 = getHexaPtr<HEXA_NS::Vertex*>(ivb2);
2912
2913     HEXA_NS::Elements* helts = _hexaDocument->joinQuads(hstarts, hdest, hva1, hvb1, hva2, hvb2, tlen);
2914     if (BadElement(helts))
2915         return result;
2916
2917     result = addElementsToTree(helts, _elementsDirItem);
2918
2919     return result;
2920 }
2921
2922 // ======== Quad Revolution
2923 QModelIndex DocumentModel::revolutionQuadUni(QModelIndex& istart, QModelIndex& icenter, QModelIndex& iaxis,
2924                                              double angle, int nbre)
2925 {
2926     QModelIndex result;
2927
2928     HEXA_NS::Quad* hstart = getHexaPtr<HEXA_NS::Quad*>(istart);
2929     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2930     HEXA_NS::Vector* haxis = getHexaPtr<HEXA_NS::Vector*>(iaxis);
2931
2932     HEXA_NS::Elements* helts = _hexaDocument->revolutionQuadUni(hstart, hcenter, haxis, angle, nbre);
2933     if (BadElement(helts))
2934         return result;
2935
2936     result = addElementsToTree(helts, _elementsDirItem);
2937
2938     return result;
2939 }
2940
2941 QModelIndex DocumentModel::revolutionQuad(QModelIndex& istart, QModelIndex& icenter, QModelIndex& iaxis,
2942                                           vector<double>& angles)
2943 {
2944     QModelIndex result;
2945
2946     HEXA_NS::Quad* hstart = getHexaPtr<HEXA_NS::Quad*>(istart);
2947     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2948     HEXA_NS::Vector* haxis = getHexaPtr<HEXA_NS::Vector*>(iaxis);
2949
2950     HEXA_NS::Elements* helts = _hexaDocument->revolutionQuad(hstart, hcenter, haxis, angles);
2951     if (BadElement(helts))
2952         return result;
2953
2954     result = addElementsToTree(helts, _elementsDirItem);
2955
2956     return result;
2957 }
2958
2959 QModelIndex DocumentModel::revolutionQuadsUni(QModelIndexList& istarts, QModelIndex& icenter, QModelIndex& iaxis,
2960                                               double angle, int nbre)
2961 {
2962     QModelIndex result;
2963
2964     HEXA_NS::Quads hstarts;
2965     int nbQuads = istarts.count();
2966     for (int i = 0; i < nbQuads; ++i)
2967         hstarts.push_back(getHexaPtr<HEXA_NS::Quad*>(istarts[i]));
2968
2969     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2970     HEXA_NS::Vector* haxis = getHexaPtr<HEXA_NS::Vector*>(iaxis);
2971
2972     HEXA_NS::Elements* helts = _hexaDocument->revolutionQuadsUni(hstarts, hcenter, haxis, angle, nbre);
2973     if (BadElement(helts))
2974         return result;
2975
2976     result = addElementsToTree(helts, _elementsDirItem);
2977
2978     return result;
2979 }
2980
2981 QModelIndex DocumentModel::revolutionQuads(QModelIndexList& istarts, QModelIndex& icenter, QModelIndex& iaxis,
2982                                            vector<double>& angles)
2983 {
2984     QModelIndex result;
2985
2986     HEXA_NS::Quads hstarts;
2987     int nbQuads = istarts.count();
2988     for (int i = 0; i < nbQuads; ++i)
2989         hstarts.push_back(getHexaPtr<HEXA_NS::Quad*>(istarts[i]));
2990
2991     HEXA_NS::Vertex* hcenter = getHexaPtr<HEXA_NS::Vertex*>(icenter);
2992     HEXA_NS::Vector* haxis = getHexaPtr<HEXA_NS::Vector*>(iaxis);
2993
2994     HEXA_NS::Elements* helts = _hexaDocument->revolutionQuads(hstarts, hcenter, haxis, angles);
2995     if (BadElement(helts))
2996         return result;
2997
2998     result = addElementsToTree(helts, _elementsDirItem);
2999
3000     return result;
3001
3002 }
3003
3004 // ==== PrismQuad or ExtrudeQuad
3005 QModelIndex DocumentModel::extrudeQuadTop(QModelIndex& istart, int nbre)
3006 {
3007     QModelIndex result;
3008
3009     HEXA_NS::Quad* hstart = getHexaPtr<HEXA_NS::Quad*>(istart);
3010     HEXA_NS::Elements* helts = _hexaDocument->extrudeQuadTop(hstart, nbre);
3011     if (BadElement(helts))
3012         return result;
3013
3014     result = addElementsToTree(helts, _elementsDirItem);
3015
3016     return result;
3017 }
3018
3019 QModelIndex DocumentModel::extrudeQuadUni(QModelIndex& istart, QModelIndex& dv, double len, int nbre)
3020 {
3021     QModelIndex result;
3022
3023     HEXA_NS::Quad* hstart = getHexaPtr<HEXA_NS::Quad*>(istart);
3024     HEXA_NS::Vector* hvec = getHexaPtr<HEXA_NS::Vector*>(dv);
3025
3026     HEXA_NS::Elements* helts = _hexaDocument->extrudeQuadUni(hstart, hvec, len, nbre);
3027     if (BadElement(helts))
3028         return result;
3029
3030     result = addElementsToTree(helts, _elementsDirItem);
3031
3032     return result;
3033 }
3034
3035 QModelIndex DocumentModel::extrudeQuad(QModelIndex& istart, QModelIndex& dv, vector<double>& tlen)
3036 {
3037     QModelIndex result;
3038
3039     HEXA_NS::Quad* hstart = getHexaPtr<HEXA_NS::Quad*>(istart);
3040     HEXA_NS::Vector* hvec = getHexaPtr<HEXA_NS::Vector*>(dv);
3041
3042     HEXA_NS::Elements* helts = _hexaDocument->extrudeQuad(hstart, hvec, tlen);
3043     if (BadElement(helts))
3044         return result;
3045     result = addElementsToTree(helts, _elementsDirItem);
3046
3047     return result;
3048 }
3049
3050 QModelIndex DocumentModel::extrudeQuadsTop (QModelIndexList& istarts, int nbre)
3051 {
3052     QModelIndex result;
3053
3054     HEXA_NS::Quads hquads;
3055     int nbQuads = istarts.count();
3056     for (int i = 0; i < nbQuads; ++i)
3057         hquads.push_back(getHexaPtr<HEXA_NS::Quad*>(istarts[i]));
3058
3059     HEXA_NS::Elements* helts = _hexaDocument->extrudeQuadsTop(hquads, nbre);
3060     if (BadElement(helts))
3061         return result;
3062     result = addElementsToTree(helts, _elementsDirItem);
3063
3064     return result;
3065 }
3066
3067 QModelIndex DocumentModel::extrudeQuadsUni (QModelIndexList& istarts, QModelIndex& axis, double len, int nbre)
3068 {
3069     QModelIndex result;
3070
3071     HEXA_NS::Quads hquads;
3072     int nbQuads = istarts.count();
3073     for (int i = 0; i < nbQuads; ++i)
3074         hquads.push_back(getHexaPtr<HEXA_NS::Quad*>(istarts[i]));
3075
3076     HEXA_NS::Vector* haxis = getHexaPtr<HEXA_NS::Vector*>(axis);
3077
3078     HEXA_NS::Elements* helts = _hexaDocument->extrudeQuadsUni(hquads, haxis, len, nbre);
3079     if (BadElement(helts))
3080         return result;
3081     result = addElementsToTree(helts, _elementsDirItem);
3082
3083     return result;
3084 }
3085
3086 QModelIndex DocumentModel::extrudeQuads(QModelIndexList& istarts, QModelIndex& iaxis, vector<double>& tlen)
3087 {
3088     QModelIndex result;
3089
3090     HEXA_NS::Quads hquads;
3091     int nbQuads = istarts.count();
3092     for (int i=0; i < nbQuads; ++i)
3093         hquads.push_back(getHexaPtr<HEXA_NS::Quad*>(istarts[i]));
3094
3095     HEXA_NS::Vector* haxis = getHexaPtr<HEXA_NS::Vector*>(iaxis);
3096
3097     HEXA_NS::Elements* helts = _hexaDocument->extrudeQuads(hquads, haxis, tlen);
3098     if (BadElement(helts))
3099         return result;
3100     result = addElementsToTree(helts, _elementsDirItem);
3101
3102     return result;
3103 }
3104
3105 // ==== Cut Edge
3106 QModelIndex DocumentModel::cutUni(QModelIndex& iEdge, int nbre)
3107 {
3108     QModelIndex result;
3109
3110     HEXA_NS::Edge* hedge = getHexaPtr<HEXA_NS::Edge*>(iEdge);
3111
3112     HEXA_NS::Elements* helts = _hexaDocument->cutUni(hedge, nbre);
3113     if (BadElement(helts))
3114         return result;
3115     result = addElementsToTree(helts, _elementsDirItem);
3116
3117     return result;
3118 }
3119
3120 QModelIndex DocumentModel::cut(QModelIndex& iEdge, vector<double>& tlen)
3121 {
3122     QModelIndex result;
3123
3124     HEXA_NS::Edge* hedge = getHexaPtr<HEXA_NS::Edge*>(iEdge);
3125
3126     HEXA_NS::Elements* helts = _hexaDocument->cut(hedge, tlen);
3127     if (BadElement(helts))
3128         return result;
3129     result = addElementsToTree(helts, _elementsDirItem);
3130
3131     return result;
3132 }
3133
3134 // ================================== END NEW ==================================