Insert number into an array of blocks

Insert number into an array of blocks

zka192020
Participant Participant
1,822 Views
11 Replies
Message 1 of 12

Insert number into an array of blocks

zka192020
Participant
Participant

Hi all,

 

Is there any way that I could do coding in VBA to insert automatic numbers inside an array of blocks?

 

zka192020_0-1643885694525.png

If you can see above, I'm manually putting text to indicate the number, I want that if I select the array it will be automatically numbered. can anyone help me how to do it in VBA, please take note that I'm new to this VBA, I'm eager to learn about it.

 

Thanks,

 

0 Likes
Accepted solutions (1)
1,823 Views
11 Replies
Replies (11)
Message 2 of 12

grobnik
Collaborator
Collaborator

Hi @zka192020 I guess tha the best could be create a block attribute, as alternative you can retrive the block insertion point and place text with properties you wish.

I'll try to create sample and share with you.

Message 3 of 12

grobnik
Collaborator
Collaborator

Hi @zka192020 I downloaded your file but seems that the block are associated in associative array, in this way I guess my idea will be not applicable, but I'm trying.

Message 4 of 12

norman.yuan
Mentor
Mentor

Firstly, as @grobnik pointed out: it would be much better for the block to have an attribute defined and use the attribute to show the number of each individual block.

 

Now that @grobnik has found out the individual block references are created as associative array, then there is no direct way to update the attribute of each individual block in the associative array.

 

Here are possible ways to do it:

 

1. Explode the associative array, so you get all the individual block references line up in its original location. Then, you find all the individual block references and based on their location (insertion point - say, from left to right) to determine the number and update the attribute, or draw a text at center of each individual block reference. 

 

2. You can try to find the size (BoundingBox) of the individual block reference, and calculate the center of each individual block reference based on the boundingbox (a big rectangle) of the associative array. In your case, it is simply evenly dividing the array's boundingbox into small rectangle. Then drawing a text for the number at the center of each individual block reference.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 5 of 12

zka192020
Participant
Participant

Hi @grobnik, actually I don't need the associative array, it just when I array it automatically turn on the associative array. Normal array would do for me. Thanks.

0 Likes
Message 6 of 12

zka192020
Participant
Participant

Hi @norman.yuan, actually I don't need the associative array, I understand the step given by you, but I don't know how to convert it into VBA coding, I'm new and still learning about VBA.

0 Likes
Message 7 of 12

grobnik
Collaborator
Collaborator

@zka192020 Hi, 

here below a sample code for inserting a number "over" the block, with "over" I mean that it's untied with block, like as attribute could be.

Before execute the code you have to explode your associative array, and in case you create a new one, please not set it as associative.

Code could be refined, for example you can check the insertion point of block and if the value of Y changes, you can restart the count.

Please note that the number sequence associated to block will be the same as insertion block sequence.

I hope this will helps you a little bit.

Bye

 

Sub Countblock()
A = 1
Dim MyTextInsPoint(0 To 2) As Double
Dim MyAlignmentPoint(0 To 2) As Double
Dim MyblockInsPoint() As Double
Dim MyBObject As Object
For Each MyBObject In ThisDrawing.ModelSpace
    If TypeOf MyBObject Is AcadBlockReference Then
        Set MyblockNew = MyBObject
        MyblockInsPoint = MyblockNew.InsertionPoint
        
        'midpoint X = (X1 + X2) / 2
        'midpoint Y = (Y1 + Y2) / 2
        'midpoint Z = (Z1 + Z2) / 2
        MyblockNew.GetBoundingBox minExt, maxExt
        MyTextInsPoint(0) = (minExt(0) + maxExt(0)) / 2#
        MyTextInsPoint(1) = (minExt(1) + maxExt(1)) / 2#
        MyTextInsPoint(2) = 0# '(minExt(2) + maxExt(2)) / 2#
      
        Set MyText = ThisDrawing.ModelSpace.AddText(CStr(A), MyTextInsPoint, 180)
        MyText.Alignment = acAlignmentMiddleCenter
        MyAlignmentPoint(0) = MyTextInsPoint(0)
        MyAlignmentPoint(1) = MyTextInsPoint(1)
        MyAlignmentPoint(2) = MyTextInsPoint(2)
        MyText.TextAlignmentPoint = MyAlignmentPoint
        MyText.Alignment = acAlignmentTopCenter
        ThisDrawing.Regen acAllViewports
        A = A + 1
    End If
Next
End Sub

 

 

 

 

 

 

 

Message 8 of 12

zka192020
Participant
Participant

Hi @grobnik,

 

Sorry for the late reply, I'm on holiday. Thank you so much for your code.

I understand a bit now how to do it, but when I try your code, the number goes up from row to row.

Can I know how to change it so that the number can be in the arrow direction.

zka192020_0-1644282568889.png

 

Thanks a lot @grobnik 

 

0 Likes
Message 9 of 12

zka192020
Participant
Participant

Hi @grobnik,

 

Another one I found from your code is that the number is based on the total qty, but actually what I want to do is every row start with number 1 (refer pic below). But can I do that the user input the starting number? For example as you can see from the last row, the number start from 3, because it doesn't have same qty of seat as per the other row.

 

zka192020_0-1644286732190.png

 

0 Likes
Message 10 of 12

grobnik
Collaborator
Collaborator
Accepted solution

Hi @zka192020 

As I wrote "Please note that the number sequence associated to block will be the same as insertion block sequence."

Probably when you inserted the blocks it was in row sequence

You can refine the code, you can get help from forum.

For user input integer you can try intUserIntegerInput = UtilityObject.GetInteger([Prompt]) and then pass the value to insert text function.

Message 11 of 12

grobnik
Collaborator
Collaborator

@zka192020 As alternative you can pick the starting point, get coordinate ask for sequence rows or columns , ask for starting number and quantity, and the code will insert a sequence of  numbers. Or there are a lot of utilty on web already done which insert automatically a rows of numbers, made by lsp language.

Bye

Message 12 of 12

zka192020
Participant
Participant

ok @grobnik, thanks a lot

0 Likes