Add Polylines To Block as Different Visibility States

Add Polylines To Block as Different Visibility States

Lawrence_Viaud
Explorer Explorer
271 Views
2 Replies
Message 1 of 3

Add Polylines To Block as Different Visibility States

Lawrence_Viaud
Explorer
Explorer

I’m currently working on an lisp routine to automate the creation of a dynamic block from polylines obj and assign visibility states using corresponding text objects. The goal is to:

1.Select multiple closed polylines in the drawing.

2.get the geometric center of each polyline for proper alignment.

3.select a set of single-line text objects, which will be used as names for the visibility states.

4.create a dynamic block containing all selected polylines, with each polyline assigned to a visibility state named after its corresponding text object.

 

challenges:

figuring out how to add selections to block as visibility states

current approach:

heres my code i have so far:

 

(defun c:CreateDynBlock	(/	    ssPlines   ssTexts	  plineList
			 textList   blkName    blkObj	  blkRef
			 index	    plObj      visState
			)
  ;; Select closed polylines and text objects
  (setq ssPlines (ssget "_X" '((0 . "LWPOLYLINE") (70 . 1))))
  (setq ssTexts (ssget "_X" '((0 . "TEXT"))))

  ;; Debugging outputs
  (princ (strcat "\nNumber of Polylines selected: "
		 (itoa (if ssPlines
			 (sslength ssPlines)
			 0
		       )
		 )
	 )
  )
  (princ (strcat "\nNumber of Text objects selected: "
		 (itoa (if ssTexts
			 (sslength ssTexts)
			 0
		       )
		 )
	 )
  )

  ;; Ensure valid selections
  (if (or (not ssPlines) (not ssTexts))
    (progn
      (princ
	"\nError: You must select closed polylines and text objects."
      )
      (exit)
    )
  )

  ;; Convert selections to lists
  (setq plineList (mapcar 'car (mapcar 'cdr (ssnamex ssPlines))))
  (setq	textList
	 (vl-remove-if
	   'null
	   (mapcar
	     (lambda (e)
	       (cdr (assoc 1 (entget e)))
	     )
	     (mapcar 'cdr (ssnamex ssTexts))
	   )
	 )
  )

  ;; Debugging: Print first few text values
  (princ "\nFirst few text values: ")
  (mapcar (lambda (txt) (princ (strcat "\n- " txt))) textList)

  ;; Ensure equal number of polylines and texts
  (if (/= (length plineList) (length textList))
    (progn
      (princ
	"\nError: The number of polylines and text objects must be the same."
      )
      (exit)
    )
  )

  

 

  • Any advice on handling visibility states in dynamic blocks more effectively using lisp?

  • Any advice on improving program

*Attached is also the file containing the polylines and text objects

0 Likes
Accepted solutions (1)
272 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

You can create a regular block definition with (LISP/VBA/.NET API...), but you can not add dynamic properties to the block definition to make it a dynamic block definition. No commands/APIs are exposed to do that. Defining dynamic properties are purely manual process, AFAIK.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

Lawrence_Viaud
Explorer
Explorer

Okay thank you, that makes sense.

0 Likes