本文不定期更新!
目 錄
- 0、相關(guān)文章
- 1、前沿
- 2、連接及庫導(dǎo)入
- 3、文字
- (1) 創(chuàng)建新文本
- (2) 對齊方式
- (3) 文本縮放
- 4、文本樣式
- (1) 創(chuàng)建文字樣式
- (2) 將特定文字樣式設(shè)為當(dāng)前
- (3) 設(shè)置字體
- (4) 設(shè)置字體傾斜角度
- 5、尺寸標(biāo)準(zhǔn)
- (1) 線性及旋轉(zhuǎn)線性標(biāo)注
- (2) 對齊標(biāo)注
- (3) 角度標(biāo)注
- (4) 弧長標(biāo)注
- (5) 直徑及半徑標(biāo)注
- 6、標(biāo)注樣式
- (1) 創(chuàng)建新標(biāo)注樣式
- (2) 將特定標(biāo)注樣式設(shè)為當(dāng)前
- (3) 設(shè)置尺寸線及尺寸界限
- (4) 設(shè)置箭頭
- (5) 設(shè)置標(biāo)注文字
- (6) 設(shè)置主單位
- (7) 設(shè)置永久標(biāo)注樣式
- 7、多重引線
- (1) 創(chuàng)建多重引線
- (2) 設(shè)置箭頭大小
- (3) 設(shè)置基線長度
- (4) 設(shè)置基線間隙
- (5) 指定文字樣式
- (6) 指定文字內(nèi)容
- 8、尾聲
0、相關(guān)文章
- Python pyautocad庫 使用簡介
- Python AutoCAD 系統(tǒng)設(shè)置
- Python AutoCAD 圖層
- Python AutoCAD 繪圖
- Python AutoCAD 修改
- Python AutoCAD 塊組
- Python AutoCAD 注釋
- Python AutoCAD 文件
- Python AutoCAD 選擇集
1、前沿
標(biāo)注是向圖形中添加測量注釋的過程,我們可以為各種對象沿各個方向創(chuàng)建標(biāo)注。要簡化圖形組織和標(biāo)注縮放,建議在圖紙空間上創(chuàng)建標(biāo)注,而不是在模型空間中創(chuàng)建標(biāo)注。
2、連接及庫導(dǎo)入
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
=============================
Author = Hulunbuir & DalaiNur
Email: liyang@alu.hit.edu.cn
Last Update: 2019.07.28 14:00
=============================
'''
from
pyautocad
import
Autocad
acad
=
Autocad
(
create_if_not_exists
=
True
)
acad
.
prompt
(
"Hello! AutoCAD from pyautocad."
)
print
(
acad
.
doc
.
Name
)
3、文字
(1) 創(chuàng)建新文本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
===========================
Author = Hulunbuir
Email:liyang@alu.hit.edu.cn
===========================
'''
textString
=
"Harbin Institute of Technology"
insertPnt
=
APoint
(
0
,
0
)
height
=
2.5
textObj
=
acad
.
model
.
AddText
(
textString
,
insertPnt
,
height
)
# textString:文本內(nèi)容;height:字高;
# 系統(tǒng)默認(rèn)對齊夾點(diǎn)位于文字基線左側(cè);
# 采用系統(tǒng)默認(rèn)對齊方式時,insertPnt為對齊夾點(diǎn)的位置坐標(biāo);
# 當(dāng)用戶自定義對齊方式時,insertPnt以對齊方式中的為準(zhǔn)。
(2) 對齊方式
AlignNum
=
7
textObj
.
Alignment
=
AlignNum
# AlignNum為對齊夾點(diǎn)位置標(biāo)識號,其取值范圍為[0,14];
# 0 基線左對齊、1 基線居中對齊、2 基線右對齊;
# 7 表示對齊夾點(diǎn)位于top cennter;
insertPnt
=
APoint
(
0
,
10
)
textObj
.
TextAlignmentPoint
=
insertPnt
# 設(shè)置對齊夾點(diǎn)在圖紙空間中的絕對位置,可為任意一已知點(diǎn);
# 若用戶自定義對齊方式,則需設(shè)置此項。
(3) 文本縮放
# 后續(xù)添加
4、文本樣式
(1) 創(chuàng)建文字樣式
txtStyleObj
=
acad
.
ActiveDocument
.
TextStyles
.
Add
(
"HIT_TxtStyle"
)
(2) 將特定文字樣式設(shè)為當(dāng)前
acad
.
ActiveDocument
.
ActiveTextStyle
=
acad
.
ActiveDocument
.
TextStyles
.
Item
(
"Standard"
)
(3) 設(shè)置字體
### ①、為當(dāng)前文本樣式設(shè)置字體
acad
.
ActiveDocument
.
ActiveTextStyle
.
SetFont
(
"楷體"
,
False
,
False
,
1
,
0
or
0
)
# acad.ActiveDocument.ActiveTextStyle.SetFont(Typeface, Bold, Italic, charSet, PitchandFamily)
# Typeface 字體名稱;
# Bold 加粗,布爾值,F(xiàn)alse為不加粗字體;
# Italic 傾斜,布爾值,F(xiàn)alse為傾斜字體;
# CharSet 字體字符集,1為默認(rèn)字符集;
# PitchAndFamily 字節(jié)及筆畫形式。
### ②、為特定文本樣式設(shè)置字體
(4) 設(shè)置字體傾斜角度
# 后續(xù)添加
5、尺寸標(biāo)準(zhǔn)
(1) 線性及旋轉(zhuǎn)線性標(biāo)注
import
math
XLine1Point
=
APoint
(
5
,
25
)
XLine2Point
=
APoint
(
25
,
35
)
DimLineLocation
=
APoint
(
10
,
20
)
RotationAngle
=
math
.
radians
(
0
)
dimRotObj
=
acad
.
model
.
AddDimRotated
(
XLine1Point
,
XLine2Point
,
DimLineLocation
,
RotationAngle
)
# XLine1Point 第一尺寸界線的起點(diǎn);
# XLine2Point 第二尺寸界線的起點(diǎn);
# DimLineLocation 尺寸線定位點(diǎn),尺寸線或其延長線過該點(diǎn);
# RotationAngle 尺寸線與水平方向的夾角,去弧度制;
# RotationAngle=0 水平標(biāo)注,RotationAngle=90 豎直標(biāo)注。
(2) 對齊標(biāo)注
ExtLine1Point
=
APoint
(
5
,
25
)
ExtLine2Point
=
APoint
(
25
,
35
)
TextPosition
=
APoint
(
-
5
,
25
)
dimAliObj
=
acad
.
model
.
AddDimAligned
(
ExtLine1Point
,
ExtLine2Point
,
TextPosition
)
# ExtLine1Point 第一尺寸界線的起點(diǎn);
# ExtLine2Point 第二尺寸界線的起點(diǎn);
# TextPosition 尺寸線定位點(diǎn),尺寸線或其延長線過該點(diǎn)。
# dimObj.Update
(3) 角度標(biāo)注
AngleVertex
=
APoint
(
0
,
0
)
FirstEndPoint
=
APoint
(
6
,
8
)
SecondEndPoint
=
APoint
(
6
,
-
8
)
TextPoint
=
APoint
(
10
,
10
)
dimAngObj
=
acad
.
model
.
AddDimAngular
(
AngleVertex
,
FirstEndPoint
,
SecondEndPoint
,
TextPoint
)
# AngleVertex 角度頂點(diǎn);
# FirstEndPoint 第一尺寸界線端點(diǎn);
# SecondEndPoint 第二尺寸界線端點(diǎn);
# TextPoint 尺寸圓弧線定位點(diǎn),即尺寸圓弧線過該點(diǎn)。
(4) 弧長標(biāo)注
ArcCenter
=
APoint
(
0
,
0
)
FirstEndPoint
=
APoint
(
6
,
8
)
SecondEndPoint
=
APoint
(
6
,
-
8
)
ArcPoint
=
APoint
(
20
,
0
)
dimArcObj
=
acad
.
model
.
AddDimArc
(
ArcCenter
,
FirstEndPoint
,
SecondEndPoint
,
ArcPoint
)
# ArcCenter 圓弧中心;
# FirstEndPoint 第一尺寸界線端點(diǎn);
# SecondEndPoint 第二尺寸界線端點(diǎn);
# ArcPoint 尺寸圓弧線定位點(diǎn),即尺寸圓弧線過該點(diǎn)。
(5) 直徑及半徑標(biāo)注
ChordPoint
=
APoint
(
0
,
10
)
FarChordPoint
=
APoint
(
0
,
-
10
)
LeaderLength
=
40
dimDiaObj
=
acad
.
model
.
AddDimDiametric
(
ChordPoint
,
FarChordPoint
,
LeaderLength
)
# ChordPoint 圓任意一直徑的端點(diǎn);
# FarChordPoint 直徑的另一端點(diǎn);
# LeaderLength 引線長度,為點(diǎn)ChordPoint到標(biāo)準(zhǔn)文字定位夾點(diǎn)的距離;
# 標(biāo)注類型可在相應(yīng)的系統(tǒng)變量如DIMUPT等中更改。
Center
=
APoint
(
0
,
0
)
ChordPoint
=
APoint
(
10
,
0
)
LeaderLength
=
10
dimRadObj
=
acad
.
model
.
AddDimRadial
(
Center
,
ChordPoint
,
LeaderLength
)
# Center 被標(biāo)注圓或圓弧的圓心
6、標(biāo)注樣式
(1) 創(chuàng)建新標(biāo)注樣式
DimStyleObj
=
acad
.
ActiveDocument
.
DimStyles
.
Add
(
"HIT_DimStyle"
)
print
(
dimAliObj
.
StyleName
)
# 打印標(biāo)注對象的標(biāo)準(zhǔn)樣式名稱
# 系統(tǒng)自帶標(biāo)注樣式 ISO-25
(2) 將特定標(biāo)注樣式設(shè)為當(dāng)前
acad
.
ActiveDocument
.
ActiveDimStyle
=
acad
.
ActiveDocument
.
DimStyles
.
Item
(
"HIT_DimStyle"
)
(3) 設(shè)置尺寸線及尺寸界限
'''
# 說明:以下即(3)~(6)的設(shè)置將生效為<樣式替代>;
# 程序自動畫圖時<樣式替代>不起作用,新添加的標(biāo)注仍采用系統(tǒng)默認(rèn);
# 手動畫圖時<樣式替代>起作用,新添加的標(biāo)注<樣式替代>;
# 設(shè)置永久標(biāo)注樣式見(7),后續(xù)添加詳細(xì)代碼;
# 標(biāo)注樣式替代是對當(dāng)前標(biāo)注樣式中的指定設(shè)置所做的更改;
# 它與在不更改當(dāng)前標(biāo)注樣式的情況下更改尺寸標(biāo)注系統(tǒng)變量等效;
# 使用標(biāo)注樣式替代,無需更改當(dāng)前標(biāo)注樣式便可臨時更改標(biāo)注系統(tǒng)變量。
'''
### ①、超出標(biāo)記
### ②、基線間距
### ③、超出尺寸線
acad
.
ActiveDocument
.
SetVariable
(
"DIMEXE"
,
1.25
)
# 指定尺寸界線超出尺寸線的距離。
### ④、起點(diǎn)偏移量
acad
.
ActiveDocument
.
SetVariable
(
"DIMEXO"
,
0.625
)
# 設(shè)定自圖形中定義標(biāo)注的點(diǎn)到尺寸界線的偏移距離。
(4) 設(shè)置箭頭
### ①、設(shè)定尺寸線箭頭類型。
acad
.
ActiveDocument
.
SetVariable
(
"DIMBLK"
,
"_ARCHTICK"
)
# "_ARCHTICK"表示建筑標(biāo)記
### ②、設(shè)定引線箭頭。
### ③、尺寸線箭頭大小
acad
.
ActiveDocument
.
SetVariable
(
"DIMASZ"
,
2.5
)
(5) 設(shè)置標(biāo)注文字
### ①、文字樣式
acad
.
ActiveDocument
.
SetVariable
(
"DIMTXSTY"
,
"HIT_TxtStyle"
)
### ②、文字高度
acad
.
ActiveDocument
.
SetVariable
(
"DIMTXT"
,
2.5
)
# 設(shè)定當(dāng)前標(biāo)注文字樣式的高度;
# 如果在此選項卡上指定的字樣式具有固定的文字高度,則該高度將替代在此處設(shè)置的文字高度;
# 如果要在此處設(shè)置標(biāo)注文字的高度,請確保將文字樣式的高度設(shè)置為 0。
### ③、文字位置
acad
.
ActiveDocument
.
SetVariable
(
"DIMTAD"
,
1
)
# 控制標(biāo)注文字相對尺寸線的垂直位置;
# 0 表示標(biāo)注文字在尺寸界線之間居中放置;
# 1 表示將標(biāo)注文字放置在尺寸線上方,從尺寸線到文字最低基線的距離為當(dāng)前 DIMGAP 的值。
acad
.
ActiveDocument
.
SetVariable
(
"DIMJUST"
,
0
)
# 水平控制標(biāo)注文字在尺寸線上相對于尺寸界線的水平位置;
# 0 表示將文字置于尺寸線之上,并在尺寸界線之間置中對正。
acad
.
ActiveDocument
.
SetVariable
(
"DIMGAP"
,
0.625
)
# 設(shè)定文字相對尺寸線的位置。
### ④、文字對齊
acad
.
ActiveDocument
.
SetVariable
(
"DIMTIH"
,
0
)
# 控制所有標(biāo)注類型(坐標(biāo)標(biāo)注除外)的標(biāo)注文字在尺寸界線內(nèi)的位置;
# 0 表示將文字與尺寸線對齊;
# 1 表示水平繪制文字。
acad
.
ActiveDocument
.
SetVariable
(
"DIMTOH"
,
0
)
# 控制標(biāo)注文字在尺寸界線外的位置。
(6) 設(shè)置主單位
### ①、線性標(biāo)注精度
acad
.
ActiveDocument
.
SetVariable
(
"DIMDEC"
,
2
)
# 設(shè)定標(biāo)注文字中的小數(shù)位數(shù);
# 2 表示顯示小數(shù)點(diǎn)后三位;
### ②、小數(shù)分隔符
acad
.
ActiveDocument
.
SetVariable
(
"DIMDSEP"
,
"."
)
# "."表示小數(shù)分隔符,也可設(shè)置為","。
acad
.
ActiveDocument
.
SetVariable
(
"DIMADEC"
,
-
1
)
# 設(shè)置角度標(biāo)注精度與線性標(biāo)注精度相同
(7) 設(shè)置永久標(biāo)注樣式
'''
略······
'''
7、多重引線
(1) 創(chuàng)建多重引線
ArrowPnt
=
APoint
(
0
,
0
)
BaselinePnt
=
APoint
(
10
,
10
)
PntsArray
=
np
.
array
(
[
ArrowPnt
,
BaselinePnt
]
)
PntsArray
=
PntsArray
.
reshape
(
1
,
PntsArray
.
shape
[
0
]
*
PntsArray
.
shape
[
1
]
)
[
0
]
MLeaderObj
=
acad
.
model
.
AddMLeader
(
PntsArray
,
0
)
# ArrowPnt 箭頭位置;
# BaselinePnt 基線位置 ;
# 1 表示多重引線的索引號,為正整數(shù)。
(2) 設(shè)置箭頭大小
MLeaderObj
.
ArrowheadSize
=
2
# 指箭頭高度;
# 此項將覆蓋系統(tǒng)變量DIMASZ的值。
(3) 設(shè)置基線長度
MLeaderObj
.
DoglegLength
=
8
(4) 設(shè)置基線間隙
MLeaderObj
.
LandingGap
=
3
# 基線端點(diǎn)到文字起點(diǎn)的距離
(5) 指定文字樣式
MLeaderObj
.
TextStyleName
=
"HIT_TxtStyle"
(6) 指定文字內(nèi)容
MLeaderObj
.
TextString
=
"HIT"
8、尾聲
以上,便是一些關(guān)于 AutoCAD注釋 的基本代碼,因篇幅有限,某些非關(guān)鍵功能未做詳細(xì)介紹,如有疑問,歡迎郵件來詢。
本文部分功能的實(shí)現(xiàn)離不開廣大博友的大力幫助,有些功能看似簡單,但第一次實(shí)現(xiàn)出來卻是相當(dāng)不容易的。
鑒于,相關(guān)示例代碼相對較少,特寫本文,一方面是為自己的階段性學(xué)習(xí)做一個總結(jié),另一方面更是為有需要的人提供多一點(diǎn)參考。
如果您已實(shí)現(xiàn)一些本文未提及的功能,還請在評論區(qū)呈現(xiàn),以便為后續(xù)學(xué)習(xí)者提供更多的幫助。
胸藏文墨懷若谷,腹有詩書氣自華,希望各位都能在知識的pāo子里快樂徜徉。
因個人水平有限,文中難免有所疏漏,還請各位大神不吝批評指正。
最后,祝各位攻城獅們,珍愛生命,保護(hù)發(fā)際線!
本文部分內(nèi)容,源于網(wǎng)絡(luò)!
歡迎大家點(diǎn)贊、評論及轉(zhuǎn)載,轉(zhuǎn)載請注明出處!
為我打call,不如為我打款!
打賞可備注郵箱,本人會將上述代碼發(fā)送給各位土豪!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
