本文不定期更新!
目 錄
- 0、相關文章
- 1、連接及庫導入
- 2、常規選擇
- (1) 屏幕拾取
- (2) 選擇過定點圖元
- (3) 多邊形框選
- (4) 全選
- 3、快速選擇
- (1) 濾出0圖層上的所有圓
- 4、尾聲
0、相關文章
- Python pyautocad庫 使用簡介
- Python AutoCAD 系統設置
- Python AutoCAD 圖層
- Python AutoCAD 繪圖
- Python AutoCAD 修改
- Python AutoCAD 塊組
- Python AutoCAD 注釋
- Python AutoCAD 文件
- Python AutoCAD 選擇集
1、連接及庫導入
#!/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
from
pyautocad
import
APoint
import
win32com
.
client
import
numpy
as
np
import
pythoncom
import
win32com
.
client
acad
=
Autocad
(
create_if_not_exists
=
True
)
acad
.
prompt
(
"Hello! AutoCAD from pyautocad."
)
print
(
acad
.
doc
.
Name
)
2、常規選擇
"創建名稱為SS1的選擇集"
try
:
acad
.
ActiveDocument
.
SelectionSets
.
Item
(
"SS1"
)
.
Delete
(
)
except
:
print
(
"Delete selection failed"
)
slt
=
acad
.
ActiveDocument
.
SelectionSets
.
Add
(
'SS1'
)
(1) 屏幕拾取
slt
.
SelectOnScreen
(
)
print
(
"請在屏幕拾取圖元,以Enter鍵結束"
)
obj
=
slt
[
0
]
print
(
obj
.
ObjectID
)
print
(
slt
)
(2) 選擇過定點圖元
pnt
=
APoint
(
0
,
0
)
slt
.
SelectAtPoint
(
pnt
)
obj
=
slt
[
0
]
print
(
obj
.
StartPoint
)
# 當點pnt存在于不止一個圖元上時
# 該選擇方法僅能選擇出其中的一個圖元
# 多個選擇不出來
(3) 多邊形框選
"選擇PointsList內各點圍成的邊界內全部圖元"
PointsList
=
[
APoint
(
0
,
0
)
,
APoint
(
15000
,
0
)
,
APoint
(
15000
,
15000
)
,
APoint
(
0
,
15000
)
,
APoint
(
0
,
0
)
]
PointsList
=
np
.
array
(
[
j
for
i
in
pnts
for
j
in
i
]
,
dtype
=
np
.
float
)
slt
.
SelectByPolygon
(
6
,
PointsList
)
# acSelectionSetWindowPolygon = 6
obj
=
slt
[
0
]
print
(
obj
.
StartPoint
)
(4) 全選
slt
.
Select
(
5
)
# acSelectionSetAll = 5
obj
=
slt
[
0
]
print
(
obj
.
layer
)
print
(
obj
.
StartPoint
)
3、快速選擇
(1) 濾出0圖層上的所有圓
" ********************************* ---- 創建圖元 ---- ********************************* "
acad
=
Autocad
(
create_if_not_exists
=
True
)
acad
.
prompt
(
"Hello! Autocad from Python."
)
print
(
acad
.
doc
.
Name
)
[
pnt1
,
pnt2
,
pnt3
,
pnt4
,
pnt5
,
pnt6
]
=
[
APoint
(
40
,
40
)
,
APoint
(
500
,
500
)
,
APoint
(
300
,
200
)
,
APoint
(
600
,
200
)
,
APoint
(
700
,
200
)
,
APoint
(
100
,
0
)
]
# 創建點的另一種方式
pnt
=
np
.
array
(
[
50
,
50
,
0
]
,
dtype
=
np
.
float
)
acad
.
model
.
AddPoint
(
pnt
)
LineObj
=
acad
.
model
.
AddLine
(
pnt1
,
pnt2
)
CircleObj
=
acad
.
model
.
AddCircle
(
pnt3
,
100
)
ArcObj
=
acad
.
model
.
AddArc
(
pnt4
,
50
,
0
,
1.57
)
EllObj
=
acad
.
model
.
AddEllipse
(
pnt5
,
pnt6
,
0.5
)
" ********************************** ---- 選擇集 ---- ********************************** "
## ke1078 歡迎到千島湖 千云山莊
acad
=
win32com
.
client
.
Dispatch
(
"AutoCAD.Application"
)
doc
=
acad
.
ActiveDocument
doc
.
Utility
.
Prompt
(
"hello world\n"
)
mp
=
doc
.
ModelSpace
def
vtpt
(
x
,
y
,
z
=
0
)
:
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
)
# 添加名稱為“SS1”的選擇集
try
:
doc
.
SelectionSets
.
Item
(
"SS1"
)
.
Delete
(
)
except
:
print
(
"Delete selection failed"
)
slcn
=
doc
.
SelectionSets
.
Add
(
"SS1"
)
# 過濾出在0圖層上的圓
ftyp
=
[
0
,
8
]
ftdt
=
[
"Circle"
,
"0"
]
filterType
=
win32com
.
client
.
VARIANT
(
pythoncom
.
VT_ARRAY
|
pythoncom
.
VT_I2
,
ftyp
)
filterData
=
win32com
.
client
.
VARIANT
(
pythoncom
.
VT_ARRAY
|
pythoncom
.
VT_VARIANT
,
ftdt
)
slcn
.
Select
(
0
,
vtpt
(
0
,
0
)
,
vtpt
(
2000
,
2000
)
,
filterType
,
filterData
)
print
(
slcn
.
count
,
"過濾后的選擇集"
)
for
i
in
slcn
:
print
(
i
.
ObjectName
)
" ************************************ ---- END ---- ************************************ "
4、尾聲
以上,便是關于 AutoCAD選擇集 的一些基本代碼,因篇幅有限,某些非關鍵功能未做詳細介紹,如有疑問,歡迎郵件來詢。
本文部分功能的實現離不開廣大博友的大力幫助,有些功能看似簡單,但第一次實現出來卻是相當不容易的。
鑒于,相關示例代碼相對較少,特寫本文,一方面是為自己的階段性學習做一個總結,另一方面更是為有需要的人提供多一點參考。
如果您已實現一些本文未提及的功能,還請在評論區呈現,以便為后續學習者提供更多的幫助。
胸藏文墨懷若谷,腹有詩書氣自華,希望各位都能在知識的pāo子里快樂徜徉。
因本人野生學習Python,水平確實有限,文中難免有所疏漏,還請各位大神不吝批評指正。
最后,祝各位攻城獅們,珍愛生命,保護發際線!
本文部分內容,源于網絡!
歡迎大家點贊、評論及轉載,轉載請注明出處!
為我打call,不如為我打款!
打賞可備注郵箱,本人會將上述代碼發送給各位土豪!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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