Jump to content

insert block into block table (VBA)


dbroada

Recommended Posts

I have a VBA routine that finally places a block on the drawing which is mostly working fine providing the block is already present in the drawing. If the block isn't already defined I get an error. How can I ensure the block is already in the drawing?

 

     Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(TriPos, "Tri", CScale, CScale, CScale, 0)

 

The block name is "Tri" and it is in a pathed folder. I am obviously missing something but I'm not sure what.

Link to comment
Share on other sites

I have a VBA routine that finally places a block on the drawing which is mostly working fine providing the block is already present in the drawing. If the block isn't already defined I get an error. How can I ensure the block is already in the drawing?

 

     Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(TriPos, "Tri", CScale, CScale, CScale, 0)

 

The block name is "Tri" and it is in a pathed folder. I am obviously missing something but I'm not sure what.

 

you could check for block existence before adding any reference to it

 

for instance

Dim BlockToCheck As AcadBlock
Dim BlockRefObj As AcadBlockReference
On Error Resume Next
Set BlockToCheck = ThisDrawing.Blocks.Item("name")
On Error Resume Next
If Not BlockToCheck Is Nothing Then
   Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(TriPos, "Tri", CScale, CScale, CScale, 0)
End If

Link to comment
Share on other sites

you could check for block existence before adding any reference to it

 

I suspect that Dave instead wants to first check if the BlockTableRecord exists, and if not, then insert it from File (as it is found within SFSP)... My VBA is pretty much non-existent (I only code in LISP/.NET), so take from this what you like:

 

Dim dwg As String
dwg = "Tri"

Dim BlockToCheck As AcadBlock
Dim BlockRefObj As AcadBlockReference
On Error Resume Next

Set BlockToCheck = ThisDrawing.Blocks.Item(dwg)
On Error Resume Next

If BlockToCheck Is Nothing Then
   dwg$ = [color="red"]AcadFindFile[/color](dwg)
End If

Set BlockRefObj = ThisDrawing.ModelSpace.InsertBlock(TriPos, dwg, CScale, CScale, CScale, 0)

 

 

... And the dependent AcadFindFile() pseudo-Function:

 

Function AcadFindFile(fname As String) As String
Dim envpath As Variant, i As Integer, FSO As FileSystemObject, srchfname As String
envpath = Split(Application.ActiveDocument.GetVariable("Acadprefix"), ";")
Set FSO = New FileSystemObject
For i = LBound(envpath) To UBound(envpath)
	srchfname = FSO.BuildPath(envpath(i), fname)
	If FSO.FileExists(srchfname) Then
		AcadFindFile = srchfname
		Exit For
	End If
Next
End Function

Edited by BlackBox
Link to comment
Share on other sites

yes, it is as BB said. I wanted to check for tis existence and if it was in the drawing already I would insert that one but if its not in the drawing then insert it from a file.

 

As I couldn't understand the AcadFindFile bit (I couldn't get FileSystemObject to work) I have taken the sledgehammer approach now by using RICVBA's search and inserting from file and immediately erasing it(!) if the block can't be found. The filepath is currently hard coded but will only be changed in one place when our network drives have their names changed (again) next.

 

Thank you both. :)

Link to comment
Share on other sites

... I couldn't understand the AcadFindFile bit (I couldn't get FileSystemObject to work) ....

 

Sorry about that, Dave. :oops:

 

It is *supposed* to replicate the FindFile() Method in .NET/LISP which accepts a string parameter, and automagically returns the first found file path whilst searching through SFSP for said string.

 

 

 

In any event, I'm glad you got it sorted. :beer:

 

Cheers

Link to comment
Share on other sites

yes, it is as BB said. I wanted to check for tis existence and if it was in the drawing already I would insert that one but if its not in the drawing then insert it from a file.

 

As I couldn't understand the AcadFindFile bit (I couldn't get FileSystemObject to work) I have taken the sledgehammer approach now by using RICVBA's search and inserting from file and immediately erasing it(!) if the block can't be found. The filepath is currently hard coded but will only be changed in one place when our network drives have their names changed (again) next.

 

Thank you both. :)

 

glad to be of any help to a "Luminous Being"!

as for the AcadFindFile function you just have to set up a reference to the Windows Scripting Host Object Model via Tools->References in the VBE

but you also have to substitute

AcadFindFile = srchfname

with

AcadFindFile = FSO.GetAbsolutePathName(srchfname)

otherwise in case of finding the file in the last "envpath", which is "" (i.e. searching the entire disk), it'd return the file name only (i.e. "Tri.dwg") without its actual path name.

 

alternatively you can avoid setting up that reference and use late binding. in this case you have to substitute

..., FSO As FileSystemObject,...

with

...FSO As Object,...

 

finally, regardless of which binding you'll use, in the main sub you have to set

dwg = "Tri.dwg"

 

hope this will help

 

bye

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...