Insert several blocks with vba

Insert several blocks with vba

Anonymous
Not applicable
1,894 Views
9 Replies
Message 1 of 10

Insert several blocks with vba

Anonymous
Not applicable

Hi I have this code working ok, but I want to insert several more blocks can anyone help

Thank you

 

Dim blockObj As AcadBlock
Dim insertionPnt(0 To 2) As Double
Dim blockRefObj As AcadBlockReference

' Insert the block
insertionPnt(0) = Val(TextBox21.Text): insertionPnt(1) = Val(TextBox28.Text): insertionPnt(2) = 0
Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "T2", 1#, 1#, 1#, 0)

0 Likes
Accepted solutions (3)
1,895 Views
9 Replies
Replies (9)
Message 2 of 10

grobnik
Collaborator
Collaborator

@Anonymous 

Hi,

the question could be stupid but all the same block name and all in the same position ?.

Could be simple to create a loop count with total amount of blocks to insert and repeat insertion command, or if you want to shift you can insert in the loop a coordinate constant shift.

May be so simply ? I guess no.

insertionPnt(0) = Val(TextBox21.Text): insertionPnt(1) = Val(TextBox28.Text): insertionPnt(2) = 0
For X= 1 to 3 'you can insert a new textbox with amount of blocks
'if you want to shift by X
N=0
insertionPnt(0)=insertionPnt(0)+ N ' shift value
Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "T2", 1#, 1#, 1#, 0)
N=N+5 'VALUE to INCREASE for X coordinate shifting

Next
0 Likes
Message 3 of 10

Anonymous
Not applicable

 

Hi

Apologies for not enough detail, The textboxes are in the userform  x and y 

The block I am inserting is T2 its the same for each of the insertions but in different positions 

Below is what I have but not working?

 

 

Dim blockObj As AcadBlock

Dim insertionPnt(0 To 😎 As Double

Dim blockRefObj As AcadBlockReference

 

' Insert the blocks

insertionPnt(0) = Val(TextBox20.Text): insertionPnt(1) = Val(TextBox28.Text): insertionPnt(2) = 0

insertionPnt(3) = Val(TextBox21.Text): insertionPnt(4) = Val(TextBox29.Text): insertionPnt(5) = 0

insertionPnt(6) = Val(TextBox22.Text): insertionPnt(7) = Val(TextBox30.Text): insertionPnt(8) =

 

Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "T2", 1#, 1#, 1#, 0)

 

Thanks for your help Chris

0 Likes
Message 4 of 10

grobnik
Collaborator
Collaborator
Accepted solution

@Anonymous 

Hi,

I guess that the issue is inside the coordinates array because insertblock is expecting an array with 3 points X, Y, and Z, not 7 or eight.

I'll investigate more, however you can put in an array the value of first couple of textbox and third value to 0 if Z coord is 0, insert block, put the second couple and insert block with new coords array name coming from other text box, and so on.

 

Dim insertionPnt(0 To 2) As Double
Dim insertionPnt1(0 To 2) As Double
Dim insertionPnt2(0 To 2) As Double
insertionPnt(0) = Val(TextBox20.Text): insertionPnt(1) = Val(TextBox28.Text): insertionPnt(2) = 0

insertionPnt1(0) = Val(TextBox21.Text): insertionPnt1(1) = Val(TextBox29.Text): insertionPnt1(2) = 0

insertionPnt2(0) = Val(TextBox22.Text): insertionPnt2(1) = Val(TextBox30.Text): insertionPnt2(2)=0

Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "T2", 1#, 1#, 1#, 0)

Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt1, "T2", 1#, 1#, 1#, 0)

Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt2, "T2", 1#, 1#, 1#, 0)

 

Try and let me know

0 Likes
Message 5 of 10

Anonymous
Not applicable
Accepted solution

Hi 

Thank you very much it seems to be working ok is it ok to accept this as a solution

Message 6 of 10

Anonymous
Not applicable

Hi 

One thing I have noticed, if there is no entry into text boxes the object T2 is placed in drawing at 0,0

have you any idea how that can happen.

Regards

0 Likes
Message 7 of 10

grobnik
Collaborator
Collaborator
Accepted solution

Hi, @Anonymous 

I just show you the method, of course, the insertion point it's 0,0,0 because coming from a numeric conversion of an empty box text value, the array will be 0,0,0 and block will be inserted in that coordinate 0,0,0.

I suggest to insert a command button side each group of coordinates text boxes in the form and insert in own event Sub the insertion block code, checking, BEFORE INSERTING BLOCK, if coordinates of X and Y are > than 0.

Let me know

Regards

0 Likes
Message 8 of 10

Ed__Jobe
Mentor
Mentor

@Anonymous wrote:

Hi 

One thing I have noticed, if there is no entry into text boxes the object T2 is placed in drawing at 0,0

have you any idea how that can happen.

Regards


What do you expect to happen? Do you want to prevent users from clicking insert when the text box is empty? Give the text box a default value.

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 9 of 10

grobnik
Collaborator
Collaborator

@Anonymous 

Here an example of what I'm saying, you can replicate the code on command button for 2nd block and so on

 

Private Sub CommandButton1_Click()
Dim insertionPnt(2) As Double ' this part should be moved inside formload event and declared as Global in order to avoid a ridimension, or use ReDim command.
If UserForm1.TextBox1.Value <> "" Then
    insertionPnt(0) = Val(TextBox1.Text): insertionPnt(1) = Val(TextBox2.Text): insertionPnt(2) = 0
    Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "T2", 1#, 1#, 1#, 0)
ElseIf UserForm1.TextBox1.Value = "" Then
    MsgBox "Insert the coordinates Value in TextBox"
End If
End Sub

 

grobnik_0-1588786273886.png

 

0 Likes
Message 10 of 10

Anonymous
Not applicable

Hi Thank you very much you have solved the issue for me

 

Chris