jtk07 发表于 2022-7-6 09:41:28

删除图层状态multi-drawi

我需要编写一个脚本文件来删除图形中的所有图层状态。
 
不是所有的图层状态在图纸中都是一样的,有什么想法吗?
 
除了给每个人命名之外,你还需要知道把什么放在那里?
 
有没有办法清除所有图层状态?
 
 
另外,我如何确认图层状态还原选项在scrip文件中始终未选中“将特性应用为视口”?

(layerstate-import "I:\\DRAWINGS\\090-0056 LFMP\\Common\\Layer_States\\ELLS.las")
(layerstate-restore "ELLS" nil)

irneb 发表于 2022-7-6 09:45:49

不幸的是,layerstate函数不能与通配符一起使用。因此,您需要找到DWG中的所有状态。不幸的是,没有现成的函数来实现这一点。下面是一个函数,用于为您提供当前图形中的状态列表:
(defun layerstate-list (/ layer0 layerTable layerXDict stateDict state stateList)
(if (and (setq layer0 (tblobjname "LAYER" "0")) ;Get the layer 0's ename
          (setq layer0 (entget layer0)) ;And its DXF codes
          (setq layerTable (cdr (assoc 330 layer0))) ;Get the ename of the layers table
          (setq layerTable (entget layerTable)) ;And its DXF codes
          (setq layerXDict (cdr (assoc 360 layerTable))) ;Get the layer table's XDictionary
          (setq stateDict (dictsearch layerXDict "ACAD_LAYERSTATES")) ;Get the layer states dictionary
   )
   (foreach state stateDict ;Repeat for all states
   (if (= (car state) 3) ;Check if it's a dictionary entry
       (setq stateList (cons (cdr state) stateList)) ;Add it to the list
   )
   )
)
stateList ;Return the generated list
)那么需要做的就是逐步完成它们:
(foreach state (layerstate-list) (layerstate-delete state))

kam1967 发表于 2022-7-6 09:50:24

irneb-你能告诉我在上面的例程中这个代码要插入到哪里吗?我试着把它放在右括号前面,但它给了我一个错误。谢谢
 
然后,需要做的就是逐步完成这些步骤:
(foreach state (layerstate-list) (layerstate-delete state))

Lee Mac 发表于 2022-7-6 09:53:55

嗨,Kam,
 
Irneb在文章末尾提供的代码片段演示了如何调用该文章中的主要代码块。
 
因此,请确保加载了“layerstate列表”代码,然后调用如下内容:
 

(defun c:test ( )
(foreach state (layerstate-list) (layerstate-delete state))
(princ)
)

kam1967 发表于 2022-7-6 09:54:16

谢谢你,李。我曾想过这样做,但后来我认为它必须在函数中才能使用layerstate list变量。我想我应该先测试一下。如果我记得的话,“/”允许它在defun函数之外运行,对吗?
 
谢谢你们俩的帮助!

Lee Mac 发表于 2022-7-6 09:59:18

 
“layerstate列表”符号是一个函数,而不是一个变量。”stateList’是该函数中用于保存列表的变量,该变量由函数返回(因为它是函数中计算的最后一个表达式)。
 
 
否,“/”将“/”后面的符号声明为函数的局部符号,这意味着这些符号存在于该函数的“命名空间”(容器)中。这意味着这些变量在函数使用时所持有的值无法访问,并且不会干扰函数外部。
 
考虑这个例子:
 
当运行时,上述将返回:
 

"Kam"   -   Value held by 'test' outside of function namespace
nil   -   Since 'test' is local to the function, the symbol has no value in the function namespace yet.
"Lee"   -   Value now held by 'test' symbol in the function namespace
"Kam"   -   Value held by 'test' outside of function namespace
         (not changed by the value of 'test' inside the function namespace)

kam1967 发表于 2022-7-6 10:00:38

谢谢你的解释,李。我现在理解了statelist变量。但是,我想知道这行可以放在statelist变量之后,还是只能放在函数外部?
 
(foreach状态(layerstate列表)(layerstate删除状态))
 
我知道layerstate列表现在包含statelist信息。如果我们将行修改为下面的内容,我很好奇为什么它在函数中不起作用。。?我试过了,但没有成功。
 
)
状态列表;返回生成的列表
(foreach状态(statelist)(layerstate delete状态))
)

Lee Mac 发表于 2022-7-6 10:04:12

 
否,否则我们将从其自己的函数定义中递归调用“layerstate list”函数。
 
 
“layerstate list”是指向函数定义的符号,我们调用此函数以返回layerstate列表。
 
 
您可以将“stateList”返回替换为:
 
(foreach state statelist (layerstate-delete state))
(princ)
 
删除由“statelist”变量持有的列表中的所有layerstate,并在末尾添加一个“princ”,以确保干净退出,因为我们现在不需要返回任何内容。

kam1967 发表于 2022-7-6 10:09:34

隐马尔可夫模型。。我尝试将其放在layerstate列表函数例程的末尾,但它不会删除任何层状态。没关系。李,至少其他方法可行。不过谢谢你。
 
)
; 状态列表;返回生成的列表
(foreach state stateList(layerstate delete state))
(普林斯)
)

irneb 发表于 2022-7-6 10:12:32

如果要使用statelist变量,有2个选项:1)修改layerstate list函数并删除statelist变量在其标头中的声明(即,现在将其设置为全局变量,而不是defun的局部变量)。
2) 通过setq将新的statelist变量分配给layerstate list返回的值
 
在这两种情况下,您仍然需要调用defun来计算列表。因此,如果您选择选项1,您仍然需要:
选项2看起来像:
然而,我不明白你为什么要在删除州名后保留州名列表。除非您有其他原因——可能列出已删除到文件中的内容,或者从某些设置中重新创建它们——尽管这可以在foreach lop本身内完成,因此无需将列表保存到变量中。但是,如果需要将列表作为变量,我会每天使用选项2,周日使用两次!尽量避开全局变量——它们只会在你最意想不到的时候引发问题。更不用说,当您使用局部变量时,其他人更清楚地了解发生了什么-全局变量通常在其他地方分配其值,只在稍后阶段在另一个LSP文件(可能)的完全不同部分中使用:非常容易受到人为因素的影响。
页: [1] 2
查看完整版本: 删除图层状态multi-drawi