2016-05-11 162 views
0

我想用python繪製填充的橢圓。如果我可以使用PIL或其他庫,這很容易。問題是我需要.dxf文件格式的橢圓。因此我使用了dxfwrite包。這使我可以繪製一個橢圓,但我找不到用純色填充它的方法。下面的代碼確實畫了一條橢圓線,但沒有填充它。python中的橢圓實體填充dxf

import dxfwrite 
from dxfwrite import DXFEngine as dxf 

name = 'ellipse.dxf' 
dwg = dxf.drawing(name) 
dwg.add(dxf.ellipse((0,0), 5., 10., segments=200)) 
dwg.save() 

有沒有人你知道一個解決方案?

+0

是否可以接受使用固體圖案填充對象來填充橢圓?如果是這樣,我可以給你看DXF。 –

+0

dxfwrite只創建DXF R12文件,而DXF R12不支持HATCH實體,但包_ezdxf_確實支持HATCH實體。 PyPI上的ezdxf:https://pypi.python.org/pypi/ezdxf/; [文檔爲HATCH](https://ezdxf.readthedocs.io/en/latest/entities.html#hatch) – mozman

回答

1

您可以通過使用固體圖案填充對象填充的橢圓:

Hatch in Autocad

對於上面的例子,這裏是從包含橢圓和艙口DXF文件的一個片段:

AcDbEntity 
    8 
0 
100 
AcDbEllipse 
10 
2472.192919 
20 
1311.37942 
30 
0.0 
11 
171.0698134145308 
21 
-27.61597470964863 
31 
0.0 
210 
0.0 
220 
0.0 
230 
1.0 
40 
0.2928953354556341 
41 
0.0 
42 
6.283185307179586 
    0 
HATCH 
    5 
5A 
330 
2 
100 
AcDbEntity 
    8 
0 
100 
AcDbHatch 
10 
0.0 
20 
0.0 
30 
0.0 
210 
0.0 
220 
0.0 
230 
1.0 
    2 
SOLID 
70 
    1 
71 
    1 
91 
     1 
92 
     5 
93 
     1 
72 
    3 
10 
2472.192919357234 
20 
1311.379420138197 
11 
171.0698134145308 
21 
-27.61597470964863 
40 
0.2928953354556341 
50 
0.0 
51 
360.0 
73 
    1 
97 
     1 
330 
59 
75 
    1 
76 
    1 
47 
0.794178 
98 
     1 
10 
2428.34191358924 
20 
1317.777876434349 
450 
     0 
451 
     0 
460 
0.0 
461 
0.0 
452 
     0 
462 
1.0 
453 
     2 
463 
0.0 
63 
    5 
421 
     255 
463 
1.0 
63 
    2 
421 
16776960 
470 
LINEAR 
1001 
GradientColor1ACI 
1070 
    5 
1001 
GradientColor2ACI 
1070 
    2 
1001 
ACAD 
1010 
0.0 
1020 
0.0 
1030 
0.0 

涉及到很多DXF代碼。這是信息的Autodesk提供:

Hatch group codes 
Group code 
Description 
100 
Subclass marker (AcDbHatch) 
10 
Elevation point (in OCS) 
DXF: X value = 0; APP: 3D point (X and Y always equal 0, Z represents the elevation) 
20, 30 
DXF: Y and Z values of elevation point (in OCS) 
Y value = 0, Z represents the elevation 
210 
Extrusion direction (optional; default = 0, 0, 1) 
DXF: X value; APP: 3D vector 
220, 230 
DXF: Y and Z values of extrusion direction 
2 
Hatch pattern name 
70 
Solid fill flag (solid fill = 1; pattern fill = 0); for MPolygon, the version of MPolygon 
63 
For MPolygon, pattern fill color as the ACI 
71 
Associativity flag (associative = 1; non-associative = 0); for MPolygon, solid-fill flag (has solid fill = 1; lacks solid fill = 0) 
91 
Number of boundary paths (loops) 
varies 
Boundary path data. Repeats number of times specified by code 91. See Boundary Path Data 
75 
Hatch style: 
0 = Hatch 「odd parity」 area (Normal style) 
1 = Hatch outermost area only (Outer style) 
2 = Hatch through entire area (Ignore style) 
76 
Hatch pattern type: 
0 = User-defined; 1 = Predefined; 2 = Custom 
52 
Hatch pattern angle (pattern fill only) 
41 
Hatch pattern scale or spacing (pattern fill only) 
73 
For MPolygon, boundary annotation flag (boundary is an annotated boundary = 1; boundary is not an annotated boundary = 0) 
77 
Hatch pattern double flag (pattern fill only): 
0 = not double; 1 = double 
78 
Number of pattern definition lines 
varies 
Pattern line data. Repeats number of times specified by code 78. See Pattern Data 
47 
Pixel size used to determine the density to perform various intersection and ray casting operations in hatch pattern computation for associative hatches and hatches created with the Flood method of hatching 
98 
Number of seed points 
11 
For MPolygon, offset vector 
99 
For MPolygon, number of degenerate boundary paths (loops), where a degenerate boundary path is a border that is ignored by the hatch 
10 
Seed point (in OCS) 
DXF: X value; APP: 2D point (multiple entries) 
20 
DXF: Y value of seed point (in OCS); (multiple entries) 
450 
Indicates solid hatch or gradient; if solid hatch, the values for the remaining codes are ignored but must be present. Optional; if code 450 is in the file, then the following codes must be in the file: 451, 452, 453, 460, 461, 462, and 470. If code 450 is not in the file, then the following codes must not be in the file: 451, 452, 453, 460, 461, 462, and 470 
0 = Solid hatch 
1 = Gradient 
451 
Zero is reserved for future use 
452 
Records how colors were defined and is used only by dialog code: 
0 = Two-color gradient 
1 = Single-color gradient 
453 
Number of colors: 
0 = Solid hatch 
2 = Gradient 
460 
Rotation angle in radians for gradients (default = 0, 0) 
461 
Gradient definition; corresponds to the Centered option on the Gradient Tab of the Boundary Hatch and Fill dialog box. Each gradient has two definitions, shifted and unshifted. A Shift value describes the blend of the two definitions that should be used. A value of 0.0 means only the unshifted version should be used, and a value of 1.0 means that only the shifted version should be used. 
462 
Color tint value used by dialog code (default = 0, 0; range is 0.0 to 1.0). The color tint value is a gradient color and controls the degree of tint in the dialog when the Hatch group code 452 is set to 1. 
463 
Reserved for future use: 
0 = First value 
1 = Second value 
470 
String (default = LINEAR) 

我希望這可能是一些對你有用的。如果我誤解了你的問題,我很抱歉。

+0

thx爲您的答案!多數民衆贊成什麼我補充。但令人遺憾的是,我不知道如何在python中實現孵化。 – ChrisG

+0

對不起,我不知道答案的部分內容。 :( –

2

艙口實體不dxfwrite支持,如果你使用ezdxf這是解決方案:

import ezdxf 

dwg = ezdxf.new('AC1015') # hatch requires the DXF R2000 (AC1015) format or later 
msp = dwg.modelspace() # adding entities to the model space 

# important: major axis >= minor axis (ratio <= 1.) else AutoCAD crashes 
msp.add_ellipse((0, 0), major_axis=(0, 10), ratio=0.5) 

hatch = msp.add_hatch(color=2) 
with hatch.edit_boundary() as boundary: # edit boundary path (context manager) 
    edge_path = boundary.add_edge_path() 
    # an edge path can contain line, arc, ellipse or spline elements 
    edge_path.add_ellipse((0, 0), major_axis_vector=(0, 10), minor_axis_length=0.5) 
    # upcoming ezdxf 0.7.7: 
    # renamed major_axis_vector to major_axis 
    # renamed minor_axis_length to ratio 

dwg.saveas("solid_hatch_ellipse.dxf")