Jump to content

Please help! I need it urgently. Deadline is coming soon. thank you so much .


Recommended Posts

Posted (edited)

Due to work requirements, I have to measure the area and write it on the drawing. I use the lisp below but the result is not as expected. I want the result like this; 5000x5000 = 25m2, currently the lisp gives the result 25000000

I would like to ask you to help me fix the result like this. The room area is 5000mm x5000 = 25m2

Thanks for your help

(defun rh:dxf (code lst) (cdr (assoc code lst)))

(defun c:plarea ( / cmde ent e_typ e_lst area vtx x_lst y_lst z_lst x_pt y_pt z_pt c_lst v_lst)

  (cond ( (/= 0 (getvar 'cmdecho)) (setq cmde (getvar 'cmdecho))  (setvar 'cmdecho 0)))

  (while (setq ss (ssget "_+.:E:S" '((0 . "POLYLINE,LWPOLYLINE") (-4 . "<OR") (70 . 1) (70 . 3) (70 . 5)(-4 . "OR>"))))
    (setq ent (ssname ss 0)
          e_typ (rh:dxf 0 (setq e_lst (entget ent)))
          area (getpropertyvalue ent "area")
          v_lst nil
    );end_setq

    (cond ( (= e_typ "POLYLINE")
            (setq ent (entnext ent)
                  vtx (rh:dxf 10 (entget ent))
            );end_setq
            (if (< (length vtx) 3) (setq vtx (reverse (cons 0.0 (reverse vtx)))))

            (while (/= "SEQEND" (cdr (assoc 0 (entget ent))))
              (setq v_lst (cons vtx v_lst)
                    ent (entnext ent)
                    vtx (rh:dxf 10 (entget ent))
              );end_setq
              (if (< (length vtx) 3) (setq vtx (reverse (cons 0.0 (reverse vtx)))))
            );end_while

            (setq x_pt (/ (apply '+ (mapcar '(lambda (x) (car x)) v_lst)) (length v_lst))
                  y_pt (/ (apply '+ (mapcar '(lambda (x) (cadr x)) v_lst)) (length v_lst))
            );end_setq

            (if (= (setq sum (apply '+ (mapcar '(lambda (x) (caddr x)) v_lst))) 0.0) (setq z_pt 0.0) (setq z_pt (/ sum (length v_lst))))
          )
          ( (= e_typ "LWPOLYLINE")
            (setq z_pt (rh:dxf 38 e_lst))
            (foreach pr e_lst
              (if (= (car pr) 10) (setq v_lst (cons (cdr pr) v_lst)))
            );end_foreach
            (setq x_pt (/ (apply '+ (mapcar '(lambda (x) (car x)) v_lst)) (length v_lst))
                  y_pt (/ (apply '+ (mapcar '(lambda (x) (cadr x)) v_lst)) (length v_lst))
            );end_setq
          )
    );end_cond
    (setq c_lst (list x_pt y_pt z_pt))

    (entmakex (list (cons 0 "MTEXT")
                    (cons 100 "AcDbEntity")
                    (cons 100 "AcDbMText")
                    (cons 10  c_lst)
                    (cons 40 (getvar 'textsize))
                    (cons 71 5)
                    (cons 72 5)
                    (cons 1 (rtos area 2 3))
              );end_list
    );end_entmakex
  );end_while
  (if cmde (setvar 'cmdecho cmde))
);end_defun

 

Edited by SLW210
Added Code Tags!!
Posted
  (cons 1 (rtos (/ area 1000000) 2 3))

Replace the above with the following.

  (cons 1 (rtos area 2 3))

 

Posted
1 hour ago, CAD2005 said:

Due to work requirements, I have to measure the area and write it on the drawing. I use the lisp below but the result is not as expected. I want the result like this; 5000x5000 = 25m2, currently the lisp gives the result 25000000

I would like to ask you to help me fix the result like this. The room area is 5000mm x5000 = 25m2

(cons 1 (strcat (rtos (/ area 1000000.0) 2 3) "m2"))
If you need to change the number of decimal places, change the last argument to 
(rtos ... 2 3) to the desired number (for example, 2 for two digits).
(cons 1 (strcat (rtos (/ area 1000000.0) 2 2) "m2"))
If you don't need the suffix "m2", (cons 1 (rtos (/ area 1000000.0) 2 3))

(defun rh:dxf (code lst) (cdr (assoc code lst)))

(defun c:plaream2 ( / cmde ent e_typ e_lst area vtx x_lst y_lst z_lst x_pt y_pt z_pt c_lst v_lst ss sum)
  (cond
    ( (/= 0 (getvar 'cmdecho))
      (setq cmde (getvar 'cmdecho))
      (setvar 'cmdecho 0)
    )
  )
  (while
    (setq ss (ssget "_+.:E:S"
      '((0 . "POLYLINE,LWPOLYLINE")
        (-4 . "<OR")
        (70 . 1) (70 . 3) (70 . 5)
        (-4 . "OR>")
      )
    ))
    (setq ent (ssname ss 0)
          e_typ (rh:dxf 0 (setq e_lst (entget ent)))
          area (getpropertyvalue ent "area")
          v_lst nil
    )
    (cond
      ( (= e_typ "POLYLINE")
        (setq ent (entnext ent)
              vtx (rh:dxf 10 (entget ent))
        )
        (if (< (length vtx) 3) (setq vtx (reverse (cons 0.0 (reverse vtx)))))
        (while (/= "SEQEND" (cdr (assoc 0 (entget ent))))
          (setq v_lst (cons vtx v_lst)
                ent (entnext ent)
                vtx (rh:dxf 10 (entget ent))
          )
          (if (< (length vtx) 3) (setq vtx (reverse (cons 0.0 (reverse vtx)))))
        )
        (setq x_pt (/ (apply '+ (mapcar '(lambda (x) (car x)) v_lst)) (length v_lst))
              y_pt (/ (apply '+ (mapcar '(lambda (x) (cadr x)) v_lst)) (length v_lst))
        )
        (if (= (setq sum (apply '+ (mapcar '(lambda (x) (caddr x)) v_lst))) 0.0)
          (setq z_pt 0.0)
          (setq z_pt (/ sum (length v_lst)))
        )
      )
      ( (= e_typ "LWPOLYLINE")
        (setq z_pt (rh:dxf 38 e_lst))
        (foreach pr e_lst
          (if (= (car pr) 10) (setq v_lst (cons (cdr pr) v_lst)))
        )
        (setq x_pt (/ (apply '+ (mapcar '(lambda (x) (car x)) v_lst)) (length v_lst))
              y_pt (/ (apply '+ (mapcar '(lambda (x) (cadr x)) v_lst)) (length v_lst))
        )
      )
    )
    (setq c_lst (list x_pt y_pt z_pt))
    (entmakex
      (list
        (cons 0 "MTEXT")
        (cons 100 "AcDbEntity")
        (cons 100 "AcDbMText")
        (cons 10  c_lst)
        (cons 40 (getvar 'textsize))
        (cons 71 5)
        (cons 72 5)
        (cons 1 (strcat (rtos (/ area 1000000.0) 2 3) "m2"))
; (cons 1 (rtos (/ area 1000000.0) 2 3)) ; If you don't need the suffix "m2"
      )
    )
  )
  (if cmde (setvar 'cmdecho cmde))
)

 

Posted

Please use Code Tags for your code (<> in the editor toolbar).

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...