forked from billhart/dxf2gcodecli
/
dxf2gcodecli.py
executable file
·484 lines (369 loc) · 18.3 KB
/
dxf2gcodecli.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
############################################################################
#
# Copyright (C) 2010-2015
# Christian Kohlöffel
# Jean-Paul Schouwstra
#
# This file is part of DXF2GCODE.
#
# DXF2GCODE 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 3 of the License, or
# (at your option) any later version.
#
# DXF2GCODE 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 DXF2GCODE. If not, see <http://www.gnu.org/licenses/>.
#
############################################################################
import os
import sys
from math import degrees, radians
import logging
logger = logging.getLogger()
from Core.Logger import LoggerClass
from copy import copy, deepcopy
import subprocess
import tempfile
import argparse
from Core.Config import MyConfig
from Core.Point import Point
from Core.LayerContent import LayerContentClass
from Core.EntityContent import EntityContentClass
import Core.Globals as g
import Core.constants as c
from Core.Shape import ShapeClass
from PostPro.PostProcessor import MyPostProcessor
from PostPro.Breaks import Breaks
from DxfImport.Import import ReadDXF
from PostPro.TspOptimisation import TSPoptimize
sys.setrecursionlimit(2500)
# Get folder of the main instance and write into globals
g.folder = os.path.dirname(os.path.abspath(sys.argv[0])).replace("\\", "/")
if os.path.islink(sys.argv[0]):
g.folder = os.path.dirname(os.readlink(sys.argv[0]))
# Create a class for our main window
class Main():
"""Main Class"""
def __init__(self):
"""
Initialization of the Main window. This is directly called after the
Logger has been initialized. The Function loads the GUI, creates the
used Classes and connects the actions to the GUI.
"""
self.MyPostProcessor = MyPostProcessor()
self.shapes = []
self.LayerContents = []
self.EntitiesRoot = []
self.filename = ""
def optimize_TSP(self):
"""
Method is called to optimize the order of the shapes. This is performed
by solving the TSP Problem.
"""
logger.debug(('Optimize order of enabled shapes per layer'))
#Get the export order from the QTreeView
logger.debug(('Updating order according to TreeView'))
for LayerContent in self.LayerContents:
#Initial values for the Lists to export.
self.shapes_to_write = []
self.shapes_fixed_order = []
shapes_st_en_points = []
#Check all shapes of Layer which shall be exported and create List
#for it.
logger.debug(("Nr. of Shapes %s; Nr. of Shapes in Route %s")
% (len(LayerContent.shapes),
len(LayerContent.exp_order)))
logger.debug(("Export Order for start: %s") % LayerContent.exp_order)
for shape in self.shapes:
self.shapes_to_write.append(shape.nr)
shapes_st_en_points.append(shape.get_st_en_points())
#Perform Export only if the Number of shapes to export is bigger than 0
if len(self.shapes_to_write)>0:
#Errechnen der Iterationen
#Calculate the iterations
iter_ = min(g.config.vars.Route_Optimisation['max_iterations'],
len(self.shapes_to_write)*50)
#Adding the Start and End Points to the List.
x_st = g.config.vars.Plane_Coordinates['axis1_start_end']
y_st = g.config.vars.Plane_Coordinates['axis2_start_end']
start = Point(x = x_st, y = y_st)
ende = Point(x = x_st, y = y_st)
shapes_st_en_points.append([start, ende])
TSPs = []
TSPs.append(TSPoptimize(st_end_points = shapes_st_en_points))
logger.info(("TSP start values initialised for Layer %s")
% LayerContent.LayerName)
logger.debug(("Shapes to write: %s")
% self.shapes_to_write)
logger.debug(("Fixed order: %s")
% self.shapes_fixed_order)
for it_nr in range(iter_):
#Only show each 50th step.
if (it_nr % 50) == 0:
TSPs[-1].calc_next_iteration()
new_exp_order = []
for nr in TSPs[-1].opt_route[1:len(TSPs[-1].opt_route)]:
new_exp_order.append(nr)
logger.debug(("TSP done with result: %s") % TSPs[-1])
LayerContent.exp_order = new_exp_order
logger.debug(("New Export Order after TSP: %s")
% new_exp_order)
else:
LayerContent.exp_order = []
def exportShapes(self, status=False, saveas=None):
"""
This function is called by the menu "Export/Export Shapes". It may open
a Save Dialog if used without LinuxCNC integration. Otherwise it's
possible to select multiple postprocessor files, which are located
in the folder.
"""
logger.debug(('Export the enabled shapes'))
#Get the export order from the QTreeView
logger.debug(("Sorted layers:"))
for i, layer in enumerate(self.LayerContents):
logger.debug("LayerContents[%i] = %s" % (i, layer))
if not(g.config.vars.General['write_to_stdout']):
#Get the name of the File to export
if saveas == None:
self.save_filename = str(filename[0].toUtf8()).decode("utf-8")
else:
filename = [None, None]
self.save_filename = saveas
(beg, ende) = os.path.split(self.save_filename)
(fileBaseName, fileExtension) = os.path.splitext(ende)
pp_file_nr = 0
for i in range(len(self.MyPostProcessor.output_format)):
name = "%s " % (self.MyPostProcessor.output_text[i])
format_ = "(*%s)" % (self.MyPostProcessor.output_format[i])
MyFormats = name + format_
if filename[1] == MyFormats:
pp_file_nr = i
if fileExtension != self.MyPostProcessor.output_format[pp_file_nr]:
self.save_filename = self.save_filename + self.MyPostProcessor.output_format[pp_file_nr]
self.MyPostProcessor.getPostProVars(pp_file_nr)
else:
self.save_filename = None
self.MyPostProcessor.getPostProVars(0)
"""
Export will be performed according to LayerContents and their order
is given in this variable too.
"""
self.MyPostProcessor.exportShapes(self.load_filename,
self.save_filename,
self.LayerContents)
if g.config.vars.General['write_to_stdout']:
self.close()
def loadFile(self, filename):
"""
Loads the file given by filename. Also calls the command to
make the plot.
@param filename: String containing filename which should be loaded
"""
self.load_filename = filename
(name, ext) = os.path.splitext(filename)
if (ext.lower() == ".ps") or (ext.lower() == ".pdf"):
logger.info(("Sending Postscript/PDF to pstoedit"))
#Create temporary file which will be read by the program
filename = os.path.join(tempfile.gettempdir(), 'dxf2gcode_temp.dxf')
pstoedit_cmd = g.config.vars.Filters['pstoedit_cmd'] #"C:\Program Files (x86)\pstoedit\pstoedit.exe"
pstoedit_opt = g.config.vars.Filters['pstoedit_opt'] #['-f','dxf','-mm']
ps_filename = os.path.normcase(self.load_filename)
cmd = [(('%s') % pstoedit_cmd)] + pstoedit_opt + [(('%s') % ps_filename), (('%s') % filename)]
logger.debug(cmd)
retcode = subprocess.call(cmd)
#self.textbox.text.delete(7.0, END)
logger.info(('Loading file: %s') % filename)
#logger.info("<a href=file:%s>%s</a>" % (filename, filename))
values = ReadDXF(filename)
#Output the information in the text window
logger.info(('Loaded layers: %s') % len(values.layers))
logger.info(('Loaded blocks: %s') % len(values.blocks.Entities))
for i in range(len(values.blocks.Entities)):
layers = values.blocks.Entities[i].get_used_layers()
logger.info(('Block %i includes %i Geometries, reduced to %i Contours, used layers: %s')\
% (i, len(values.blocks.Entities[i].geo), len(values.blocks.Entities[i].cont), layers))
layers = values.entities.get_used_layers()
insert_nr = values.entities.get_insert_nr()
logger.info(('Loaded %i Entities geometries, reduced to %i Contours, used layers: %s, Number of inserts: %i') \
% (len(values.entities.geo), len(values.entities.cont), layers, insert_nr))
self.makeShapesAndPlot(values)
def makeShapesAndPlot(self, values):
"""
Plots all data stored in the values parameter to the Canvas
@param values: Includes all values loaded from the dxf file
"""
#Generate the Shapes
self.makeShapes(values,
p0 = Point(x = self.cont_dx, y = self.cont_dy),
pb = Point(x = 0.0, y = 0.0),
sca = [self.cont_scale, self.cont_scale, self.cont_scale],
rot = self.rotate)
# Break insertion
# Breaks(self.LayerContents).process()
def makeShapes(self, values, p0, pb, sca, rot):
"""
Instance is called by the Main Window after the defined file is loaded.
It generates all ploting functionality. The parameters are generally
used to scale or offset the base geometry (by Menu in GUI).
@param values: The loaded dxf values from the dxf_import.py file
@param p0: The Starting Point to plot (Default x=0 and y=0)
@param bp: The Base Point to insert the geometry and base for rotation
(Default is also x=0 and y=0)
@param sca: The scale of the basis function (default =1)
@param rot: The rotation of the geometries around base (default =0)
"""
self.values = values
#Put back the contours
del(self.shapes[:])
del(self.LayerContents[:])
del(self.EntitiesRoot)
self.EntitiesRoot = EntityContentClass(Nr = 0, Name = 'Entities',
parent = None, children = [],
p0 = p0, pb = pb,
sca = sca, rot = rot)
#Start mit () bedeutet zuweisen der Entities -1 = Standard
#Start with () means to assign the entities -1 = Default ???
self.makeEntitiesShapes(parent = self.EntitiesRoot)
self.LayerContents.sort()
def makeEntitiesShapes(self, parent = None, ent_nr = -1, layerNr=-1):
"""
Instance is called prior to plotting the shapes. It creates
all shape classes which are later plotted into the graphics.
@param parent: The parent of a shape is always an Entities. It may be root
or, if it is a Block, this is the Block.
@param ent_nr: The values given in self.values are sorted so
that 0 is the Root Entities and 1 is beginning with the first block.
This value gives the index of self.values to be used.
"""
if parent.Name == "Entities":
entities = self.values.entities
else:
ent_nr = self.values.Get_Block_Nr(parent.Name)
entities = self.values.blocks.Entities[ent_nr]
#Zuweisen der Geometrien in die Variable geos & Konturen in cont
#Assigning the geometries in the variables geos & contours in cont
ent_geos = entities.geo
#Loop for the number of contours
for cont in entities.cont:
#Abfrage falls es sich bei der Kontur um ein Insert eines Blocks handelt
#Query if it is in the contour of an insert of a block
if ent_geos[cont.order[0][0]].Typ == "Insert":
ent_geo = ent_geos[cont.order[0][0]]
#Zuweisen des Basispunkts f�r den Block
#Assign the base point for the block
new_ent_nr = self.values.Get_Block_Nr(ent_geo.BlockName)
new_entities = self.values.blocks.Entities[new_ent_nr]
pb = new_entities.basep
#Skalierung usw. des Blocks zuweisen
#Scaling, etc. assign the block
p0 = ent_geos[cont.order[0][0]].Point
sca = ent_geos[cont.order[0][0]].Scale
rot = ent_geos[cont.order[0][0]].rot
#Erstellen des neuen Entitie Contents f�r das Insert
#Creating the new Entitie Contents for the insert
NewEntitieContent = EntityContentClass(Nr = 0,
Name = ent_geo.BlockName,
parent = parent, children = [],
p0 = p0,
pb = pb,
sca = sca,
rot = rot)
parent.addchild(NewEntitieContent)
self.makeEntitiesShapes(parent = NewEntitieContent,
ent_nr = ent_nr,
layerNr = ent_geo.Layer_Nr)
else:
self.shapes.append(ShapeClass(len(self.shapes),
cont.closed,
40,
0.0,
parent,
[]))
#Loop for the number of geometries
for ent_geo_nr in range(len(cont.order)):
ent_geo = ent_geos[cont.order[ent_geo_nr][0]]
if cont.order[ent_geo_nr][1]:
ent_geo.geo.reverse()
for geo in ent_geo.geo:
geo = copy(geo)
geo.reverse()
self.appendshapes(geo)
ent_geo.geo.reverse()
else:
for geo in ent_geo.geo:
self.appendshapes(copy(geo))
#All shapes have to be CW direction.
self.shapes[-1].AnalyseAndOptimize()
self.shapes[-1].FindNearestStPoint()
self.addtoLayerContents(self.shapes[-1], layerNr if layerNr != -1 else ent_geo.Layer_Nr)
parent.addchild(self.shapes[-1])
def appendshapes(self, geo):
"""
Documentation required
"""
self.shapes[-1].geos.append(geo)
if g.config.machine_type == 'drag_knife' and geo.type == 'HoleGeo':
self.shapes[-1].disabled = True
self.shapes[-1].allowedToChange = False
def addtoLayerContents(self, shape, lay_nr):
"""
Instance is called while the shapes are created. This gives the
structure which shape is laying on which layer. It also writes into the
shape the reference to the LayerContent Class.
@param shape: The shape to be appended of the shape
@param lay_nr: The Nr. of the layer
"""
#Check if the layer already exists and add shape if it is.
for LayCon in self.LayerContents:
if LayCon.LayerNr == lay_nr:
LayCon.shapes.append(shape)
return
#If the Layer does not exist create a new one.
LayerName = self.values.layers[lay_nr].name
self.LayerContents.append(LayerContentClass(lay_nr, LayerName, [shape]))
def closeEvent(self, e):
logger.debug(("exiting"))
self.writeSettings()
e.accept()
if __name__ == "__main__":
"""
The main function which is executed after program start.
"""
Log=LoggerClass(logger)
#Get local language and install if available.
g.config = MyConfig()
Log.set_console_handler_loglevel()
Log.add_file_logger()
window = Main()
g.window = window
parser = argparse.ArgumentParser()
parser.add_argument("filename",nargs="?")
# parser.add_argument("-f", "--file", dest = "filename",
# help = "read data from FILENAME")
parser.add_argument("-e", "--export", dest = "export_filename",
help = "export data to FILENAME")
parser.add_argument("-q", "--quiet", action = "store_true",
dest = "quiet", help = "no GUI")
# parser.add_option("-v", "--verbose",
# action = "store_true", dest = "verbose")
options = parser.parse_args()
#(options, args) = parser.parse_args()
logger.debug("Started with following options \n%s" % (parser))
if not(options.filename is None):
window.filename = options.filename.decode("cp1252")
#Initialize the scale, rotate and move coordinates
window.cont_scale = 1.0
window.cont_dx = 0.0
window.cont_dy = 0.0
window.rotate = 0.0
window.loadFile(window.filename)
if not(options.export_filename is None):
window.optimize_TSP()
window.exportShapes(None, options.export_filename)