在python dxf中实体填充椭圆

时间:2016-05-11 13:00:30

标签: python-2.7 python-3.x dxf

我想用python绘制填充的椭圆。如果我可以使用PIL或其他一些库,这将很容易。问题是我需要.dxf文件格式的椭圆。因此我使用了dxfwrite包。这允许我绘制一个椭圆,但我找不到用纯色填充它的方法。下面的代码确实绘制了一条椭圆线,但没有填充它。

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()

你们中的任何人都知道解决方案吗?

2 个答案:

答案 0 :(得分:2)

dxfwrite不支持HATCH实体,如果你使用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")

答案 1 :(得分: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)

我希望这对你有用。如果我误解了你的问题,我很抱歉。