Author Topic: HELP WITH A LISP  (Read 8852 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
HELP WITH A LISP
« on: January 17, 2017, 11:39:06 AM »
Hi , i use this code to export attribiute points from my drawings. I select with window all the points and export them to a file  P,X,Y,Z   or P,X,Y    or    P,X,Y,D

The only problem is that all points are radom in the file not in the correct possition. For example we hane 10,5,2,3,9,T1,S3,1 etc .The correct possition is 1,2,3,5,9,S3,T1. I know  i can do this with MS Excell or with a notepad , but i need something faster. I don't know if it possoble to be done with lisp.

The code is

Code - Auto/Visual Lisp: [Select]
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                             ;;
  3. ;;                                      PXYZD                                  ;;
  4. ;;                                                                             ;;
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6.  
  7. (defun c:PXYZD ( / *error* del des ent idx lst obj ord out sel )
  8.  
  9.     (defun *error* ( msg )
  10.         (if (= 'file (type des))
  11.             (close des)
  12.         )
  13.         (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
  14.             (princ (strcat "\nError: " msg))
  15.         )
  16.         (princ)
  17.     )
  18.  
  19.     (setq ord '("POINT" POINT-X POINT-Y "ELEV" "DESC")
  20.           out  (LM:uniquefilename (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))) ".crd")
  21.           del  ","
  22.     )
  23.     (if (setq sel (ssget '((0 . "INSERT") (66 . 1))))
  24.         (if (setq des (open out "w"))
  25.             (progn
  26.                 (repeat (setq idx (sslength sel))
  27.                     (setq ent (ssname sel (setq idx (1- idx)))
  28.                           obj (vlax-ename->vla-object ent)
  29.                     )
  30.                     (setq lst
  31.                         (append
  32.                             (mapcar '(lambda ( a b ) (cons a (rtos b)))
  33.                                '(point-x point-y point-z)
  34.                                 (trans (cdr (assoc 10 (entget ent))) ent 0)
  35.                             )
  36.                             (mapcar '(lambda ( x ) (cons (strcase (vla-get-tagstring x)) (vla-get-textstring x)))
  37.                                 (append
  38.                                     (vlax-invoke obj 'getattributes)
  39.                                     (vlax-invoke obj 'getconstantattributes)
  40.                                 )
  41.                             )
  42.                         )
  43.                     )
  44.                     (if (setq lst (vl-remove 'nil (mapcar '(lambda ( x ) (cdr (assoc x lst))) ord)))
  45.                         (write-line (LM:lst->str lst del) des)
  46.                     )
  47.                 )
  48.                 (setq des (close des))
  49.             )
  50.             (princ (strcat "\nUnable to open file: \"" out "\" for writing."))
  51.         )
  52.     )
  53.     (princ)
  54. )                
  55.  
  56. ;; List to String  -  Lee Mac
  57. ;; Concatenates each string in a list, separated by a given delimiter
  58.  
  59. (defun LM:lst->str ( lst del )
  60.     (if (cdr lst)
  61.         (strcat (car lst) del (LM:lst->str (cdr lst) del))
  62.         (car lst)
  63.     )
  64. )
  65.  
  66. ;; Unique Filename  -  Lee Mac
  67. ;; Returns a unique filename for a given path & file extension
  68.  
  69. (defun LM:uniquefilename ( pth ext / fnm tmp )
  70.     (if (findfile (setq fnm (strcat pth ext)))
  71.         (progn
  72.             (setq tmp 1)
  73.             (while (findfile (setq fnm (strcat pth "(" (itoa (setq tmp (1+ tmp))) ")" ext))))
  74.         )
  75.     )
  76.     fnm
  77. )
  78.  
  79.  
  80. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  81. ;;                                                                             ;;
  82. ;;                                      PXYZ                                   ;;
  83. ;;                                                                             ;;
  84. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  85.  
  86. (defun c:PXYZ ( / *error* del des ent idx lst obj ord out sel )
  87.  
  88.     (defun *error* ( msg )
  89.         (if (= 'file (type des))
  90.             (close des)
  91.         )
  92.         (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
  93.             (princ (strcat "\nError: " msg))
  94.         )
  95.         (princ)
  96.     )
  97.  
  98.     (setq ord '("POINT" POINT-X POINT-Y "ELEV" )
  99.           out  (LM:uniquefilename (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))) ".crd")
  100.           del  ","
  101.     )
  102.     (if (setq sel (ssget '((0 . "INSERT") (66 . 1))))
  103.         (if (setq des (open out "w"))
  104.             (progn
  105.                 (repeat (setq idx (sslength sel))
  106.                     (setq ent (ssname sel (setq idx (1- idx)))
  107.                           obj (vlax-ename->vla-object ent)
  108.                     )
  109.                     (setq lst
  110.                         (append
  111.                             (mapcar '(lambda ( a b ) (cons a (rtos b)))
  112.                                '(point-x point-y point-z)
  113.                                 (trans (cdr (assoc 10 (entget ent))) ent 0)
  114.                             )
  115.                             (mapcar '(lambda ( x ) (cons (strcase (vla-get-tagstring x)) (vla-get-textstring x)))
  116.                                 (append
  117.                                     (vlax-invoke obj 'getattributes)
  118.                                     (vlax-invoke obj 'getconstantattributes)
  119.                                 )
  120.                             )
  121.                         )
  122.                     )
  123.                     (if (setq lst (vl-remove 'nil (mapcar '(lambda ( x ) (cdr (assoc x lst))) ord)))
  124.                         (write-line (LM:lst->str lst del) des)
  125.                     )
  126.                 )
  127.                 (setq des (close des))
  128.             )
  129.             (princ (strcat "\nUnable to open file: \"" out "\" for writing."))
  130.         )
  131.     )
  132.     (princ)
  133. )                
  134.  
  135. ;; List to String  -  Lee Mac
  136. ;; Concatenates each string in a list, separated by a given delimiter
  137.  
  138. (defun LM:lst->str ( lst del )
  139.     (if (cdr lst)
  140.         (strcat (car lst) del (LM:lst->str (cdr lst) del))
  141.         (car lst)
  142.     )
  143. )
  144.  
  145. ;; Unique Filename  -  Lee Mac
  146. ;; Returns a unique filename for a given path & file extension
  147.  
  148. (defun LM:uniquefilename ( pth ext / fnm tmp )
  149.     (if (findfile (setq fnm (strcat pth ext)))
  150.         (progn
  151.             (setq tmp 1)
  152.             (while (findfile (setq fnm (strcat pth "(" (itoa (setq tmp (1+ tmp))) ")" ext))))
  153.         )
  154.     )
  155.     fnm
  156. )
  157.  
  158.  
  159.  
  160. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  161. ;;                                                                             ;;
  162. ;;                                      PXY                                    ;;
  163. ;;                                                                             ;;
  164. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  165.  
  166. (defun c:PXY ( / *error* del des ent idx lst obj ord out sel )
  167.  
  168.     (defun *error* ( msg )
  169.         (if (= 'file (type des))
  170.             (close des)
  171.         )
  172.         (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
  173.             (princ (strcat "\nError: " msg))
  174.         )
  175.         (princ)
  176.     )
  177.  
  178.     (setq ord '("POINT" POINT-X POINT-Y )
  179.           out  (LM:uniquefilename (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))) ".crd")
  180.           del  ","
  181.     )
  182.     (if (setq sel (ssget '((0 . "INSERT") (66 . 1))))
  183.         (if (setq des (open out "w"))
  184.             (progn
  185.                 (repeat (setq idx (sslength sel))
  186.                     (setq ent (ssname sel (setq idx (1- idx)))
  187.                           obj (vlax-ename->vla-object ent)
  188.                     )
  189.                     (setq lst
  190.                         (append
  191.                             (mapcar '(lambda ( a b ) (cons a (rtos b)))
  192.                                '(point-x point-y point-z)
  193.                                 (trans (cdr (assoc 10 (entget ent))) ent 0)
  194.                             )
  195.                             (mapcar '(lambda ( x ) (cons (strcase (vla-get-tagstring x)) (vla-get-textstring x)))
  196.                                 (append
  197.                                     (vlax-invoke obj 'getattributes)
  198.                                     (vlax-invoke obj 'getconstantattributes)
  199.                                 )
  200.                             )
  201.                         )
  202.                     )
  203.                     (if (setq lst (vl-remove 'nil (mapcar '(lambda ( x ) (cdr (assoc x lst))) ord)))
  204.                         (write-line (LM:lst->str lst del) des)
  205.                     )
  206.                 )
  207.                 (setq des (close des))
  208.             )
  209.             (princ (strcat "\nUnable to open file: \"" out "\" for writing."))
  210.         )
  211.     )
  212.     (princ)
  213. )                
  214.  
  215. ;; List to String  -  Lee Mac
  216. ;; Concatenates each string in a list, separated by a given delimiter
  217.  
  218. (defun LM:lst->str ( lst del )
  219.     (if (cdr lst)
  220.         (strcat (car lst) del (LM:lst->str (cdr lst) del))
  221.         (car lst)
  222.     )
  223. )
  224.  
  225. ;; Unique Filename  -  Lee Mac
  226. ;; Returns a unique filename for a given path & file extension
  227.  
  228. (defun LM:uniquefilename ( pth ext / fnm tmp )
  229.     (if (findfile (setq fnm (strcat pth ext)))
  230.         (progn
  231.             (setq tmp 1)
  232.             (while (findfile (setq fnm (strcat pth "(" (itoa (setq tmp (1+ tmp))) ")" ext))))
  233.         )
  234.     )
  235.     fnm
  236. )
  237.  
  238. [code]
  239.  

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: HELP WITH A LISP
« Reply #1 on: January 17, 2017, 12:18:46 PM »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: HELP WITH A LISP
« Reply #2 on: January 17, 2017, 12:29:26 PM »
Thanks Lee for the heads up.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

pedroantonio

  • Guest
Re: HELP WITH A LISP
« Reply #3 on: January 17, 2017, 02:48:00 PM »
Any ideas ?

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: HELP WITH A LISP
« Reply #4 on: January 17, 2017, 03:47:13 PM »
HELP WITH THE SUBJECT LINE!
Please tell me how the subject of this thread and the question relate. Please tell me how this subject line will help another user find this thread if they have a similar problem.

Re: HELP WITH A LISP
I don't think I understand fully what the output is you are trying to sort looks like but you can use simple Unix tools to do this and a thousand other different combinations of what you may want/need (There are a lot of tools created for just operating on delimited text files). -e.g. AWK and SORT for example.

https://www.theswamp.org/index.php?topic=8476.msg492563#msg492563
http://www.linuxquestions.org/questions/linux-general-1/how-to-use-awk-to-sort-243177/
http://www.linuxfocus.org/English/September1999/article103.html
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ChrisCarlson

  • Guest
Re: HELP WITH A LISP
« Reply #5 on: January 17, 2017, 03:50:39 PM »
You simply need to sort the data, before passing it to "LM:lst->str". Although you probably won't get much help in this topic. Changing the command is one thing but completely deleting the attribution, not cool.

pedroantonio

  • Guest
Re: HELP WITH A LISP
« Reply #6 on: January 17, 2017, 05:42:10 PM »
for example the export coordinates are

Code: [Select]
3,293649.870,4225552.726,
5,293657.899,4225547.326,
7,293650.257,4225543.748,
6,293653.935,4225545.454,
8,293647.561,4225542.282,
2,293639.053,4225547.491,
9,293644.538,4225540.620,
1,293642.102,4225539.320,
s2,293635.766,4225553.648,
s1,293637.563,4225539.929,
s3,293655.810,4225554.488,
s4,293662.935,4225556.538,
4,293654.540,4225554.975,

The numbers are random. Is any command or function to put them in the correct possition ??

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: HELP WITH A LISP
« Reply #7 on: January 17, 2017, 06:43:17 PM »

Perhaps try something like
Code - Auto/Visual Lisp: [Select]
  1. (vl-sort lst '<)


or Google
AutoLisp sort list of strings
https://www.google.com.au/webhp?source=search_app&gfe_rd=cr&ei=WFtzVuOUI-zN8AefzYCIAg&gws_rd=ssl#q=AutoLisp+sort+list+of+strings

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

mailmaverick

  • Bull Frog
  • Posts: 493
Re: HELP WITH A LISP
« Reply #8 on: January 17, 2017, 11:25:44 PM »
Sorting is not a difficult task, kindly attach your AutoCAD file.

pedroantonio

  • Guest
Re: HELP WITH A LISP
« Reply #9 on: January 18, 2017, 05:02:36 AM »
I dont know how to use this command

Code - Auto/Visual Lisp: [Select]
  1. (vl-sort lst '<)
  2.  


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: HELP WITH A LISP
« Reply #10 on: January 18, 2017, 08:22:15 AM »
Then look it up in the help files.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: HELP WITH A LISP
« Reply #11 on: January 18, 2017, 08:29:16 AM »
Then look it up in the help files.

Or make use my hard work and click the function name in the code block?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: HELP WITH A LISP
« Reply #12 on: January 18, 2017, 09:01:09 AM »
Then look it up in the help files.
... and make your apology for Source & attribution: http://bit.ly/11XjtTZ

ronjonp

  • Needs a day job
  • Posts: 7527
Re: HELP WITH A LISP
« Reply #13 on: January 18, 2017, 09:42:59 AM »
I dont know how to use this command

Code - Auto/Visual Lisp: [Select]
  1. (vl-sort lst '<)
  2.  
There are hyperlinks in the post that give an explanation  ... does not get much easier than that to TRY and figure it out.  ::)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: HELP WITH A LISP
« Reply #14 on: January 18, 2017, 10:05:16 AM »
Thank you, ronjonp!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org