Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello there,
I have a code connecting some blocks by polylines (please refer to the code below). I have trouble with the result, it has not worked as well.
(defun draw-pline-between-blocks (bl1 bl2 offset)
"Function to draw a polyline between two blocks with a fillet at pt2 and pt3, and a customizable offset."
(if (and bl1 bl2) ; Check if both blocks are valid
(progn
;; Get the coordinates of the blocks
(setq p1 (cdr (assoc 10 (entget bl1)))) ; Coordinates of the first block
(setq p2 (cdr (assoc 10 (entget bl2)))) ; Coordinates of the second block
;; Assign coordinates to variables x1, y1, x2, y2
(setq x1 (car p1) y1 (cadr p1)) ; Coordinates of the first block
(setq x2 (car p2) y2 (cadr p2)) ; Coordinates of the second block
;; Create a list of points based on the conditions described
(cond
((and (/= x1 x2) (/= y1 y2)) ; If x1 ≠ x2 and y1 ≠ y2
(setq pt1 (list x1 y1)
pt2 (list (+ x1 offset) y1)
pt3 (list (+ x1 offset) y2)
pt4 (list x2 y2)))
((= x1 x2) ; If x1 = x2
(if (> y1 y2)
(setq pt1 (list x1 y1)
pt2 (list (- x1 offset) y1)
pt3 (list (- x1 offset) y2)
pt4 (list x2 y2))
(setq pt1 (list x1 y1)
pt2 (list (- x1 offset) y1)
pt3 (list (- x1 offset) y2)
pt4 (list x2 y2))))
((= y1 y2) ; If y1 = y2
(if (< x1 x2)
(setq pt1 (list x1 y1)
pt2 (list x1 (- y1 offset))
pt3 (list x2 (- y1 offset))
pt4 (list x2 y2))
(setq pt1 (list x1 y1)
pt2 (list x1 (- y1 offset))
pt3 (list x2 (- y1 offset))
pt4 (list x2 y2))))
)
;; Print the coordinates of the points to the screen
(princ (strcat "\nCoordinates of pt1: " (rtos (car pt1) 2 3) ", " (rtos (cadr pt1) 2 3)))
(princ (strcat "\nCoordinates of pt2: " (rtos (car pt2) 2 3) ", " (rtos (cadr pt2) 2 3)))
(princ (strcat "\nCoordinates of pt3: " (rtos (car pt3) 2 3) ", " (rtos (cadr pt3) 2 3)))
(princ (strcat "\nCoordinates of pt4: " (rtos (car pt4) 2 3) ", " (rtos (cadr pt4) 2 3)))
(command "_.FILLET" "_R" 200) ; Set the fillet radius
;; Draw the polyline through the defined points
(command "_.PLINE" pt1 pt2 pt3 pt4 "")
;; Apply fillet at pt2 and pt3 with a radius of 200mm
(command "_.FILLET" "_P" pt2 pt3)
)
(princ "\nYou need to select 2 valid blocks!") ; Error message when blocks are not valid
)
(princ)
)
(defun c:ss ()
"Command to select a list of blocks and draw a polyline sequentially between each pair with a customizable offset."
(setq offset (getreal "\nEnter the offset distance (default 500mm): "))
(if (not offset) (setq offset 500)) ; Default value if none is entered
(setq block-list nil)
(princ "\nSelect the list of blocks.")
(setq bl (car (entsel "\nSelect the first block (press Enter to finish): ")))
;; Loop to select blocks
(while bl
(setq block-list (append block-list (list bl)))
(setq bl (car (entsel "\nSelect the next block (press Enter to finish): ")))
)
(if (and block-list (> (length block-list) 1))
(progn
(princ "\nDrawing polyline between block pairs...")
;; Iterate through the list and draw the polyline
(setq i 0)
(while (< i (- (length block-list) 1))
(draw-pline-between-blocks (nth i block-list) (nth (+ i 1) block-list) offset)
(setq i (1+ i))
)
(princ "\nPolyline drawing complete.")
)
(princ "\nYou need to select at least 2 blocks!")
)
(princ)
)
- The 2nd & 3rd polylines were drawn wrong as below photo:
Please refer to the attached files for your information.
Thanks & best regards,
-Kin-
Solved! Go to Solution.