The object disconnected from its clients !

Discussion in 'AutoCAD' started by Arnaud Lesauvage, Mar 22, 2005.

  1. Hi all !

    This error raises in Autocad VBA (Autodesk Map 5 SP1) when I call:
    Set oAcadLWPolyLine = oAcadEntity.Copy
    It does NOT happen if I call "Thisrawing.PurgeAll" before !

    Let me first explain what the VBA macro is doing :

    The user selects an item in a listbox on a form. The drawing
    (opened from a dwt), is "SavedAs" with a name that depends on the
    users choice.
    There are then 2 queries (Map queries) being run, which draw
    entities on the drawing.

    This is where I call ThisDrawing.PurgeAll.

    Then, I open a selection Set with :
    gpCode(0) = 8 : gpValue(0) = myLayer
    oSelSet.Select acSelectionSetAll, , , gpCode, gpValue

    I then loop through the SelectionSet and do things depending on
    the entity's type :

    For Each oAcadEntity In oSelSet
    Select Case True
    Case TypeOf oAcadEntity Is AcadLWPolyline
    Set oAcadLWPolyline = oAcadEntity.Copy <- ERROR!!!
    Case TypeOf oAcadEntity Is AcadPolyline
    Set oAcadPolyline = oAcadEntity.Copy
    Case Else
    etc ....
    End Select
    Next oAcadEntity

    I ran the macro step by step : oAcadLWPolyline is defined and
    equals to nothing (the error raises on the first loop). The
    SelectionSet is defined and filled with entities (lwpolylines and
    lines, that's good). oAcadEntity's type is AcadLWPolyline.

    I really don't understand what's going wrong here !
    I need to remove the "PurgeAll", because there are a lot of empty
    layers and unused text styles that I need to keep in the drawing.

    Can someone help me on this ?

    Thanks a lot !

    Arnaud Lesauvage
     
    Arnaud Lesauvage, Mar 22, 2005
    #1
  2. Arnaud Lesauvage

    Joe Sutphin Guest

    You cannot execute the Copy method while iterating through a collection.

    Joe
     
    Joe Sutphin, Mar 22, 2005
    #2
  3. Thanks for your answer Joe.
    Why does it work when I purge the drawing ?
     
    Arnaud Lesauvage, Mar 22, 2005
    #3
  4. Arnaud Lesauvage

    Joe Sutphin Guest

    I don't know. Does the routine work the first time through or the second?

    Can you post the entire routine?

    Joe
    --
     
    Joe Sutphin, Mar 22, 2005
    #4
  5. It's a "one shot" routine : it starts in a new drawing and inserts
    everything that is needed. The PurgeAll is useless, but it makes
    it works...
    I can, but it's kind of huge... I'll post if you want to check for
    yourself though !
    For the moment, I worked around the problem by removing the
    oAcadEntity.Copy as you suggested and by replacing it with
    ThisDrawing.ModelSpace.AddLightWeightPolyline(oAcadEntity.Coordinates).
    It works fine, but I am still very curious !
     
    Arnaud Lesauvage, Mar 22, 2005
    #5
  6. I found it odd as to why as Joe pointed out "You cannot execute the Copy method while iterating through a collection.".
    This is the full help explanation "NOTE You cannot execute this method while simultaneously iterating through a collection. An iteration will open the work space for a read-only operation, while this method attempts to perform a read-write operation. Complete any iteration before you call this method.".
    I would suggest you may be able to iterate the collection adding the items to an array of entities and then process the array. If that doesn't work either you may be able to iterate the collection adding the items handles to an array and then process the array using the HandleToObject method.

    Regards - Nathan
     
    Nathan Taylor, Mar 22, 2005
    #6
  7. You wouldn't have to go to the extreme of using handles. I can't replicate
    your error on my setup, but I think all you would have to do would be to
    replace your "For Each" with a "For i = 0 to ModelSpace.Count-1" type of
    loop. This way, when new objects are added to ModelSpace, they will have
    higher indices than the original count and won't get reprocessed by the
    loop. With a "For Each" loop that makes copies, every time you think you're
    getting one item closer to the end of ModelSpace, you're adding another
    item... you're on a ModelSpace treadmill.

    James



    method while iterating through a collection.".
    while simultaneously iterating through a collection. An iteration will open
    the work space for a read-only operation, while this method attempts to
    perform a read-write operation. Complete any iteration before you call this
    method.".
    to an array of entities and then process the array. If that doesn't work
    either you may be able to iterate the collection adding the items handles to
    an array and then process the array using the HandleToObject method.
     
    James Belshan, Mar 22, 2005
    #7
  8. The Count property will be queried on each iteration so the new objects
    will be processed. Either assign the current value of the property to a
    variable or use a different collection for the iterating; a selection
    set or collection object, for example.
     
    Frank Oquendo, Mar 22, 2005
    #8
  9. Arnaud is actually using a selection set. I think the note in the help may be referring to what your talking about in iterating the modelspace collection. In which case I don't think his problem is to do with the note from the help file.
    Regards - Nathan
     
    Nathan Taylor, Mar 23, 2005
    #9
  10. Arnaud Lesauvage

    Jeff Mishler Guest

    I think that the problem stems from this line:

    Set oAcadLWPolyline = oAcadEntity.Copy

    since the former is dimensioned as a AcadLWPolyline and the latter is
    dimensioned as a AcadEntity, and the copy returns the same object as the
    original.
    Using this, with the oCopyEntity dimensioned as an AcadEntity works for
    me....

    Set oCopyEntity = oAcadEntity.Copy
    Set oAcadLWPolyline = oCopyEntity
     
    Jeff Mishler, Mar 23, 2005
    #10

  11. Hi Jeff,
    Did he original code raise an error ?
    (Set oAcadLWPolyline = oAcadEntity.Copy)
     
    Arnaud Lesauvage, Mar 23, 2005
    #11
  12. Indeed, I am using a Selection Set.
    The NOTE in the documentation actually states that what I was
    doing was wrong (i.e. using the "copy" method), but the error
    message was really surprising !
    A "good" error message could have stated that a write operation
    could not be performed, for instance.
    And furthermore, why can I write to the workspace (if I understand
    the meaning of this word) using
    ThisDrawing.ModelSpace.AddSomeKindOfEntity(), and not using
    oAcadEntity.Copy ?

    Arnaud
     
    Arnaud Lesauvage, Mar 23, 2005
    #12
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.