- 积分
- 1905
- 明经币
- 个
- 注册时间
- 2022-4-4
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 枫叶棋语 于 2023-2-12 12:23 编辑
# -*- coding: utf-8 -*
import re
import clr
from pycad.runtime import *
from pycad.system import *
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import Clipboard
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
from Autodesk.AutoCAD.EditorInput import *
from pycad.runtime.edx import *
import System
clr.ImportExtensions(System.Linq)
# COM调用函数
def GetOrCreateCOMSession(progid):
try:
from System.Runtime.InteropServices import Marshal
return Marshal.GetActiveObject(progid)
except:
try:
from System import Type, Activator
t = Type.GetTypeFromProgID(progid)
return Activator.CreateInstance(t)
except: pass
def DynamicBlockreferenceOperate(tr,blockref=BlockReference,name=str,width=float,deepth=float):
DynamicProperty=blockref.DynamicBlockReferencePropertyCollection
atts=blockref.AttributeCollection
for Property in DynamicProperty:
if Property.PropertyName=="width": Property.Value=float(width)
if Property.PropertyName=="deepth": Property.Value=float(deepth)
for attid in atts :
attobj=tr.getobject(attid,OpenMode().ForWrite)
if attobj.Tag == r'配电柜编号':
attobj.TextString=name
def ReadRanges():
ExcelApp=GetOrCreateCOMSession("Excel.Application")
SelectedRanges=ExcelApp.Selection
cols=SelectedRanges.Columns.Count
rows=SelectedRanges.Rows.Count
rowlist=list()
collist=list()
for row in range(rows):
rng=SelectedRanges[row+1,1]
if not(rng.EntireColumn.Hidden or rng.EntireRow.Hidden):
rowlist.append(row+1)
for col in range(cols):
rng=SelectedRanges[1,col+1]
if not(rng.EntireColumn.Hidden or rng.EntireRow.Hidden):
collist.append(col+1)
arr=System.Array.CreateInstance(str,len(rowlist),len(collist))
for i in range(len(rowlist)):
for j in range(len(collist)):
arr[i,j]= str(SelectedRanges[rowlist,collist[j]].Value2)
return arr
# if not(rng.EntireColumn.Hidden or rng.EntireRow.Hidden or rng.Value2==None):
# dictfinal[str(SelectedRanges[row,1].Value2)]={"width":str(SelectedRanges[row,2].Value2),"deepth":str(SelectedRanges[row,3].Value2)}
# return dictfinal
def ReadAtt(tr,blockref=BlockReference):
atts=blockref.AttributeCollection
for attid in atts :
attobj=tr.getobject(attid,OpenMode().ForWrite)
if attobj.Tag == r'配电柜编号':
value=attobj.TextString
return value
@command(flags=acrx.CommandFlags.UsePickSet)
def atte(doc):
# 对Excel内容排序
# ExcelApp=GetOrCreateCOMSession("Excel.Application")
# SelectedRanges=ExcelApp.Selection
# rnglist=list()
# for rng in SelectedRanges:
# if not(rng.EntireColumn.Hidden or rng.EntireRow.Hidden or rng.Value2==None):
# rnglist.append(rng)
# ranges=rnglist.OrderBy(lambda x: x.Row).ThenBy(lambda x: x.Column)
with dbtrans(doc) as tr:
res=edx.ssget(mode=':A',filters=(0,'*insert'))
if (not res.ok()) and (len(res.value)) : return
ids= tuple(res)
arr=ReadRanges()
rows= arr.GetLength(0)
blocks=map(lambda x:tr.getobject(x,OpenMode().ForWrite) ,ids)
for blockref,row in zip(blocks,range(rows)):
DynamicBlockreferenceOperate(tr,blockref,arr[row,0],arr[row,1],arr[row,2])
|
|