-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathTabReadWrite.cpp
508 lines (435 loc) · 15.9 KB
/
TabReadWrite.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/* -*-c++-*- IfcQuery www.ifcquery.com
*
MIT License
Copyright (c) 2017 Fabian Gerold
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma warning( disable: 4996 )
#include <random>
#include <sstream>
#include <QtGui/QKeyEvent>
#include <QtWidgets/QFileDialog>
#include <QtCore/QFile>
#include <QtCore/QSettings>
#include <QtCore/QTextStream>
#include <osgUtil/Optimizer>
#include <ifcpp/model/BasicTypes.h>
#include <ifcpp/model/BuildingModel.h>
#include <ifcpp/model/BuildingException.h>
#include <ifcpp/model/BuildingGuid.h>
#include <ifcpp/model/StatusCallback.h>
#include <ifcpp/reader/ReaderSTEP.h>
#include <ifcpp/writer/WriterSTEP.h>
#include <ifcpp/writer/WriterUtil.h>
#include "IncludeGeometryHeaders.h"
#include "IfcPlusPlusSystem.h"
#include "viewer/ViewerWidget.h"
#include "viewer/OrbitCameraManipulator.h"
#include "cmd/CommandManager.h"
#include "StoreyShiftWidget.h"
#include "TabReadWrite.h"
TabReadWrite::TabReadWrite( IfcPlusPlusSystem* sys, ViewerWidget* viewer, QWidget* parent ) : m_system(sys), m_viewer(viewer), QWidget( parent )
{
QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));
QStringList keys = settings.allKeys();
if( keys.contains( "recentFiles" ) )
{
m_recent_files = settings.value("recentFiles").toStringList();
}
m_combo_recent_files = new QComboBox();
m_combo_recent_files->setMaxVisibleItems( 40 );
connect( m_combo_recent_files, SIGNAL(currentIndexChanged(int)), this, SLOT( slotRecentFilesIndexChanged(int) ) );
m_btn_load = new QPushButton( "Load" );
m_btn_load->setStyleSheet( "min-width: 30px;" );
connect( m_btn_load, SIGNAL(clicked()), this, SLOT( slotLoadRecentIfcFileClicked() ) );
updateRecentFilesCombo();
QPushButton* btn_add_file = new QPushButton( "Choose file" );
connect( btn_add_file, SIGNAL( clicked() ), this, SLOT( slotAddOtherIfcFileClicked() ) );
// write
m_le_path_write = new QLineEdit( "IfcPlusPlus-out.ifc" );
QPushButton* btn_set_out_path = new QPushButton( "..." );
connect( btn_set_out_path, SIGNAL( clicked() ), this, SLOT( slotSetWritePathClicked() ) );
QString path_out;
if( settings.contains( "pathIfcFileOut" ) )
{
path_out = settings.value("pathIfcFileOut").toString();
}
m_le_path_write->setText( path_out );
QPushButton* btn_write_file = new QPushButton( "Write ifc file" );
connect( btn_write_file, SIGNAL( clicked() ), this, SLOT( slotWriteFileClicked() ) );
m_txt_out = new QTextEdit();
#ifdef _DEBUG
std::string str = "test";
std::string str1 = encodeStepString(str);
std::stringstream uuid_strs;
uuid_strs << createGUID32().c_str() << std::endl;
uuid_strs << createGUID32().c_str() << std::endl;
uuid_strs << createBase64Uuid().data() << std::endl;
m_txt_out->setText( uuid_strs.str().c_str() );
#endif
StoreyShiftWidget* storey_shift = new StoreyShiftWidget(m_system);
// layout
QHBoxLayout* combo_hbox = new QHBoxLayout();
combo_hbox->addWidget( m_combo_recent_files, 1 );
combo_hbox->addWidget( btn_add_file );
combo_hbox->addWidget( m_btn_load, 0 );
m_io_widget = new QWidget(this);
QVBoxLayout* io_vbox = new QVBoxLayout(m_io_widget);
io_vbox->setContentsMargins( 0, 0, 0, 0 );
io_vbox->addWidget( new QLabel( "Read IFC file" ), 0 );
io_vbox->addLayout( combo_hbox, 1 );
io_vbox->addSpacing( 10 );
io_vbox->addStretch( 1 );
m_io_splitter = new QSplitter( Qt::Horizontal );
m_io_splitter->addWidget( m_io_widget );
m_io_splitter->addWidget( m_txt_out );
QHBoxLayout* hbox = new QHBoxLayout();
hbox->addWidget( m_io_splitter );
setLayout( hbox );
if( keys.contains( "IOsplitterSizes" ) )
{
m_io_splitter->restoreState(settings.value("IOsplitterSizes").toByteArray());
}
}
TabReadWrite::~TabReadWrite(){}
void TabReadWrite::closeEvent( QCloseEvent* )
{
QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));
settings.setValue("IOsplitterSizes", m_io_splitter->saveState());
}
void TabReadWrite::messageTarget( void* ptr, shared_ptr<StatusCallback::Message> m )
{
TabReadWrite* myself = (TabReadWrite*)ptr;
if( myself )
{
std::lock_guard<std::mutex> lock(myself->m_mutex_messages);
std::string reporting_function_str( m->m_reporting_function );
std::wstringstream strs_report;
if( reporting_function_str.size() > 0 )
{
strs_report << m->m_reporting_function << ", ";
}
strs_report << m->m_message_text.c_str();
if( m->m_entity )
{
BuildingEntity* ent = dynamic_cast<BuildingEntity*>(m->m_entity);
if( ent )
{
strs_report << ", IFC entity: #" << ent->m_tag << "=" << EntityFactory::getStringForClassID(m->m_entity->classID());
}
}
std::wstring message_str = strs_report.str().c_str();
if( m->m_message_type == StatusCallback::MESSAGE_TYPE_GENERAL_MESSAGE )
{
QString qt_str = QString::fromStdWString( message_str );
myself->txtOut( qt_str );
}
else if( m->m_message_type == StatusCallback::MESSAGE_TYPE_WARNING )
{
QString qt_str = QString::fromStdWString( message_str );
myself->txtOutWarning( qt_str );
}
else if( m->m_message_type == StatusCallback::MESSAGE_TYPE_ERROR )
{
QString qt_str = QString::fromStdWString( message_str );
myself->txtOutError( qt_str );
}
else if( m->m_message_type == StatusCallback::MESSAGE_TYPE_PROGRESS_VALUE )
{
myself->progressValue( m->m_progress_value, m->m_progress_type );
}
else if( m->m_message_type == StatusCallback::MESSAGE_TYPE_CLEAR_MESSAGES )
{
myself->clearTxtOut();
}
}
}
void TabReadWrite::keyPressEvent( QKeyEvent* e )
{
if( e->text() == "c" )
{
m_recent_files.clear();
QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));
settings.setValue("recentFiles",m_recent_files );
updateRecentFilesCombo();
}
}
void TabReadWrite::updateRecentFilesCombo()
{
m_combo_recent_files->blockSignals( true );
m_combo_recent_files->clear();
for( size_t ii = 0; ii < m_recent_files.size(); ++ii )
{
const QString& path_recent_file = m_recent_files.at( ii );
QFileInfo fi( path_recent_file );
QString file_name = fi.filePath();
if( file_name.size() > 100 )
{
file_name = "..." + file_name.right(97);
}
m_combo_recent_files->addItem( file_name );
}
m_combo_recent_files->blockSignals( false );
}
void TabReadWrite::slotRecentFilesIndexChanged(int idx)
{
progressValue( 0, "" );
m_btn_load->setFocus();
}
void TabReadWrite::loadIfcFile( QString& path_in )
{
// redirect message callbacks
m_system->getGeometryConverter()->setMessageCallBack(std::bind(&TabReadWrite::messageTarget, this, std::placeholders::_1));
txtOut( QString( "loading file: " ) + path_in );
QApplication::processEvents();
clock_t millisecs = clock();
m_system->notifyModelCleared();
m_txt_out->clear();
QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));
if( !QFile::exists(path_in) )
{
txtOutError( QString("file ") + path_in + QString(" does not exist\n") );
// remove all non-existing files from recent files combo
for( int i=0; i<m_recent_files.size(); )
{
const QString& recent_file = m_recent_files[i];
if( !QFile::exists(recent_file) )
{
m_recent_files.takeAt( i );
}
else
{
++i;
}
}
settings.setValue("recentFiles",m_recent_files );
updateRecentFilesCombo();
return;
}
else
{
// move to top of recent files list
int i = m_recent_files.indexOf( path_in );
if( i > 0 )
{
QString current_path = m_recent_files.takeAt( i );
m_recent_files.insert( 0, current_path );
m_recent_files.removeDuplicates();
settings.setValue("recentFiles",m_recent_files );
updateRecentFilesCombo();
}
else
{
m_recent_files.insert( 0, path_in );
m_recent_files.removeDuplicates();
settings.setValue("recentFiles",m_recent_files );
updateRecentFilesCombo();
}
}
try
{
std::string path_str = path_in.toStdString();
if (path_str.length() == 0)
{
return;
}
// first remove previously loaded geometry from scenegraph
osg::ref_ptr<osg::Switch> model_switch = m_system->getModelNode();
SceneGraphUtils::clearAllChildNodes(model_switch);
m_system->clearSelection();
// reset the IFC model
shared_ptr<GeometryConverter> geometry_converter = m_system->getGeometryConverter();
geometry_converter->clearMessagesCallback();
geometry_converter->resetModel();
geometry_converter->getGeomSettings()->setNumVerticesPerCircle(16);
geometry_converter->getGeomSettings()->setMinNumVerticesPerArc(4);
std::stringstream err;
// load file to IFC model
shared_ptr<ReaderSTEP> step_reader(new ReaderSTEP());
step_reader->setMessageCallBack(std::bind(&TabReadWrite::messageTarget, this, std::placeholders::_1));
step_reader->loadModelFromFile(path_str, geometry_converter->getBuildingModel());
// convert IFC geometric representations into Carve geometry
geometry_converter->setCsgEps(1.5e-08 * geometry_converter->getBuildingModel()->getUnitConverter()->getLengthInMeterFactor());
geometry_converter->convertGeometry();
// convert Carve geometry to OSG
shared_ptr<ConverterOSG> converter_osg(new ConverterOSG(geometry_converter->getGeomSettings()));
converter_osg->setMessageTarget(geometry_converter.get());
converter_osg->convertToOSG(geometry_converter->getShapeInputData(), model_switch);
if (model_switch)
{
bool optimize = true;
if (optimize)
{
osgUtil::Optimizer opt;
opt.optimize(model_switch);
}
// if model bounding sphere is far from origin, move to origin
const osg::BoundingSphere& bsphere = model_switch->getBound();
if (bsphere.center().length() > 10000)
{
if (bsphere.center().length()/bsphere.radius() > 100)
{
//std::unordered_set<osg::Node*> set_applied;
//SceneGraphUtils::translateGroup(model_switch, , set_applied, length_in_meter*0.001);
osg::MatrixTransform* mt = new osg::MatrixTransform();
mt->setMatrix(osg::Matrix::translate(-bsphere.center()*0.98));
int num_children = model_switch->getNumChildren();
for (int i = 0; i < num_children; ++i)
{
osg::Node* node = model_switch->getChild(i);
if (!node)
{
continue;
}
mt->addChild(node);
}
SceneGraphUtils::removeChildren(model_switch);
model_switch->addChild(mt);
}
}
}
}
catch( BuildingException& e )
{
txtOutError( e.what() );
}
catch(std::exception& e)
{
txtOutError( e.what() );
}
m_viewer->update();
osgViewer::View* main_view = m_viewer->getMainView();
if( main_view )
{
osgGA::CameraManipulator* camera_manip = main_view->getCameraManipulator();
OrbitCameraManipulator* orbit_manip = dynamic_cast<OrbitCameraManipulator*>( camera_manip );
if( orbit_manip )
{
osg::BoundingSphere bs = m_system->getModelNode()->computeBound();
orbit_manip->zoomToBoundingSphere( bs );
}
}
int time_diff = (clock() - millisecs)/(double)CLOCKS_PER_SEC;
int num_entities = m_system->getIfcModel()->getMapIfcEntities().size();
txtOut( tr("File loaded: ") + QString::number(num_entities) + " entities in " + QString::number( round(time_diff*10)*0.1 ) + " sec." );
m_system->notifyModelLoadingDone();
progressValue( 1.0, "" );
}
void TabReadWrite::txtOut( QString txt )
{
QString basecol = palette().text().color().name();
m_txt_out->append( "<div style=\"color:" + basecol + ";\">" + txt.replace( "\n", "<br/>" ) + "</div><br/>" );
}
void TabReadWrite::txtOutWarning( QString txt )
{
m_txt_out->append( "<div style=\"color:#dca103;\">Warning: " + txt.replace( "\n", "<br/>" ) + "</div><br/>" );
}
void TabReadWrite::txtOutError( QString txt )
{
m_txt_out->append( "<div style=\"color:red;\">Error: " + txt.replace( "\n", "<br/>" ) + "</div><br/>" );
}
void TabReadWrite::progressValue(double progress_value_in, const std::string& progress_type)
{
Q_EMIT(signalProgressValue(progress_value_in, progress_type));
}
void TabReadWrite::clearTxtOut()
{
m_txt_out->clear();
}
void TabReadWrite::slotLoadRecentIfcFileClicked()
{
QPushButton* btn_load = (QPushButton*)sender();
if( !btn_load )
{
return;
}
int row = m_combo_recent_files->currentIndex();
if( row < 0 || row >= m_combo_recent_files->count() )
{
return;
}
m_io_widget->setDisabled( true );
if( row < m_recent_files.size() )
{
QString file_name = m_recent_files.at( row );
loadIfcFile( file_name );
}
m_io_widget->setDisabled(false);
}
void TabReadWrite::slotClearRecentIfcFiles()
{
m_recent_files.clear();
QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));
settings.setValue("recentFiles",m_recent_files );
updateRecentFilesCombo();
}
void TabReadWrite::slotAddOtherIfcFileClicked()
{
QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));
QStringList keys = settings.allKeys();
QString default_dir = "";
if( keys.contains( "recentFiles" ) )
{
default_dir = settings.value( "defaultDir" ).toString();
}
QString selected_file = QFileDialog::getOpenFileName(this, "Choose IFC file", default_dir, QString(), Q_NULLPTR, QFileDialog::DontUseNativeDialog);
if( !selected_file.isEmpty() )
{
QDir current_dir;
settings.setValue( "defaultDir", current_dir.absoluteFilePath(selected_file) );
loadIfcFile( selected_file );
}
}
void TabReadWrite::slotSetWritePathClicked()
{
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this, "IfcPlusPlus - choose path to write ifc file", m_le_path_write->text(), "All Files (*);;Text Files (*.ifc)", &selectedFilter, QFileDialog::DontUseNativeDialog);
if( !fileName.isEmpty() )
{
m_le_path_write->setText(fileName);
}
}
void TabReadWrite::slotWriteFileClicked()
{
QString path = m_le_path_write->text();
QSettings settings(QSettings::UserScope, QLatin1String("IfcPlusPlus"));
settings.setValue("pathIfcFileOut", path );
txtOut( "writing file: " + path );
int millisecs = clock();
std::string path_std = path.toStdString();
try
{
if (path_std.length() == 0)
{
return;
}
shared_ptr<GeometryConverter> geom_converter = m_system->getGeometryConverter();
shared_ptr<BuildingModel>& model = geom_converter->getBuildingModel();
std::string applicationName = "IfcPlusPlus";
model->initFileHeader(path_std, applicationName);
std::stringstream stream;
shared_ptr<WriterSTEP> writer_step(new WriterSTEP());
writer_step->writeModelToStream(stream, model);
QFile file_out(QString::fromStdString(path_std.c_str()));
if (!file_out.open(QIODevice::WriteOnly | QIODevice::Text))
{
return;
}
QTextStream file_out_stream(&file_out);
file_out_stream << stream.str().c_str();
file_out.close();
}
catch( std::exception& e )
{
txtOutWarning( "couldn't write file " + path + e.what() );
}
int time_diff = clock() - millisecs;
txtOut( "file written (" + QString::number( time_diff*0.001 ) + " sec)" );
progressValue( 1.0, "" );
}