What's wrong with this function?

What's wrong with this function?

Anonymous
Not applicable
366 Views
6 Replies
Message 1 of 7

What's wrong with this function?

Anonymous
Not applicable
I have this in a class module.
I thought it used to work. Now I try and it gives no error, and reports
block was inserted, but no INSERT is found in dwg after it runs.( in a blank
dwg with the 'passed' blockname defined in dwg)

Public Function InsertBlock(oDoc As AcadDocument, sBlockName As String, oBlk
As AcadBlock, inspt As Variant, dblSclX As Double, dblSclY As Double,
dblSclZ As Double, dblRot As Double, sLyr As String) As AcadBlockReference
'see if block is defined in dwg
Dim blkCheckBlock As AcadBlock
On Error Resume Next
Set blkCheckBlock = oDoc.Blocks.Item(sBlockName)
'if Block doesn't exist, leave
If Err Then
Err.Clear
Set blkCheckBlock = Nothing
Debug.Print "block " & sBlockName & " not found"
Exit Function
Else
'block exists so insert it
Dim brefThisBlockRef As AcadBlockReference
Set brefThisBlockRef = oDoc.oBlk.InsertBlock(inspt, sBlockName,
dblSclX, dblSclY, dblSclZ, dblRot)
'put it on the right layer
brefThisBlockRef.Layer = sLyr
'return the block object
Set InsertBlock = brefThisBlockRef
Debug.Print "block " & sBlockName & " inserted"
End If
'clean up
Set blkCheckBlock = Nothing
Set brefThisBlockRef = Nothing

End Function

the test sub to call this is:
Sub test()
Dim doc As AcadDocument
Set doc = ThisDrawing

doc.Utility.prompt "Start test"
Debug.Print "Start test"
'this is the class mod in which the insert block func is defined
'set to your test name
Dim cblk As cmnBlock
Set cblk = New cmnBlock

Dim mweldname As String
'this is the blockname - change to your block name to test
mweldname = "2x10h"
doc.Utility.prompt "Block name is: " & mweldname
'''''''''''
Dim cspace As AcadBlock
'currspace function below
Set cspace = CurrSpace(doc)
Debug.Print "Current space is " & cspace.Name
doc.Utility.prompt "Current space is " & cspace.Name


Dim inspt As Variant
inspt = doc.Utility.GetPoint(, "Pick point for weld sym")
Debug.Print "inspt is : " & inspt(0) & ", " & inspt(1) & ", " & inspt(2)
doc.Utility.prompt "inspt is : " & inspt(0) & ", " & inspt(1) & ", " &
inspt(2)
Dim weldscl As Double
weldscl = 1
Dim weldrot As Double
weldrot = 0
Dim weldLyr As String
weldLyr = "0"

doc.Utility.prompt "Call insertblock function:"
Set blkWeldSymRef = cblk.InsertBlock(doc, mweldname, cspace, inspt, weldscl,
weldscl, weldscl, weldrot, weldLyr)
Debug.Print "Done testw"
doc.Utility.prompt "Done testw"
'clean up
Set blkWeldSymRef = Nothing
Set cblk = Nothing
Set doc = Nothing

End Sub

Public Function CurrSpace(doc as AcadDocument) As AcadBlock
Set CurrSpace = doc.ActiveLayout.Block
End Function

Does anyone see anything obviously wrong here?
other than terrible programming flow?
tia
Mark
0 Likes
367 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Should be:
'it needs to be inserted into modelspace
Set brefThisBlockRef = oDoc.ModelSpace.InsertBlock(inspt, sBlockName,
dblSclX, dblSclY, dblSclZ, dblRot)
'include this line to update the block right away so it appears
brefThisBlockRef.Update

-Bob

"Mark Propst" wrote in message
news:67F1B944DD8F4C3E02BB4B37F16FEE4A@in.WebX.maYIadrTaRb...
> I have this in a class module.
> I thought it used to work. Now I try and it gives no error, and reports
> block was inserted, but no INSERT is found in dwg after it runs.( in a
blank
> dwg with the 'passed' blockname defined in dwg)
>
> Public Function InsertBlock(oDoc As AcadDocument, sBlockName As String,
oBlk
> As AcadBlock, inspt As Variant, dblSclX As Double, dblSclY As Double,
> dblSclZ As Double, dblRot As Double, sLyr As String) As AcadBlockReference
> 'see if block is defined in dwg
> Dim blkCheckBlock As AcadBlock
> On Error Resume Next
> Set blkCheckBlock = oDoc.Blocks.Item(sBlockName)
> 'if Block doesn't exist, leave
> If Err Then
> Err.Clear
> Set blkCheckBlock = Nothing
> Debug.Print "block " & sBlockName & " not found"
> Exit Function
> Else
> 'block exists so insert it
> Dim brefThisBlockRef As AcadBlockReference
> Set brefThisBlockRef = oDoc.oBlk.InsertBlock(inspt, sBlockName,
> dblSclX, dblSclY, dblSclZ, dblRot)
> 'put it on the right layer
> brefThisBlockRef.Layer = sLyr
> 'return the block object
> Set InsertBlock = brefThisBlockRef
> Debug.Print "block " & sBlockName & " inserted"
> End If
> 'clean up
> Set blkCheckBlock = Nothing
> Set brefThisBlockRef = Nothing
>
> End Function
>
> the test sub to call this is:
> Sub test()
> Dim doc As AcadDocument
> Set doc = ThisDrawing
>
> doc.Utility.prompt "Start test"
> Debug.Print "Start test"
> 'this is the class mod in which the insert block func is defined
> 'set to your test name
> Dim cblk As cmnBlock
> Set cblk = New cmnBlock
>
> Dim mweldname As String
> 'this is the blockname - change to your block name to test
> mweldname = "2x10h"
> doc.Utility.prompt "Block name is: " & mweldname
> '''''''''''
> Dim cspace As AcadBlock
> 'currspace function below
> Set cspace = CurrSpace(doc)
> Debug.Print "Current space is " & cspace.Name
> doc.Utility.prompt "Current space is " & cspace.Name
>
>
> Dim inspt As Variant
> inspt = doc.Utility.GetPoint(, "Pick point for weld sym")
> Debug.Print "inspt is : " & inspt(0) & ", " & inspt(1) & ", " & inspt(2)
> doc.Utility.prompt "inspt is : " & inspt(0) & ", " & inspt(1) & ", " &
> inspt(2)
> Dim weldscl As Double
> weldscl = 1
> Dim weldrot As Double
> weldrot = 0
> Dim weldLyr As String
> weldLyr = "0"
>
> doc.Utility.prompt "Call insertblock function:"
> Set blkWeldSymRef = cblk.InsertBlock(doc, mweldname, cspace, inspt,
weldscl,
> weldscl, weldscl, weldrot, weldLyr)
> Debug.Print "Done testw"
> doc.Utility.prompt "Done testw"
> 'clean up
> Set blkWeldSymRef = Nothing
> Set cblk = Nothing
> Set doc = Nothing
>
> End Sub
>
> Public Function CurrSpace(doc as AcadDocument) As AcadBlock
> Set CurrSpace = doc.ActiveLayout.Block
> End Function
>
> Does anyone see anything obviously wrong here?
> other than terrible programming flow?
> tia
> Mark
>
>
0 Likes
Message 3 of 7

Anonymous
Not applicable
That would be if you wanted it inserted into modelspace.
my function:
Public Function InsertBlock(oDoc As AcadDocument, sBlockName As String, oblk
As AcadBlock,...etc
has oblk As AcadBlock param so that the line
Set brefThisBlockRef = oDoc.oblk.InsertBlock(inspt, sBlockName, dblSclX,
dblSclY, dblSclZ, dblRot)
will insert the block into what ever 'block' is passed in, modelspace,
paperspacelayout, or blockdefinition object
at least that was the idea, obviously it's not working as I thought it
should.
Any other ideas?
Thanks for your input
Mark

"Bob Dorris" wrote in message
news:66D4215AC2A1475BA0284CFA1E8F4732@in.WebX.maYIadrTaRb...
>
> Should be:
> 'it needs to be inserted into modelspace
> Set brefThisBlockRef = oDoc.ModelSpace.InsertBlock(inspt, sBlockName,
> dblSclX, dblSclY, dblSclZ, dblRot)
0 Likes
Message 4 of 7

Anonymous
Not applicable
Mark Propst wrote:
> That would be if you wanted it inserted into modelspace.
> my function:
> Public Function InsertBlock(oDoc As AcadDocument, sBlockName As
> String, oblk As AcadBlock,...etc
> has oblk As AcadBlock param so that the line
> Set brefThisBlockRef = oDoc.oblk.InsertBlock(inspt, sBlockName,
> dblSclX, dblSclY, dblSclZ, dblRot)
> will insert the block into what ever 'block' is passed in, modelspace,
> paperspacelayout, or blockdefinition object
> at least that was the idea, obviously it's not working as I thought it
> should.
> Any other ideas?

oDoc.oBlock will not work since the AcadDocument class has no oBlock
property. oDoc.Blocks(oBlock.Name) *will* work.

However, the block all by itself is enough:
oBlock.InsertBlock(yada, yada)

--
"It's more important that you know how to find the answer than to have
the answer." - Me
0 Likes
Message 5 of 7

Anonymous
Not applicable
Thanks again FrankO!
:-)
I finally got that and changed the Insert func to just take the block obj
and no doc

now I've got another question....but I guess that's another thread!
Thanks again for all your help.
Mark
0 Likes
Message 6 of 7

Anonymous
Not applicable
Passing the document and a block in the document is
a bit superfluous and a source of errors.

Ditch the oDoc parameter and get the document from
the AcadBlock's Document property instead.

"Mark Propst" wrote in message news:67F1B944DD8F4C3E02BB4B37F16FEE4A@in.WebX.maYIadrTaRb...
> I have this in a class module.
> I thought it used to work. Now I try and it gives no error, and reports
> block was inserted, but no INSERT is found in dwg after it runs.( in a blank
> dwg with the 'passed' blockname defined in dwg)
>
> Public Function InsertBlock(oDoc As AcadDocument, sBlockName As String, oBlk
> As AcadBlock, inspt As Variant, dblSclX As Double, dblSclY As Double,
> dblSclZ As Double, dblRot As Double, sLyr As String) As AcadBlockReference
> 'see if block is defined in dwg
> Dim blkCheckBlock As AcadBlock
> On Error Resume Next
> Set blkCheckBlock = oDoc.Blocks.Item(sBlockName)
> 'if Block doesn't exist, leave
> If Err Then
> Err.Clear
> Set blkCheckBlock = Nothing
> Debug.Print "block " & sBlockName & " not found"
> Exit Function
> Else
> 'block exists so insert it
> Dim brefThisBlockRef As AcadBlockReference
> Set brefThisBlockRef = oDoc.oBlk.InsertBlock(inspt, sBlockName,
> dblSclX, dblSclY, dblSclZ, dblRot)
> 'put it on the right layer
> brefThisBlockRef.Layer = sLyr
> 'return the block object
> Set InsertBlock = brefThisBlockRef
> Debug.Print "block " & sBlockName & " inserted"
> End If
> 'clean up
> Set blkCheckBlock = Nothing
> Set brefThisBlockRef = Nothing
>
> End Function
>
> the test sub to call this is:
> Sub test()
> Dim doc As AcadDocument
> Set doc = ThisDrawing
>
> doc.Utility.prompt "Start test"
> Debug.Print "Start test"
> 'this is the class mod in which the insert block func is defined
> 'set to your test name
> Dim cblk As cmnBlock
> Set cblk = New cmnBlock
>
> Dim mweldname As String
> 'this is the blockname - change to your block name to test
> mweldname = "2x10h"
> doc.Utility.prompt "Block name is: " & mweldname
> '''''''''''
> Dim cspace As AcadBlock
> 'currspace function below
> Set cspace = CurrSpace(doc)
> Debug.Print "Current space is " & cspace.Name
> doc.Utility.prompt "Current space is " & cspace.Name
>
>
> Dim inspt As Variant
> inspt = doc.Utility.GetPoint(, "Pick point for weld sym")
> Debug.Print "inspt is : " & inspt(0) & ", " & inspt(1) & ", " & inspt(2)
> doc.Utility.prompt "inspt is : " & inspt(0) & ", " & inspt(1) & ", " &
> inspt(2)
> Dim weldscl As Double
> weldscl = 1
> Dim weldrot As Double
> weldrot = 0
> Dim weldLyr As String
> weldLyr = "0"
>
> doc.Utility.prompt "Call insertblock function:"
> Set blkWeldSymRef = cblk.InsertBlock(doc, mweldname, cspace, inspt, weldscl,
> weldscl, weldscl, weldrot, weldLyr)
> Debug.Print "Done testw"
> doc.Utility.prompt "Done testw"
> 'clean up
> Set blkWeldSymRef = Nothing
> Set cblk = Nothing
> Set doc = Nothing
>
> End Sub
>
> Public Function CurrSpace(doc as AcadDocument) As AcadBlock
> Set CurrSpace = doc.ActiveLayout.Block
> End Function
>
> Does anyone see anything obviously wrong here?
> other than terrible programming flow?
> tia
> Mark
>
>
0 Likes
Message 7 of 7

Anonymous
Not applicable
Incredible!
That's actually what my slow-a** brain finally figured out! To have it
confirmed by you is a treat!
Thanks!
:-)
Mark

"Tony Tanzillo" wrote in message
news:4CD3EF8B630120BCC990CE9588AAA0E0@in.WebX.maYIadrTaRb...
> Passing the document and a block in the document is
> a bit superfluous and a source of errors.
>
> Ditch the oDoc parameter and get the document from
> the AcadBlock's Document property instead.
0 Likes