Jump to content

Lisp to put text (or Mtext) at geometric center of multiple areas


Recommended Posts

Posted

Hello everyone, 

 

I have text that i need to put at geo center of multiple polylines, so i want to select the polylines and the text and then text to be put at the geo center of the polylines.

Anyone have lisp for that ?

 

Thank you

Posted (edited)

This will ask you to select a text but it could be anything really 

find the midpoint of the bounding box around selected item

ask for polylines to be selected

make a copy the first selected item to the geo center of the polylines

 

;;----------------------------------------------------------------------------;;
;; Copy text to the geo center of polyline(s)
(defun C:foo (/ txt BP SS poly PT newtxt)
  (vl-load-com)
  (setq txt (vlax-ename->vla-object (car (entsel "\nSelect Text to Move"))))
  (vla-getboundingbox txt 'minpt 'maxpt)
  (setq BP (mapcar '/ (mapcar '+ (vlax-safearray->list minpt) (vlax-safearray->list maxpt)) '(2 2 2))) ;midpoint of text
  (prompt "\nSelect Polyline")
  (if (setq SS (ssget '((0 . "*POLYLINE"))))
    (foreach poly (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq PT (osnap (vlax-curve-getStartPoint poly) "gcen"))
      (setq newtxt (vla-copy txt))
      (vla-move newtxt (vlax-3d-point BP) (vlax-3d-point pt)) 
    )
  )
  (princ)
)

 

Edited by mhupp
added (vl-load-com)
  • Like 2
Posted
49 minutes ago, mhupp said:

This will ask you to select a text but it could be anything really 

find the midpoint of the bounding box around selected item

ask for polylines to be selected

make a copy the first selected item to the geo center of the polylines

 

;;----------------------------------------------------------------------------;;
;; Copy text to the geo center of polyline(s)
(defun C:foo (/ txt BP SS poly PT newtxt)
  (setq txt (vlax-ename->vla-object (car (entsel "\nSelect Text to Move"))))
  (vla-getboundingbox txt 'minpt 'maxpt)
  (setq BP (mapcar '/ (mapcar '+ (vlax-safearray->list minpt) (vlax-safearray->list maxpt)) '(2 2 2))) ;midpoint of text
  (prompt "\nSelect Polyline")
  (if (setq SS (ssget '((0 . "*POLYLINE"))))
    (foreach poly (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
      (setq PT (osnap (vlax-curve-getStartPoint poly) "gcen"))
      (setq newtxt (vla-copy txt))
      (vla-move newtxt BP pt)
    )
  )
  (princ)
)

 

Thank you for your reply,

i don't know but it doesn't seem to work well.

you can try it on the attached file

 

new block.dwg

Posted (edited)

The center of the bounding box is not the same as the centroid.

 

Perhaps the program could use the selected polylines to create regions that were then unioned. Massprop could then be used to determine the centroid for the collection of regions.  But how do you access the results of massprop in vlisp?

 

Edited by lrm
  • Like 1
Posted
14 minutes ago, mhupp said:

Maybe its because I didn't have (vl-load-com) its there now.

but everything is working on this end.

https://ibb.co/j8HPK1x

 

@lrm bounding box is for the text only.

it gives me this error when i try it

image.png.0647362e6efce54757fb77fc8ecaef8e.png

Posted
13 minutes ago, asdfgh said:

it gives me this error when i try it

image.png.0647362e6efce54757fb77fc8ecaef8e.png

Try it with vlax-invoke or convert the coordinates to variant as the error message indicates to.

(vla-move newtxt (vlax-3d-point BP) (vlax-3d-point pt)) ;; replaced with (vla-move newtxt BP pt)

 

  • Like 2
Posted
4 minutes ago, Tharwat said:

Try it with vlax-invoke or convert the coordinates to variant as the error message indicates to.

(vla-move newtxt (vlax-3d-point BP) (vlax-3d-point pt)) ;; replaced with (vla-move newtxt BP pt)

 

That worked so well.

Thank you so much

  • Like 1
Posted
3 minutes ago, asdfgh said:

That worked so well.

Thank you so much

All credits to @mhupp :) 

Happy coding.

Posted (edited)

Gcen is a snap that can be used to do just that. Must be say closed pline to work.

 

(setq pt (osnap (vlax-curve-getStartPoint (vlax-ename->vla-object (car  (entsel "Pick obj")))) "gcen"))

 

Edited by BIGAL
  • Like 1

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