View Full Version : Select a block by name in paperspace
A.E.D.
2015-09-29, 01:56 PM
Hi
I need some lisp assistance with selecting a block that has attributes.
Currently my function works with "entsel" and I get what I need but I would like to change that specific line (se code) to select block by name instead of prompting user for selection.
(defun C:GetVal ( / blk tag val enx)
(setq blk (car(entsel))) ;<--- change this line
(setq tag "TAG")
(while
(and
(null val)
(= "ATTRIB"
(cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))
)
)
(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
(setq val (cdr (assoc 1 enx)))
)
)
(princ val)
);C:GetVal
Things to know:
There is only one block per layout with that given name.
The caller function is looping though each layout.
Here is a section of the caller function
(foreach lay (layoutlist)
(setvar 'CTab lay)
(setq n (atoi (C:GetVall)))
(repeat n
(COMMAND (Somecommand)
)
)
parts code originated from http://forums.augi.com/showthread.php?150072-Get-Attribute-Value-from-Block-Tag&p=1231319&viewfull=1#post1231319
(http://forums.augi.com/showthread.php?150072-Get-Attribute-Value-from-Block-Tag&p=1231319&viewfull=1#post1231319)
I'm so lost and would appreciate any kind of help
Thanks
!
BlackBox
2015-09-29, 03:15 PM
Welcome to AUGI. :beer:
Building on the code you posted a link for, and *if* I understood you correctly, I believe this is what you're after:
(defun c:FOO (/ LM:GetAttributeValue ss eName)
(defun LM:GetAttributeValue (blk tag / val enx)
(while
(and
(null val)
(= "ATTRIB"
(cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))
)
)
(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
(setq val (cdr (assoc 1 enx)))
)
)
)
(if (setq ss (ssget "_x" '((0 . "INSERT") (2 . "YourBlockNameHere") (66 . 1))))
(repeat (1+ (sslength ss))
(setq eName (ssname ss 0))
(prompt (strcat "\n[Layout] : "
(cdr (assoc 410 (entget eName)))
", [Attribute Value] : "
(LM:GetAttributeValue eName "YourTagNameHere")
)
)
(if (< 1 (sslength ss))
(ssdel eName ss)
)
)
)
(graphscr)
(textscr)
(princ)
)
A.E.D.
2015-09-29, 05:36 PM
Welcome to AUGI. :beer:
Building on the code you posted a link for, and *if* I understood you correctly, I believe this is what you're after:
(defun c:FOO (/ LM:GetAttributeValue ss eName)
(defun LM:GetAttributeValue (blk tag / val enx)
(while
(and
(null val)
(= "ATTRIB"
(cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))
)
)
(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
(setq val (cdr (assoc 1 enx)))
)
)
)
(if (setq ss (ssget "_x" '((0 . "INSERT") (2 . "YourBlockNameHere") (66 . 1))))
(repeat (1+ (sslength ss))
(setq eName (ssname ss 0))
(prompt (strcat "\n[Layout] : "
(cdr (assoc 410 (entget eName)))
", [Attribute Value] : "
(LM:GetAttributeValue eName "YourTagNameHere")
)
)
(if (< 1 (sslength ss))
(ssdel eName ss)
)
)
)
(graphscr)
(textscr)
(princ)
)
Hi Blackbox thank you for your reply!
I'm sorry to say that this is not quite what I was thinking of. My bad for not being clear enough. Although your code taught me, as a lisp beginner, more about lisp which I'm grateful of, thanks.
About the looping, I already have a caller function that calls this "C:GetVal" function within a loop, so "C:GetVal" isn't suppose to do any looping by it self.
What I want "C:GetVal" to do is to only return that attribute value from that one block with that specific name in the current layout only.
If you look at my first post I highlighted the line that I wanted to replace.
So basically I want to replace
(setq blk (car(entsel))) with something like
"select the block with the blockname "myBlock" in current layout and assign it to variable blk"
Appreciate your help.
edit:
I would appreciate if you could help me with my current code (1:st post) and continue from there if possible.
peter
2015-09-29, 08:37 PM
P=
(setq ssSelections (ssget "x" (list (cons 0 "insert")(cons 2 "myblock")(cons 410 (getvar "ctab")))))
(setq blk (ssname ssSelections 0))
A.E.D.
2015-10-01, 09:42 AM
P=
(setq ssSelections (ssget "x" (list (cons 0 "insert")(cons 2 "myblock")(cons 410 (getvar "ctab")))))
(setq blk (ssname ssSelections 0))
Thank you very much! Now my lisp works perfectly.
To give back to the community I'll share my lisp, which I have assembled together piece by piece from the help of the communities.
Thanks goes out to peter, blackbox, Tharwat, Lee Mac (this dude is like everywhere), irneb (at cadtutor.net) and probably some more, sorry
Description: To export tables from one or several layouts to a csv file. Later on, if you wish, you can create your own excel file that does 3 things; 1st imports this csv data and format it with comma separation, 2nd consolidates the data, 3 sort the consolidated data.
The "myBlock" is the block containing the "myTag" attribute. There should be one "myBlock" per layout if you wish to export drawing by regarding to how many instances there are, think of "myBlock" as drawing specification block and "myTag" is the attribute number in that block showing how many instances of the element should be created. You can set to export each layout once only, look at code (set n to 1).
Call "AED_TableLaoutExport" to export current layout once only.
Call "AED_TableLaoutExport_AllLayouts" to export all layouts and repeat for each instances (unless you set n to 1, look for red comment text in code)
(vl-load-com)
(defun ReadList (lst /)
(cond
((= (type lst) 'Str) (ReadList (read (strcat "(" (vl-string-translate ",;" " " lst) ")"))))
((= (type lst) 'Sym) (eval lst))
((= (type lst) 'List) (mapcar (function ReadList) lst))
(t lst)
)
)
(defun SelectObjects (pt filter / ss n lst)
(if pt
(progn
(setq pt (ReadList pt))
(if (= (type pt) 'List)
(cond
((and (>= (length pt) 2) (vl-every (function (lambda (i) (member (type i) '(Int Real)))) pt))
(setq n (list (/ (getvar 'ViewSize) (* (getvar 'PickBox) 80.0)) 0.0)
n (cons (car n) n)
ss (ssget "C" (mapcar '- pt n) (mapcar '+ pt n) filter)
)
)
((vl-every (function
(lambda (i)
(and (= (type i) 'List) (vl-every (function (lambda (j) (member (type j) '(Int Real)))) i))
)
)
pt
)
(cond
((= (length pt) 1) (setq lst (SelectObjects (car pt) filter)))
((= (length pt) 2) (setq ss (ssget "C" (car pt) (cadr pt) filter)))
(t (setq ss (ssget "CP" pt filter)))
)
)
)
)
)
(setq ss (ssget "X" filter))
)
(if ss
(progn (setq n (sslength ss)) (while (>= (setq n (1- n)) 0) (setq lst (cons (ssname ss n) lst))))
)
lst
)
(defun TableExport (pt fname prefix / lst eo f row col str)
(if (and (setq lst (SelectObjects pt '((0 . "ACAD_TABLE"))))
(setq f (open fname "a"))
)
(progn
(foreach en lst
(setq eo (vlax-ename->vla-object en)
row -1
)
(while (< (setq row (1+ row)) (vla-get-Rows eo))
(setq col -1
str ""
)
(while (< (setq col (1+ col)) (vla-get-Columns eo))
(setq str (strcat str ",\"" (vla-GetText eo row col) "\""))
)
(setq str (strcat ",\"" (vla-get-Handle eo) "\"" str "\n"))
(if prefix
(setq str (strcat ",\"" prefix "\"" str))
)
(princ (substr str 2) f)
)
)
(close f)
)
)
)
(defun C:AED_TableLaoutExport ()
;Line below is an alternative export, to export each layout in own csv (excel) file. Change path here
;(COMMAND (TableExport '((0 0) (20000 20000)) (strcat (strcat "C://Table//" (strcat (getvar 'DWGNAME)(strcat "--" (getvar 'CTAB))) ".csv" )) (getvar 'CTAB)) )
;Line below my preffered export, to export and combine all layouts in one csv (excel) file. Change path here
(COMMAND (TableExport '((0 0) (20000 20000)) "C://Table//temp.csv" (getvar 'CTAB)) )
(command)
);DT:AED_TableLaoutExport
;; Prints All Layout Tabs and names the new PDFs with the name of the
;; drawing followed by the layout tab name.
;;
;; Adjust the paper size and .ctb file as needed.
;;
;; The PDFs will be placed in the folder where the drawing resides
;;
(defun C:AED_TableLaoutExport_AllLayouts ()
(foreach lay (layoutlist)
(setvar 'CTab lay)
(command "zoom" "all")
(setq n (C:AED_GetNumber)) ; Don't have "myBlock"? set n to 1 to export each layout only a single time
(setq c 0)
(repeat n
(setq c (1+ c))
(COMMAND (C:AED_TableLaoutExport))
(print (strcat (strcat "Layout: " lay) (strcat " - Export: " (itoa c)) (strcat " of " (itoa n))))
)
)
)
(defun C:AED_GetNumber ( / blk tag val enx)
(setq ssSelections (ssget "x" (list (cons 0 "insert")(cons 2 "myBlock")(cons 410 (getvar "ctab"))))) ; Change "myBlock" to name of your block
(setq blk (ssname ssSelections 0))
(setq tag "myTag") ;Change "myTag" to the name of the attribute
(while
(and
(null val)
(= "ATTRIB"
(cdr (assoc 0 (setq enx (entget (setq blk (entnext blk))))))
)
)
(if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
(setq val (cdr (assoc 1 enx)))
)
)
(princ (atoi val))
);C:AED_GetNumber
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.