Welcome folks today in this blog post we will be converting autocad drawings
to image files using dxfgrabber
library in python. All the full source code of application is given below.
Get Started
In order to get started you need to install the following libraries using the pip
command as shown below
pip install dxfgrabber
After installing this library make an app.py
file and copy paste the following code
app.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 |
import dxfgrabber import cv2 import numpy as np import sys """ def absdiff(num1, num2): if num1 <= num2: return num2 - num1 else: return num1 - num2 """ def getMinXY(shapes): minX, minY = 99999, 99999 for shape in shapes: if shape.dxftype == 'LINE': minX = min(minX, shape.start[0], shape.end[0]) minY = min(minY, shape.start[1], shape.end[1]) elif shape.dxftype == 'ARC': minX = min(minX, shape.center[0]) minY = min(minY, shape.center[1]) return minX, minY def getMaxXY(shapes): maxX, maxY = -99999, -99999 for shape in shapes: if shape.dxftype == 'LINE': maxX = max(maxX, shape.start[0], shape.end[0]) maxY = max(maxY, shape.start[1], shape.end[1]) elif shape.dxftype == 'ARC': maxX = max(maxX, shape.center[0]) maxY = max(maxY, shape.center[1]) return maxX, maxY #Translate x,y values as per fourth quadrant i.e positive x and negative y. Update y value to positive, to be used by opencv axes. # shift origin of image by 100 pixels for better clarity of output image def getXY(point): x, y = point[0], point[1] return int(x-minX+100), int(abs(y-maxY)+100) dxf = dxfgrabber.readfile(sys.argv[1]) shapes = dxf.entities.get_entities() minX, minY = getMinXY(shapes) maxX, maxY = getMaxXY(shapes) #baseX, baseY = absdiff(minX, 0), absdiff(minY, 0) #absMaxX, absMaxY = absdiff(maxX, 0), absdiff(maxY, 0) print(maxX, maxY) print(minX, minY) #print(absMaxX, absMaxY) #print(baseX, baseY) canvas = np.zeros((512,512,3), np.uint8) for shape in shapes: if shape.dxftype == 'LINE': x1, y1 = getXY(shape.start) x2, y2 = getXY(shape.end) canvas = cv2.line(canvas, (x1,y1), (x2,y2), (255, 0, 255), 1) elif shape.dxftype == 'ARC': centerX, centerY = getXY(shape.center) if (shape.start_angle > 180) and (shape.end_angle < shape.start_angle): canvas = cv2.ellipse(canvas, (centerX, centerY), (int(shape.radius), int(shape.radius)), 180, int(shape.start_angle) - 180, 180 + int(shape.end_angle), (0, 0, 255), 1) else: canvas = cv2.ellipse(canvas, (centerX, centerY), (int(shape.radius), int(shape.radius)), 0, int(360 - shape.start_angle), int(360 - shape.end_angle), (0, 0, 255), 1) cv2.imshow('test', canvas) cv2.waitKey(50) cv2.imwrite('res.png', canvas) cv2.waitKey(0) cv2.destroyAllWindows() |
Now if you run this python script app.py
by typing the below command
python app.py input.dwg
Here we are providing the input
file as a command line argument to the script