Create a 2d CAD file ".dxf" with python [ezdxf]

.dxf??

Various file formats are used in CAD (software for drawing blueprints of machines, etc.). ".Dxf" is one of them.

There are other file formats, but the commonly used ".dwg" can only be read by specific software such as AutoCAD.

On the other hand, since the specifications of ".dxf" are open to the public, it can be read and written not only by specific software but also by various software. (You can read the contents by opening it with a text editor.)

Therefore, it is a file format like the industry standard used for exchanging drawings between various companies (maybe ...).

ezdxf?? It is a library that creates and reads dxf files with python. https://ezdxf.readthedocs.io/en/stable/

For example, if you use AutoCAD (which is quite expensive), you can create a program of self-moving images using VBA. https://www.youtube.com/watch?v=cL7vAIFjsmQ If you have AutoCAD, this is often more convenient.

However There may be some demand for CAD auto-drawing using python, CAD auto-drawing even if you don't have AutoCAD ... (Maybe). "Ezdxf" can be used in such cases.

Installation is also easy with Anaconda! conda install -c conda-forge ezdxf

Try to draw a straight line

Use ezdxf to draw a straight line and save it (although the tutorial is the same). You can create a .dxf file with a statement of 5 lines.

#python 3.85
import ezdxf #'0.13.1'

#dxf version specification
doc = ezdxf.new("R2010")

#Create a new entity in model space
msp = doc.modelspace() 

#Create a straight line
msp.add_line(start=[0, 0], end=[100, 0])

#Save
doc.saveas('line.dxf')

When I open the created .dxf file, I see a straight line like this (lonely). line.png

By the way At first, I downloaded jwcad, a free CAD with a high penetration rate. I didn't know how to use it at all (I can't move the shapes!), So This CAD file was opened in Libre Office Draw.

If you get used to it, jwcad seems to be a wonderful CAD, but it seems that the operation method is different from other CAD, It seems that there are Hamar points when people who used other CAD use it as it is. (Since studying jwcad is troublesome, I cut corners)

There seems to be other free CAD, so For those who usually use it for paid CAD work to play at home, You may want to try another CAD.

If it is one line, it will be awkward, so I will increase the number of lines a little more and make a quadrangle.

import ezdxf #'0.13.1'
doc = ezdxf.new("R2010")  #dxf version specification

msp = doc.modelspace() #add new entities to the modelspace
msp.add_line([0, 0], [100, 0])
msp.add_line([100, 0], [100, 100])
msp.add_line([100, 100], [0, 100])
msp.add_line([0, 100], [0, 0])
doc.saveas('tetra.dxf')  #Save

Try to open the created file.

shikaku.png

I was able to draw a rectangle properly.

Layer settings

In 2d CAD such as .dxf, it is necessary to consider the layer setting. A layer is a collection of the same line type, and the line type (dots, etc.) and color are different for each layer.

Predefine the layers and specify the layers when drawing a straight line or circle (argument: dxfattribs).

import ezdxf #'0.13.1'
#Specify version with dxf
doc = ezdxf.new("R2010", setup=True)

#Layer definition
doc.layers.new(name="MyLine1", dxfattribs={'linetype': 'DASHED', 'color': 7})
doc.layers.new(name="MyLine2", dxfattribs={'linetype': 'CONTINUOUS', 'color': 1})
doc.layers.new(name="MyLine3", dxfattribs={'linetype': 'CENTER', 'color': 2})

#add new entities to the modelspace
msp = doc.modelspace() 

#Add a straight line
msp.add_line([0, 0], [100, 0], dxfattribs={'layer': 'MyLine1'})
msp.add_line([100, 0], [100, 100], dxfattribs={'layer': 'MyLine1'})
msp.add_line([100, 100], [0, 100], dxfattribs={'layer': 'MyLine1'})
msp.add_line([0, 100], [0, 0], dxfattribs={'layer': 'MyLine1'})

#center[50, 50],Addition of a circle with a radius of 50
msp.add_circle(center=[50, 50], radius=50, dxfattribs={'layer': 'MyLine2'})

#Add arc
msp.add_arc(center=[50, 50], radius=40, 
            start_angle=0, end_angle=90, dxfattribs={'layer': 'MyLine2'})
msp.add_arc(center=[50, 50], radius=40,
            start_angle=90, end_angle=360, dxfattribs={'layer': 'MyLine3'})

#[50, 50]Add a point at the position of
msp.add_point([50, 50], dxfattribs={'layer': 'MyLine1'})

#Save image
doc.saveas('sample.dxf')

When I opened the created file, it became as follows.

sample.png

At first, the point at [50, 50] was too small, and I misunderstood it as dust on the LCD monitor.

Recommended Posts

Create a 2d CAD file ".dxf" with python [ezdxf]
Create a Photoshop format file (.psd) with python
Create 3d gif with python3
Create a directory with python
[Python] How to create a 2D histogram with Matplotlib
Solve ABC166 A ~ D with Python
Create a virtual environment with Python!
Create an Excel file with Python3
Create a binary file in Python
Create a file uploader with Django
[Python] Create a file & folder path specification screen with tkinter
Create a Python function decorator with Class
Creating a simple PowerPoint file with Python
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Create a large text file with shellscript
Create a VM with a YAML file (KVM)
Create Excel file with Python + similarity matrix
Solve AtCoder ABC168 with python (A ~ D)
Create a word frequency counter with Python 3.4
Create a deb file from a python package
[GPS] Create a kml file in Python
I made a configuration file with Python
Create a frame with transparent background with tkinter [Python]
How to read a CSV file with Python 2/3
Create a GIF file using Pillow in Python
Create a GUI executable file created with tkinter
Create a LINE BOT with Minette for Python
How to create a JSON file in Python
Create a PDF file with a random page size
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
Solve A ~ D of yuki coder 247 with python
Steps to create a Twitter bot with python
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)
Create 3D scatter plot with SciPy + matplotlib (Python)
Create a MIDI file in Python using pretty_midi
Create a Python console application easily with Click
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
Create a cylinder with open3d + STL file output
Create a Python module
Create a Python environment
A story about making 3D space recognition with Python
Why not create a stylish table easily with Python?
Create a python development environment with vagrant + ansible + fabric
Create a Layer for AWS Lambda Python with Docker
[python] Create a date array with arbitrary increments with np.arange
Python script to create a JSON file from a CSV file
Run a Python file with relative import in PyCharm
Create a fake Minecraft server in Python with Quarry
Create a company name extractor with python using JCLdic