How to align multiple dynamic block to a polyline?

How to align multiple dynamic block to a polyline?

yu85.info
Collaborator Collaborator
2,319 Views
12 Replies
Message 1 of 13

How to align multiple dynamic block to a polyline?

yu85.info
Collaborator
Collaborator

Hi, I have this DWG file (attached). It contains dynamic block with an alignment parameter. Is there a wat to automatically align al of them? 

The original is on the left of the drawing (A) and the result I am asking is on the right (B)

Thank you very much in advance.

    

0 Likes
Accepted solutions (2)
2,320 Views
12 Replies
Replies (12)
Message 2 of 13

CADaSchtroumpf
Advisor
Advisor

Hi,

Try with this (copy/paste the code directly in command line)

((lambda ( / js n en js_pl en_pl dxf_ent pt_ins deriv alpha)
  (defun update_block (block_record ang pt / s_e dxf_e)
    (if (/= (getvar "ATTMODE") 1) (setvar "ATTMODE" 1))
    (setq s_e block_record)
    (while (/= (cdr (assoc 0 (entget (setq s_e (entnext s_e))))) "SEQEND")
      (setq dxf_e (entget s_e))
      (if (eq (cdr (assoc 0 dxf_e)) "ATTRIB")
        (progn
          (entmod (setq dxf_e (subst (cons 50 ang) (assoc 50 dxf_e) dxf_e)))
          (entmod (setq dxf_e (subst (cons 10 (polar pt (+ (angle pt (cdr (assoc 10 dxf_e))) ang) (distance pt (cdr (assoc 10 dxf_e))))) (assoc 10 dxf_e) dxf_e)))
        )
      )
    )
    (entupd block_record)
  )
  (princ "\nSelect the blocks to align")
  (setq js (ssget '((0 . "INSERT") (67 . 0) (8 . "חשמל") (66 . 1) (2 . "`*U*"))))
  (cond
    (js
      (repeat (setq n (sslength js))
        (setq en (ssname js (setq n (1- n))))
        (if (/= (getpropertyvalue en "BlockTableRecord/Name") "GOOD")
          (ssdel en js)
        )
      )
      (cond
        (js
          (princ "\nSelect the polyline")
          (while (null (setq js_pl (ssget "_+.:E:S" '((0 . "LWPOLYLINE") (67 . 0) (8 . "חשמל")))))
            (princ "\nOject isn't valide")
          )
          (setq en_pl (ssname js_pl 0))
          (repeat (setq n (sslength js))
            (setq
              en (ssname js (setq n (1- n)))
              dxf_ent (entget en)
              pt_ins (cdr (assoc 10 dxf_ent))
              deriv (vlax-curve-getfirstderiv en_pl (vlax-curve-getparamatpoint en_pl (vlax-curve-getClosestPointTo en_pl pt_ins)))
              alpha (- (atan (cadr deriv) (car deriv)) (angle '(0 0 0) (getvar "UCSXDIR")))
            )
            (entmod (subst (cons 50 alpha) (assoc 50 dxf_ent) dxf_ent))
            (update_block en alpha pt_ins)
          )
        )
      )
    )
  )
  (prin1)
))
0 Likes
Message 3 of 13

yu85.info
Collaborator
Collaborator
Hi, thank you very much for your answer. but for some reason I can not
paste it in the command line.
Can you explain me please how to make it as a LISP?
Thank you again
0 Likes
Message 4 of 13

CADaSchtroumpf
Advisor
Advisor

I saved the file in DOS-862 codepage (Hebrew) under the name: yu85_info.lsp
Drag this file from windows explorer into the autocad graphics window. The YU85_INFO command must be available.
Otherwise check the characters "חשמל" (layer name) under your system in the delivered lisp file.
If you have (8 . "????") change to (8 . "חשמל"), there are 2 occurrences in the file.

For my part I can not test this lisp because autocad no longer understands it (it understands it in ANSI)

0 Likes
Message 5 of 13

yu85.info
Collaborator
Collaborator

Hi @CADaSchtroumpf , You have helped me a lot. If it is ok I have another question.

I've changed the layer name in the DWG attached ("dwg1") to English [ELEC]. I've changed your lisp I have attached ("aba") in ANSI. But is there a way to make it variable so it will not fit only to one layer. For example I've added a copy of the block and the polyline to another layer - "TEST". If I try to make this command on this layer it does not work unless I change the LISP to "TEST" instead of "ELEC".

I mean that it will do this to any block I will pick no matter in which layer it is. 

Thank you very very much sir  

0 Likes
Message 6 of 13

CADaSchtroumpf
Advisor
Advisor
Accepted solution

So I introduced an additional step: you have to make a unique selection of a dynamic block.

This allows you to create a specific selection filter with the name of the block, its insertion layer, etc.

So it should also work with another dynamic block other than 'GOOD'

On the other hand, the alignment polyline must also be on the same layer as the insertion of the blocks (this can be deleted)

I also fixed a bug on the positioning of attributes

0 Likes
Message 7 of 13

yu85.info
Collaborator
Collaborator

THIS IS JUST GREAT!

You have saved me so much time. I appreciate it so much.

Thank you sir.

  

0 Likes
Message 8 of 13

yu85.info
Collaborator
Collaborator

Hi, @CADaSchtroumpf sorry to disturb you again. It seems that when the block is aligned to the polyline it turns upside down. I don't know how to fix it.

I  will appreciate it very much if you will have time to look at it. 

I attached the LISP and a DWG file that I used with your great LISP.

Thank you very much sir

 

0 Likes
Message 9 of 13

CADaSchtroumpf
Advisor
Advisor
Accepted solution

Here is what I can do to improve the reading, however the alignment will not be perfect if there is a flip.
This is because your block is right justified.
It would be necessary to make a justified block in the center so that it is a little less shocking ...

 

Before line 67 : (entmod (subst (cons 50 alpha) (assoc 50 dxf_ent) dxf_ent))

Insert this two lines

            (if (< alpha 0.0) (setq alpha (+ (* 2 pi) alpha)))
            (if (and (> alpha (* pi 0.5)) (< alpha (* pi 1.5))) (setq alpha (rem (+ alpha pi) (* 2 pi))))

 

0 Likes
Message 10 of 13

yu85.info
Collaborator
Collaborator
It seems to works very well so far
I appreciate your help and patience very much sir
Thank you!
0 Likes
Message 11 of 13

krazeymike
Enthusiast
Enthusiast

Hello all,

Is anybody able to help me modify this lisp so that it works for 3D Polylines?

Thanks CADaSchtroumpf for an awesome start.

Here's a copy of what I am using currently however it only seems to work for 2D Polylines and the models I work in are 3D

Message 12 of 13

krazeymike
Enthusiast
Enthusiast

Since this is an old thread thought I would reply to you and see if you're able to help with the above.

Thank you in advance

0 Likes
Message 13 of 13

krazeymike
Enthusiast
Enthusiast

Figured it out a while ago, posted for anyone interested. 3D polylines are just Polyline not LWPolyline 😁

0 Likes