AutoCAD script for updating an Attribute in a block

AutoCAD script for updating an Attribute in a block

francesca_boothCE7KE
Explorer Explorer
593 Views
30 Replies
Message 1 of 31

AutoCAD script for updating an Attribute in a block

francesca_boothCE7KE
Explorer
Explorer

So I've written a pythod code that sets out what I assumed would be the correct script layout for AutoCAD.

 

I've run into an issue on the Open command in AutoCAD 2025. I've changed my FILDEA from 1 to 0 and 0 back to 1 to see if that would change anything but it doesn't. When the script tries to run the OPEN command it's just stalls on either the popup or saying I need to press entre to confirm docuemtn that I want to open which in turn kind of cancelles the script.

 

I've even tried changing the open to -open.

0 Likes
594 Views
30 Replies
Replies (30)
Message 21 of 31

francesca_boothCE7KE
Explorer
Explorer

I have updated my CAD to the latest and tried in safemode and it's still the same issue, it needs me to physically press enter to open the drawing, but it then won't run the rest of the script on the opened drawing. This also means it won't try to open any other drawing (I'm currently only testing with 2 dwg's). I will give the Accorconsole.exe a go to see if that will work.

As for the testing of 2024, I have someone who can try for me, as I don't have permission at the moment to install a new CAD software on my work computer.

0 Likes
Message 22 of 31

francesca_boothCE7KE
Explorer
Explorer

When trying to do something similar to what you've written I get the below but it didn't update the one that I specified, or with the script I needed it to be updated by (I took out anything I didn't need including all other drawings)

francesca_boothCE7KE_1-1756870282710.png

 

0 Likes
Message 23 of 31

paullimapa
Mentor
Mentor

@francesca_boothCE7KE 

Update: I made a change to the Open.scr since accoreconsole cmd file already opens the dwg:

paullimapa_0-1756872080837.png

I see at least 2 problems.

First, the dwg path may be way too long for command dos box execution.

Second you're using a lisp function called (CHGATT) that's unlike the basic AutoCAD commands in the Script file you previously posted.

So to prove that the Open & the ATTEDIT command actually works in a Script file, first do the very simple tests as I stated in one of my previous replies using the three files I've already provided: A-01.dwg, open.scr & open.cmd (Update: To make the testing easier, I've attached Open.zip file containing all 3 files)

Save all three files into your Downloads folder.

Next add Downloads folder to AutoCAD's Options>Files>Support File Search Path & Trusted Locations.

paullimapa_0-1756871358539.png

Then double click to run under Downloads open.cmd 

Now do a screenshot of what the dos box window shows and share that here.

 


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

francesca_boothCE7KE
Explorer
Explorer

Ah yes the CHGATT isn't standard CAD it's something my work has specifically, that we have loaded in, someone else at work asked me to use that to also give a go as they know someone who use to work here used it to update attribute text.

0 Likes
Message 25 of 31

francesca_boothCE7KE
Explorer
Explorer

francesca_boothCE7KE_0-1756873584460.png

 

0 Likes
Message 26 of 31

paullimapa
Mentor
Mentor

Great, your screenshot proves that the Attedit command sequence works at least in the sample A-01.dwg I attached. 

paullimapa_0-1756873944812.png

Now edit the Open.cmd file and replace the Downloads\A-01.dwg with path & location of your drawing file & save the changes.

But leave the Open.scr file as is.

Next double click the revised Open.cmd.

Then do another screenshot and share that here.


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

francesca_boothCE7KE
Explorer
Explorer

I did change the open.scr a little bit to match the new block that we were just given.

 

francesca_boothCE7KE_0-1756877441776.png

 

0 Likes
Message 28 of 31

paullimapa
Mentor
Mentor

Looks like that worked!!!

Did you open the dwg file to confirm that the attribute value changed?


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

francesca_boothCE7KE
Explorer
Explorer

Yes I did, sorry for not mention, but it did change. To scale this up to do it across 502 drawings (where the change is a bit different on each drawing), do I have to change the command prompt to do each drawing individually, or can I do that within the script (open.scr.)

0 Likes
Message 30 of 31

paullimapa
Mentor
Mentor

Depends on what is "a bit different".

You can use the same script file by repeating the ATTEDIT command sequence as many times as you want searching for different values to change.

But if other searches change Attribute values you don't want then you'll need a unique script file for each drawing.

So your Open.cmd file will contain not only a unique drawing name for each line but also a unique script file.  


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

komondormrex
Mentor
Mentor

check the following. 

acet needs to be installed. 

utilizes odbx. 

(vl-catch-all-apply 'acet-load-expresstools)
(defun c:mass_change_att (/ acad_dbx_object work_directory att_value sheet_att)
  (setq acad_dbx_object (vlax-create-object (strcat "objectdbx.axdbdocument." (substr (getvar 'acadver) 1 2))))
  (setq work_directory (strcat (acet-ui-pickdir "Pick directory to process" "c:\\") "\\"))
  (foreach dwg_file (vl-directory-files work_directory "*.dwg" 1)
    (if (acet-str-find "Sh[0-9]+of[0-9]+.dwg" dwg_file t t)
      (if (null (vl-catch-all-error-p (vl-catch-all-apply 'vla-open (list acad_dbx_object (strcat work_directory dwg_file)))))
        (progn
          (setq att_value (substr (vl-filename-base dwg_file) (+ 3 (vl-string-search "Sh" dwg_file)))
              att_value (vl-string-subst " OF " "of" att_value)
          )
            (vlax-map-collection (vla-get-modelspace (vla-get-database acad_dbx_object))
              '(lambda (object) (if (and (= "AcDbBlockReference" (vla-get-objectname object))
                                (= "TL_LINE08" (vla-get-effectivename object))
                            )
                            (if (vl-some '(lambda (att) (= "SHEET" (vla-get-tagstring (setq sheet_att att))))
                                    (vlax-invoke object 'getattributes)
                              )
                                (progn
                                    (vla-put-textstring sheet_att att_value)
                                    (princ (strcat "\nFile: \"" 
                                       dwg_file 
                                       "\", attribute tagged \"SHEET\" in block \"TL_LINE08\" changed value to \"" 
                                       att_value 
                                       "\""
                                   )
                              )
                                (vla-saveas acad_dbx_object (strcat work_directory dwg_file))
                                )
                            )
                           )
               )
            )
        )
        (princ (strcat "\nFile: \"" dwg_file "\" - can't open!"))
        )
      (princ (strcat "\nFile: \"" dwg_file "\" - skipped!"))
    )
  )
  (vlax-release-object acad_dbx_object)
  (princ)
)

 

0 Likes