Message 1 of 7
What's wrong with this function?

Not applicable
06-11-2003
05:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
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