Convert Text Attribute to DIESEL expression with LISP

Convert Text Attribute to DIESEL expression with LISP

Anonymous
Not applicable
1,679 Views
2 Replies
Message 1 of 3

Convert Text Attribute to DIESEL expression with LISP

Anonymous
Not applicable

Hi all,

 

I have following code which updates the attribute value to what I want but, apart from updating the value, I want to change the attribute from being 'static text' to 'field'. Plus I want the code to update the value automatically (i.e. does not ask for user intervention)

 

I would appreciate if someone could help me:

 

(defun c:TXT2FLD ( / s )
(princ "\nSelect Attribute")
    (if (setq s (ssget "_+.:E:S:L" '((0 . "INSERT") (66 . 1))))
		(LM:setattributevalue (ssname s 0) "drawingno." (strcase (menucmd "M=$(substr, $(getvar, dwgname), 1, $(-, $(strlen, $(getvar, dwgname)), 4))")))
    (princ "\nInvalid selection or no selection made!")
	)
(princ)	
)
(defun LM:setattributevalue ( blk tag val / end enx )
    (while
        (and
            (null end)
            (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
        )
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (if (entmod (subst (cons 1 val) (assoc 1 enx) enx))
                (progn
                    (entupd blk)
                    (setq end val)
                )
            )
        )
    )
)

 

0 Likes
Accepted solutions (1)
1,680 Views
2 Replies
Replies (2)
Message 2 of 3

CodeDing
Advisor
Advisor
Accepted solution

@Anonymous,

 

Change this:

(LM:setattributevalue (ssname s 0) "drawingno." (strcase (menucmd "M=$(substr, $(getvar, dwgname), 1, $(-, $(strlen, $(getvar, dwgname)), 4))")))

To this:

(LM:setattributevalue (ssname s 0) "drawingno." "%<\ $(substr, $(getvar, dwgname), 1, $(-, $(strlen, $(getvar, dwgname)), 4))>%")

... when trying to programatically insert fields into areas designated for text, I first create my expression, then copy the text from the "Field Expression" area. In your case, we will not include "AcDiesel" because your attribute can recognize that it is a Diesel Expression.

 

image.png

 

EDIT: Please note that after adding this field, you may need to UPDATE your field for it to take effect! I would recommend adding (command "UPDATEFIELD" ename "") somewhere after adding this attribute...

 

;%<\AcDiesel $(getvar, onlineusername)>%

(defun c:TXT2FLD ( / s )
(princ "\nSelect Attribute")
    (if (setq s (ssget "_+.:E:S:L" '((0 . "INSERT") (66 . 1))))
    (progn
		(LM:setattributevalue (ssname s 0) "drawingno." "%<\ $(substr, $(getvar, dwgname), 1, $(-, $(strlen, $(getvar, dwgname)), 4))>%")
		(command "UPDATEFIELD" (ssname s 0) "")
    );progn
    (princ "\nInvalid selection or no selection made!")
    );if
(princ)	
)
(defun LM:setattributevalue ( blk tag val / end enx )
    (while
        (and
            (null end)
            (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
        )
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (if (entmod (subst (cons 1 val) (assoc 1 enx) enx))
                (progn
                    (entupd blk)
                    (setq end val)
                )
            )
        )
    )
)

 

Hope this helps!

Best,

~DD

0 Likes
Message 3 of 3

CodeDing
Advisor
Advisor

@Anonymous,

 

I don't currently have the knowledge to code that myself, or the time to research it. I will refer you to THIS solution that @Kent1Cooper provided on a post that I contributed to a while back. It is similar to what you are trying to accomplish. I hope you can get what you are looking for by browsing that solution, or by others contributing to this post. Wish I could do more for you!

 

Best,

~DD