Adding blocks with fieldcode attributes using VBA

Discussion in 'AutoCAD' started by John W. Janzen, May 6, 2004.

  1. I am trying to use VB/VBA to insert a block that has attributes contaaining
    fieldcodes. If I use the AutoCAD insret command, everything works as
    expected, however, using the method ThisDrawing.ModelSpace.InsertBlock will
    simply insert the block without the fieldcodes.

    Is there a new method to invoke when using fieldcodes? Or am I missing
    something simpler?

    regards,


    --
    John W. Janzen


    Autodesk Authorized Consultant
    Autodesk Registered Developer

    Cadmin
    www.cadmin.com
    ADT Standards Manager now available for ADT 2005
    www.cadmin.com/ADTStdMngr.htm
     
    John W. Janzen, May 6, 2004
    #1
  2. Can you send me a drawing with a block with
    attributes that contains field codes? I'd
    like to have a look at it. Have you tried to
    to insert the field code by including the
    field expression in the value passed to the
    attributeReference's TextString property?
     
    Tony Tanzillo, May 6, 2004
    #2
  3. Hey Tony,

    Actually, it's ANY AutoCAD block with a field code. I used file name to
    test. Inserting a a field code like in Text and Mtext is simple, but I can't
    see where the field codes are stored within an attribute value of a
    block...otherwise, I don't know what the user has entered! Simply stated, if
    the user creates a block with attributes containing Field Codes, the AutoCAD
    Insert command works fine, it's just the VBA methods that are a mystery.

    Not sure if I explained it any better.

    John
     
    John W. Janzen, May 6, 2004
    #3
  4. Well, I just tried it (defined an attribute with the
    drawing filename field), and inserted it using the
    InsertBlock method (from LISP, not VBA) and it works
    here. I set the attribute definition to PRESET, not
    sure if that has anything to do with it.

    What did find interesting, is that if you saveas to
    another filename, the fields update, but if you then
    issue UNDO 1, the field reverts to the previous
    filename. Not sure that makes sense.
     
    Tony Tanzillo, May 6, 2004
    #4
  5. Hmmm...just adjusted my blocks wto use PRESET, however, still doesn't work
    with VBA.

    This is the routine I am using...

    Sub test()
    Dim ms As IAcadModelSpace2
    Set ms = ThisDrawing.ModelSpace
    Dim pt
    pt = ThisDrawing.Utility.GetPoint(, "Insertion point: ")
    ms.InsertBlock pt, "block", 1, 1, 1, 0
    End Sub


    John
     
    John W. Janzen, May 6, 2004
    #5
  6. Is the field code inserted into your attribute
    definition?
     
    Tony Tanzillo, May 6, 2004
    #6
  7. yes, it is...like I said, if I use the acad insert, it works great, but if I
    use the VBA method, it doesn't.

    John
     
    John W. Janzen, May 7, 2004
    #7
  8. Well, I've boiled it down to extension dictionaries. Fieldcode data is
    attached to the attributes references as extension dictionaries. These are
    created automatically when using the acad insert command, but are missing if
    the InsertBlock method is used.

    The attributes within the block definition contain the extension
    dictionaries, but not in the same format. Trying to map them now and apply
    manually...I'll let you know what I come up with.

    John
     
    John W. Janzen, May 7, 2004
    #8
  9. Hi John,
    Only attributes with mode acAttributeModeConstant seem to work. Are you too
    seeing the same?

    Ravi

    Sub test()
    Dim pt(2) As Double
    Dim bk As IAcadBlock2
    Set bk = ThisDrawing.Blocks.Add(pt, "ModeNormal")
    bk.AddCircle pt, 1
    bk.AddAttribute 0.5, acAttributeModeNormal, "Prompt", pt, "tag",
    "Normal: %<\AcVar Filename \f ""%fn7"">%"
    Call ThisDrawing.ModelSpace.InsertBlock(pt, "ModeNormal", 1, 1, 1, 0)

    Set bk = ThisDrawing.Blocks.Add(pt, "ModeConstant")
    bk.AddCircle pt, 3
    bk.AddAttribute 0.5, acAttributeModeConstant, "Prompt", pt, "tag",
    "Constant: %<\AcVar Filename \f ""%fn7"">%"
    pt(0) = 10: pt(1) = 10
    Call ThisDrawing.ModelSpace.InsertBlock(pt, "ModeConstant", 1, 1, 1, 0)

    pt(0) = 0: pt(1) = 0
    Set bk = ThisDrawing.Blocks.Add(pt, "ModePreset")
    bk.AddCircle pt, 5
    bk.AddAttribute 0.5, acAttributeModePreset, "Prompt", pt, "tag",
    "Preset: %<\AcVar Filename \f ""%fn7"">%"
    pt(0) = 20: pt(1) = 20
    Call ThisDrawing.ModelSpace.InsertBlock(pt, "ModePreset", 1, 1, 1, 0)

    pt(0) = 0: pt(1) = 0
    Set bk = ThisDrawing.Blocks.Add(pt, "ModeVerify")
    bk.AddCircle pt, 7
    bk.AddAttribute 0.5, acAttributeModeVerify, "Prompt", pt, "tag",
    "Verify: %<\AcVar Filename \f ""%fn7"">%"
    pt(0) = 30: pt(1) = 30
    Call ThisDrawing.ModelSpace.InsertBlock(pt, "ModeVerify", 1, 1, 1, 0)

    AcadApplication.Update
    ZoomExtents
    End Sub
     
    Ravi Pothineni, May 7, 2004
    #9
  10. You're correct about what you're seeing. I just confirmed it.

    There are a few other things I noticed as well (see below).

    To insert the field into the attribute text, you don't have
    to fiddle with extension dictionaries (and in fact, you shouldn't
    do that because it requires dealing with custom objects like
    the FIELD object).

    You only have to set the TextString property of the
    AcadAttributeReference to the field expression, which
    for the DWG filename is:

    %<\AcVar Filename \f \"%fn6\">%

    The above gives you just the filename without case
    conversion and path. Also note that if you code this
    as a literal string in VB, you'll have to deal with
    the embedded double quotes.

    I noticed that when I did this using Visual LISP, AutoCAD
    loaded these:

    Loading AEC Base...
    Loading AEC Project Base...
    Loading AEC Base UI...

    My guess is that this is where the field evaluators are.

    Another thing I also noticed, is that Autodesk neglected
    to provide any support for fields to the editors in the
    Properties Palette (not good, because it looks like there
    is no field at all, but rather just regular text).
     
    Tony Tanzillo, May 7, 2004
    #10
  11. Usually when we refer to 'attributes', we mean variable
    block attributes, not constant attributes (which are
    essentially like text entities in the block definition).
     
    Tony Tanzillo, May 7, 2004
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.