File: WKT_io_plugin.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (128 lines) | stat: -rw-r--r-- 3,698 bytes parent folder | download
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "Scene_polylines_item.h"

#include <CGAL/Three/CGAL_Lab_io_plugin_interface.h>
#include <CGAL/Three/Three.h>
#include <QInputDialog>
#include <QApplication>
#include <fstream>

#include <CGAL/IO/WKT.h>
#include <QMessageBox>

using namespace CGAL::Three;
class CGAL_Lab_wkt_plugin :
  public QObject,
  public CGAL::Three::CGAL_Lab_io_plugin_interface
{
  Q_OBJECT
  Q_INTERFACES(CGAL::Three::CGAL_Lab_io_plugin_interface)
  Q_PLUGIN_METADATA(IID "com.geometryfactory.CGALLab.IOPluginInterface/1.90" FILE "wkt_io_plugin.json")

public:
  bool isDefaultLoader(const CGAL::Three::Scene_item *item) const
  {
    if(qobject_cast<const Scene_polylines_item*>(item))
      return true;
    return false;
  }

  QString name() const { return "wkt_plugin"; }

  // To change if we end up supporting other objects in the plugin
  QString nameFilters() const { return "WKT polylines (*.wkt)"; }
  bool canLoad(QFileInfo fileinfo) const;
  QList<Scene_item*> load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true);

  bool canSave(const CGAL::Three::Scene_item*);
  bool save(QFileInfo fileinfo,QList<CGAL::Three::Scene_item*>&);
};

bool CGAL_Lab_wkt_plugin::
canLoad(QFileInfo) const {
  return true;
}

QList<Scene_item*>
CGAL_Lab_wkt_plugin::
load(QFileInfo fileinfo, bool& ok, bool add_to_scene) {
  std::ifstream in(fileinfo.filePath().toUtf8());

  if(!in)
    std::cerr << "Error!\n";

  QApplication::setOverrideCursor(Qt::WaitCursor);

  if(fileinfo.size() == 0)
  {
    CGAL::Three::Three::warning( tr("The file you are trying to load is empty."));
    ok = false;
    QApplication::restoreOverrideCursor();
    return QList<Scene_item*>();
  }

  std::list<std::vector<Scene_polylines_item::Point_3> > polylines;
  bool success = CGAL::IO::read_multi_linestring_WKT (in, polylines);
  if(! success){
    in.close();
    in.open(fileinfo.filePath().toUtf8());
    std::vector<Scene_polylines_item::Point_3> polyline;
    std::cout << " read" << std::endl;
    success = CGAL::IO::read_linestring_WKT (in, polyline);
    std::cout << " done " <<  std::boolalpha << success << "  "  << polyline.size() << std::endl;
    if(! success){
      ok = false;
      QApplication::restoreOverrideCursor();
      return QList<Scene_item*>();
    }
    polylines.push_back(polyline);
  }

  Scene_polylines_item* item = new Scene_polylines_item;
  item->polylines = polylines;
  item->setName(fileinfo.completeBaseName());
  item->setColor(Qt::black);
  std::cerr << "Number of polylines in item: " << item->polylines.size() << std::endl;
  item->invalidateOpenGLBuffers();
  ok = true;
  if(add_to_scene)
    CGAL::Three::Three::scene()->addItem(item);

  QApplication::restoreOverrideCursor();

  return QList<Scene_item*>() << item;
}

bool CGAL_Lab_wkt_plugin::canSave(const CGAL::Three::Scene_item* item)
{
  // This plugin supports polylines
  return (qobject_cast<const Scene_polylines_item*>(item));
}

bool CGAL_Lab_wkt_plugin::
save(QFileInfo fileinfo,QList<CGAL::Three::Scene_item*>& items)
{
  Scene_item* item = items.front();
  // Check extension (quietly)
  std::string extension = fileinfo.suffix().toUtf8().data();
  std::cerr << extension << std::endl;
  if (extension != "wkt" && extension != "WKT")
    return false;

  std::ofstream out(fileinfo.filePath().toUtf8().data(), std::ios::binary);
  out.precision (std::numeric_limits<double>::digits10 + 2);

  // This plugin supports point sets
  Scene_polylines_item* polylines_item =
    qobject_cast<Scene_polylines_item*>(item);
  if (polylines_item)
  {
    CGAL::IO::write_multi_linestring_WKT (out, polylines_item->polylines);
    items.pop_front();
    return true;
  }

  return false;
}


#include "WKT_io_plugin.moc"