Script

Script

swankster
Enthusiast Enthusiast
989 Views
16 Replies
Message 1 of 17

Script

swankster
Enthusiast
Enthusiast

I am looking to find the syntax to use for a script that when run, would update an attribute in my titleblock (BLOCK NAME: TitleBlockInformation, attribute name: DWGNAME) and the current layout tab name with the CAD file name (XXX-D-P003-01).

 

So far I have something like this:

 

EXPERT
2
attreq
0
gatte
b
TitleBlockInformation
DWGNAME
"drawingname"
y
gatte

attreq
1
EXPERT
0

0 Likes
Accepted solutions (1)
990 Views
16 Replies
Replies (16)
Message 2 of 17

paullimapa
Mentor
Mentor

Not sure if a script file like this can identify if a block is in a specific layout

Is there a reason for not using lisp?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 17

swankster
Enthusiast
Enthusiast

I could use a lisp or have a script execute a lisp. Seems like there would be a lisp out there like this already.

0 Likes
Message 4 of 17

Sea-Haven
Mentor
Mentor

What may be easier for future reference is to use a Field in the Dwgname attribute, big hint the field value is just that drawing name. So save the updated title block in your master DWT then you never have to set again. Bedit an existing titleblock add a field, bsave, then attsync you title block dwgname should appear.

 

SeaHaven_0-1701388422105.png

 

0 Likes
Message 5 of 17

swankster
Enthusiast
Enthusiast

I can't use a field as I don't have the authority to modify the standard block. I also have some drawings with multiple tabs that I would deal with manually.

0 Likes
Message 6 of 17

Sea-Haven
Mentor
Mentor

Double posted see other post.

 

0 Likes
Message 7 of 17

paullimapa
Mentor
Mentor

*Modified as field*

ok, give this TtbNam.lsp function a try:

 

 

;;; TtbNam.lsp selects all BLOCKs Matching NAME: TitleBlockInformation, change attribute name: DWGNAME to current layout tab name with the CAD file name (XXX-D-P003-01)
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/script/m-p/12419897#M458513
(defun c:TtbNam (/ cmdecho menuecho dwgname count ss i en tabnam)
   (vl-load-com)
   ; setup environment
   (setq cmdecho (getvar "cmdecho")
         menuecho (getvar "menuecho")
         dwgname (vl-filename-base (getvar "dwgname")) ; get drawing name no extensions
         count 0
   )
   (setvar "cmdecho" 0)
   (setvar "menuecho" 0) 
  ; select all TitleBlockInformation with attributes
   (if (setq ss (ssget "X" '((0 . "INSERT") (2 . "TitleBlockInformation") (66 . 1))))
    (progn
     (repeat (setq i (sslength ss)) ; loop through selection set
      (setq en (ssname ss (setq i (1- i)))) ; get entity
      (setq tabnam (cdr(assoc 410 (entget en)))) ; get layout tab name
      (foreach itm (vlax-invoke (vlax-ename->vla-object en) 'getattributes) ; get attributes in block
         (if(= (vla-get-TagString itm) "DWGNAME") ; match found
           (progn
;            (vla-put-textstring itm (strcat tabnam "-" dwgname )) ; put layoutname & dwgname
            (vla-put-textstring itm (strcat "%<\\AcDiesel $(GETVAR,CTAB)>%-%<\\AcVar Filename \\f \"%tc3%fn2\">%")) ; put layoutname & dwgname as field
            (setq count (1+ count)) ; keep track of how many updated
           )
         )
      ) ; foreach attribute
     ) ; repeat next block   
  (vla-Regen (vla-get-activedocument (vlax-get-acad-object)) acActiveViewport) ; regen vport to refresh field
     (princ (strcat "\nUpdated Total of [" (itoa count) "] TitleBlockInformation's DWGNAME Attribute."))
    ) ; progn
    (alert"No TitleBlockInformation Blocks Found in Current Drawing.")
   ) ; if
   ; restore environment
   (setvar "menuecho" menuecho)
   (setvar "cmdecho" cmdecho)
 (princ) ; clean exit
) ; defun 
(princ "TtbNam loaded. Type TtbNam to Update All TitleBlockInformation's DWGNAME Attribute.")(princ)

 

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 17

swankster
Enthusiast
Enthusiast

Thanks. It didn't really work but I did find two other lisps that got me what I was looking for.

0 Likes
Message 9 of 17

paullimapa
Mentor
Mentor

Perhaps you can share the lisps here so we can see your solution 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 10 of 17

swankster
Enthusiast
Enthusiast
Accepted solution

Sure @paullimapa, Attached DNO.lsp will change the specified attribute to the drawing name. LNA.lsp updates the layout tab name to match the same specified attribute.

0 Likes
Message 11 of 17

paullimapa
Mentor
Mentor

Thanks for posting the solutions you found. I actually misread your OP. I thought you wanted to change the Attribute to include both the Layout name & Drawing name.

I would still recommend for DNO.lsp to change the Attribute using a FIELD this way when the Drawing name changes like if you do a SaveAs the Attribute value automatically updates:

 

; dno updates block attribute value to dwgname
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/script/m-p/12421113#M458538
(defun c:dno (/ bname tag)
 (vl-load-com)
  (setq bname "TitleBlockInformation")
  (setq tag "DWGNAME")
  (vlax-for b (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
    (vlax-for a	b
      (and (vlax-property-available-p a 'name)
	   (= (strcase bname) (strcase (vla-get-name a)))
	   (vl-some '(lambda (x)
		       (and (= (strcase tag) (strcase (vla-get-tagstring x)))
          (vla-put-textstring x "%<\\AcVar Filename \\f \"%fn2\">%") ; put dwgname as field
;			    (vla-put-textstring x (vl-filename-base (getvar 'dwgname)))
		       )
		     )
		    (vlax-invoke a 'getattributes)
	   )
      )
    )
  )
  (vla-Regen (vla-get-activedocument (vlax-get-acad-object)) acActiveViewport) ; regen vport to refresh field
  (princ)
)

 

As for the Layout name, I'm not sure why you need that to match with the Attribute value when in reality all you need is to change it to match with the Drawing name which is what you really want. This can be done using this simple line of code:

 

(if(/=(getvar"ctab")"Model")(vl-cmdf"_.Layout""_R"(getvar"ctab")(vl-filename-base (getvar 'dwgname))))

 

Lastly, I'm also not sure why you want the Layout name to match with the Drawing name. Whenever you PLOT or PUBLISH, the Drawing name is already placed as the prefix followed by the Layout name. If you rename the Layout to always match with the Drawing name then the plotted file name would show the same name twice.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 12 of 17

swankster
Enthusiast
Enthusiast

Thanks for the info. I often have a group of steel details that get brought into a project and there are numerous things that needs to happen to get them integrated into that project and these lisps are a part of that. The field option won't work on drawings with multiple tabs as the drawings are named XXX-D-P016-08-09.dwg for sheets 8 and 9, for example. The drawing names most often never change once they're established, either. After I run the DNO lisp, I edit the DWGNAME attribute to sheet 08 or 09 and then run LNA and it names my layout tabs. When publishing, the layout tab is what I see under sheet name and I don't have the "Prefix sheet title with file name" turned on.

0 Likes
Message 13 of 17

paullimapa
Mentor
Mentor

Thanks for your detailed explanation as to why you don't want to use FIELDS. 

I assume since you sometimes like the example you described where you need to edit the Attribute value after you've run the DNO.lsp that you only run DNO once after all the title blocks are setup. Otherwise everytime you run DNO it'll change every instance of the found Attribute again. Don't you want to also have a routine that just changes the Attribute value in the current Layout only?


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 14 of 17

paullimapa
Mentor
Mentor

I just had another thought on Field implementation.

First run the simple line of code I provided to automatically change the Layout name to match with the Drawing name ie: XXX-D-P016-08-09:

(if(/=(getvar"ctab")"Model")(vl-cmdf"_.Layout""_R"(getvar"ctab")(vl-filename-base (getvar 'dwgname))))

Second, Rename the Layout to what you want like: XXX-D-P016-08.

Now, run the lisp code similar to what I provided TtbNam.lsp that will create a FIELD linking the Layout name as the value in the Title Block's DWGNAME Attribute.

The advantage of this is when you make a copy of the Layout and call it say: XXX-D-P016-09 the Title Block's DWGNAME Attribute in the new layout will automatically update to match.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 15 of 17

Sea-Haven
Mentor
Mentor

Something like this, done for some one else, will do all, could add do this one layout only.

 

 

 

; update 1 attribute in all titles in all layouts
; By AlanH Nov 23

(defun c:attup ( / tagblk tagname pt blk blkname layouts layout ss obj atts att)
(setq tagblk (nentsel "\nPick a attribute "))
(setq pt (cadr tagblk))
(setq blk (ssname (ssget pt) 0))
(setq blkname (cdr (assoc 2 (entget blk))))
(setq tagname (cdr (assoc 2 (entget (car tagblk)))))
(setq str (getstring "\nEnter new text " ))
(setq layouts (layoutlist))
(foreach layout layouts
(setvar 'ctab layout)
(command "pspace")
(IF  (setq ss (ssget "X" (list (cons 0 "INSERT")(cons 410 (getvar 'ctab))(cons 2 blkname))))
(progn
(setq obj (vlax-ename->vla-object (ssname ss 0)))
(setq atts (vlax-invoke obj 'Getattributes))
(foreach att atts
(if (= (vlax-get att 'tagstring) tagname)
(vlax-put att 'textstring str)
)
)
)
)
)
(alert "all done")
(princ)
)
(c:attup)

 

 

0 Likes
Message 16 of 17

swankster
Enthusiast
Enthusiast

Seems like there's something wrong with that. After I load the lisp, the selection box pops up like it wants me to select something, even after I open a new drawing.

0 Likes
Message 17 of 17

swankster
Enthusiast
Enthusiast

Thanks for all your help but I think I am satisfied with what I have, for now. The tabbed drawings are already setup with tabs and the content is done, so I was only looking for a quick way to integrate them into the project.

0 Likes