Author Topic: scale image from center  (Read 1408 times)

0 Members and 1 Guest are viewing this topic.

PM

  • Guest
scale image from center
« on: June 14, 2021, 01:30:02 PM »
Hi i use this code to scale images in layout for specific scales. The problem is that when i do the scale the image moves. Is it possible to scale from the center ?

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:myimgscale (/)
  3.  
  4.  (princ "\n")(princ "\n")(princ "\n")
  5.            (initget "1 2 3")
  6.            (setq
  7.              l
  8.               (cond
  9.                 ((getkword
  10.                    "\nslelect image 1:1 :
  11.                        1. for scale 1:500.
  12.                        2. for scale 1:1000.
  13.                        3. for scale 1:5000.
  14. "
  15.                  )
  16.                 )
  17.                 ("1")
  18.               )
  19.            )
  20.  
  21. (if (eq l "1")
  22.          ((lambda (/ ent)
  23.            (while (not (setq ent (car (entsel)))))
  24.            (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 2)
  25.             );end wile
  26.          ); end lambda
  27.        ); end if
  28.        (if (eq l "2")
  29.          ((lambda (/ ent)
  30.            (while (not (setq ent (car (entsel)))))
  31.            (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 1)
  32.             );end wile
  33.          ); end lambda
  34.        ); end if
  35.          ((lambda (/ ent)
  36.            (while (not (setq ent (car (entsel)))))
  37.            (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 0.2)
  38.             );end wile
  39.          ); end lambda
  40.        ); end if
  41.  
  42. ) ; end if
  43.  
  44.  

Thanks

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: scale image from center
« Reply #1 on: June 14, 2021, 02:26:58 PM »
Calculate center point and then provide that point to scale command...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

PM

  • Guest
Re: scale image from center
« Reply #2 on: June 14, 2021, 05:23:13 PM »
I find a Lee Mac code.


Code - Auto/Visual Lisp: [Select]
  1. ;; Scale About Center  -  Lee Mac
  2.  
  3. (defun c:sac ( / ll sel obj ur )
  4.    (initget 6)
  5.    (if
  6.        (and
  7.            (setq *scl*
  8.                (cond
  9.                    (   (getdist (strcat "\nSpecify Scale Factor" (if *scl* (strcat " <" (rtos *scl* 2) ">: ") ": "))))
  10.                    (   *scl*   )
  11.                )
  12.            )
  13.            (ssget "_:L")
  14.        )
  15.        (progn
  16.                (if (null (vl-catch-all-error-p (vl-catch-all-apply 'vla-getboundingbox (list obj 'll 'ur))))
  17.                    (vla-scaleentity obj
  18.                        (vlax-3D-point
  19.                            (mapcar '(lambda ( a b ) (/ (+ a b) 2.0))
  20.                                (vlax-safearray->list ll)
  21.                                (vlax-safearray->list ur)
  22.                            )
  23.                        )
  24.                        *scl*
  25.                    )
  26.                )
  27.            )
  28.            (vla-delete sel)
  29.        )
  30.    )
  31.    (princ)
  32. )
  33.  
  34.  

can any one help to add this option menu with the specific scales from the previous code?

Code - Auto/Visual Lisp: [Select]
  1.  
  2.  (princ "\n")(princ "\n")(princ "\n")
  3.            (initget "1 2 3")
  4.            (setq
  5.              l
  6.               (cond
  7.                 ((getkword
  8.                    "\nslelect image 1:1 :
  9.                       1. for scale 1:500.
  10.                       2. for scale 1:1000.
  11.                       3. for scale 1:5000.
  12. "
  13.                  )
  14.                 )
  15.                 ("1")
  16.               )
  17.            )
  18.  
  19.  


Thanks

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: scale image from center
« Reply #3 on: June 14, 2021, 10:21:39 PM »
Try this it will make a dcl for you look at ans. There is examples at the top of the code, perfect for this type of "please choose".

Code: [Select]
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not but)(setq but 1))
(setq ans  (ah:butts but "V"   '("Choose scale 1:X " "  100" "  200" "  300" "  400" "  500" )))



« Last Edit: June 14, 2021, 10:25:48 PM by BIGAL »
A man who never made a mistake never made anything

PM

  • Guest
Re: scale image from center
« Reply #4 on: June 15, 2021, 02:23:19 AM »
HI BIGAL. i confused now. Can any one do the changes in post 3 ?

Thanks

PM

  • Guest
Re: scale image from center
« Reply #5 on: June 15, 2021, 02:27:29 AM »
I find and this code for center scale

Code - Auto/Visual Lisp: [Select]
  1. ;;  ScaleAboutCenters.lsp [command name: SAC]
  2. ;;  To Scale multiple objects, each About its own Center, by the same User-specified
  3. ;;    scale factor.
  4. ;;  Uses the middle of each object's bounding box as the base point for scaling, to
  5. ;;    keep objects centered at approximately the same position in the drawing.
  6. ;;    [For Mtext, that will be based on the defined Mtext box width, not the extents
  7. ;;    of the content; for a Block or Text, the center of its extents in the drawing, not
  8. ;;    its insertion point; for an Arc, the center of its extents, not its geometric center;
  9. ;;    some entity types' (e.g. Spline's) bounding box can sometimes reach beyond
  10. ;;    its extents and affect results slightly.]
  11. ;;  Rejects selection of objects on locked Layers, or without a "center" [Rays, Xlines].
  12. ;;  Stores scale factor; offers as default on subsequent use in same editing session.
  13. ;;  Kent Cooper, 6 May 2014
  14.  
  15. (defun C:SAC (/ *error* cmde ss inc ent)
  16.   (defun *error* (errmsg)
  17.     (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
  18.       (princ (strcat "\nError: " errmsg))
  19.     ); end if
  20.     (command "_.undo" "_end")
  21.     (setvar 'cmdecho cmde)
  22.     (princ)
  23.   ); end defun - *error*
  24.   (setq cmde (getvar 'cmdecho))
  25.   (setvar 'cmdecho 0)
  26.   (command "_.undo" "_begin")
  27.   (setq *SACscl
  28.     (cond
  29.       ( (getreal
  30.           (strcat
  31.             "\nEnter Scale Factor <"
  32.             (if *SACscl (rtos *SACscl 2 4) "1"); offer default: prior value / 1 on first use
  33.             ">: "
  34.           ); strcat
  35.         ); getreal
  36.       ); User-input condition
  37.       (*SACscl); Enter on subsequent use [prior value]
  38.       (1); Enter on first use
  39.     ); cond & *SACscl
  40.     ss (ssget ":L" '((-4 . "<NOT") (0 . "RAY,XLINE") (-4 . "NOT>")))
  41.       ;; not objects on Locked Layers or without finite extents
  42.   ); setq
  43.   (repeat (setq inc (sslength ss))
  44.     (setq ent (ssname ss (setq inc (1- inc))))
  45.     (vla-getboundingbox (vlax-ename->vla-object ent) 'minpt 'maxpt)
  46.     (command
  47.       ".scale" ent "" "_none"
  48.       (mapcar '/ ; midpoint of bounding box
  49.         (mapcar '+ (vlax-safearray->list minpt) (vlax-safearray->list maxpt))
  50.         '(2 2 2)
  51.       ); mapcar
  52.       *SACscl
  53.     ); command
  54.   ); repeat
  55.   (command "_.undo" "_end")
  56.   (setvar 'cmdecho cmde)
  57.   (princ)
  58. ); defun
  59. (prompt "\nType SAC to Scale objects About each one's Center.")
  60.  

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: scale image from center
« Reply #6 on: June 15, 2021, 08:27:54 PM »
This replaces all the (setq *SACscl code 27-42 just add more scales as you want.

Code: [Select]
(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
(if (not but)(setq but 1))
(setq *SACscl (atof (ah:butts but "V"   '("Choose scale 1:X " "  1" "  500" "  1000" "  2000" "  5000" ))))
(setq ss (ssget ":L" '((-4 . "<NOT") (0 . "RAY,XLINE") (-4 . "NOT>"))))
      ;; not objects on Locked Layers or without finite extents

A man who never made a mistake never made anything

PM

  • Guest
Re: scale image from center
« Reply #7 on: June 16, 2021, 02:21:58 AM »
Hi BIGAL. I prefer to convert 

this

Code - Auto/Visual Lisp: [Select]
  1. (defun c:myimgscale (/)
  2.  
  3.  (princ "\n")(princ "\n")(princ "\n")
  4.            (initget "1 2 3")
  5.            (setq
  6.              l
  7.               (cond
  8.                 ((getkword
  9.                    "\nslelect image 1:1 :
  10.                       1. for scale 1:500.
  11.                       2. for scale 1:1000.
  12.                       3. for scale 1:5000.
  13. "
  14.                  )
  15.                 )
  16.                 ("1")
  17.               )
  18.            )
  19.  
  20. (if (eq l "1")
  21.          ((lambda (/ ent)
  22.            (while (not (setq ent (car (entsel)))))
  23.            (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 2)
  24.             );end wile
  25.          ); end lambda
  26.        ); end if
  27.        (if (eq l "2")
  28.          ((lambda (/ ent)
  29.            (while (not (setq ent (car (entsel)))))
  30.            (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 1)
  31.             );end wile
  32.          ); end lambda
  33.        ); end if
  34.          ((lambda (/ ent)
  35.            (while (not (setq ent (car (entsel)))))
  36.            (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 0.2)
  37.             );end wile
  38.          ); end lambda
  39.        ); end if
  40.  
  41. ) ; end if
  42.  
  43.  


with


Code - Auto/Visual Lisp: [Select]
  1.     ;;  ScaleAboutCenters.lsp [command name: SAC]
  2.     ;;  To Scale multiple objects, each About its own Center, by the same User-specified
  3.     ;;    scale factor.
  4.     ;;  Uses the middle of each object's bounding box as the base point for scaling, to
  5.     ;;    keep objects centered at approximately the same position in the drawing.
  6.     ;;    [For Mtext, that will be based on the defined Mtext box width, not the extents
  7.     ;;    of the content; for a Block or Text, the center of its extents in the drawing, not
  8.     ;;    its insertion point; for an Arc, the center of its extents, not its geometric center;
  9.     ;;    some entity types' (e.g. Spline's) bounding box can sometimes reach beyond
  10.     ;;    its extents and affect results slightly.]
  11.     ;;  Rejects selection of objects on locked Layers, or without a "center" [Rays, Xlines].
  12.     ;;  Stores scale factor; offers as default on subsequent use in same editing session.
  13.     ;;  Kent Cooper, 6 May 2014
  14.      
  15.     (defun C:SAC (/ *error* cmde ss inc ent)
  16.       (defun *error* (errmsg)
  17.         (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
  18.           (princ (strcat "\nError: " errmsg))
  19.         ); end if
  20.         (command "_.undo" "_end")
  21.         (setvar 'cmdecho cmde)
  22.         (princ)
  23.       ); end defun - *error*
  24.       (setq cmde (getvar 'cmdecho))
  25.       (setvar 'cmdecho 0)
  26.       (command "_.undo" "_begin")
  27.       (setq *SACscl
  28.         (cond
  29.           ( (getreal
  30.               (strcat
  31.                 "\nEnter Scale Factor <"
  32.                 (if *SACscl (rtos *SACscl 2 4) "1"); offer default: prior value / 1 on first use
  33.                 ">: "
  34.               ); strcat
  35.             ); getreal
  36.           ); User-input condition
  37.           (*SACscl); Enter on subsequent use [prior value]
  38.           (1); Enter on first use
  39.         ); cond & *SACscl
  40.         ss (ssget ":L" '((-4 . "<NOT") (0 . "RAY,XLINE") (-4 . "NOT>")))
  41.           ;; not objects on Locked Layers or without finite extents
  42.       ); setq
  43.       (repeat (setq inc (sslength ss))
  44.         (setq ent (ssname ss (setq inc (1- inc))))
  45.         (vla-getboundingbox (vlax-ename->vla-object ent) 'minpt 'maxpt)
  46.         (command
  47.           ".scale" ent "" "_none"
  48.           (mapcar '/ ; midpoint of bounding box
  49.             (mapcar '+ (vlax-safearray->list minpt) (vlax-safearray->list maxpt))
  50.             '(2 2 2)
  51.           ); mapcar
  52.           *SACscl
  53.         ); command
  54.       ); repeat
  55.       (command "_.undo" "_end")
  56.       (setvar 'cmdecho cmde)
  57.       (princ)
  58.     ); defun
  59.     (vl-load-com)
  60.     (prompt "\nType SAC to Scale objects About each one's Center.")
  61.      
  62.  


Thanks

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: scale image from center
« Reply #8 on: June 16, 2021, 08:47:43 PM »
A hidden part of the multi radio button is look at variable BUT it is which button number is picked so you can have your 1. 1:1, 2. 1:200

Just do the code above then type !but you will see a number appear that matches the button picked. You dont need the (setq ans if you use the but value.

(ah:butts but "V"   '("Choose scale 1:X " "1.  1" "2.  500" "3.  1000" "4.   2000" "5.   5000" ))

Also
Code: [Select]
(while (not (setq ent (car (entsel "\npick entity ")))))
(cond
     ((= but 1)        (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 1.0))
     ((= but 2)        (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 2.0))
     ((= but 3)        (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 0.2))    
)
« Last Edit: June 16, 2021, 08:51:24 PM by BIGAL »
A man who never made a mistake never made anything

PM

  • Guest
Re: scale image from center
« Reply #9 on: June 17, 2021, 05:55:45 AM »
 BIGAL i want to ask something. When i choose a scale the window close before i press OK  is it possible to fix it?


Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/)
  2.  
  3. (if (not AH:Butts)(load "Multi Radio buttons.lsp"))
  4. (if (not but)(setq but 1))
  5. (setq *testscl (atof (ah:butts but "V"   '("Choose scale 1:X" "1.  1:500" "2.  1:1000" "3.  1:5000"))))
  6.  
  7.  
  8.  
  9. (while (not (setq ent (car (entsel "\npick entity ")))))
  10.      ((= but 1)        (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 2.0))
  11.      ((= but 2)        (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 1.0))
  12.      ((= but 3)        (command "_scale" ent "" (cdr (assoc 11 (entget ent))) 0.2))        
  13. )      
  14. )
  15.  
  16. ; Multi button Dialog box for a single choice repalcment of initget
  17. ; By Alan H Feb 2019 info@alanh.com.au
  18.  
  19. ; Example code as the radio button click will close the default button setting is required
  20. ; It will remember what button ws pressed if ran again with same request.
  21. ; You can have "H" horizontal or "V" vertical for DCL.
  22.  
  23.  
  24. ; a=97 A=65 1=49 0=48
  25. ; example make an options list a-h 97 then 8 more.
  26. ; (if (not AH:Butts)(load "Multi Radio buttons.lsp"))
  27. ; (if (not but)(setq but 1))
  28. ; (setq ans2 (ah:butts 1 "v"  (make_letters "Please choose" 97 8)))
  29.  
  30.  
  31. ; (if (not AH:Butts)(load "Multi radio buttons.lsp"))           ; loads the program if not loaded already
  32. ; (if (not but)(setq but 1))                            ; this is needed to set default button
  33.                                                 ; you can reset default button to user pick
  34. ; (if (not AH:Butts)(load "Multi Radio buttons.lsp"))
  35. ; (if (not but)(setq but 1))
  36. ; (setq ans (substr  (ah:butts but "V"   '("Choose colour to remain " "  1" "  2" "  3" "  4" "  5" "  6" "  7")) ))
  37. ; spaced characters
  38. ; or just use "but" value and compare to a list
  39. ; ans holds the button picked value as a string
  40.                                                                         ; if you want ans a number use (atof ans) or (atoi ans)
  41.  
  42. ;(if (not AH:Butts)(load "Multi Radio buttons.lsp"))
  43. ;(if (= but nil)(setq but 1))
  44. ;(setq ans (ah:butts but "h"  '("Yes or No" "Yes" "No"))) ; ans holds the button picked value
  45.  
  46.  
  47. ; (if (not AH:Butts)(load "Multi Radio buttons.lsp"))
  48. ; (if (= but nil)(setq but 1))
  49. ; (setq ans (atoi(ah:butts but "V" '("Choose a number" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10")))) ; ans holds the button picked as an integer value
  50.  
  51. (defun make_letters (heading  asc numch  / lstchar)
  52. (if (/= heading nil)(setq lstchar (list heading))(setq lstchar '()))
  53. (repeat numch
  54. (setq lstchar (cons (chr asc) lstchar))
  55. (setq asc (+ asc 1))
  56. )
  57. (setq lstchar (reverse lstchar))
  58. (princ lstchar)
  59. )
  60.  
  61. (defun AH:Butts (AHdef verhor butlst / fo fname x  k numch asc)
  62. (setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w"))
  63. ;(setq fo (open (setq fname "D:\\acadtemp\\test.dcl") "w"))
  64. (write-line  "AHbutts : dialog  {" fo)
  65. (write-line  (strcat "  label =" (chr 34) (nth 0 butlst) (chr 34) " ;" )fo)
  66. (write-line "   : row   {" fo)
  67. (if (=  (strcase verhor) "V")
  68. (write-line "   : boxed_radio_column    {" fo)
  69. (write-line  (strcat " width = " (rtos (+ (strlen (nth 0 butlst)) 10) 2 0) " ;")  fo)           ; increase 10 if label does not appear
  70. )
  71. (write-line "   : boxed_radio_row       {" fo)
  72. )
  73. (setq x 1)
  74. (setq numch (length butlst))
  75. (repeat (- numch 1)
  76. (write-line "   : radio_button  {" fo)
  77. (write-line  (strcat "key = "  (chr 34) "Rb" (rtos x 2 0)  (chr 34) ";") fo)
  78. (write-line  (strcat "label = " (chr 34) (nth x  butlst) (chr 34) ";") fo)
  79. (write-line "   }" fo)
  80. ;(if (or (= numch nil) (< numch 8))
  81. (write-line "spacer_1 ;" fo)
  82. ;)
  83. (setq x (+ x 1))
  84. )
  85. (write-line "   }" fo)
  86. (write-line "   }" fo)
  87. (write-line "spacer_1 ;" fo)
  88. (write-line "   ok_only;" fo)
  89. (write-line "   }" fo)
  90. (close fo)
  91. (setq dcl_id (load_dialog fname))
  92. (if (not (new_dialog "AHbutts" dcl_id) )
  93. )
  94. (setq x 1)
  95. (repeat (- (length butlst) 1)
  96. (setq k (strcat "Rb" (rtos x 2 0)))
  97. (action_tile k  (strcat "(setq but "  (rtos x 2 0) ")" "(done_dialog)"))
  98. (if (= ahdef x)(set_tile k "1"))
  99. (setq x (+ x 1))
  100. )
  101. (action_tile "accept" (strcat "(setq but "  (rtos ahdef 2 0) ")" "(done_dialog)"))
  102. (unload_dialog dcl_id)
  103. (nth but butlst)
  104.  
  105. ) ; end defun
  106.  
  107.  
  108.  
  109.  

Thanks

BIGAL

  • Swamp Rat
  • Posts: 1417
  • 40 + years of using Autocad
Re: scale image from center
« Reply #10 on: June 17, 2021, 09:18:46 PM »
I wrote it that way its a single pick answer that is a rule with Radio Buttons, it resets which button is high lighted so when run again and want same button would just pick OK.

You only need the 3 lines of code for it to work the (if (not AH:Butts)(load "Multi Radio buttons.lsp")) will load the code automatically from a directory, 2 methods make sure Multi Radio Buttons.lsp is saved in a "Support" directory. Or change the load to full path eg (load "d:\\alan\\lisps\\Multi radio buttons.lsp") so it does not need to live in your code.

You can use the if not on other providers a good example is the excellent code provided  by Lee-mac.com.
(if (not LM:readcsv)(load "Readcsv-V1-3"))
A man who never made a mistake never made anything