Author Topic: In Search of 'Align Objects'  (Read 14355 times)

0 Members and 1 Guest are viewing this topic.

M-dub

  • Guest
In Search of 'Align Objects'
« on: August 09, 2007, 12:19:20 PM »
It wouldn't surprise me if it already exists somewhere, but I haven't found it yet.
Does anyone know of a routine that will allow you to select a number of different objects, whether they're text, lines, blocks, etc. and align them somehow?  I've got a couple for aligning text to objects or points, but I need something that will align all objects selected.
A picture is worth 100 words...


T.Willey

  • Needs a day job
  • Posts: 5251
Re: In Search of 'Align Objects'
« Reply #1 on: August 09, 2007, 12:54:11 PM »
The simple way would be to get the bounding box of each object, and then move it from that point to the side specified by the user.  It wouldn't really line them up though (unless they were spaced like that to begin with).
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: In Search of 'Align Objects'
« Reply #2 on: August 09, 2007, 01:46:34 PM »
See if this works for you.  Not tested with different ucs, so you can test that, but should work there also.
Code: [Select]
(defun c:AlignObjects (/ ActDoc Sel AliObj AliLl AliUr ss AliOpt Ent tempObj tempLl tempUr)

(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(vla-EndUndoMark ActDoc)
(vla-StartUndoMark ActDoc)
(if
(and
(setq Sel (entsel "\n Select base object (for aligning): "))
(not (redraw (car Sel) 3))
(setq AliObj (vlax-ename->vla-object (car Sel)))
(not (vla-GetBoundingBox AliObj 'AliLl 'AliUr))
(setq AliLl (safearray-value AliLl))
(setq AliUr (safearray-value AliUr))
(setq ss (ssget))
(not (initget "Left Right Center Top Bottom"))
(setq AliOpt
(if (setq AliOpt (getkword "\n Alignment option [Center / Left / Right / Top / Bottom]: <Center> "))
AliOpt
"Center"
)
)
(setq cnt -1)
)
(while (setq Ent (ssname ss (setq cnt (1+ cnt))))
(setq tempObj (vlax-ename->vla-object Ent))
(vla-GetBoundingBox tempObj 'tempLl 'tempUr)
(setq tempLl (safearray-value tempLl))
(setq tempUr (safearray-value tempUr))
(cond
((= AliOpt "Center")
(vlax-invoke
tempObj
'Move
(mapcar '(lambda (a b) (/ (+ a b) 2.0)) tempLl tempUr)
(mapcar '(lambda (a b) (/ (+ a b) 2.0)) AliLl AliUr)
)
)
((= AliOpt "Left")
(vlax-invoke
tempObj
'Move
tempLl
(list
(car AliLl)
(cadr tempLl)
(caddr tempLl)
)
)
)
((= AliOpt "Right")
(vlax-invoke
tempObj
'Move
tempUr
(list
(car AliUr)
(cadr tempUr)
(caddr tempUr)
)
)
)
((= AliOpt "Top")
(vlax-invoke
tempObj
'Move
tempUr
(list
(car tempUr)
(cadr AliUr)
(caddr tempUr)
)
)
)
((= AliOpt "Bottom")
(vlax-invoke
tempObj
'Move
tempLl
(list
(car tempLl)
(cadr AliLl)
(caddr tempLl)
)
)
)
)
)
)
(if Sel (redraw (car Sel) 4))
(vla-EndUndoMark ActDoc)
(princ)
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

M-dub

  • Guest
Re: In Search of 'Align Objects'
« Reply #3 on: August 09, 2007, 02:01:04 PM »
That, my friend, is Sweet!

:-D

Thank-you!

LE

  • Guest
Re: In Search of 'Align Objects'
« Reply #4 on: August 09, 2007, 02:28:42 PM »
That, my friend, is Sweet!

:-D

Thank-you!

See.... how easy, it is - the life with lisp.... now, you can dedicate more into here or to play with your daughter.... :)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: In Search of 'Align Objects'
« Reply #5 on: August 09, 2007, 02:46:49 PM »
That, my friend, is Sweet!

:-D

Thank-you!
Glad you like.  You're welcome.

See.... how easy, it is - the life with lisp.... now, you can dedicate more into here or to play with your daughter.... :)
^ Second.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

M-dub

  • Guest
Re: In Search of 'Align Objects'
« Reply #6 on: August 09, 2007, 02:51:16 PM »
I know, I know...

I know that if I were to figure out the language and how it works, I'd be good at it.  I really have no excuse as to why I haven't learned it yet... other than a combination of procrastination, intimidation, laziness and a honey-do list that won't quit!

I think about it every time I post a request in here.  It's embarassing, but ... I dunno...

Thank-you all again.

qjchen

  • Bull Frog
  • Posts: 285
  • Best wishes to all
Re: In Search of 'Align Objects'
« Reply #7 on: August 09, 2007, 09:06:20 PM »
:)

Tony Hotchkiss has ever write such code and write an article to explain it, althought the code has not include "Middle", but maybe you can add it easily.

http://management.cadalyst.com/cadman/article/articleDetail.jsp?id=282016
http://qjchen.mjtd.com
My blog http://chenqj.blogspot.com (Chinese, can be translate into English)

M-dub

  • Guest
Re: In Search of 'Align Objects'
« Reply #8 on: August 13, 2007, 08:35:36 AM »
Ok, is it just me and the system I'm using?  For some reason, I'm getting this error every time I try to use Tim's AlignObjects routine.

Command: AO ; error: no function definition: VLAX-GET-ACAD-OBJECT

ronjonp

  • Needs a day job
  • Posts: 7527
Re: In Search of 'Align Objects'
« Reply #9 on: August 13, 2007, 09:21:27 AM »
You have to load (vl-load-com).

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

M-dub

  • Guest
Re: In Search of 'Align Objects'
« Reply #10 on: August 13, 2007, 09:29:16 AM »
:oops:

Many thanks, Ron.  I guess it was loaded through another routine I had already loaded last week.
Updated the code and it seems to work.  :)

Thanks again!
Code: [Select]
(defun c:AO (/ ActDoc Sel AliObj AliLl AliUr ss AliOpt Ent tempObj tempLl tempUr)

(vl-load-com)
(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(vla-EndUndoMark ActDoc)
(vla-StartUndoMark ActDoc)
(if
(and
(setq Sel (entsel "\n Select base object (for aligning): "))
(not (redraw (car Sel) 3))
(setq AliObj (vlax-ename->vla-object (car Sel)))
(not (vla-GetBoundingBox AliObj 'AliLl 'AliUr))
(setq AliLl (safearray-value AliLl))
(setq AliUr (safearray-value AliUr))
(setq ss (ssget))
(not (initget "Left Right Center Top Bottom"))
(setq AliOpt
(if (setq AliOpt (getkword "\n Alignment option [Center / Left / Right / Top / Bottom]: <Center> "))
AliOpt
"Center"
)
)
(setq cnt -1)
)
(while (setq Ent (ssname ss (setq cnt (1+ cnt))))
(setq tempObj (vlax-ename->vla-object Ent))
(vla-GetBoundingBox tempObj 'tempLl 'tempUr)
(setq tempLl (safearray-value tempLl))
(setq tempUr (safearray-value tempUr))
(cond
((= AliOpt "Center")
(vlax-invoke
tempObj
'Move
(mapcar '(lambda (a b) (/ (+ a b) 2.0)) tempLl tempUr)
(mapcar '(lambda (a b) (/ (+ a b) 2.0)) AliLl AliUr)
)
)
((= AliOpt "Left")
(vlax-invoke
tempObj
'Move
tempLl
(list
(car AliLl)
(cadr tempLl)
(caddr tempLl)
)
)
)
((= AliOpt "Right")
(vlax-invoke
tempObj
'Move
tempUr
(list
(car AliUr)
(cadr tempUr)
(caddr tempUr)
)
)
)
((= AliOpt "Top")
(vlax-invoke
tempObj
'Move
tempUr
(list
(car tempUr)
(cadr AliUr)
(caddr tempUr)
)
)
)
((= AliOpt "Bottom")
(vlax-invoke
tempObj
'Move
tempLl
(list
(car tempLl)
(cadr AliLl)
(caddr tempLl)
)
)
)
)
)
)
(if Sel (redraw (car Sel) 4))
(vla-EndUndoMark ActDoc)
(princ)
)
(prompt "\nEnter AO to start.")

T.Willey

  • Needs a day job
  • Posts: 5251
Re: In Search of 'Align Objects'
« Reply #11 on: August 13, 2007, 11:06:11 AM »
Thanks Ron.  I always forget to add that, as mine is called in my mnl file.  Glad you got it working Mike, but just an FYI... I don't think it will work with non-world ucs.  Can be fixed, but as of now I'm pretty sure it won't work.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

M-dub

  • Guest
Re: In Search of 'Align Objects'
« Reply #12 on: August 13, 2007, 11:23:32 AM »
Thanks Ron.  I always forget to add that, as mine is called in my mnl file.  Glad you got it working Mike, but just an FYI... I don't think it will work with non-world ucs.  Can be fixed, but as of now I'm pretty sure it won't work.
Thanks Tim,

On another note, I'm playing with this one to try to modify the 'Center' option.  We'll see if I'm able to do it without scratching my head to the bone.  :)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: In Search of 'Align Objects'
« Reply #13 on: August 13, 2007, 11:56:08 AM »
Thanks Ron.  I always forget to add that, as mine is called in my mnl file.  Glad you got it working Mike, but just an FYI... I don't think it will work with non-world ucs.  Can be fixed, but as of now I'm pretty sure it won't work.
Thanks Tim,
You're welcome.

On another note, I'm playing with this one to try to modify the 'Center' option.  We'll see if I'm able to do it without scratching my head to the bone.  :)
Whats wrong with this option?  You want it to center from all the objects selected? instead of selection one, and moving everything to it's center?  If so, should be too hard, but if you want to figure it out I will let you.  :wink:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

M-dub

  • Guest
Re: In Search of 'Align Objects'
« Reply #14 on: August 13, 2007, 12:16:57 PM »
I would like to try it myself, but what I'm aiming for is actually going to be two more options.
The current 'center' option, I would call 'Middle' and I would add the option to center everything on either the X or the Y axis, so it would be something like (CH) Center Horizontal or (CV) Center Vertical.

*running off to play with it now, over my lunch*