Blog Links

  各种图元的绘制是 CAD 绝对的核心功能,CAD 主要能实现如下图所示的图元绘制,包括常见的,直线、多段线、圆、图案填充、单行文字、多行文字、标注等等。

AutoCAD绘图菜单

  本文主要采用 pyautocad 库及 pywin32 库连接 CAD,两种连接方式如下:

  • 方式一: pyautocad
 
 

""" ============================= tác giả: DalNur Thư điện tử: liyang@alu.hit.edu.cn ============================= """

from

pyautocad

import

Autocad

,

APoint

,

aDouble

import

math pyacad

=

Autocad

(

create_if_not_exists

=

True

)

pyacad

.

prompt

(

"Hello! Autocad from pyautocad."

)

print

(

pyacad

.

doc

.

Name

)

  • 方式二:pywin32
 
 

""" ============================= tác giả: DalNur Thư điện tử: liyang@alu.hit.edu.cn ============================= """

import

pythoncom

import

win32com

.

client

import

math wincad

=

win32com

.

client

.

Dispatch

(

"AutoCAD.Application"

)

doc

=

wincad

.

ActiveDocument doc

.

Utility

.

Prompt

(

"Hello! Autocad from pywin32com.n"

)

msp

=

doc

.

ModelSpace

print

(

doc

.

Name

)

def

vtpnt

(

x

,

y

,

z

=

)

:

return

win32com

.

client

.

VARIANT

(

pythoncom

.

VT_ARRAY

|

pythoncom

.

VT_R8

,

(

x

,

y

,

z

)

)

def

vtobj

(

obj

)

:

return

win32com

.

client

.

VARIANT

(

pythoncom

.

VT_ARRAY

|

pythoncom

.

VT_DISPATCH

,

obj

)

def

vtfloat

(

lst

)

:

return

win32com

.

client

.

VARIANT

(

pythoncom

.

VT_ARRAY

|

pythoncom

.

VT_R8

,

lst

)

  1. 设置点显示
pyacad

.

ActiveDocument

.

SetVariable

(

"PDMODE"

,

35

)

pyacad

.

ActiveDocument

.

SetVariable

(

"PDSIZE"

,

2

)

  1. pyautocad绘直线
startPoint 

=

APoint

(

5

,

8

)

endPoint

=

APoint

(

20

,

15

)

lineObj

=

pyacad

.

model

.

AddLine

(

startPoint

,

endPoint

)

  1. pywin32绘制点
startPntCoords 

=

vtpnt

(

5

,

5

)

startPoint

=

msp

.

AddPoint

(

startPntCoords

)

endPntCoords

=

vtpnt

(

20

,

20

)

endPoint

=

msp

.

AddPoint

(

endPntCoords

)

  1. pywin32绘直线
lineObj 

=

msp

.

AddLine

(

startPntCoords

,

endPntCoords

)

  以上代码运行后,效果如下图所示:

3.1 绘制普通多段线

  1. pyautocad绘多段线
 
pnts 

=

[

APoint

(

5

,

5

)

,

APoint

(

10

,

5

)

,

APoint

(

20

,

20

)

,

APoint

(

25

,

20

)

]

pnts

=

[

j

for

i

in

pnts

for

j

in

i

]

pnts

=

aDouble

(

pnts

)

plineObj

=

pyacad

.

model

.

AddPolyLine

(

pnts

)

clr

=

pyacad

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

,

,

255

)

plineObj

.

TrueColor

=

clr segmentIndex

=

2

startWidth

=

0.5

endWidth

=

1.0

plineObj

.

SetWidth

(

segmentIndex

,

startWidth

,

endWidth

)

pyacad

.

ActiveDocument

.

preferences

.

LineweightDisplay

=

1

pyacad

.

ActiveDocument

.

Application

.

ZoomAll

(

)

pyacad

.

ActiveDocument

.

Application

.

Cập nhật

(

)

  1. pywin32绘多段线
 
pnts 

=

[

5

,

20

,

,

10

,

20

,

,

20

,

5

,

,

25

,

5

,

]

points

=

vtfloat

(

pnts

)

plineObj

=

msp

.

AddPolyline

(

points

)

clr

=

doc

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

255

,

,

)

plineObj

.

TrueColor

=

clr segmentIndex

=

startWidth

=

1.0

endWidth

=

0.5

plineObj

.

SetWidth

(

segmentIndex

,

startWidth

,

endWidth

)

doc

.

preferences

.

LineweightDisplay

=

1

doc

.

Application

.

ZoomAll

(

)

doc

.

Application

.

Cập nhật

(

)

  以上代码运行后,效果如下图所示:

Xem Thêm  Auto tune là gì? Chức năng Auto Tune và Cách sử dụng Auto tune để có giọng hát hay hơn ❤️ Bài viết Auto tune là gì? Chức năng Auto Tune và Cách sử dụng Auto tune để có giọng hát hay hơn

3.2 绘制含圆弧多段线

  详见博客:Python 二次开发 AutoCAD 简介 >> 七、绘图 >> 7.1 倒角矩形 。

3.4 绘制样条曲线

splinePnts 

=

[

APoint

(

5

,

12

)

,

APoint

(

12

,

5

)

,

APoint

(

18

,

8

)

,

APoint

(

25

,

5

)

]

splinePnts

=

[

j

for

i

in

splinePnts

for

j

in

i

]

splinePnts

=

aDouble

(

splinePnts

)

startTan

=

APoint

(

1

,

-

10

)

endTan

=

APoint

(

1

,

-

5

)

SplineObj

=

pyacad

.

model

.

AddSpline

(

splinePnts

,

startTan

,

endTan

)

4.1 绘制圆

  1. pyautocad
 
center 

=

APoint

(

5

,

5

)

radius

=

4

circleObj

=

pyacad

.

model

.

AddCircle

(

center

,

radius

)

clr

=

pyacad

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

,

,

255

)

circleObj

.

TrueColor

=

clr

  1. pywin32
center

,

radius

=

vtpnt

(

5

,

5

,

)

,

2

circleObj

=

msp

.

AddCircle

(

center

,

radius

)

clr

=

doc

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

255

,

,

)

circleObj

.

TrueColor

=

clr

  以上代码运行后,效果如下图所示:

4.2 绘制圆弧

  An arc is always drawn counterclockwise from the start point to the endpoint. The StartPoint and EndPoint properties of an arc are calculated through the StartAngle, EndAngle, and Radius properties.

  1. pyautocad
 
center 

=

APoint

(

5

,

5

)

radius

=

4

startAngle

=

math

.

radians

(

-

60

)

endAngle

=

math

.

radians

(

60

)

arcObj

=

pyacad

.

model

.

AddArc

(

center

,

radius

,

math

.

radians

(

-

60

)

,

math

.

radians

(

60

)

)

clr

=

pyacad

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

,

,

255

)

arcObj

.

TrueColor

=

clr

  1. pywin32
 
center

,

radius

=

vtpnt

(

5

,

5

,

)

,

2

startAngle

=

math

.

radians

(

-

60

)

endAngle

=

math

.

radians

(

60

)

arcObj

=

msp

.

AddArc

(

center

,

radius

,

math

.

radians

(

-

60

)

,

math

.

radians

(

60

)

)

clr

=

doc

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

255

,

,

)

arcObj

.

TrueColor

=

clr

  以上代码运行后,效果如下图所示:

5.1 绘制椭圆

  1. pyautocad
 
center 

=

APoint

(

8

,

5

,

)

majAxis

=

APoint

(

5

,

,

)

radRatio

=

0.5

ellObj

=

pyacad

.

model

.

AddEllipse

(

center

,

majAxis

,

radRatio

)

clr

=

pyacad

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

,

,

255

)

ellObj

.

TrueColor

=

clr

  1. pywin32
 
center 

=

vtpnt

(

13

,

5

,

)

majAxis

=

vtpnt

(

5

,

,

)

radRatio

=

0.5

ellObj

=

msp

.

AddEllipse

(

center

,

majAxis

,

radRatio

)

clr

=

doc

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

255

,

,

)

ellObj

.

TrueColor

=

clr

  以上代码运行后,效果如下图所示:

5.2 绘制椭圆弧

  1. pyautocad
 
center 

=

APoint

(

-

3

,

5

,

)

majAxis

=

APoint

(

5

,

,

)

radRatio

=

0.5

ellObj

=

pyacad

.

model

.

AddEllipse

(

center

,

majAxis

,

radRatio

)

ellObj

.

startAngle

=

-

90

*

(

3.14

/

180

)

ellObj

.

endAngle

=

90

*

(

3.14

/

180

)

clr

=

pyacad

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

,

,

255

)

ellObj

.

TrueColor

=

clr

  1. pywin32
 
center 

=

vtpnt

(

3

,

5

,

)

majAxis

=

vtpnt

(

5

,

,

)

radRatio

=

0.5

ellObj

=

msp

.

AddEllipse

(

center

,

majAxis

,

radRatio

)

ellObj

.

startAngle

=

90

*

(

3.14

/

180

)

ellObj

.

endAngle

=

-

90

*

(

3.14

/

180

)

clr

=

doc

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

255

,

,

)

ellObj

.

TrueColor

=

clr

  以上代码运行后,效果如下图所示:

Xem Thêm  Auto Club Driving School

6.1 实体填充

  1. pyautocad
 
pnt1

,

pnt2

,

pnt3

,

pnt4

=

APoint

(

-

10

,

5

,

)

,

APoint

(

-

10

,

10

,

)

,

APoint

(

-

3

,

5

,

)

,

APoint

(

-

3

,

10

,

)

solidObj

=

pyacad

.

model

.

AddSolid

(

pnt1

,

pnt2

,

pnt3

,

pnt4

)

clr

=

pyacad

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

,

,

255

)

solidObj

.

TrueColor

=

clr

  1. pywin32
 
pnt1

,

pnt2

,

pnt3

,

pnt4

=

vtpnt

(

3

,

5

,

)

,

vtpnt

(

3

,

10

,

)

,

vtpnt

(

10

,

5

,

)

,

vtpnt

(

10

,

10

,

)

solidObj

=

msp

.

AddSolid

(

pnt1

,

pnt2

,

pnt3

,

pnt4

)

clr

=

doc

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

clr

.

SetRGB

(

255

,

,

)

solidObj

.

TrueColor

=

clr

  以上代码运行后,效果如下图所示:

6.2 圆形图案填充

ptnName

,

ptnType

,

bAss

,

center

,

radius

=

"ANSI31"

,

,

True

,

vtpnt

(

10

,

10

,

)

,

7

outerLoop

=

[

]

circleObj

=

msp

.

AddCircle

(

center

,

radius

)

outerLoop

.

append

(

circleObj

)

outerLoop

=

vtobj

(

outerLoop

)

hatchObj

=

msp

.

AddHatch

(

ptnType

,

ptnName

,

bAss

)

hatchObj

.

AppendOuterLoop

(

outerLoop

)

hatchObj

.

Evaluate

(

)

hatchObj

.

PatternScale

=

0.2

  以上代码运行后,效果如下图所示:

6.3 闭合多段线图案填充

  详见博客:Python 二次开发 AutoCAD 简介 >> 七、绘图 >> 7.2 闭合多段线图案填充 。

  Autodesk AutoCAD 2014: ActiveX Reference Guide 文档中关于绘图次序例子的Python代码如下:

  1. Set drawing to display lineweights and create a True Color object.
doc

.

preferences

.

LineweightDisplay

=

1

clr

=

doc

.

Application

.

GetInterfaceObject

(

"AutoCAD.AcCmColor.19"

)

  1. Draw a polyline
points 

=

[

4

,

4

,

,

3

,

5

,

,

6

,

20

,

]

points

=

vtfloat

(

points

)

plineObj

=

msp

.

AddPolyline

(

points

)

plineObj

.

Lineweight

=

211

clr

.

SetRGB

(

90

,

110

,

150

)

plineObj

.

TrueColor

=

clr

  1. Draw a line
startPoint 

=

vtpnt

(

5

,

13

)

endPoint

=

vtpnt

(

5

,

27

)

lineObj

=

msp

.

AddLine

(

startPoint

,

endPoint

)

lineObj

.

Lineweight

=

211

clr

.

SetRGB

(

50

,

80

,

230

)

lineObj

.

TrueColor

=

clr

  1. Draw a circle
centerPoint 

=

vtpnt

(

10

,

15

,

)

radius

=

5

circleObj

=

msp

.

AddCircle

(

centerPoint

,

radius

)

circleObj

.

Lineweight

=

211

clr

.

SetRGB

(

60

,

200

,

220

)

circleObj

.

TrueColor

=

clr doc

.

Application

.

ZoomAll

(

)

doc

.

Application

.

Cập nhật

(

)

  1. Get an extension dictionary and, if necessary, add a SortentsTable object.
eDictionary 

=

msp

.

GetExtensionDictionary

(

)

try

:

sentityObj

=

eDictionary

.

GetObject

(

"ACAD_SORTENTS"

)

except

:

sentityObj

=

eDictionary

.

AddObject

(

"ACAD_SORTENTS"

,

"AcDbSortentsTable"

)

ObjIds

=

[

plineObj

.

ObjectID

,

lineObj

.

ObjectID

,

circleObj

.

ObjectID

]

  1. Move the circle object to the bottom
varObject 

=

doc

.

ObjectIdToObject

(

ObjIds

[

2

]

)

varObject

=

vtobj

(

[

varObject

,

]

)

sentityObj

.

MoveToBottom

(

varObject

)

doc

.

Application

.

Cập nhật

(

)

import

pyautocad

.

types pyautocad

.

types

.

distance

(

Pnt1

,

Pnt2

)

print

(

Pnt1

)

print

(

tuple

(

Pnt1

)

)

print

(

danh sách

(

Pnt1

)

)

print

(

Pnt1

+

Pnt2

)

LineObj

.

layer

=

"Python"

  以上,便是关于 Python 二次开发 AutoCAD 绘图 的简单介绍。

Xem Thêm  Online automaty ZDARMA: Bez registrace

  因篇幅有限,某些非关键功能未做详细介绍,如有疑问,欢迎邮件交流,Thư điện tử: liyang@alu.hit.edu.cn

  本文部分功能的实现离不 ke1078 同学的大力帮助,有些功能看似简单,但第一次实现却是相当不容易的。

  鉴于相关示例代码相对较少,特写本文,一方面是为自己的阶段性学习做一个总结,另一方面更是为有需要的人提供多一点参考。

  本文仅用于个人学习,除此之外,无其他任何用途。

  因本人野生学习 Python ,水平确实有限,文中难免有所疏漏,还请各位大神不吝批评指正。

  胸藏文墨怀若谷,腹有诗书气自华,希望各位都能在知识的 pāo 子里快乐徜徉。

  本文最早发表于 2018-12-13 18:20:21,作为博客 Python 二次开发 AutoCAD 简介 的一部分。

  为了清晰直观,于 2019-07-20 14:23:44 单独创建本文。

  本文部分内容,源于网络!

  欢迎大家点赞、评论及转载,转载请注明出处!

  为我打call,不如为我打款!

  打赏可备注邮箱,本人将赠送本系列博客的全部 Python 源代码。

  最后,祝各位攻城狮们,珍爱生命,保护发际线!

Trả lời