Results 1 to 9 of 9

Thread: Rename specific characters in a block name

  1. #1
    AUGI Addict jpaulsen's Avatar
    Join Date
    2002-04
    Location
    Colorado
    Posts
    2,020
    Login to Give a bone
    0

    Default Rename specific characters in a block name

    I am looking for a way to rename specific characters in a block name. For example, if the 3rd and 4th characters of a selected block name are CS, FC or ND I want those characters changed to DN.

    I wrote the following routine a year or so ago to update our tile block. Now I want it to check the block name and change it but I have no clue how I can do this. I tried (command "-rename") but it does not accept wildcards.

    Code:
    (defun updatetitleblock ( / vss tbname sync ntbname BlkEnt EntData lgname)
      (setq vss (car (entsel "\nSelect Titleblock to Update: ")))
        (setq tbname (cdr (assoc 2 (entget vss))))
      (if (findfile (setq ntbname (strcat "Z:/Autodesk 2009/JRSym/TitleBlocks/"tbname".dwg")))
        (progn
          (command "-insert" (strcat tbname"="ntbname) "y")
          (command)
          (initget "Yes No")
          (setq sync (getkword "\nUpdate attribute locations [Yes/No] <Y>: "))
          (princ (strcat "\n"tbname" redefined."))
        )
        (princ (strcat "\nCould not find "ntbname))
      )
      (setq BlkEnt (tblobjname "block" tbname)) ; get titleblock block definition ename
      (while (setq BlkEnt (entnext BlkEnt)) ; this will step through all the entities within the block
        (setq EntData (entget BlkEnt)) ; get the dxf list of the entity
        (if
          (and
            (= (cdr (assoc 0 EntData)) "INSERT") ; make sure it is a block
            (wcmatch (strcase (cdr (assoc 2 EntData))) "LOGO*") ; make sure the block name starts with LOGO
          )
          (progn
            (setq lgname (cdr (assoc 2 EntData)))
            (command "-insert" (strcat lgname"=Z:/Autodesk 2009/JRSym/Logos/JRLogos/"lgname) "y")
            (command)
            (princ (strcat " "lgname" redefined."))
          )
        )
      )
      (if (= sync nil)
        (setq sync "Y")
      )
      (if (or (= (substr sync 1 1) "Y") (= (substr sync 1 1) "y") (= sync ""))
        (progn
          (command "attsync" "n" tbname)
        )
      )
    (princ)
    )
    (defun c:updatetitleblock () (updatetitleblock))
    (defun c:udtb () (updatetitleblock))
    So I am looking for way to check the block name in "tbname" and rename based on the criteria in my first paragraph above. Any help would be appreciated.

  2. #2
    Certifiable AUGI Addict ccowgill's Avatar
    Join Date
    2004-08
    Location
    Iron Station, NC
    Posts
    3,197
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    Quote Originally Posted by jpaulsen View Post
    I am looking for a way to rename specific characters in a block name. For example, if the 3rd and 4th characters of a selected block name are CS, FC or ND I want those characters changed to DN.

    I wrote the following routine a year or so ago to update our tile block. Now I want it to check the block name and change it but I have no clue how I can do this. I tried (command "-rename") but it does not accept wildcards.

    Code:
    (defun updatetitleblock ( / vss tbname sync ntbname BlkEnt EntData lgname)
      (setq vss (car (entsel "\nSelect Titleblock to Update: ")))
        (setq tbname (cdr (assoc 2 (entget vss))))
      (if (findfile (setq ntbname (strcat "Z:/Autodesk 2009/JRSym/TitleBlocks/"tbname".dwg")))
        (progn
          (command "-insert" (strcat tbname"="ntbname) "y")
          (command)
          (initget "Yes No")
          (setq sync (getkword "\nUpdate attribute locations [Yes/No] <Y>: "))
          (princ (strcat "\n"tbname" redefined."))
        )
        (princ (strcat "\nCould not find "ntbname))
      )
      (setq BlkEnt (tblobjname "block" tbname)) ; get titleblock block definition ename
      (while (setq BlkEnt (entnext BlkEnt)) ; this will step through all the entities within the block
        (setq EntData (entget BlkEnt)) ; get the dxf list of the entity
        (if
          (and
            (= (cdr (assoc 0 EntData)) "INSERT") ; make sure it is a block
            (wcmatch (strcase (cdr (assoc 2 EntData))) "LOGO*") ; make sure the block name starts with LOGO
          )
          (progn
            (setq lgname (cdr (assoc 2 EntData)))
            (command "-insert" (strcat lgname"=Z:/Autodesk 2009/JRSym/Logos/JRLogos/"lgname) "y")
            (command)
            (princ (strcat " "lgname" redefined."))
          )
        )
      )
      (if (= sync nil)
        (setq sync "Y")
      )
      (if (or (= (substr sync 1 1) "Y") (= (substr sync 1 1) "y") (= sync ""))
        (progn
          (command "attsync" "n" tbname)
        )
      )
    (princ)
    )
    (defun c:updatetitleblock () (updatetitleblock))
    (defun c:udtb () (updatetitleblock))
    So I am looking for way to check the block name in "tbname" and rename based on the criteria in my first paragraph above. Any help would be appreciated.
    I may not be understanding this, but what if you substitute the string for the block name and use that for your rename? I believe you would use VL-STRING-SUBST to replace the pattern.

  3. #3
    AUGI Addict jpaulsen's Avatar
    Join Date
    2002-04
    Location
    Colorado
    Posts
    2,020
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    The string "tbname" is the block name. If the block name contains CS, FC or ND I want to rename the block by replacing those characters with DN.

    For example, if the block name is "TbCS-22x34-L-cnst" I want it renamed to "TbDN-22x34-L-cnst.dwg". Likewise if the block name is "TbFC-22x34-L-cnst.dwg" I want it renamed to "TbDN-22x34-L-cnst.dwg".

  4. #4
    Certifiable AUGI Addict ccowgill's Avatar
    Join Date
    2004-08
    Location
    Iron Station, NC
    Posts
    3,197
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    Quote Originally Posted by jpaulsen View Post
    The string "tbname" is the block name. If the block name contains CS, FC or ND I want to rename the block by replacing those characters with DN.

    For example, if the block name is "TbCS-22x34-L-cnst" I want it renamed to "TbDN-22x34-L-cnst.dwg". Likewise if the block name is "TbFC-22x34-L-cnst.dwg" I want it renamed to "TbDN-22x34-L-cnst.dwg".
    I'm still not quite getting it, but what if you used something such as
    Code:
    get block name
    (cond 
    character 3 and 4 = cs
         subst dn for cs 
    character3 and 4 = fc
         subst dn for fc
    )end cond
    (command "rename" tbname tbnewname)
    would that possibly get you what you are looking for? What my pseudo code is trying to get at is, get the blocks name, check if it matches the old string, if it does, replace the string name with dn. this would give you the ability to just specify stings when renaming the block. Or am I missing the point entirely?

  5. #5
    The Silent Type RobertB's Avatar
    Join Date
    2000-01
    Location
    Seattle WA USA
    Posts
    5,859
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    Quote Originally Posted by jpaulsen View Post
    The string "tbname" is the block name. If the block name contains CS, FC or ND I want to rename the block by replacing those characters with DN.

    For example, if the block name is "TbCS-22x34-L-cnst" I want it renamed to "TbDN-22x34-L-cnst.dwg". Likewise if the block name is "TbFC-22x34-L-cnst.dwg" I want it renamed to "TbDN-22x34-L-cnst.dwg".
    As long as the position of the substring is consistently the third and fourth characters:
    Code:
    (setq tbName "TbCS-22x34-L-cnst")
    (cond ((wcmatch (strcase tbName) "??CS*,??FC*,??ND*")
           (setq newName (vl-string-subst "DN" (substr tbName 3 2) tbName))))
    R. Robert Bell
    Design Technology Manager
    Stantec
    Opinions expressed are mine alone and do not reflect the views of Stantec.

  6. #6
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    Quote Originally Posted by RobertB View Post
    As long as the position of the substring is consistently the third and fourth characters:
    Code:
    (setq tbName "TbCS-22x34-L-cnst")
    (cond ((wcmatch (strcase tbName) "??CS*,??FC*,??ND*")
           (setq newName (vl-string-subst "DN" (substr tbName 3 2) tbName))))
    That may cause names like CSCS... to be changed to DNCS...

    Why do you want to use the vl-string stuff? The OP's stipulated the 3rd & 4th character positions - you don't need to search for the position as vl-string-subst does. Try:
    Code:
    (defun RenameDN (BName /)
      (if (wcmatch BName "??CS*,??FC*,??ND*")
        (strcat (substr BName 1 2) "DN" (substr BName 5))
        BName
      )
    )
    Or if you want it to also be non-case-sensitive:
    Code:
    (defun RenameDN (BName /)
      (if (wcmatch (strcase BName) "??CS*,??FC*,??ND*")
        (strcat (substr BName 1 2) "DN" (substr BName 5))
        BName
      )
    )

  7. #7
    The Silent Type RobertB's Avatar
    Join Date
    2000-01
    Location
    Seattle WA USA
    Posts
    5,859
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    Quote Originally Posted by irneb View Post
    That may cause names like CSCS... to be changed to DNCS...

    Why do you want to use the vl-string stuff? The OP's stipulated the 3rd & 4th character positions - you don't need to search for the position as vl-string-subst does.
    I assumed that the first 2 characters are probably "Tb" but didn't want to force that in the code so I used wildcards for the first two characters. Even if what you assume is a possiblity, it is easy to deal with in the vl-string-subst (which I should have posted to begin with), e.g.:
    Code:
    (setq newName (vl-string-subst "DN" (substr tbName 3 2) tbName 2))
    I don't understand what you mean by "you don't need to search for the position as vl-string-subst does." I am not searching for the position. vl-string-subst just finds the first instance and replaces it. I don't see how your code is any more efficient. The jury is out on which is more readable, hence maintainable. I'd argue that both are equal approaches.
    R. Robert Bell
    Design Technology Manager
    Stantec
    Opinions expressed are mine alone and do not reflect the views of Stantec.

  8. #8
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    Internally vl-string-subst uses vl-string-search to find the position of the string to be substituted, then it actually uses a similar approach to the strcat/substr method to replace the string with the new version. The wcmatch already searched the string, didn't it? Thus those 2 characters should be there, in those positions, why a second search?

    Both methods would produce the same result, but "equal" is pushing it a bit. Readability is in the eye of the beholder. As for efficiency, I think it speaks for itself.

    But we're discussing something which is not really of essence, the OP seems to be answered in either case.

  9. #9
    The Silent Type RobertB's Avatar
    Join Date
    2000-01
    Location
    Seattle WA USA
    Posts
    5,859
    Login to Give a bone
    0

    Default Re: Rename specific characters in a block name

    Quote Originally Posted by irneb View Post
    But we're discussing something which is not really of essence, the OP seems to be answered in either case.
    Agreed. But fun to discuss, none the less.
    R. Robert Bell
    Design Technology Manager
    Stantec
    Opinions expressed are mine alone and do not reflect the views of Stantec.

Similar Threads

  1. Replies: 6
    Last Post: 2012-08-15, 04:55 PM
  2. Rename block name
    By yazgunesi02 in forum AutoLISP
    Replies: 1
    Last Post: 2010-04-16, 09:57 PM
  3. Choosing a specific block within a dynamic block?
    By cadman_meg in forum Dynamic Blocks - Technical
    Replies: 5
    Last Post: 2007-10-11, 09:59 PM
  4. Multiple block Rename
    By BCrouse in forum AutoLISP
    Replies: 7
    Last Post: 2006-08-08, 04:24 PM
  5. Rename block
    By kennet.sjoberg in forum AutoLISP
    Replies: 2
    Last Post: 2004-09-21, 10:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •