Jump to content

Change Image Path in Many Drawings


Guus

Recommended Posts

22 hours ago, rlx said:

Twenty-ish years ago we also had hybrid drawings , where part of the drawing was tif and saved in same folder as drawing , and other part drawn in AutoCad but that not allowed any more where I work , now its tif or dwg , not both.

 

That's because document controllers (In my opinion) are idiots and cannot see that the future is to convert everything to DWGs. for us (with the idiots in charge), the first project profits would pay to convert a whole drawing rather than each project as and when a part of the TIFF needs changing.

  • Agree 2
Link to comment
Share on other sites

Don't get me started on non-CAD people calling the shots on CAD. It's a dark place!

 

So I was looking to avoid Express Tools so I came up with this version and a  new thought, why the need for old file path, it's in the drawing already. I am sure this could be made better. 

 

See if this works...

 

;;; Update image paths in a folder of drawings.                                                                                            |
;;;                                                                                                                                        |
;;; https://www.cadtutor.net/forum/topic/90022-change-image-path-in-many-drawings/?do=findComment&comment=650179                           |
;;;                                                                                                                                        |
;;; By SLW210 (Steve Wilson)                                                                                                               |
;;;________________________________________________________________________________________________________________________________________|
;;;                                                                                                                                        |
;;; Original- August 26th, 2024                                                                                                            |
;;;                                                                                                                                        |
;;; V1.0- August 27th, 2024  Select files and folders added.                                                                               |
;;;                                                                                                                                        |
;;; V1.1- August 29th, 2024  Select files and folders added without Express Tools and no need to select old path.                          |
;;;                                                                                                                                        |
;;;________________________________________________________________________________________________________________________________________|
;;;                                                                                                                                        |


(defun c:UPP () (c:UpYourPath))		; You can change the shortcut to suit what is convenient for you.

(defun c:UpYourPath (/ new-path folder dwg-files dwg-path old-path new-path-file new-path-ext)
  (vl-load-com)

  ;; Function to get a list of DWG files from a folder
  (defun get-dwg-files (folder)
    (vl-directory-files folder "*.dwg" 1)
  )

  ;; Function to update image path in a drawing
  (defun update-dwg (dwg-path old-path new-path)
    (princ (strcat "\nProcessing: " dwg-path))
    (setq doc (vla-open (vla-get-documents (vlax-get-acad-object)) dwg-path))
    (vla-startundomark doc)

    ;; Update paths in Model Space
    (setq model-space (vla-get-ModelSpace doc))
    (update-entity-paths model-space old-path new-path)
    
    ;; Update paths in Layouts
    (vlax-for layout (vla-get-Layouts doc)
      (setq paper-space (vla-get-Block layout))
      (update-entity-paths paper-space old-path new-path)
    )
    
    (vla-endundomark doc)
    (vla-save doc)
    (vla-close doc)
  )
  
  ;; Function to update image paths in entities
  (defun update-entity-paths (space old-path new-path)
    (vlax-for ent space
      (if (and (vlax-property-available-p ent 'ObjectName)
               (= "IMAGE" (vla-get-ObjectName ent))
               (vlax-property-available-p ent 'Path)
               (wcmatch (vla-get-Path ent) old-path))
        (progn
          (princ (strcat "\nUpdating image path: " (vla-get-Path ent)))
          (vla-put-Path ent new-path)
        )
      )
    )
  )

  ;; Prompt user to select the new image file path
  (setq new-path (getfiled "Select New Image File" "" "TIFF;JPEG;PNG;BMP;GIF;" 10) ; You can add more image extensions be sure to place ; between.

  (if (not new-path)
    (progn
      (princ "\nNo image file selected.")
      (exit)
    )
  )
  
  ;; Extract the new image file path details
  (setq new-path-file (vl-filename-base new-path))
  (setq new-path-ext (vl-filename-extension new-path))
  (setq new-path-dir (vl-filename-directory new-path))
  
  ;; Prompt user to select a DWG file to determine the folder path
  (setq sample-dwg (getfiled "Select Any DWG File in Target Folder" "" "dwg" 10))
  (if (not sample-dwg)
    (progn
      (princ "\nNo DWG file selected.")
      (exit)
    )
  )
  
  ;; Extract the folder path from the selected DWG file
  (setq folder (vl-filename-directory sample-dwg))
  
  ;; Get the list of DWG files in the selected folder
  (setq dwg-files (get-dwg-files folder))
  (if (null dwg-files)
    (progn
      (princ "\nNo DWG files found in the selected folder.")
      (exit)
    )
  )
  
  ;; Construct the old path to match the new file name
  (setq old-path (strcat new-path-dir "\\" new-path-file new-path-ext))
  
  ;; Update image paths in each DWG file
  (foreach dwg dwg-files
    (setq dwg-path (strcat folder "\\" dwg))
    (update-dwg dwg-path old-path new-path)
  )
  
  (princ "\nImage paths updated successfully.")
  (princ)
)


 

  • Like 1
Link to comment
Share on other sites

"Don't get me started on non-CAD people calling the shots on CAD. It's a dark place!"

 

Where I worked they wanted our cad dwg's to be controlled by the document control system, think Word, excel, powerpoint etc. Just said to the manager you tell us how to make it work, they still run Acad out side then save final dwg into document control. 

Edited by BIGAL
  • Like 2
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...