Jump to content

Help Lisp to move two block centers align horizontally


Anh Quan

Recommended Posts

move block.dwgimage.png.65f4e64850562fde7de8ea4c91ae3574.png

 

I really need your help,

I have 2 blocks next to each other but their center is not horizontal I want them to be aligned horizontally by AutoLISP as the picture depicts can someone help me. (I don't know anything about Lisp)

Link to comment
Share on other sites

Have you looked into point filters? That would be much easier than writing a function.

 

Select the green block and the insertion grip. It asks for a destination point. Type .Y and give it the insertion point of the red block. That takes the Y coordinate of your point and lets you then specify the X and Z. Drag down the green block (the block's X is assumed) and place it.

 

If you have to do that a thousand times, yes, write a function. If you only have to do it once, try the filter.

  • Like 1
Link to comment
Share on other sites

16 minutes ago, CyberAngel said:

Have you looked into point filters? That would be much easier than writing a function.

 

Select the green block and the insertion grip. It asks for a destination point. Type .Y and give it the insertion point of the red block. That takes the Y coordinate of your point and lets you then specify the X and Z. Drag down the green block (the block's X is assumed) and place it.

 

If you have to do that a thousand times, yes, write a function. If you only have to do it once, try the filter.

 

Thank you, however I have to do it many times and don't know the lisp skill :(

Link to comment
Share on other sites

Try this, block2snap

 

(defun c:block2snap ( / SnapStart Xstart Ystart Zstart snap blkset LenSet Ecount Ename Edata InsPt Xins Yins Zins Xround Yround Zround PtRound NewInsData newdata xoffset yoffset zoffset txtsize)

  (defun *error* ( msg )
    (if doc (_EndUndo doc))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ)
  )

  (defun blksnap (val incr base / Snapdist NewNum NuymSteps Rounded NewCoord)
    (setq Snapdist (- val base)
          NewNum (/ Snapdist incr)
          NumSteps (atoi (rtos NewNum 2 0))
          Rounded (* NumSteps incr)
          NewCoord (+ base rounded)
    )
  )

  (setq xoffset 0)
  (setq yoffset 0)
  (setq zoffset 0)
  (setq SnapStart (getvar "SNAPBASE"))
  (setq Xstart (car SnapStart)
        YStart (cadr SnapStart)
        ZStart (caddr SnapStart)
  )

  (setq snap 2.5) ;;default snap distance
  (if (= xsnap nil)(setq xsnap snap))
  (if (= ysnap nil)(setq ysnap snap))
  (if (= zsnap nil)(setq zsnap 0))
  (if (= MWidth nil)(setq MWidth snap))

  (setq snap (getreal (strcat "\nEnter x-axis snap spacing (" (rtos xsnap) "): " ) ))
  (if (/= snap nil)(progn
      (setq xsnap snap)
      (setq ysnap snap)
  ))
  (setq snap (getreal (strcat "\nEnter y-axis snap spacing (" (rtos ysnap) "): " ) ))
  (if (/= snap nil)(setq ysnap snap))
  (setq zsnap (getreal (strcat "\nEnter z-axis snap spacing (" (rtos zsnap) ")(set to 0 to set Z axis to 0): " ) ))
  (if (/= zsnap nil)(setq zsnap 0))

  (setq blkset (ssget '((0 . "INSERT"))))
  (setq LenSet (sslength blkSet))
  (setq Ecount 0)


  (repeat LenSet
    (setq Ename (ssname blkset Ecount)
          Edata (entget Ename)
          mycons 10) ;;Cons for entity reference 10 = block coordinates

    (setq InsPt (cdr (assoc mycons Edata))
          Xins (car InsPt)
          Yins (cadr InsPt)
          Zins (caddr InsPt)
          Xround (blksnap Xins xsnap xstart)
          YRound (blksnap Yins ysnap ystart)
          ZRound (blksnap Zins ysnap ystart)
          PtRound (list (+ Xround xoffset) (+ YRound yoffset))
          PtRound (list (+ Xround xoffset) (+ YRound yoffset) (+ ZRound zoffset))
          NewInsData (cons mycons PtRound)
          Newdata (subst NewInsdata (assoc mycons Edata) Edata)
    )

    (if (= "INSERT" (cdr (assoc 0 Edata))) (entmod Newdata))

    (setq ECount (1+ Ecount))
  )
  (princ)
)

 

  • Like 1
Link to comment
Share on other sites

On 5/9/2023 at 3:08 PM, Steven P said:

Try this, block2snap

 

(defun c:block2snap ( / SnapStart Xstart Ystart Zstart snap blkset LenSet Ecount Ename Edata InsPt Xins Yins Zins Xround Yround Zround PtRound NewInsData newdata xoffset yoffset zoffset txtsize)

  (defun *error* ( msg )
    (if doc (_EndUndo doc))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ)
  )

  (defun blksnap (val incr base / Snapdist NewNum NuymSteps Rounded NewCoord)
    (setq Snapdist (- val base)
          NewNum (/ Snapdist incr)
          NumSteps (atoi (rtos NewNum 2 0))
          Rounded (* NumSteps incr)
          NewCoord (+ base rounded)
    )
  )

  (setq xoffset 0)
  (setq yoffset 0)
  (setq zoffset 0)
  (setq SnapStart (getvar "SNAPBASE"))
  (setq Xstart (car SnapStart)
        YStart (cadr SnapStart)
        ZStart (caddr SnapStart)
  )

  (setq snap 2.5) ;;default snap distance
  (if (= xsnap nil)(setq xsnap snap))
  (if (= ysnap nil)(setq ysnap snap))
  (if (= zsnap nil)(setq zsnap 0))
  (if (= MWidth nil)(setq MWidth snap))

  (setq snap (getreal (strcat "\nEnter x-axis snap spacing (" (rtos xsnap) "): " ) ))
  (if (/= snap nil)(progn
      (setq xsnap snap)
      (setq ysnap snap)
  ))
  (setq snap (getreal (strcat "\nEnter y-axis snap spacing (" (rtos ysnap) "): " ) ))
  (if (/= snap nil)(setq ysnap snap))
  (setq zsnap (getreal (strcat "\nEnter z-axis snap spacing (" (rtos zsnap) ")(set to 0 to set Z axis to 0): " ) ))
  (if (/= zsnap nil)(setq zsnap 0))

  (setq blkset (ssget '((0 . "INSERT"))))
  (setq LenSet (sslength blkSet))
  (setq Ecount 0)


  (repeat LenSet
    (setq Ename (ssname blkset Ecount)
          Edata (entget Ename)
          mycons 10) ;;Cons for entity reference 10 = block coordinates

    (setq InsPt (cdr (assoc mycons Edata))
          Xins (car InsPt)
          Yins (cadr InsPt)
          Zins (caddr InsPt)
          Xround (blksnap Xins xsnap xstart)
          YRound (blksnap Yins ysnap ystart)
          ZRound (blksnap Zins ysnap ystart)
          PtRound (list (+ Xround xoffset) (+ YRound yoffset))
          PtRound (list (+ Xround xoffset) (+ YRound yoffset) (+ ZRound zoffset))
          NewInsData (cons mycons PtRound)
          Newdata (subst NewInsdata (assoc mycons Edata) Edata)
    )

    (if (= "INSERT" (cdr (assoc 0 Edata))) (entmod Newdata))

    (setq ECount (1+ Ecount))
  )
  (princ)
)

 

it seems for me to be able to enter the spacing, however i don't understand the axis spacing. sir

Link to comment
Share on other sites

Yes, my one doesn't line the blocks up with each other but lines them up with a grid spacing which is where the axis comes into it. I assumed that the blocks would be nearly lined up and so lining both to grid will line them up.

 

For example if one is a at Y axis 10.587 and the other at 9.55, set the grid spacing to say, 2.5 and they will both be moved to Y axis value of 10

  • Like 1
Link to comment
Share on other sites

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