Attribute update use with excel file

Attribute update use with excel file

Anonymous
Not applicable
1,322 Views
10 Replies
Message 1 of 11

Attribute update use with excel file

Anonymous
Not applicable

Hi guys,

        I want VBA macro for attribute update with help of excel document , update the drawings with the batch type .The multiple drawings need to update . please give the VBA script for that .

 

Thank you.

0 Likes
1,323 Views
10 Replies
Replies (10)
Message 2 of 11

grobnik
Collaborator
Collaborator

Hi, @Anonymous 
Could you share the dwg and excel too?
It will be more easy to do.
However I'll show you some tricks, in a nex post.

0 Likes
Message 3 of 11

JTBWorld
Advisor
Advisor

See http://help.autodesk.com/view/OARX/2021/ENU/?guid=GUID-2133E7A1-44FA-4F69-A79D-1220B889CA06


Jimmy Bergmark
JTB World - Software development and consulting for CAD and license usage reports
https://jtbworld.com

0 Likes
Message 4 of 11

grobnik
Collaborator
Collaborator

Hi @Anonymous ,

here below a simple code for extracting attributes from a block, and transfer to excel.

If Excel it's not opened the procedure open a new Excel session with an empty worksheet.

For doing the opposite I'll show you how to do, it's more or less the same way but reverse, you should be able to get value from excel cell and write in the correct position inside Attribute array coming from the previously extracted and transferred to Excel.

First of all you have to change inside the procedure the block name (now called "A1", "A2" and "A3"), later if selected block has attributes these will be stored in LISTA_ATTRIBUTI array variable.

There are also some check on Attribute Value ( value "-"), specific for my job, should be bypassed.

Inside LISTA_ATTRIBUTI you will have inside the Textstring property the attribute value, and inside TAGSTRING you will have the attribute "NAME", for changing it you have to know the position and write a code like LISTA_ATTRIBUTI (1).textstring=".....Excel cell Value...." '  (example to have cell value wrks.Cells (ROW, COLUMN).value).

 

Sub ESPORTA_ATTRIBUTI_BLOCCHI_P_AND_I()

Dim wrkb As Excel.Workbooks                                                                 ' Attribuzione della variabile "Cartella di lavoro Excel"
Dim wrks As Excel.Worksheet                                                                 ' Attribuzione della variabile "Foglio di lavoro"

RIGA = 2
COLONNA = 2

On Error Resume Next

Set objExcel = GetObject(, "Excel.Application")                                             ' Attribusce alla variabile "objExcel" l'applicazione Excel

If Err.Number > 0 Then
    Set objExcel = CreateObject("Excel.Application")
End If

objExcel.Visible = True                                                                     ' Rende visivile Excel
Set wrkb = objExcel.Workbooks.Add                                                           ' Attribuisce alla variabile wrkb la cartella di lavoro Excel corrente
Set wrks = objExcel.ActiveSheet                                                             ' Attribuisce alla variabile wrks il foglio di lavoro

objExcel.Application.ScreenUpdating = False

wrks.Cells(RIGA, COLONNA) = "PROGRESSIVO BLOCCO"
wrks.Cells(RIGA, COLONNA + 1) = "NOME LAYER"
wrks.Cells(RIGA, COLONNA + 2) = "NOME EFFETTIVO"
'wrks.Range("D1") = "X"
'wrks.Range("E1") = "Y"
'wrks.Range("F1") = "Z"
wrks.Cells(RIGA, COLONNA + 3) = "TAG"
wrks.Cells(RIGA, COLONNA + 4) = "LOOP"
wrks.Cells(RIGA, COLONNA + 5) = "DESCRIPTION"
wrks.Cells(RIGA, COLONNA + 6) = "I/O"

wrks.Name = "Importazione"                                                                  ' Nuova denominazione "Foglio1"
objExcel.Sheets("Foglio2").Delete                                                           ' Eliminazione del "Foglio2" in eccesso
objExcel.Sheets("Foglio3").Delete                                                           ' Eliminazione del "Foglio3" in eccesso

Dim VETTORE() As Variant
i = 1

ReDim Preserve VETTORE(NUMERO, 4)

For Each ENTITY In ThisDrawing.ModelSpace                                                   ' Per ogni entità nello spazio modello
    If ENTITY.ObjectName = "AcDbBlockReference" Then                                        ' Se l'entità è un blocco AUTOCAD
        If ENTITY.EffectiveName = "A1" Or ENTITY.EffectiveName = "A2" Or ENTITY.EffectiveName = "A3" Or ENTITY.EffectiveName = "D1" Then                                                   ' Se il blocco si chiama come il valore impostato nelle variabile "BN"
            If ENTITY.HasAttributes = True Then                                             ' Se il blocco ha attributi
                LISTA_ATTRIBUTI = ENTITY.GetAttributes                                      ' Inserisce nell'array "LISTA_ATTRIBUTI" tutti gli attributi del blocco
                PUNTO_INSERIMENTO = ENTITY.InsertionPoint                                   ' Coordinate del punto di inserimento del blocco selezionato --> PUNTO_INSERIMENTO è una matrice "X", "Y", "Z"
                If LISTA_ATTRIBUTI(0).TextString = "-" And LISTA_ATTRIBUTI(1).TextString = "-" And LISTA_ATTRIBUTI(2).TextString = "-" And LISTA_ATTRIBUTI(3).TextString = "-" Then
                    
                Else
                    wrks.Cells(RIGA + 2, COLONNA) = ENTITY.Name                             ' Nome blocco
                    wrks.Cells(RIGA + 2, COLONNA + 1) = ENTITY.Layer                        ' Layer blocco
                    wrks.Cells(RIGA + 2, COLONNA + 2) = ENTITY.EffectiveName                ' Nome effettivo blocco
                    'wrks.Range("D" & RIGA) = PUNTO_INSERIMENTO(0)
                    'wrks.Range("E" & RIGA) = PUNTO_INSERIMENTO(1)
                    'wrks.Range("F" & RIGA) = PUNTO_INSERIMENTO(2)
                    wrks.Cells(RIGA + 2, COLONNA + 3) = LISTA_ATTRIBUTI(0).TextString       ' Scrittura attributo N.1 - TAG
                    wrks.Cells(RIGA + 2, COLONNA + 4) = LISTA_ATTRIBUTI(1).TextString       ' Scrittura attributo N.2 - LOOP
                    wrks.Cells(RIGA + 2, COLONNA + 5) = LISTA_ATTRIBUTI(2).TextString       ' Scrittura attributo N.3 - DESCRIPTION
                    wrks.Cells(RIGA + 2, COLONNA + 6) = LISTA_ATTRIBUTI(3).TextString       ' Scrittura attributo N.4 - I/O
                    RIGA = RIGA + 1
                    i = i + 1
                End If
            End If
        End If
        End If
Next
End sub

 

I'm sorry but inside the code there are comments in ITALIAN language.

You asked also for apply the procedure on a lot of drawing, I'll show you later how I managed this issue in past.

Let me know.

Bye

0 Likes
Message 5 of 11

grobnik
Collaborator
Collaborator

Concerning the loop for open, do what you need, save and close several drawings, you can try to store the drawings list into a txt file, use the contents as filename to open and save with new or same drawing name, here below a procedure more or less easy, based upon old "DOS" command for creating the list of dwg file name to be open (if this is not still available):

  • Open a MS Windows command windows typing "CMD" con windows command bar,
  • Change path where your drawing are stored, typing "CD XXXX" (where XXX it's the directory name where your dwg are stored).
  • Then type dir *.DWG >FILENAME.TXT (see dwg.txt in the below code) if everything is correct you will have a text file with the list of dwg to be modify.
  • The text file shall be purged by other unwanted information like the file length, the last saved data, and so on (see image below). You can use a text editor for doing this purging operations, like notepad by MSWindows or something of more advanced that allows you to select a block in vertical mode instead only in horizontal (see for example TextPad software https://www.textpad.com/home should be free of charge).
  • image.png

     

  • On the opposite there are some software able to produce a list of file already "clean", see Directory List & Print (Pro) https://www.infonautics-software.ch/directorylistprint/ so you can export the list directly as text file, excel and so on (should be free of charge).
  • Please note that txt file, containing the list of drawing, shall not be delimited type, but each row shall contain the file name, including extension .dwg

and loop by the following code:

Sub MyFileOpenLoop()
Dim MyPath As String
Dim MyFileName As String
MyPaht = "C:\Users\IO\Documents\" 'your path could change, insert here yuor path including last backslash char.

Open MyPath & "dwg.txt" For Input As #1 'dwg.txt it's the list of file name to open as drawing.
Do While Not (EOF(1))
    Input #1, MyFileName
    Debug.Print MyFileName
    Application.Documents.Open MyPath & MyFileName 'Open drawing

    'Do Something here with the file opened

    ThisDrawing.SaveAs MyPath & "NewFileName.DWG" 'save drawing with another name
    ThisDrawing.Close 'close the drawing
Loop 'loop on the next file drawing
Close #1
End Sub

of course there should be added some controls like to check if file is open or some others detail.

Bye

0 Likes
Message 6 of 11

Anonymous
Not applicable

Cad and excel file is attached , i am new in VBA MACRO CAD so help for this thank you so much.

0 Likes
Message 7 of 11

grobnik
Collaborator
Collaborator

@Anonymous Thank you I'll have a look and I give you a simplified code.

Bye

0 Likes
Message 8 of 11

grobnik
Collaborator
Collaborator

Hi @Anonymous ,

just a question about the Excel File, which, as I understand shall contain info to be updated in drawing's Title Block.

The question is, how to determinate if title block shall be update or not, due to inside the excel file there are some blank field and others containing information, on the opposite on drawing all attribute fields are filled.

An option could be:

  • if Excel Row contain data, will be substituted on drawing, if any data are available on excel cell, the title block attributes will be not updated.

What do you think about that ?

Thank you, bye

0 Likes
Message 9 of 11

grobnik
Collaborator
Collaborator

Hi @Anonymous 

here attached the code and Excel file.

I'll Explain how to run the procedure:

  1. Open the Excel file (attached MyAttributes.xlsx)
  2. Fill Inside the Excel file the field related to block attribute to modify, please note that the field PF_CHANGE_REV_2 to 8 it has been converted in text cell, due to attribute value require a format like "01", "02" etc. As the previous message, all cells not empty will be considered as attribute to be modified into drawing if cell left blank the attribute value will be not modified.
  3. Attributes that will be modified are actually only related to your indications "HERE NEED TO UPDATE THE ATTRIBUTE" in a next future you can modify the procedure and adding other values already available on Excel.
  4. Please check the drawing name placed in the last Excel file column, these shall be exactly the same of that stored in the computer, without dwg extension. Fill with all drawing filename to be modified.
  5. Keep the excel file opened, will be used by Autocad.
  6. Open new empty drawing in Autocad.
  7. Save attached project (dvb file extension) in your folder, load the procedure in Autocad as project (MANAGE ->LOAD APPLICATION ->Browse your path.  
  8. Press Visual Basic Editor on Autocad, double click on Module1 and check the row code with MyPath = "C:\Users\IO\Documents\VBA Test file\" 'your path could change, Modify there your path including last backslash char as per your own path where drawing are stored. It's important for open drawings, if not you got an error.
  9. Inside Visual Basic Editor click on Tools ->Reference and click on Microsoft Excel and Microsoft Office refence library (see picture below, if you have office or excel different release from .16 you will find a different number after the dot, select it in any case). If not selected the procedure could not run well.
  10. grobnik_1-1608717891565.png
  11. Run the procedure with grobnik_2-1608718205138.png

     

  12. Procedure should open the first indicated file drawing listed in Excel, change the Attributes as required, save the file with own file drawing name adding _UPD and dwg extension.
  13. File will be closed after modification, and opened the second one in the list.
  14. Do not close the first blank drawing, you can close at the end, and do not run the procedure with already opened drawing to be modify.

Concerning the random number I left as is, I don't know how do you want to manage it.

Let Me know.

 

0 Likes
Message 10 of 11

Anonymous
Not applicable

Thank you so much ,I will check and update soon.

0 Likes
Message 11 of 11

grobnik
Collaborator
Collaborator

Hi @Anonymous , 

let me know.

Please note that the above steps 8,9, and 10 shall be done only the first time you will load and run the procedure. 

Later you can save the project in VBA and above steps are no more necessary.

Bye

0 Likes