How update a block (swap with block of same name)

Discussion in 'AutoCAD' started by Scott Mcfarren, Dec 7, 2004.

  1. When we revise a block in our library it would be nice to have a routine
    to put the new version of the block into any drawing "overwriting" the
    drawing's old version of the block (with the same name of course),
    but at the same time retaining all circumstances of each insertion
    (i.e. attribute values, scale, rotation etc.).

    This apparently works with the dialog version of the insert command,
    by browsing to find the new version of the block file, then you get the
    ". . .already exists - do you want to redefine it" alert. YES ! So how
    do I get around the dialog process in lisp ??? ("-insert" doesn't allow
    for this browsing - same name replacement - action).

    [Autocad are you listening - here we are fighting the dialog box
    interface thing AGAIN ! Please provide command line (lisp access)
    to ALL the dialog box functions]

    Thanks in advance,

    Scott McFarren
     
    Scott Mcfarren, Dec 7, 2004
    #1
  2. Scott Mcfarren

    OLD-CADaver Guest

    They did a long time ago. When promted for the name use a tilde ~

    Command: -INSERT
    Block Name: PEANUT=~

    and a file selection browse window will open.
     
    OLD-CADaver, Dec 7, 2004
    #2
  3. (command ".-INSERT" "BLOCKNAME=FILENAME")
    (command)
     
    Tony Tanzillo, Dec 7, 2004
    #3
  4. Scott Mcfarren

    Tom Smith Guest

    When we revise a block in our library it would be nice

    You weren't the first person to think of this.

    The blockname=filename method Tony mentioned is very, very old. This usage
    was provided for exactly the reasons you mention, because it's a
    near-universal scenario. I believe this has been around since my first use
    of Acad 2.6. Certainly it came long before dialog boxes and xrefs. This is
    what we did instead of xrefs before those became available.

    You can force a dialog interface to pop up with the "~" trick as OC noted,
    but assuming that the revised standard block exists somewhere on your search
    path, there's no reason you should need to browse to "find" it.
     
    Tom Smith, Dec 7, 2004
    #4
  5. Scott Mcfarren

    RDI Guest

    I've got my system set up so that acad.lsp loads an office wide lsp file,
    checks to see if there is file called Acad_prj.lsp in the same directory as
    the current drawing and if there is, loads it and then does a few other
    things.

    Any way, when a block for a certain project is changed, I include a call to
    UPDATEBLOCK in acad_prj.lsp. When someone opens a dwg file in the same
    directory as the acad_prj.lsp file, the block gets updated.

    (updateblock "northarrow")
    (defun UpdateBlock(name)
    (if (and (findfile (strcat name ".dwg"))(tblsearch "Block" name))
    (command "insert" (STRCAT name "=") "y" ^c)
    )
    )
     
    RDI, Dec 7, 2004
    #5
  6. Scott Mcfarren

    Tom Smith Guest

    (command "insert" (STRCAT name "=") "y" ^c)

    ^c is not an Acad command, nor is it a cancel. It's a special character code
    used in menu macros and has no meaning in lisp.

    It has the effect of cancelling your command sequence only because it's
    meaningless to the (command) function. You could use (command %#$&) with
    exactly the same effect. A cleaner way to terminate a command sequence is to
    call (command) with no arguments as in Tony's example.
     
    Tom Smith, Dec 7, 2004
    #6
  7. Thanks everbody.

    This is what works for me now. One pick on a block and all occurences
    are replaced (updated). YIPPEE !

    (setq SAMPK (entsel "\nPick sample of the block type to be replaced . .
    .."))
    (setq SAMBLK (entget (car SAMPK)))
    (setq BLKNAME (cdr (assoc 2 SAMBLK)))
    (setq INSNAME (strcat BLKNAME "=" BLKNAME ".dwg"))
    (command "_insert" INSNAME ^c^c)

    I'm not dealing with the browse step (but will keep in mind the suggestions
    for other routines).

    Scott
     
    Scott Mcfarren, Dec 7, 2004
    #7
  8. Scott Mcfarren

    Tom Smith Guest

    (command "_insert" INSNAME ^c^c)

    See Tony's response and my previous reply to RDI. Change to

    (command "_insert" INSNAME)
    (command)

    If you're fairly new to customization, you need to make sure you learn
    things Properly. A "^c" character sequence is not a command, nor is it an
    escape, nor is it meaningful in lisp. Essentially, you're just causing the
    (command) function to crash and burn.

    You might say this "works" in the present context, in that it does terminate
    the command sequence. However, you're going to be bitterly disappointed if
    you start believing that special menu-macro character codes work in lisp.
    Try issuing (command ^p) to run a plot, for instance, or any of the other
    control codes. They don't work.

    Also, this needs some bulletproofing before you hand it to anyone else. For
    instance, it will crash if the user doesn't pick an object, or if the object
    isn't a block, or if the block name doesn't match a block name on the search
    path. You might use it as an exercise in crash-proofing.
     
    Tom Smith, Dec 7, 2004
    #8
  9. Mr Smith:

    Your statement about browsing to find a file "should not be required
    if the file is in the path somewhere" is not correct. The files listed in
    the
    dialog pulldown are only block definitions in the current drawing, not the
    path. The browse operation forces Acad to look outside the current
    drawing for the block - which is exactly what I need to be done because
    the block in the file is what I need to replace.

    As for your comment about ^c^c having no meaning in lisp I shall refrain
    from embarrasing you publicly.

    I posted the code that worked initially so those who gracoiusly
    contributed ideas could see what at minimum worked. Bulletproofing
    distracts from the guts of the real work so that version was not shown.

    I am sorry you are having such a bad day.

    Scott
     
    Scott Mcfarren, Dec 7, 2004
    #9
  10. Scott Mcfarren

    Don I Guest

    I've used this for YEARS and have never once had a problem with it.
     
    Don I, Dec 7, 2004
    #10
  11. Scott Mcfarren

    OLD-CADaver Guest

    <<As for your comment about ^c^c having no meaning in lisp I shall refrain from embarrasing you publicly. >>

    Then embarrass me, I'm used to it. Tell me, what does ^c^c mean in your version of AutoLISP??
     
    OLD-CADaver, Dec 7, 2004
    #11
  12. Scott Mcfarren

    OLD-CADaver Guest

    <<I've used this for YEARS and have never once had a problem with it. >>

    Oh, no doubt, as long as you're looking for a bail from the routine, nearly anything can accomplish that as stated by Mr. Smith. But the more elegant method of closing a command is the (command), as previously stated.

    In more complex routines that may require a clean exit from a command followed by additional steps, you'll be disappointed with the ^c^c. Let's say that after you've inserted the new block definition you wish to ATTSYNC the existing attributes to the new definition. Using ^c^c will kill the routine before it gets to ATTSYNC, while using (command) will close the insert command and allow you to move on to the ATTSYNC command.

    Better yet avoid "(command" calls all together. (I know, I know, I still use it often myself, but I'm just lazy)
     
    OLD-CADaver, Dec 7, 2004
    #12
  13. I did not say anything about the appropiate use or effects of caret
    characters -
    I just don't like it when people say things in absolute like "has no meaning
    .. . .".
    Obviously they do. May not be to some's standards, sooo what !

    In these endeavors I tend not to rule out any means of getting code to work
    for me soon as possible. Some of you guys are taking yourselves and the
    coding
    too seriously.

    I've got to get back to work now.

    Thanks again anyway,

    Scott
     
    Scott Mcfarren, Dec 8, 2004
    #13
  14. Scott Mcfarren

    OLD-CADaver Guest

    <<I just don't like it when people say things in absolute like "has no meaning
    . .".
    Obviously they do. >>

    Okay, what do they mean in your version of AutoLISP.

    In my version, they have no meaning. There is no definition of a function in my version AutoLISP for ^c^c, just as there is no meaning for @#$%, which would have worked just as well for your use.



    <<Some of you guys are taking yourselves and the coding too seriously. >>

    Hmm... as far as I can tell, everybody here (except me) was attempting to be helpful in clarifying a coding error that will cause failure of your future coding. I, personally, think you owe Mr.Tanzillo and Mr. Smith an apology for your derision of their coding skills. Skills, i might add, that have been proven considerably more than competent for several years on these forums.
     
    OLD-CADaver, Dec 8, 2004
    #14
  15. I've got to get back to work now.

    Many of us are also working.

    If you don't want to learn why do you frequent this NG?

    I believe that the most important thing to learn for you
    are your manners.


    Marc'Antonio Alessi
     
    Marc'Antonio Alessi, Dec 8, 2004
    #15
  16. As for your comment about ^c^c having no meaning
    Ummmm. Well, while he may be a tad pedantic, Tom is
    nontheless correct and you're not going to embarrass him.

    In LISP ^C^C is just another symbol (all of which are
    initially assigned to the value NIL), hence:

    (command "._-INSERT" "BLOCKNAME" ^C^C )

    Is no different than

    (command "._-INSERT" "BLOCKNAME" NIL)

    which is no different than:

    (command "._-INSERT" "BLOCKNAME" BURP)

    Because the symbols 'BURP' and '^C^C' evaluate to nil. To wit:

    Command: (eval ^c^c)
    nil
    Command:
     
    Tony Tanzillo, Dec 8, 2004
    #16
  17. Yes but as you pointed out in your first post:

    the form:

    (command "._-INSERT" "BLOCKNAME") (command)

    is preferable then:

    (command "._-INSERT" "BLOCKNAME" NIL)


    --

    Marc'Antonio Alessi
    http://xoomer.virgilio.it/alessi
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    --
     
    Marc'Antonio Alessi, Dec 8, 2004
    #17
  18. Scott Mcfarren

    Tom Smith Guest

    I've used this for YEARS and have never once had a problem with it.

    I explained why it "works" -- or at least gives the appearance of working
    properly om this particular context. Unless the language is changed, it
    might appear to work properly for CENTURIES. I'm simply pointing out that
    the menu macro control code ^c isn't doing what you appear to think it's
    doing.

    As Tony elaborates below, the ^c is just an undefined symbol in lisp,
    therefore it evaluates to nil. You could use absolutely any undefined symbol
    to do the same thing -- (command MerryChristmas) amounts to precisely the
    same thing as (command ^c). You could use (command *crash*and*burn*) for
    years with exactly the same effect.

    Try a few of the other menu macro control codes in lisp and see what they
    do, for instance (command ^B) for toggling snaps, or (command ^G) for
    toggling grid. You'll find that none of them do what the code implies they
    should..
     
    Tom Smith, Dec 8, 2004
    #18
  19. Scott Mcfarren

    Tom Smith Guest

    Your statement about browsing to find a file "should not be required
    Several replies pointed you to the command sequence -insert
    <blockname>=<filename>, a the age-old method of redefining a block with
    respect to an external file. One suggestion gave a variant, namely -insert
    <blockname>=~ which will bring up a dialog to browse for the external file.

    If the external file is on the search path, then it can be inserted by
    entering its name alone, as in the first syntax. Since you know the name of
    the block that needs to be updated, and you presumably know it's on your
    path if it's part of a standard block library, then manually browsing to
    identify the file, as in the second syntax, isn't necessary. Like I said.

    And like you did. I note that you didn't do this in your "initial" pass at
    the code, and stated "I'm not dealing with the browse step." In fact, my
    statement was and is correct, and your code reflects it.
    Go ahead, I can take it! Hint: if you want to know the "meaning" of a thing
    in lisp, you can use the evaluation function, as in (eval ^c).

    It evaluates to the same thing as %#$& or any other symbol which has not
    been bound to a value, namely nil, which is about as close as you can come
    to "having no meaning" in lisp. Using an explicit nil as in (command nil)
    would be better coding practice than using a random series of characters
    which evaluates to nil, for clarity if nothing else. Calling (command) with
    no arguments, however, is a more conventional way of cancelling a command
    sequence in lisp.
     
    Tom Smith, Dec 8, 2004
    #19
  20. Scott Mcfarren

    Tom Smith Guest

    while he may be a tad pedantic

    LOL Tony you're not often so diplomatic, thanks.
     
    Tom Smith, Dec 8, 2004
    #20
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.