Edit attributes of nested blocks

Edit attributes of nested blocks

Dan-Rod
Advocate Advocate
863 Views
9 Replies
Message 1 of 10

Edit attributes of nested blocks

Dan-Rod
Advocate
Advocate

Hello

 

0 Likes
864 Views
9 Replies
Replies (9)
Message 2 of 10

norman.yuan
Mentor
Mentor

Just want to know if it is possible to do what you want with AutoCAD VBA? It is definitely YES (assuming the said blocks that have nested block in it are used correctly), so as with other programming language/API, such as Lisp, or .NET API.

 

If you intend to do it with VBA, you should show the code you have done so far and where/what is the coding difficulties you are to overcome and need help with. 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 10

Dan-Rod
Advocate
Advocate

I am looking for support with the code, I have no experience performing routines, only using the program, this is my intention when posting the problem.

0 Likes
Message 4 of 10

Ed__Jobe
Mentor
Mentor

@Dan-Rod wrote:

I am looking for support with the code, I have no experience performing routines, only using the program, this is my intention when posting the problem.


Why are you seeking help here if you got help in the lisp forum?

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 10

grobnik
Collaborator
Collaborator

@Dan-Rod 

Try the code below, could help you to export blocks attributes to excel worksheet.

For the other parts of your request I don't understand the main issue.

bye

 

 

 

Sub TestNBlo()
Set NewExcel = GetObject(, "Excel.Application")
Set WRKBS = NewExcel.Workbooks.Add
Set wrks = NewExcel.ActiveSheet
    MyRow = 1
    MyCol = 1
Dim ent As AcadEntity
Dim blkRef As AcadBlockReference
Dim att As AcadAttributeReference
Dim atts As Variant
Dim i As Integer

With wrks.Cells
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .Font.Name = "Times New Roman"
    .Font.Size = 10
    .NumberFormat = "@"
End With

Dim blkDef As AcadBlock

For Each blkDef In ThisDrawing.Blocks ' Assume the block definition does exist in ThisDrawing

For Each ent In blkDef

    If TypeOf ent Is AcadBlockReference Then

        Set blkRef = ent

        If blkRef.HasAttributes Then
            
            wrks.Cells(MyRow, MyCol).Value = blkRef.EffectiveName
            atts = blkRef.GetAttributes()
            For i = 0 To UBound(atts)
            
             wrks.Cells(MyRow + 1, MyCol + 1).Value = atts(i).TagString
             wrks.Cells(MyRow + 1, MyCol + 2).Value = atts(i).TextString
             
            MyRow = MyRow + 1
            'MyCol = MyCol + 1
            
            Next
        End If

    End If

Next
MyCol = MyCol + 2
MyRow = 1
Next
MsgBox "END"
End Sub

 

 

0 Likes
Message 6 of 10

Dan-Rod
Advocate
Advocate

Hello, thanks for your time,

It gives me an alert attached to the photo,

As for what I am looking for, it is a way to be able to give consecutive attributes mentioned in the publication. If there is a way to extract them in Excel, modify and enter the new data, it would be perfect. It is the "GA" attribute of the "CAT" blocks. which are nested in the blocks between parentheses.

 

 

DanRod_1-1712009894125.png

 

0 Likes
Message 7 of 10

grobnik
Collaborator
Collaborator

@Dan-Rod 

Hi, as far as I read you are not so expert with VBA.

I'll try to explain:

first of all procedure has been developed inside AUTOCAD VBA, so if you have not an Autocad release able to developing VBA Application, the procedure shall be devolped inside Excel, pointing to Autocad drawing, could be more or less the same but at the same time little bit different.

Starting from point above, you should declare inside Autocad the Excel library in order to use the Excel Worksheet like an object of Autocad.

So if the answer first point is true, first of all you have to create a VBA project, insert a Module, and paste the procedure.

Second point concerning the Excel library please look at picture below:

1) Open Visual Basic Editor

grobnik_0-1712076437539.png

2) Excel Reference

Inside development module please find "Tools" -> "References", a window like the below should appear.

grobnik_1-1712076550417.png

Please check the Excel Objet Library Flag, if you do not have the release 16 of the Library, probably you will find a "MISSING" side the Excel 16 Library text, in case de-flag the MISSING one, scroll the library list and flag the latest Excel Object Library available in your computer where MS OFFICE software tools is installed .

 

The attribute writing it's the same as the attribute reading, once you have the array atts(i).TextString of selected block do you want to modify, which is containing the value of your attribute (LABEL or TAG indicated in atts(i).Tagstring) you have to find a way to check the modified field in excel, and rewrite into Autocad like as  atts( ... " POSITION OF ATTRIBUTE IN THE ARRAY .... ").TextString = wrks.Cells(MyRow , MyCol).Value.

 

I guess you have to create a mix of procedures, a part with autocad, and a part with Excel, I guess this will take time, or completely into EXCEL.

 

So first of all you have select only the blocks to be modify, get attributes, write to excel, modify in excel and rewrite in the block.

Please note that you could have a lot of blocks with the same name, but with different attributes, in this case you have to retrive a block properties named block ID, and write it to excel in order to modify only that block.

If this is not required, and you want to increase only one attribute sequentially as the block will be found during the scan in my opinion could be more easy.

 

I know are a lot of information, and suggestion perhaps you are searching for a working code but this take a time to working on.

I'm suggesting to use forums, or part of other routine on web and make experiment. Everybody in this forum or MS office Forum could help you.

Bye.

Message 8 of 10

grobnik
Collaborator
Collaborator
Hi,
Lookig at ".... It is the "GA" attribute of the "CAT" blocks. which are nested in the blocks between parentheses..." It can be possible, but once you will modify the GA attribute sequence the CAT blocks position nested into blocks between parentheses will be the same as previously.
Probably you don't need to change the nested block position (CAT) but modify only the GA attributes, and maintain the same position. Let us know if understand well.
0 Likes
Message 9 of 10

Dan-Rod
Advocate
Advocate

Thanks

 

 

 

0 Likes
Message 10 of 10

Dan-Rod
Advocate
Advocate

Thanks for the information, I already ran the macro

0 Likes