.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
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).
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.
I was able to draw a rectangle properly.
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.
At first, the point at [50, 50] was too small, and I misunderstood it as dust on the LCD monitor.
Recommended Posts