Jump to content

AutoLISP for Inserting Multiple Raster Images with Filename Attached


TonyBaloni

Recommended Posts

This nice little subroutine below by Alan Thompson places multiple images on AutoCAD.  And it works beautifully.

 

But what I also need to do is to add the name of the file next to each image (see the attached picture).

 

Any ideas where to start?  I am very new to AutoLISP.

 

Thank you

 

Tony

 

(defun c:ImageIn (/ lst a b fin)
  ;; Alan J. Thompson, 05.10.10
  ;; DosLib required
(vl-load-com)
  (if (setq lst (dos_getfilem "Select images to insert:" (getvar "dwgprefix") ""))
    ((lambda (layers)
       (foreach file (vl-sort (cdr lst) (function >))
         (if fin
           (progn
             (vla-GetBoundingBox (car fin) 'a 'b)
             (setq b (vlax-safearray->list b))
             (vl-cmdf "_.image" "_attach" (strcat (car lst) file) (list 0. (1+ (cadr b))) "" "")
           )
           (vl-cmdf "_.image" "_attach" (strcat (car lst) file) '(0. 0. 0.) "" "")
         )
         (vla-put-layer
           (car (setq fin (cons (vlax-ename->vla-object (entlast)) fin)))
           (vla-get-name (vla-add layers (vl-filename-base file)))
         )
       )
     )
      (vla-get-layers
        (cond (*AcadDoc*)
              ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
        )
      )
    )
  )
  (princ)
)

Drawing1-Model.png

Link to comment
Share on other sites

I did find this online (see below)....but it returns "error: ADS request error"

 

Maybe I can use this after inserting the images...to put the name of the image next to it.

 

If anyone out there knows how to fix it (?)

 

 

(defun c:ImageNameLabel (/ s h i e)

  (if (and (setq s (ssget '((0 . "IMAGE"))))
           (setq h (getvar 'textsize))
           )
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (entmake (list (cons 0 "TEXT")
                     (assoc 10 (entget e))
                     (cons 40 h)
                     (cons 1 (getpropertyvalue  e "ImageName"))))))
  (princ)
)

Link to comment
Share on other sites

I get error "; error: bad DXF group: nil"

 

Here's the revised code (maybe I didn't edit correctly?):

 

(defun c:ImageNameLabel (/ s h i e)

  (if (and (setq s (ssget '((0 . "IMAGE"))))
           (setq h (getvar 'textsize))
           )
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (entmake (list (cons 0 "TEXT")
                     (assoc 10 (entget e))
                     (cons 40 h)
                     (assoc 1 (entget e)))))))
  (princ)
)

Link to comment
Share on other sites

You can change the value of the variable 'g' in the following routine to adjust the gap distance between image & text.

(defun c:test (/ s h i e g l r)
  (if (and (setq s (ssget '((0 . "IMAGE"))))
           (setq h (getvar 'textsize))
           (setq g (/ h 2.0))
      )
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (vla-getboundingbox (vlax-ename->vla-object e) 'l 'r)
      (setq l (vlax-safearray->list l)
            r (vlax-safearray->list r)
      )
      (and l r
           (entmake
             (list
               (cons 0 "TEXT")
               (cons 10 (polar (mapcar '(lambda (j k) (/ (+ j k) 2.0)) l r)
                               0.0
                               (+ g (/ (distance l (list (car r) (cadr l) 0.0))))
                               )
                     )
               (cons 40 h)
               (cons 1 (getpropertyvalue e "ImageName"))
               )
             )
           )
      )
    )
  (princ)
)

 

Link to comment
Share on other sites

getting "; error: ADS request error"

 

I have AutoCAD 2014 (one of the last perpetual licenses) and running on Windows 10 (and it works just fine).

 

Could that be a factor with all these errors?

 

Just so you know, Autodesk sent me the install files when I moved to Windows 10 (but they also recommended staying with Windows 7)

 

 

 

Link to comment
Share on other sites

(defun c:test (/ s h i e g l r)
  (if (and (setq s (ssget '((0 . "IMAGE"))))
           (setq h (getvar 'textsize))
           (setq g (/ h 2.0))
      )
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (vla-getboundingbox (vlax-ename->vla-object e) 'l 'r)
      (setq l (vlax-safearray->list l)
            r (vlax-safearray->list r)
      )
      (and l r
           (entmake
             (list
               (cons 0 "TEXT")
               (cons 10 (polar (mapcar '(lambda (j k) (/ (+ j k) 2.0)) l r)
                               0.0
                               (+ g (/ (distance l (list (car r) (cadr l) 0.0)) 2.0))
                               )
                     )
               (cons 40 h)
               (cons 1 (getpropertyvalue e "ImageName"))
               )
             )
           )
      )
    )
  (princ)
) (vl-load-com)

 

  • Like 1
Link to comment
Share on other sites

Back to 1st post if you use Getfiled rather than DOSLIB, you can pick say 1 JPG then you can get the directory where the JPG's are located. (setq DT (getfiled "Specify File Name" "d:" "jpg" 1)) then (setq path (vl-filename-directory DT)) to get path.

 

Then use (setq jpgs (vl-directory-files path "*.JPG")) this will make a list of your jpg names so can feed the insert image and file name in one go. 

 

Will add to "to do list".

Edited by BIGAL
Link to comment
Share on other sites

"test.lsp" works okay with AutoCAD 2021 but not with 2014.

 

Any ideas why 2021 but not 2014?

 

I was hoping to stay with 2014.  Otherwise all good to go with "ImageIn" (then label with "test" after sorting images)

Link to comment
Share on other sites

That's because the function getpropertyvalue was release a few years back and not up to AutoCAD 2014 , so try the following which should work on any CAD.

(defun c:test (/ s h i e g l r)
  (if (and (setq s (ssget '((0 . "IMAGE"))))
           (setq h (getvar 'textsize))
           (setq g (/ h 2.0))
      )
    (repeat (setq i (sslength s))
      (setq e (vlax-ename->vla-object (ssname s (setq i (1- i)))))
      (vla-getboundingbox e 'l 'r)
      (setq l (vlax-safearray->list l)
            r (vlax-safearray->list r)
      )
      (and l r
           (entmake
             (list
               (cons 0 "TEXT")
               (cons 10 (polar (mapcar '(lambda (j k) (/ (+ j k) 2.0)) l r)
                               0.0
                               (+ g (/ (distance l (list (car r) (cadr l) 0.0)) 2.0))
                               )
                     )
               (cons 40 h)
               (cons 1 (vla-get-name e))
               )
             )
           )
      )
    )
  (princ)
) (vl-load-com)

 

Link to comment
Share on other sites

WOW that was awesome!  Thank you

 

So now I'll use "ImageIn.lsp" (as was written by Alan Thompson) to load multiple pictures into AutoCAD 2014.

 

Then, after using AutoCAD to sort the pictures, I can label them with "test.lsp".

 

"imageIn.lsp" does create a new layer for each picture loaded, and I don't need that...but I can live with it...unless someone knows how to turn that off easily.

 

Again, thank you.

  • Like 1
Link to comment
Share on other sites

(defun c:ImageIn ( / lst a b fin )
  ;; Alan J. Thompson, 05.10.10
  ;; DosLib required
  ;; no layers creation while attaching images... mod. by M.R.

  (vl-load-com)

  (if (setq lst (dos_getfilem "Select images to insert:" (getvar "dwgprefix") ""))
    (foreach file (vl-sort (cdr lst) (function >))
      (if fin
        (progn
          (vla-GetBoundingBox (car fin) 'a 'b)
          (setq b (vlax-safearray->list b))
          (vl-cmdf "_.image" "_attach" (strcat (car lst) file) (list 0. (1+ (cadr b))) "" "")
        )
        (vl-cmdf "_.image" "_attach" (strcat (car lst) file) '(0. 0. 0.) "" "")
      )
      (setq fin (cons (vlax-ename->vla-object (entlast)) fin))
    )
  )
  (princ)
)

 

Link to comment
Share on other sites

Just a comment I threw multiple images of all different sizes at code and the result was interesting with images not stacked nicely. It needs to look at image bounding box and move image to suit so touches last. Where images are all same size works ok.

Link to comment
Share on other sites

When I run the routine by marrko_ribar (see above) I sometimes get the images stacked next to each other, sometimes there's a big gap (it seems to vary with different sessions of AutoCAD).  There must be a setting or variable in AutoCAD 2014 that is changing (?).

 

Is there a way to control the gap regardless of what the current settings are in that particular AutoCAD session?

 

Ideally I would want 1/4" gap, always -- the images are always inserted as 1" high.

 

The reason for the gap -- so I can rotate some of the images without overlapping the adjacent images (which is what happening when there's no gap between images).

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...