-
I'm trying to batch create layouts by using Code: @command(flags=Ap.CmdFlags.USEPICKSET|Ap.CmdFlags.REDRAW)
def batchAddLayout():
try:
if not _check_layout('demo'):
print("请先创建demo布局")
return
Ed.Editor.selectPrevious()
selects = Ed.Editor.selectImplied()
if selects[0] == Ed.PromptStatus.eOk:
ids = selects[1].objectIds()
# del selects
if len(ids) == 0:
print("未获取到选择图框")
return
layoutName_prefix = Ed.Editor.getString("\n布局前缀: ",)
if layoutName_prefix[0] != Ed.PromptStatus.eNormal or layoutName_prefix[1] == '':
print("error")
return
layoutName_prefix = layoutName_prefix[1]
layoutName_serial = Ed.Editor.getInteger("\n布局起始序号: ",)
if layoutName_serial[0]!= Ed.PromptStatus.eNormal:
print("error")
return
layoutName_serial = int(layoutName_serial[1])
# db.transactionManager().startTransaction()
print(ids)
# ids = ssres[1].objectIds()
center_points = []
for i in range(len(ids)):
id = ids[i]
ent = Db.Entity(id, Db.OpenMode.kForRead)
if not ent.isKindOf(Db.Polyline.desc()):
print(f"所选对象类型:{ent.isA().name()}, 跳过")
continue
# del ent
pl = Db.Polyline.cast(ent)
p1 = pl.getPoint2dAt(0)
p2 = pl.getPoint2dAt(2)
center_points.append(Ge.Point2d((p1.x+p2.x)/2,(p1.y+p2.y)/2))
# del ent, pl, p1, p2
if len(center_points) == 0:
print("未获取到有效图框")
return
print(f"共{len(center_points)}个有效图框")
sorted_center_points = sorted(center_points, key=lambda p: (p.x, p.y))
db = Db.curDb()
lm = Db.LayoutManager()
for i in range(len(sorted_center_points)):
inc_layoutName = f"{layoutName_prefix}{layoutName_serial + i}"
if lm.layoutExists(inc_layoutName):
print(f"布局${inc_layoutName}已存在")
return
else:
print(f"新建布局名:${inc_layoutName}")
# del inc_layoutName
lm.dispose()
for i in range(len(sorted_center_points)):
center_point = sorted_center_points[i]
print(center_point)
inc_layoutName = f"{layoutName_prefix}{layoutName_serial + i}"
print(inc_layoutName)
_add_layout(db, center_point, inc_layoutName)
db.dispose()
except Exception as err:
traceback.print_exc(err)
def _add_layout(db:Db.Database,center_point:Ge.Point2d,layoutName:str):
"""
基于 'demo' 布局克隆一个新布局,并设置新布局中视口的中心坐标。
Args:
center_point (Ge.Point2d): 新布局中视口要设置的中心坐标。
layoutName (str): 新布局的名称。
"""
lm = Db.LayoutManager()
if not lm.layoutExists('demo'):
print('demo布局不存在')
return
# 将当前布局设置为 'demo' 布局
lm.setCurrentLayout('demo')
# 将当前布局设置回 'Model' 布局
lm.setCurrentLayout("Model")
# 以可写模式打开 'demo' 布局
demoLayout = Db.Layout(lm.getLayouts()['demo'])
demoLayout.upgradeOpen()
# 基于 'demo' 布局克隆一个新布局
lm.cloneLayout(demoLayout, layoutName,lm.countLayouts(),db)
# 检查新创建的布局是否存在
if not lm.layoutExists(layoutName):
print('新创建的布局不存在')
return
# 将当前布局设置为新创建的布局
lm.setCurrentLayout(layoutName)
# 以可写模式打开新创建的布局
newLayout = Db.Layout(lm.getLayouts()[layoutName])
# 获取新布局中的视口数组
pViewportArray = newLayout.getViewportArray()
# 打印视口数组
print(pViewportArray)
# 如果视口数组为空,则直接返回
if not pViewportArray:
print('视口数组为空')
return
pViewport = Db.Viewport(pViewportArray[1])
pViewport.upgradeOpen()
# 设置视口的中心坐标
pViewport.setViewCenter(center_point)
# 关闭视口
pViewport.close()
pViewport.dispose()
# 将当前布局设置回 'Model' 布局
lm.setCurrentLayout("Model")
newLayout.close()
newLayout.dispose()
# 关闭 'demo' 布局
demoLayout.close()
demoLayout.dispose()
lm.dispose() Environment
How to fix it?😢 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hi How are you measuring memory? |
Beta Was this translation helpful? Give feedback.
-
definitely the cache is causing your memory to run up, same code, same drawing import traceback
from pyrx import Ap, Db, Ed, Ge, Gi, command
@command()
def doit():
try:
db = Db.curDb()
man = Db.LayoutManager()
token = man.setupForLayouts(db)
for i in range(2, 99):
man.copyLayout("Layout1", "Layout{}".format(i), db)
for name, _ in man.getLayouts(db).items():
man.setCurrentLayout(name, db)
man.clearSetupForLayouts(token)
except Exception as err:
traceback.print_exc(err) Normally, you would not use dispose() in Python, for example, there’s only one Layout Manager in AutoCAD, so calling dispose on it does nothing. If the Database is from it a current drawing, Database.dispose() does nothing |
Beta Was this translation helpful? Give feedback.
-
Thanks, Dan. |
Beta Was this translation helpful? Give feedback.
The memory that the module uses is 16 bytes for the C++ pointer and like 40 bytes in python so at 5300mp, you would need 94,642,857 items in python 😀
I’m guessing your seeing the cache, try turning it off