Lisp to Pull Attribute Text From a Block and Assign to Variable

Lisp to Pull Attribute Text From a Block and Assign to Variable

SYPine
Explorer Explorer
1,157 Views
5 Replies
Message 1 of 6

Lisp to Pull Attribute Text From a Block and Assign to Variable

SYPine
Explorer
Explorer

Below is a code that I have been working on to pull values from the "Title Block" and sets those values to variables so I can export them to an Excel file later on. I can use the below code to edit the values (vla-put-textstring att), but I can't pull them into a variable for some reason. The line that is giving me trouble is "(setq customer (vla-get-textstring att))". (Its not just the customer though, its all of the lines that are trying to assign the value to a variable) I receive the error "bad argument type: FILE "XXXXXXX" (With "XXXXXXX" being the value that is currently in the block attribute). I'm sure its something small that I am overlooking as I am quite the novice, but thank you for your time and input.

 

(setq ss1 (ssget "X" (list '(0 . "INSERT") (cons 2 "TITLEBLOCKTA"))))
(repeat (setq i (sslength ss1))
 (princ "\nGrabbing From BOM")
 (foreach att (vlax-invoke (vlax-ename->vla-object (ssname ss1 (setq i (1- i)))) 'getattributes)
  ;Get Job Number
  (if (= "TB-JOB" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Job Number")
    (setq jobnum (strcase (vla-get-textstring att)))
    (princ "\n" jobnum)
   )
  )
  ;Get Descritpion
  (if (= "TB-TITLE1" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Description")
    (setq description (strcase (vla-get-textstring att)))
    (princ "\n" description)
   )
  )
  ;Get Customer
  (if (= "TB-CLIENT" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Customer")
    (setq customer (vla-get-textstring att))
    (princ "\n" customer)
   )
  )
  ;Get City and State
  (if (= "TB-LOCATION" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Location")
    (setq citystate (strcase (vla-get-textstring att)))
    (princ "\n" citystate)
   )
  )
	;Variables Recieved----------
	;Job Number - jobnum
	;Description - description
	;Customer - customer
	;City and State - citystate (TOWN, STATE ABBR.)
 );foreach
);repeat

 

 

 

0 Likes
Accepted solutions (1)
1,158 Views
5 Replies
Replies (5)
Message 2 of 6

dlanorh
Advisor
Advisor
Accepted solution

@SYPine wrote:

Below is a code that I have been working on to pull values from the "Title Block" and sets those values to variables so I can export them to an Excel file later on. I can use the below code to edit the values (vla-put-textstring att), but I can't pull them into a variable for some reason. The line that is giving me trouble is "(setq customer (vla-get-textstring att))". (Its not just the customer though, its all of the lines that are trying to assign the value to a variable) I receive the error "bad argument type: FILE "XXXXXXX" (With "XXXXXXX" being the value that is currently in the block attribute). I'm sure its something small that I am overlooking as I am quite the novice, but thank you for your time and input.

 

 

 

(setq ss1 (ssget "X" (list '(0 . "INSERT") (cons 2 "TITLEBLOCKTA"))))
(repeat (setq i (sslength ss1))
 (princ "\nGrabbing From BOM")
 (foreach att (vlax-invoke (vlax-ename->vla-object (ssname ss1 (setq i (1- i)))) 'getattributes)
  ;Get Job Number
  (if (= "TB-JOB" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Job Number")
    (setq jobnum (strcase (vla-get-textstring att)))
    (princ "\n" jobnum)
   )
  )
  ;Get Descritpion
  (if (= "TB-TITLE1" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Description")
    (setq description (strcase (vla-get-textstring att)))
    (princ "\n" description)
   )
  )
  ;Get Customer
  (if (= "TB-CLIENT" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Customer")
    (setq customer (vla-get-textstring att))
    (princ "\n" customer)
   )
  )
  ;Get City and State
  (if (= "TB-LOCATION" (strcase (vla-get-tagstring att)))
   (progn
    (princ "\nGrabbing Location")
    (setq citystate (strcase (vla-get-textstring att)))
    (princ "\n" citystate)
   )
  )
	;Variables Recieved----------
	;Job Number - jobnum
	;Description - description
	;Customer - customer
	;City and State - citystate (TOWN, STATE ABBR.)
 );foreach
);repeat

 

 

 

 

 


After you assign each Attribute text string to a variable you are trying to (princ) the value. However the lines are constructed incorrectly and autolisp thinks your are writing to a file

 

 

 

(princ "\n" variable_name)

 

 

 

where variable_name is the pointer to a file. Thus the error you are seeing.

 

The correct format is

 

 

 

(princ (strcat "\n" variable_name))

 

 

 

 

I am not one of the robots you're looking for

Message 3 of 6

ronjonp
Mentor
Mentor

Maybe something like this?

 

;; Is there more than one titleblock?
(if (setq ss1 (ssget "_X" '((0 . "INSERT") (2 . "TITLEBLOCKTA"))))
  (repeat (setq i (sslength ss1))
    (princ "\nGrabbing From BOM")
    (setq o (vlax-ename->vla-object (ssname ss1 (setq i (1- i)))))
    (setq a (mapcar '(lambda (x) (list (vla-get-tagstring att) (vla-get-textstring att)))
		    (vlax-invoke o 'getattributes)
	    )
    )
    ;; this will store the variables in a list 'r'
    (setq r (cons a r))
    (mapcar 'print (car r))
  )
)

 

 

0 Likes
Message 4 of 6

SYPine
Explorer
Explorer

I can't believe I overlooked that 🙃. Worked great and thanks for your help.

0 Likes
Message 5 of 6

dlanorh
Advisor
Advisor

The error message was a dead givaway. Sometimes you're so involved you can't see the wood for the trees. You might find this page useful in hunting down the cause of errors

I am not one of the robots you're looking for

Message 6 of 6

SYPine
Explorer
Explorer

This will be really helpful! I appreciate it

0 Likes