This repository was archived by the owner on Jun 14, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathangular-dxf.js
More file actions
106 lines (103 loc) · 5.43 KB
/
Copy pathangular-dxf.js
File metadata and controls
106 lines (103 loc) · 5.43 KB
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
angular.module('ngDxf', [])
.service('ngDxf', function () {
function toJson(dxf) {
var json = {};
dxf = dxf.split(/\s*\r\n/g).map(function (item) {
if (!isNaN(parseFloat(item)))
item = parseFloat(item);
return item;
});
function dxfObj(parent) {
this.data = {};
this.depth = parent ? parent.depth + 1 : 0;
this.parent = parent;
this.goToParent = function () {
this.parent.data[this.parent.key] = this.data;
return this.parent;
};
}
function getNextState(state, data) { //for reaading the codes
state.code = data;
state.read = false;
return state;
}
function processData(state, data, index) {
if (state.code === 0) {
if (data === "SECTION") { //initialize our new section
state.obj = new dxfObj();
} else if (data === "APPID") {
state.obj = state.obj.goToParent();
} else if (data === "ENDSEC") { //end section
while (state.obj.parent) {
state.obj = state.obj.goToParent();
}
angular.merge(json, state.obj.data);
} else if (data === "POLYLINE") {
if (!(state.obj.data instanceof Array)) {
state.obj.data = [];
}
state.obj.key = data;
state.obj = new dxfObj(state.obj); //push a new dxfObj to the stack then set the current object to that object.
state.obj.data.vertices = [];
} else if (data === "VERTEX") {
if (state.obj.depth > 2) {
state.obj.parent.data.vertices.push(state.obj.data);
state.obj = state.obj.parent;
}
state.obj = new dxfObj(state.obj);
} else if (data === "SEQEND") { //Push the polyline to the entity array.
if (state.obj.depth > 2) {
state.obj.parent.data.vertices.push(state.obj.data);
state.obj = state.obj.parent;
}
if (state.obj.depth > 1) {
var temp = {};
temp[state.obj.parent.key] = state.obj.data;
state.obj.parent.data.push(temp);
state.obj = state.obj.parent;
}
} else if (data === "EOF") {
}
} else if (state.code === 1) {
state.obj.data = data;
} else if (state.code === 2) { //block name
state.obj.key = data; //sets the key that we want to modify
state.obj = new dxfObj(state.obj); //set the current object and saves a reference to the parent
} else if (state.code === 8) { //LAYER NAME
if (!(state.obj.data instanceof Array)) {
state.obj.data.name = data;
}
} else if (state.code === 9) {
if (state.obj.depth > 1) {
state.obj = state.obj.goToParent();
}
state.obj.key = data;
state.obj = new dxfObj(state.obj);
} else if (state.code === 10) {
state.obj.data.x = data;
} else if (state.code === 20) {
state.obj.data.y = data;
} else if (state.code === 30) {
state.obj.data.z = data;
} else if (state.code === 62) { //COLOR NUMBER
state.obj.data.color = data;
} else if (70 <= state.code && state.code <= 78) { //Integer values, such as repeat counts, flags bits, or modes
state.obj.data.flag = data;
}
state.read = true;
return state;
}
dxf.reduce(function (state, data, index) {
if (state.read)
return getNextState(state, data);
return processData(state, data, index);
}, {"read": true});
return json;
}
return {
'toJson': function (dxf) {
return toJson(dxf);
}
};
})
;