==dxf import script ==
script for DXF import by kitsu (Ed Blake) - provide superior import to our native DXF import for many files
This commit is contained in:
282
release/scripts/bpymodules/dxfColorMap.py
Normal file
282
release/scripts/bpymodules/dxfColorMap.py
Normal file
@@ -0,0 +1,282 @@
|
||||
# dictionary mapping AutoCAD color indexes with Blender colors
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# color_map.py Final by Ed Blake (AKA Kitsu)
|
||||
# --------------------------------------------------------------------------
|
||||
# ***** BEGIN GPL LICENSE BLOCK *****
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation,
|
||||
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
# ***** END GPL LICENCE BLOCK *****
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
color_map = {
|
||||
0:[0.0, 0.0, 0.0],
|
||||
1:[0.99609375, 0.0, 0.0],
|
||||
2:[0.99609375, 0.99609375, 0.0],
|
||||
3:[0.0, 0.99609375, 0.0],
|
||||
4:[0.0, 0.99609375, 0.99609375],
|
||||
5:[0.0, 0.0, 0.99609375],
|
||||
6:[0.99609375, 0.0, 0.99609375],
|
||||
7:[0.99609375, 0.99609375, 0.99609375],
|
||||
8:[0.25390625, 0.25390625, 0.25390625],
|
||||
9:[0.5, 0.5, 0.5],
|
||||
10:[0.99609375, 0.0, 0.0],
|
||||
11:[0.99609375, 0.6640625, 0.6640625],
|
||||
12:[0.73828125, 0.0, 0.0],
|
||||
13:[0.73828125, 0.4921875, 0.4921875],
|
||||
14:[0.50390625, 0.0, 0.0],
|
||||
15:[0.50390625, 0.3359375, 0.3359375],
|
||||
16:[0.40625, 0.0, 0.0],
|
||||
17:[0.40625, 0.26953125, 0.26953125],
|
||||
18:[0.30859375, 0.0, 0.0],
|
||||
19:[0.30859375, 0.20703125, 0.20703125],
|
||||
20:[0.99609375, 0.24609375, 0.0],
|
||||
21:[0.99609375, 0.74609375, 0.6640625],
|
||||
22:[0.73828125, 0.1796875, 0.0],
|
||||
23:[0.73828125, 0.55078125, 0.4921875],
|
||||
24:[0.50390625, 0.12109375, 0.0],
|
||||
25:[0.50390625, 0.375, 0.3359375],
|
||||
26:[0.40625, 0.09765625, 0.0],
|
||||
27:[0.40625, 0.3046875, 0.26953125],
|
||||
28:[0.30859375, 0.07421875, 0.0],
|
||||
29:[0.30859375, 0.23046875, 0.20703125],
|
||||
30:[0.99609375, 0.49609375, 0.0],
|
||||
31:[0.99609375, 0.828125, 0.6640625],
|
||||
32:[0.73828125, 0.3671875, 0.0],
|
||||
33:[0.73828125, 0.61328125, 0.4921875],
|
||||
34:[0.50390625, 0.25, 0.0],
|
||||
35:[0.50390625, 0.41796875, 0.3359375],
|
||||
36:[0.40625, 0.203125, 0.0],
|
||||
37:[0.40625, 0.3359375, 0.26953125],
|
||||
38:[0.30859375, 0.15234375, 0.0],
|
||||
39:[0.30859375, 0.2578125, 0.20703125],
|
||||
40:[0.99609375, 0.74609375, 0.0],
|
||||
41:[0.99609375, 0.9140625, 0.6640625],
|
||||
42:[0.73828125, 0.55078125, 0.0],
|
||||
43:[0.73828125, 0.67578125, 0.4921875],
|
||||
44:[0.50390625, 0.375, 0.0],
|
||||
45:[0.50390625, 0.4609375, 0.3359375],
|
||||
46:[0.40625, 0.3046875, 0.0],
|
||||
47:[0.40625, 0.37109375, 0.26953125],
|
||||
48:[0.30859375, 0.23046875, 0.0],
|
||||
49:[0.30859375, 0.28515625, 0.20703125],
|
||||
50:[0.99609375, 0.99609375, 0.0],
|
||||
51:[0.99609375, 0.99609375, 0.6640625],
|
||||
52:[0.73828125, 0.73828125, 0.0],
|
||||
53:[0.73828125, 0.73828125, 0.4921875],
|
||||
54:[0.50390625, 0.50390625, 0.0],
|
||||
55:[0.50390625, 0.50390625, 0.3359375],
|
||||
56:[0.40625, 0.40625, 0.0],
|
||||
57:[0.40625, 0.40625, 0.26953125],
|
||||
58:[0.30859375, 0.30859375, 0.0],
|
||||
59:[0.30859375, 0.30859375, 0.20703125],
|
||||
60:[0.74609375, 0.99609375, 0.0],
|
||||
61:[0.9140625, 0.99609375, 0.6640625],
|
||||
62:[0.55078125, 0.73828125, 0.0],
|
||||
63:[0.67578125, 0.73828125, 0.4921875],
|
||||
64:[0.375, 0.50390625, 0.0],
|
||||
65:[0.4609375, 0.50390625, 0.3359375],
|
||||
66:[0.3046875, 0.40625, 0.0],
|
||||
67:[0.37109375, 0.40625, 0.26953125],
|
||||
68:[0.23046875, 0.30859375, 0.0],
|
||||
69:[0.28515625, 0.30859375, 0.20703125],
|
||||
70:[0.49609375, 0.99609375, 0.0],
|
||||
71:[0.828125, 0.99609375, 0.6640625],
|
||||
72:[0.3671875, 0.73828125, 0.0],
|
||||
73:[0.61328125, 0.73828125, 0.4921875],
|
||||
74:[0.25, 0.50390625, 0.0],
|
||||
75:[0.41796875, 0.50390625, 0.3359375],
|
||||
76:[0.203125, 0.40625, 0.0],
|
||||
77:[0.3359375, 0.40625, 0.26953125],
|
||||
78:[0.15234375, 0.30859375, 0.0],
|
||||
79:[0.2578125, 0.30859375, 0.20703125],
|
||||
80:[0.24609375, 0.99609375, 0.0],
|
||||
81:[0.74609375, 0.99609375, 0.6640625],
|
||||
82:[0.1796875, 0.73828125, 0.0],
|
||||
83:[0.55078125, 0.73828125, 0.4921875],
|
||||
84:[0.12109375, 0.50390625, 0.0],
|
||||
85:[0.375, 0.50390625, 0.3359375],
|
||||
86:[0.09765625, 0.40625, 0.0],
|
||||
87:[0.3046875, 0.40625, 0.26953125],
|
||||
88:[0.07421875, 0.30859375, 0.0],
|
||||
89:[0.23046875, 0.30859375, 0.20703125],
|
||||
90:[0.0, 0.99609375, 0.0],
|
||||
91:[0.6640625, 0.99609375, 0.6640625],
|
||||
92:[0.0, 0.73828125, 0.0],
|
||||
93:[0.4921875, 0.73828125, 0.4921875],
|
||||
94:[0.0, 0.50390625, 0.0],
|
||||
95:[0.3359375, 0.50390625, 0.3359375],
|
||||
96:[0.0, 0.40625, 0.0],
|
||||
97:[0.26953125, 0.40625, 0.26953125],
|
||||
98:[0.0, 0.30859375, 0.0],
|
||||
99:[0.20703125, 0.30859375, 0.20703125],
|
||||
100:[0.0, 0.99609375, 0.24609375],
|
||||
101:[0.6640625, 0.99609375, 0.74609375],
|
||||
102:[0.0, 0.73828125, 0.1796875],
|
||||
103:[0.4921875, 0.73828125, 0.55078125],
|
||||
104:[0.0, 0.50390625, 0.12109375],
|
||||
105:[0.3359375, 0.50390625, 0.375],
|
||||
106:[0.0, 0.40625, 0.09765625],
|
||||
107:[0.26953125, 0.40625, 0.3046875],
|
||||
108:[0.0, 0.30859375, 0.07421875],
|
||||
109:[0.20703125, 0.30859375, 0.23046875],
|
||||
110:[0.0, 0.99609375, 0.49609375],
|
||||
111:[0.6640625, 0.99609375, 0.828125],
|
||||
112:[0.0, 0.73828125, 0.3671875],
|
||||
113:[0.4921875, 0.73828125, 0.61328125],
|
||||
114:[0.0, 0.50390625, 0.25],
|
||||
115:[0.3359375, 0.50390625, 0.41796875],
|
||||
116:[0.0, 0.40625, 0.203125],
|
||||
117:[0.26953125, 0.40625, 0.3359375],
|
||||
118:[0.0, 0.30859375, 0.15234375],
|
||||
119:[0.20703125, 0.30859375, 0.2578125],
|
||||
120:[0.0, 0.99609375, 0.74609375],
|
||||
121:[0.6640625, 0.99609375, 0.9140625],
|
||||
122:[0.0, 0.73828125, 0.55078125],
|
||||
123:[0.4921875, 0.73828125, 0.67578125],
|
||||
124:[0.0, 0.50390625, 0.375],
|
||||
125:[0.3359375, 0.50390625, 0.4609375],
|
||||
126:[0.0, 0.40625, 0.3046875],
|
||||
127:[0.26953125, 0.40625, 0.37109375],
|
||||
128:[0.0, 0.30859375, 0.23046875],
|
||||
129:[0.20703125, 0.30859375, 0.28515625],
|
||||
130:[0.0, 0.99609375, 0.99609375],
|
||||
131:[0.6640625, 0.99609375, 0.99609375],
|
||||
132:[0.0, 0.73828125, 0.73828125],
|
||||
133:[0.4921875, 0.73828125, 0.73828125],
|
||||
134:[0.0, 0.50390625, 0.50390625],
|
||||
135:[0.3359375, 0.50390625, 0.50390625],
|
||||
136:[0.0, 0.40625, 0.40625],
|
||||
137:[0.26953125, 0.40625, 0.40625],
|
||||
138:[0.0, 0.30859375, 0.30859375],
|
||||
139:[0.20703125, 0.30859375, 0.30859375],
|
||||
140:[0.0, 0.74609375, 0.99609375],
|
||||
141:[0.6640625, 0.9140625, 0.99609375],
|
||||
142:[0.0, 0.55078125, 0.73828125],
|
||||
143:[0.4921875, 0.67578125, 0.73828125],
|
||||
144:[0.0, 0.375, 0.50390625],
|
||||
145:[0.3359375, 0.4609375, 0.50390625],
|
||||
146:[0.0, 0.3046875, 0.40625],
|
||||
147:[0.26953125, 0.37109375, 0.40625],
|
||||
148:[0.0, 0.23046875, 0.30859375],
|
||||
149:[0.20703125, 0.28515625, 0.30859375],
|
||||
150:[0.0, 0.49609375, 0.99609375],
|
||||
151:[0.6640625, 0.828125, 0.99609375],
|
||||
152:[0.0, 0.3671875, 0.73828125],
|
||||
153:[0.4921875, 0.61328125, 0.73828125],
|
||||
154:[0.0, 0.25, 0.50390625],
|
||||
155:[0.3359375, 0.41796875, 0.50390625],
|
||||
156:[0.0, 0.203125, 0.40625],
|
||||
157:[0.26953125, 0.3359375, 0.40625],
|
||||
158:[0.0, 0.15234375, 0.30859375],
|
||||
159:[0.20703125, 0.2578125, 0.30859375],
|
||||
160:[0.0, 0.24609375, 0.99609375],
|
||||
161:[0.6640625, 0.74609375, 0.99609375],
|
||||
162:[0.0, 0.1796875, 0.73828125],
|
||||
163:[0.4921875, 0.55078125, 0.73828125],
|
||||
164:[0.0, 0.12109375, 0.50390625],
|
||||
165:[0.3359375, 0.375, 0.50390625],
|
||||
166:[0.0, 0.09765625, 0.40625],
|
||||
167:[0.26953125, 0.3046875, 0.40625],
|
||||
168:[0.0, 0.07421875, 0.30859375],
|
||||
169:[0.20703125, 0.23046875, 0.30859375],
|
||||
170:[0.0, 0.0, 0.99609375],
|
||||
171:[0.6640625, 0.6640625, 0.99609375],
|
||||
172:[0.0, 0.0, 0.73828125],
|
||||
173:[0.4921875, 0.4921875, 0.73828125],
|
||||
174:[0.0, 0.0, 0.50390625],
|
||||
175:[0.3359375, 0.3359375, 0.50390625],
|
||||
176:[0.0, 0.0, 0.40625],
|
||||
177:[0.26953125, 0.26953125, 0.40625],
|
||||
178:[0.0, 0.0, 0.30859375],
|
||||
179:[0.20703125, 0.20703125, 0.30859375],
|
||||
180:[0.24609375, 0.0, 0.99609375],
|
||||
181:[0.74609375, 0.6640625, 0.99609375],
|
||||
182:[0.1796875, 0.0, 0.73828125],
|
||||
183:[0.55078125, 0.4921875, 0.73828125],
|
||||
184:[0.12109375, 0.0, 0.50390625],
|
||||
185:[0.375, 0.3359375, 0.50390625],
|
||||
186:[0.09765625, 0.0, 0.40625],
|
||||
187:[0.3046875, 0.26953125, 0.40625],
|
||||
188:[0.07421875, 0.0, 0.30859375],
|
||||
189:[0.23046875, 0.20703125, 0.30859375],
|
||||
190:[0.49609375, 0.0, 0.99609375],
|
||||
191:[0.828125, 0.6640625, 0.99609375],
|
||||
192:[0.3671875, 0.0, 0.73828125],
|
||||
193:[0.61328125, 0.4921875, 0.73828125],
|
||||
194:[0.25, 0.0, 0.50390625],
|
||||
195:[0.41796875, 0.3359375, 0.50390625],
|
||||
196:[0.203125, 0.0, 0.40625],
|
||||
197:[0.3359375, 0.26953125, 0.40625],
|
||||
198:[0.15234375, 0.0, 0.30859375],
|
||||
199:[0.2578125, 0.20703125, 0.30859375],
|
||||
200:[0.74609375, 0.0, 0.99609375],
|
||||
201:[0.9140625, 0.6640625, 0.99609375],
|
||||
202:[0.55078125, 0.0, 0.73828125],
|
||||
203:[0.67578125, 0.4921875, 0.73828125],
|
||||
204:[0.375, 0.0, 0.50390625],
|
||||
205:[0.4609375, 0.3359375, 0.50390625],
|
||||
206:[0.3046875, 0.0, 0.40625],
|
||||
207:[0.37109375, 0.26953125, 0.40625],
|
||||
208:[0.23046875, 0.0, 0.30859375],
|
||||
209:[0.28515625, 0.20703125, 0.30859375],
|
||||
210:[0.99609375, 0.0, 0.99609375],
|
||||
211:[0.99609375, 0.6640625, 0.99609375],
|
||||
212:[0.73828125, 0.0, 0.73828125],
|
||||
213:[0.73828125, 0.4921875, 0.73828125],
|
||||
214:[0.50390625, 0.0, 0.50390625],
|
||||
215:[0.50390625, 0.3359375, 0.50390625],
|
||||
216:[0.40625, 0.0, 0.40625],
|
||||
217:[0.40625, 0.26953125, 0.40625],
|
||||
218:[0.30859375, 0.0, 0.30859375],
|
||||
219:[0.30859375, 0.20703125, 0.30859375],
|
||||
220:[0.99609375, 0.0, 0.74609375],
|
||||
221:[0.99609375, 0.6640625, 0.9140625],
|
||||
222:[0.73828125, 0.0, 0.55078125],
|
||||
223:[0.73828125, 0.4921875, 0.67578125],
|
||||
224:[0.50390625, 0.0, 0.375],
|
||||
225:[0.50390625, 0.3359375, 0.4609375],
|
||||
226:[0.40625, 0.0, 0.3046875],
|
||||
227:[0.40625, 0.26953125, 0.37109375],
|
||||
228:[0.30859375, 0.0, 0.23046875],
|
||||
229:[0.30859375, 0.20703125, 0.28515625],
|
||||
230:[0.99609375, 0.0, 0.49609375],
|
||||
231:[0.99609375, 0.6640625, 0.828125],
|
||||
232:[0.73828125, 0.0, 0.3671875],
|
||||
233:[0.73828125, 0.4921875, 0.61328125],
|
||||
234:[0.50390625, 0.0, 0.25],
|
||||
235:[0.50390625, 0.3359375, 0.41796875],
|
||||
236:[0.40625, 0.0, 0.203125],
|
||||
237:[0.40625, 0.26953125, 0.3359375],
|
||||
238:[0.30859375, 0.0, 0.15234375],
|
||||
239:[0.30859375, 0.20703125, 0.2578125],
|
||||
240:[0.99609375, 0.0, 0.24609375],
|
||||
241:[0.99609375, 0.6640625, 0.74609375],
|
||||
242:[0.73828125, 0.0, 0.1796875],
|
||||
243:[0.73828125, 0.4921875, 0.55078125],
|
||||
244:[0.50390625, 0.0, 0.12109375],
|
||||
245:[0.50390625, 0.3359375, 0.375],
|
||||
246:[0.40625, 0.0, 0.09765625],
|
||||
247:[0.40625, 0.26953125, 0.3046875],
|
||||
248:[0.30859375, 0.0, 0.07421875],
|
||||
249:[0.30859375, 0.20703125, 0.23046875],
|
||||
250:[0.19921875, 0.19921875, 0.19921875],
|
||||
251:[0.3125, 0.3125, 0.3125],
|
||||
252:[0.41015625, 0.41015625, 0.41015625],
|
||||
253:[0.5078125, 0.5078125, 0.5078125],
|
||||
254:[0.7421875, 0.7421875, 0.7421875],
|
||||
255:[0.99609375, 0.99609375, 0.99609375],
|
||||
}
|
||||
1326
release/scripts/bpymodules/dxfImportObjects.py
Normal file
1326
release/scripts/bpymodules/dxfImportObjects.py
Normal file
File diff suppressed because it is too large
Load Diff
323
release/scripts/bpymodules/dxfReader.py
Normal file
323
release/scripts/bpymodules/dxfReader.py
Normal file
@@ -0,0 +1,323 @@
|
||||
"""This module provides a function for reading dxf files and parsing them into a useful tree of objects and data.
|
||||
|
||||
The convert function is called by the readDXF fuction to convert dxf strings into the correct data based
|
||||
on their type code. readDXF expects a (full path) file name as input.
|
||||
"""
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# DXF Reader v0.8 by Ed Blake (AKA Kitsu)
|
||||
# --------------------------------------------------------------------------
|
||||
# ***** BEGIN GPL LICENSE BLOCK *****
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation,
|
||||
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
# ***** END GPL LICENCE BLOCK *****
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
|
||||
from dxfImportObjects import *
|
||||
|
||||
class InitializationError(Exception): pass
|
||||
|
||||
class StateMachine:
|
||||
"""(finite) State Machine from the great David Mertz's great Charming Python article."""
|
||||
|
||||
def __init__(self):
|
||||
self.handlers = []
|
||||
self.startState = None
|
||||
self.endStates = []
|
||||
|
||||
def add_state(self, handler, end_state=0):
|
||||
"""All states and handlers are functions which return
|
||||
a state and a cargo."""
|
||||
self.handlers.append(handler)
|
||||
if end_state:
|
||||
self.endStates.append(handler)
|
||||
def set_start(self, handler):
|
||||
"""Sets the starting handler function."""
|
||||
self.startState = handler
|
||||
|
||||
|
||||
def run(self, cargo=None):
|
||||
if not self.startState:
|
||||
raise InitializationError,\
|
||||
"must call .set_start() before .run()"
|
||||
if not self.endStates:
|
||||
raise InitializationError, \
|
||||
"at least one state must be an end_state"
|
||||
handler = self.startState
|
||||
while 1:
|
||||
(newState, cargo) = handler(cargo)
|
||||
#print cargo
|
||||
if newState in self.endStates:
|
||||
return newState(cargo)
|
||||
#break
|
||||
elif newState not in self.handlers:
|
||||
raise RuntimeError, "Invalid target %s" % newState
|
||||
else:
|
||||
handler = newState
|
||||
|
||||
def convert(code, value):
|
||||
"""Convert a string to the correct Python type based on its dxf code.
|
||||
code types:
|
||||
ints = 60-79, 170-179, 270-289, 370-389, 400-409, 1060-1070
|
||||
longs = 90-99, 420-429, 440-459, 1071
|
||||
floats = 10-39, 40-59, 110-139, 140-149, 210-239, 460-469, 1010-1059
|
||||
hex = 105, 310-379, 390-399
|
||||
strings = 0-9, 100, 102, 300-309, 410-419, 430-439, 470-479, 999, 1000-1009
|
||||
"""
|
||||
if 59 < code < 80 or 169 < code < 180 or 269 < code < 290 or 369 < code < 390 or 399 < code < 410 or 1059 < code < 1071:
|
||||
value = int(value)
|
||||
elif 89 < code < 100 or 419 < code < 430 or 439 < code < 460 or code == 1071:
|
||||
value = long(value)
|
||||
elif 9 < code < 60 or 109 < code < 150 or 209 < code < 240 or 459 < code < 470 or 1009 < code < 1060:
|
||||
value = float(value)
|
||||
elif code == 105 or 309 < code < 380 or 389 < code < 400:
|
||||
value = int(value, 16) # should be left as string?
|
||||
else: # it's already a string so do nothing
|
||||
pass
|
||||
return value
|
||||
|
||||
|
||||
def findObject(infile, kind=''):
|
||||
"""Finds the next occurance of an object."""
|
||||
obj = False
|
||||
while 1:
|
||||
line = infile.readline()
|
||||
if not line: # readline returns '' at eof
|
||||
return False
|
||||
if not obj: # We're still looking for our object code
|
||||
if line.lower().strip() == '0':
|
||||
obj = True # found it
|
||||
else: # we are in an object definition
|
||||
if kind: # if we're looking for a particular kind
|
||||
if line.lower().strip() == kind:
|
||||
obj = Object(line.lower().strip())
|
||||
break
|
||||
else: # otherwise take anything non-numeric
|
||||
if line.lower().strip() not in string.digits:
|
||||
obj = Object(line.lower().strip())
|
||||
break
|
||||
obj = False # whether we found one or not it's time to start over
|
||||
return obj
|
||||
|
||||
def handleObject(infile):
|
||||
"""Add data to an object until end of object is found."""
|
||||
line = infile.readline()
|
||||
if line.lower().strip() == 'section':
|
||||
return 'section' # this would be a problem
|
||||
elif line.lower().strip() == 'endsec':
|
||||
return 'endsec' # this means we are done with a section
|
||||
else: # add data to the object until we find a new object
|
||||
obj = Object(line.lower().strip())
|
||||
obj.name = obj.type
|
||||
done = False
|
||||
data = []
|
||||
while not done:
|
||||
line = infile.readline()
|
||||
if not data:
|
||||
if line.lower().strip() == '0':
|
||||
#we've found an object, time to return
|
||||
return obj
|
||||
else:
|
||||
# first part is always an int
|
||||
data.append(int(line.lower().strip()))
|
||||
else:
|
||||
data.append(convert(data[0], line.strip()))
|
||||
obj.data.append(data)
|
||||
data = []
|
||||
|
||||
def handleTable(table, infile):
|
||||
"""Special handler for dealing with nested table objects."""
|
||||
item, name = get_name(table.data)
|
||||
if name: # We should always find a name
|
||||
table.data.remove(item)
|
||||
table.name = name.lower()
|
||||
# This next bit is from handleObject
|
||||
# handleObject should be generalized to work with any section like object
|
||||
while 1:
|
||||
obj = handleObject(infile)
|
||||
if obj.type == 'table':
|
||||
print "Warning: previous table not closed!"
|
||||
return table
|
||||
elif obj.type == 'endtab':
|
||||
return table # this means we are done with the table
|
||||
else: # add objects to the table until one of the above is found
|
||||
table.data.append(obj)
|
||||
|
||||
|
||||
|
||||
|
||||
def handleBlock(block, infile):
|
||||
"""Special handler for dealing with nested table objects."""
|
||||
item, name = get_name(block.data)
|
||||
if name: # We should always find a name
|
||||
block.data.remove(item)
|
||||
block.name = name.lower()
|
||||
# This next bit is from handleObject
|
||||
# handleObject should be generalized to work with any section like object
|
||||
while 1:
|
||||
obj = handleObject(infile)
|
||||
if obj.type == 'block':
|
||||
print "Warning: previous block not closed!"
|
||||
return block
|
||||
elif obj.type == 'endblk':
|
||||
return block # this means we are done with the table
|
||||
else: # add objects to the table until one of the above is found
|
||||
block.data.append(obj)
|
||||
|
||||
|
||||
|
||||
|
||||
"""These are the states/functions used in the State Machine.
|
||||
states:
|
||||
start - find first section
|
||||
start_section - add data, find first object
|
||||
object - add obj-data, watch for next obj (called directly by start_section)
|
||||
end_section - look for next section or eof
|
||||
end - return results
|
||||
"""
|
||||
|
||||
def start(cargo):
|
||||
"""Expects the infile as cargo, initializes the cargo."""
|
||||
#print "Entering start state!"
|
||||
infile = cargo
|
||||
drawing = Object('drawing')
|
||||
section = findObject(infile, 'section')
|
||||
if section:
|
||||
return start_section, (infile, drawing, section)
|
||||
else:
|
||||
return error, (infile, "Failed to find any sections!")
|
||||
|
||||
def start_section(cargo):
|
||||
"""Expects [infile, drawing, section] as cargo, builds a nested section object."""
|
||||
#print "Entering start_section state!"
|
||||
infile = cargo[0]
|
||||
drawing = cargo[1]
|
||||
section = cargo[2]
|
||||
# read each line, if it is an object declaration go to object mode
|
||||
# otherwise create a [index, data] pair and add it to the sections data.
|
||||
done = False
|
||||
data = []
|
||||
while not done:
|
||||
line = infile.readline()
|
||||
|
||||
if not data: # if we haven't found a dxf code yet
|
||||
if line.lower().strip() == '0':
|
||||
# we've found an object
|
||||
while 1: # no way out unless we find an end section or a new section
|
||||
obj = handleObject(infile)
|
||||
if obj == 'section': # shouldn't happen
|
||||
print "Warning: failed to close previous section!"
|
||||
return end_section, (infile, drawing)
|
||||
elif obj == 'endsec': # This section is over, look for the next
|
||||
drawing.data.append(section)
|
||||
return end_section, (infile, drawing)
|
||||
elif obj.type == 'table': # tables are collections of data
|
||||
obj = handleTable(obj, infile) # we need to find all there contents
|
||||
section.data.append(obj) # before moving on
|
||||
elif obj.type == 'block': # the same is true of blocks
|
||||
obj = handleBlock(obj, infile) # we need to find all there contents
|
||||
section.data.append(obj) # before moving on
|
||||
else: # found another sub-object
|
||||
section.data.append(obj)
|
||||
else:
|
||||
data.append(int(line.lower().strip()))
|
||||
else: # we have our code, now we just need to convert the data and add it to our list.
|
||||
data.append(convert(data[0], line.strip()))
|
||||
section.data.append(data)
|
||||
data = []
|
||||
def end_section(cargo):
|
||||
"""Expects (infile, drawing) as cargo, searches for next section."""
|
||||
#print "Entering end_section state!"
|
||||
infile = cargo[0]
|
||||
drawing = cargo[1]
|
||||
section = findObject(infile, 'section')
|
||||
if section:
|
||||
return start_section, (infile, drawing, section)
|
||||
else:
|
||||
return end, (infile, drawing)
|
||||
|
||||
def end(cargo):
|
||||
"""Expects (infile, drawing) as cargo, called when eof has been reached."""
|
||||
#print "Entering end state!"
|
||||
infile = cargo[0]
|
||||
drawing = cargo[1]
|
||||
#infile.close()
|
||||
return drawing
|
||||
|
||||
def error(cargo):
|
||||
"""Expects a (infile, string) as cargo, called when there is an error during processing."""
|
||||
#print "Entering error state!"
|
||||
infile = cargo[0]
|
||||
err = cargo[1]
|
||||
infile.close()
|
||||
print "There has been an error:"
|
||||
print err
|
||||
return False
|
||||
|
||||
def readDXF(filename):
|
||||
"""Given a file name try to read it as a dxf file.
|
||||
|
||||
Output is an object with the following structure
|
||||
drawing
|
||||
header
|
||||
header data
|
||||
classes
|
||||
class data
|
||||
tables
|
||||
table data
|
||||
blocks
|
||||
block data
|
||||
entities
|
||||
entity data
|
||||
objects
|
||||
object data
|
||||
where foo data is a list of sub-objects. True object data
|
||||
is of the form [code, data].
|
||||
"""
|
||||
infile = open(filename)
|
||||
|
||||
sm = StateMachine()
|
||||
sm.add_state(error, True)
|
||||
sm.add_state(end, True)
|
||||
sm.add_state(start_section)
|
||||
sm.add_state(end_section)
|
||||
sm.add_state(start)
|
||||
sm.set_start(start)
|
||||
try:
|
||||
drawing = sm.run(infile)
|
||||
if drawing:
|
||||
drawing.name = filename
|
||||
for obj in drawing.data:
|
||||
item, name = get_name(obj.data)
|
||||
if name:
|
||||
obj.data.remove(item)
|
||||
obj.name = name.lower()
|
||||
setattr(drawing, name.lower(), obj)
|
||||
# Call the objectify function from dxfImportObjects to cast
|
||||
# raw objects into the right types of object
|
||||
obj.data = objectify(obj.data)
|
||||
#print obj.name
|
||||
finally:
|
||||
infile.close()
|
||||
return drawing
|
||||
if __name__ == "__main__":
|
||||
filename = r".\examples\block-test.dxf"
|
||||
drawing = readDXF(filename)
|
||||
for item in drawing.entities.data:
|
||||
print item
|
||||
|
||||
|
||||
1005
release/scripts/import_dxf.py
Normal file
1005
release/scripts/import_dxf.py
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user