Our office uses VBA to run a topography routine on points collected by land surveyors in the field. The topo routine looks through all the points in a textfile, inserting them into the modelspace, and can color code points based on their descriptions. The routine also looks at the descriptions, and inserts block symbols where necessary (for trees, manholes, power, etc). To avoid large file sizes, the blocks are stored locally on our machines and attached as external references when needed.
There was a big difference between AutoCAD's VBA and the VBA I had to write to implement this correctly in IntelliCAD. Again, as I've found with other routines, IntelliCAD requires slightly more proper code with error checking. The specific difference is that, when adding an item to the drawing database (a layer, a block, a selection set), IntelliCAD WILL crash if the item already exists. AutoCAD gracefully uses the existing item. So...error checking is your friend in IntelliCAD.
Also, it seems necessary to attach the external reference before inserting a block of that name. The code I've converted from AutoCAD doesn't seem to need that pre-requisite. It simply inserts the block from file, everytime, since it handles the error check gracefully.
The below code shows the error checking routine, which looks to see if the block has already been inserted into the drawing's collection of blocks. If not, then it attaches the external reference (which by the required arguments, also inserts it into modelspace). If another block of the same type is to be inserted, the check will see that the external reference is already attached, and use that block instead.
I hope this helps out anyone who's working on code that automatically inserts blocks into the modelspace. The LAYER_Exists function works much in the same way...check to see if it exists; if it does, set a pointer to that existing layer; if not, create the layer and set a pointer to it. Use the pointer to set the layer of what you're inserting into modelspace (block, text, etc.)
Code: Select all
'checks to see that the layer the block is to be inserted on already 'exists. If the layer exists, it sets the layerObj to the layer specified. 'If not, it creates the layer and sets the layerObj. 'The layerObj can then be used to set the layer of the block object. Call LAYER_Exists(LayerName, layerObj) 'This creates the path of the block to be inserted. 'txtString is the descriptor that is pulled out of the point file. blkName = BlkPath + txtString + ".dwg" foundBLK = False 'Looks through blocks collection to see if external reference has been added For Each Blk In ThisWorkspace.ActiveDocument.Blocks If Blk.Name = txtString Then Set BlkInsObj = ThisWorkspace.ActiveDocument.ModelSpace.InsertBlock(startPoint, txtString, CDbl(Dscale.text) / 10, CDbl(Dscale.text) / 10, 1#, rotang) 'Sets the layer for the block in modelspace BlkInsObj.Layer = layerObj.Name BlkInsObj.Update foundBLK = True End If Next Blk 'If external reference hasn't been added, add the block to the drawing If foundBLK = False Then Set ExtRef = ThisWorkspace.ActiveDocument.ModelSpace.AttachExternalReference(blkName, txtString, startPoint, CDbl(Dscale.text) / 10, CDbl(Dscale.text) / 10, 1#, rotang, False) 'Sets the layer for the block in modelspace ExtRef.Layer = layerObj.Name ExtRef.Update End If