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

atributes to excel

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
CADGISCODES
537 Views, 5 Replies

atributes to excel

hello,

 

anybody know lisp code's fragment, how exporti info to excel,

 

such as select line, and export to excel line's length,

 

but I need only lisp code, no data extraction

5 REPLIES 5
Message 2 of 6
hmsilva
in reply to: CADGISCODES

Hi CADGISCODES,

 

the easiest way is to create a .csv file, then open Excel and import the .csv file as text...

i.e.

 

(defun c:test (/ *error* ent file fname i line ss)

  (vl-load-com)

  (defun *error* (msg)
    (if	file
      (close file)
    )
    (cond ((not msg))
	  ((member msg '("Function cancelled" "quit / exit abort")))
	  ((princ (strcat "\n** Error: " msg " ** ")))
    );; cond
    (princ)
  );;; *error*


  (if
    (and (not (prompt "\n<<< Select the lines >>>"))
	 (setq ss (ssget '((0 . "LINE"))))
	 (setq fname (getfiled "Enter the file name: "
			       (strcat (getvar "dwgprefix")
				       (vl-filename-base (getvar "dwgname"))
			       )
			       "csv"
			       1
		     )
	 )
    );; and
     (progn
       (setq file (open fname "w"))
       (setq line "Layer\tlength\tStart Point\tEnd Point")
       (write-line line file)
       (repeat (setq i (sslength ss))
	 (setq ent (entget (ssname ss (setq i (1- i)))))
	 (setq
	   line	(strcat
		  (cdr (assoc 8 ent))
		  "\t"
		  (rtos	(distance (cdr (assoc 10 ent)) (cdr (assoc 11 ent)))
			2
			3
		  )
		  "\t"
		  (rtos (car (cdr (assoc 10 ent))) 2 3)
		  ","
		  (rtos (car (cddr (assoc 10 ent))) 2 3)
		  ","
		  (rtos (car (cdddr (assoc 10 ent))) 2 3)
		  "\t"
		  (rtos (car (cdr (assoc 11 ent))) 2 3)
		  ","
		  (rtos (car (cddr (assoc 11 ent))) 2 3)
		  ","
		  (rtos (car (cdddr (assoc 11 ent))) 2 3)
		)
	 )
	 (write-line line file)
       );; repeat
       (close file)
     );; progn
  );; if
  (*error* nil)
  (princ)
);;; test

 

HTH

Henrique

EESignature

Message 3 of 6
CADGISCODES
in reply to: hmsilva

Thank you hmsilva
Message 4 of 6
Lee_Mac
in reply to: CADGISCODES

The code could become slightly more concise through the use of mapcar, e.g.:

 

(defun c:test ( / *error* des enx ept idx sel spt txt )

    (defun *error* ( msg )
        (if (= 'file (type des)) (close des))
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
    
    (if (and (setq sel (ssget '((0 . "LINE"))))
             (setq txt (getfiled "Create output file" "" "txt" 1))
        )
        (if (setq des (open txt "w"))
            (progn
                (write-line "Layer\tLength\tSx\tSy\tSz\tEx\tEy\tEz" des)
                (repeat (setq idx (sslength sel))
                    (setq enx (entget (ssname sel (setq idx (1- idx))))
                          spt (cdr (assoc 10 enx))
                          ept (cdr (assoc 11 enx))
                    )
                    (write-line
                        (strcat (cdr (assoc 8 enx))
                            (apply 'strcat
                                (mapcar '(lambda ( x ) (strcat "\t" (rtos x 2 3)))
                                    (cons (distance spt ept) (append spt ept))
                                )
                            )
                        )
                        des
                    )
                )
                (setq des (close des))
            )
            (princ (strcat "\nUnable to open \"" txt "\" for writing."))
        )
    )
    (princ)
)
Message 5 of 6
CADGISCODES
in reply to: Lee_Mac

it's good, thanks ;]
Message 6 of 6
hmsilva
in reply to: CADGISCODES


@CADGISCODES wrote:
Thank you hmsilva

You're welcome, CADGISCODES

 


@Lee_Mac wrote:
The code could become slightly more concise through the use of mapcar, e.g.:



More concise, indeed... 🙂

 

Henrique

 

EESignature

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

Post to forums  

Autodesk Design & Make Report

”Boost