3dwannab Posted November 17 Posted November 17 (edited) Hi all, Here's the error I'm getting: It's got to do with line 47 of the code below: It's like commands are blocked when the rename button is clicked. The renaming of the layers is okay and works. To reproduce this bug, do the following: If you have a block in a drawing, select it and the dialog will pop up. Select an item in the block list, enter a new name and that error above should appear. Main code: (vl-load-com) ; ;; ; ;; Renaming of Layers and Blocks ; ;; Wrote on 2022.12.16 by 3dwannab ; ;; (defun c:RLB (/ *error* _getlayersandblocks _renameLayer _renameBlock laylst blklst objs dcl_id old new) (defun *error* (errmsg) (and acDoc (vla-EndUndoMark acDoc)) (and errmsg (not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*")) (princ (strcat "\n<< Error: " errmsg " >>\n")) ) (setvar 'cmdecho var_cmdecho) ) ;; Start the undo mark here (setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object))) (or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc)) ;; Get any system variables here (setq var_cmdecho (getvar "cmdecho")) (setvar 'cmdecho 0) ;; Function to rename a layer (defun _renameLayer (old new / layer) (setq layer (tblobjname "LAYER" old)) (if layer (progn (entmod (subst (cons 2 new) (assoc 2 (entget layer)) (entget layer))) (princ (strcat "Renamed layer: " old " to " new "\n")) ) (princ (strcat "Layer " old " not found.\n")) ) ) ;; Function to rename a block (defun _renameBlock (old new / block) (setq block (tblobjname "BLOCK" old)) (if block (progn ;; This command is the problem (command "_.rename" "_B" old new) ; (entmod (subst (cons 2 new) (assoc 2 (entget block)) (entget block))) ; (princ (strcat "Renamed Block in function: " old " to " new "\n")) ) (princ (strcat "Layer " old " not found.\n")) ) ) ;; Function to populate layer and block lists for the dialog (defun _getlayersandblocks (objs / blkname lay) (setq laylst nil) ;; Clear the previous list (setq blklst nil) ;; Clear the previous list ;; Iterate through the selection set objects (vlax-for objs (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object) ) ) ;; Get the layer name of the object (setq lay (vla-get-layer objs)) ;; Add the layer to the list if not already present, avoid adding layers 0 and Defpoints (if (and (not (member lay laylst)) (not (or (= lay "0") (= lay "Defpoints")))) (setq laylst (cons lay laylst)) ) ;; Check if the object is a block reference (if (eq (vla-get-objectname objs) "AcDbBlockReference") (progn ;; Handle dynamic block names properly (setq blkname (vla-get-effectivename objs)) ;; Use the regular name if no effective name exists (if (not blkname) (setq blkname (vla-get-name objs))) ;; Add the block name to the list if not already present (if (and blkname (not (member blkname blklst))) (setq blklst (cons blkname blklst)) ) ) ) ) ;; Sort the lists alphabetically (setq laylst (acad_strlsort laylst)) (setq blklst (acad_strlsort blklst)) ) ;; Initialize lists to avoid any issues during multiple runs (setq laylst nil blklst nil ) ;; Check if there is an active selection set (if (ssget) (progn ;; Iterate through the selection set objects (vlax-for objs (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object) ) ) (_getlayersandblocks objs) ) ;; Load and display the DCL dialog (setq dcl_id (load_dialog "3dwannab_Rename_Layers_and_Blocks.dcl")) (if (and dcl_id (new_dialog "rename_layer_block" dcl_id)) (progn (start_list "layer_list") (foreach lay laylst (add_list lay) ) (end_list) (start_list "block_list") (foreach blk blklst (add_list blk) ) (end_list) ;; Update the layer name in the text box when a layer is selected from the list (action_tile "layer_list" "(set_tile \"new_layer_name\" (nth (atoi (get_tile \"layer_list\")) laylst))" ) ;; Update the block name in the text box when a block is selected from the list (action_tile "block_list" "(set_tile \"new_block_name\" (nth (atoi (get_tile \"block_list\")) blklst))" ) ;; Handle user input for renaming layers (action_tile "rename_layer" "(setq index (atoi (get_tile \"layer_list\"))) ;; Get the index of the selected layer (setq old (nth index laylst)) ;; Get the old layer name using the index (setq new (get_tile \"new_layer_name\")) ;; Get the new layer name from the text box (if old (progn (_renameLayer old new) ;; Call the _renameLayer function (set_tile \"new_layer_name\" \"\") ;; Clear the input box for the new layer name ;; Rebuild the layer list to reflect changes (_getlayersandblocks objs) ;; Refresh the laylst (start_list \"layer_list\") (mapcar '(lambda (lay) (add_list lay)) laylst) ;; Add each layer to the list (end_list) ;; Update the selected layer name in the text box (set_tile \"new_layer_name\" new) ) (alert \"No layer selected.\") ;; Handle case where no valid layer is selected ) " ) ;; Handle user input for renaming blocks (action_tile "rename_block" "(setq index (atoi (get_tile \"block_list\"))) ;; Get the index of the selected block (setq old (nth index blklst)) ;; Get the old block name using the index (setq new (get_tile \"new_block_name\")) ;; Get the new block name from the text box (if old (progn (_renameBlock old new) ;; Call the _renameBlock function (set_tile \"new_block_name\" \"\") ;; Clear the input box for the new block name ;; Rebuild the block list to reflect changes (_getlayersandblocks objs) ;; Refresh the blklst (start_list \"block_list\") (mapcar '(lambda (blk) (add_list blk)) blklst) ;; Add each block to the list (end_list) ;; Update the selected block name in the text box (set_tile \"new_block_name\" new) ) (alert \"No block selected.\") ;; Handle case where no valid block is selected ) " ) ;; Handle the close button and Escape key (action_tile "close" "(done_dialog)") (action_tile "cancel" "(done_dialog)") (action_tile "escape" "(done_dialog)") (start_dialog) (unload_dialog dcl_id) ) (alert "Failed to load DCL file.") ) ) (alert "No objects selected.") ;; Inform the user if nothing is selected ) (vla-EndUndoMark acDoc) (*error* nil) (princ) ) ;; (c:RLB) ;; Unblock for testing Here's the DCL : // ;; DCL Code for 3dwannab_Rename_Layers_and_Blocks.dcl // ;; Save this as 3dwannab_Rename_Layers_and_Blocks.dcl rename_layer_block : dialog { label = "Rename Layers and Blocks"; : row { : column { label = "Layers"; : list_box { key = "layer_list"; width = 75; height = 25; } : edit_box { key = "new_layer_name"; label = "New Layer Name:"; } : button { key = "rename_layer"; label = "Rename Layer"; } } : column { label = "Blocks"; : list_box { key = "block_list"; width = 75; height = 25; } : edit_box { key = "new_block_name"; label = "New Block Name:"; } : button { key = "rename_block"; label = "Rename Block"; } } } : row { : button { key = "close"; is_cancel = true; label = "Close"; } } } Here's the UI: Edited November 17 by 3dwannab Quote
3dwannab Posted November 17 Author Posted November 17 (edited) I fixed it with the following: I've been at this for 4 hours, yet once I posted here, I found the solution in 10 minutes which is always the way!! I'll post the full code once I'm happy with it. EDIT: Posted the program over here. ;; ;; Function to rename a block withing a DCL ;; Call the function within the DCL: ;; (_renameBlock Oldname NewName) ;; (defun _renameBlock (old new / actDoc) (if (tblobjname "BLOCK" new) (princ (strcat "\n\"" new "\" block aleady exists.")) ) (if (and (tblobjname "BLOCK" old) (not (tblobjname "BLOCK" new))) (progn (setq actDoc (vla-get-ActiveDocument (vlax-get-acad-object))) (vla-SendCommand actDoc (strcat "-rename _B " old "\r" new "\r")) ) ) ) Edited November 18 by 3dwannab posted link to program 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.