2025-12-09 15:50:16 +04:00

414 lines
13 KiB
Common Lisp

(defun clearDrawing ( / ss)
(setq ss (ssget "_X" '((0 . "*"))))
(if ss (command "_.erase" ss ""))
(princ "\nDrawing cleared.")
)
(defun getVisibilityFromName (deviceName partNumber)
(cond
((wcmatch (strcase deviceName) "*EX*") "Panel")
((wcmatch (strcase deviceName) "*PMM*") "Panel")
((wcmatch (strcase deviceName) "*TP*") "Panel")
((wcmatch (strcase deviceName) "*VSU*") "Panel")
((wcmatch (strcase deviceName) "*APF*") "APF")
((and (wcmatch (strcase deviceName) "*VFD*")
partNumber
(wcmatch (strcase partNumber) "35S*")) "APF")
((wcmatch (strcase deviceName) "*VFD*") "VFD")
((wcmatch (strcase deviceName) "*SIO*") "SIO")
((wcmatch (strcase deviceName) "*FIO*") "FIO")
((wcmatch (strcase deviceName) "*SPARE*") "")
(T "DEFAULT")
)
)
(defun str-trim (s)
(vl-string-trim " \t\n\r" s)
)
(defun setDeviceAttributes (blk ip device port / tag2 tag3 isVFD spacePos slashPos baseName part2 tag ip1 ip2 spacePosIP)
(setq tag2 device)
(setq tag3 "")
(setq ip1 ip)
(setq ip2 "")
;; === VFD logic ===
(if (and device (vl-string-search "VFD" (strcase device)))
(progn
(setq spacePos (vl-string-search " " device))
(if spacePos
(progn
(setq part1 (substr device 1 spacePos))
(setq part2 (substr device (+ spacePos 2)))
(setq slashPos (vl-string-search "/VFD" part1))
(if slashPos
(setq baseName (substr part1 1 slashPos))
(setq baseName part1)
)
(setq tag2 (str-trim part1))
(setq tag3 (strcat (str-trim baseName) "/" (str-trim part2)))
)
)
)
)
;; === IP splitting ===
(setq spacePosIP (vl-string-search " " ip))
(if spacePosIP
(progn
(setq ip1 (str-trim (substr ip 1 spacePosIP)))
(setq ip2 (str-trim (substr ip (+ spacePosIP 2))))
)
)
(foreach att (vlax-invoke blk 'GetAttributes)
(setq tag (strcase (vla-get-tagstring att)))
(cond
((= tag "IP") (vla-put-textstring att ip1))
((= tag "IP2") (vla-put-textstring att ip2))
((= tag "TAG2") (vla-put-textstring att tag2))
((= tag "PORT") (vla-put-textstring att port))
((= tag "TAG3") (if (> (strlen tag3) 0) (vla-put-textstring att tag3)))
)
)
)
(defun place-enet-devices (originX originY deviceGroup / blockName width count i x y basePt lastEnt targetPt blk visibilityValue deviceName device ip fullBlockName port)
(setq blockName "HDV2_ENET_DEVICE_")
(setq width 1.2)
(setq count (length deviceGroup))
(setq i 0)
(setq y 20.4718)
(while (< i count)
(setq blockSpacing 1.2)
(setq groupWidth (* blockSpacing (1- count)))
(setq centerX 21.2)
(setq startX (- centerX (/ groupWidth 2.0)))
(setq x (+ startX (* i blockSpacing) 0.3))
(setq basePt '(0 0 0))
(setq targetPt (list (+ originX x) (+ originY y) 0))
(setq devicePair (nth i deviceGroup))
(setq device (cdr (assoc "NAME" devicePair)))
(setq ip (cdr (assoc "IP" devicePair)))
(setq port (cdr (assoc "PORT" devicePair)))
(setq partNumber (cdr (assoc "PARTNUM" devicePair)))
(setq deviceName (getVisibilityFromName device partNumber))
(setq fullBlockName (strcat blockName deviceName))
(command "_.-INSERT" fullBlockName basePt 0.8 0.8 0.8 0)
(if (setq lastEnt (entlast))
(progn
(setq blk (vlax-ename->vla-object lastEnt))
(vla-move blk
(vlax-3d-point basePt)
(vlax-3d-point targetPt)
)
(vla-put-rotation blk 0.0)
(setDeviceAttributes blk ip device port)
)
(princ "\nFailed to get last entity.")
)
(setq i (1+ i))
)
)
(defun labelZone32Lines (ent / vlaBlock att basePt labelPt labelStr height index rotation)
(if (and ent (eq (cdr (assoc 0 (entget ent))) "INSERT"))
(progn
(setq vlaBlock (vlax-ename->vla-object ent))
(setq index 1)
(foreach att (vlax-invoke vlaBlock 'GetAttributes)
(if (wcmatch (strcase (vla-get-tagstring att)) "LINE*")
(progn
(setq labelStr (if (< index 10) (strcat "0" (itoa index)) (itoa index)))
(setq basePt (vlax-get att 'InsertionPoint))
(setq height (vla-get-height att))
(setq rotation (vla-get-rotation att))
(setq labelPt
(list (- (car basePt) 0.05)
(+ (cadr basePt) 0.65)
(caddr basePt)))
(entmake
(list
(cons 0 "TEXT")
(cons 8 "0")
(cons 7 "WD")
(cons 62 7)
(cons 10 labelPt)
(cons 11 labelPt)
(cons 40 height)
(cons 72 1)
(cons 73 2)
(cons 1 labelStr)
(cons 50 rotation)
(cons 100 "AcDbEntity")
(cons 100 "AcDbText")
)
)
(setq index (1+ index))
)
)
)
)
(princ "\nInvalid entity passed to labelZone32Lines.")
)
(princ)
)
(defun setLayoutWireNumbers (layoutEnt zoneNumber layoutIndex / vlaBlock att tag wireNumber cblIndex layoutNum)
(if (and layoutEnt (eq (cdr (assoc 0 (entget layoutEnt))) "INSERT"))
(progn
(setq vlaBlock (vlax-ename->vla-object layoutEnt))
(setq cblIndex 5)
;; Format layout number as 3-digit string (702, 703, 704, etc.)
(setq layoutNum (itoa (+ 702 layoutIndex)))
(foreach att (vlax-invoke vlaBlock 'GetAttributes)
(setq tag (strcase (vla-get-tagstring att)))
(if (= tag "WIRENO")
(progn
;; Concatenate: zone + layoutNum + "-CBL" + cblNum
(setq wireNumber
(strcat
zoneNumber
layoutNum
"-CBL"
(if (< cblIndex 10)
(strcat "0" (itoa cblIndex))
(itoa cblIndex)
)
)
)
(vla-put-textstring att wireNumber)
(setq cblIndex (1+ cblIndex))
)
)
)
)
(princ "\nInvalid layout entity passed to setLayoutWireNumbers.")
)
(princ)
)
(defun parseCSVLine (line / pos result)
(setq result '())
(while (setq pos (vl-string-search "," line))
(setq result (append result (list (substr line 1 pos))))
(setq line (substr line (+ pos 2)))
)
(append result (list line))
)
(defun getDPMDataFromCSV ( / file filename line headers row dpm ip name deviceIP port partNumber dpmList deviceGroups currentGroup zoneRaw zoneNumber)
(setq filename (getfiled "Select CSV File" (strcat (getenv "USERPROFILE") "\\Desktop\\") "csv" 0))
(if (not filename)
(progn (princ "\nNo file selected.") (exit))
)
(setq file (open filename "r"))
(if (not file)
(progn (princ "\nFailed to open file.") (exit))
)
;; Skip metadata lines and header row (first 3 lines)
(read-line file)
(read-line file)
(read-line file)
(setq dpmList '())
(setq deviceGroups '())
(setq currentGroup '())
(setq zoneNumber "")
(while (setq line (read-line file))
(setq row (parseCSVLine line))
;; Ensure row has enough columns
(if (>= (length row) 10)
(progn
(setq dpm (nth 3 row))
(setq ip (nth 4 row))
(setq name (nth 5 row))
(setq partNumber (nth 6 row))
(setq deviceIP (nth 7 row))
(setq port (nth 8 row))
(setq zoneRaw (nth 9 row))
;; Extract zone number - preserve it as-is with leading zeros
(if (and zoneRaw (/= zoneRaw ""))
(progn
;; Extract only digits from zone string (e.g., "MCM01" -> "01")
(setq zoneNumber
(str-trim
(vl-list->string
(vl-remove-if-not
'(lambda (c) (<= 48 c 57))
(vl-string->list zoneRaw)
)
)
)
)
)
)
(if (and dpm (/= dpm ""))
(progn
(if (> (length currentGroup) 0)
(progn
(while (< (length currentGroup) 24)
(setq currentGroup
(append currentGroup
(list (list
(cons "NAME" "SPARE")
(cons "IP" "")
(cons "PORT" "")
(cons "ZONE" zoneNumber)
(cons "PARTNUM" "")
))
)
)
)
(setq deviceGroups (append deviceGroups (list currentGroup)))
(setq currentGroup '())
)
)
(if (not (assoc dpm dpmList))
(setq dpmList (append dpmList (list (cons dpm (list ip zoneNumber)))))
)
)
)
(if (and name (/= name ""))
(setq currentGroup
(append currentGroup
(list (list
(cons "NAME" name)
(cons "IP" deviceIP)
(cons "PORT" port)
(cons "ZONE" zoneNumber)
(cons "PARTNUM" partNumber)
))
)
)
)
(if (= (length currentGroup) 24)
(progn
(setq deviceGroups (append deviceGroups (list currentGroup)))
(setq currentGroup '())
)
)
)
)
)
(if (> (length currentGroup) 0)
(progn
(while (< (length currentGroup) 24)
(setq currentGroup
(append currentGroup
(list (list
(cons "NAME" "SPARE")
(cons "IP" "")
(cons "PORT" "")
(cons "ZONE" zoneNumber)
(cons "PARTNUM" "")
))
)
)
)
(setq deviceGroups (append deviceGroups (list currentGroup)))
)
)
(close file)
(list dpmList deviceGroups)
)
(defun c:init-diagrams ( / blockName count offsetX i x y basePt targetPt lastEnt dpmPair dpmName dpmIP deviceGroup dpmUpsPt dpmUpsObj tag ent vlaObj zoneName zoneNumber dpmData)
(clearDrawing)
(setq blockName "layout")
(setq csvData (getDPMDataFromCSV))
(setq dpmList (car csvData))
(setq deviceGroups (cadr csvData))
(setq count (length dpmList))
(if (and blockName (> count 0))
(progn
(setq offsetX 43.5)
(setq i 0)
(while (< i count)
(setq x (* i offsetX))
(setq y 0)
(setq basePt '(0 0 0))
(setq targetPt (list x y 0))
;; Get DPM data and zone number
(setq dpmPair (nth i dpmList))
(setq dpmName (car dpmPair))
(setq dpmData (cdr dpmPair))
(setq dpmIP (car dpmData))
(setq zoneNumber (cadr dpmData))
(setq deviceGroup (nth i deviceGroups))
;; Insert layout
(command "_.-INSERT" blockName basePt 1 1 0)
(setq lastEnt (entlast))
(if lastEnt
(progn
(vla-move
(vlax-ename->vla-object lastEnt)
(vlax-3d-point basePt)
(vlax-3d-point targetPt)
)
(setLayoutWireNumbers lastEnt zoneNumber i)
)
)
;; Insert DPM-UPS
(setq dpmUpsPt (list (+ x 16.1) (+ y 2.1173) 0))
(command "_.-INSERT" "DPM-UPS" dpmUpsPt 1 1 0)
(setq lastEnt (entlast))
(if lastEnt
(progn
(setq dpmUpsObj (vlax-ename->vla-object lastEnt))
(foreach att (vlax-invoke dpmUpsObj 'GetAttributes)
(setq tag (strcase (vla-get-tagstring att)))
(cond
((= tag "IPADDRESS") (vla-put-textstring att dpmIP))
((= tag "TAG2") (vla-put-textstring att dpmName))
)
)
)
)
;; Insert ZONE_32H
(setq desiredX (+ x 0.7658))
(setq desiredY (+ y 25.6873))
(command "_.-INSERT" "ZONE_32H" '(0 0 0) 1 1 0)
(setq ent (entlast))
(if ent
(progn
(setq vlaObj (vlax-ename->vla-object ent))
(vla-move vlaObj (vlax-3d-point 0 0 0) (vlax-3d-point desiredX desiredY 0))
(setq zoneName (strcat zoneNumber (itoa (+ 702 i)) "."))
(foreach att (vlax-invoke vlaObj 'GetAttributes)
(vla-put-textstring att zoneName)
)
(labelZone32Lines ent)
)
)
;; layout label
(setq labelPt (list (+ desiredX 14.0) 32.0 0.0))
(command "_.text" labelPt 1.5 0 (strcat "Layout " (itoa (1+ i))))
(place-enet-devices x y deviceGroup)
(setq i (1+ i))
)
(princ (strcat "\nInserted " (itoa count) " layouts side-by-side."))
)
(princ "\nInvalid input.")
)
(princ)
)