Place a block at start and end of every line on layer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a layer that only contains pathing for an automated vehicle. I then need to place a "QR CODE" block at every start and end point of all the lines of that pathing. The placed block needs to be on a different layer. I got ChatGPT to start the code I'm just having a hard time getting it working and am not a very competent programmer. Any suggestions or help guys?
(defun c:QRPlace ()
(setq line_layer "MUSA-AGV-AMR PATH")
(setq qr_block_name "QR CODE")
(setq qr_block_layer "MUSA-QR CODES")
(setq tolerance 0.05) ; 5% tolerance for coordinate check
(if (and (tblsearch "LAYER" line_layer) (tblsearch "BLOCK" qr_block_name) (tblsearch "LAYER" qr_block_layer))
(progn
(setq points '()) ; Initialize list to store points
(setq ss (ssget "_X" (list '(0 . "LINE") '(8 . line_layer)))) ; Select lines on specified layer
(if ss
(progn
(setq i 0)
(while (< i (sslength ss))
(setq ent (ssname ss i))
(setq startpt (cdr (assoc 10 (entget ent)))) ; Get the start point of the line
(setq endpt (cdr (assoc 11 (entget ent)))) ; Get the end point of the line
; Check for duplicates or close coordinates and add to the list
(if (and (not (member startpt points :test 'equalp))
(not (member endpt points :test 'equalp))
(not (is-close-coord startpt points tolerance))
(not (is-close-coord endpt points tolerance)))
(progn
(setq points (cons startpt points))
(setq points (cons endpt points))
)
)
(setq i (1+ i))
)
; Insert QR CODE block at unique points in the list
(foreach point points
(command "_insert" qr_block_name point) ; Insert block at the point
(command "_.chprop" "_last" "" "LA" qr_block_layer "") ; Change block to QR CODES layer
)
(princ "\nQR CODE blocks inserted successfully at unique points of lines on specified layer.")
)
(princ "\nNo lines found on the specified layer.")
)
)
(princ "\nLayer or block not found. Please check the names.")
)
)
(defun is-close-coord (coord lst tol)
; Check if the coordinate is close to any coordinate in the list within the given tolerance
(foreach item lst
(if (and (<= (distance coord item) tol) (>= (distance coord item) (- tol)))
(return T)
)
)
nil
)