Batch Renaming of Xref Drawings' "Saved Paths"

Discussion in 'AutoCAD' started by kzalec, Nov 9, 2004.

  1. kzalec

    T.Willey Guest

    James,

    I myself am doing it to learn, and am very close (I think) to finding an answer, but work has now come into play. They have deadlines now, so I don't know when I will be able to finish it, but I hope people still respond to answer my questions because they come from someone that truely wants to learn.

    I thought there might be a package out there already that would do what he wanted, but since we don't really have any packages here that I know of, except AcadX from Tony, and that is a corporate thing that I don't get to play with, I didn't know where to lead the OP.

    Thanks for posting this information, and I will try and finish the code when I can, even if it has been solved, just for the learning of it.
    Tim
     
    T.Willey, Nov 10, 2004
    #21
  2. kzalec

    Rudy Tovar Guest

    We're not going to give you everything for FREE...

    It's going to cost you time to make the interface for the core...
     
    Rudy Tovar, Nov 10, 2004
    #22
  3. kzalec

    Rudy Tovar Guest

    Looks like you just lost our interest...

    We don't like being taken advantange of....

    Well, I figure in about 5 years, you should be able to write it
    yourself...but by then we'll be wired to the computer and we'll just use
    brain waves.
     
    Rudy Tovar, Nov 10, 2004
    #23
  4. kzalec

    kzalec Guest

    Below is what I first got from T. Willey. It doesn't seem complicated and is nearly all we need to do the trick. All I need now is a script file to open dwgs/run routine/save dwgs. A fancier DBX or AutoLISP routine w/ dialog box would be nice if Mr. Willey can come up with something. This is a "learning" forum. No one has to reply if they don't want to! I've contributed quite a lot of help to this forum over the years, and occasionally I ask for help too. I'm very grateful to Autodesk for providing this great discussion group. Original simple code from T. Willey:

    ;;;(COMMAND "-XREF" "P" "---original xref name here---" "---path and new xref name here---")
    ;;;(COMMAND "-XREF" "P" "---original xref name here---" "---path and new xref name here---")
    ;;;(COMMAND "-XREF" "P" "---original xref name here---" "---path and new xref name here---") ;rest of lines and add as many as you need...
    (DEFUN C:XPATH ()
    (command "zoom" "e")
    (COMMAND "-XREF" "P" "B_PRT_PS" "B_PRT_PS.DWG")
    (COMMAND "-XREF" "P" "C_MEC_PS2" "C_MEC_PS2.DWG")
    (COMMAND "-XREF" "P" "C_MEC_PS" "C_MEC_PS.DWG")
    (COMMAND "-XREF" "P" "C_PRT_PS" "C_PRT_PS.DWG")
    (COMMAND "-XREF" "P" "V_APRT" "V_APRT.DWG")
    (COMMAND "-XREF" "P" "V_ASHT_M" "V_ASHT_M.DWG")
    (COMMAND "-XREF" "P" "V_BPRT" "V_BPRT.DWG")
    (COMMAND "-XREF" "P" "V_BSHT_M" "V_BSHT_M.DWG")
    (COMMAND "-XREF" "P" "V_CPRT" "V_CPRT.DWG")
    (COMMAND "-XREF" "P" "V_CSHT_M" "V_CSHT_M.DWG")
    (COMMAND "RESUME")
    )
     
    kzalec, Nov 10, 2004
    #24
  5. I always thought he was just being efficient...

    QUOTE OF THE DAY
    It's pretty hard to be efficient without being obnoxious.
    - Kin Hubbard

    Sorry, I don't remember where I got this quote, or even why I saved it.
    Maybe I resemble it too. <g>

    Anyway, just had to add this here.

    Arnold Williams
     
    Arnold Williams, Nov 10, 2004
    #25
  6. kzalec

    kzalec Guest

    I was just wondering if the REDIR Expresstools command (in conjunction with the REDIRMODE Expresstools command / .LSP routine) could be modified to rename drawings used as XREFs instead of just searching for and replacing their path names as it was designed to do. I looked at the REDIR.LSP code that Autodesk provides. It looks really complicated to me, but someone who knows more about AutoLISP might be able to modify it slightly to do what we want. Just an idea. I might be way off track...
     
    kzalec, Nov 10, 2004
    #26
  7. kzalec

    T.Willey Guest

    See if this works for you. Add as many name to the list as you would like. Tell me how it works. Maybe make a test directory first. This is posted "as is" I take no responsibility for what happens. But on my test it worked.

    The way it is right now, if you don't have write access to a drawing that you want to change, then it makes a copy of it and changes that one.

    Tim

    (defun c:ChangeXrefDrawing (/ DirPath DwgList NewPath DwgNumList DwgList2)

    (alert "\n Select directory of files to be renamed.")
    (if (setq DirPath (DIRECTORY-DIA))
    (progn
    (alert "\n Select drawing files to be renamed.")
    (setq DwgList (vl-sort (vl-directory-files DirPath "*.dwg") '<))
    (setq NewPath (strcat DirPath "OldDrawings\\"))
    (vl-mkdir NewPath)
    (setq DwgNumList (ChangeXrefDia DwgList))
    (foreach item DwgNumList
    (setq DwgList2 (cons (nth item DwgList) DwgList2))
    )
    (alert "\n Select directory of files to have X-Ref's renamed.")
    (setq RenameList (RenameDwg Dwglist2 DirPath NewPath))
    (setq DirPath (DIRECTORY-DIA))
    (alert "\n Select drawing files to have X-Ref's renamed.")
    (setq DwgList (vl-sort (vl-directory-files DirPath "*.dwg") '<))
    (setq DwgNumList (ChangeXrefDia DwgList))
    (foreach item DwgNumList
    (setq DwgFile (strcat DirPath (nth item DwgList)))
    (setq Results (tmw:OpenDBX DwgFile))
    (RenameXrefs (car Results) RenameList)
    (vla-SaveAs (car Results) (cadr Results))
    (tmw:CloseDBX Results)
    )
    )
    )
    (princ)
    )





    ;---------------------------------------

    (defun DIRECTORY-DIA ( / sh folder parentfolder folderobject result)
    ;By Tony Tanzillo
    ;Modified by Tim Willey
    (vl-load-com)
    (setq sh
    (vla-getInterfaceObject
    (vlax-get-acad-object)
    "Shell.Application"
    )
    )

    (setq folder
    (vlax-invoke-method
    sh
    'BrowseForFolder
    0
    ""
    0
    )
    )
    (vlax-release-object sh)

    (if folder
    (progn
    (setq parentfolder
    (vlax-get-property folder 'ParentFolder)
    )
    (setq FolderObject
    (vlax-invoke-method
    ParentFolder
    'ParseName
    (vlax-get-property Folder 'Self)
    )
    )
    (setq result
    (vlax-get-property FolderObject 'Path)
    )
    (mapcar 'vlax-release-object
    (list folder parentfolder folderobject)
    )
    (setq result (strcat result "\\"))
    )
    )
    )

    ;----------------------------------------------------

    (defun ChangeXrefDia (DwgList / blist diaload ipblist bilist)

    (if DwgList
    (progn
    (setq DwgList (vl-sort DwgList '<))
    (setq diaload (load_dialog "CloseDwg.dcl"))
    (if (not (new_dialog "Cdrawings" diaload))
    (exit)
    )
    (start_list "tx2" 3); clear the list
    (mapcar 'add_list DwgList)
    (end_list)
    (mode_tile "d-save" 1)
    (action_tile "accept"
    "(progn
    (setq UpdList (get_tile \"tx2\"))
    (done_dialog 1)
    )"
    )
    (action_tile "cancel"
    "(progn
    (setq UpdList nil)
    (done_dialog 1)
    )"
    )
    (start_dialog)
    (if Updlist
    (setq UpdList (read (strcat "(" UpdList ")")))
    UpdList
    )
    )
    )
    )

    ;-------------------------------------------------------

    (defun RenameDwg (DwgList DirPath NewPath / RenameList OldNameList NewNameList Pos1)

    (setq RenameList
    (list
    (cons "X-ALIGN-HORZ.DWG" "X300-S-TS-W001.DWG")
    (cons "X-ALIGN-HORZ (SITE20).DWG" "X300-S-TS-W002.DWG")
    (cons "X-ALIGN-HORZ (SITE40).DWG" "X300-S-TS-W003.DWG")
    )
    )
    (foreach item RenameList
    (setq OldNameList (cons (strcase (car item)) OldNameList))
    (setq NewNameList (cons (strcase (cdr item)) NewNameList))
    )
    (foreach item DwgList
    (if (setq Pos1 (vl-position (strcase item) OldNameList))
    (progn
    (vl-file-copy (strcat DirPath item) (strcat NewPath item))
    (vl-file-rename (strcat DirPath item) (strcat DirPath (nth Pos1 NewNameList)))
    )
    )
    )
    (list OldNameList NewNameList)
    )

    ;-------------------------------------------------------

    (defun RenameXrefs (dbxDoc NameList / BlockList XrPath XrDwgName SpaceList Pos1 cnt len1 test1)

    (setq BlockList (vla-get-Blocks dbxDoc))
    (vlax-for item BlockList
    (if
    (or
    (vl-string-search "*MODEL_SPACE" (strcase (vla-get-Name item)))
    (vl-string-search "*PAPER_SPACE" (strcase (vla-get-Name item)))
    )
    (setq SpaceList (cons item SpaceList))
    )
    )
    (foreach item SpaceList
    (setq cnt1 0)
    (setq len1 (vla-get-Count item))
    (repeat len1
    (setq test1 (vla-Item item cnt1))
    (if (vlax-property-available-p test1 'Path)
    (progn
    (setq XrPath (vla-get-Path test1))
    (if (vl-string-search "\\" XrPath)
    (progn
    (setq XrDwgName (strcat (vl-filename-base XrPath) ".dwg"))
    (setq XrPath (vl-filename-directory XrPath))
    )
    (setq XrDwgName XrPath)
    )
    (if (setq Pos1 (vl-position (strcase XrDwgName) (car NameList)))
    (if (/= XrPath XrDwgName)
    (vla-put-Path test1 (strcat XrPath "\\" (nth Pos1 (cadr NameList))))
    (vla-put-Path test1 (nth Pos1 (cadr NameList)))
    )
    )
    )
    )
    (setq cnt1 (1+ cnt1))
    )
    )
    )

    ;-------------------------------------------------------

    (defun tmw:OpenDBX (FileName / NewFileName TestIf dbxDoc)

    (if FileName
    (progn
    (if (setq TestIf (open FileName "a"))
    (close TestIf)
    (progn
    (setq NewFileName (strcat (vl-filename-directory FileName) "\\" (strcat "CopyOf-" (vl-filename-base FileName) ".dwg")))
    (vl-file-copy FileName NewFileName)
    (setq FileName NewFileName)
    )
    )
    (setq dbxDoc (vla-GetInterfaceObject (vlax-get-ACAD-Object) "ObjectDBX.AxDbDocument.16"))
    (vla-Open dbxDoc FileName)
    (list dbxDoc FileName)
    )
    )
    )

    ;----------------------------------------------------------------------

    (defun tmw:CloseDBX (Results / FileName)

    (setq FileName (cadr Results))
    (vl-Catch-All-Apply '(lambda () (vla-Close (car Resutls))))
    (vlax-Release-Object (car Results))
    ;(if (vl-string-search "CopyOf-" (vl-filename-base FileName))
    ; (vl-file-delete FileName)
    ;)
    )
     
    T.Willey, Nov 10, 2004
    #27
  8. kzalec

    kzalec Guest

    Wow. I'll try it. But first can you tell me if I have to add the entire PATH (for both old and new Xref names) rather than just the old and new Xref drawing names? I would think the entire path to the network directory would have to be indicated in addition to the file names. Please let me know what to do about this. Thanks for all your help once again! I really appreciate it and will definitely be studying this code to try and understand the logic behind it.
     
    kzalec, Nov 10, 2004
    #28
  9. kzalec

    kzalec Guest

    Well I just tried to run the .LSP routine and I got the following error message:

    ; error: no function definition: DWGLIST

    .............I doubt that I know enough about AutoLISP to fix the problem, but I'll look at it.
     
    kzalec, Nov 10, 2004
    #29
  10. kzalec

    kzalec Guest

    Okay, I found the problem. It was just one little space between the "f" and "u" in "defun". Once I closed that up, the program ran fine with no errors. So all I have to do now is to provide the correct list of new and old Xref drawing names. Thanks very much. Looks like this might do the trick. It's even more than I asked for because we don't really have any need to rename directories or point to new directories at this point. Thanks again for all your help!
     
    kzalec, Nov 10, 2004
    #30
  11. kzalec

    T.Willey Guest

    Just post here and tell me how it worked when you are finished with all of them. I am interrested to know. It was kind of fun to write, and nice to learn.

    Thanks to all the people that posted on this topic and helped me to learn a lot.

    Tim
     
    T.Willey, Nov 10, 2004
    #31
  12. kzalec,
    You are in way over your head.
    When you realize dbx will error out for a variety of reasons, you will wake up.
    Real life programs that manipulate lots of files must have a logging mechanism to see if the change was successful.
    Dealing with the error checking involved with these kinds of programs takes in depth experience and the ability to fix
    code yourself.
    I get the feeling you did not learn much and ended up with a tool that works in theory, but crashes in reality when you
    are not looking.
    I know because I run into the same problems and the workarounds are not easy.
    Toolpack is the way to go.

    Now that I said that, Terry needs to add logging to his program! It crashes out too sometimes and I need to know where
    it left off. I say this because he has a clue and his still has room to grow.

    I did a dbx prog and used logging and it works great, even though it skips files here and there. The interesting thing
    is that I used an Autodesk provided exe to do the dbx work. So my prog was just a fancy sheell to run the exe on a
    bunch of files. Either way, the error checking and logging is critical when you are touching lots of files.

    I am guessing your supervisor would not be happy if he/she knew of the vulnerabilities of your solution...


    kzalec <>
    |>Okay, I found the problem. It was just one little space between the "f" and "u" in "defun". Once I closed that up, the program ran fine with no errors. So all I have to do now is to provide the correct list of new and old Xref drawing names. Thanks very much. Looks like this might do the trick. It's even more than I asked for because we don't really have any need to rename directories or point to new directories at this point. Thanks again for all your help!

    James Maeding
    jmaeding at hunsaker dot com
    Civil Engineer/Programmer
     
    James Maeding, Nov 11, 2004
    #32
  13. kzalec

    Joe Burke Guest

    Rudy,

    For what it's worth, I did not mention the word "professional". Personally I think
    putting that word in front of some occupation is sometimes a ruse to lend
    credibility. For instance, who are the professional programmers in this NG? Only
    those who sell code? Do you think that sets them apart from those of us who write for
    our own use, in terms of quality? I certainly don't.

    My sense is Luis got out of the code writing business. Does that diminish his
    contributions here? Obviously not. Does anyone care whether he's a "professional".
    No, we know him by the quality of his work and his willingness to share what he
    knows. The same idea applies to Tony.

    Granted, "virtue" is a vague word. Within context it meant Tony should be allowed
    some leeway, given the fact the content of his posts is typically valuable to
    everyone reading here.

    I'll leave the "arrogant" word alone.

    Regards
    Joe Burke
     
    Joe Burke, Nov 11, 2004
    #33
  14. kzalec

    T.Willey Guest

    James,

    Good point. Here is the latest version that creates a log file. The only thing I don't like is that I had to put in a lot of alert boxes (5) so people would know what to do. I couldn't figure out any other way. Suggestions welcomed, and comments.

    Tim

    ps. Change the extension from "txt" to "lsp".
     
    T.Willey, Nov 11, 2004
    #34
  15. kzalec

    LUCAS Guest

    Hi T.Willey,
    Where can I give more information about
    (getvar "cputicks")
    (getvar "millisecs")


    thing I don't like is that I had to put in a lot of alert boxes (5) so
    people would know what to do. I couldn't figure out any other way.
    Suggestions welcomed, and comments.
     
    LUCAS, Nov 12, 2004
    #35
  16. kzalec

    T.Willey Guest

    Lucas,

    http://hyperpics.com/

    On this web site there is a pdf file that shows all system variables.

    Tim
     
    T.Willey, Nov 12, 2004
    #36
  17. maybe I missed it, but your logging does not seem to test if a drawing was changed or not.
    That is the main reason I log, not just to remember what was run.

    Here is the method I use in another prog to check if a file was changed, it uses doslib...
    Note that I am running an exe whose path is saved to CD-PATH to set the date back to what it was before I purged the
    file. Also note the use of dos_attrib to set the attribute back. This is necessary so my IT person does not back up
    every file on the server after I purge a bunch of files.

    This is the kind of stuff that is worth paying the $200 for. I thought since I am ripping on the original poster so
    much though I would give something back.

    Of course you have to fill in the variables such as FILE and so on, but this shows how to detect and log the important
    info.

    ;MAKE COMPARISON LIST (FILENAME CUR-DATE ARCH-ATTRIB FILESIZE)
    (SETQ BEFORE (LIST FILE
    (CDR (CAR (dos_filedate FILE)))
    (CDR (CAR (dos_attrib FILE)))
    (CDR (CAR (dos_filesize FILE)))
    )
    )

    ;CHECK IF CLEANED BY DATE
    (IF (= (CDR (CAR (dos_filedate FILE)))(NTH 1 BEFORE))
    ;ISSUE ERROR TO LOG
    (IF (= 1 (LOGAND 1 (NTH 2 BEFORE)))
    (PROGN
    (SETQ LOG-LIST (CONS (STRCAT "Note: " FILE " was not purged, file is read-only.") LOG-LIST))
    (PRINC "Unsuccessful, file is read-only.\n.")
    )
    (PROGN
    (SETQ LOG-LIST (CONS (STRCAT "Note: " FILE " was not purged, error occurred.") LOG-LIST))
    (PRINC "Unsuccessful, error occurred.\n.")
    )
    )
    (PROGN
    ;LOG FILE, SET ARCHIVE AND DATE BACK
    (SETQ FS (CDR (CAR (dos_filesize FILE)))
    LOG-LIST (CONS (STRCAT FILE " was purged, file size went from " (RTOS (NTH 3 BEFORE) 2 0)
    " to " (RTOS FS 2 0) " bytes."
    )
    LOG-LIST
    )
    ;BYTE-TOT is used to report later on, how many bytes were purged.
    BYTE-TOT (+ BYTE-TOT (- (NTH 3 BEFORE) FS))
    )
    ;TIME
    (dos_exewait (STRCAT CD-PATH " " FILE "," (DATE-TIME-24 (NTH 1 BEFORE))))
    ;ATTRIB
    (dos_attrib FILE (+ (LOGAND 1 (NTH 2 BEFORE))
    (LOGAND 2 (NTH 2 BEFORE))
    (LOGAND 4 (NTH 2 BEFORE))
    (LOGAND 8 (NTH 2 BEFORE))
    )
    )
    (PRINC (STRCAT "Successful, file size went down by " (RTOS (/ (- (NTH 3 BEFORE) FS) 1000000.0) 2 2) "
    MegaBytes.\n."))
    )
    )

    "T.Willey" <>
    |>James,
    |>
    |>Good point. Here is the latest version that creates a log file. The only thing I don't like is that I had to put in a lot of alert boxes (5) so people would know what to do. I couldn't figure out any other way. Suggestions welcomed, and comments.
    |>
    |>Tim
    |>
    |>ps. Change the extension from "txt" to "lsp".

    James Maeding
    jmaeding at hunsaker dot com
    Civil Engineer/Programmer
     
    James Maeding, Nov 12, 2004
    #37
  18. kzalec

    T.Willey Guest

    James,

    The way my routine works with the log, once you open the drawing it checks to see if there are any xref's that match the list within the drawing, when it finds one it will change the path and make a note of old path new path. In the log file it shows only the drawing name and the old path new path of the xrefs that got changed.

    I admit I haven't read your code yet, I'm sick and am going home soon, but I will read it on Monday or maybe over the weekend if I feel better. Thanks for posting it so that I can learn.

    Tim
     
    T.Willey, Nov 12, 2004
    #38
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.