Jump to content

Recommended Posts

Posted
15 hours ago, mhupp said:

 

added a "*" to ssget to pick up mtext or text. updated the foreach to pull the vla-objects name from the selection set.

 

;;----------------------------------------------------------------------------;;
;; Modify Text or Blocks to align Horozontal or Vertical
;; https://www.cadtutor.net/forum/topic/99091-i-need-a-lisp-to-align-blocks-and-texts-vertically/
(defun C:ATB () (C:AlignTextBlock))
(defun C:AlignTextBlock (/ vars vals pt1 pt2 vector mode ent ed pt newpt)
  (vl-load-com)
  (setq vars '(OSMODE ORTHOMODE)
        vals (mapcar 'getvar vars)
  )
  (mapcar 'setvar vars '(0 1))
  (setq pt1 (getpoint "\nAlignment Point: "))
  (setq pt2 (getpoint pt1 "\nSelect Horozontal or Vertical:"))
  (setq vector (mapcar '- pt2 pt1))
  (if (eq (car Vector) 0.0) (setq mode 'V) (setq mode 'H))
  (while (setq ss (ssget '((0 . "*TEXT,INSERT"))))
    (foreach obj (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS))))
      (setq pt (vlax-safearray->list (vlax-variant-value (vla-get-InsertionPoint obj)))
            newpt (if (eq mode 'V) (list (car pt1) (cadr pt) (caddr pt)) (list (car pt) (cadr pt1) (caddr pt)))
      )
      (vla-Move obj (vlax-3d-point pt) (vlax-3d-point newpt))
    )
  )
  (mapcar 'setvar vars vals)
  (princ)
)
(princ "\nAlignTextBlock Lisp Loaded")
(princ "\nType ATB or AlignTextBlock to run command")

 

@mhuppthank you very much

Posted

Why not post a before and after drawing?

 

Show an example of what you have and then an example of what you want.

Posted (edited)
3 hours ago, SLW210 said:

Why not post a before and after drawing?

 

Show an example of what you have and then an example of what you want.

@SLW210Thank you for looking! If I choose axis 2 as the reference, then axis 2 does not move and axis 1 moves and aligns with axis 2.

pic1.jpg

Edited by sd2006
Posted

@mhupp's LISP works for me, but I took a stab at it anyway.

 

I just tweaked the original abc.lsp, I would prefer if you would post a link to the original so I can properly credit the author.

 

For more information, Kent Cooper has quite a few LISPs for aligning blocks for certain and probably (M)Text, etc. (as well as sorting spacing etc.) on the Autodesk Forums, they should be easy to locate.

 

This worked on your provided drawing as well as one I made for test with Polylines, Lines, Blocks, Mtext, Text and Attributes.

 

;;; Align selected objects in X or Y direction with reference object.                                    |
;;;                                                                                                      |
;;; https://www.cadtutor.net/forum/topic/99091-i-need-a-lisp-to-align-blocks-and-texts-vertically/       |
;;;                                                                                                      |
;;; Modified from the provided abc.lsp (author unknown) by SLW210 (a.k.a. Steve Wilson)                  |
;;;                                                                                                      |
;;; Was horizontal only, added vertical align option, error and undo.                                    |
;;;                                                                                                      |
;;; *****************************************************************************************************|

(defun c:AlignXY (/	  *error* OS	  mode	  ss	  albl
		  alpt	  alptx	  alpty	  ctr	  ename	  inpt
		  inptx	  inpty	  newpt	  olderr
		 )

  ;; Error handler 
  (setq olderr *error*)
  (defun *error* (msg)
    (if	(/= msg "Function cancelled")
      (princ (strcat "\nError: " msg))
    )
    (if	OS
      (setvar "OSMODE" OS)
    )
    (command "_.UNDO" "_End")
    (setq *error* olderr)
    (princ)
  )

  ;; Save and set system vars
  (setq OS (getvar "OSMODE"))
  (setvar "OSMODE" 0)

  ;; Start UNDO group
  (command "_.UNDO" "_Begin")

  ;; Ask user for alignment direction
  (initget "Horizontal Vertical")
  (setq mode (getkword "\nAlign [Horizontal/Vertical] <Horizontal>: "))
  (if (null mode)
    (setq mode "Horizontal")
  )

  ;; Select objects
  (princ "\nSelect blocks or text to align evenly: ")
  (if (not (setq ss (ssget)))
    (progn
      (princ "\nNothing selected.")
      (*error* "Function cancelled")
      (exit)
    )
  )

  ;; Select reference object
  (if (not
	(setq albl (entsel "\nSelect reference text or block: "))
      )
    (progn
      (princ "\nNo reference selected.")
      (*error* "Function cancelled")
      (exit)
    )
  )

  (setq albl (car albl))
  (setq alpt (cdr (assoc 10 (entget albl))))

  (if (not alpt)
    (progn
      (princ "\nInvalid reference object.")
      (*error* "Function cancelled")
      (exit)
    )
  )

  (setq alptx (car alpt))
  (setq alpty (cadr alpt))

  ;; Loop through selection
  (setq ctr 0)
  (while (setq ename (ssname ss ctr))
    (setq inpt (cdr (assoc 10 (entget ename))))

    (if	inpt
      (progn
	(setq inptx (car inpt))
	(setq inpty (cadr inpt))

	;; Decide new point
	(cond
	  ((= mode "Horizontal")
	   (setq newpt (list inptx alpty))
	  )
	  ((= mode "Vertical")
	   (setq newpt (list alptx inpty))
	  )
	)

	(command "move" ename "" inpt newpt)
      )
    )

    (setq ctr (+ ctr 1))
  )

  ;; End UNDO group cleanly
  (command "_.UNDO" "_End")

  ;; Restore vars and error handler
  (setvar "OSMODE" OS)
  (setq *error* olderr)

  (prompt "\nAlignment complete.")
  (princ)
)

 

 

  • Like 1
Posted (edited)

This is a LISP program that displays text and blocks vertically. I've modified it according to your instructions, and it works, but not exactly as I expected.

My goal is that when I select an object to align, other objects will align with the selected object (the object selected for alignment will not move).

Thank you very much for your help.

(defun c:abc ()

  (setq OS (getvar "OSMODE"))
  (setvar "OSMODE" 0)

(princ "\nSelect blocks or text to align vertical evenly: ")
(setq ss (ssget))

  (setq albl (entsel "\nSelect text or block to align with: "))
  (setq albl (car albl))
  (setq alpt (cdr (assoc 10 (entget albl))))
  (setq alptx (cadr alpt))
  (setq ctr 0)

    (while
      (setq ename (ssname ss ctr))
      (setq inpt (cdr (assoc 10 (entget ename))))
      (setq inptx (car inpt) )
      (setq inpty (cadr inpt))
      (setq newpt (list alptx inpty))
      (command "move" ename "" inpt newpt)
      (setq ctr (+ ctr 1))
    )

  (setvar "OSMODE" OS)
)

 

Edited by SLW210
Added Code Tags!!!
Posted

You have a topic on this lisp already. please take the time to read it.

Bạn đã có một chủ đề về Lisp này rồi. Xin hãy dành chút thời gian để đọc nó.

 

 

  • Agree 1
Posted

@mhuppThank you! I've looked at it, and honestly, I don't understand anything about Lisp. I only know how to use it! I hope you can help me.

Posted

Copy what @SLW210 posted in the other thread into a text file save it as .lsp and load that instead of abc.lsp.

 

The command to type to run SLW210's Lisp is "AlignXY"

 

  • Agree 1
Posted

I have already told you to use a more descriptive thread title and to use Code Tags for your Code. (<> in the editor toolbar).

 

Not to mention you have started a new thread for a topic you already have currently.

 

I will merge this thread with the other one.

  • SLW210 changed the title to I need a LISP to align blocks and texts vertically
Posted

If you need the LISP to do something else please post a .dwg with what you have and then a what you want it to be example.

 

These LISPs are doing what you have so far indicating as to what you need.

Posted

Still starting from the mhupp code, I think this corresponds to your request: align all the blocks to the position of a block. Same for text or mtext.

(defun C:ABC ( / vars vals ss ssref pt_ref pt2 vector mode ent ed pt newpt)
  (vl-load-com)
  (setq vars '(OSMODE ORTHOMODE)
        vals (mapcar 'getvar vars)
  )
  (mapcar 'setvar vars '(0 1))
  (princ "\nSelect Block or Texte.")
  (while (null (setq ss (ssget '((0 . "*TEXT,INSERT"))))))
  (princ "\nSelect ONE texte or block to align selection")
  (while (null (setq ssref (ssget "_+.:E:S" '((0 . "*TEXT,INSERT"))))))
  (setq pt_ref (cdr (assoc 10 (entget (ssname ssref 0)))))
  (setq pt2 (getpoint pt_ref "\nSelect Horozontal or Vertical:"))
  (setq vector (mapcar '- pt2 pt_ref))
  (if (eq (car Vector) 0.0) (setq mode 'V) (setq mode 'H))
  (foreach ent (vl-remove-if 'listp (mapcar 'cadr (ssnamex SS)))
    (setq ed (entget ent)
      pt (cdr (assoc 10 ed))
      newpt (if (eq mode 'V) (list (car pt_ref) (cadr pt) (caddr pt)) (list (car pt) (cadr pt_ref) (caddr pt)))
    )
    (vla-Move (vlax-ename->vla-object ent) (vlax-3d-point pt) (vlax-3d-point newpt))
  )
  (mapcar 'setvar vars vals)
  (princ)
)

 

  • Like 2
Posted

@Tsuky works as well, not sure what @sd2006 is struggling with.

 

The one I posted works the same as the OP's in first post for horizontal align, I just added the vertical align and some error checking.

 

Home today, so also tested in AutoCAD 2000i.

  • Agree 1
Posted

Here is another option from @CAB at the swamp. 

 

Here is another from @Jonathan Handojo.

 

If I get back to drawing clean ups and need it, I will probably have the program Align Plus from JTB World reinstalled if not a big hassle with IT.

 

You should have stated long ago you needed help using the LISPs.

 

How to Run an AutoLISP Program | Lee Mac Programming

 

Here is a short video if you prefer that method.

 

AutoCAD LISP Files/Command Explained | How to Use .LISP Commands for Beginners

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