dbroada Posted November 13, 2014 Share Posted November 13, 2014 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. Quote Link to comment Share on other sites More sharing options...
RICVBA Posted November 13, 2014 Share Posted November 13, 2014 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 Quote Link to comment Share on other sites More sharing options...
BlackBox Posted November 13, 2014 Share Posted November 13, 2014 (edited) 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 November 14, 2014 by BlackBox Quote Link to comment Share on other sites More sharing options...
dbroada Posted November 14, 2014 Author Share Posted November 14, 2014 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. Quote Link to comment Share on other sites More sharing options...
BlackBox Posted November 14, 2014 Share Posted November 14, 2014 ... I couldn't understand the AcadFindFile bit (I couldn't get FileSystemObject to work) .... Sorry about that, Dave. 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. Cheers Quote Link to comment Share on other sites More sharing options...
RICVBA Posted November 14, 2014 Share Posted November 14, 2014 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 Quote Link to comment Share on other sites More sharing options...
dbroada Posted November 14, 2014 Author Share Posted November 14, 2014 RICVBA, thanks for that. I will read it again next week and see what I can learn. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.