Author Topic: odbx : a simple way to manage renamed files and their xrefs via a batch process  (Read 2683 times)

0 Members and 1 Guest are viewing this topic.

BrunoToniutti

  • Guest
hi,
as I wrote in topic
http://www.theswamp.org/index.php?topic=15712.0
here a demo lsp to manage renamed files and all xrefs quickly,
caution : path for dwg files is harcoded, thus you must modify pathDwgs to test it

Because we do not have all the same needs, my purpose is simply to show a way and no to post a full ready solution for everyone.

it's a very simple solution :-) but it makes all I need about xrefs when I rename a bunch of files.

all questions are welcome,
thx to reply if you test it with something else then acad 2007.

About sample in zip:
there are 3 dwg using xrefs : _principal.dwg carre.dwg cercle.dwg
I rename them to : ARC__principal.dwg ARC_carre.dwg ARC_cercle.dwg thus all xrefs's path are broken


Bruno Toniutti

Code: [Select]
;FUNCTION:
; purpose it's to show a simple way to manage renamed xrefs via a batch odbx process
; Thus user's interface is not a key point
;
;
;USE:
; (BATodbxXrefsRenameDemo listOldnameNewname) or (BATodbxXrefsRenameDemo nil)
; listOldnameNewname = '((Oldname1 Newname1)(Oldname2 Newname2)....)
; OldnameX = vl-filename-base
; NewnameX = [Autolisp path/]filename.dwg
; listOldnameNewname doesn't need to be a full list of substitutions,
; process will ask for xrefs's substitution missing
;
; sample 1:
;  (BATodbxXrefsRenameDemo nil)
;
; sample 2:
;  (setq lstA '(("carre" "ARC_carre.dwg")))
;  (BATodbxXrefsRenameDemo lstA)
;
;PROGRAMMING:
; Bruno Toniutti
;
;VERSIONS:
; 1.070404 specific light release for TheSwamp.org
;
;NOTES:
; This software is provided "as is" without express or implied warranty.
; For display, acet-ui-status from express tools is used
; 1.070404 tested with MAP 3D 2007 and Win XP sp2
;-----------------------------------------------------------------------------------------------------


; error function
(defun errorFunction ()
  (setq *error* saveError)
  (acet-ui-status)
  (if (and objBlock (not (vlax-object-released-p objBlock))) (vlax-release-object objBlock))
  (if (and odbx (not (vlax-object-released-p odbx))) (vlax-release-object odbx))
  (foreach x '(pathDwgs odbxSaveFlag file files odbx index oldFileName yetRedefine newFileName xrefsListGlobal saveError objBlock)
    (set x nil)
  )
  (gc)
  (princ)
)

(defun BATodbxXrefsRenameDemo (xrefsListGlobal / pathDwgs odbxSaveFlag file files odbx index oldFileName yetRedefine newFileName saveError objBlock)

  ; to modify: pathDwgs (to create your own manner to get a path or dwg files)
  (setq pathDwgs "Z:/")

  ; variables set
  (setq
    saveError *error*
    *error* errorFunction
    index 0
  )

  ; main

  ; to modify: files (to create your own manner to get a list of dwg files)
  (if (setq files (vl-directory-files pathDwgs "*.dwg"))
   
    (progn
      (if (< (atoi (substr (getvar "ACADVER") 1 2)) 16)
        (setq odbx (vlax-get-or-create-object "ObjectDBX.AxDbDocument"))
        (setq odbx (vlax-get-or-create-object (strcat "ObjectDBX.AxDbDocument." (substr (getvar "ACADVER") 1 2))))
      )

      ; main loop
      (foreach file files
        (acet-ui-status (strcat "File : " (itoa (setq index (1+ index))) " on " (itoa (length files)) " files \n" file) "BATodbxXrefsRename is working...")
        (if (vl-catch-all-error-p (vl-Catch-All-Apply '(lambda () (vla-open odbx (strcat pathDwgs file)))))
          (alert (strcat "Odbx connection no available with file :\n" file))
          (progn
            (setq odbxSaveFlag nil)
            (vlax-for objBlock (vla-get-blocks odbx)
              (if (= (vlax-get objBlock 'IsXRef) -1)
                (progn
                  (setq oldFileName (vl-filename-base (vla-get-path objBlock)))
                  (if (setq yetRedefine (vl-member-if '(lambda (x) (= (car x) oldFileName)) xrefsListGlobal))
                    (if (cadar yetRedefine)
                      (progn
                        (vla-put-path objBlock (cadar yetRedefine))
                        (setq odbxSaveFlag T)
                      )
                    )
                    (progn
                      (if (setq newFileName (getfiled (strcat "Select new xref to replace " oldFileName ".dwg") pathDwgs "dwg" 0))
                        (progn
                          ; to modify : newFileName
                          ; (if you don't want keep path, or simply change path, or attach a file that doesn't exist, or what you want etc...)
                          (vla-put-path objBlock newFileName)
                          (setq odbxSaveFlag T)
                        )
                        (alert (strcat "No file selected, " oldFileName ".dwg will be kept."))
                      )
                      (setq xrefsListGlobal (cons (list oldFileName newFileName) xrefsListGlobal))
                    )
                  )
                )
              )
            )
            (if odbxSaveFlag (vla-saveas odbx (strcat pathDwgs file)))
          )
        )
      )
      (acet-ui-status)
      (vlax-release-object odbx)
    )
    (alert "None dwg found")
  )
  (princ)
)
« Last Edit: April 05, 2007, 09:22:52 AM by Bruno Toniutti »

paul_s

  • Guest
Hello Bruno,

I just found your code. This is what I've been looking for a long time
now. I needed a lisp/vlsip program to repath xrefs with ObjectDbx.
And it works for me in Autocad 2008.  Can you please update your
code to include images.  I desperately need to repath images too.
How about OLE links. I also have a lot drawings that has Excel sheets.
I will use this program for archiving drawings.

Thank you,
Paul

Patrick_35

  • Guest
Hi
It's nice.

Just a question with odbx
I read in your lisp when you save the drawing, you use vla-saveas. It's good.
I also work with odbx and I dont't understand why vla-save don't work :(

@+

Joe Burke

  • Guest
Hi
It's nice.

Just a question with odbx
I read in your lisp when you save the drawing, you use vla-saveas. It's good.
I also work with odbx and I dont't understand why vla-save don't work :(

@+

Patrick,

That's just how it is. The Save method doesn't work with ODBX files.

BrunoToniutti

  • Guest
oops sorry, since september 2007, I didn't have any time to read it and btw to answer

Bruno