AutoLisp loop not selecting block when looping

AutoLisp loop not selecting block when looping

Anonymous
Not applicable
812 Views
5 Replies
Message 1 of 6

AutoLisp loop not selecting block when looping

Anonymous
Not applicable

Hi,

I am very new to this and have got something that almost works but just one hiccup. I've got an excel spreadsheet to update the attribute block in multiple layouts but have to select the block for each layout for it to work.

Whenever I run the AutoLisp, it will prompt me to select my block individually as it loops between the layout pages. I just want it to automatically select the block as they are named the same.

Could anyone please help me on this? I have no knowledge in writing the code and can only make a little sense of it. I managed to get this far by just combining a few things I found. Hoping to slowly learn more.

 

 

(defun c:upd (/ _delFinder ss e a data b attrvalues)

(foreach layout (layoutlist)
  (setvar "ctab" layout)
  
  
;;;	pBe 21July2014	;;; 
  (defun _delFinder (str md / d l str)
    (while (setq d (vl-string-position md str nil T))
      (setq l	(cons (substr str (+ 2 d)) l)
	    str	(substr str 1 d)
      )
    )
    (cons str l)
  )
  (if (and (setq a    nil
		 data nil
		 ss   (ssget
			"_:L:S"
			'((0 . "INSERT") (2 . "INDESCO_A1A,`*U*"))
		      )
	   )
	   (eq (vla-get-effectivename
		 (setq e (vlax-ename->vla-object (ssname ss 0)))
	       )
	       "INDESCO_A1A"
	   )
	   (setq fn (findfile "RootData.csv"))
      )
    (progn
      (setq of (open fn "r"))
      (while (setq a (read-line of))
	(setq data (cons (_delFinder a 44) data))
      )
      (close of)
      (setq attrvalues
	     (vl-remove-if
	       '(lambda	(v)
		  (member (Car v)
			  '("LOCATION_ID"
			    "PIPE_SIZE"
			    "LENGTH_(IN.)"
			    "FINISH"
			   )
		  )
		)
	       (mapcar '(lambda	(at)
			  (list	(strcase (vla-get-tagstring at))
				(vla-get-textstring at)
				at
			  )
			)
		       (vlax-invoke e 'Getattributes)
	       )
	     )
      )
      (if (setq b (assoc (cadr (assoc "DRAWING_NO" attrvalues)) data))
	(mapcar	'(lambda (j h)
		   (vla-put-textstring (last j) h)
		 )
		(cdr attrvalues)
		(cdr b)
	)
      )
    )
  )
  (princ)

  );foreach
)
(vl-load-com)

 

0 Likes
813 Views
5 Replies
Replies (5)
Message 2 of 6

ВeekeeCZ
Consultant
Consultant

You didn't post any code to see...

Anyway, did you try THIS Lee's code to update titleblocks?

0 Likes
Message 3 of 6

Anonymous
Not applicable

My bad. Please see the the code:

 

 

(defun c:upd (/ _delFinder ss e a data b attrvalues)

(foreach layout (layoutlist)
(setvar "ctab" layout)


;;; pBe 21July2014 ;;;
(defun _delFinder (str md / d l str)
(while (setq d (vl-string-position md str nil T))
(setq l (cons (substr str (+ 2 d)) l)
str (substr str 1 d)
)
)
(cons str l)
)
(if (and (setq a nil
data nil
ss (ssget
"_:L:S"
'((0 . "INSERT") (2 . "INDESCO_A1A,`*U*"))
)
)
(eq (vla-get-effectivename
(setq e (vlax-ename->vla-object (ssname ss 0)))
)
"INDESCO_A1A"
)
(setq fn (findfile "RootData.csv"))
)
(progn
(setq of (open fn "r"))
(while (setq a (read-line of))
(setq data (cons (_delFinder a 44) data))
)
(close of)
(setq attrvalues
(vl-remove-if
'(lambda (v)
(member (Car v)
'("LOCATION_ID"
"PIPE_SIZE"
"LENGTH_(IN.)"
"FINISH"
)
)
)
(mapcar '(lambda (at)
(list (strcase (vla-get-tagstring at))
(vla-get-textstring at)
at
)
)
(vlax-invoke e 'Getattributes)
)
)
)
(if (setq b (assoc (cadr (assoc "DRAWING_NO" attrvalues)) data))
(mapcar '(lambda (j h)
(vla-put-textstring (last j) h)
)
(cdr attrvalues)
(cdr b)
)
)
)
)
(princ)

);foreach
)
(vl-load-com)

0 Likes
Message 4 of 6

dbhunia
Advisor
Advisor

Try this.....

 

(defun c:upd (/ _delFinder ss e a data b attrvalues)

(foreach layout (layoutlist)
  (setvar "ctab" layout)
  
  
;;;	pBe 21July2014	;;; 
  (defun _delFinder (str md / d l str)
    (while (setq d (vl-string-position md str nil T))
      (setq l	(cons (substr str (+ 2 d)) l)
	    str	(substr str 1 d)
      )
    )
    (cons str l)
  )
  (if (and (setq a    nil
		 data nil
		 ss   (ssget "_A" (list '(0 . "INSERT") '(2 . "INDESCO_A1A,`*U*")(cons 410 (getvar "ctab")))))
	   )
	   (eq (vla-get-effectivename
		 (setq e (vlax-ename->vla-object (ssname ss 0)))
	       )
	       "INDESCO_A1A"
	   )
	   (setq fn (findfile "RootData.csv"))
      )
    (progn
      (setq of (open fn "r"))
      (while (setq a (read-line of))
	(setq data (cons (_delFinder a 44) data))
      )
      (close of)
      (setq attrvalues
	     (vl-remove-if
	       '(lambda	(v)
		  (member (Car v)
			  '("LOCATION_ID"
			    "PIPE_SIZE"
			    "LENGTH_(IN.)"
			    "FINISH"
			   )
		  )
		)
	       (mapcar '(lambda	(at)
			  (list	(strcase (vla-get-tagstring at))
				(vla-get-textstring at)
				at
			  )
			)
		       (vlax-invoke e 'Getattributes)
	       )
	     )
      )
      (if (setq b (assoc (cadr (assoc "DRAWING_NO" attrvalues)) data))
	(mapcar	'(lambda (j h)
		   (vla-put-textstring (last j) h)
		 )
		(cdr attrvalues)
		(cdr b)
	)
      )
    )
  )
  (princ)

  );foreach
)
(vl-load-com)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 5 of 6

dbhunia
Advisor
Advisor

Sorry it my mistake, Try this.......

 

(defun c:upd (/ _delFinder ss e a data b attrvalues)

(foreach layout (layoutlist)
  (setvar "ctab" layout)
  
  
;;;	pBe 21July2014	;;; 
  (defun _delFinder (str md / d l str)
    (while (setq d (vl-string-position md str nil T))
      (setq l	(cons (substr str (+ 2 d)) l)
	    str	(substr str 1 d)
      )
    )
    (cons str l)
  )
  (if (and (setq a    nil
		 data nil
		 ss   (ssget
			"_A"
			(list '(0 . "INSERT") '(2 . "INDESCO_A1A,`*U*") (cons 410 (getvar "ctab")))
		      )
	   )
	   (eq (vla-get-effectivename
		 (setq e (vlax-ename->vla-object (ssname ss 0)))
	       )
	       "INDESCO_A1A"
	   )
	   (setq fn (findfile "RootData.csv"))
      )
    (progn
      (setq of (open fn "r"))
      (while (setq a (read-line of))
	(setq data (cons (_delFinder a 44) data))
      )
      (close of)
      (setq attrvalues
	     (vl-remove-if
	       '(lambda	(v)
		  (member (Car v)
			  '("LOCATION_ID"
			    "PIPE_SIZE"
			    "LENGTH_(IN.)"
			    "FINISH"
			   )
		  )
		)
	       (mapcar '(lambda	(at)
			  (list	(strcase (vla-get-tagstring at))
				(vla-get-textstring at)
				at
			  )
			)
		       (vlax-invoke e 'Getattributes)
	       )
	     )
      )
      (if (setq b (assoc (cadr (assoc "DRAWING_NO" attrvalues)) data))
	(mapcar	'(lambda (j h)
		   (vla-put-textstring (last j) h)
		 )
		(cdr attrvalues)
		(cdr b)
	)
      )
    )
  )
  (princ)

  );foreach
)
(vl-load-com)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 6 of 6

pbejse
Mentor
Mentor

@Anonymous wrote:

Hi,

 

Could anyone please help me on this? I have no knowledge in writing the code and can only make a little sense of it. I managed to get this far by just combining a few things I found. Hoping to slowly learn more.

 

1. We wont use (layoutlist) 

2. We put the name of the TAG with the value after reading the CSV

3. We will use the layout tab name to identify what values go with block "INDESCO_A1A"

[ Note: Suggest to use field value CTAB for "DRAWING_NO" tag. if you decide to go with that, we will need to change one item on the code ]

This

 

(setq f (assoc (vla-get-tagstring at) Found))

To this

 

 

(setq f (assoc (vla-get-tagstring at) (Cdr Found)))

Here you go [ using layout name as an identifier 

 

(defun c:upd (/ _delFinder fn ss e i of a data found layoutname)

;;;	pBe 21July2019	;;;   

(defun _delFinder (str md / d l str)
    (while (setq d (vl-string-position md str nil T))
      (setq l	(cons (substr str (+ 2 d)) l)
	    str	(substr str 1 d)
      )
    )
    (cons str l)
  )

	(if
          (and
            (setq data nil  fn (findfile "RootData.csv"))
            (setq ss  (ssget "_X" '((0 . "INSERT") (2 . "INDESCO_A1A,`*U*")(410 . "~Model")))
		      )
              )
          (progn

	;;;		Read the CSV file			;;;
            
      		(setq of (open fn "r"))
		      (while (setq a (read-line of))
			(setq data (cons (_delFinder a 44) data))
		      )
      		(close of)
            
	;;;		Combine Tag and Value			;;;
            
            	(setq data (reverse data) tag  (car data) data  (Cdr data))
		(setq data (mapcar '(lambda (v)
                   		(mapcar 'list  tag v )) data))

	;;;	Modify block as per layout tab name		;;;
                       	
            (repeat (setq i (sslength ss))              
              (setq layoutname (cdr (assoc 410 (entget
                                                 (setq e (ssname ss (setq i (1- i))))))))
              
              	(if (and
                      (eq (vla-get-effectivename
                            (setq e (vlax-ename->vla-object e))) "INDESCO_A1A")
                      (setq Found (vl-some '(lambda (At)
                                  (if (assoc layoutname (mapcar 'reverse at))
						At)) data))
                      )
                  (Foreach at (vlax-invoke e 'Getattributes)
			  (if (setq f (assoc (vla-get-tagstring at) Found))
                            	(vla-put-textstring at (cadr f)))
			) 
	       	)
              )
            )
          )
            (princ)
            )

HTH