前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用python生成 dxf 文件

用python生成 dxf 文件

作者头像
用户6021899
发布2024-01-02 12:15:48
2080
发布2024-01-02 12:15:48
举报

安装dxf模块后,就可以用python 画图,生成dxf文件。

下面是我写的一个示例,用来画很多个不相交的、大小和位置都随机的圆构成的多孔图形。

代码语言:javascript
复制
代码语言:javascript
复制
import sdxf as s
import random
from random import random, randint

d = s.Drawing()
d.append(s.Text('圆的世界!', point=(0, 105, 1)))
# d.append(s.Line(points=[(0, 0, 0), (1, 1, 1)]))  # 画线段


c0, r0 = (0, 0, 0), 100  # 外围大圆的中心和半径
# d.append(s.Arc(center=c0, radius=r0, startAngle=0.0, endAngle=90))  # 画圆弧
d.append(s.Circle(center=c0, radius=r0))  # 画圆

siblings = []  # 格式[((x1,y1,z1),r1),((x2,y2,z2),r2)...]
r_lsl, r_usl = 3, 15
xy_lsl, xy_usl = -100, 100

i = 0
j = 0
while i <= 1000 and j <= 100000:
    r = randint(r_lsl, r_usl)
    center = xy_lsl+(xy_usl-xy_lsl)*random(), xy_lsl+(xy_usl-xy_lsl)*random(), 0

    # 需在大圆内部
    dx = center[0] - c0[0]
    dy = center[1] - c0[1]
    if dx*dx + dy*dy >= (r0-r)*(r0-r):
        continue

    # 需在所有小圆的外面
    for circle in siblings:
        dx = center[0] - circle[0][0]
        dy = center[1] - circle[0][1]
        if dx*dx + dy*dy <= (circle[1]+r)*(circle[1]+r):
            break
    else:
        d.append(s.Circle(center=center, radius=r))
        siblings.append((center, r))
        print(f"r= {r}, center= {center}")
        i += 1

    j += 1

d.saveas(r'k:\circle world.dxf')

# help(s)

该python代码生成的dxf文件在CAD中打开的效果如下:

模块的帮助文档:

NAME

sdxf

DESCRIPTION

SDXF - Stani's DXF

Python library to generate dxf drawings

Copyright www.stani.be

Version v1.2 (11/16/08)

License GPL

Homepage http://www.stani.be/python/sdxf

Library by Stani, whose website is now defunct.

Modifications by Kelly Farrell (http://www.kellbot.com)

to support LWPOLYLINE.

CLASSES

_Call(builtins.object)

Layer

LineType

Style

View

_Collection(_Call)

Block

Drawing

_Entity(_Call)

Arc

Circle

Face

Insert

Line

LineList

LwPolyLine

Point

Rectangle

Solid

Text

Mtext

class Arc(_Entity)

| Arc(center=(0, 0, 0), radius=1, startAngle=0.0, endAngle=90, **common)

|

| Arc, angles in degrees.

|

| Method resolution order:

| Arc

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, center=(0, 0, 0), radius=1, startAngle=0.0, endAngle=90, **common)

| Angles in degrees.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Block(_Collection)

| Block(name, layer='0', flag=0, base=(0, 0, 0), entities=[])

|

| Use list methods to add entities, eg append.

|

| Method resolution order:

| Block

| _Collection

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, name, layer='0', flag=0, base=(0, 0, 0), entities=[])

| Initialize self. See help(type(self)) for accurate signature.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Circle(_Entity)

| Circle(center=(0, 0, 0), radius=1, **common)

|

| Circle

|

| Method resolution order:

| Circle

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, center=(0, 0, 0), radius=1, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Drawing(_Collection)

| Drawing(insbase=(0.0, 0.0, 0.0), extmin=(0.0, 0.0), extmax=(0.0, 0.0), layers=[<sdxf.Layer object at 0x0000018154B3EC10>], linetypes=[<sdxf.LineType object at 0x0000018154B3EA30>], styles=[<sdxf.Style object at 0x0000018154B66E50>], blocks=[], views=[], entities=None, fileName='test.dxf')

|

| Dxf drawing. Use append or any other list methods to add objects.

|

| Method resolution order:

| Drawing

| _Collection

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, insbase=(0.0, 0.0, 0.0), extmin=(0.0, 0.0), extmax=(0.0, 0.0), layers=[<sdxf.Layer object at 0x0000018154B3EC10>], linetypes=[<sdxf.LineType object at 0x0000018154B3EA30>], styles=[<sdxf.Style object at 0x0000018154B66E50>], blocks=[], views=[], entities=None, fileName='test.dxf')

| Initialize self. See help(type(self)) for accurate signature.

|

| __str__(self)

| Returns drawing as dxf string.

|

| save(self)

|

| saveas(self, fileName)

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Face(_Entity)

| Face(points, **common)

|

| 3dface

|

| Method resolution order:

| Face

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, points, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Insert(_Entity)

| Insert(name, point=(0, 0, 0), xscale=None, yscale=None, zscale=None, cols=None, colspacing=None, rows=None, rowspacing=None, rotation=None, **common)

|

| Block instance.

|

| Method resolution order:

| Insert

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, name, point=(0, 0, 0), xscale=None, yscale=None, zscale=None, cols=None, colspacing=None, rows=None, rowspacing=None, rotation=None, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Layer(_Call)

| Layer(name='pydxf', color=7, lineType='continuous', flag=64)

|

| Layer

|

| Method resolution order:

| Layer

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, name='pydxf', color=7, lineType='continuous', flag=64)

| Initialize self. See help(type(self)) for accurate signature.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Line(_Entity)

| Line(points, **common)

|

| Line

|

| Method resolution order:

| Line

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, points, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class LineList(_Entity)

| LineList(points=[], closed=0, **common)

|

| Like polyline, but built of individual lines.

|

| Method resolution order:

| LineList

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, points=[], closed=0, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class LineType(_Call)

| LineType(name='continuous', description='Solid line', elements=[], flag=64)

|

| Custom linetype

|

| Method resolution order:

| LineType

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, name='continuous', description='Solid line', elements=[], flag=64)

| Initialize self. See help(type(self)) for accurate signature.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class LwPolyLine(_Entity)

| LwPolyLine(points, flag=0, width=None, **common)

|

| This is a LWPOLYLINE. I have no idea how it differs from a normal PolyLine

|

| Method resolution order:

| LwPolyLine

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, points, flag=0, width=None, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Mtext(Text)

| Mtext(text='', point=(0, 0, 0), width=250, spacingFactor=1.5, down=0, spacingWidth=None, **options)

|

| Surrogate for mtext, generates some Text instances.

|

| Method resolution order:

| Mtext

| Text

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, text='', point=(0, 0, 0), width=250, spacingFactor=1.5, down=0, spacingWidth=None, **options)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Point(_Entity)

| Point(points=None, **common)

|

| Colored solid fill.

|

| Method resolution order:

| Point

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, points=None, **common)

| None values will be omitted.

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

PolyLine = class LineList(_Entity)

| PolyLine(points=[], closed=0, **common)

|

| Like polyline, but built of individual lines.

|

| Method resolution order:

| LineList

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, points=[], closed=0, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Rectangle(_Entity)

| Rectangle(point=(0, 0, 0), width=1, height=1, solid=None, line=1, **common)

|

| Rectangle, creates lines.

|

| Method resolution order:

| Rectangle

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, point=(0, 0, 0), width=1, height=1, solid=None, line=1, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Solid(_Entity)

| Solid(points=None, **common)

|

| Colored solid fill.

|

| Method resolution order:

| Solid

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, points=None, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Style(_Call)

| Style(name='standard', flag=0, height=0, widthFactor=40, obliqueAngle=50, mirror=0, lastHeight=1, font='arial.ttf', bigFont='')

|

| Text style

|

| Method resolution order:

| Style

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, name='standard', flag=0, height=0, widthFactor=40, obliqueAngle=50, mirror=0, lastHeight=1, font='arial.ttf', bigFont='')

| Initialize self. See help(type(self)) for accurate signature.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class Text(_Entity)

| Text(text='', point=(0, 0, 0), alignment=None, flag=None, height=1, justifyhor=None, justifyver=None, rotation=None, obliqueAngle=None, style=None, xscale=None, **common)

|

| Single text line.

|

| Method resolution order:

| Text

| _Entity

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, text='', point=(0, 0, 0), alignment=None, flag=None, height=1, justifyhor=None, justifyver=None, rotation=None, obliqueAngle=None, style=None, xscale=None, **common)

| None values will be omitted.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

class View(_Call)

| View(name, flag=0, width=1, height=1, center=(0.5, 0.5), direction=(0, 0, 1), target=(0, 0, 0), lens=50, frontClipping=0, backClipping=0, twist=0, mode=0)

|

| Method resolution order:

| View

| _Call

| builtins.object

|

| Methods defined here:

|

| __init__(self, name, flag=0, width=1, height=1, center=(0.5, 0.5), direction=(0, 0, 1), target=(0, 0, 0), lens=50, frontClipping=0, backClipping=0, twist=0, mode=0)

| Initialize self. See help(type(self)) for accurate signature.

|

| __str__(self)

| Return str(self).

|

| ----------------------------------------------------------------------

| Methods inherited from _Call:

|

| __call__(self, **attrs)

| Returns a copy with modified attributes.

|

| copy(self)

| Returns a copy.

|

| ----------------------------------------------------------------------

| Data descriptors inherited from _Call:

|

| __dict__

| dictionary for instance variables (if defined)

|

| __weakref__

| list of weak references to the object (if defined)

FUNCTIONS

ViewByWindow(name, leftBottom=(0, 0), rightTop=(1, 1), **options)

main()

#---test

DATA

ALIGNED = 3

ANONYMOUS = 1

AT_LEAST = 1

BASELINE = 0

BOTTOM = 1

BOTTOM_CENTER = 8

BOTTOM_LEFT = 7

BOTTOM_RIGHT = 9

BYBLOCK = 0

BYLAYER = 256

BY_STYLE = 5

CENTER = 1

CLOSED = 1

CLOSED_N = 32

CONTINOUS_LINETYPE_PATTERN = 128

CURVE_FIT = 2

EXACT = 2

EXTERNAL = 16

FIT = 5

LEFT = 0

LEFT_RIGHT = 1

MIDDLE = 2

MIDDLE_CENTER = 5

MIDDLE_LEFT = 4

MIDDLE_RIGHT = 6

NON_CONSTANT_ATTRIBUTES = 2

POLYFACE_MESH = 64

POLYGON_MESH = 16

POLYLINE_3D = 8

REFERENCED = 64

RESOLVED = 32

RIGHT = 2

SPLINE_FIT = 4

TOP = 3

TOP_BOTTOM = 3

TOP_CENTER = 2

TOP_LEFT = 1

TOP_RIGHT = 3

XREF = 4

XREF_OVERLAY = 8

__license__ = 'GPL'

__url__ = 'http://www.stani.be/python/sdxf'

VERSION

v1.2 (11/16/08)

AUTHOR

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-12-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档