Author Topic: door lisp  (Read 1984 times)

0 Members and 2 Guests are viewing this topic.

PM

  • Guest
door lisp
« on: December 14, 2022, 09:32:16 AM »
Hi i write this code to insert block door. I have a problem in this code. I pick 2 points to give the width of door (is the scale of block door) and i pick 1 point to insert the door. I want the insert block have the rotation angle of the two points i pick in the beginning when i set the width of the door. CAn any one help?

Code - Auto/Visual Lisp: [Select]
  1. (Defun c:ed1()
  2.  (command "_layer" "_m" "WINDOOR" "_c" "90" "" "")
  3.  (setvar "OSMODE" 3) ; end ,mid, 1+2 =3
  4.  (setq scl (getdist "\n pick 2 points for door width"))
  5.  (setq scl1 scl)
  6.  (setq dt1 (getpoint "\n Pick insert point"))
  7.  (command "insert" "c:\\blocks\\d1.dwg" dt1 scl1 scl1)
  8.  (command "setvar" "clayer" "0")
  9. )
  10.  

Thanks

mhupp

  • Bull Frog
  • Posts: 250
Re: door lisp
« Reply #1 on: December 14, 2022, 11:50:25 AM »
Use two points to get the scale and angle. angle function returns radians so you have to convert to degrees to use it in the insert.
Code - Auto/Visual Lisp: [Select]
  1. (Defun c:ed1(/ oldlay lay pt1 pt2 scl ang)
  2.   ;(command "_layer" "_m" "WINDOOR" "_c" "90" "" "")
  3.   (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "WINDOOR") (70 . 0) (62 . 90)))  
  4.   (setq oldos (getvar 'osmode))
  5.   (setq lay (getvar 'clayer))
  6.   (setvar 'OSMODE 3) ; end ,mid, 1+2 =3
  7.   (setq pt1 (getpoint "\nPick Points for Door Width: ")
  8.         pt2 (getpoint pt1)
  9.         scl (distance pt1 pt2)
  10.         ang (/ (* (angle pt1 pt2) 180.0) pi)
  11.   )  
  12.   (if (tblsearch "Block" "d1") ;if block is existing in drawing pull from block library its faster
  13.     (command "_.Insert" "d1" (getpoint "\nInsert Point: ") scl scl ang)  
  14.     (command "_.Insert" "c:\\blocks\\d1.dwg" (getpoint "\nInsert Point: ") scl scl ang)
  15.   )
  16.   ;(command "setvar" "clayer" "0")
  17.   (setvar 'clayer lay) ;restore snaps and layer before command
  18.   (setvar 'osmode oldos)
  19. )

PKENEWELL

  • Bull Frog
  • Posts: 320
Re: door lisp
« Reply #2 on: December 14, 2022, 11:59:00 AM »
Mhupp beat me to the point.  8-)

However - Here is a re-write how I would do it with several improvements and shows how to get the scale and rotation angle, and adds other things like error handling, local functions and variables, and setting an undo group. This could be improved further with visual LISP functions, but I think this is enough for you to chew on right now.

Code - Auto/Visual Lisp: [Select]
  1. (Defun c:ed1( / *error* rtd blkn ol os p1 p2 scl ang); localize variables
  2.    
  3.    ; Local error function; will not interfere with other local or global error functions
  4.    (defun *error* (msg)
  5.       (if (not (wcmatch (strcase msg T) "*break*,*cancel*,*quit*,*exit*"))
  6.          (princ (strcat "\nError: " msg "\n"))
  7.          (princ "\nProgram Aborted.\n")
  8.       )
  9.       ; restore system variables to original values.
  10.      (setvar "clayer" ol)
  11.      (setvar "osmode" os)
  12.      ; End undo group
  13.      (command "._Undo" "_E")
  14.    )
  15.  
  16.    ;; function to convert radians to degrees.
  17.    (defun rtd (ang)(* (/ ang pi) 180.0))
  18.  
  19.   ; Get old values of system variable to be changed.
  20.   (setq ol (getvar "clayer") os (getvar "osmode")
  21.         blkn "c:\\blocks\\d1.dwg"
  22.   )
  23.    ; Begin undo group
  24.   (command "._Undo" "_BE")
  25.   (setvar "OSMODE" 3) ; end ,mid, 1+2 =3
  26.  
  27.   (princ "\n Pick 2 points for door width: ")
  28.   ;; ensures all input is entered before running progam to prevent errors due to operator aborting.
  29.   (if (and
  30.         (setq p1 (getpoint "\nfirst Point: "))
  31.         (setq p2 (getpoint p1 "\nSecond Point: "))
  32.       )
  33.     (progn
  34.        ; check to see if layer exists before creating
  35.        (if (not (tblsearch "LAYER" "WINDOOR"))
  36.           (command "_layer" "_m" "WINDOOR" "_c" "90" "" "")
  37.        )
  38.        ; get the scale and angle
  39.        (setq scl (distance p1 p2)
  40.              ang (angle p1 p2)
  41.        )
  42.        ; get the insertion point then insert the block
  43.        (if (setq ins (getpoint "\nPick insert point for door: "))
  44.           (command "._-insert" blkn "_non" ins scl "" (rtd ang))
  45.        )
  46.     )
  47.   )
  48.  
  49.   ; restore system variables to original values.
  50.   (setvar "clayer" ol)
  51.   (setvar "osmode" os)
  52.  
  53.   ; End undo group
  54.   (command "._Undo" "_E")
  55.   ; exits quiently with no return value
  56.   (princ)
  57. )
  58.  

Edit: Oops had the wrong angle function - corrected.
« Last Edit: December 14, 2022, 12:09:16 PM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

mhupp

  • Bull Frog
  • Posts: 250
Re: door lisp
« Reply #3 on: December 14, 2022, 12:21:11 PM »
also might get the wrong angle depending on how you pick the points. so another feature prob would be change the angle by 180

PM

  • Guest
Re: door lisp
« Reply #4 on: December 14, 2022, 01:45:37 PM »
Hi, I am working in grads so i change

Code: [Select]
   ;; function to convert radians to degrees.
   (defun rtd (ang)(* (/ ang pi) 180.0))

with

Code: [Select]
   ;; function to convert radians to grads.
   (defun rtd (ang)(* (/ ang pi) 200.0))

But i find an error in with the angle !!!!

Any ideas?

mhupp

  • Bull Frog
  • Posts: 250
Re: door lisp
« Reply #5 on: December 14, 2022, 03:22:56 PM »
look at my post above. See if this helps no longer picking a base point.
First point is also the insertion point for d1.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo(/ pt1 pt2 scl ang)
  2.   ;(command "_layer" "_m" "WINDOOR" "_c" "90" "" "")
  3.   (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "WINDOOR") (70 . 0) (62 . 90)))  
  4.   (setq os (getvar 'osmode))
  5.   (setq ol (getvar 'clayer))
  6.   (setvar 'cmdecho 0)
  7.   (setvar 'osmode 3) ; end ,mid, 1+2 =3
  8.   (setq pt1 (getpoint "\nPick 2 Points for Door Width")
  9.         pt2 (getpoint pt1)
  10.         scl (distance pt1 pt2)
  11.         ang (/ (* (angle pt1 pt2) 200.0) pi)
  12.   )  
  13.   (if (tblsearch "Block" "d1") ;if block is existing in drawing pull from block library its faster
  14.     (command "_.Insert" "d1" pt1 scl scl ang)  
  15.     (command "_.Insert" "c:\\blocks\\d1.dwg" pt1 scl scl ang)
  16.   )
  17.   (setq blk (entlast))
  18.   (initget "Yes No")
  19.   (setq rep
  20.     (cond
  21.       ((getkword "\nOther Direction? Yes/<No>: ")) ("No")
  22.     )
  23.   )
  24.   (cond
  25.     ((= rep "Yes")
  26.       (entdel blk)
  27.       (setq ang (/ (* (angle pt2 pt1) 200.0) pi))
  28.       (if (tblsearch "Block" "d1") ;if block is existing in drawing pull from block library its faster
  29.         (command "_.Insert" "d1" pt2 scl scl ang)  
  30.         (command "_.Insert" "c:\\blocks\\d1.dwg" pt2 scl scl ang)
  31.       )
  32.     )
  33.   )    
  34.   ;(command "setvar" "clayer" "0")
  35.   (setvar 'clayer ol)
  36.   (setvar 'osmode os)
  37.   (setvar 'cmdecho 1)
  38. )
« Last Edit: December 14, 2022, 03:30:09 PM by mhupp »

PM

  • Guest
Re: door lisp
« Reply #6 on: December 14, 2022, 03:55:29 PM »
Hi mhupp . Thanks for you time. You didn' understand my question. PKENEWELL code works fine. The problem is when the walls are not horizontal or vertical , but have an angle the door not insert correct.  The angle is not correct

Thanks

PKENEWELL

  • Bull Frog
  • Posts: 320
Re: door lisp
« Reply #7 on: December 14, 2022, 04:51:32 PM »
PM - please send me a sample drawing and the block in question. I'll take a look at it and figure out what is going on. For your application - if might be easier to use the (angtos) function to get the formatted angle for the insert command, rather than the "rtd" sub-function. I.e. change:
Code - Auto/Visual Lisp: [Select]
  1. (command "._-insert" blkn "_non" ins scl "" (rtd ang))
to
Code - Auto/Visual Lisp: [Select]
  1. (command "._-insert" blkn "_non" ins scl "" (angtos ang))
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

mhupp

  • Bull Frog
  • Posts: 250
Re: door lisp
« Reply #8 on: December 14, 2022, 05:10:50 PM »
Still think you should have an option to flip the door.


PM

  • Guest
Re: door lisp
« Reply #9 on: December 14, 2022, 05:28:28 PM »
mhupp your code is good but P prefer PKENEWELL.
PKENEWELL I upload the blocks i use and a test.dwg file. I  still have problem with the angle

BIGAL

  • Swamp Rat
  • Posts: 1429
  • 40 + years of using Autocad
Re: door lisp
« Reply #10 on: December 14, 2022, 09:47:44 PM »
My $0.05 the full package is a little more than that, insert a door asks pick a wall how far along then width and the 4 possible combintaions, L or R, Inor Out, auto trims say 2 lines internal walls or 4 lines external walls. Oh yeah and is 3d. Happy to discuss further.

There is some nice dynamic doors out there so allows more options in 1 block.

A man who never made a mistake never made anything

PM

  • Guest
Re: door lisp
« Reply #11 on: December 15, 2022, 08:41:15 AM »
Hi mhupp ,I try your code again , but I have problem with the angle of the door when insert !!!!


Thanks


PKENEWELL

  • Bull Frog
  • Posts: 320
Re: door lisp
« Reply #12 on: December 15, 2022, 10:15:36 AM »
mhupp your code is good but P prefer PKENEWELL.
PKENEWELL I upload the blocks i use and a test.dwg file. I  still have problem with the angle

OK - seems the door needs to be in open position. Try this:

Code - Auto/Visual Lisp: [Select]
  1. (Defun c:ed1( / *error* blkn ol os p1 p2 scl ang); localize variables
  2.  
  3.    (defun *error* (msg)
  4.            (if (not (wcmatch (strcase msg T) "*break*,*cancel*,*quit*,*exit*"))
  5.               (princ (strcat "\nError: " msg "\n"))
  6.          (princ "\nProgram Aborted.\n")
  7.       )
  8.       ; restore system variables to original values.
  9.      (setvar "clayer" ol)
  10.      (setvar "osmode" os)
  11.      ; End undo group
  12.      (command "._Undo" "_E")
  13.      (setvar "cmdecho" oe)
  14.      (princ)
  15.    )
  16.  
  17.   ; Get old values of system variable to be changed.
  18.   (setq ol (getvar "clayer") os (getvar "osmode") oe (getvar "cmdecho")
  19.         blkn "c:\\blocks\\d1.dwg"
  20.   )
  21.    ; Begin undo group
  22.   (command "._Undo" "_BE")
  23.   (setvar "OSMODE" 3) ; end ,mid, 1+2 =3
  24.  
  25.   (princ "\n Pick 2 points for door width: ")
  26.   ;; ensures all input is entered before running progam to prevent errors due to operator aborting.
  27.   (if (and
  28.         (setq p1 (getpoint "\nfirst Point: "))
  29.         (setq p2 (getpoint p1 "\nSecond Point: "))
  30.       )
  31.     (progn
  32.        ; check to see if layer exists before creating
  33.        (if (not (tblsearch "LAYER" "WINDOOR"))
  34.           (command "_layer" "_m" "WINDOOR" "_c" "90" "" "")
  35.        )
  36.        ; get the scale and angle
  37.        (setq scl (distance p1 p2)
  38.              ang (angle p1 p2)
  39.        )
  40.        ; get the insertion point then insert the block
  41.        (if (setq ins (getpoint "\nPick insert point for door: "))
  42.           (command "._-insert" blkn "_non" ins scl "" (angtos (+ (/ pi 2) ang)))
  43.        )
  44.     )
  45.   )
  46.  
  47.   ; restore system variables to original values.
  48.   (setvar "clayer" ol)
  49.   (setvar "osmode" os)
  50.  
  51.   ; End undo group
  52.   (command "._Undo" "_E")
  53.   (setvar "cmdecho" oe)
  54.   ; exits quietly with no return value
  55.   (princ)
  56. )
  57.  
« Last Edit: December 15, 2022, 11:02:47 AM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

PKENEWELL

  • Bull Frog
  • Posts: 320
Re: door lisp
« Reply #13 on: December 15, 2022, 10:23:51 AM »
Still think you should have an option to flip the door.

Personally - I like yours better Mhupp  :-) Here is mine altered to do the same. PM - if you still like the original version - see my previous post.

EDIT: Changed Flip direction to a Mirror command instead of flipping the insertion point and rotating.  :uglystupid2: Also cleaned up and added command echo suppression.

Code - Auto/Visual Lisp: [Select]
  1. (Defun c:ed1( / *error* blkn oe ol os p1 p2 rs scl ang); localize variables
  2.  
  3.    (defun *error* (msg)
  4.            (if (not (wcmatch (strcase msg T) "*break*,*cancel*,*quit*,*exit*"))
  5.               (princ (strcat "\nError: " msg "\n"))
  6.          (princ "\nProgram Aborted.\n")
  7.       )
  8.       ; restore system variables to original values.
  9.      (setvar "clayer" ol)
  10.      (setvar "osmode" os)
  11.      ; End undo group
  12.      (command-s "._Undo" "_E")
  13.      (setvar "cmdecho" oe)
  14.      (princ)
  15.    )
  16.  
  17.   ; Get old values of system variable to be changed.
  18.   (setq ol (getvar "clayer") os (getvar "osmode") oe (getvar "cmdecho")
  19.         blkn "c:\\blocks\\d1.dwg"
  20.   )
  21.   (setvar "cmdecho" 0)
  22.    ; Begin undo group
  23.   (command "._Undo" "_BE")
  24.   (setvar "OSMODE" 3) ; end ,mid, 1+2 =3
  25.  
  26.   (princ "\n Pick 2 points for door width: ")
  27.   ;; ensures all input is entered before running progam to prevent errors due to operator aborting.
  28.   (if (and
  29.         (setq p1 (getpoint "\nfirst (Insertion) Point: "))
  30.         (setq p2 (getpoint p1 "\nSecond Point: "))
  31.       )
  32.     (progn
  33.        ; check to see if layer exists before creating
  34.        (if (not (tblsearch "LAYER" "WINDOOR"))
  35.           (command "_layer" "_m" "WINDOOR" "_c" "90" "" "")
  36.        )
  37.        ; get the scale and angle
  38.        (setq scl (distance p1 p2)
  39.              ang (angle p1 p2)
  40.        )
  41.        ; Insert the block
  42.        (command "._-insert" blkn "_non" p1 scl "" (angtos (+ (/ pi 2) ang)))
  43.        ;; Initialize Input
  44.        (initget "Yes No")
  45.        ; prompt for flip door and mirror if "Yes".
  46.        (if (= (setq rs (getkword "\nFlip Direction? [Yes/No] <No>:")) "Yes")
  47.              (command "._mirror" (entlast) "" "_non" p1 "_non" p2 "y")
  48.        )
  49.     )
  50.   )
  51.  
  52.   ; restore system variables to original values.
  53.   (setvar "clayer" ol)
  54.   (setvar "osmode" os)
  55.   ; End undo group
  56.   (command "._Undo" "_E")
  57.   (setvar "cmdecho" oe)
  58.   ; exits quiently with no return value
  59.   (princ)
  60. )
  61.  
« Last Edit: December 15, 2022, 11:07:42 AM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

PM

  • Guest
Re: door lisp
« Reply #14 on: December 15, 2022, 10:29:59 AM »
Thank you PKENEWELL and mhupp for the help