(defun readDeviceListFromFile ( / file filePath line result) ;; Open file dialog for user to pick device list text file (setq filePath (getfiled "Select Device List File" "" "txt" 0)) (if filePath (progn (setq file (open filePath "r")) (setq result '()) ;; initialize empty list before appending (while (setq line (read-line file)) (setq result (append result (list (strcase (vl-string-trim " \t\r\n" line))))) ) (close file) result ) nil ) ) (defun updateTagAttribute (ent tagValue / obj attribs att) ;; Convert entity to VLA object (setq obj (vlax-ename->vla-object ent)) (if (and obj (eq (vla-get-hasattributes obj) :vlax-true)) (progn (setq attribs (vlax-invoke obj 'GetAttributes)) (foreach att attribs (if (eq (strcase (vla-get-TagString att)) "TAG1") (vla-put-TextString att tagValue) ) ) ) ) ) (defun c:Init_Network ( / dpmCount rowCount colCount i row col x y spacingX spacingY blkName scaleFactor startPt endPt zigzagStart secondCableEnds zigzagStarts ) (setq blkName "DPM") ; Block name (setq hdvBlkName "HDV2_1756-EN4TR_CHILD") ; HDV block name (setq scaleFactor 0.45) ; Scale of each block (setq spacingX 15.0) ; Horizontal spacing between blocks (setq spacingY 9.0) ; Vertical spacing between rows (setq dpmOffsetX 5.0) ; X offset for DPM blocks (setq hdvY 4.0) ; Y position for HDV block (setq secondCableEnds '()) ; Store vertical line tops (setq zigzagStarts '()) ; Store zigzag start points (command "_.ERASE" "ALL" "") (setq devices (readDeviceListFromFile)) (if (not devices) (progn (princ "\nNo device file selected or file was empty.") (exit) ) ) (setq dpmCount (length devices)) ;; Calculate grid size (setq colCount (fix (sqrt dpmCount))) (if (< (* colCount colCount) dpmCount) (setq colCount (+ colCount 1)) ) (setq rowCount (fix (/ (+ dpmCount colCount -1) colCount))) ;; Calculate centering offset for DPM blocks (setq gridWidth (* (- colCount 1) spacingX)) (setq centerOffsetX (- (/ gridWidth 2.0))) ;; Insert HDV block at (0, hdvY) on layer 0 (command "_.LAYER" "S" "0" "") (command "_.INSERT" hdvBlkName (list 0 hdvY) scaleFactor scaleFactor 0) ;; Loop to place blocks and draw lines (setq i 0) (repeat dpmCount (setq row (fix (/ i colCount))) (setq col (rem i colCount)) (setq x (+ (* col spacingX) dpmOffsetX)) ; Use variable (setq y (* -1 row spacingY)) ; No Y offset (setq dpmName (nth i devices)) ;; get line content ;; Insert DPM block on layer 0 (command "_.LAYER" "S" "0" "") (command "_.INSERT" blkName (list x y) scaleFactor scaleFactor 0) ;; Get the last inserted entity (setq ent (entlast)) ;; Call the function (updateTagAttribute ent dpmName) ;; First cable: vertical up (setq startPt (list (+ x 7.3656) (+ y 4.4406))) (setq endPt (list (car startPt) (+ (cadr startPt) 2.0))) (entmakex (list '(0 . "LINE") (cons 10 startPt) (cons 11 endPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) (setq secondCableEnds (append secondCableEnds (list endPt))) ;; Check if last block in row OR last block in entire drawing (if (or (= (rem (1+ i) colCount) 0) (= i (- dpmCount 1))) (progn ;; Last block in row or last block overall — elbow and downward (setq startPt (list (+ x 7.2677) (+ y 3.6094))) (setq elbowPt (list (- (car startPt) 0.2) (- (cadr startPt) 0.2))) (entmakex (list '(0 . "LINE") (cons 10 startPt) (cons 11 elbowPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) (setq endPt (list (car elbowPt) (- (cadr elbowPt) 4.5))) (entmakex (list '(0 . "LINE") (cons 10 elbowPt) (cons 11 endPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) (setq zigzagStarts (append zigzagStarts (list nil))) ; Placeholder ) (progn ;; Zigzag jump (setq zigzagStart (list (+ x 7.3656) (+ y 3.7852))) (setq elbowPt (list (+ x 7.8585) (+ y 5.1262))) (entmakex (list '(0 . "LINE") (cons 10 zigzagStart) (cons 11 elbowPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Upward line (setq startPt elbowPt) (setq endPt (list (car startPt) (+ (cadr startPt) 1.3144))) (entmakex (list '(0 . "LINE") (cons 10 startPt) (cons 11 endPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Store the end point of the upward line (second cable's end) (setq zigzagStarts (append zigzagStarts (list endPt))) ) ) (setq i (1+ i)) ) ;; Connect second cable end to next block's first cable end (if not last in row) (setq i 0) (repeat (- dpmCount 1) (if (/= (rem (1+ i) colCount) 0) ; Only if not last in row (progn (setq pt1 (nth i zigzagStarts)) ; Current block's second line end (setq pt2 (nth (1+ i) secondCableEnds)) ; Next block's first line end (if (and pt1 pt2 (listp pt1) (listp pt2)) ; Enhanced check (entmakex (list '(0 . "LINE") (cons 10 pt1) (cons 11 pt2) '(62 . 256) '(8 . "AS_ENET_CABLE") ) ) ) ) ) (setq i (1+ i)) ) ;; Connect last block in row to first block in next row (setq i 0) (repeat rowCount (setq lastInRowIndex (- (* (1+ i) colCount) 1)) ; Last block index in current row (setq firstInNextRowIndex (* (1+ i) colCount)) ; First block index in next row (if (and (< lastInRowIndex dpmCount) (< firstInNextRowIndex dpmCount)) (progn ;; Get the downward line end point from last block in row (setq row i) (setq col (- colCount 1)) ; Last column (setq x (+ (* col spacingX) dpmOffsetX)) ; Use variable (setq y (* -1 row spacingY)) ; Use variable (setq downwardEndPt (list (- (+ x 7.2677) 0.2) (- (- (+ y 3.6094) 0.2) 4.5))) ;; Get first block coordinates in next row (setq nextRow (1+ i)) (setq nextCol 0) ; First column (setq nextX (+ (* nextCol spacingX) dpmOffsetX)) ; Use variable (setq nextY (* -1 nextRow spacingY)) ; Use variable (setq nextBlockFirstLineEnd (list (+ nextX 7.3656) (+ (+ nextY 4.4406) 2.0))) ;; Draw horizontal line from downward end to next block's X coordinate (setq horizontalEndPt (list (car nextBlockFirstLineEnd) (cadr downwardEndPt))) (entmakex (list '(0 . "LINE") (cons 10 downwardEndPt) (cons 11 horizontalEndPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Draw vertical line up to next block's first line end (entmakex (list '(0 . "LINE") (cons 10 horizontalEndPt) (cons 11 nextBlockFirstLineEnd) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ) ) (setq i (1+ i)) ) ;; Connect first block to HDV block (if (> dpmCount 0) (progn ;; First block coordinates (setq firstX (+ (* 0 spacingX) dpmOffsetX)) ; First block X (setq firstY (* -1 0 spacingY)) ; First block Y (row 0) ;; First block's first line end point (setq firstBlockEnd (list (+ firstX 7.3656) (+ (+ firstY 4.4406) 2.0))) ;; HDV connection end point (setq hdvEndPt (list (+ 0 1.4) (- hdvY 2.8))) ; HDV is at (0, hdvY) ;; Step 1: Horizontal line to X destination - 0.5 (setq horizontalEnd (list (+ (car hdvEndPt) 0.5) (cadr firstBlockEnd))) (entmakex (list '(0 . "LINE") (cons 10 firstBlockEnd) (cons 11 horizontalEnd) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Step 2: Vertical line to Y destination - 0.5 (setq verticalEnd (list (car horizontalEnd) (+ (cadr hdvEndPt) 0.5))) (entmakex (list '(0 . "LINE") (cons 10 horizontalEnd) (cons 11 verticalEnd) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Step 3: Final line to HDV end point (entmakex (list '(0 . "LINE") (cons 10 verticalEnd) (cons 11 hdvEndPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ) ) ;; Connect last block to HDV block (if (> dpmCount 0) (progn ;; Last block index and coordinates (setq lastBlockIndex (- dpmCount 1)) (setq lastRow (fix (/ lastBlockIndex colCount))) (setq lastCol (rem lastBlockIndex colCount)) (setq lastX (+ (* lastCol spacingX) dpmOffsetX)) (setq lastY (* -1 lastRow spacingY)) ;; HDV connection end point for last block (setq hdvLastEndPt (list (+ 0 0.1) (- hdvY 5.83))) ; HDV is at (0, hdvY) ;; Check if we have only one row (if (= rowCount 1) (progn ;; Single row: extend vertical line down by 1.5 units below destination Y, keeping same X (setq startPt (list (- (+ lastX 7.2677) 0.2) (- (+ lastY 3.6094) 0.2))) (setq extendedVerticalEnd (list (car startPt) (- (cadr hdvLastEndPt) 1.5))) (entmakex (list '(0 . "LINE") (cons 10 startPt) (cons 11 extendedVerticalEnd) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Horizontal line LEFT to final destination X (setq horizontalEnd (list (car hdvLastEndPt) (cadr extendedVerticalEnd))) (entmakex (list '(0 . "LINE") (cons 10 extendedVerticalEnd) (cons 11 horizontalEnd) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Vertical line UP to final destination (entmakex (list '(0 . "LINE") (cons 10 horizontalEnd) (cons 11 hdvLastEndPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ) (progn ;; Multiple rows: use original 2-segment path ;; Last block's vertical line end point (downward line end) (setq lastBlockVerticalEnd (list (- (+ lastX 7.2677) 0.2) (- (- (+ lastY 3.6094) 0.2) 4.5))) ;; Step 1: Horizontal line LEFT to X destination (setq horizontalEnd (list (car hdvLastEndPt) (cadr lastBlockVerticalEnd))) (entmakex (list '(0 . "LINE") (cons 10 lastBlockVerticalEnd) (cons 11 horizontalEnd) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ;; Step 2: Vertical line to final destination (entmakex (list '(0 . "LINE") (cons 10 horizontalEnd) (cons 11 hdvLastEndPt) '(62 . 256) '(8 . "AS_ENET_CABLE"))) ) ) ) ) (princ) )