create block and insert it

create block and insert it

Anonymous
Not applicable
266 Views
2 Replies
Message 1 of 3

create block and insert it

Anonymous
Not applicable
I am trying to simplify my dvbs with functions and below I'm trying to create a block then insert the block. Below puts a block in the blocks collection but does not let me insert the block. I don't think that I am calling the block reference out correctly. Is my function set up correctly?


Sub BlockTEST()

Dim LayerName As String
Dim strRandomBlkName As String

LayerName = "Geometry"

' Make point array for block's insertion point
Dim ptOrigin(2) As Double
ptOrigin(0) = 0#: ptOrigin(1) = 0#: ptOrigin(2) = 0#

' Create the block object
Dim blockObj As AcadBlock
strRandomBlkName = randomblkName
Set Block = ThisDrawing.Blocks.Add(ptOrigin, strRandomBlkName)

' Make point arrays for lines
Dim pt1(2) As Double
pt1(0) = -1#: pt1(1) = 6#: pt1(2) = 0#
Dim pt2(2) As Double
pt2(0) = 1#: pt2(1) = 0#: pt2(2) = 0#
Dim pt3(2) As Double
pt3(0) = 0#: pt3(1) = -1#: pt3(2) = 0#
Dim pt4(2) As Double
pt4(0) = 0#: pt4(1) = 6#: pt4(2) = 0#

Block.AddLine pt1, pt2
Block.AddLine pt3, pt4
' Make sure layer exists
'ThisDrawing.Layers.Add (LayerName)

End Sub

Public Function insertBlock(blkRef As AcadBlockReference) As AcadBlock

'Insert the block, creating a block reference
'Dim blockObj As AcadBlock
'Dim blkRef As AcadBlockReference
Dim pt As Variant

blockObj = ThisDrawing.Blocks.Add(pt, strRandomBlkName)

pt = ThisDrawing.Utility.GetPoint(, "Insert block: ")

Set blockObj = ThisDrawing.ModelSpace.insertBlock(pt, strRandomBlkName, 1#, 1#, 1#, 0)
'Set blkRef = insertBlock
End Function

Public Function randomblkName() As String
Dim iRandom As Integer
iRandom = Int((1000 * Rnd) + 1)
randomblkName = "TEST" & "-" & CStr(iRandom) & "-" & Format(Now, "yymmss")
End Function
0 Likes
267 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
RobertMi> wrote in message news:5410600@discussion.autodesk.com...

...ignoring the need for suitable error trapping for the sake of simple
example

Public Function MyInsertBlock(sBlkName as string) As AcadBlockReference
Dim pt As Variant
pt = ThisDrawing.Utility.GetPoint(, "Insert block: ")

Set MyInsertBlock = ThisDrawing.ModelSpace.insertBlock(pt, sBlkName , 1#,
1#, 1#, 0)
End Function
0 Likes
Message 3 of 3

Anonymous
Not applicable
Thansks for your help, I changed it to a sub. I kind of learning when to and not to use functions. I did get this to work, I just wanted something half way simple and then build on it.

'-- begin
Sub BlockTEST()

Dim LayerName As String
Dim strRandomBlkName As String

LayerName = "Geometry"

' Make point array for block's insertion point
Dim ptOrigin(2) As Double
ptOrigin(0) = 0#: ptOrigin(1) = 0#: ptOrigin(2) = 0#

' Create the block object
Dim blockObj As AcadBlock
strRandomBlkName = randomblkName
Set Block = ThisDrawing.Blocks.Add(ptOrigin, strRandomBlkName)

' Make point arrays for lines
Dim pt1(2) As Double
pt1(0) = -1#: pt1(1) = 6#: pt1(2) = 0#
Dim pt2(2) As Double
pt2(0) = 1#: pt2(1) = 0#: pt2(2) = 0#
Dim pt3(2) As Double
pt3(0) = 0#: pt3(1) = -1#: pt3(2) = 0#
Dim pt4(2) As Double
pt4(0) = 0#: pt4(1) = 6#: pt4(2) = 0#

Block.AddLine pt1, pt2
Block.AddLine pt3, pt4

Call insertBlock(strRandomBlkName)

End Sub

Public Sub insertBlock(strRandomBlkName As String)
Dim pt As Variant
pt = ThisDrawing.Utility.GetPoint(, "Insert block: ")
Set blockObj = ThisDrawing.ModelSpace.insertBlock(pt, strRandomBlkName, 1#, 1#, 1#, 0)
End Sub

Public Function randomblkName() As String
Dim iRandom As Integer
iRandom = Int((1000 * Rnd) + 1)
randomblkName = "TEST" & "-" & CStr(iRandom) & "-" & Format(Now, "yymmss")
End Function
'-- end
0 Likes