Jump to content

Lisp code to rotate blocks as mentioned below


Sandeep RC

Recommended Posts

Hello, can anybody help me with the lisp routine that can rotate multiple blocks placed at the end of polyline in the direction of polyline?

I think this can be done by considering the reference that all the polylines are drawn from center to outward direction.

Please see the attached CAD file for reference.

image.png.7bf4c81662bfa20769232f51f6886a52.png

drawing 2.dwg

Edited by Sandeep RC
Link to comment
Share on other sites

how about placing them anew?

made a function like that recently just changed it a bit
 


(defun c:placetest (/ doc sel bname ent amount)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (if (setq sel (ssget '((0 . "ARC,CIRCLE,ELLIPSE,LINE,*POLYLINE,SPLINE"))))
    (progn
      (setq bname "fj") ;; edit -- possibly getstring
      (if (= bname "")
        (progn
          (while (or (not ent)(not (= (cdr (assoc 0 (entget ent))) "INSERT")))
            (setq ent (car (entsel "\nSelect Block: ")))
          )
          (setq bname (vla-get-effectivename (vlax-ename->vla-object ent)))
        )
      )
      (if (not (tblsearch "Block" bname)) (progn (princ "\nError: Blockname invalid.") (exit)))
      
      (vlax-for obj (setq sel (vla-get-activeselectionset doc))
        (placeBlk (vla-get-modelspace doc) obj bname)
      )
      (vla-delete sel)
    )
  )
  (princ)
)


;|
  Places a block at the end of a Curve
  @Param space \<vla-object> Types: Block, ModelSpace, PaperSpace
  @Param cObj \<vla-object> Curve Object
  @Param bname \<string> Block Name
  @Returns ?
|;
(defun placeBlk (space cObj bname / *error* osmode dist obj rltvAng)
  
  (defun *error* (msg)
    (if osmode (setvar "osmode" osmode))
    (princ (strcat "Error: " msg))
    (princ)
  )
  
  (setq osmode (getvar "osmode"))
  (setvar "osmode" 0)
  (setq rltvAng 0)
  (setq dist (vlax-curve-getdistatparam cObj (vlax-curve-getendparam cObj)))
  (setq pt (vlax-curve-getpointatdist cObj dist))
  (vla-insertblock space (vlax-3d-point pt) bname 1 1 1 (+ (get-curve-angle cObj dist) rltvAng))

  (setvar "osmode" osmode)
)

(defun pauseCmdTillEnd ()
  (while (= 1 (getvar "cmdactive"))
   (command pause))
)

(defun get-curve-angle (cObj dist / deriv) 
  (setq deriv (vlax-curve-getFirstDeriv cObj (vlax-curve-getParamAtDist cObj dist))) ; get vector
  (atan (cadr deriv) (car deriv)) ; Calculate the angle of the tangent vector
)

 

Link to comment
Share on other sites

or this would allow you to change the relativ angle for the block placement wich is more like what i originally used
 

(defun c:placetest (/ doc sel bname ent amount rltv)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (if (setq sel (ssget '((0 . "ARC,CIRCLE,ELLIPSE,LINE,*POLYLINE,SPLINE"))))
    (progn
      (setq bname "fj") ;; edit -- possibly getstring
      (if (= bname "")
        (progn
          (while (or (not ent)(not (= (cdr (assoc 0 (entget ent))) "INSERT")))
            (setq ent (car (entsel "\nSelect Block: ")))
          )
          (setq bname (vla-get-effectivename (vlax-ename->vla-object ent)))
        )
      )
      (if (not (tblsearch "Block" bname)) (progn (princ "\nError: Blockname invalid.") (exit)))
      
      (vlax-for obj (setq sel (vla-get-activeselectionset doc))
        (setq rltv (placeBlk (vla-get-modelspace doc) obj bname rltv))
      )
      (vla-delete sel)
    )
  )
  (princ)
)


;|
  Places a block at the end of a Curve
  @Param space \<vla-object> Types: Block, ModelSpace, PaperSpace
  @Param cObj \<vla-object> Curve Object
  @Param bname \<string> Block Name
  @Param rltvAng \<real> relative angle as real
  @Returns \<real> relative angle as real
|;
(defun placeBlk (space cObj bname rltvAng / *error* osmode dist obj )
  
  (defun *error* (msg)
    (if osmode (setvar "osmode" osmode))
    (princ (strcat "Error: " msg))
    (princ)
  )
  
  (setq osmode (getvar "osmode"))
  (setvar "osmode" 0)
  (setq dist (vlax-curve-getdistatparam cObj (vlax-curve-getendparam cObj)))
  (setq pt (vlax-curve-getpointatdist cObj dist))
  (if rltvAng
    (vla-insertblock space (vlax-3d-point pt) bname 1 1 1 (+ (get-curve-angle cObj dist) rltvAng))
    (progn
      (setq obj (vla-insertblock space (vlax-3d-point pt) bname 1 1 1 0))
      (setvar "osmode" osmode)
      (command "_rotate" (vlax-vla-object->ename obj) "" pt) (pauseCmdTillEnd)
      (setq rltvAng (- (vla-get-rotation obj) (get-curve-angle cObj dist)))
      (setvar "osmode" 0)
    )
  )

  (setvar "osmode" osmode)
  rltvAng
)

(defun pauseCmdTillEnd ()
  (while (= 1 (getvar "cmdactive"))
   (command pause))
)

(defun get-curve-angle (cObj dist / deriv) 
  (setq deriv (vlax-curve-getFirstDeriv cObj (vlax-curve-getParamAtDist cObj dist))) ; get vector
  (atan (cadr deriv) (car deriv)) ; Calculate the angle of the tangent vector
)

 

Link to comment
Share on other sites

@EnM4st3r Just checked both the codes. The first one is the one i wanted. cheers mate. much appreciated. Tomorrow i will run on my actual file that contains around 2500 plines. Thank a lot once again. ❤️

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