Jump to content

Recommended Posts

So I have tried to modify a batt insulation lsp originally created by David Sides to draw studs. You would think this would be easy because all I need are 45 degree lines, but I am having some trouble. I have gotten it to work for Horizontal walls and it is ok for vertical walls a bit glitchy but not horrible. Walls at an angle on the otherhand are horrible. Any advice?

 

 


;; 
STUD.LSP
;;
;;==========================================================
(setq 
xos (getvar "osmode"))
(setvar "osmode" 0)
(defun SSV 
(savelist)
(mapcar
 '(lambda 
(sysvar)
  (list sysvar (getvar 
sysvar))
 )
savelist)
)
(setq lista '("aunits" 
"auprec" "blipmode" "cmdecho" 

                      "orthomode"))
;;=========================================================
(defun 
RSV (savelist)
(mapcar
 '(lambda 
(sysvar)
  (setvar (car sysvar)(cadr 
sysvar))
  (car 
sysvar)
 )
savelist)
)
;;==========================================================
(defun 
HI ()
(setq olderr *error*)
(defun *error* 
(errstr)
 (print errstr)(princ)
 (setq lista (RSV 
lista))
 (setq *error* 
olderr)
 (princ)
)
(setq lista (SSV 
lista))
(setvar "aunits" 3)
(setvar "auprec" 

(setvar "blipmode" 0)
(setvar "cmdecho" 0)
(setvar 
"orthomode" 0)
(prompt "\nArchitectural studs. ")
(prompt 
"\nBy Alison Baillargeon. July 12, 2011. 
")
(princ)
)
;;============================================================
(defun 
BYE ()
(setq lista (RSV lista))
(setq *error* 
olderr)(princ)
(princ "\nProcessing complete. 
")
(princ)
)
;;============================================================
(defun 
c:STUD (/ A ANG1 ANG2 ANG3 LEN1 LEN2 PT1 PT2 PT3 PT4 PT5 THK1 THK2 THK3 
THK4)
(HI)
(setq PT1 (getpoint "\nEnter start point... 
")
   PT2 (getpoint PT1 "\nEnter end point. 
")
   THK1(getreal "\nEnter wall thickness. 
")
   THK2(* THK1 
THK1)
   THK3(+ THK2 
THK2)
   THK4(sqrt 
THK3)
   LEN1(distance PT1 
PT2)
   LEN2(/ LEN1 
THK1)
   ANG1(angle PT1 
PT2)
   ANG2(+ ANG1 
0.7854)
   ANG3(+ ANG1 
5.4977)
   a 0
)
(while (< a 
LEN2)
 (if (= a 
0)
  (progn
   (setq PT3 (polar PT1 
ANG2 THK4)
    PT4 (polar PT3 ANG3 
THK4)
    PT5 (polar PT4 ANG2 
THK4)
      A 
0.5
   )
   (command ".pline" 
PT1 "w" 0.0 0.0 PT3 PT4)
  )


  (progn
   (setq PT3 
PT5
    PT4 (polar PT3 ANG3 
THK4)
    PT5 (polar PT4 ANG2 
THK4))
   (command PT3 
PT4)
   (setq a (+ a 
2))
  )
 )
)
(command 
"")
(command ".break" "l" PT4 (polar PT4 ANG1 
THK4))
       (BYE)
(setvar 
"osmode" xos)
(princ)
)

Edited by Movieangel

Share this post


Link to post
Share on other sites

I think I almost got it. My lisp works just as I would expect to create studs (after a whole day of trial and error as I am a newbie to Lisps) but it only works the first time after it has been loaded. In order for it to do what I expect after the first time, I have to type APPLOAD and reload it again. If I don't, it goes crazy. I am guessing it does not reset syetem variables and such, but I don't know how to fix it. Any help would be appreciated.

 


;; STUD.LSP
;;
;;==========================================================
(setq 
xos (getvar "osmode"))
(setvar "osmode" 0)
(defun SSV 
(savelist)
(mapcar
 '(lambda 
(sysvar)
  (list sysvar (getvar 
sysvar))
 )
savelist)
)
(setq lista '("aunits" 
"auprec" "blipmode" "cmdecho" 

                      "orthomode"))
;;=========================================================
(defun 
RSV (savelist)
(mapcar
 '(lambda 
(sysvar)
  (setvar (car sysvar)(cadr 
sysvar))
  (car 
sysvar)
 )
savelist)
)
;;==========================================================
(defun 
HI ()
(setq olderr *error*)
(defun *error* 
(errstr)
 (print errstr)(princ)
 (setq lista (RSV 
lista))
 (setq *error* 
olderr)
 (princ)
)
(setq lista (SSV 
lista))
(setvar "aunits" 3)
(setvar "auprec" 

(setvar "blipmode" 0)
(setvar "cmdecho" 0)
(setvar 
"orthomode" 0)
(prompt "\nArchitectural studs. ")
(prompt 
"\nBy Alison Baillargeon. July 12, 2011. 
")
(princ)
)
;;============================================================
(defun 
BYE ()
(setq lista (RSV lista))
(setq *error* 
olderr)(princ)
(princ "\nProcessing complete. 
")
(princ)
)
;;============================================================
(defun 
c:STUD (/ A ANG1 ANG2 ANG3 LEN1 LEN2 PT1 PT2 PT3 PT4 PT5 THK1 THK2 THK3 
THK4)
(HI)
(setq PT1 (getpoint "\nEnter start point... 
")
   PT2 (getpoint PT1 "\nEnter end point. 
")
   THK1(getreal "\nEnter wall thickness. 
")
   THK2(* THK1 
THK1)
   THK3(+ THK2 
THK2)
   THK4(sqrt 
THK3)
   LEN1(distance PT1 
PT2)
   LEN2(/ LEN1 
THK1)
   ANG1(angle PT1 
PT2)
   ANG2(+ ANG1 
0.7854)
   ANG3(+ ANG1 
5.4977)
   a 0
)
(while (< a 
LEN2)
 (if (= a 
0)
  (progn
   (setq PT3 (polar PT1 
ANG2 THK4)
    PT4 (polar PT3 ANG3 
THK4)
    PT5 (polar PT4 ANG2 
THK4)
      A 
0.5
   )
   (command ".pline" 
PT1 "w" 0.0 0.0 PT3 PT4)
  )


  (progn
   (setq PT3 
PT5
    PT4 (polar PT3 ANG3 
THK4)
    PT5 (polar PT4 ANG2 
THK4))
   (command PT3 
PT4)
   (setq a (+ a 
2))
  )
 )
)
(command 
"")
(command ".break" "l" PT4 (polar PT4 ANG1 
THK4))
       (BYE)
(setvar 
"osmode" xos)
(princ)
)

Share this post


Link to post
Share on other sites

The following code uses a change in coordinate frame to make the calculation of the 'stud' points more succinct, however the drawback for this crafty shortcut lies in the fact that the trans function uses the arbitrary axis algorithm to define a coordinate frame from the supplied normal vector, and so this code will only work in UCS for which the UCS plane is parallel to the WCS plane.

 

(defun c:stud ( / p1 p2 n l s ) ;; © Lee Mac 2011
 (if
   (and
     (setq p1 (getpoint "\nSpecify Start Point: "))
     (setq p2 (getpoint "\nSpecify End Point: " p1))
     (progn (initget 6)
       (setq *w
         (cond
           (
             (getdist p1
               (strcat "\nSpecify Wall Thickness"
                 (if *w (strcat " <" (rtos *w) ">: ") ": ")
               )
             )
           )
           ( *w )
         )
       )
     )
   )
   (progn
     (setq n (mapcar '- (trans p2 1 0) (trans p1 1 0))
           l (list (trans p1 1 n))
           s -1
     )
     (repeat (fix (/ (distance p1 p2) *w))
       (setq l (cons (list (+ (* (setq s (- s)) *w) (caar l)) (cadar l) (+ *w (caddar l))) l))
     )
     (entmakex
       (append
         (list
           (cons 0 "LWPOLYLINE")
           (cons 100 "AcDbEntity")
           (cons 100 "AcDbPolyline")
           (cons 90 (length l))
           (cons 70 0)
         )
         (mapcar '(lambda ( p ) (cons 10 (trans p n 0))) l)
       )
     )
   )
 )
 (princ)
)

 

The alternative approach, using polar may look something like this, and would work in all UCS/Views:

 

(defun c:stud ( / p1 p2 a n l s ) ;; © Lee Mac 2011
 (if
   (and
     (setq p1 (getpoint "\nSpecify Start Point: "))
     (setq p2 (getpoint "\nSpecify End Point: " p1))
     (progn (initget 6)
       (setq *w
         (cond
           (
             (getdist p1
               (strcat "\nSpecify Wall Thickness"
                 (if *w (strcat " <" (rtos *w) ">: ") ": ")
               )
             )
           )
           ( *w )
         )
       )
     )
   )
   (progn
     (setq n (trans '(0. 0. 1.) 1 0 t)
           a (angle p1 p2)
           l (list p1)
           s -1
     )
     (repeat (fix (/ (distance p1 p2) *w))
       (setq l (cons (polar (polar (car l) a *w) (+ a (* (setq s (- s)) (/ pi 2.))) *w) l))
     )
     (entmakex
       (append
         (list
           (cons 0 "LWPOLYLINE")
           (cons 100 "AcDbEntity")
           (cons 100 "AcDbPolyline")
           (cons 90 (length l))
           (cons 70 0)
           (cons 210 n)
         )
         (mapcar '(lambda ( p ) (cons 10 (trans p 1 n))) l)
       )
     )
   )
 )
 (princ)
)

Edited by Lee Mac

Share this post


Link to post
Share on other sites

I have deleted your other thread regarding the reloading of your LISP, because you have also asked in this thread and received a response.

Share this post


Link to post
Share on other sites

Wow Lee Mac. Thanks so much for the code. I worked all day on mine, no kidding, over 8 hours, and it was still glitchy and yours is totally different but works perfectly. I wish I could understand lisps a bit better so this kind of stuff wouldn't take me forever, but I do really appreciate your help and maybe I can disect your code to figure out how it actually works to make future attempts a bit easier. Both work great. I will only be using this on floor plans so the UCS should not have any issues. Are there any advantages of one over the other? Thanks again. Really Really appreciate it.

Share this post


Link to post
Share on other sites
Wow Lee Mac. Thanks so much for the code. I worked all day on mine, no kidding, over 8 hours, and it was still glitchy and yours is totally different but works perfectly. I wish I could understand lisps a bit better so this kind of stuff wouldn't take me forever, but I do really appreciate your help and maybe I can disect your code to figure out how it actually works to make future attempts a bit easier.

 

You're very welcome :)

 

To be honest, its mostly practice coupled with a lot of reading - almost everything I know about LISP I have learnt from the Visual LISP IDE Help Documentation and these (and other) forums.

 

Both work great. I will only be using this on floor plans so the UCS should not have any issues. Are there any advantages of one over the other? Thanks again. Really Really appreciate it.

 

The first code was demonstrating an alternative approach to the usual methods, I define a coordinate frame such that the points may be calculated at zero rotation, then transformed back to WCS when creating the LWPolyline. This allows for easier coordinate manipulation, but has some drawbacks. I was mainly experimenting with other ways to approach the problem, the second code is far more practical.

 

The second code would be a better base for a beginner to learn from since it uses a more intuitive approach, calculating the points of the LWPolyline using the polar function.

 

If you have any questions about any of the code I have posted I would be happy to provide some clarification.

 

Lee

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×