Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a057e25

Browse files
committed
optimize select
1 parent f82e94f commit a057e25

File tree

2 files changed

+148
-71
lines changed

2 files changed

+148
-71
lines changed

front/src/components/pages/PackingTab.vue

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@
257257

258258
<border-conditions ref="borderConditionsComponent" :dialog="dialogBorderConditions" :properties="properties"
259259
@closeDialog="dialogBorderConditions = false"/>
260-
<import-packing ref="refImportPacking"/>
260+
<import-packing ref="refImportPacking" @loadtxtpacking="loadTxtPacking"/>
261261
<download-packing ref="refExportPacking"/>
262262

263263
</v-toolbar>
@@ -434,63 +434,36 @@
434434
p.mouseReleased = () => {
435435
locked = false;
436436
if (dragged) {
437-
// let box = {
438-
// points: [
439-
// {
440-
// x: Math.min(bx, xInit),
441-
// y: Math.min(by, yInit)
442-
// },
443-
// {
444-
// x: Math.max(bx, xInit),
445-
// y: Math.min(by, yInit)
446-
// },
447-
// {
448-
// x: Math.max(bx, xInit),
449-
// y: Math.max(by, yInit)
450-
// },
451-
// {
452-
// x: Math.min(bx, xInit),
453-
// y: Math.max(by, yInit)
454-
// },
455-
// ]
456-
// };
457-
// let height = this.packing.height;
458-
// let width = this.packing.width;
459-
// if(this.packing.graph) {
460-
// this.packing.polygons.forEach(pol => {
461-
// for (let i = 0; i < pol.points.length; i++) {
462-
// let pntA = pol.points[i];
463-
// let pntB = pol.points[(i + 1) % pol.points.length];
464-
//
465-
// if ([pntA.x, pntA.y] in this.packing.graph && [pntB.x, pntB.y] in this.packing.graph[[pntA.x, pntA.y]]) {
466-
// this.packing.graph[[pntA.x, pntA.y]][[pntB.x, pntB.y]] = {
467-
// selected: false
468-
// }
469-
// } else if ([pntB.x, pntB.y] in this.packing.graph && [pntA.x, pntA.y] in this.packing.graph[[pntB.x, pntB.y]]) {
470-
// this.packing.graph[[pntB.x, pntB.y]][[pntA.x, pntA.y]] = {
471-
// selected: false
472-
// }
473-
// }
474-
// }
475-
// });
476-
// this.packing.polygons.forEach(pol => {
477-
// pol.selected = this.polygonIntersection(pol, box, width, height, p);
478-
// for (let i = 0; i < pol.points.length; i++) {
479-
// let pntA = pol.points[i];
480-
// let pntB = pol.points[(i + 1) % pol.points.length];
481-
//
482-
// if ([pntA.x, pntA.y] in this.packing.graph && [pntB.x, pntB.y] in this.packing.graph[[pntA.x, pntA.y]]) {
483-
// this.packing.graph[[pntA.x, pntA.y]][[pntB.x, pntB.y]] = {
484-
// selected: pol.selected || this.packing.graph[[pntA.x, pntA.y]][[pntB.x, pntB.y]].selected
485-
// }
486-
// } else if ([pntB.x, pntB.y] in this.packing.graph && [pntA.x, pntA.y] in this.packing.graph[[pntB.x, pntB.y]]) {
487-
// this.packing.graph[[pntB.x, pntB.y]][[pntA.x, pntA.y]] = {
488-
// selected: pol.selected || this.packing.graph[[pntB.x, pntB.y]][[pntA.x, pntA.y]].selected
489-
// }
490-
// }
491-
// }
492-
// });
493-
// }
437+
let box = {
438+
points: [
439+
{
440+
x: Math.min(bx, xInit),
441+
y: Math.min(by, yInit)
442+
},
443+
{
444+
x: Math.max(bx, xInit),
445+
y: Math.min(by, yInit)
446+
},
447+
{
448+
x: Math.max(bx, xInit),
449+
y: Math.max(by, yInit)
450+
},
451+
{
452+
x: Math.min(bx, xInit),
453+
y: Math.max(by, yInit)
454+
},
455+
]
456+
};
457+
let height = this.packing.height;
458+
let width = this.packing.width;
459+
if(this.packing.graph) {
460+
this.packing.polygons.forEach(pol => {
461+
pol.selected = false;
462+
});
463+
this.packing.polygons.forEach(pol => {
464+
pol.selected = this.polygonIntersection(pol, box, width, height, p);
465+
});
466+
}
494467
dragged = false;
495468
p.draw();
496469
}
@@ -526,17 +499,8 @@
526499
const pntA = JSON.parse("[" + pointA + "]");
527500
const pntB = JSON.parse("[" + pointB + "]");
528501
529-
if (graph[pointA][pointB].selected) {
530-
p.stroke(189, 189, 189);
531-
} else {
532-
p.stroke(33, 33, 33);
533-
}
534-
535-
if (graph[pointA][pointB].selected) {
536-
p.strokeWeight(4);
537-
} else {
538-
p.strokeWeight(3);
539-
}
502+
p.stroke(33, 33, 33);
503+
p.strokeWeight(3);
540504
541505
p.line(
542506
((pntA[0] / width) * widthContainer) + xAxisOffset,
@@ -755,6 +719,17 @@
755719
}
756720
}
757721
}
722+
if(polygon.selected) {
723+
p.fill('rgba(0,0,0, 0.25)');
724+
p.beginShape();
725+
polygon.points.forEach(pnt => {
726+
p.vertex(
727+
((pnt.x / width) * this.getWidth(p)) + this.getOffsetXAxis(),
728+
(((height - pnt.y) / height) * this.getHeight(p)) + + this.getOffsetYAxis()
729+
);
730+
});
731+
p.endShape(p.CLOSE);
732+
}
758733
},
759734
execute() {
760735
this.$validator.validateAll().then(result => {
@@ -838,6 +813,11 @@
838813
}
839814
});
840815
},
816+
loadTxtPacking(data) {
817+
this.$store.commit("newPacking", data);
818+
this.parseMesh(data);
819+
this.$refs.borderConditionsComponent.updatePacking();
820+
},
841821
parseMesh(mesh) {
842822
//console.log(resp);
843823

front/src/components/templates/ImportPacking.vue

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
<script>
2828
import validation from './../../services/validation.service';
29+
import * as poly2tri from 'poly2tri';
2930
3031
export default {
3132
name: "ImportPacking",
@@ -36,6 +37,11 @@
3637
file: null
3738
}
3839
},
40+
computed: {
41+
properties () {
42+
return this.$store.getters.getProperties;
43+
},
44+
},
3945
created() {
4046
this.validation.changeLanguage('en');
4147
},
@@ -49,8 +55,99 @@
4955
});
5056
} else if(this.file.type === 'text/plain') {
5157
this.file.text().then(res => {
52-
console.log(res);
53-
this.close();
58+
try {
59+
let lines = res.split("\n");
60+
let firstLine = lines[0].split(" ");
61+
let numberOfPoints = parseInt(firstLine[0]);
62+
let numberOfEdges = parseInt(firstLine[1]);
63+
let numberOfProperties = parseInt(firstLine[2]);
64+
let numberOfPolygons = parseInt(firstLine[3]);
65+
let points = {};
66+
let propertiesFile = {};
67+
let polygons = [];
68+
let height = 0;
69+
let width = 0;
70+
for (let i = 1; i < lines.length; i++) {
71+
if (i <= numberOfPoints) {
72+
let pnt = lines[i].split(" ");
73+
let x = parseFloat(pnt[0]);
74+
let y = parseFloat(pnt[1]);
75+
width = (width < x) ? x : width;
76+
height = (height < y) ? y : height;
77+
points[i] = {
78+
x: x,
79+
y: y
80+
}
81+
} else if (i <= numberOfPoints + numberOfEdges) {
82+
83+
} else if (i <= numberOfPoints + numberOfEdges + numberOfProperties) {
84+
if (lines[i] in this.properties) {
85+
propertiesFile[i - (numberOfPoints + numberOfEdges)] = this.properties[lines[i]];
86+
} else {
87+
this.$store.commit("addProperty", {
88+
label: lines[i],
89+
typeOfValue: "Number",
90+
color: "#F44336",
91+
default: "11",
92+
selected: false
93+
});
94+
propertiesFile[i - (numberOfPoints + numberOfEdges)] = this.properties[lines[i]];
95+
}
96+
97+
} else if (i <= numberOfPoints + numberOfEdges + numberOfProperties + numberOfPolygons) {
98+
let polygonLine = lines[i].split(" ");
99+
let vertices = parseInt(polygonLine[0]);
100+
let pointsArray = [];
101+
let propertiesArray = [];
102+
let numberOfProperties = parseInt(polygonLine[vertices + 3]);
103+
for (let j = 1; j <= vertices; j++) {
104+
pointsArray.push(points[polygonLine[j]]);
105+
}
106+
107+
for (let j = vertices + 4; j < vertices + 4 + numberOfProperties; j++) {
108+
let aProperty = propertiesFile[polygonLine[j]];
109+
propertiesArray.push({key: aProperty.label, value: aProperty.default});
110+
}
111+
112+
let triangulation = [];
113+
if (propertiesArray.length > 0) {
114+
let contour = [];
115+
pointsArray.forEach(pnt => {
116+
contour.push(new poly2tri.Point(pnt.x, pnt.y))
117+
});
118+
let swctx = new poly2tri.SweepContext(contour);
119+
swctx.triangulate();
120+
let triangles = swctx.getTriangles();
121+
triangles.forEach(function (t) {
122+
let triangle = [];
123+
t.getPoints().forEach(function (p) {
124+
triangle.push({x: p.x, y: p.y});
125+
});
126+
127+
triangulation.push(triangle);
128+
});
129+
}
130+
131+
polygons.push({
132+
label: "",
133+
radius: null,
134+
points: pointsArray,
135+
area: parseFloat(polygonLine[vertices + 1]),
136+
hole: parseInt(polygonLine[vertices + 2]) === 1,
137+
properties: propertiesArray,
138+
triangulation: triangulation
139+
});
140+
}
141+
}
142+
this.close();
143+
this.$emit("loadtxtpacking", {
144+
polygons: polygons,
145+
height: height,
146+
width: width
147+
});
148+
} catch (e) {
149+
alert("Cannot load file (wrong format)");
150+
}
54151
});
55152
} else {
56153
this.$toast("File not supported");

0 commit comments

Comments
 (0)