求个大佬帮忙看下红色处的代码,研究找不到原因
本帖最后由 惜惜2 于 2024-10-7 11:00 编辑<blockquote>(setq filename (vl-filename-base (getvar "dwgname")))
(if (setq space-pos (vl-string-search " " filename))
(progn
(setq drawing-number (substr filename 1 space-pos))
(setq part-name (substr filename (+ space-pos 2)))
(setq ss (ssget "x" '((0 . "INSERT"))))
(repeat (setq n (sslength ss))
(setq blockref (SSNAME SS (setq n (1- n))))
(setq blockref (vlax-ename->vla-object blockref))
(if (= (vla-get-HasAttributes blockref) :vlax-true)
(foreach attrib
(VLAX-SAFEARRAY->LIST
(VLAX-VARIANT-VALUE (vla-getattributes blockref))
)
(cond((= (vla-get-tagstring attrib) "客户图号")
(vla-put-textstring attrib drawing-number)
)
((= (vla-get-tagstring attrib) "零件名称")
(vla-put-textstring attrib part-name)
)
((and(= (vla-get-tagstring attrib) "设计日期")
(equal (vla-get-textstring attrib) ""))
(vla-put-textstring attrib (menucmd "m=$(edtime,$(getvar,DATE),yyyy-mo-dd)"))))
)
((= (vla-get-tagstring attrib) "产品编码")
(let ((product-code-value (vla-get-textstring attrib))))
((= (vla-get-tagstring attrib) "客户")
(let ((extracted-code(substr product-code 2 3))))
(vla-put-textstring attrib extracted-code)
)
)
)
)
)
)
)
(princ)
(defun c:test ()
(setq e1 nil)
(setq filename (vl-filename-base (getvar "dwgname")))
(if (setq space-pos (vl-string-search " " filename))
(progn
(setq drawing-number (substr filename 1 space-pos))
(setq part-name (substr filename (+ space-pos 2)))
(setq ss (ssget "x" '((0 . "INSERT"))))
(repeat (setq n (sslength ss))
(setq blockref (SSNAME SS (setq n (1- n))))
(setq blockref (vlax-ename->vla-object blockref))
(if (= (vla-get-HasAttributes blockref) :vlax-true)
(progn
(foreach attrib
(VLAX-SAFEARRAY->LIST
(VLAX-VARIANT-VALUE (vla-getattributes blockref))
)
(if (= (vla-get-tagstring attrib) "产品编码")
(setq e1 (vla-get-textstring attrib))
)
)
(setq E2 (substr E1 2 3))
(foreach attrib
(VLAX-SAFEARRAY->LIST
(VLAX-VARIANT-VALUE (vla-getattributes blockref))
)
(setq attribname (vla-get-tagstring attrib))
(cond ((wcmatch attribname "客户图号")
(vla-put-textstring attrib drawing-number)
)
((wcmatch attribname "零件名称")
(vla-put-textstring attrib part-name)
)
((and
(wcmatch attribname "设计日期")
(= (vla-get-textstring attrib) "")
)
(vla-put-textstring
attrib
(menucmd "m=$(edtime,$(getvar,DATE),yyyy-mo-dd)")
)
)
((wcmatch attribname "客户")
(vla-put-textstring attrib E2)
)
)
)
)
)
)
)
)
(princ)
) 本帖最后由 惜惜2 于 2024-10-10 14:22 编辑
ljpnb 发表于 2024-10-7 17:34
是的,先取值
平时很少用到属性块,也学习了一下(setq E1 nil)
(setq E3 nil)
(setq filename (vl-filename-base (getvar "dwgname")))
(if (setq space-pos (vl-string-search " " filename))
(progn
(setq drawing-number (substr filename 1 space-pos))
(setq part-name (substr filename (+ space-pos 2)))
(setq ss (ssget "x" '((0 . "INSERT"))))
(repeat (setq n (sslength ss))
(setq blockref (SSNAME SS (setq n (1- n))))
(setq blockref (vlax-ename->vla-object blockref))
(if (= (vla-get-HasAttributes blockref) :vlax-true)
(progn
(foreach attrib
(VLAX-SAFEARRAY->LIST
(VLAX-VARIANT-VALUE (vla-getattributes blockref))
)
(if(or (= (vla-get-tagstring attrib) "产品编码")
(= (vla-get-tagstring attrib) "编号"))
(setq e1 (vla-get-textstring attrib))
)
)
(setq E2 (substr E1 2 3))
(foreach attrib
(VLAX-SAFEARRAY->LIST
(VLAX-VARIANT-VALUE (vla-getattributes blockref))
)
(if(and(= (vla-get-tagstring attrib) "设计日期")(not (equal (vla-get-textstring attrib) "")))
(setq E3 (vl-prin1-to-string (vla-get-textstring attrib)))
)
)
(if E3
(setq e3-length (length E3))
)
(if E3
(if (>= e3-length 8)
(setq E4 (strcat (substr E3 0 4) "-" (substr E3 4 2) "-" (substr E3 6 2)))
(setq E4 nil))
)
(foreach attrib
(VLAX-SAFEARRAY->LIST
(VLAX-VARIANT-VALUE (vla-getattributes blockref))
)
(cond ((= (vla-get-tagstring attrib) "客户图号")
(vla-put-textstring attrib drawing-number)
)
((= (vla-get-tagstring attrib) "零件名称")
(vla-put-textstring attrib part-name)
)
((and(= (vla-get-tagstring attrib) "设计日期")
(equal (vla-get-textstring attrib) ""))
(if E4
(vla-put-textstring attrib E4)
(vla-put-textstring attrib (menucmd "m=$(edtime,$(getvar,DATE),yyyy-mo-dd)")))
)
((= (vla-get-tagstring attrib) "客户")
(vla-put-textstring attrib E2)
)
)
)
)
)
)
)
)
(princ)
有时间帮忙看下这个吗,假设属性值不为空时,赋值给E3并转换格式再赋值给E4这里有问题 kozmosovia 发表于 2024-10-7 11:01
(let ((extracted-code (substr product-code 2 3)))
这个不是alisp的函数
(setq filename (vl-filename-base (getvar "dwgname")))
(if (setq space-pos (vl-string-search " " filename))
(progn
(setq drawing-number (substr filename 1 space-pos))
(setq part-name (substr filename (+ space-pos 2)))
(setq ss (ssget "x" '((0 . "INSERT"))))
(repeat (setq n (sslength ss))
(setq blockref (SSNAME SS (setq n (1- n))))
(setq blockref (vlax-ename->vla-object blockref))
(if (= (vla-get-HasAttributes blockref) :vlax-true)
(foreach attrib
(VLAX-SAFEARRAY->LIST
(VLAX-VARIANT-VALUE (vla-getattributes blockref))
)
(cond ((= (vla-get-tagstring attrib) "客户图号")
(vla-put-textstring attrib drawing-number)
)
((= (vla-get-tagstring attrib) "零件名称")
(vla-put-textstring attrib part-name)
)
((and(= (vla-get-tagstring attrib) "设计日期")
(equal (vla-get-textstring attrib) ""))
(vla-put-textstring attrib (menucmd "m=$(edtime,$(getvar,DATE),yyyy-mo-dd)")))))
)
((= (vla-get-tagstring attrib) "产品编码")
(setq E1 (vla-get-textstring attrib))
)
((= (vla-get-tagstring attrib) "客户")
(setq E2 (substr E1 2 3))
(vla-put-textstring attrib E2)
)
)
)
)
)
)
)
(princ)
我提取变量赋值也是不成功大佬再帮忙看看 (substr "abcdef" 2 3)
返回:"bcd"
(substr "abcdef" 2 2)
返回:"bc"
是这个意思么 被承包的东子 发表于 2024-9-20 08:06
(substr "abcdef" 2 3)
返回:"bcd"
是的,是因为我的变量值没有加引号? 本帖最后由 惜惜2 于 2024-10-7 11:01 编辑
有大佬帮忙看下吗?这段不成功,谢谢
((= (vla-get-tagstring attrib) "产品编码")
(let ((product-code-value (vla-get-textstring attrib))))
((= (vla-get-tagstring attrib) "客户")
(let ((extracted-code(substr product-code 2 3))))
(vla-put-textstring attrib extracted-code)
本帖最后由 kozmosovia 于 2024-10-7 11:06 编辑
(let ((extracted-code (substr product-code 2 3)))
这个不是alisp的函数
另外除非产品编码属性定义在客户之前,否则需要循环两次属性才能修改客户内容:第一次获得需要改的内容,第二次修改。
本帖最后由 惜惜2 于 2024-10-7 11:09 编辑
kozmosovia 发表于 2024-10-7 11:01
(let ((extracted-code (substr product-code 2 3)))
这个不是alisp的函数
产品编码属性定义在客户之前,另外使用字段是如何获取特定内容的,要求是第二到三位数,我看是获取了全部的值。用字段更新过去,再使用substr去筛选客户标签一次? (vla-put-textstring attrib (menucmd "m=$(edtime,$(getvar,DATE),yyyy-mo-dd)")))))这句右括号多了3个 本帖最后由 惜惜2 于 2024-10-7 15:20 编辑
ljpnb 发表于 2024-10-7 14:59
(vla-put-textstring attrib (menucmd "m=$(edtime,$(getvar,DATE),yyyy-mo-dd)")))))这句右括号多了3个
删掉以后还是提取不成功,是什么原因,我这个也是在你写的基础上面改的,前面的都正常。我测试发现E1赋值不成功,返回的是E2为空的值
(if (setq space-pos (vl-string-search " " filename))
文件名带有空格才会执行后面的代码