Jump to content

Add a word in the middle of the selected rectangle


yxl030

Recommended Posts

I did it!
Everything corresponds to the terms of reference - add a word in the middle of all selected rectangles
And the genius in Paint :)

new.png.a8d126aa67e71a95e95a7d53e6d36f93.png

But seriously:

1. You need to attach a sample dwg file

2. The forum helps, and does not just do it for everyone. You must try to make the Lisp yourself and you will be corrected and helped.

  • Thanks 1
Link to comment
Share on other sites

Try this has a homework attached google text readable hint lee-mac.

 

; simple text at mid of rectang alanh AUG 2021

(defun c:wow ( / rec co-ord p1 p2 p3 and d1 d2)
(setq oldang (getvar 'aunits))
(setvar 'aunits 3)
(while (setq rec (entsel "\npick rectang Enter to exit"))
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car rec)))))
(setq p1 (nth 0 co-ord)
      p2 (nth 1 co-ord)
      p3 (nth 2 co-ord)
)
(setq d1 (distance p1 p2)
      d2 (distance p2 p3)
)
(setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5)))
(if (> d1 d2)
(setq ang (angle p1 p2))
(setq ang (angle p2 p3))
)
(command "text" mp 2.5 ang "abc")
)
(setvar 'aunits (oldang))
(princ)
)

 

  • Like 2
Link to comment
Share on other sites

13 hours ago, maratovich said:

I did it!
Everything corresponds to the terms of reference - add a word in the middle of all selected rectangles
And the genius in Paint :)

new.png.a8d126aa67e71a95e95a7d53e6d36f93.png

But seriously:

1. You need to attach a sample dwg file

2. The forum helps, and does not just do it for everyone. You must try to make the Lisp yourself and you will be corrected and helped.

I don't know LISP, and even have trouble reading it.

Link to comment
Share on other sites

4 hours ago, BIGAL said:

Try this has a homework attached google text readable hint lee-mac.

 


; simple text at mid of rectang alanh AUG 2021

(defun c:wow ( / rec co-ord p1 p2 p3 and d1 d2)
(setq oldang (getvar 'aunits))
(setvar 'aunits 3)
(while (setq rec (entsel "\npick rectang Enter to exit"))
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car rec)))))
(setq p1 (nth 0 co-ord)
      p2 (nth 1 co-ord)
      p3 (nth 2 co-ord)
)
(setq d1 (distance p1 p2)
      d2 (distance p2 p3)
)
(setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5)))
(if (> d1 d2)
(setq ang (angle p1 p2))
(setq ang (angle p2 p3))
)
(command "text" mp 2.5 ang "abc")
)
(setvar 'aunits (oldang))
(princ)
)

 

Thank you BIGAL!That's what I want,Thanks.

But, how can I modify the code to select more than one rectangle at a time?

Link to comment
Share on other sites

Your home work look up using (setq ss (SSGET then a (Repeat (sslength ss) its very simple change to code. Just look at some of the code supplied here.

Edited by BIGAL
Link to comment
Share on other sites

I did my homework all afternoon and still failed.

CAD says:

Error: Parameter type error: consp #<VLA-OBJECT IAcadLWPolyline 000000003c3d77b8>

;https://www.cadtutor.net/forum/topic/73614-add-a-word-in-the-middle-of-the-selected-rectangle/
; simple text at mid of rectang alanh AUG 2021

(defun C:wow(/ rec co-ord p1 p2 p3 and d1 d2)
(vl-load-com)
(setq oldang (getvar 'aunits))
(setvar 'aunits 3)
(setq i 0)
(setq ent (ssget '((0 . "LWPOLYLINE") (90 . 4) (-4 . "&=") (70 . 1))))
(if ent
    (repeat (sslength ent)
    (setq rec (vlax-ename->vla-object (ssname ent i)))
;(while (setq rec (entsel "\npick rectang Enter to exit"))
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car rec)))))
(setq p1 (nth 0 co-ord)
      p2 (nth 1 co-ord)
      p3 (nth 2 co-ord)
)
(setq d1 (distance p1 p2)
      d2 (distance p2 p3)
)
(setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5)))
(if (> d1 d2)
(setq ang (angle p1 p2))
(setq ang (angle p2 p3))
)
(command "text" "J" "MC" mp 250 ang "ABC")
(setq i i+1)
)
)
(setvar 'aunits (oldang))
(princ)
)

 

Link to comment
Share on other sites

Nice try! You were close :)

image.thumb.png.ea2040fceece0d8d1cf65d27874a4fd9.png

(defun c:wow (/ ang co-ord d1 d2 ent i mp oldang p1 p2 p3 rec)
  (vl-load-com)
  (setq oldang (getvar 'aunits))
  (setvar 'aunits 3)
  (setq i 0)
  (setq ent (ssget '((0 . "LWPOLYLINE") (90 . 4) (-4 . "&=") (70 . 1))))
  (if ent
    (repeat (sslength ent)
      ;; (setq rec (vlax-ename->vla-object (ssname ent i)))
      (setq rec (ssname ent i))
      ;; (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car rec)))))
      (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget rec))))
      (setq p1 (nth 0 co-ord)
	    p2 (nth 1 co-ord)
	    p3 (nth 2 co-ord)
      )
      (setq d1 (distance p1 p2)
	    d2 (distance p2 p3)
      )
      (setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5)))
      (if (> d1 d2)
	(setq ang (angle p1 p2))
	(setq ang (angle p2 p3))
      )
      (command "text" "J" "MC" mp 250 ang "ABC")
      ;; (setq i i+1)
      (setq i (1+ i))
    )
  )
  ;; (setvar 'aunits (oldang))
  (setvar 'aunits oldang)
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

Thanks Ronjonp

 

 yxl030 good to see, you will get lots more help often behind the scenes if you have a go yourself and your learning. Rename "wow" to what you want.

 

Add (c:wow) as last line then when you load etc it will run straight away.

Edited by BIGAL
Link to comment
Share on other sites

Thank you for your patient guidance , Ronjonp !

It works very well now.

 

Thanks BIGAL, your code saved me a lot of time. 

You are right,Spent some time, I learned a lot .

 

Link to comment
Share on other sites

Glad to help I do my repeats slightly different, note this processes in opposite order starting at last. Its just preference both ways work.

 

(repeat (setq i (sslength ent))
(setq rec (vlax-ename->vla-object (ssname ent  (setq i (1- i)))))
...
; no need for i + 1
) ; repeat

 

  • Like 1
Link to comment
Share on other sites

On 8/25/2021 at 7:29 AM, BIGAL said:

Try this has a homework attached google text readable hint lee-mac.

 


; simple text at mid of rectang alanh AUG 2021

(defun c:wow ( / rec co-ord p1 p2 p3 and d1 d2)
(setq oldang (getvar 'aunits))
(setvar 'aunits 3)
(while (setq rec (entsel "\npick rectang Enter to exit"))
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car rec)))))
(setq p1 (nth 0 co-ord)
      p2 (nth 1 co-ord)
      p3 (nth 2 co-ord)
)
(setq d1 (distance p1 p2)
      d2 (distance p2 p3)
)
(setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5)))
(if (> d1 d2)
(setq ang (angle p1 p2))
(setq ang (angle p2 p3))
)
(command "text" mp 2.5 ang "abc")
)
(setvar 'aunits (oldang))
(princ)
)

 

Great routine, BIGAL. May I ask you to modify the code so that the text is rotated not according to the longest border of the rectangle, but according to the border picked by (entsel) method? Like the picture below.

735884459_textinrectangle.thumb.jpg.8d088e2e5640c841ea5093bbe4e0617e.jpg

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