+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

Thread: Code help

  1. #1
    Forum Newbie
    Using
    Civil 3D 2010
    Join Date
    Aug 2009
    Posts
    8

    Default Code help

    Registered forum members do not see this ad.

    I have a list of blocks that i need to replace when there is one in the dwg file. Below is the code that i use to do that with but i would like to add to this.
    My problem
    When we receive files from our surveyor the blocks are repeated; example, if we have a block named X then it creates a new block for every occurrence (like so X_1, X_2, X_3) and so on. So the code below allows me to select all the blocks that have an X_ in the name and then it will replace with the correct name X. Currently I use the quick select command >block reference > Name > wilcard match > to search and find any block with an X_ in the name.

    My Goal
    For the code to do all the replaces using a list in the code.
    I would like to add the search to the lisp and if no blocks in current file the just ignore.
    the list would be something like
    X_
    APOINT_
    MH_
    FH_
    I will compile the list in the code while testing.
    then the code will replace name with name from another list
    X
    APOINT
    MH
    FH
    and so.

    Basicly I have a dwg with about 4000 blocks in it and it should only have abut 100.
    X_1
    X_2
    X_1000
    Should all be X

    Thanks for the help

    (DEFUN C:replace ()
    (SETQ SS (SSGET))
    (SETQ L 0)
    (SETQ NB (GETSTRING "\nNEW BLOCK NAME TO REPLACE OLD:"))
    (SETQ N (SSLENGTH SS))
    (WHILE (< L N)
    (SETQ E (ENTGET (SSNAME SS L)))
    (SETQ E
    (SUBST (CONS 2 NB)
    (ASSOC 2 E)
    E
    )
    )
    (ENTMOD E)
    (SETQ L (1+ L))
    )
    )

  2. #2
    Senior Member
    Using
    Map 3D 2008
    Join Date
    Apr 2009
    Location
    The one after 909
    Posts
    402

    Default

    This should get you started. My lunch break is almost over so I can't do it all for you, but it'll help with the automation:

    Code:
    (mapcar '(lambda (x) (ssget "X" (list (cons 2 x)))) (list "X_*" "APOINT_*"))
    That returns a list of selection sets, the first one being all blocks with a name that begins with "X_", and so on. You can make the list as long as you'd like.

    Alls you need is to loop through and do something with each selection set, like a block replace.

    Good luck, I'm sure you've got enough smarts to figure out the rest, if you've come so far already. ^.^

    EDIT: And before anyone "corrects" me, yes, you could probably just extend the Lambda to include the block replace. I just don't have the time.

    EDIT EDIT: Meh..

    See if this works, it's untested. It'll insert the correct block at the same insertion point of the incorrect block, then delete the incorrect one. At least, that's the idea. My fingers are crossed.

    Code:
    (defun c:test( / ss entSet)
      (mapcar '(lambda (x)
             (setq ss (ssget "X" (list (cons 2 (strcat x "_*")))))
             (if ss
               (progn
             (setq entSet (vl-remove-if-not '(lambda (y) (= (type (cadr y)) 'ENAME)) (ssnamex ss)))
             (mapcar '(lambda (z) (vl-cmdf "-insert" x (cdr (assoc 10 (entget z))) "" "" "" "erase" z "")) entSet)
             )
               )
             )
          (list "X" "APOINT"))
      )
    Last edited by Freerefill; 26th Aug 2009 at 05:42 pm.
    ~* And, in the end, the love you take *~
    ~* Is equal to the love you make *~
    - The Beatles -


    GrPlayground
    / Text Find/Replace / Batch Engine / Tower Defense

  3. #3
    Forum Newbie
    Using
    Civil 3D 2010
    Join Date
    Aug 2009
    Posts
    8

    Default

    First of all thanks for the hlep!

    This is what i get when running the code.

    test ; error: bad argument type: lentityp (0 <Entity name: -42ebaaf0>
    0)

  4. #4
    Senior Member
    Using
    Map 3D 2008
    Join Date
    Apr 2009
    Location
    The one after 909
    Posts
    402

    Default

    Quote Originally Posted by rphillips View Post
    First of all thanks for the hlep!

    This is what i get when running the code.

    test ; error: bad argument type: lentityp (0 <Entity name: -42ebaaf0>
    0)
    Yep, I know what went wrong.. forgot to mapcar a 'cadr to the vl-remove-if-not.

    Try this:

    Code:
    (defun c:test( / ss entSet)
      (mapcar '(lambda (x)
             (setq ss (ssget "X" (list (cons 2 (strcat x "_*")))))
             (if ss
               (progn
             (setq entSet (mapcar 'cadr (vl-remove-if-not '(lambda (y) (= (type (cadr y)) 'ENAME)) (ssnamex ss))))
             (mapcar '(lambda (z) (vl-cmdf "-insert" x (cdr (assoc 10 (entget z))) "" "" "" "erase" z "")) entSet)
             )
               )
             )
          (list "X" "APOINT"))
      )
    ~* And, in the end, the love you take *~
    ~* Is equal to the love you make *~
    - The Beatles -


    GrPlayground
    / Text Find/Replace / Batch Engine / Tower Defense

  5. #5
    Forum Newbie
    Using
    Civil 3D 2010
    Join Date
    Aug 2009
    Posts
    8

    Default

    Thanks, I think that this works but i dont understand the code. This process takes to long there is about 4000 blocks and i belive this code has to repeat for every block.

    This is what i am going to use

    (setq SS (ssget "x" (list (cons 2 "X_*"))))
    (if ss
    (progn
    (SETQ L 0)
    (SETQ N (SSLENGTH SS))
    (WHILE (< L N)
    (SETQ E (ENTGET (SSNAME SS L)))
    (SETQ E
    (SUBST (CONS 2 "X")
    (ASSOC 2 E)
    E
    )
    )
    (ENTMOD E)
    (SETQ L (1+ L))
    )
    )
    )


    then i will repeat the code for to search for other blocks this seams to be a lot faster.

    At the begining of my code i set cmdecho to 0 and then at the end i set it to 1 but for some reason when i do a purge it still list all the blocks deleted. Is there another way to get the command line not to repeat the info?

  6. #6
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,789

    Default

    FYI,

    ssnamex
    is a slow process and will take a long time to execute for a large number of blocks.
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  7. #7
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,789

    Default

    If you are substituting the Block Name, the alternative block definition must be in the drawing also.

    The old block definitions will also still be in the database and will need to be purged.
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  8. #8
    Forum Newbie
    Using
    Civil 3D 2010
    Join Date
    Aug 2009
    Posts
    8

    Default

    (DEFUN C:RB ( / OriginalErrorHandling)

    (vl-load-com)
    (setq OriginalErrorHandling *error*)
    (setq *error* ReplaceBlock_Error)
    (setvar "cmdecho" 0)
    (command "undo" "begin")
    (command"ucs" "")
    (setq SS (ssget "x" (list (cons 2 "X_*"))))
    (if ss
    (progn
    (SETQ L 0)
    (SETQ N (SSLENGTH SS))
    (WHILE (< L N)
    (SETQ E (ENTGET (SSNAME SS L)))
    (SETQ E
    (SUBST (CONS 2 "X")
    (ASSOC 2 E)
    E
    )
    )
    (ENTMOD E)
    (SETQ L (1+ L))
    )
    )
    )
    (setq SS (ssget "x" (list (cons 2 "APOINT_*"))))
    (if ss
    (progn
    (SETQ L 0)
    (SETQ N (SSLENGTH SS))
    (WHILE (< L N)
    (SETQ E (ENTGET (SSNAME SS L)))
    (SETQ E
    (SUBST (CONS 2 "APOINT")
    (ASSOC 2 E)
    E
    )
    )
    (ENTMOD E)
    (SETQ L (1+ L))
    )
    )
    )
    (command"-purge" "blocks" "" "no")
    (command "ucs" "p")
    (command "undo" "end")
    (setvar "cmdecho" 1)
    (princ)
    )



    This is what i have so far
    the lines in red are the lines that i wll have to change
    the text in cyan is what will be changed in each line.

    Does anyone see a better way to do this.
    thanks

  9. #9
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    16,789

    Default

    Does BLOCKREPLACE from ET not cut it?
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  10. #10
    Forum Newbie
    Using
    Civil 3D 2010
    Join Date
    Aug 2009
    Posts
    8

    Default

    Registered forum members do not see this ad.

    Sure it will work if i want to go and pick all the blocks.
    Please read first post for more information.
    Thanks for the help

    I know the blocks will still be there that is why there is a purge at the end of the code. I just dont what the end user to see all the blocks being deleted.

Similar Threads

  1. Vba Code
    By JONTHEPOPE in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 20th Feb 2009, 07:48 pm
  2. Ability to write code? or ability to see need for code?
    By feargt in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 5th Oct 2008, 09:04 pm
  3. Help with code
    By cadmoogle in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 16th Oct 2007, 05:49 am
  4. My first code...
    By Seann in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 2nd Feb 2007, 08:09 pm
  5. VBA Code????
    By acatalin2003 in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 4th Oct 2005, 11:20 am

Bookmarks

Posting Permissions

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