Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Non-Attribute Block Create VBA Help Needed

Status
Not open for further replies.

okswug

IS-IT--Management
Feb 9, 2006
8
0
0
US
Hopefully this is a good place for this question.

Using VBA, I need to create an AutoCAD Block on my drawing. The Block doesn't exist, thus the create part.

I need to create two columns of data (a grid so to speak). The first column is a control point number, the second column has the coordinates in it. Both should be MTEXT, I guess (I might have to have the coordinates "word wrap" to be three lines inside a box...don't know yet)

I need to have each column outlined with a line, so it looks like a grid, or an Excel table.

I don't want attributes, because the data is coming from an external database, and we don't want any manual chnages to the blocks.

So, has anybody got any clues to get me going quick on my way to Block Creation?

TIA
 
Hi okswug,

Can you post what you have so far? Or are you starting from scratch?

Let us know, I'm sure we can be of some help.

Todd
 
I'm basically starting from scratch. I have all the data in "memory" from the database, and the linking back and forth to the DB is all done. I just got to draw the boxes and put text in them.

Sounded easy, but it gets hard for my brain when I start thinking about text height and where 0,0 is located in respect to the placement of the block....
 
Hi okswung,

One option is to create the MText, then get the bounding box, use these numbers and determine an offset, then draw the box around the text and create your block. Be careful, you'll need to generate unique block names for each line (otherwise you'll be just redefining each line and they'll all read the same.) As for your text height, using the bounding box method will allow your height to fluctuate if need be. Your insertion point can be one of your offset corners from your box around your text.

HTH
Todd

 
I think I can look into that. I assume that if they want the coordinates "stacked", that I would use MText and "stack" them, or use individual text strings. If they don't want them "stacked", it doesn't matter. If "stacked", I'll put that string in first, and then align the Control Point string with it, like the following.

------------------
| T2 | 1'-0" |
| | 1'-3 1/2" |
| | 0'-0" |
------------------

Now all I got to do is code the dumb thing :)
 
Okay, if I created a Block and inserted it and wish to redefine the block, how do I delete it?

I treid this:

ThisDrawing.Blocks.Item("TextBlock").Delete

and it says it is referenced. I deleted it manually (so I didn't write code to remove it from the drawing) first.

Any ideas on how to remove or redefine it?
 
Hi okswug,

To delete the block, you'll first need to delete the references (the blocks inserted into the drawing), then you can delete the block definition.

Check out FAQ-5792 on how to create selection sets and look in the example usage of BuildFilter - it has almost exactly what you need.

To redefine blocks, in your case anyway, you'll need to add your item to the block (text in this case) but AutoCAD has some funky methods to do this. This routine should help you out without having to go into all the gory details:
Code:
Public Sub AddObjectsToBlock(blkRef As AcadBlockReference, entArray() As AcadEntity, Optional delObj As Boolean = True)
  '
  ' Title     : AddObjectsToBlock
  '
  ' Version   : ?.?.?
  ' Author(s) : Frank Oquendo
  ' Created   : 02/22/2001 09:08:41 AM
  ' Last Edit : 02/22/2001 09:08:41 AM, TDC
  '
  ' Description:
  ' ¯¯¯¯¯¯¯¯¯¯¯¯
  '   This routine is used to add the specified objects
  '   to a given block definition.
  '
  ' Additional files/functions required:
  ' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  '   1) None
  '
  ' Example usage:
  ' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  '    AddObjectsToBlock blkRef, ents, False
  '
  ' Requires the following variables:
  ' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  '    * Input assignments
  '      1) blkRef   = Block reference (not definition) to update.
  '      2) entArray = Array of entities to add to the block definition.
  '      3) delObj   = Flag to have the routine drawing entities.
  '
  ' Updates:
  ' ¯¯¯¯¯¯¯¯
  ' 03/22/2006 09:08:41 AM - 1.0.0 - TDC
  '    1) Initially created
  '
  ' Future considerations:
  ' ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  '    1) None
  '
  ' AddObjectsToBlock begins here:
  ' ——————————————————————————————————————————————————

    
  Dim blkDef As AcadBlock, origin(0 To 2) As Double, i As Long
  
  origin(0) = 0: origin(1) = 0: origin(2) = 0
  
  Set blkDef = ThisDrawing.Blocks(blkRef.Name)
  
  For i = LBound(entArray) To UBound(entArray)
    entArray(i).Move blkRef.InsertionPoint, origin
  Next
  
  ThisDrawing.CopyObjects entArray, blkDef
  
  If delObj Then
    For i = LBound(entArray) To UBound(entArray)
      entArray(i).Delete
    Next
  End If

End Sub

Don't forget after your call to the above routine you'll need to perform an entity update.

HTH
Todd
 
Thanks. I figured out my "can't delete problem. I had it "grabbed" earlier in the code, so it was "referenced". Anyway, I'll read the thread you posted to educate myself anyway. The code you posted makes sense and works.

I tried to check the Blocks Collection for the block, before I deleted it, and all was well...except if it isn't there, then it triggers an error. I was looking for that specific block [ Item("TextBlock").Delete ] and if it isn't there, whammo. So I guess next time I have time, I'll grab all the entities in the block collection and look at them individually, rather than just ask.

Hard to write some dumb program when you can write for 10min every 4 hours, huh?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top