Rename Layout according to attributes in Titleblock.

Rename Layout according to attributes in Titleblock.

Anonymous
Not applicable
1,247 Views
17 Replies
Message 1 of 18

Rename Layout according to attributes in Titleblock.

Anonymous
Not applicable

Hi!

I have a titleblock called "KN" with attributes tags called "JN" and "NR". I am trying to create a lisp that renames the lisp automatically to  the values of "NR"_"JN", according to the titleblock on the current layout viewport, as all the titleblocks are called KN.

I have been trying the suggestions from this post, but with no success. https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/rename-layout-to-match-drawing-numbe...

0 Likes
Accepted solutions (1)
1,248 Views
17 Replies
Replies (17)
Message 2 of 18

ahmed.abdelmotey
Advocate
Advocate

try this code

you need to verify that title block exists in every layout or it will return an error

 

 

(defun c:RenameLayout()
(vl-load-com)
	(foreach lay (layoutlist)
		(setq obj (vlax-EName->vla-Object (ssname (ssget "x" (list (cons 0 "INSERT") (cons 2 "KN") (cons 410 lay))) 0)))
		(if 
			(setq lstatts (vlax-SafeArray->list (variant-Value (vla-GetAttributes obj))))
				(foreach atts lstatts 	
					(cond 
						((= (vla-Get-TagString atts) "NR")(setq a (vla-Get-TextString atts)))
						((= (vla-Get-TagString atts) "JN")(setq b (vla-Get-TextString atts)))
					)
				)
				(if 
					(not (member (strcat a "_" b) (layoutlist)))
					(command "_Layout" "r" lay (strcat a "_" b))
					(alert "Duplicate Information")		
				)
		)
	)
)

 

Ahmed Abd-Elmotey
Landscape Architect & beginner lisp programmer
0 Likes
Message 3 of 18

Anonymous
Not applicable

Thanks for the suggestion. I already tried it, but it still gives me the error "; error: bad argument type: lselsetp nil"

0 Likes
Message 4 of 18

ahmed.abdelmotey
Advocate
Advocate
this means that the layout doesn't have a block named KN
Ahmed Abd-Elmotey
Landscape Architect & beginner lisp programmer
0 Likes
Message 5 of 18

hmsilva
Mentor
Mentor

@Anonymous wrote:

Thanks for the suggestion. I already tried it, but it still gives me the error "; error: bad argument type: lselsetp nil"


Try to add a 'progn' in ahmed.abdelmotey's code

(defun c:test ()
    (vl-load-com)
    (foreach lay (layoutlist)
        (setq obj (vlax-EName->vla-Object
                      (ssname (ssget "_x" (list (cons 0 "INSERT") (cons 2 "KN") (cons 410 lay))) 0)
                  )
        )
        (if
            (setq lstatts (vlax-SafeArray->list (variant-Value (vla-GetAttributes obj))))
               (progn;<<<<
                   (foreach atts lstatts
                       (cond
                           ((= (vla-Get-TagString atts) "NR") (setq a (vla-Get-TextString atts)))
                           ((= (vla-Get-TagString atts) "JN") (setq b (vla-Get-TextString atts)))
                       )
                   )
                   (if
                       (not (member (strcat a "_" b) (layoutlist)))
                          (command "_Layout" "r" lay (strcat a "_" b))
                          (alert "Duplicate Information")
                   )
               );<<<<
        )
    )
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 6 of 18

hmsilva
Mentor
Mentor

@ahmed.abdelmotey wrote:

try this code

you need to verify that title block exists in every layout or it will return an error 


Hi ahmed.abdelmotey,

to try to avoid the error, test the data, before supply it to a function...

 

(defun c:test (/ a b lstatts obj ss)
    (vl-load-com)
    (foreach lay (layoutlist)
        (if (and (setq ss (ssget "_x" (list (cons 0 "INSERT") (cons 2 "KN") (cons 410 lay))))
                 (setq obj (vlax-EName->vla-Object (ssname ss 0)))
                 (setq lstatts (vlax-SafeArray->list (variant-Value (vla-GetAttributes obj))))
            )
            (progn
                (foreach atts lstatts
                    (cond
                        ((= (vla-Get-TagString atts) "NR") (setq a (vla-Get-TextString atts)))
                        ((= (vla-Get-TagString atts) "JN") (setq b (vla-Get-TextString atts)))
                    )
                )
                (if
                    (not (member (strcat a "_" b) (layoutlist)))
                       (command "_Layout" "r" lay (strcat a "_" b))
                       (alert "Duplicate Information")
                )
            )
        )
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 7 of 18

ahmed.abdelmotey
Advocate
Advocate
thank you for the modification 🙂
Ahmed Abd-Elmotey
Landscape Architect & beginner lisp programmer
0 Likes
Message 8 of 18

hmsilva
Mentor
Mentor

@ahmed.abdelmotey wrote:
thank you for the modification 🙂

You're welcome, ahmed.abdelmotey! 🙂

Henrique

EESignature

0 Likes
Message 9 of 18

Anonymous
Not applicable

Thanks for the edits. At first it still gave me the error, but then it got to me: my titleblock is not on the layout sheet, but in modelspace. So the titleblock is visible in the viewport. 

Is there a way to modify it so, that it would retrieve these values even if the titleblock is in modelspace and is visible on the layout via viewport. 


0 Likes
Message 10 of 18

hmsilva
Mentor
Mentor

@Anonymous wrote:

Thanks for the edits. At first it still gave me the error, but then it got to me: my titleblock is not on the layout sheet, but in modelspace. So the titleblock is visible in the viewport. 
Is there a way to modify it so, that it would retrieve these values even if the titleblock is in modelspace and is visible on the layout via viewport. 


You're welcome, karl_haha!
I have a deadline to meet, so at the moment, I don't have much free time, but I'll see what I can do.

Henrique

EESignature

0 Likes
Message 11 of 18

hmsilva
Mentor
Mentor

Unstested...

 

(defun c:demo (/ get_att _ctab a b ent hnd i layts ll lstatts msllpt msurpt obj psllpt psurpt ss ss1 ur vlaobj vp)
    (defun get_att (lst tag)
        (vl-some '(lambda (x)
                      (if (= (vla-get-tagstring x) (strcase tag))
                          (vla-get-textstring x)
                      )
                  )
                 lst
        )
    )
    (if (and (= 0 (getvar "tilemode")) (>= (getvar "cvport") 2))
        (command "_.pspace")
    )
    (setq _ctab (getvar 'CTAB))
    (setq layts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
    (foreach lay (layoutlist)
        (setvar 'CTAB lay)
        (if (and (= 0 (getvar "tilemode")) (>= (getvar "cvport") 2))
            (command "_.pspace")
        )
        (command "_.zoom" "_E")
        (if (setq ss1 (ssget "_X" (list '(0 . "VIEWPORT") (cons 410 lay) (cons -4 "!=") (cons 69 1))))
            (progn
                (repeat (setq i (sslength ss1))
                    (setq hnd    (ssname ss1 (setq i (1- i)))
                          ent    (entget hnd)
                          vp     (cdr (assoc 69 ent))
                          VlaObj (vlax-ename->vla-object hnd)
                    )
                    (vlax-invoke-method VlaObj 'GetBoundingBox 'll 'ur)
                    (setq psllpt (vlax-safearray->list ll)
                          psurpt (vlax-safearray->list ur)
                          msllpt (trans psllpt 3 2)
                          msurpt (trans psurpt 3 2)
                    )
                    (if (not (and (= 0 (getvar "tilemode")) (>= (getvar "cvport") 2)))
                        (command "_.mspace")
                    )
                    (setvar 'CVPORT vp)
                    (if (setq ss (ssget "_C" msllpt msurpt (list (cons 0 "INSERT") (cons 2 "KN"))))
                        (if (and (setq obj (vlax-EName->vla-Object (ssname ss 0)))
                                 (setq lstatts (vlax-invoke obj "GetAttributes"))
                                 (setq a (get_att lstatts "NR"))
                                 (setq b (get_att lstatts "JN"))
                            )
                            (if
                                (not (vl-position (strcat a "_" b) (layoutlist)))
                                   (vla-put-name (vla-item layts lay) (strcat a "_" b))
                            )
                        )
                    )
                )
                (command "_.pspace")
            )
        )
    )
    (setvar 'CTAB _ctab)
    (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 12 of 18

ahmed.abdelmotey
Advocate
Advocate
without lisp you can create a viewport with the whole size of our paper
make it active and then command : CHSPace and then select your title block
it will be moved to paper space and then the problem shud be solved
Ahmed Abd-Elmotey
Landscape Architect & beginner lisp programmer
0 Likes
Message 13 of 18

Anonymous
Not applicable

Hei Henrique,
thanks for your help.
Sadly this lisp doesn't work. It gives no errors, but it also doesn't change anything. I believe the problem is that it doesn't find the titleblock.
I found this post: http://www.cadtutor.net/forum/showthread.php?60972-extract-data-seen-in-viewport-only-paperspace-dat...
There's a lisp that selects objects only visible in the active viewport. Perhaps it can help?

Ahmed, inresponse to your solution, I need the titleblock to be in model space because it is very hard to identify the drawings without the titleblock as usually I have more than 10 drawings in one file.


0 Likes
Message 14 of 18

hmsilva
Mentor
Mentor

@Anonymous wrote:

Hei Henrique,
thanks for your help.
Sadly this lisp doesn't work. It gives no errors, but it also doesn't change anything.
...


Could you please attach a sample dwg with some titleblocks?

 

Henrique

EESignature

0 Likes
Message 15 of 18

Anonymous
Not applicable

Here you go:

I created a layout as well.

0 Likes
Message 16 of 18

hmsilva
Mentor
Mentor
Accepted solution

Hi Karl,

the titleblock name is "KN_KONSTR" not "KN"...

 

Try this quickly revised code

 

(defun c:demo (/ get_att _ctab a b ent hnd i layts ll lstatts msllpt msurpt obj psllpt psurpt ss ss1 targ ur vlaobj vp)
   (defun get_att (lst tag)
      (vl-some '(lambda (x)
                   (if (= (vla-get-tagstring x) (strcase tag))
                      (vla-get-textstring x)
                   )
                )
               lst
      )
   )
   (if (and (= 0 (getvar "tilemode")) (>= (getvar "cvport") 2))
      (command "_.pspace")
   )
   (setq _ctab (getvar 'CTAB))
   (setq layts (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
   (foreach lay (layoutlist)
      (setvar 'CTAB lay)
      (if (and (= 0 (getvar "tilemode")) (>= (getvar "cvport") 2))
         (command "_.pspace")
      )
      (command "_.zoom" "_E")
      (if (setq ss1 (ssget "_X" (list '(0 . "VIEWPORT") (cons 410 lay) (cons -4 "!=") (cons 69 1))))
         (progn
            (repeat (setq i (sslength ss1))
               (if (and (= 0 (getvar "tilemode")) (>= (getvar "cvport") 2))
                  (command "_.pspace")
               )
               (setq hnd    (ssname ss1 (setq i (1- i)))
                     ent    (entget hnd)
                     vp     (cdr (assoc 69 ent))
                     VlaObj (vlax-ename->vla-object hnd)
               )
               (vlax-invoke-method VlaObj 'GetBoundingBox 'll 'ur)
               (if (not (and (= 0 (getvar "tilemode")) (>= (getvar "cvport") 2)))
                  (command "_.mspace")
               )
               (setvar 'CVPORT vp)
               (setq psllpt (vlax-safearray->list ll)
                     psurpt (vlax-safearray->list ur)
                     targ   (vlax-get VlaObj "Target")
                     msllpt (mapcar '+ (trans psllpt 3 2) targ)
                     msurpt (mapcar '+ (trans psurpt 3 2) targ)
               )
               (if (setq ss (ssget "_C" msllpt msurpt (list (cons 0 "INSERT") (cons 2 "KN_KONSTR"))))
                  (if (and (setq obj (vlax-EName->vla-Object (ssname ss 0)))
                           (setq lstatts (vlax-invoke obj "GetAttributes"))
                           (setq a (get_att lstatts "NR"))
                           (setq b (get_att lstatts "JN"))
                      )
                     (if
                        (not (vl-position (strcat a "_" b) (layoutlist)))
                          (progn
                             (vla-put-name (vla-item layts lay) (strcat a "_" b))
                             (setq lay (strcat a "_" b))
                          )
                     )
                  )
               )
            )
            (command "_.pspace")
         )
      )
   )
   (setvar 'CTAB _ctab)
   (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 17 of 18

Anonymous
Not applicable

Thanks Henrique, this works brilliantly. 🙂

0 Likes
Message 18 of 18

hmsilva
Mentor
Mentor

@Anonymous wrote:

Thanks Henrique, this works brilliantly. 🙂


You're welcome, karl_haha
Glad I could help!

Henrique

EESignature

0 Likes