Author Topic: Global Width Factor change  (Read 5391 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
Global Width Factor change
« on: June 13, 2018, 11:05:37 AM »
I don't know who wrote this originally and I've had it for a couple of years and never used it. Dusted it off and made some minor changes
to it. With luck it works, cause I have no clue what's really happening here.

I'm looking for some guidance on my comments to be sure that I understand what's happening with each line of code. Those lines that are blank
are blank because I don't know.

Code - Auto/Visual Lisp: [Select]
  1. ;;Author unknown
  2. ;;Changes Text Width Factor for all text in drawing from 1.0 to 0.85
  3. ;;Type txtwd at the command line
  4. ;;Jerry Logan - Modified to include comments 6-12-2018
  5. ;;
  6. ;;
  7.  
  8. (defun c:txtwd ()
  9. (setq twd (ssget "x" '((0 . "TEXT,MTEXT")(41 . 1.0))));;build selection set for TEXT & MTEXT with WIDTH factor of 1.0
  10. (setq index_1 0);;
  11.  (while
  12.   (< index_1 (sslength twd));;get number of objects from twd
  13.  (setq txt_list (entget (ssname twd index_1)));;return the entity name of twd and set it to txt_lst
  14.  (setq txt_list (subst (cons 41 0.85) (assoc 41 txt_list) txt_list ));;change txt_list entities from 1.0 text width to .85
  15.  (entmod txt_list);;modify the entity
  16.  (setq index_1 (+ index_1 1);;
  17. );;
  18. );;end while
  19.  (command "redraw");;redraw drawing
  20. );;end defun

J. Logan
ACAD 2015
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Global Width Factor change
« Reply #1 on: June 13, 2018, 01:13:37 PM »
I've added some comments to your code, without modifying the code itself:
Code - Auto/Visual Lisp: [Select]
  1. ;;Author unknown
  2. ;;Changes Text Width Factor for all text in drawing from 1.0 to 0.85
  3. ;;Type txtwd at the command line
  4. ;;Jerry Logan - Modified to include comments 6-12-2018
  5. ;;
  6. ;;
  7.  
  8. (defun c:txtwd ()
  9.     ;; define function; local symbols should be declared here
  10.    
  11.     (setq twd (ssget "x" '((0 . "TEXT,MTEXT")(41 . 1.0))))
  12.     ;; Obtain selection set of all Text entities with width factor 1.0 and all MText entities with text box width equal to 1.0
  13.     ;; Note that DXF group 41 is the width of the text box for MText ** not the width factor **
  14.  
  15.     (setq index_1 0)
  16.     ;; Initialise variable index_1 to 0
  17.  
  18.     (while (< index_1 (sslength twd))
  19.         ;; Whilst the value of the variable index_1 is less than the number of objects in the set
  20.  
  21.         (setq txt_list (entget (ssname twd index_1)))
  22.         ;; Obtain the DXF data of the entity at index 'index_1' (initially 0) within the selection set
  23.  
  24.         (setq txt_list (subst (cons 41 0.85) (assoc 41 txt_list) txt_list))
  25.         ;; Substitute the dotted pair (41 . 0.85) for the existing dotted pair (41 . 1.0) with the DXF data list
  26.  
  27.         (entmod txt_list)
  28.         ;; Update the DXF data in the drawing database
  29.  
  30.         (setq index_1 (+ index_1 1))
  31.         ;; Increment the value of 'index_1' to look at the next entity in the selection set
  32.        
  33.     ) ;; end WHILE
  34.    
  35.     (command "redraw")
  36.     ;; This is not necessary
  37.  
  38.     ;; A (princ) or (prin1) would typically be evaluated here so that the function returns a null symbol
  39.     ;; to the command-line, rather than the value returned by the last evaluated expression
  40.    
  41. ) ;; end DEFUN

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Global Width Factor change
« Reply #2 on: June 13, 2018, 01:16:01 PM »
FWIW, here is how I might write the code:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:txtwd ( / idx sel )
  2.     (if (setq sel (ssget "_X" '((0 . "TEXT") (41 . 1.0))))
  3.         (repeat (setq idx (sslength sel))
  4.             (entmod (subst '(41 . 0.85) '(41 . 1.0) (entget (ssname sel (setq idx (1- idx))))))
  5.         )
  6.     )
  7.     (princ)
  8. )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Global Width Factor change
« Reply #3 on: June 13, 2018, 01:46:07 PM »
And here is a commented version of the above:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:txtwd ( / idx sel )
  2.     ;; Define function, declare local symbols 'idx' and 'sel'
  3.    
  4.     (if ;; If the following expression returns a non-nil value
  5.  
  6.         (setq sel (ssget "_X" '((0 . "TEXT") (41 . 1.0))))
  7.         ;; Automatically obtain a selection set of all single-line text with width-factor equal to 1.0
  8.         ;; in all layouts of the drawing and assign the selection set to variable 'sel'
  9.        
  10.         (repeat (setq idx (sslength sel))
  11.             ;; Evaluate the following expressions a number of times equal to the number of objects
  12.             ;; in the selection set. At the same time, initialise a local variable 'idx' to the
  13.             ;; number of objects in the set.
  14.            
  15.             (entmod ;; Modify the following DXF data in the drawing database
  16.                
  17.                 (subst ;; Perform the following substitution in the DXF data list
  18.  
  19.                    '(41 . 0.85) ;; New Item
  20.  
  21.                    '(41 . 1.0)  ;; Item to be substituted
  22.  
  23.                     (entget ;; Obtain the DXF data list for the following entity
  24.  
  25.                         (ssname sel ;; Obtain the entity at the following index in the selection set 'sel'
  26.  
  27.                             (setq idx (1- idx)) ;; Decrement the index variable 'idx' (since indexes are zero-based)
  28.  
  29.                         ) ;; end SSNAME
  30.  
  31.                     ) ;; end ENTGET
  32.  
  33.                 ) ;; end SUBST
  34.                
  35.             ) ;; end ENTMOD
  36.            
  37.         ) ;; end REPEAT
  38.  
  39.         ;; The 'ELSE' expression would go here
  40.        
  41.     ) ;; end IF
  42.  
  43.     (princ)
  44.     ;; Return a null symbol to the command-line hence suppressing the
  45.     ;; value returned by the last evaluated expression (i.e. the IF expression)
  46.    
  47. ) ;; end DEFUN

jlogan02

  • Bull Frog
  • Posts: 327
Re: Global Width Factor change
« Reply #4 on: June 13, 2018, 04:58:59 PM »
Thanks Lee so much for the explanations and the different perspective.

J. Logan
ACAD 2015
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

Coder

  • Swamp Rat
  • Posts: 827
Re: Global Width Factor change
« Reply #5 on: June 13, 2018, 05:41:31 PM »
Very generous and helpful Lee as always.


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Global Width Factor change
« Reply #6 on: June 13, 2018, 06:45:54 PM »
You're most welcome guys.  :-)

w64bit

  • Newt
  • Posts: 78
Re: Global Width Factor change
« Reply #7 on: February 10, 2021, 05:12:04 PM »
If I add a modified code from Reply #2 to a SCR file (to avoid loading the LSP):
Code: [Select]
(if (setq sel (ssget "_X" '((0 . "TEXT") (41 . 1.0))))
        (repeat (setq idx (sslength sel))
            (entmod (subst '(41 . 0.85) '(41 . 1.0) (entget (ssname sel (setq idx (1- idx))))))
        )
    )
(princ)

how I can avoid listing this output in the command line?
Code: [Select]
((-1 . <Entity name: 14d7d07c190>) (0 . "TEXT") (330 . <Entity name: 14d7d083020>) (5 . "91") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbText") (10 118.65 101.299 0.0) (40 . 2.5) (1 . "TEXT") (50 . 0.0) (41 . 0.85) (51 . 0.0) (7 . "Standard") (71 . 0) (72 . 0) (11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . "AcDbText") (73 . 0))

BIGAL

  • Swamp Rat
  • Posts: 1414
  • 40 + years of using Autocad
Re: Global Width Factor change
« Reply #8 on: February 10, 2021, 06:51:53 PM »
Pretty easy (setvar 'cmdecho 0)
A man who never made a mistake never made anything

w64bit

  • Newt
  • Posts: 78
Re: Global Width Factor change
« Reply #9 on: February 12, 2021, 03:48:09 AM »
Tried putting it in several places with no effect.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Global Width Factor change
« Reply #10 on: February 12, 2021, 04:35:12 AM »
Tried putting it in several places with no effect.
Code: [Select]
(if (setq sel (ssget "_X" '((0 . "TEXT") (41 . 1.0))))
  (progn
    (repeat (setq idx (sslength sel))
       (entmod (subst '(41 . 0.85) '(41 . 1.0) (entget (ssname sel (setq idx (1- idx))))))
    )
    (princ)
  )
)

Crank

  • Water Moccasin
  • Posts: 1503
Re: Global Width Factor change
« Reply #11 on: February 13, 2021, 07:06:25 AM »
If I add a modified code from Reply #2 to a SCR file (to avoid loading the LSP):
Code: [Select]
(if (setq sel (ssget "_X" '((0 . "TEXT") (41 . 1.0))))
        (repeat (setq idx (sslength sel))
            (entmod (subst '(41 . 0.85) '(41 . 1.0) (entget (ssname sel (setq idx (1- idx))))))
        )
    )
(princ)

how I can avoid listing this output in the command line?
Code: [Select]
((-1 . <Entity name: 14d7d07c190>) (0 . "TEXT") (330 . <Entity name: 14d7d083020>) (5 . "91") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbText") (10 118.65 101.299 0.0) (40 . 2.5) (1 . "TEXT") (50 . 0.0) (41 . 0.85) (51 . 0.0) (7 . "Standard") (71 . 0) (72 . 0) (11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . "AcDbText") (73 . 0))
It's better to place the complete code in the script and place the command TXTWD on the next line.
Vault Professional 2023     +     AEC Collection