Search code examples
autocadautolisp

Attach Images by coordinates from filename in AutoLISP


In AutoCad I try to batch insert images from a folder.

The filename contains the x and y coordinates.

f.ex. "0053-0068.jpg" -> x = 53000, y = 68000

As input I have a list with all the filenames (path included):

f.ex. "C:\Pictures\0053-0068.jpg"

  1. How do I extract the coordinates from the string?

  2. How do I attach the pictures?

(defun c:loadimages ()
  
  ; [...] load list (implemented and working correctly)

  (foreach img lst ; img = "C:\Pictures\0053-0068.jpg"

    (setq x ?  ) ; x = 53000.0
    (setq y ?  ) ; y = 68000.0
    (setq z 0.0) ; z = const.

    (setq scl 1000.0) ; scl = const.
    (setq ang    0.0) ; ang = const.

    (command "_IMAGE" "_ATTACH" (strcat "img1=" img) '(x y z) scl ang)
  )
  (princ)
)
(princ)

Solution

  • You can find the position of "-" in the file name with vl-string-search (or vl-string-position) to split this string and then use atof.

    (foreach img lst
    (setq base (vl-filename-base img)
      pos  (vl-string-search "-" base)
    )
    (setq x (* 1000. (atof (substr base 1 pos))))
    (setq y (* 1000. (atof (substr base (+ 2 pos)))))
    (setq z 0.0) ; z = const.
    
    (setq scl 1000.0) ; scl = const.
    (setq ang    0.0) ; ang = const.
    
    (command "_IMAGE" "_ATTACH" (strcat "img1=" img) (list x y z) scl ang)
    

    )