- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Im trying to add 5 or 6-digit numeric ID value found in multileaders text then try to match it with the closest polyline based on arrow head location or insertion add it to the layer name of that line or polyline as a suffix, any help would be appreciated.
(defun c:MovePolylinesToLayer (/ ssMleaders ssEntities i Mle MleText layerName allEntities closestEnt entPos leaderVertices)
;; Function to find the closest entity to a point
(defun getClosestEntity (point entities / closestEnt closestDist dist entPos)
(setq closestEnt nil
closestDist nil)
(foreach ent entities
(setq entPos (getEntityPosition ent))
(if entPos
(progn
(setq dist (distance point entPos))
(if (or (not closestDist) (< dist closestDist))
(setq closestEnt ent
closestDist dist)
)
)
)
)
closestEnt
)
;; Function to get the position of the entity
(defun getEntityPosition (ent)
(cond
((= (vla-get-ObjectName ent) "AcDbLine") (vlax-get ent 'StartPoint))
((or (= (vla-get-ObjectName ent) "AcDbPolyline")
(= (vla-get-ObjectName ent) "AcDb2dPolyline"))
(vlax-curve-getPointAtParam ent 0.0))
(t nil)
)
)
;; Function to get leader line vertices from MULTILEADER using DXF group code 10
(defun getLeaderVertices (Mle / entData vertices)
(setq entData (entget (vlax-vla-object->ename Mle)))
(setq vertices (mapcar 'cdr (vl-remove-if-not
'(lambda (pair) (= (car pair) 10))
entData)))
(if (null vertices)
(progn
(prompt "\nError: No leader line vertices found.")
nil
)
vertices
)
)
;; Main routine
(if (and (setq ssMleaders (ssget '((0 . "MULTILEADER"))))
(setq ssEntities (ssget '((0 . "LWPOLYLINE,POLYLINE,LINE"))))
(> (sslength ssMleaders) 0)
(> (sslength ssEntities) 0))
(progn
(repeat (setq i (sslength ssMleaders))
(setq Mle (vlax-ename->vla-object (ssname ssMleaders (setq i (1- i)))))
(setq MleText (vla-get-TextString Mle))
;; Construct layer name from MULTILEADER text
(setq layerName (strcat "UID-" (vl-string-translate " " "_" MleText)))
;; Create a new layer if it does not exist
(if (null (tblsearch "Layer" layerName))
(entmake (append
(list (cons 0 "LAYER")
(cons 100 "AcDbSymbolTableRecord")
(cons 100 "AcDbLayerTableRecord")
(cons 2 layerName)
(cons 70 0)
(cons 62 7)) ;; Default layer color (white)
))
)
;; Get the leader line vertices
(setq leaderVertices (getLeaderVertices Mle))
(if (and leaderVertices (not (equal leaderVertices '())))
(progn
(setq entPos (car leaderVertices)) ;; Get the first vertex
(prompt (strcat "\nLeader position: " (vl-prin1-to-string entPos)))
;; Find all entities (polylines and lines)
(setq allEntities (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssEntities)))))
;; Find the closest entity to the MULTILEADER
(setq closestEnt (getClosestEntity entPos allEntities))
;; If a closest entity is found, move it to the layer
(if closestEnt
(progn
(vla-put-Layer closestEnt layerName)
(prompt (strcat "\nMoved entity to layer: " layerName))
)
(prompt "\nNo closest entity found.")
)
)
(prompt "\nError: No leader line vertices found.")
)
)
)
(prompt "\nNo multileaders or entities found.")
)
(princ "\nMovePolylinesToLayer command completed.")
(princ)
)
(princ "\nMovePolylinesToLayer command loaded.")
(princ)
Im getting this error when running my lisp , any help would be greatly appreciated>
getting this error Leader position: (335650.0 4.85079e+06 0.0); error: Automation Error. Key not found
@komondormrex , any help is much appreciated.
Solved! Go to Solution.