Author Topic: Need fresh eyes for this lisp  (Read 1413 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Need fresh eyes for this lisp
« on: November 27, 2016, 07:17:35 AM »
Hi all
I want to rename this block then move to new layer
New Name is "BOTANICAL NAME"
and adding the code as suffix for a new layer
The lisp runs OK for one time then gives an error
Code - Auto/Visual Lisp: [Select]
  1. Select New name for Block:
  2. Select Code: ; error: Automation Error. Key not found

The Code
Code - Auto/Visual Lisp: [Select]
  1. (defun c:RNB (/ a b blk blknm c code d e newblknm newly oldblknm vl-blk )
  2.  
  3.   (while
  4.     (if (setq blk (car (entsel  "\nSelect Block: ")))
  5.       (progn
  6.         (setq VL-Blk (vlax-ename->vla-object blk))
  7.         (setq OldBLkNM (vla-get-Name VL-Blk))
  8.         (SETQ NewBlkNm (VLA-GET-TEXTSTRING (_value "\nSelect New name for Block: ")))  
  9.         (setq NewLy (strcat "L-PLNT-TREE-" (VLA-GET-TEXTSTRING (_value "\nSelect Code: "))))
  10.         (mapcar '(lambda ( a b c d e ) (MakeLayer a b "Continuous" c d 0 e ))
  11.                 (list NewLy)
  12.                 '(82)
  13.                 '(0.13)
  14.                 '(nil)
  15.                 (list (strcat "TREES TYPE " NewBlkNm))
  16.                 )
  17.         (if (equal OldBLkNM NewBlkNm)
  18.           (vlax-put-property VL-Blk 'layer NewLy)
  19.           (progn
  20.             (vlax-put-property VL-Blk 'layer NewLy)
  21.             (vlax-put-property VL-Blk 'name NewBlkNm)
  22.             )
  23.           )
  24.         )
  25.       )
  26.     )
  27.   )
  28.  
  29.  
  30. (defun _value   (str / )
  31.   (while
  32.     (not
  33.       (if (and (setq entt (nentsel str))
  34.                (setq ATTr (car entt))
  35.                (member (vla-get-objectname (setq ATTr (vlax-ename->vla-object ATTr)))
  36.                        '("AcDbAttribute" "AcDbText" "*DIM*" "AcDbMText")))
  37.         ATTr (progn (princ str) nil))))
  38.   ATTr
  39.   )
  40.  
  41. (defun MakeLayer ( name colour linetype lineweight willplot bitflag description )    ;; © Lee Mac 2010
  42.   (regapp "AcAecLayerStandard")
  43.     ;; (MakeLayer name colour linetype lineweight willplot bitflag description )
  44.     ;; Specifications:
  45.     ;; Description        Data Type        Remarks
  46.     ;; -----------------------------------------------------------------
  47.     ;; Layer Name          STRING          Only standard chars allowed
  48.     ;; Layer Colour        INTEGER         may be nil, -ve for Layer Off, Colour < 256
  49.     ;; Layer Linetype      STRING          may be nil, If not loaded, CONTINUOUS.
  50.     ;; Layer Lineweight    REAL            may be nil, 0 <= x <= 2.11
  51.     ;; Plot?               BOOLEAN         T = Plot Layer, nil otherwise
  52.     ;; Bit Flag            INTEGER         0=None, 1=Frozen, 2=Frozen in VP, 4=Locked
  53.     ;; Description         STRING          may be nil for no description
  54.     ;; Function will return list detailing whether layer creation is successful.
  55.  
  56.   (or (tblsearch "LAYER" name)
  57.     (entmake
  58.       (append
  59.         (list
  60.           (cons 0       "LAYER")
  61.           (cons 100     "AcDbSymbolTableRecord")
  62.           (cons 100     "AcDbLayerTableRecord")
  63.           (cons 2       name)
  64.           (cons 70      bitflag)
  65.           (cons 290     (if willplot 1 0))
  66.           (cons 6       (if (and linetype (tblsearch "LTYPE" linetype)) linetype "CONTINUOUS"))
  67.           (cons 62      (if (and colour (< 0 (abs colour) 256)) colour 7))
  68.           (cons 370     (fix (* 100 (if (and lineweight (<= 0.0 lineweight 2.11)) lineweight 0.0))))
  69.           )
  70.         (if description (list (list -3 (list "AcAecLayerStandard" (cons 1000 "") (cons 1000 description)))))
  71.         ))))

Thanks

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Need fresh eyes for this lisp
« Reply #1 on: November 27, 2016, 07:55:43 AM »
I removed IF after while
it is working well put for several time then gives the same error
Code - Auto/Visual Lisp: [Select]
  1. Select Code: ; error: Automation Error. Key not found

Code - Auto/Visual Lisp: [Select]
  1. (defun c:RNB (/ a b blk blknm c code d e newblknm newly oldblknm vl-blk )
  2.  
  3.   (while
  4.     (setq blk (car (entsel  "\nSelect Block: ")))
  5.     (setq VL-Blk (vlax-ename->vla-object blk))
  6.     (setq OldBLkNM (vla-get-Name VL-Blk))
  7.     (SETQ NewBlkNm (VLA-GET-TEXTSTRING (_value "\nSelect New name for Block: ")))
  8.     (setq NewLy (strcat "L-PLNT-TREE-" (VLA-GET-TEXTSTRING (_value "\nSelect Code: "))))
  9.     (mapcar '(lambda ( a b c d e ) (MakeLayer a b "Continuous" c d 0 e ))
  10.             (list NewLy)
  11.             '(82)
  12.             '(0.13)
  13.             '(nil)
  14.             (list (strcat "TREES TYPE " NewBlkNm))
  15.             )
  16.     (if (equal OldBLkNM NewBlkNm)
  17.       (vlax-put-property VL-Blk 'layer NewLy)
  18.       (progn
  19.         (vlax-put-property VL-Blk 'layer NewLy)
  20.         (vlax-put-property VL-Blk 'name NewBlkNm)
  21.         )
  22.       )
  23.     )
  24.   )
  25.  
  26. (defun _value   (str / )
  27.   (while
  28.     (not
  29.       (if (and (setq entt (nentsel str))
  30.                (setq ATTr (car entt))
  31.                (member (vla-get-objectname (setq ATTr (vlax-ename->vla-object ATTr)))
  32.                        '("AcDbAttribute" "AcDbText" "*DIM*" "AcDbMText")))
  33.         ATTr (progn (princ str) nil))))
  34.   ATTr
  35.   )
  36.  
  37. (defun MakeLayer ( name colour linetype lineweight willplot bitflag description )    ;; © Lee Mac 2010
  38.   (regapp "AcAecLayerStandard")
  39.     ;; (MakeLayer name colour linetype lineweight willplot bitflag description )
  40.     ;; Specifications:
  41.     ;; Description        Data Type        Remarks
  42.     ;; -----------------------------------------------------------------
  43.     ;; Layer Name          STRING          Only standard chars allowed
  44.     ;; Layer Colour        INTEGER         may be nil, -ve for Layer Off, Colour < 256
  45.     ;; Layer Linetype      STRING          may be nil, If not loaded, CONTINUOUS.
  46.     ;; Layer Lineweight    REAL            may be nil, 0 <= x <= 2.11
  47.     ;; Plot?               BOOLEAN         T = Plot Layer, nil otherwise
  48.     ;; Bit Flag            INTEGER         0=None, 1=Frozen, 2=Frozen in VP, 4=Locked
  49.     ;; Description         STRING          may be nil for no description
  50.     ;; Function will return list detailing whether layer creation is successful.
  51.  
  52.   (or (tblsearch "LAYER" name)
  53.     (entmake
  54.       (append
  55.         (list
  56.           (cons 0       "LAYER")
  57.           (cons 100     "AcDbSymbolTableRecord")
  58.           (cons 100     "AcDbLayerTableRecord")
  59.           (cons 2       name)
  60.           (cons 70      bitflag)
  61.           (cons 290     (if willplot 1 0))
  62.           (cons 6       (if (and linetype (tblsearch "LTYPE" linetype)) linetype "CONTINUOUS"))
  63.           (cons 62      (if (and colour (< 0 (abs colour) 256)) colour 7))
  64.           (cons 370     (fix (* 100 (if (and lineweight (<= 0.0 lineweight 2.11)) lineweight 0.0))))
  65.           )
  66.         (if description (list (list -3 (list "AcAecLayerStandard" (cons 1000 "") (cons 1000 description)))))
  67.         ))))
« Last Edit: November 27, 2016, 08:27:15 AM by HasanCAD »

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Need fresh eyes for this lisp
« Reply #2 on: November 27, 2016, 08:12:16 AM »
The first thing I noticed:
Code: [Select]
(setq OldBLkNM (vla-get-Name VL-Blk))change to:
Code: [Select]
(setq OldBLkNM (vla-get-EffectiveName VL-Blk))
EDIT: Btw whats that _value function? I'm a bad reader, sorry.

Instead of:
Code: [Select]
(SETQ NewBlkNm (VLA-GET-TEXTSTRING (_value "\nSelect New name for Block: ")))
(setq NewLy (strcat "L-PLNT-TREE-" (VLA-GET-TEXTSTRING (_value "\nSelect Code: "))))
I would suggest:
Code: [Select]
(setq NewBlkNm (getV "\nSelect New name for Block: "))
(setq NewLy (strcat "L-PLNT-TREE" (getV "\nSelect Code: ")))

with:
Code: [Select]
(defun getV ( msg / e txt )
(setvar 'errno 0)
(while (/= 52 (getvar 'errno))
(setq e (car (nentsel msg)))
(cond
((= 7 (getvar 'errno)) (princ "\nMissed.") (setvar 'errno 0))
((/= (member (cdr (assoc 0 (entget e))) '("TEXT" "MTEXT" "ATTRIB")))
(princ "\nInvalid object.")
)
((and e (not (snvalid (setq txt (cdr (assoc 1 (entget e)))))))
(princ "\nInvalid content.") (setq txt nil)
)
(txt (setvar 'errno 52))
(T nil)
); cond
); while
(if txt txt "")
); defun
EDIT2:
Also change this part:
Code: [Select]
(if (equal OldBLkNM NewBlkNm)
(vlax-put-property VL-Blk 'layer NewLy)
(progn
(vlax-put-property VL-Blk 'layer NewLy)
(vlax-put-property VL-Blk 'name NewBlkNm)
)
)
To:
Code: [Select]
(if (equal OldBLkNM NewBlkNm)
(vlax-put-property VL-Blk 'layer NewLy)
(progn
(vlax-put-property VL-Blk 'layer NewLy)
(vlax-put-property
(vla-item
(vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object)))
(vla-get-EffectiveName VL-Blk)
)
'Name
NewBlkNm
)
)
)
« Last Edit: November 27, 2016, 08:35:52 AM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Need fresh eyes for this lisp
« Reply #3 on: November 28, 2016, 04:15:54 AM »
Thanks Grrr1337 for your quick reply
getV not working
But the lisp working perfect