-
Notifications
You must be signed in to change notification settings - Fork 17
/
mainwindow.cpp.autosave
347 lines (266 loc) · 14 KB
/
mainwindow.cpp.autosave
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace Eigen;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// https://www.youtube.com/watch?v=2VIGb31iwds
OpencascadeWidget = new Opencascade(this);
ui->gridLayout_opencascade->addWidget(OpencascadeWidget);
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWindow::Update_Opencascade);
timer->start(200);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Update_Opencascade()
{
OpencascadeWidget->Redraw();
OpencascadeWidget->get_selections();
}
bool MainWindow::open_dxf_file(std::string filename){
dx_iface().clear(&fData); // Clear previous loaded dxf data.
bool ok = iface->fileImport( filename, &fData );
if (!ok) {
std::cout << "Error reading file " << filename << std::endl;
} else {
std::cout << "Ok reading file " << filename << std::endl;
// Process opencascade primitives.
load_opencascade_primitives();
}
return ok;
}
bool MainWindow::save_dxf_file(std::string filename){
bool ok = iface->fileExport(filename, DRW::AC1027 , false, &fData);
if (!ok) {
std::cout << "Error saving file " << filename << std::endl;
} else {
std::cout << "Ok saving file " << filename << std::endl;
}
return ok;
}
//! Example how to write a primitive to the dxf file.
bool MainWindow::write_entity(){
std::cout<<"add line item"<<std::endl;
DRW_Line testline;
testline.basePoint.x = 10;
testline.basePoint.y = 20;
testline.secPoint.x = 30;
testline.secPoint.y = 30;
iface->addLine(testline);
return 1;
}
void MainWindow::load_opencascade_primitives()
{
OpencascadeWidget->erase_all();
// Print the dxf output.
auto blocks = fData.mBlock->ent;
for (auto iter = blocks.begin(); iter != blocks.end(); iter++){
if(DRW::POINT == (*iter)->eType){ std::cout<<"point"<<std::endl;
DRW_Point *point = dynamic_cast<DRW_Point*>(*iter);
Handle(AIS_Shape) ashape=draw_primitives().draw_3d_point({point->basePoint.x,point->basePoint.y,point->basePoint.z});
ashape=draw_primitives().colorize(ashape,draw_primitives().autocad_color(point->color),1);
struct data d;
d.ashape=ashape;
d.type=primitivetype::point;
d.start={point->basePoint.x,point->basePoint.y,point->basePoint.z};
d.end={point->basePoint.x,point->basePoint.y,point->basePoint.z};
datavec.push_back(d);
}
if(DRW::LINE == (*iter)->eType){
DRW_Line *line = dynamic_cast<DRW_Line*>(*iter);
Handle(AIS_Shape) ashape=draw_primitives().draw_3d_line({line->basePoint.x, line->basePoint.y, line->basePoint.z},{line->secPoint.x, line->secPoint.y, line->secPoint.z});
ashape=draw_primitives().colorize(ashape,draw_primitives().autocad_color(line->color),1);
struct data d;
d.ashape=ashape;
d.type=primitivetype::line;
d.start={line->basePoint.x, line->basePoint.y, line->basePoint.z};
// Line middle-point, half-way point.
d.control.push_back({line->basePoint.x+line->secPoint.x/2, line->basePoint.y+line->secPoint.y/2,line->basePoint.z+line->secPoint.z/2});
d.end={line->secPoint.x, line->secPoint.y, line->secPoint.z};
datavec.push_back(d);
}
// Lwpolyline is saved as clockwise [cw]
if(DRW::LWPOLYLINE == (*iter)->eType){ std::cout<<"lwpolyline"<<std::endl;
DRW_LWPolyline *lwpolyline = dynamic_cast<DRW_LWPolyline*>(*iter);
std::vector<gp_Pnt> pntvec;
for(unsigned int i=0; i<lwpolyline->vertlist.size(); i++){
pntvec.push_back({lwpolyline->vertlist.at(i)->x,lwpolyline->vertlist.at(i)->y,0});
} // Close the polyline.
pntvec.push_back({lwpolyline->vertlist.front()->x,lwpolyline->vertlist.front()->y,0});
Handle(AIS_Shape) ashape=draw_primitives().draw_3d_line_wire(pntvec);
ashape=draw_primitives().colorize(ashape,draw_primitives().autocad_color(lwpolyline->color),1);
// Calculate if lwpolyline (wire) is cw or ccw. If area<0 => ccw. If area>0 => cw.
double area=0;
std::vector<gp_Pnt>::iterator it,it1;
for(it=pntvec.begin(); it<pntvec.end()-1; it++){
it1=it+1;
area+=(it1->X()-it->X())*(it1->Y()+it->Y());
}
area=area/2; // Area / 2.
// std::cout<<"lwpolyline area: "<<area<<std::endl; // https://www.youtube.com/watch?v=HG7I4oniOyA
// If a ccw, invert to cw.
if(area<0){
std::reverse(pntvec.begin(),pntvec.end());
}
// Check output agian.
area=0;
for(it=pntvec.begin(); it<pntvec.end()-1; it++){
it1=it+1;
area+=(it1->X()-it->X())*(it1->Y()+it->Y());
}
area=area/2; // Area / 2.
// std::cout<<"lwpolyline cw area : "<<area<<std::endl; // https://www.youtube.com/watch?v=HG7I4oniOyA
struct data d;
d.ashape=ashape;
d.type=primitivetype::lwpolyline;
d.start={pntvec.front().X(),pntvec.front().Y(),0}; // std::cout<<"lwpolyline points x: "<<d.start.X()<<" y:"<<d.start.Y()<<" no z"<<std::endl;
for(unsigned int i=1; i<pntvec.size()-1; i++){
d.control.push_back({pntvec.at(i).X(),pntvec.at(i).Y(),0}); // std::cout<<"lwpolyline points x: "<<pntvec.at(i).X()<<" y:"<<pntvec.at(i).Y()<<" no z"<<std::endl;
}
d.end={pntvec.back().X(),pntvec.back().Y(),0}; // std::cout<<"lwpolyline points x: "<<d.end.X()<<" y:"<<d.end.Y()<<" no z"<<std::endl;
datavec.push_back(d);
}
// The spline has no direction just as the line.
if(DRW::SPLINE == (*iter)->eType){ std::cout<<"spline"<<std::endl;
DRW_Spline *spline = dynamic_cast<DRW_Spline*>(*iter);
std::vector<gp_Pnt> pntvec; // input
for(unsigned int i=0; i<spline->controllist.size(); i++){
pntvec.push_back({spline->controllist.at(i)->x,spline->controllist.at(i)->y,spline->controllist.at(i)->z});
}
Handle(AIS_Shape) ashape=draw_primitives().draw_3d_spline(pntvec,5);
ashape=draw_primitives().colorize(ashape,draw_primitives().autocad_color(spline->color),1);
OpencascadeWidget->show_shape(ashape);
//std::cout<<"spline fitlist.size:"<<spline->fitlist.size()<<std::endl;
//std::cout<<"spline controllist.size:"<<spline->controllist.size()<<std::endl;
struct data d;
d.ashape=ashape;
d.type=primitivetype::spline;
d.start={spline->controllist.front()->x,spline->controllist.front()->y,spline->controllist.front()->z};
for(unsigned int i=1; i<spline->controllist.size()-1; i++){
d.control.push_back({spline->controllist.at(i)->x,spline->controllist.at(i)->y,spline->controllist.at(i)->z});
}
d.end={spline->controllist.back()->x,spline->controllist.back()->y,spline->controllist.back()->z};
datavec.push_back(d);
}
// Arc's are saved as clockwise [cw] by draw_primivite->get points funtion !
if(DRW::ARC == (*iter)->eType){ std::cout<<"arc"<<std::endl;
DRW_Arc *arc = dynamic_cast<DRW_Arc*>(*iter);
Handle(AIS_Shape) ashape=draw_primitives().draw_2d_acad_arc({arc->center().x,arc->center().y,arc->center().z}, arc->radius(),
arc->startAngle(),arc->endAngle());
ashape=draw_primitives().colorize(ashape,draw_primitives().autocad_color(arc->color),1);
struct data d;
d.ashape=ashape;
d.type=primitivetype::arc;
// Request a few arc circumfence points.
int division=8;
d.control=draw_primitives().get_arc_circumfence_points({arc->center().x,arc->center().y,arc->center().z}, arc->radius(),
arc->startAngle(),arc->endAngle(),division);
d.start=d.control.front(); // std::cout<<"arc starpoint x:"<<d.start.X()<<" y:"<<d.start.Y()<<" z:"<<d.start.Z()<<std::endl;
d.end=d.control.back(); // std::cout<<"arc endpoint x:"<<d.end.X()<<" y:"<<d.end.Y()<<" z:"<<d.end.Z()<<std::endl;
d.control.pop_back();
d.control.erase(d.control.begin());
for(unsigned int i=0; i<d.control.size(); i++){
std::cout<<"arc controlpoints x:"<<d.control.at(i).X()<<" y:"<<d.control.at(i).Y()<<" z:"<<d.control.at(i).Z()<<std::endl;
}
datavec.push_back(d);
}
// Circles are saved clockwise [cw], startpoint is right side of circle.
if(DRW::CIRCLE == (*iter)->eType){ // std::cout<<"circle"<<std::endl;
DRW_Circle *circle = dynamic_cast<DRW_Circle*>(*iter);
Handle(AIS_Shape) ashape=draw_primitives().draw_2d_circle({circle->basePoint.x,circle->basePoint.y,circle->basePoint.z},circle->radious);
ashape=draw_primitives().colorize(ashape,draw_primitives().autocad_color(circle->color),1);
struct data d;
d.ashape=ashape;
d.type=primitivetype::circle;
// For contour recognize we need a few circle circumfence points to perform the pip (point in polygon) algoritme.
// Right side of circle
// Clockwise output, division (segmentation value) :
int division=8;
d.control=draw_primitives().get_cirlce_circumfence_points({circle->basePoint.x,circle->basePoint.y,circle->basePoint.z},circle->radious,division);
d.start=d.control.front();
d.end=d.control.back();
d.control.pop_back();
d.control.erase(d.control.begin());
datavec.push_back(d);
}
// Ellipses are saved clockwise [cw], startpoint is secpoint.
if(DRW::ELLIPSE == (*iter)->eType){ std::cout<<"ellipse"<<std::endl;
DRW_Ellipse *ellipse = dynamic_cast<DRW_Ellipse*>(*iter);
// To be reviewed when we have more time:
Handle(AIS_Shape) ashape=draw_primitives().draw_2d_ellipse({ellipse->basePoint.x,ellipse->basePoint.y,ellipse->basePoint.z},
{ellipse->secPoint.x,ellipse->secPoint.y,ellipse->secPoint.z}, // offset from base
ellipse->staparam,
ellipse->endparam,
ellipse->ratio);
ashape=draw_primitives().colorize(ashape,draw_primitives().autocad_color(ellipse->color),1);
std::cout<<"ellipse secpoint x:"<<ellipse->secPoint.x<<" y:"<<ellipse->secPoint.y<<" z:"<<ellipse->secPoint.z<<std::endl;
std::cout<<"ellipse extpoint x:"<<ellipse->extPoint.x<<" y:"<<ellipse->extPoint.y<<" z:"<<ellipse->extPoint.z<<std::endl;
struct data d;
d.ashape=ashape;
d.type=primitivetype::ellipse;
// Clockwise output, division (segmentation value) :
int division=8;
d.control=draw_primitives().get_ellipse_circumfence_points({ellipse->basePoint.x,ellipse->basePoint.y,ellipse->basePoint.z},
{ellipse->secPoint.x,ellipse->secPoint.y,ellipse->secPoint.z}, // offset from base
ellipse->staparam,
ellipse->endparam,
ellipse->ratio,division);
d.start=d.control.front();
d.end=d.control.back();
d.control.pop_back();
d.control.erase(d.control.begin());
datavec.push_back(d);
// Acad's ellipse nr's, https://github.com/grotius-cnc/cadcam/blob/master/dxf/read_ellipse_AC1027.cpp
// x,y,z centerpoint 10,20,30
// x,y,z endpoint mayor 11,21,31 ( coordinates relative to ellipse centerpoint..)
// ratio 40
// start angle 41
// end angle 42
}
}
// Show shapes from datavec
for(unsigned int i=0; i<datavec.size(); i++){
OpencascadeWidget->show_shape(datavec.at(i).ashape);
}
OpencascadeWidget->zoom_all(); // Zoom to extends. Top view is already set when initializing the opencascadewidget at startup.
}
void MainWindow::on_toolButton_view_top_pressed()
{
OpencascadeWidget->set_orthographic();
OpencascadeWidget->set_view_top();
}
void MainWindow::on_toolButton_open_dxf_pressed(){
OpencascadeWidget->set_orthographic();
OpencascadeWidget->set_view_top();
std::string filename=ui->lineEdit_dxf_filename->text().toStdString();
open_dxf_file(filename); // Read the dxf, for now it's directly displayed by opencascade.
OpencascadeWidget->zoom_all();
}
void MainWindow::on_toolButton_save_dxf_pressed()
{
std::string filename=ui->lineEdit_dxf_filename->text().toStdString();
save_dxf_file(filename);
}
void MainWindow::on_toolButton_add_line_pressed()
{
write_entity();
}
void MainWindow::on_toolButton_calculate_contours_pressed()
{
}
void MainWindow::on_toolButton_stacket_page_plus_pressed()
{
if(ui->stackedWidget->currentIndex() != ui->stackedWidget->count()-1){
ui->stackedWidget->setCurrentIndex(ui->stackedWidget->currentIndex()+1);
} else {
//if(ui->stackedWidget->currentIndex() == ui->stackedWidget->count()-1){
ui->stackedWidget->setCurrentIndex(0);
}
std::cout<<"curindex:"<<ui->stackedWidget->currentIndex()<<std::endl;
//}
}