Jump to content

(defun c: LISP THAT I NEED)


Qonfire

Recommended Posts

Salut

 

In this thread hopefully there will be a final lisp that I want and need

 

1st I need some way of measuring the area lets say rectangle.

 

(defun c:xxx (/ pt1 pt2 pt3 pt4 ltbay1 bay num_br)
(setq pt1 (getpoint "/nTop:"))
(setq pt2 (getcorner "/nBottom" pt1))
(setq pt3 (list (nth 0 pt2)(nth 1 pt1)))
(setq pt4 (list (nth 0 pt1)(nth 1 pt2)))
(command "_rectang" pt1 pt2)  ;;;; i can live without actually drawing it but at this stage of lisp i want it there


(setq ltbay1 (rtos (- (nth 0 pt3) (nth 0 pt1)) 4 1)) ;; i want to measure distance of one side of rectangle
(setq num_br (1+ (fix (/ ltbay1 (rtos 15 4 1)))))

 (print num_br)

 

 

My error is measuring a "distance" by substracting x coordinate of one point from another,i dont know about other ways of getting that distance i added rtos when i saw the problem doesnt help

I also forsee a problem for specifying rectangle in different quadrants of xy axis

but for now i do it simpliest way

 

I hope to have comments suggestions

Link to comment
Share on other sites

;1

(setq ltbay1 (rtos (- (nth 0 pt3) (nth 0 pt1)) 4 1)) ;; i want to measure distance of one side of rectangle

;2

(setq num_br (1+ (fix (/ ltbay1 (rtos 15 4 1)))))

You setq ltbay1 as a string in line 1, and string can't be use in math / in line 2 Edited by ketxu
Link to comment
Share on other sites

I'm not sure I understand why you are dividing the linear distance along one side (the X-Axis?) by 15, however, here is one way to get the area of the rectangle being drawn:

 

(defun c:XXX ( / pt1 pt2 recArea)
 (princ "\rRECTANGLE + AREA ")
 (vl-load-com)
 (if (and (setq pt1 (getpoint "\nSpecify start point: "))
   (not (initget 32))
   (setq pt2 (getpoint pt1 "\nSpecify end point: ")))
   (progn
     (entlast)
     (command "._rectang" pt1 pt2)
     (if (setq recArea (vla-get-area (vlax-ename->vla-object (entlast)))) 
       (prompt (strcat "\nArea = " (rtos recArea 4 1)))))
   (cond (pt1 (prompt "\n** Invalid input: Must specify end point ** "))
       ((prompt "\n** Invalid input: Must specify start point ** "))))
 (princ))

 

Note - Untested, written on my MacBook

Link to comment
Share on other sites

I'm not sure I understand why you are dividing the linear distance along one side (the X-Axis?) by 15, .....

 

By Dividin x axis by 15 i will get the points of projection from one side of a rectangle( x-axes) to next x-axis bottom of rectangle, but i realized that

 

(setq ltbay1 (- (nth 0 pt3) (nth 0 pt1))) outputs one type of units and I need "feet" to divide by 15 ft,hence I dont know when is a good time to convert my numbers to feet...because i have a bit more math to do and i dont want to do conversions repeatedly

Link to comment
Share on other sites

By Dividin x axis by 15 i will get the points of projection from one side of a rectangle( x-axes) to next x-axis bottom of rectangle, but i realized that

 

(setq ltbay1 (- (nth 0 pt3) (nth 0 pt1))) outputs one type of units and I need "feet" to divide by 15 ft,hence I dont know when is a good time to convert my numbers to feet...because i have a bit more math to do and i dont want to do conversions repeatedly

 

No worries... if you could post the rest of the code, or clarify what additional calculations you require, perhaps myself (or someone smarter) would be able to help suggest a more efficient way of doing what you need?

Link to comment
Share on other sites

A newbie code. Hope it helps

 

(defun c:test ( / pt1 pt2 x y area)
 
(if
 (and
   (setq pt1 (getpoint "\nPick top corner  :"))
(setq pt2 (getcorner "\nPick bottom corner  :" pt1))
(command "_.rectang" pt1 pt2)
 )

(setq x (abs (- (car pt2) (car pt1))))
(setq y (abs (- (cadr pt2) (cadr pt1))))      
(setq area (/ (* x y) 12)) 

(princ (strcat "\nLenght of x is  : " (rtos x)))
(princ (strcat "\nLenght of y is  : " (rtos y)))
(princ (strcat "\nArea of rec is  : " (rtos area)))
(princ)
)
)

Link to comment
Share on other sites

Here's another way of doing it, with some comments to help explain what the code does:

 

[color=green];; This is a keyboard shortcut[/color]
(defun c:RECA () (c:RectangleArea))

[color=green];; This is the main function[/color]
(defun c:RectangleArea ( / *error* oldCmdecho pt1 pt2 x y)

[color=green]  ;; This loads the visual lisp library, and makes ActiveX available[/color]
 (vl-load-com)

[color=green]  ;; This displays the command name at the command line.[/color]
 (princ "\rRECTANGLE+AREA ")

[color=green]  ;; This is an error handler which is only in use during this function.[/color]
 (defun *error*  (msg)

[color=green]    ;; In the event of an error, this will restore the original[/color]
[color=green]    ;; system variable setting, *IF* that variable is defined.[/color]
   (and oldCmdecho (setvar 'cmdecho oldCmdecho))
   (cond ((not msg))                             [color=green]                      ; Normal exit[/color]
         ((member msg '("Function cancelled" "quit / exit abort")))[color=green]     ; <esc> or (quit)[/color]
         ((princ (strcat "\n** Error: " msg " ** ")))) [color=green]                ; Fatal error, display it[/color]
   (princ))

[color=green]  ;; This stores the current system variable value, and sets it[/color]
[color=green]  ;; to not echo the command prompts that would normally be shown.[/color]
 (and (setq oldCmdecho (getvar 'cmdecho)) (setvar 'cmdecho 0))

 (if (and (setq pt1 (getpoint "\nPick top corner  :"))

[color=#008000]           ;; This dashes the selection line from pt1[/color]
          (not (initget 32))
          (setq pt2 (getcorner pt1 "\nPick bottom corner  :"))

[color=green]           ;; vl-cmdf returns T, whereas command returns NIL.[/color]
[color=green]           ;; Using command would prevent your code from completing the[/color]
[color=green]           ;; "AND" statement.[/color]
          (vl-cmdf "_.rectang" pt1 pt2))

[color=green]    ;; Here I've combined several steps into one for simplicity.[/color]
   (princ
     (strcat
       "\nLength of x is  : "
       (rtos (setq x (abs (- (car pt2) (car pt1)))))
       "\nLenght of y is  : "
       (rtos (setq y (abs (- (cadr pt2) (cadr pt1)))))
       "\nArea of rec is  : "
       (rtos (/ (* x y) 12)))))

 [color=green];; If the code performs normally, and no errors (or Esc),[/color]
[color=green]  ;; this will restore the original system variable setting.[/color]
 (setvar 'cmdecho oldCmdecho)
 (princ))

 

Hope this helps!

Edited by BlackBox
Added (not (initget 32))
Link to comment
Share on other sites

Divide and measure doesn't allow for the offset on edges that may be needed. This is a new post for the same subject that has been extensively commented on already.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...