Jump to content

Align text (or mtext) to the center of the circles


Recommended Posts

Posted

Changing the text's basepoint to middle center is definitely the way to go, because if there's ever a change needed in the text, it will stay centered to the circle. It's also the easiest to code from this way.

 

However, if circumstances do not allow you to change the base point of the text, then it's not like it still can't be done:

 

  1. Use the textbox function to determine the boundary points of the text. Include the text style and height so the calculations are correct. (unless vla-GetBoundingBox alone can calculate the bounding of MTEXT without taking into consideration the mtext width).
  2. Use the midpoint of the coordinates returned to place it in the center of the circle.

It involves a bit of calculation, so I'll leave it to the other programmers to write it up.

  • Thanks 1
Posted

I temporarily change the base point to middle centre, then align the text and then reset to as it was - I think the code is a bit long winded but it works

  • Confused 1
Posted
1 hour ago, Steven P said:

Я временно меняю базовую точку на центральную, затем выравниваю текст, а потом возвращаюсь к исходному состоянию. Думаю, код немного длинноват, но он работает

Where's the code?

Posted

Perhaps not the cleanest code, this returns the alignment of a selected text, which you can apply to your selected texts in the codes above, 

 

(defun gettextalign ( txtset / txtset Edata ptx_old pty_old pty_new ptx_new mycons)

;;  (setq txtset (ssget '((0 . "*TEXT"))))
  (setq Edata (entget (ssname txtset 0)))
  (setq mycons 10)
  (if (/= 0 (nth 1 (cdr (assoc 11 Edata))))(setq mycons 11))

  (setq ptx_old (nth 1 (assoc mycons Edata)))
  (setq pty_old (nth 2 (assoc mycons Edata)))

  (command "_.justifytext" txtset "" "MC")
  (setq Edata (entget (ssname txtset 0)))
  (setq ptx_new (nth 1 (assoc mycons Edata)))
  (setq pty_new (nth 2 (assoc mycons Edata)))

  (if (< ptx_old ptx_new)(setq alignx "L"))
  (if (> ptx_old ptx_new)(setq alignx "R"))
  (if (= ptx_old ptx_new)(setq alignx "C"))

  (if (> pty_old pty_new)(setq aligny "T"))
  (if (< pty_old pty_new)(setq aligny "B"))
  (if (= pty_old pty_new)(setq aligny "M"))


  (setq xyalign (strcat aligny alignx))
  (command "_.justifytext" txtset "" xyalign)

  xyalign
)

 

  • Like 1
Posted

The mtext moves to the center of the circle, but when you select the text, the code returns an error.
Select the text (Text or MText): ; error: no function definition: VLA-GET-JUSTIFY
How can this be fixed?

(defun c:CentCircleTxt (/ enCircle enText objCircle objText center objName)
  (vl-load-com)  

  (setq enCircle (car (entsel "\nSelect a circle: ")))
  (if (not enCircle)
    (progn (princ "\nThe circle is not selected.") (exit))
  )
  (setq objCircle (vlax-ename->vla-object enCircle))

  (setq center (vla-get-center objCircle))

  (setq enText (car (entsel "\nSelect the text (Text or MText): ")))
  (if (not enText)
    (progn (princ "\nThe text is not selected.") (exit))
  )
  (setq objText (vlax-ename->vla-object enText))

  (setq objName (vla-get-ObjectName objText)) 

  (cond
    ((= objName "AcDbText")
     (if (/= (vla-get-Justify objText) acJustifyMiddleCenter)
       (vla-put-Justify objText acJustifyMiddleCenter)
     )

     (vla-put-AlignmentPoint objText center)
    )

    ((= objName "AcDbMText")

     (if (/= (vla-get-AttachmentPoint objText) acAttachmentPointMiddleCenter)
       (vla-put-AttachmentPoint objText acAttachmentPointMiddleCenter)
     )

     (vla-put-InsertionPoint objText center)
    )

    (t
     (princ (strcat "\nThe selected object is not supported: " objName))
    )
  )

  (vla-Update objText)

  (princ "\nThe center of the text is the same as the center of the circle.")
  (princ)
)

 

Posted (edited)

@nikon The text within circles looks like you should be using a block with an attribute rather than two separate objects.

Edited by ronjonp

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