Author Topic: Block update  (Read 2509 times)

0 Members and 1 Guest are viewing this topic.

Fabricio28

  • Swamp Rat
  • Posts: 670
Block update
« on: September 12, 2014, 12:51:36 PM »
Hi all,

I've used that code in many files. Now I had to make a modification in MARGEM block.
How Can I do to update MARGEM block in thoses files?

I opened the file and invoke command and nothing changed. But when I insert manually the block updated.

Code: [Select]
;COMANDOS PARA A INSERÇÃO DE BLOCOS CROQUI
(defun c:CQ ()
(command "_.layer" "_T" "MARGEM" "_M" "MARGEM" "c" "7" "" ""
     "_.layer" "_T" "MARGEM" "_U" "MARGEM" "_M" "MARGEM" "c" "7" "" "")

(command "LAYER" "S" "MARGEM" "" "insert" "MARGEM" pause "" "" "")
(setvar 'imageframe 2)
(setvar 'imageframe 0)
(command "_.regenall")
)


danallen

  • Guest
Re: Block update
« Reply #1 on: September 12, 2014, 02:54:32 PM »
this is my subroutine for updating block definition from file, needs vars BNAME for blockname, and BFNAME for path including drawing file name with .DWG

if I recall correctly the use of = in the insert command causes redefinition

Code: [Select]
(progn
  (command "-insert" (strcat bname "=" bfname))
  (if (tblsearch "BLOCK" bname) (command "y")) ;yes to redefine
  (command) (command) ;cancel insert after redefine
  (princ (strcat "\nUpdated block " bname " from file " bfname))
  ;; find block insertions & update
  (setq inslst (ssget "X" (list (cons 0 "INSERT")(cons 2 bname)(cons 67 (if (XYZ_IS_PAPER) 1 0)))))
  (if inslst (XYZ_MAPSET '(lambda (x) (entupd x)) inslst nil)) ;requires external routine
)
;;;=========================================================================
;;; XYZ_is_paper - returns nil for modelspace, T for paperspace
;;;  by Vladimir Nesterovsky
;;; use (cons 67 (if (XYZ_is_paper) 1 0)) for ssget "X" of mspace vs. pspace
;;;  note that pspace will get all objects in layouts, not just current
;;;=========================================================================
(defun XYZ_is_paper () (> 2 (getvar "cvport") (getvar "tilemode")))

;;;=========================================================================
;;; Apply func to every entity in ss - from Looking Glass Microproducts
;;;  ex: (XYZ_MAPSET '(lambda (x) (redraw x 2)) ss1 nil)
;;;=========================================================================
(defun XYZ_mapset (func ss prmpt / i l suffix ename)
  (if prmpt (SETV "modemacro" (getvar "modemacro")))
  (If ss
    (Progn (Setq func (Eval func)
i 0
l (SsLength ss)
suffix (Strcat " of " (ItoA l))
   ) ;_ Setq
   (Repeat l
     (Setq i (1+ i)
   ename (SsName ss (- l i))
     ) ;_ Setq
     (If prmpt
       (Setvar "modemacro" (Strcat prmpt " " (ItoA i) suffix))
     ) ;_ If
     (princ "")
     (func ename)
   ) ;_ Repeat
   (If prmpt
     (RSETV "modemacro")
   ) ;_ If
   ss
    ) ;_ Progn
  ) ;_ If
) ;_ Defun

;==========================================================
; SETV function saves setvar settings to be reset at end with RSETV
;     (setv "cmdecho" 0) set cmdecho off
;     (rsetv "cmdecho")  resets cmdecho (see below)
;   taken from Essential AutoLISP by Roy Harkow
;==========================================================
(defun SETV (sysvar newval / cmdnam)
  (setq cmdnam (read (strcat sysvar "1"))) ;Create   [savevar]1
  (set cmdnam (getvar sysvar)) ;Save     [savevar]'s value
  (setvar sysvar newval) ;Then set [savevar] to new value
)

(defun RSETV (sysvar / )
  (if (eval (read (strcat sysvar "1"))) ;Only change if exists
    (progn
      (setq cmdnam (read (strcat sysvar "1"))) ;Create   [savevar]1
      (setvar sysvar (eval cmdnam)) ;Restore  [savevar]'s value
      (set cmdnam nil)
    ) ;end progn
  ) ;end if
)


Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Block update
« Reply #2 on: September 12, 2014, 03:19:45 PM »
Thank you for the quick replay danallen,

I tried to used your subrotine without sucess.

blockname: MARGEM
path of my block: C:\Users\Owner\Desktop\MARGEM\MARGEM.dwg

I open my file and the block isn't redefining.

Could you help me, how to use the subrotine, please?


danallen

  • Guest
Re: Block update
« Reply #3 on: September 12, 2014, 03:40:26 PM »
please post your code that you attempted

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Block update
« Reply #4 on: September 12, 2014, 03:44:52 PM »
I'm not a programmer. Just a curious   :-o

Code: [Select]
(progn
  (command "-insert" (strcat bname "=" bfname))
  (if (tblsearch "MARGEM" bname) (command "y")) ;yes to redefine
  (command) (command) ;cancel insert after redefine
  (princ (strcat "\nUpdated block " bname " C:\Users\Owner\Desktop\MARGEM\MARGEM.dwg " bfname))
  ;; find block insertions & update
  (setq inslst (ssget "X" (list (cons 0 "INSERT")(cons 2 bname)(cons 67 (if (XYZ_IS_PAPER) 1 0)))))
  (if inslst (XYZ_MAPSET '(lambda (x) (entupd x)) inslst nil)) ;requires external routine
)
;;;=========================================================================
;;; XYZ_is_paper - returns nil for modelspace, T for paperspace
;;;  by Vladimir Nesterovsky
;;; use (cons 67 (if (XYZ_is_paper) 1 0)) for ssget "X" of mspace vs. pspace
;;;  note that pspace will get all objects in layouts, not just current
;;;=========================================================================
(defun XYZ_is_paper () (> 2 (getvar "cvport") (getvar "tilemode")))

;;;=========================================================================
;;; Apply func to every entity in ss - from Looking Glass Microproducts
;;;  ex: (XYZ_MAPSET '(lambda (x) (redraw x 2)) ss1 nil)
;;;=========================================================================
(defun XYZ_mapset (func ss prmpt / i l suffix ename)
  (if prmpt (SETV "modemacro" (getvar "modemacro")))
  (If ss
    (Progn (Setq func (Eval func)
i 0
l (SsLength ss)
suffix (Strcat " of " (ItoA l))
   ) ;_ Setq
   (Repeat l
     (Setq i (1+ i)
   ename (SsName ss (- l i))
     ) ;_ Setq
     (If prmpt
       (Setvar "modemacro" (Strcat prmpt " " (ItoA i) suffix))
     ) ;_ If
     (princ "")
     (func ename)
   ) ;_ Repeat
   (If prmpt
     (RSETV "modemacro")
   ) ;_ If
   ss
    ) ;_ Progn
  ) ;_ If
) ;_ Defun

;==========================================================
; SETV function saves setvar settings to be reset at end with RSETV
;     (setv "cmdecho" 0) set cmdecho off
;     (rsetv "cmdecho")  resets cmdecho (see below)
;   taken from Essential AutoLISP by Roy Harkow
;==========================================================
(defun SETV (sysvar newval / cmdnam)
  (setq cmdnam (read (strcat sysvar "1"))) ;Create   [savevar]1
  (set cmdnam (getvar sysvar)) ;Save     [savevar]'s value
  (setvar sysvar newval) ;Then set [savevar] to new value
)

(defun RSETV (sysvar / )
  (if (eval (read (strcat sysvar "1"))) ;Only change if exists
    (progn
      (setq cmdnam (read (strcat sysvar "1"))) ;Create   [savevar]1
      (setvar sysvar (eval cmdnam)) ;Restore  [savevar]'s value
      (set cmdnam nil)
    ) ;end progn
  ) ;end if
)

danallen

  • Guest
Re: Block update
« Reply #5 on: September 12, 2014, 04:01:42 PM »
if you don't want to program, just try this command lisp
note you need double backslashes in paths in lisp

Code: [Select]

;COMANDOS PARA A INSERÇÃO DE BLOCOS CROQUI
(defun c:CQ ()
(command "_.layer" "_T" "MARGEM" "_M" "MARGEM" "c" "7" "" ""
     "_.layer" "_T" "MARGEM" "_U" "MARGEM" "_M" "MARGEM" "c" "7" "" "")

(command "LAYER" "S" "MARGEM" "")
(command "-insert" "MARGEM=C:\\Users\\Owner\\Desktop\\MARGEM\\MARGEM.dwg" )(if (tblsearch "BLOCK" "MARGEM") (command "y"))
(command pause "" "" "")
(setvar 'imageframe 2)
(setvar 'imageframe 0)
(command "_.regenall")
)


Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Block update
« Reply #6 on: September 12, 2014, 04:11:35 PM »
Genius danallen!!  :-D

I'm really happy, you're very kind.

Thank you very much

danallen

  • Guest
Re: Block update
« Reply #7 on: September 12, 2014, 04:13:20 PM »
you are welcome, do keep trying to learn, that is how I started 18 years ago with such simple commands as you are working on

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Block update
« Reply #8 on: September 12, 2014, 04:19:56 PM »
you are welcome, do keep trying to learn, that is how I started 18 years ago with such simple commands as you are working on

I can write very simple command, not so fancy yet.
I've learnt so much since I became a member on this great forum. Thank to people like you!

RC

  • Guest
Re: Block update
« Reply #9 on: September 15, 2014, 02:15:25 PM »
You may already know this but just to clarify the problem you were having AutoCAD was assuming you wanted to insert the "Margem" block that was already defined in your drawing file.  You have to tell it you want to use a definition from another source, that's why it did not update the block from the directory.

LONG hand solutions are
Use the INSERT dialog box and 'browse' for the DWG file you wish to use as a replacement block and  you will be prompted to redefine the block.
or
Use the command line version of -INSERT and when prompted for the block to insert key-in
MARGEM=C:\Users\Owner\Desktop\MARGEM\MARGEM
you will be prompted to redefine  (that is how Mr. Allen's routine functions)
or
Use the command line version of -INSERT and when prompted for the block to insert key-in
MARGEM=~
the tilde ~ will open a windows file selection window and you can then 'browse' to the proper location for the file and select the right dwg.
you will be prompted to redefine.

For those of us old enough to remember AutoCAD WITHOUT reference files, this is how we obtained a reference file type function.  Every so often we would 'refresh' a block from the network using the "=" function to redefine the block.  For 'known' files we could automate the update with lisp, extracting the name of the block by selecting it and building the file location into the code; enter UDB select the block and bingo it was updated.

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: Block update
« Reply #10 on: September 15, 2014, 03:04:21 PM »
Many thanks for the comments, Randy!

very kind of you clarify the problem. I really appreciate that. :)

RC

  • Guest
Re: Block update
« Reply #11 on: September 15, 2014, 06:52:47 PM »
you are welcome, it helps to know how the application functions so when you do get into coding you know what to expect.  Sometimes we forget there are folks reading these boards that don't have a complete understanding of many functions.