+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    37

    Default Align Block and Attributes to X Y or Z

    Registered forum members do not see this ad.

    I usually do extensive research before posting a question to this forum, but I have not been able to find the answer I'm looking for.

    I'm trying to create a lisp that asks you for a plane
    Code:
    (getkword "X Y or Z <Y>:")
    Then pick a point
    Code:
    (getpoint "\nSelect Point)
    Pick blocks
    Code:
    (setq blks (ssget '((0 . "INSERT"))))
    Finally aligns all blocks to the selected plane.

    I have a Lisp that does this, except that when the blocks have attributes, the attributes stay in place and I would have to manually move them.

    I know that a way to do it is to select all blocks i want and go to the properties and change either the X Y or Z values, but that also seems tedious.

    Any help is greatly appreciated.

    Thanks.

  2. #2
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    594

    Default

    Post your code.

  3. #3
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    37

    Default

    Here is one that I just found and it works great, but only on the Y axis. I noticed this one uses a Move command, and that would make sense as to why the attributes are also moving. unlike the entmod version
    Code:
    (defun c:abc ()
    
       (setq OS (getvar "OSMODE"))
       (setvar "OSMODE" 0)
    
    	(princ "\nSelect blocks or text to align horizontaly 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 alpty (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 inptx alpty))
           (command "move" ename "" inpt newpt)
           (setq ctr (+ ctr 1))
         )
    
       (setvar "OSMODE" OS)
    )

    Thanks.

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,408

    Default

    Quote Originally Posted by RubberDinero View Post
    I have a Lisp that does this, except that when the blocks have attributes, the attributes stay in place and I would have to manually move them.
    If you are modifying the drawing database using entmod, you will need to separately modify the insertion point (DXF 10) of the INSERT entity, and the insertion point or alignment points (DXF 10/11) of all ATTRIB entities which follow.

    Alternatively, if you modify the ActiveX insertionpoint property of the VLA Block Reference object, this will apply to the block reference and any associated attribute references, e.g.:
    Code:
    (defun c:ab ( / axs idx obj pnt sel )
        (initget "X Y Z")
        (setq axs (cond ((getkword "\nSelect axis [X/Y/Z] <Y>: ")) ("Y")))
        
        (if (and (setq sel (ssget "_:L" '((0 . "INSERT"))))
                 (setq pnt (getpoint "\nSpecify alignment point: "))
                 (setq pnt (trans pnt 1 (trans '(0 0 1) 1 0 t)))
            )
            (repeat (setq idx (sslength sel))
                (setq obj (vlax-ename->vla-object (ssname sel (setq idx (1- idx)))))
                (vla-put-insertionpoint obj (vlax-3D-point (mapcar '(lambda ( a b c ) (if (= axs c) b a)) (vlax-get obj 'insertionpoint) pnt '("X" "Y" "Z"))))
            )
        )
        (princ)
    )
    (vl-load-com) (princ)
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  5. #5
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    594

    Default

    Here's another:
    Code:
    (defun c:foo (/ a i k p s)
      ;; RJP - 01.03.2018
      (or (setq k (getenv "AlignBlocks")) (setq k "X"))
      (if (and (not (initget "X Y Z"))
    	   (setq k (cond ((getkword (strcat "\nAlignment [X/Y/Z] <" k ">: ")))
    			 (k)
    		   )
    	   )
    	   (setq p (getpoint "\nPick an alignment point: "))
    	   (setq s (ssget ":L" '((0 . "insert"))))
          )
        (progn
          (setenv "AlignBlocks" k)
          (setq i (vl-position k '("X" "Y" "Z")))
          (setq a (nth i p))
          (foreach b (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
    	(setq p (vlax-get b 'insertionpoint))
    	(vlax-put b
    		  'insertionpoint
    		  (cond	((= 0 i) (list a (cadr p) (caddr p)))
    			((= 1 i) (list (car p) a (caddr p)))
    			((list (car p) (cadr p) a))
    		  )
    	)
          )
        )
      )
      (princ)
    )
    (vl-load-com)
    Last edited by ronjonp; 3rd Jan 2018 at 08:28 pm. Reason: Fixed bug Lee pointed out

  6. #6
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,408

    Default

    @ronjonp, what if the source block has coordinates '(1 1 1) ?
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  7. #7
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    594

    Default

    Quote Originally Posted by Lee Mac View Post
    @ronjonp, what if the source block has coordinates '(1 1 1) ?
    DOH!

  8. #8
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    37

    Default

    Lee, you are on a whole different level! This worked great!
    I definitely haven't dove into the VLA language aspect of creating Lisp, but it definitely seems like it's more powerful and somewhat easier to write, though i have no idea what I'm looking at(for the most part).

  9. #9
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    37

    Default

    Just tried yours and it also works great! I definitely need to start digging into these languages a lot deeper.

    Edited: This one is reffering to ronjonp's code.

  10. #10
    Full Member
    Using
    AutoCAD 2016
    Join Date
    Aug 2016
    Posts
    37

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by ronjonp View Post
    Here's another:
    Code:
    (defun c:foo (/ a i k p s)
      ;; RJP - 01.03.2018
      (or (setq k (getenv "AlignBlocks")) (setq k "X"))
      (if (and (not (initget "X Y Z"))
    	   (setq k (cond ((getkword (strcat "\nAlignment [X/Y/Z] <" k ">: ")))
    			 (k)
    		   )
    	   )
    	   (setq p (getpoint "\nPick an alignment point: "))
    	   (setq s (ssget ":L" '((0 . "insert"))))
          )
        (progn
          (setenv "AlignBlocks" k)
          (setq i (vl-position k '("X" "Y" "Z")))
          (setq a (nth i p))
          (foreach b (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
    	(setq p (vlax-get b 'insertionpoint))
    	(vlax-put b
    		  'insertionpoint
    		  (cond	((= 0 i) (list a (cadr p) (caddr p)))
    			((= 1 i) (list (car p) a (caddr p)))
    			((list (car p) (cadr p) a))
    		  )
    	)
          )
        )
      )
      (princ)
    )
    (vl-load-com)
    I'm going to use your code, because I noticed that if i pre-select the objects before invoking the command, it leave the grips visible and i can click the insertion grip after all the prompts and align to it.
    Lee's code may do this as well, but i didn't try it.
    Last edited by RubberDinero; 3rd Jan 2018 at 09:02 pm.

Similar Threads

  1. Align attributes
    By frans1954 in forum AutoCAD Drawing Management & Output
    Replies: 0
    Last Post: 12th Feb 2012, 09:31 pm
  2. Object Data (OD) attributes to Block attributes
    By mpozauko in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 6th Jan 2012, 01:08 pm
  3. Align attributes with the block
    By MarkytheSparky in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 6th May 2008, 12:05 am
  4. Align and move block attributes with and to a line?
    By EX-BIT in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 13th Mar 2008, 07:25 pm
  5. How do I align text within a block when rotating a block
    By troutspinner in forum AutoCAD General
    Replies: 2
    Last Post: 15th Mar 2007, 01:23 am

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts