Read DXF in python

It seems that you can read DXF with python using dxfgrabber pip install dxfgrabber

Create an appropriate DXF file with free 2D CAD jw_cad and read it.

dxf.py


import dxfgrabber

dxf = dxfgrabber.readfile("test.dxf")
#header
print(dxf.header)
#{'$ACADVER': 'AC1009', '$DWGCODEPAGE': 'ANSI_1252', '$INSBASE': (0.0, 0.0, 0.0), '$EXTMIN': (0.0, 0.0), '$EXTMAX': (841.0, 594.0), '$LIMMIN': (0.0, 0.0), '$LIMMAX': (841.0, 594.0), '$LTSCALE': 1.0}

#Graphic elements
# print(vars(dxf.entities))
cirs = [e for e in dxf.entities if e.dxftype == 'CIRCLE']
lines = [e for e in dxf.entities if e.dxftype == 'LINE']

#Circle
print(vars(cirs[0]))
# {'dxftype': 'CIRCLE', 'handle': None, 'owner': None, 'paperspace': None, 'layer': '_0-0_', 'linetype': 'CONTINUOUS', 'thickness': 0.0, 'extrusion': (0.0, 0.0, 1.0), 'ltscale': 1.0, 'line_weight': 0, 'invisible': 0, 'color': 7, 'true_color': None, 'transparency': None, 'shadow_mode': None, 'layout_tab_name': None, 'center': (94.41901840490794, 356.85030674846627), 'radius': 16.151818630112004}
#center is the center of the circle x, y
#radius is the circular radius
#linetype is the line type

#Straight line
print(vars(lines[0]))
#{'dxftype': 'LINE', 'handle': None, 'owner': None, 'paperspace': None, 'layer': '_0-0_', 'linetype': 'CONTINUOUS', 'thickness': 0.0, 'extrusion': None, 'ltscale': 1.0, 'line_weight': 0, 'invisible': 0, 'color': 7, 'true_color': None, 'transparency': None, 'shadow_mode': None, 'layout_tab_name': None, 'start': (386.4472392638037, 316.28711656441715), 'end': (386.4472392638037, 522.9865030674847)}
#start is the starting point x,y, end is the end point x,y
#linetype is the line type

#Line type list(jw_cad)
print(vars(dxf.linetypes))
#{'_table_entries': {'CONTINUOUS': <dxfgrabber.linetypes.Linetype object at 0x000002B43A5796A0>, 'DASHED1': <dxfgrabber.linetypes.Linetype object at 0x000002B43A5796D8>, 'DASHED2': <dxfgrabber.linetypes.Linetype object at 0x000002B43A579710>, 'DASHED3': <dxfgrabber.linetypes.Linetype object at 0x000002B43A579748>, 'CENTER1': <dxfgrabber.linetypes.Linetype object at ...

reference https://qiita.com/ackermanrf128/items/d9275a7d077c1dff3ec7

Recommended Posts

Read DXF in python
Read Euler's formula in Python
Read Namespace-specified XML in Python
Read Outlook emails in Python
Read Fortran output in python
Read Protocol Buffers data in Python3
Read PNG chunks in Python (class)
Read files in parallel with Python
Create and read messagepacks in Python
Quadtree in Python --2
Python in optimization
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
[python] Read data
flatten in python
Read the file line by line in Python
Read the file line by line in Python
Read and write JSON files in Python
[Python] Read the specified line in the file
Read text in images with python OCR
Sorted list in Python
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Implement Enigma in python