Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Quick select and export

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
Anonymous
1169 Views, 15 Replies

Quick select and export

Hi,

Does anyone know how to quick select all the blocks from a specific layer (for instance XPTO) and export them along with their attributes, exactly as we do by quick selecting them and then, through AutoCAD Express -> Attribute Export Info, but in a script...

Thanks in advance.

15 REPLIES 15
Message 2 of 16
JustoAg
in reply to: Anonymous

Try this: (setq ss (ssget "X" (list (cons 0 "INSERT") (cons 8 "YOUR_LAYERNAME") ))) To create a selection set with all the blks in certain layer. Then: (bns_attout "myfile.txt" ss) which is the function form of AutoCAD Express -> Attribute Export (you have to have it already loaded, of course) At te end you'll have a file (myfile.txt) with the info formated like the express routine would do. HTH, Justo.
Message 3 of 16
Hallex
in reply to: Anonymous

Change selection filter to your suit:

{code}

;;==================================== ATO.LSP========================================;;


;; Attribute export to data file
;; fixo () 2012 * all rights released

;;local defun
(defun lst-str (lst del / st strout)
  ;;; by Fatty T.O.H. 2004 
 (setq strout (car lst))
 (foreach st (cdr lst)
  (setq strout (strcat strout del st))
 )
 strout
)




(defun C:ATO(/ att_line data datafile data_line dirty en filename
		    header header_lines i new_line obj records sset stab txtline)
(if
  (setq	sset (ssget (list (cons 0 "INSERT")
			  (cons 66 1)
			  (cons 410 (getvar "CTAB")))))
   (progn
     (setq data nil)
     (setq filename (getfiled "Enter output filename:"
			      (getvar "dwgprefix")
			      "csv"	;  <--- change file extension to suit (may be txt, dat etc...)
			      1))
     (setq datafile (open filename "W"))

     (while (setq en (ssname sset 0))
       (setq obj (vlax-ename->vla-object en))
       (setq txtline nil
	     att_line nil)
       (setq header_lines
	      (append header_lines
		      (list (strcat "'"
				    (vla-get-handle obj)
				    "\t"
				    (vla-get-effectivename obj)))))
       (foreach	att  (append
		       (vlax-invoke obj 'getattributes)
		       (cond ((vl-catch-all-error-p
				(setq stab (vlax-invoke obj 'getconstantattributes))))
			     (stab))
		       )
	 (if (not (member (vla-get-tagstring att) dirty))
	   (setq dirty (cons (vla-get-tagstring att) dirty)))
	 (setq att_line	(cons (cons (vla-get-tagString att) (vla-get-textString att))
			      att_line)))
       (setq txtline (append (reverse att_line) txtline))
       (setq data (cons txtline data))
       (ssdel en sset)

       )

     (setq data (reverse data))

     (setq records nil)

     (foreach record  data

       (setq new_line nil)

       (foreach	tag  dirty

	 (setq new_line	(cons (if (assoc tag record)
				(assoc tag record)
				(cons tag "<>"))
			      new_line))
	 )
       (setq records (append records (list (mapcar 'cdr (reverse new_line)))))

       )



     (setq data_line "Handle\tBlockName")
     (foreach tag dirty
       (setq data_line (strcat data_line (strcat "\t" tag)))
	     )
    
     (write-line data_line datafile)


     (setq i 0)
     (while (setq header (nth i header_lines))
       (setq data_line (strcat (strcat header "\t") (lst-str (nth i records) "\t")))
       (write-line data_line datafile)
       (setq i (1+ i))
       )

     (close datafile)
     )
   )
  (princ (strcat "\n\t --- Output file: " "\"" filename"\"" " created.   ---"))
(princ)
      )
(princ "\n\t --- Start command with \"ATO\"   ---")
(princ)
 (or(vl-load-com)
  (princ))
;;==================================== code end ========================================;;

 

{code}

 

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 16
Anonymous
in reply to: JustoAg

I'm afraid that it doesn't work...

Never the less, here is my LISP file:

 

(defun C:exta02 (/ss )
	(setq ss (ssget "X" (list (cons 0 "INSERT") (cons 8 "LAYER_BLOCK") )))
	(bns_attout "block.txt" ss)
)

 And it returns the error:

 

; error: An error has occurred inside the *error* functionAutoCAD variable 

setting rejected: "osmode" nil

 

I've also tried the following code but still the same error

 

(defun C:exta02 (/ss filename)
	(setq ss (ssget "X" (list (cons 0 "INSERT") (cons 8 "LAYER_BLOCK") )))
	(setq filename (getfiled "Enter output filename:"
		(getvar "dwgprefix") "txt" 1)
	)
	(bns_attout filename ss)
)

 

Message 5 of 16
Anonymous
in reply to: Hallex

It does work, however it asks me to select the objects...

As I mentioned, I need it to select them for me, to be precise blocks named "BLOCK_A" inside layer "LAYER_BLOCK".

Message 6 of 16
hmsilva
in reply to: Anonymous

With soma changes...

 (defun C:exta0 (/ ss filename)
  (setq ss (ssget "X"
    (list (cons 0 "INSERT")
   (cons 8 "LAYER_BLOCK")
   (cons 2 "BLOCK_A")
    )
    )
  )
  (setq filename (getfiled "Enter output filename:"
      (getvar "dwgprefix")
      "txt"
      1
   )
  )
  (bns_attout filename ss)
)

 

will do the trick

henrique

EESignature

Message 7 of 16
hmsilva
in reply to: hmsilva

My mistake

 

(code)

(defun C:exta02 (/ ss filename)
  (load "attout")
  (setq ss (ssget "X"
    (list (cons 0 "INSERT")
   (cons 8 "LAYER_BLOCK")
   (cons 2 "BLOCK_A")
    )
    )
  )
  (setq filename (getfiled "Enter output filename:"
      (getvar "dwgprefix")
      "txt"
      1
   )
  )
  (bns_attout filename ss)
)

(code)

 

henrique

EESignature

Message 8 of 16
Anonymous
in reply to: hmsilva

Somehow it might work...

I say this because although I entered a tst01.txt name for file at Desktop folder, when it comes to open the file I just can't find it.

 

Anyway, I picked your code

 

(setq ss (ssget "X"
    (list (cons 0 "INSERT")
   (cons 8 "LAYER_BLOCK")
   (cons 2 "BLOCK_A")
    )
    )

 and inserted it at Hallex 'ato.lsp' and now it works like a charm as follows:

 

;;==================================== ATO.LSP========================================;;


;; Attribute export to data file
;; fixo () 2012 * all rights released

;;local defun
(defun lst-str (lst del / st strout)
  ;;; by Fatty T.O.H. 2004 
 (setq strout (car lst))
 (foreach st (cdr lst)
  (setq strout (strcat strout del st))
 )
 strout
)




(defun C:ato(/ att_line data datafile data_line dirty en filename
		    header header_lines i new_line obj records sset stab txtline)
(if
  (setq	sset (ssget "X"
    (list (cons 0 "INSERT")
   (cons 8 "LAYER_BLOCK")
   (cons 2 "BLOCK_A")
    )
    ))
   (progn
     (setq data nil)
     (setq filename (getfiled "Enter output filename:"
			      (getvar "dwgprefix")
			      "txt"	;  <--- change file extension to suit (may be txt, dat etc...)
			      1))
     (setq datafile (open filename "W"))

     (while (setq en (ssname sset 0))
       (setq obj (vlax-ename->vla-object en))
       (setq txtline nil
	     att_line nil)
       (setq header_lines
	      (append header_lines
		      (list (strcat "'"
				    (vla-get-handle obj)
				    "\t"
				    (vla-get-effectivename obj)))))
       (foreach	att  (append
		       (vlax-invoke obj 'getattributes)
		       (cond ((vl-catch-all-error-p
				(setq stab (vlax-invoke obj 'getconstantattributes))))
			     (stab))
		       )
	 (if (not (member (vla-get-tagstring att) dirty))
	   (setq dirty (cons (vla-get-tagstring att) dirty)))
	 (setq att_line	(cons (cons (vla-get-tagString att) (vla-get-textString att))
			      att_line)))
       (setq txtline (append (reverse att_line) txtline))
       (setq data (cons txtline data))
       (ssdel en sset)

       )

     (setq data (reverse data))

     (setq records nil)

     (foreach record  data

       (setq new_line nil)

       (foreach	tag  dirty

	 (setq new_line	(cons (if (assoc tag record)
				(assoc tag record)
				(cons tag "<>"))
			      new_line))
	 )
       (setq records (append records (list (mapcar 'cdr (reverse new_line)))))

       )



     (setq data_line "Handle\tBlockName")
     (foreach tag dirty
       (setq data_line (strcat data_line (strcat "\t" tag)))
	     )
    
     (write-line data_line datafile)


     (setq i 0)
     (while (setq header (nth i header_lines))
       (setq data_line (strcat (strcat header "\t") (lst-str (nth i records) "\t")))
       (write-line data_line datafile)
       (setq i (1+ i))
       )

     (close datafile)
     )
   )
  (princ (strcat "\n\t --- Output file: " "\"" filename"\"" " created.   ---"))
(princ)
      )
(princ "\n\t --- Start command with \"ATO\"   ---")
(princ)
 (or(vl-load-com)
  (princ))
;;==================================== code end ========================================;;

 

 

However if there is a simple solution for this I'll be glad to test it.

Message 9 of 16
hmsilva
in reply to: Anonymous

For me works...

 

(code)

 

(defun C:exta02 (/ ss filename)
  (load "attout")
  (setq ss (ssget "X"
    (list (cons 0 "INSERT")
   (cons 8 "LAYER_BLOCK")
   (cons 2 "BLOCK_A")
    )
    )
  )
  (setq filename (getfiled "Enter output filename:"
      (getvar "dwgprefix")
      "txt"
      1
   )
  )
  (bns_attout filename ss)
)

 

(end code)

 

the file was created successfully with both codes...

EESignature

Message 10 of 16
Anonymous
in reply to: hmsilva

Ooopsss!

Looking for it at wrong folder.

Thanks.

Message 11 of 16
Anonymous
in reply to: Hallex

I picked up Hallex code to try something out, and I've just realized that the attributes are coming out in the wrong order.

Any ideas?

Message 12 of 16
zimmerroo
in reply to: Hallex

Is it possible to modify this LISP routine to just simply "select" all attributes in a drawing with the same name?  But not export?

 

I'm looking to streamline the "QSELECT" command.  I'd like to select all "DOORTAGS" blocks in one page to move them around as a group.

 

Any thoughts?

 

a.Z.

Message 13 of 16
marko_ribar
in reply to: zimmerroo

Untested...

 

(defun c:selblksbytag ( / tag ss i stab bl )

  (vl-load-com)

  (initget 1)
  (setq tag (getstring t "\nSpecify TAG (case sensitive) : "))
  (setq ss (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (if (= (getvar 'cvport) 1) (cons 410 (getvar 'ctab)) (cons 410 "Model")))))
  (repeat (setq i (sslength ss))
    (if (not (vlax-property-available-p (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i))))) 'Path))
      (foreach att (append (vlax-invoke obj 'getattributes) (cond ( (vl-catch-all-error-p (setq stab (vlax-invoke obj 'getconstantattributes))) ) (stab) ))
        (if (= (vla-get-tagstring att) tag)
          (setq bl (cons (vlax-vla-object->ename obj) bl))
        )
      )
    )
  )
  (setq ss (ssadd))
  (foreach b bl
    (ssadd b ss)
  )
  (sssetfirst nil ss)
  (princ)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 14 of 16
zimmerroo
in reply to: marko_ribar

Hi Mark -

 

THank you.  Tested the lisp routine, but no luck.

Message 15 of 16
zimmerroo
in reply to: marko_ribar

Is there a way to create a lisp routine that would run the following:

 

1. QSELECT

2. Select "Block Reference" by default.

3. Select "Name" by dafault for the Properties

 

Then all I'd have to do is select the block name and be off and running?  

Message 16 of 16
marko_ribar
in reply to: zimmerroo

Look I had forgot ab obj variable for localizing and TAG specification is always uppercase... That's why it failed... Try now...

 

(defun c:selblksbytag ( / tag ss i obj stab bl )

  (vl-load-com)

  (initget 1)
  (setq tag (getstring t "\nSpecify TAG : "))
  (setq ss (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (if (= (getvar 'cvport) 1) (cons 410 (getvar 'ctab)) (cons 410 "Model")))))
  (repeat (setq i (sslength ss))
    (if (not (vlax-property-available-p (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i))))) 'Path))
      (foreach att (append (vlax-invoke obj 'getattributes) (cond ( (vl-catch-all-error-p (setq stab (vlax-invoke obj 'getconstantattributes))) ) (stab) ))
        (if (= (vla-get-tagstring att) (strcase tag))
          (setq bl (cons (vlax-vla-object->ename obj) bl))
        )
      )
    )
  )
  (setq ss (ssadd))
  (foreach b bl
    (ssadd b ss)
  )
  (sssetfirst nil ss)
  (princ)
)

The name is Marko, not Mark...

Marko Ribar, d.i.a. (graduated engineer of architecture)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost