votuanh Posted November 15, 2018 Posted November 15, 2018 I am writing lisp to paste object length to attribute with tag "LEN". I wrote this code: (defun c :ptv (/ reb mrk fm) (setq fm "%lu2%pr0") (setq reb (vla-get-objectid (vlax-ename->vla-object (car (nentsel "\nSelect the Rebar: "))))) (setq reb (strcat "%<\\AcObjProp Object(%<\\_ObjId "(itoa reb)">%).Length \\f " fm ">%")) (setq len "LEN") (setq reb (list (cons len reb))) (setq mrk (entget (car (nentsel "\nPick the Mark: ")))) (setq mrk (vlax-ename->vla-object (cdr (assoc 330 mrk)))) (LM:setattributevalues mrk reb) (command "regen") );defun (defun LM:setattributevalues ( blk lst / itm ) (foreach att (vlax-invoke blk 'getattributes) (if (setq itm (assoc (vla-get-tagstring att) lst)) (vla-put-textstring att (cdr itm)) ) ) ) When I run this lisp I get that error when I click on block. But this code run perfectly when I click on the attribute in the block. Please help me to correct this code. Quote
Emmanuel Delay Posted November 16, 2018 Posted November 16, 2018 (edited) Not sure what you're copying, but if the problem is just pasting to a block (attribute "LEN"), then try this (defun c:ptv (/ reb mrk fm len) (setq fm "%lu2%pr0") (setq reb (vla-get-objectid (vlax-ename->vla-object (car (nentsel "\nSelect the Rebar: "))))) (setq reb (strcat "%<\\AcObjProp Object(%<\\_ObjId "(itoa reb)">%).Length \\f " fm ">%")) (setq len "LEN") (setq reb (list (cons len reb))) ;; These two lines let the client select a subentity (nentsel). ;;(setq mrk (entget (car (nentsel "\nPick the Mark: ")))) ;; This second line then searches for the parent (of the attribute) ;;(setq mrk (vlax-ename->vla-object (cdr (assoc 330 mrk)))) ;; instead this line lets the user select the block itself (entsel) instead of (nentsel) (setq mrk (vlax-ename->vla-object (car (entsel "\nPick the Mark: ")))) (LM:setattributevalues mrk reb) (command "regen") ) ;defun (defun LM:setattributevalues ( blk lst / itm ) (foreach att (vlax-invoke blk 'getattributes) (if (setq itm (assoc (vla-get-tagstring att) lst)) (vla-put-textstring att (cdr itm)) ) ) ) Edited November 16, 2018 by Emmanuel Delay 1 Quote
dlanorh Posted November 16, 2018 Posted November 16, 2018 (edited) 11 hours ago, votuanh said: I am writing lisp to paste object length to attribute with tag "LEN". I wrote this code: (defun c :ptv (/ reb mrk fm) (setq fm "%lu2%pr0") (setq reb (vla-get-objectid (vlax-ename->vla-object (car (nentsel "\nSelect the Rebar: "))))) (setq reb (strcat "%<\\AcObjProp Object(%<\\_ObjId "(itoa reb)">%).Length \\f " fm ">%")) (setq len "LEN") (setq reb (list (cons len reb))) (setq mrk (entget (car (nentsel "\nPick the Mark: ")))) (setq mrk (vlax-ename->vla-object (cdr (assoc 330 mrk)))) (LM:setattributevalues mrk reb) (command "regen") );defun (defun LM:setattributevalues ( blk lst / itm ) (foreach att (vlax-invoke blk 'getattributes) (if (setq itm (assoc (vla-get-tagstring att) lst)) (vla-put-textstring att (cdr itm)) ) ) ) When I run this lisp I get that error when I click on block. But this code run perfectly when I click on the attribute in the block. Please help me to correct this code. The obvious answer is : You are not passing a block with attributes and/or that you are not passing a dotted pair list of tags and values to the (LM:setattributevalues) function. See Lee's documentation below ;; Set Attribute Values - Lee Mac ;; Sets attributes with tags found in the association list to their associated values. ;; blk - [vla] VLA Block Reference Object ;; lst - [lst] Association list of ((<tag> . <value>) ... ) ;; Returns: nil (defun LM:vl-setattributevalues ( blk lst / itm ) (foreach att (vlax-invoke blk 'getattributes) (if (setq itm (assoc (vla-get-tagstring att) lst)) (vla-put-textstring att (cdr itm)) ) ) ) Edited November 16, 2018 by dlanorh 1 Quote
Lee Mac Posted November 17, 2018 Posted November 17, 2018 There are many issues with your current code - you'll need to: Account for null user input for selection of both the source object & destination Check whether the selected source object has a Length property Check whether the selected destination object is an attributed block Account for 32-bit & 64-bit OS & applications when obtaining the ObjectID It's probably easier for you to create a custom program to call my Length Field function. 1 Quote
votuanh Posted November 19, 2018 Author Posted November 19, 2018 Thank you guys for your help. The Emmanuel Delay’s code is working well. And thank Lee Mac for your subprogram. On 11/16/2018 at 3:39 PM, Emmanuel Delay said: Not sure what you're copying, but if the problem is just pasting to a block (attribute "LEN"), then try this (defun c:ptv (/ reb mrk fm len) (setq fm "%lu2%pr0") (setq reb (vla-get-objectid (vlax-ename->vla-object (car (nentsel "\nSelect the Rebar: "))))) (setq reb (strcat "%<\\AcObjProp Object(%<\\_ObjId "(itoa reb)">%).Length \\f " fm ">%")) (setq len "LEN") (setq reb (list (cons len reb))) ;; These two lines let the client select a subentity (nentsel). ;;(setq mrk (entget (car (nentsel "\nPick the Mark: ")))) ;; This second line then searches for the parent (of the attribute) ;;(setq mrk (vlax-ename->vla-object (cdr (assoc 330 mrk)))) ;; instead this line lets the user select the block itself (entsel) instead of (nentsel) (setq mrk (vlax-ename->vla-object (car (entsel "\nPick the Mark: ")))) (LM:setattributevalues mrk reb) (command "regen") ) ;defun (defun LM:setattributevalues ( blk lst / itm ) (foreach att (vlax-invoke blk 'getattributes) (if (setq itm (assoc (vla-get-tagstring att) lst)) (vla-put-textstring att (cdr itm)) ) ) ) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.