Searching for an instance of an existing block in a drawing

Searching for an instance of an existing block in a drawing

Anonymous
Not applicable
364 Views
8 Replies
Message 1 of 9

Searching for an instance of an existing block in a drawing

Anonymous
Not applicable
If I have a VBA macro which creates and inserts blocks then before creating
the block I can search the drawing for an instance of the block using this
code;



Public Function BlockExists(blockName As String) As Boolean



Dim blkDef As AcadBlock



On Error Resume Next

Set blkDef = ThisDrawing.Blocks("Datum Level Marker")

BlockExists = (Err.Number = 0)



End Function





Private Sub cmdOK_Click()

Dim Block As AcadBlock

Dim exblockRefObj As AcadBlockReference

Dim insertpt As Variant



For Each Block In ThisDrawing.Blocks

If Block.Name = "Datum Level Marker" Then



MsgBox "Block Exists in Drawing!", vbOKOnly, "Datum Level Marker Block:
"



' Insert the block



frmDatumLevelAtt.Hide

insertpt = ThisDrawing.Utility.GetPoint(, "Insertion Point : ")

Set exblockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertpt, "Datum
Level Marker", 1#, 1#, 1#, 0)



End If



Next Block



If Not BlockExists("Datum Level Marker") Then



' Create the Datum Level block



This works ok if I know the name of the block beforehand, but say I create
and name blocks according to data entered into text boxes how can I search
for instances of the block not knowing what it will be called until those
text boxes have been filled?



My block name is acquired by concatenating 2 textboxes when defining the
block as follows;



Dim Name As String



Name = TextBox1.text & " x " & TextBox2.text & " mm RC COLUMN"



Any help would be gratefully appreciated



Chris Boyd
0 Likes
365 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
Not the answer you are hoping for but a suggection. Instead of using text boxs to get the block name I would use list boxes and populate them with any possible block names that might be used to what ever block standards you use. That way you wont end up with blocks names that are misspelled, some people have have the fat fingers and hit the wrong keys when typing in a name. .....Murph
0 Likes
Message 3 of 9

Anonymous
Not applicable
If your program creates the block, then why should
it not know what the name is? Unless I"m missing
something, all you need to do is store the name of
the block produced from the contents of the text
boxes in a program variable, and then use it in the
BlockExists() function.

"Chris Boyd" wrote in message
news:243905A36086D92898B984F5D7365CCF@in.WebX.maYIadrTaRb...
> If I have a VBA macro which creates and inserts blocks then before
creating
> the block I can search the drawing for an instance of the block using this
> code;
>
>
>
> Public Function BlockExists(blockName As String) As Boolean
>
>
>
> Dim blkDef As AcadBlock
>
>
>
> On Error Resume Next
>
> Set blkDef = ThisDrawing.Blocks("Datum Level Marker")
>
> BlockExists = (Err.Number = 0)
>
>
>
> End Function
>
>
>
>
>
> Private Sub cmdOK_Click()
>
> Dim Block As AcadBlock
>
> Dim exblockRefObj As AcadBlockReference
>
> Dim insertpt As Variant
>
>
>
> For Each Block In ThisDrawing.Blocks
>
> If Block.Name = "Datum Level Marker" Then
>
>
>
> MsgBox "Block Exists in Drawing!", vbOKOnly, "Datum Level Marker
Block:
> "
>
>
>
> ' Insert the block
>
>
>
> frmDatumLevelAtt.Hide
>
> insertpt = ThisDrawing.Utility.GetPoint(, "Insertion Point : ")
>
> Set exblockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertpt,
"Datum
> Level Marker", 1#, 1#, 1#, 0)
>
>
>
> End If
>
>
>
> Next Block
>
>
>
> If Not BlockExists("Datum Level Marker") Then
>
>
>
> ' Create the Datum Level block
>
>
>
> This works ok if I know the name of the block beforehand, but say I create
> and name blocks according to data entered into text boxes how can I search
> for instances of the block not knowing what it will be called until those
> text boxes have been filled?
>
>
>
> My block name is acquired by concatenating 2 textboxes when defining the
> block as follows;
>
>
>
> Dim Name As String
>
>
>
> Name = TextBox1.text & " x " & TextBox2.text & " mm RC COLUMN"
>
>
>
> Any help would be gratefully appreciated
>
>
>
> Chris Boyd
>
>
0 Likes
Message 4 of 9

Anonymous
Not applicable
Say I have a block named 450x450 RC COL already in the drawing, later on I
'forget' it exists and use the macro to try to create it, I want it to
search for that block instance and tell me it exists before it creates it
and appends the new objects onto the existing block, hence giving me 2 sets
of the same linework and 2 sets of associated notes and 2 sets of attributes
all with the same properties.

Does this make sense?

this is what currently happens

Chris

"Tony Tanzillo" wrote in message
news:F136D9233964B4F0AA97538320EFBA1A@in.WebX.maYIadrTaRb...
> If your program creates the block, then why should
> it not know what the name is? Unless I"m missing
> something, all you need to do is store the name of
> the block produced from the contents of the text
> boxes in a program variable, and then use it in the
> BlockExists() function.
>
> "Chris Boyd" wrote in message
> news:243905A36086D92898B984F5D7365CCF@in.WebX.maYIadrTaRb...
> > If I have a VBA macro which creates and inserts blocks then before
> creating
> > the block I can search the drawing for an instance of the block using
this
> > code;
> >
> >
> >
> > Public Function BlockExists(blockName As String) As Boolean
> >
> >
> >
> > Dim blkDef As AcadBlock
> >
> >
> >
> > On Error Resume Next
> >
> > Set blkDef = ThisDrawing.Blocks("Datum Level Marker")
> >
> > BlockExists = (Err.Number = 0)
> >
> >
> >
> > End Function
> >
> >
> >
> >
> >
> > Private Sub cmdOK_Click()
> >
> > Dim Block As AcadBlock
> >
> > Dim exblockRefObj As AcadBlockReference
> >
> > Dim insertpt As Variant
> >
> >
> >
> > For Each Block In ThisDrawing.Blocks
> >
> > If Block.Name = "Datum Level Marker" Then
> >
> >
> >
> > MsgBox "Block Exists in Drawing!", vbOKOnly, "Datum Level Marker
> Block:
> > "
> >
> >
> >
> > ' Insert the block
> >
> >
> >
> > frmDatumLevelAtt.Hide
> >
> > insertpt = ThisDrawing.Utility.GetPoint(, "Insertion Point : ")
> >
> > Set exblockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertpt,
> "Datum
> > Level Marker", 1#, 1#, 1#, 0)
> >
> >
> >
> > End If
> >
> >
> >
> > Next Block
> >
> >
> >
> > If Not BlockExists("Datum Level Marker") Then
> >
> >
> >
> > ' Create the Datum Level block
> >
> >
> >
> > This works ok if I know the name of the block beforehand, but say I
create
> > and name blocks according to data entered into text boxes how can I
search
> > for instances of the block not knowing what it will be called until
those
> > text boxes have been filled?
> >
> >
> >
> > My block name is acquired by concatenating 2 textboxes when defining the
> > block as follows;
> >
> >
> >
> > Dim Name As String
> >
> >
> >
> > Name = TextBox1.text & " x " & TextBox2.text & " mm RC COLUMN"
> >
> >
> >
> > Any help would be gratefully appreciated
> >
> >
> >
> > Chris Boyd
> >
> >
>
>
>
>
0 Likes
Message 5 of 9

Anonymous
Not applicable
"Chris Boyd" wrote in message
news:686B9384A4036B02531AA185F9CE95E2@in.WebX.maYIadrTaRb...
> Say I have a block named 450x450 RC COL already in the drawing, later on I
> 'forget' it exists and use the macro to try to create it, I want it to
> search for that block instance and tell me it exists before it creates it
> and appends the new objects onto the existing block, hence giving me 2
sets
> of the same linework and 2 sets of associated notes and 2 sets of
attributes
> all with the same properties.
>
> Does this make sense?
>

You shouldn't try to create it, if it already exists.

If you check to see if it already exists before you
create it, and if it does, then get the existing
AcadBlock, then you would just skip the creation process,
possibly delete the exising elements in the block's
collection, and then continue from there.
0 Likes
Message 6 of 9

Anonymous
Not applicable
That's what I'm trying to do! Although until the user enters the values into
the textboxes the macro doesn't know what the name of the block will be,
hence what existing block name it should be looking for. What I'm trying to
achieve is for the macro to search for the blockname after the user trys to
create it, warns the user that the block already exists and simply inserts a
copy of the block. I know how to find an instance of a block with a name I
already know, but this needs to look varying block names.

Chris

"Tony Tanzillo" wrote in message
news:A333BC0BE45A84B6BA593C04B7CD10A5@in.WebX.maYIadrTaRb...
> "Chris Boyd" wrote in message
> news:686B9384A4036B02531AA185F9CE95E2@in.WebX.maYIadrTaRb...
> > Say I have a block named 450x450 RC COL already in the drawing, later on
I
> > 'forget' it exists and use the macro to try to create it, I want it to
> > search for that block instance and tell me it exists before it creates
it
> > and appends the new objects onto the existing block, hence giving me 2
> sets
> > of the same linework and 2 sets of associated notes and 2 sets of
> attributes
> > all with the same properties.
> >
> > Does this make sense?
> >
>
> You shouldn't try to create it, if it already exists.
>
> If you check to see if it already exists before you
> create it, and if it does, then get the existing
> AcadBlock, then you would just skip the creation process,
> possibly delete the exising elements in the block's
> collection, and then continue from there.
>
>
0 Likes
Message 7 of 9

Anonymous
Not applicable
It sounds like you are looking for the "TBLSEARCH" function from LISP. When
porting programs from LSIP to VB/VBA, I had to write this function. Here it
is:

'***************************************************************

Function BlkSearch(ByVal BlkName As String) As Boolean
On Error GoTo BlkSrch_Err
Dim acadBlocks As acadBlocks
Dim BlkTst As String

Set acadBlocks = ThisDrawing.Blocks
BlkTst = acadBlocks.ITEM(BlkName).Name
If BlkTst <> "" Then
BlkSearch = True
Else
BlkSearch = False
End If
Exit Function
BlkSrch_Err:
If Err = -2145386476 Then
BlkSearch = False
Exit Function
End If
End Function

'*********************************************************************
You can then call this like so in your CmdOK_Click:

Name = TextBox1.text & " x " & TextBox2.text & " mm RC COLUMN"


If BlkSearch(Name) then
msgbox "Block Exists"
Exit Sub ' (or call routing to insert block)
else...

JHR

"Chris Boyd" wrote in message
news:F8D57E508EFBA81CCAAD229383DC09D2@in.WebX.maYIadrTaRb...
> That's what I'm trying to do! Although until the user enters the values
into
> the textboxes the macro doesn't know what the name of the block will be,
> hence what existing block name it should be looking for. What I'm trying
to
> achieve is for the macro to search for the blockname after the user trys
to
> create it, warns the user that the block already exists and simply inserts
a
> copy of the block. I know how to find an instance of a block with a name I
> already know, but this needs to look varying block names.
>
> Chris
>
> "Tony Tanzillo" wrote in message
> news:A333BC0BE45A84B6BA593C04B7CD10A5@in.WebX.maYIadrTaRb...
> > "Chris Boyd" wrote in message
> > news:686B9384A4036B02531AA185F9CE95E2@in.WebX.maYIadrTaRb...
> > > Say I have a block named 450x450 RC COL already in the drawing, later
on
> I
> > > 'forget' it exists and use the macro to try to create it, I want it to
> > > search for that block instance and tell me it exists before it creates
> it
> > > and appends the new objects onto the existing block, hence giving me 2
> > sets
> > > of the same linework and 2 sets of associated notes and 2 sets of
> > attributes
> > > all with the same properties.
> > >
> > > Does this make sense?
> > >
> >
> > You shouldn't try to create it, if it already exists.
> >
> > If you check to see if it already exists before you
> > create it, and if it does, then get the existing
> > AcadBlock, then you would just skip the creation process,
> > possibly delete the exising elements in the block's
> > collection, and then continue from there.
> >
> >
>
>
0 Likes
Message 8 of 9

Anonymous
Not applicable
Here is an example of determining if a block exists in a drawing. Set the
block name argument equal to the concatenated name from your UI.

Public Sub test()
Dim oBlock As AcadBlock
Dim strBlockName as String

strBlockName = "myBlock"

Set oBlock = GetBlock(ThisDrawing.Database, strBlockName)

If Not oBlock Is Nothing Then
Debug.Print oBlock.Name
End If

End Sub


'Get a block object from a drawing database
Public Function GetBlock(oDataBase As AcadDatabase, strBlockName As String)
As AcadBlock
Dim oBlock As AcadBlock

On Error Resume Next

'Find the block in the database
Set oBlock = oDataBase.Blocks.Item(strBlockName)

'if there is no error
If Err.Number = 0 Then
'Then return the block
Set GetBlock = oBlock
Else
'else return Nothing
Set GetBlock = Nothing
Err.Clear
End If

End Function

--
Bobby C. Jones
www.AcadX.com
0 Likes
Message 9 of 9

Anonymous
Not applicable
If your question is 'how to determine if a block already
exists without knowing its name' the answer is that you
can't.

You're not supposed to search for the block name *after*
the user tries to create it, because an error will occur
if the block already exists. So, you must test for existence
of the block *before* the user tries to create it.

If your code is responsible for creating the block, using
a name that you acquire from the user via text boxes, then
it is within your means to first test to see if the block
exists, before you try to create it for the user.


"Chris Boyd" wrote in message
news:F8D57E508EFBA81CCAAD229383DC09D2@in.WebX.maYIadrTaRb...
> That's what I'm trying to do! Although until the user enters the values
into
> the textboxes the macro doesn't know what the name of the block will be,
> hence what existing block name it should be looking for. What I'm trying
to
> achieve is for the macro to search for the blockname after the user trys
to
> create it, warns the user that the block already exists and simply inserts
a
> copy of the block. I know how to find an instance of a block with a name I
> already know, but this needs to look varying block names.
>
> Chris
>
> "Tony Tanzillo" wrote in message
> news:A333BC0BE45A84B6BA593C04B7CD10A5@in.WebX.maYIadrTaRb...
> > "Chris Boyd" wrote in message
> > news:686B9384A4036B02531AA185F9CE95E2@in.WebX.maYIadrTaRb...
> > > Say I have a block named 450x450 RC COL already in the drawing, later
on
> I
> > > 'forget' it exists and use the macro to try to create it, I want it to
> > > search for that block instance and tell me it exists before it creates
> it
> > > and appends the new objects onto the existing block, hence giving me 2
> > sets
> > > of the same linework and 2 sets of associated notes and 2 sets of
> > attributes
> > > all with the same properties.
> > >
> > > Does this make sense?
> > >
> >
> > You shouldn't try to create it, if it already exists.
> >
> > If you check to see if it already exists before you
> > create it, and if it does, then get the existing
> > AcadBlock, then you would just skip the creation process,
> > possibly delete the exising elements in the block's
> > collection, and then continue from there.
> >
> >
>
>
0 Likes