Author Topic: substitute block with another block, is this the best way to do it?  (Read 3268 times)

0 Members and 1 Guest are viewing this topic.

gp.triple

  • Mosquito
  • Posts: 14
hello for update my P&ID i wrote  a lisp that check the filename and substitute a block "A" with a block "1", "B" with "2" etc etc

it's working fine but i'm not sure that is the best way to do it.

here an extract:

Code - Auto/Visual Lisp: [Select]
  1. ;;sostituisco linee con quelle straniere
  2. ;; Esempio sostituisco "A" con "CW"
  3.   (if
  4.     (and
  5.         (Setq lin_cw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_cooling-water.dwg"))         ;;definisco blocco da inserire RIPETERE UN SETQ PER OGNI LINEA
  6.                   (Setq lin_pw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_potable-water.dwg"))
  7.                   (Setq lin_hw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_hot-water.dwg"))
  8.                   (Setq lin_dw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_demi-water.dwg"))
  9.                   (Setq lin_chw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_chilled-water.dwg"))
  10.                   (Setq lin_fw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_fire-water.dwg"))
  11.                   (Setq lin_ww (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_well-water.dwg"))
  12.                   (Setq lin_c (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_steam-condensate.dwg"))
  13.                   (Setq lin_s (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_steam.dwg"))
  14.                   (Setq lin_hs (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_high-pressure-steam.dwg"))
  15.                   (Setq lin_pa (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_plant-air.dwg"))
  16.                   (Setq lin_ia (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_instrument-air.dwg"))
  17.                   (Setq lin_ba (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_breathing-air.dwg"))
  18.                   (Setq lin_sa (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_atmospheric-air-in-suction.dwg"))
  19.                   (Setq lin_aa (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_atmospheric-air.dwg"))
  20.                   (Setq lin_p (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_process-fluid.dwg"))
  21.                   (Setq lin_n (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_nitrogen.dwg"))
  22.                   (Setq lin_ni(findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_nitrogen-low-purity.dwg"))
  23.                   (Setq lin_ga (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_carbon-dioxide.dwg"))
  24.                   (Setq lin_gi (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_inert-gas.dwg"))
  25.                   (Setq lin_av (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_atmospheric-vent.dwg"))
  26.                   (Setq lin_pv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_vent-to-blow-down.dwg"))
  27.                   (Setq lin_tv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_vent-to-tox.dwg"))
  28.                   (Setq lin_sv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_safety-valve-vent.dwg"))
  29.                   (Setq lin_vv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_vacuum-vent.dwg"))
  30.                 (Setq lin_ns (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_brine.dwg"))
  31.                 (Setq lin_nh (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_ammonia.dwg"))
  32.                 (Setq lin_ps (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_process-sewer.dwg"))
  33.                 (Setq lin_uu (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_chemical-sewer.dwg"))
  34.                 (Setq lin_ss (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_meteoric-sewer.dwg"))
  35.                 (Setq lin_ff (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_foam-fluid.dwg"))
  36.                                    
  37.                 (       Vl-some '(lambda (nme)                         
  38.                         (if (wcmatch (strcase (getvar "DWGNAME"))
  39.                                      (strcat nme "*.DWG"))                                                                                                              ;;se il file inizia con
  40.                           nme
  41.                         )
  42.                       )
  43.                      '("CH" "C1" "C2" "LI" "MN" "ON" "NO" "RJ" "SH" "WT")                                                       ;;definisco iniziali nome file
  44.             )
  45.         )
  46.         (progn                                                                                                                                                                                          ;;procedura sostituzione
  47.           (command "_-insert" lin_cw "-50,0" "" "" "" "" "" "" "" "" "")                                ;;inserisco blocco DEFINITO  NEL SETQ
  48.           (entdel (entlast))
  49.                 (if (setq ss (ssget "_X" '((2 . "lin_acquatorre"))))                                                            ;;blocco da sostituire
  50.                         (repeat (setq n (sslength ss))
  51.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  52.                           (entmod (subst '(2 . "lin_cooling-water") '(2 . "lin_acquatorre") edata))     ;;Prima blocco nuovo e poi quello da cambiare
  53.                         )
  54.                 )                                                                                                                                                                              
  55.           (command "_-insert" lin_pw "-50,0" "" "" "" "" "" "" "" "" "")                                                ;; inserisco blocco DEFINITO  NEL SETQ
  56.           (entdel (entlast))
  57.                 (if (setq ss (ssget "_X" '((2 . "lin_acquapotabile"))))                                                         ;blocco da sostituire
  58.                         (repeat (setq n (sslength ss))
  59.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  60.                           (entmod (subst '(2 . "lin_potable-water") '(2 . "lin_acquapotabile") edata))  ;;Prima blocco nuovo e poi quello da cambiare
  61.                         )
  62.                 )
  63.           (command "_-insert" lin_hw "-50,0" "" "" "" "" "" "" "" "" "")                                                ;; inserisco blocco DEFINITO  NEL SETQ
  64.           (entdel (entlast))
  65.                 (if (setq ss (ssget "_X" '((2 . "lin_acquacalda"))))                                                            ;blocco da sostituire
  66.                         (repeat (setq n (sslength ss))
  67.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  68.                           (entmod (subst '(2 . "lin_hot-water") '(2 . "lin_acquacalda") edata))         ;;Prima blocco nuovo e poi quello da cambiare
  69.                         )
  70.                 )
  71.           (command "_-insert" lin_dw "-50,0" "" "" "" "" "" "" "" "" "")                                                ;; inserisco blocco DEFINITO  NEL SETQ
  72.           (entdel (entlast))
  73.                 (if (setq ss (ssget "_X" '((2 . "lin_acquademi"))))                                                             ;blocco da sostituire
  74.                         (repeat (setq n (sslength ss))
  75.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  76.                           (entmod (subst '(2 . "lin_demi-water") '(2 . "lin_acquademi") edata))         ;;Prima blocco nuovo e poi quello da cambiare
  77.                         )
  78.                 )
  79.           (command "_-insert" lin_chw "-50,0" "" "" "" "" "" "" "" "" "")                                               ;; inserisco blocco DEFINITO  NEL SETQ
  80.           (entdel (entlast))
  81.                 (if (setq ss (ssget "_X" '((2 . "lin_acquagelida"))))                                                           ;blocco da sostituire
  82.                         (repeat (setq n (sslength ss))
  83.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  84.                           (entmod (subst '(2 . "lin_chilled-water") '(2 . "lin_acquagelida") edata))    ;;Prima blocco nuovo e poi quello da cambiare
  85.                         )
  86.                 )
  87.            (command "_-insert" lin_fw "-50,0" "" "" "" "" "" "" "" "" "")                                               ;; inserisco blocco DEFINITO  NEL SETQ
  88.            (entdel (entlast))
  89.                 (if (setq ss (ssget "_X" '((2 . "lin_acquantincendio"))))                                                               ;blocco da sostituire
  90.                         (repeat (setq n (sslength ss))
  91.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  92.                           (entmod (subst '(2 . "lin_fire-water") '(2 . "lin_acquantincendio") edata))   ;;Prima blocco nuovo e poi quello da cambiare
  93.                         )
  94.                 )

is possible to write a list of original block and a list of new block an cycle the substitution?

 

thanks
« Last Edit: December 02, 2021, 04:36:07 AM by gp.triple »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: substitute block with another block, is this the best way to do it?
« Reply #1 on: December 02, 2021, 10:08:36 AM »
Here's an untested example:
Code - Auto/Visual Lisp: [Select]
  1. (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  2. (foreach file
  3.          '(("lin_cooling-water.dwg" "lin_acquatorre") ("lin_potable-water.dwg" "lin_acquapotabile"))
  4.   (if (findfile (strcat path (car file)))
  5.     (progn (command "_-insert" (strcat path (car file)) '(0 0 0) nil)
  6.            (if (setq ss (ssget "_X" (list (cons 2 (car file)))))
  7.              ;;blocco da sostituire
  8.              (repeat (setq n (sslength ss))
  9.                (setq edata (entget (ssname ss (setq n (1- n)))))
  10.                (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  11.                ;;Prima blocco nuovo e poi quello da cambiare
  12.              )
  13.            )
  14.     )
  15.   )
  16. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mhupp

  • Bull Frog
  • Posts: 250
Re: substitute block with another block, is this the best way to do it?
« Reply #2 on: December 03, 2021, 01:46:32 AM »
If you use "blockname=path to new block" with the insert command it will update the block library with the new file. then you wont need to entmod all the blocks. but it is important to have the same insertion point relative to the two blocks or things might be shifted.

Code - Auto/Visual Lisp: [Select]
  1. (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  2. (foreach file '(("lin_cooling-water.dwg" "lin_acquatorre") ("lin_potable-water.dwg" "lin_acquapotabile"))
  3.   (if (findfile (strcat path (car file)))
  4.     (progn
  5.       (command "_.Insert" (strcat (cadr file) "=" path (car file)) '(0 0 0) 1 1 0)
  6.       (entdel (entlast))
  7.     )
  8.   )
  9. )

easy to read example
Code - Auto/Visual Lisp: [Select]
  1. (command "_.Insert" "lin_acquatorre=C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_cooling-water.dwg" '(0 0 0) 1 1 0)
« Last Edit: December 03, 2021, 02:59:35 AM by mhupp »

gp.triple

  • Mosquito
  • Posts: 14
Re: substitute block with another block, is this the best way to do it?
« Reply #3 on: December 06, 2021, 03:03:50 AM »
Here's an untested example:
Code - Auto/Visual Lisp: [Select]
  1. (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  2. (foreach file
  3.          '(("lin_cooling-water.dwg" "lin_acquatorre") ("lin_potable-water.dwg" "lin_acquapotabile"))
  4.   (if (findfile (strcat path (car file)))
  5.     (progn (command "_-insert" (strcat path (car file)) '(0 0 0) nil)
  6.            (if (setq ss (ssget "_X" (list (cons 2 (car file)))))
  7.              ;;blocco da sostituire
  8.              (repeat (setq n (sslength ss))
  9.                (setq edata (entget (ssname ss (setq n (1- n)))))
  10.                (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  11.                ;;Prima blocco nuovo e poi quello da cambiare
  12.              )
  13.            )
  14.     )
  15.   )
  16. )

I didn't know "foreach" function, thanks now i've to study how to use it correctly

gp.triple

  • Mosquito
  • Posts: 14
Re: substitute block with another block, is this the best way to do it?
« Reply #4 on: December 06, 2021, 03:07:19 AM »
If you use "blockname=path to new block" with the insert command it will update the block library with the new file. then you wont need to entmod all the blocks. but it is important to have the same insertion point relative to the two blocks or things might be shifted.

Code - Auto/Visual Lisp: [Select]
  1. (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  2. (foreach file '(("lin_cooling-water.dwg" "lin_acquatorre") ("lin_potable-water.dwg" "lin_acquapotabile"))
  3.   (if (findfile (strcat path (car file)))
  4.     (progn
  5.       (command "_.Insert" (strcat (cadr file) "=" path (car file)) '(0 0 0) 1 1 0)
  6.       (entdel (entlast))
  7.     )
  8.   )
  9. )

easy to read example
Code - Auto/Visual Lisp: [Select]
  1. (command "_.Insert" "lin_acquatorre=C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_cooling-water.dwg" '(0 0 0) 1 1 0)

yes the block have the same insertion point, thanks for sharing i didn't know that's possible to use "blockname=path to new block"

CatDance

  • Newt
  • Posts: 57
Re: substitute block with another block, is this the best way to do it?
« Reply #5 on: December 09, 2021, 12:27:08 AM »
hello for update my P&ID i wrote  a lisp that check the filename and substitute a block "A" with a block "1", "B" with "2" etc etc

it's working fine but i'm not sure that is the best way to do it.

here an extract:

Code - Auto/Visual Lisp: [Select]
  1. ;;sostituisco linee con quelle straniere
  2. ;; Esempio sostituisco "A" con "CW"
  3.   (if
  4.     (and
  5.         (Setq lin_cw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_cooling-water.dwg"))         ;;definisco blocco da inserire RIPETERE UN SETQ PER OGNI LINEA
  6.                   (Setq lin_pw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_potable-water.dwg"))
  7.                   (Setq lin_hw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_hot-water.dwg"))
  8.                   (Setq lin_dw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_demi-water.dwg"))
  9.                   (Setq lin_chw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_chilled-water.dwg"))
  10.                   (Setq lin_fw (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_fire-water.dwg"))
  11.                   (Setq lin_ww (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_well-water.dwg"))
  12.                   (Setq lin_c (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_steam-condensate.dwg"))
  13.                   (Setq lin_s (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_steam.dwg"))
  14.                   (Setq lin_hs (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_high-pressure-steam.dwg"))
  15.                   (Setq lin_pa (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_plant-air.dwg"))
  16.                   (Setq lin_ia (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_instrument-air.dwg"))
  17.                   (Setq lin_ba (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_breathing-air.dwg"))
  18.                   (Setq lin_sa (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_atmospheric-air-in-suction.dwg"))
  19.                   (Setq lin_aa (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_atmospheric-air.dwg"))
  20.                   (Setq lin_p (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_process-fluid.dwg"))
  21.                   (Setq lin_n (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_nitrogen.dwg"))
  22.                   (Setq lin_ni(findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_nitrogen-low-purity.dwg"))
  23.                   (Setq lin_ga (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_carbon-dioxide.dwg"))
  24.                   (Setq lin_gi (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_inert-gas.dwg"))
  25.                   (Setq lin_av (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_atmospheric-vent.dwg"))
  26.                   (Setq lin_pv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_vent-to-blow-down.dwg"))
  27.                   (Setq lin_tv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_vent-to-tox.dwg"))
  28.                   (Setq lin_sv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_safety-valve-vent.dwg"))
  29.                   (Setq lin_vv (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_vacuum-vent.dwg"))
  30.                 (Setq lin_ns (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_brine.dwg"))
  31.                 (Setq lin_nh (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_ammonia.dwg"))
  32.                 (Setq lin_ps (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_process-sewer.dwg"))
  33.                 (Setq lin_uu (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_chemical-sewer.dwg"))
  34.                 (Setq lin_ss (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_meteoric-sewer.dwg"))
  35.                 (Setq lin_ff (findfile  "C:\\dati\\Lamberti_std\\block_update\\blocks\\lin_foam-fluid.dwg"))
  36.                                    
  37.                 (       Vl-some '(lambda (nme)                         
  38.                         (if (wcmatch (strcase (getvar "DWGNAME"))
  39.                                      (strcat nme "*.DWG"))                                                                                                              ;;se il file inizia con
  40.                           nme
  41.                         )
  42.                       )
  43.                      '("CH" "C1" "C2" "LI" "MN" "ON" "NO" "RJ" "SH" "WT")                                                       ;;definisco iniziali nome file
  44.             )
  45.         )
  46.         (progn                                                                                                                                                                                          ;;procedura sostituzione
  47.           (command "_-insert" lin_cw "-50,0" "" "" "" "" "" "" "" "" "")                                ;;inserisco blocco DEFINITO  NEL SETQ
  48.           (entdel (entlast))
  49.                 (if (setq ss (ssget "_X" '((2 . "lin_acquatorre"))))                                                            ;;blocco da sostituire
  50.                         (repeat (setq n (sslength ss))
  51.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  52.                           (entmod (subst '(2 . "lin_cooling-water") '(2 . "lin_acquatorre") edata))     ;;Prima blocco nuovo e poi quello da cambiare
  53.                         )
  54.                 )                                                                                                                                                                              
  55.           (command "_-insert" lin_pw "-50,0" "" "" "" "" "" "" "" "" "")                                                ;; inserisco blocco DEFINITO  NEL SETQ
  56.           (entdel (entlast))
  57.                 (if (setq ss (ssget "_X" '((2 . "lin_acquapotabile"))))                                                         ;blocco da sostituire
  58.                         (repeat (setq n (sslength ss))
  59.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  60.                           (entmod (subst '(2 . "lin_potable-water") '(2 . "lin_acquapotabile") edata))  ;;Prima blocco nuovo e poi quello da cambiare
  61.                         )
  62.                 )
  63.           (command "_-insert" lin_hw "-50,0" "" "" "" "" "" "" "" "" "")                                                ;; inserisco blocco DEFINITO  NEL SETQ
  64.           (entdel (entlast))
  65.                 (if (setq ss (ssget "_X" '((2 . "lin_acquacalda"))))                                                            ;blocco da sostituire
  66.                         (repeat (setq n (sslength ss))
  67.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  68.                           (entmod (subst '(2 . "lin_hot-water") '(2 . "lin_acquacalda") edata))         ;;Prima blocco nuovo e poi quello da cambiare
  69.                         )
  70.                 )
  71.           (command "_-insert" lin_dw "-50,0" "" "" "" "" "" "" "" "" "")                                                ;; inserisco blocco DEFINITO  NEL SETQ
  72.           (entdel (entlast))
  73.                 (if (setq ss (ssget "_X" '((2 . "lin_acquademi"))))                                                             ;blocco da sostituire
  74.                         (repeat (setq n (sslength ss))
  75.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  76.                           (entmod (subst '(2 . "lin_demi-water") '(2 . "lin_acquademi") edata))         ;;Prima blocco nuovo e poi quello da cambiare
  77.                         )
  78.                 )
  79.           (command "_-insert" lin_chw "-50,0" "" "" "" "" "" "" "" "" "")                                               ;; inserisco blocco DEFINITO  NEL SETQ
  80.           (entdel (entlast))
  81.                 (if (setq ss (ssget "_X" '((2 . "lin_acquagelida"))))                                                           ;blocco da sostituire
  82.                         (repeat (setq n (sslength ss))
  83.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  84.                           (entmod (subst '(2 . "lin_chilled-water") '(2 . "lin_acquagelida") edata))    ;;Prima blocco nuovo e poi quello da cambiare
  85.                         )
  86.                 )
  87.            (command "_-insert" lin_fw "-50,0" "" "" "" "" "" "" "" "" "")                                               ;; inserisco blocco DEFINITO  NEL SETQ
  88.            (entdel (entlast))
  89.                 (if (setq ss (ssget "_X" '((2 . "lin_acquantincendio"))))                                                               ;blocco da sostituire
  90.                         (repeat (setq n (sslength ss))
  91.                           (setq edata (entget (ssname ss (setq n (1- n)))))
  92.                           (entmod (subst '(2 . "lin_fire-water") '(2 . "lin_acquantincendio") edata))   ;;Prima blocco nuovo e poi quello da cambiare
  93.                         )
  94.                 )

is possible to write a list of original block and a list of new block an cycle the substitution?

 

thanks

where is Lee Mac ? Usually he is around to give some inputs.

A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

Brief history of Microsoft
https://www.youtube.com/watch?v=BLaMbaVT22E

CatDance

  • Newt
  • Posts: 57
Re: substitute block with another block, is this the best way to do it?
« Reply #6 on: December 09, 2021, 12:30:34 AM »
Here's an untested example:
Code - Auto/Visual Lisp: [Select]
  1. (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  2. (foreach file
  3.          '(("lin_cooling-water.dwg" "lin_acquatorre") ("lin_potable-water.dwg" "lin_acquapotabile"))
  4.   (if (findfile (strcat path (car file)))
  5.     (progn (command "_-insert" (strcat path (car file)) '(0 0 0) nil)
  6.            (if (setq ss (ssget "_X" (list (cons 2 (car file)))))
  7.              ;;blocco da sostituire
  8.              (repeat (setq n (sslength ss))
  9.                (setq edata (entget (ssname ss (setq n (1- n)))))
  10.                (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  11.                ;;Prima blocco nuovo e poi quello da cambiare
  12.              )
  13.            )
  14.     )
  15.   )
  16. )

wow this one looks a lot shorter then the op.
A must-read book: Amazing story how Bill + Paul started Microsoft and history of computer revolution.
https://www.amazon.com/Hard-Drive-Making-Microsoft-Empire/dp/0887306292

Brief history of Microsoft
https://www.youtube.com/watch?v=BLaMbaVT22E

gp.triple

  • Mosquito
  • Posts: 14
Re: substitute block with another block, is this the best way to do it?
« Reply #7 on: December 21, 2021, 11:40:54 AM »
Here's an untested example:
Code - Auto/Visual Lisp: [Select]
  1. (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  2. (foreach file
  3.          '(("lin_cooling-water.dwg" "lin_acquatorre") ("lin_potable-water.dwg" "lin_acquapotabile"))
  4.   (if (findfile (strcat path (car file)))
  5.     (progn (command "_-insert" (strcat path (car file)) '(0 0 0) nil)
  6.            (if (setq ss (ssget "_X" (list (cons 2 (car file)))))
  7.              ;;blocco da sostituire
  8.              (repeat (setq n (sslength ss))
  9.                (setq edata (entget (ssname ss (setq n (1- n)))))
  10.                (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  11.                ;;Prima blocco nuovo e poi quello da cambiare
  12.              )
  13.            )
  14.     )
  15.   )
  16. )
Hi i finally find some time to play with your code,i wrote all the correct block name
but i obtain only a "nil" if i run the command.
could you help me?

Code - Auto/Visual Lisp: [Select]
  1.    (defun c:test()
  2.  
  3.  
  4. (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  5. (foreach file
  6.   '(("col_acquatorre" "col_cooling-water" ));   ("col_potable-water" "col_acquapotabile")       ("col_hot-water" "col_acquacalda")      ("col_demi-water" "col_acquademi")      ("col_chilled-water" "col_acquagelida") ("col_fire-water" "col_acquantincendio")        ("col_well-water" "col_acquapozzo")     ("col_foam-fluid" "col_acquaschiuma")   ("col_athmosferic-air" "col_ariaatmosferica")   ("col_athmosferic-air-in-suction" "col_ariadepressione")        ("col_plant-air" "col_ariacomprserv")   ("col_instrument-air" "col_ariacomprstrum")     ("col_hot-oil" "col_fluidodiat")        ("col_carbon-dioxide" "col_anidrcarb")  ("col_inert-gas" "col_gasinerte")       ("col_nitrogen" "col_azoto")    ("col_ammonia" "col_ammoniaca") ("col_brine" "col_salamoia")    ("col_process-fluid" "col_processo")    ("col_chemical-sewer" "col_fognachim")  ("col_meteoric-sewer" "col_fognameteo") ("col_sanitary-sewer" "col_fognanera")  ("col_process-sewer" "col_fognaproc")   ("col_athmospheric-vent" "col_sfiatiatmosf")    ("col_safety-valve-vent" "col_valvsicur")       ("col_tempered-fluid" "col_sfiaticombust")      ("col_vacuum-vent" "col_vuoto") ("col_vent-to-blow-down" "col_sfiatibldw")      ("col_high-pressure-steam" "col_vaporeap")      ("col_low-pressure-steam" "col_vaporebp")       ("col_steam-condensate" "col_condensa"))
  7.   (if (findfile (strcat path (car file)))
  8.     (progn (command "_-insert" (strcat path (car file)) '(0 0 0) nil)
  9.            (if (setq ss (ssget "_X" (list (cons 2 (car file)))))
  10.            
  11.              (repeat (setq n (sslength ss))
  12.                (setq edata (entget (ssname ss (setq n (1- n)))))
  13.                (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  14.              
  15.              )
  16.            )
  17.     )
  18.   )
  19. )
  20. )

ronjonp

  • Needs a day job
  • Posts: 7527
Re: substitute block with another block, is this the best way to do it?
« Reply #8 on: December 21, 2021, 03:44:11 PM »
Try this ( untested again ):
Code - Auto/Visual Lisp: [Select]
  1. ;; Change this
  2. (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  3. ;; To this
  4. (entmod (subst (cons 2 (VL-FILENAME-BASE (car file))) (cons 2 (cadr file)) edata))
  5. ;; Or use @MHUPP suggestion and redefine the block on insert
  6. (setvar 'expert 5)
  7. (command "_-insert" (strcat (cadr file) "=" path (car file)) '(0 0 0) nil)

Just noticed you changed the input list .. the first example had the filename extension but now it's removed.
Findfile will not work so nothing happens.
("col_acquatorre" "col_cooling-water")

Quick example with the redefine route ( be aware that the expert variable is set to 5 )
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ path)
  2.   (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  3.   (setvar 'expert 5)
  4.   (foreach file '(("col_acquatorre" "col_cooling-water"))
  5.     (if (findfile (strcat path (car file) ".dwg"))
  6.       (progn (command "_-insert" (strcat (cadr file) "=" path (car file) ".dwg") '(0 0 0) nil)
  7. ;;;          (if (setq ss (ssget "_X" (list (cons 2 (car file)))))
  8. ;;;            (repeat (setq n (sslength ss))
  9. ;;;              (setq edata (entget (ssname ss (setq n (1- n)))))
  10. ;;;              (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  11. ;;;            )
  12. ;;;          )
  13.       )
  14.     )
  15.   )
  16. )
« Last Edit: December 21, 2021, 03:55:22 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

gp.triple

  • Mosquito
  • Posts: 14
Re: substitute block with another block, is this the best way to do it?
« Reply #9 on: December 22, 2021, 04:03:56 AM »
Try this ( untested again ):
Code - Auto/Visual Lisp: [Select]
  1. ;; Change this
  2. (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  3. ;; To this
  4. (entmod (subst (cons 2 (VL-FILENAME-BASE (car file))) (cons 2 (cadr file)) edata))
  5. ;; Or use @MHUPP suggestion and redefine the block on insert
  6. (setvar 'expert 5)
  7. (command "_-insert" (strcat (cadr file) "=" path (car file)) '(0 0 0) nil)

Just noticed you changed the input list .. the first example had the filename extension but now it's removed.
Findfile will not work so nothing happens.
("col_acquatorre" "col_cooling-water")

Quick example with the redefine route ( be aware that the expert variable is set to 5 )
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ path)
  2.   (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  3.   (setvar 'expert 5)
  4.   (foreach file '(("col_acquatorre" "col_cooling-water"))
  5.     (if (findfile (strcat path (car file) ".dwg"))
  6.       (progn (command "_-insert" (strcat (cadr file) "=" path (car file) ".dwg") '(0 0 0) nil)
  7. ;;;          (if (setq ss (ssget "_X" (list (cons 2 (car file)))))
  8. ;;;            (repeat (setq n (sslength ss))
  9. ;;;              (setq edata (entget (ssname ss (setq n (1- n)))))
  10. ;;;              (entmod (subst (cons 2 (car file)) (cons 2 (cadr file)) edata))
  11. ;;;            )
  12. ;;;          )
  13.       )
  14.     )
  15.   )
  16. )

sorry the missing extension is my mistake when combine the block name.
in any case adding the extension the autocad output is this

Code: [Select]
Command: TEST
_-insert Enter block name or [?]: C:\dati\Lamberti_std\block_update\blocks\col_cooling-water.dwg
Units: Millimeters   Conversion:    1.0000
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]:
Enter X scale factor, specify opposite corner, or [Corner/XYZ] <1>:
Command: nil

the last code work, i changed the block name order and it change my block
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ path)
  2.   (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  3.   (setvar 'expert 5)
  4.   (foreach file '(("col_cooling-water" "col_acquatorre" )) ;inverted block name order
  5.     (if (findfile (strcat path (car file) ".dwg"))
  6.       (progn (command "_-insert" (strcat (cadr file) "=" path (car file) ".dwg") '(0 0 0) nil)
  7.       )
  8.     )
  9.   )
  10. )

is it correct to have a "nil" as last command during the run?

Code: [Select]
Command: TEST
_-insert Enter block name or [?]: col_acquatorre=C:\dati\Lamberti_std\block_update\blocks\col_cooling-water.dwg Duplicate definition of block _Oblique  ignored.
Units: Millimeters   Conversion:    1.0000
Specify insertion point or [Basepoint/Scale/X/Y/Z/Rotate/Explode/REpeat]:
Enter X scale factor, specify opposite corner, or [Corner/XYZ] <1>:
Command: nil



gp.triple

  • Mosquito
  • Posts: 14
Re: substitute block with another block, is this the best way to do it?
« Reply #10 on: December 22, 2021, 06:25:37 AM »
update this code change my blocks but keep the original block name.
i try to explain better
old block name:    "col_acquatorre"
new block name:  "col_cooling-water"

after running the code i have a block named "col_acquatorre" but it contains "col_cooling-water"

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: substitute block with another block, is this the best way to do it?
« Reply #11 on: December 22, 2021, 06:20:28 PM »
I’m not where I can write code for an example but this is something that I do regularly.

Step 1: I have a simple drawing with all replacement blocks defined in it
Step 2: I insert said drawing
Step 3: Entmod assoc 2 with the new block name on each insert.
Step 4: Purge said drawing
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

gp.triple

  • Mosquito
  • Posts: 14
Re: substitute block with another block, is this the best way to do it?
« Reply #12 on: December 23, 2021, 02:29:08 AM »
I’m not where I can write code for an example but this is something that I do regularly.

Step 1: I have a simple drawing with all replacement blocks defined in it
Step 2: I insert said drawing
Step 3: Entmod assoc 2 with the new block name on each insert.
Step 4: Purge said drawing

in that way you update the block definition don't replace a block with a different block, isn't it?

gp.triple

  • Mosquito
  • Posts: 14
Re: substitute block with another block, is this the best way to do it?
« Reply #13 on: December 27, 2021, 11:05:10 AM »
@ronjonp
at the end I find an ugly solution but better than starting code
i first redefine the block and than rename them with the correct name

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:test (/ path)
  3.   (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  4.   (setvar 'expert 5)
  5.   (foreach file '(("COL_COOLING-WATER""COL_ACQUATORRE") ("col_potable-water""col_acquapotabile") ("col_hot-water""col_acquacalda") ("col_demi-water""col_acquademi") ("col_chilled-water""col_acquagelida") ("col_fire-water""col_acquantincendio") ("col_well-water""col_acquapozzo") ("col_foam-fluid""col_acquaschiuma") ("col_athmosferic-air""col_ariaatmosferica") ("col_athmosferic-air-in-suction""col_ariadepressione") ("col_plant-air""col_ariacomprserv") ("col_instrument-air""col_ariacomprstrum") ("col_hot-oil""col_fluidodiat") ("col_carbon-dioxide""col_anidrcarb") ("col_inert-gas""col_gasinerte") ("col_nitrogen""col_azoto") ("col_ammonia""col_ammoniaca") ("col_brine""col_salamoia") ("col_process-fluid""col_processo") ("col_chemical-sewer""col_fognachim") ("col_meteoric-sewer""col_fognameteo") ("col_sanitary-sewer""col_fognanera") ("col_process-sewer""col_fognaproc") ("col_athmospheric-vent""col_sfiatiatmosf") ("col_safety-valve-vent""col_valvsicur") ("col_tempered-fluid""col_sfiaticombust") ("col_vacuum-vent""col_vuoto") ("col_vent-to-blow-down""col_sfiatibldw") ("col_high-pressure-steam""col_vaporeap") ("col_low-pressure-steam""col_vaporebp") ("col_steam-condensate""col_condensa"))
  6.     (if (findfile (strcat path (car file) ".dwg"))
  7.       (progn (command "_-insert" (strcat (cadr file) "=" path (car file) ".dwg") '(0 0 0) nil)
  8.       (command "_-rename" "b"  (cadr file) (car file))
  9.       )
  10.     )
  11.   )
  12.  
  13.   (setvar 'expert 0)
  14. )  
  15.  
  16.  

ronjonp

  • Needs a day job
  • Posts: 7527
Re: substitute block with another block, is this the best way to do it?
« Reply #14 on: December 27, 2021, 11:12:35 AM »
@ronjonp
at the end I find an ugly solution but better than starting code
i first redefine the block and than rename them with the correct name

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:test (/ path)
  3.   (setq path "C:\\dati\\Lamberti_std\\block_update\\blocks\\")
  4.   (setvar 'expert 5)
  5.   (foreach file '(("COL_COOLING-WATER""COL_ACQUATORRE") ("col_potable-water""col_acquapotabile") ("col_hot-water""col_acquacalda") ("col_demi-water""col_acquademi") ("col_chilled-water""col_acquagelida") ("col_fire-water""col_acquantincendio") ("col_well-water""col_acquapozzo") ("col_foam-fluid""col_acquaschiuma") ("col_athmosferic-air""col_ariaatmosferica") ("col_athmosferic-air-in-suction""col_ariadepressione") ("col_plant-air""col_ariacomprserv") ("col_instrument-air""col_ariacomprstrum") ("col_hot-oil""col_fluidodiat") ("col_carbon-dioxide""col_anidrcarb") ("col_inert-gas""col_gasinerte") ("col_nitrogen""col_azoto") ("col_ammonia""col_ammoniaca") ("col_brine""col_salamoia") ("col_process-fluid""col_processo") ("col_chemical-sewer""col_fognachim") ("col_meteoric-sewer""col_fognameteo") ("col_sanitary-sewer""col_fognanera") ("col_process-sewer""col_fognaproc") ("col_athmospheric-vent""col_sfiatiatmosf") ("col_safety-valve-vent""col_valvsicur") ("col_tempered-fluid""col_sfiaticombust") ("col_vacuum-vent""col_vuoto") ("col_vent-to-blow-down""col_sfiatibldw") ("col_high-pressure-steam""col_vaporeap") ("col_low-pressure-steam""col_vaporebp") ("col_steam-condensate""col_condensa"))
  6.     (if (findfile (strcat path (car file) ".dwg"))
  7.       (progn (command "_-insert" (strcat (cadr file) "=" path (car file) ".dwg") '(0 0 0) nil)
  8.       (command "_-rename" "b"  (cadr file) (car file))
  9.       )
  10.     )
  11.   )
  12.  
  13.   (setvar 'expert 0)
  14. )  
  15.  
  16.  
Glad you got it sorted :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC