F.A.O. Frank O.

Discussion in 'AutoCAD' started by Dave F., Jul 12, 2004.

  1. Dave F.

    Dave F. Guest

    Hi Frank

    As your site is down at the moment, could you post your filterlayout routine
    here please.

    Thanks

    Dave F.
     
    Dave F., Jul 12, 2004
    #1
  2. Public Sub FilterLayout(ss As AcadSelectionSet, layoutName As String)

    Dim max As Long, objArray() As AcadEntity

    max = -1

    For i = 0 To ss.Count - 1
    If
    LCase(acadApp.ActiveDocument.ObjectIdToObject(ss.Item(i).OwnerID).Layout.Nam
    e) _
    <> LCase(layoutName) Then
    max = max + 1
    ReDim Preserve objArray(0 To max)
    Set objArray(max) = ss.Item(i)
    End If
    Next

    ss.RemoveItems objArray

    End Sub


    --
    R. Robert Bell


    Hi Frank

    As your site is down at the moment, could you post your filterlayout routine
    here please.

    Thanks

    Dave F.
     
    R. Robert Bell, Jul 12, 2004
    #2
  3. I'll have to beg everyone's pardon here, but I've
    had about enough of this mindless perpetuation of
    ignorance.

    WRT the 'code' you requested, why on Earth would you
    want to use that junk for? Not only is it completely
    unnecessary in most scenarios, but it is probably
    some of the most horrifying, inefficient code I've
    ever seen posted here.

    Do yourself a favor. Don't allow yourself to become
    the victim of someone else's learning mistakes, as
    that is basically what the 'code' Robert just posted
    amounts to.

    Why?

    First, constructing arrays by using "redim preserve"
    once for each element you add to the array, is the
    trademark signature of an entry-level, amateur. But,
    that's not important. What is important, is that this
    practice is also the primary reason why so many VB and
    VBA applications written by beginners, run 10x slower
    than they otherwise can.

    So, WRT the 'REDIM PRESERVE' business for building
    arrays, don't ever do this!!!

    If you need to build an array and the final size
    cannot be easily determined from the outset, then
    you use a collection, which can be appended to
    much more efficiently.

    Second, the manner in which that code goes about
    filtering by layout is basically like taking the
    high road to China.

    When you have a selection set and want to process
    only entities in that reside on a given layout,
    then all you need to do is integrate a simple If/Then
    construct in your primary selection set processing
    loop, using ObjectID of the desired layout's Block,
    and the OwnerID of each entity.

    The following represents the main processing loop for
    your selection set, where you perform whatever
    operations you're doing on each entity. All you need
    to do, is skip over the entities that are not on the
    desired layout, and all you have to do to find out
    what layout an entity resides on, is to compare the
    value of its OwnerID to the ObjectID of the Layout's
    Block object:

    Public Sub ProcessSelectionSet(SS As AcadSelectionSet)

    ' Assume that you only want to process
    ' entities on the layout named "Floor Plan"

    ' First, we get the ObjectID of the AcadBlock
    ' associated with the "Floor Plan" Layout:

    Dim BlockID as Long
    BlockID = ThisDrawing.Layouts("Floor Plan").Block.ObjectId

    ' >>> PAY ATTENTION <<<<
    '
    ' The value of an entity's OwnerID property is
    ' EQUAL to the value of the ObjectID property
    ' of the AcadBlock object that is associated
    ' with the layout which the entity resides on.

    ' So.... All you must do is process the entities in
    ' your selection set, and skip those that are not on
    ' the desired layout:

    Dim Entity As AcadEntity

    For Each Entity in SS
    If Entity.OwnerID = BlockID Then
    ' This entity is on the desired Layout
    ' So, just do what you want to it.
    End If
    Next Entity
    End Sub

    You see, what those who wrote and perpetuate that kind
    of 'code' didn't understand, is that determining if a
    given entity resides on a given layout, is far simpler
    than all of the crap you see them going through in that
    code you asked for.

    The most important thing to remember, is that there is
    no need to 'pre-process' your selection set and remove
    entities on all but the desired layout, unless you're
    going to pass the selection set to some code that you
    don't have, which unconditionally processes all of the
    elements in the selection set. In a scenario like that,
    then you must filter the selection set, but if all you
    need to do is loop through the selection set and do
    something to each entity on a specific layout, then the
    need to preprocess it to remove entities on all other
    layouts, is entirely pointless.

    If you want to write a generic function to filter the
    entities not on a desired layout, you can use something
    similar to the above example along with a collection
    object, to build a far more efficient filtering function
    that will not make your code run like a slug.

    I'll leave that as an exercise.
     
    Tony Tanzillo, Jul 12, 2004
    #3
  4. Dave F.

    antmjr Guest

    Just out of curiosity, from autocad vba programming by kramer and gibb, pg.110
    If you need to iterate a selection and to *change* it (maybe removing some elements), start from the end:


    Sub My_Filter2(SS As AcadSelectionSet, ET As Object)
    Dim I As Integer
    Dim Ent(0) As Object /*this is another witty trick*/

    I = SS.Count
    While (I > 0)
    I = I - 1
    Set Ent(0) = SS.Item(I)
    If Ent(0).EntityType <> ET.EntityType Then
    SS.RemoveItems (Ent)
    End If
    Wend
    End Sub
     
    antmjr, Jul 13, 2004
    #4
  5. Dave F.

    TomD Guest

    Thanks for your perspective on this, Tony. Your post couldn't have been
    more timely for me.

    In particular, thanks for mentioning collections. I've always wondered why
    go through the redim stuff when a generic collection would handle most
    situations 'better'. I made the apparently erroneous assumption that a
    collection added overhead, so I've been avoiding going that route.
     
    TomD, Jul 13, 2004
    #5
  6. Dave F.

    Dave F. Guest

    Thanks Tony

    TomD - I've been told on here that using scripting dictionary
    (tools-reference- Microsoft scripting runtime) is faster/more useful than
    even collections. The two I find most useful are Exists & Remove.

    Dave F.
     
    Dave F., Jul 13, 2004
    #6
  7. Dave F.

    TomD Guest

    Ooook. I'll have to look into that. The only think I've really done with
    WSH is for files (reading/writing ASCII and getting file lists, etc.).

    Thanks for the tip.
     
    TomD, Jul 13, 2004
    #7
  8. Dave F.

    Dragnsbld Guest

    Hey Tony,
    Have you used Collections to get an unknown array of text from a DB to be inserted in a DWG at various locations within the DWG?
     
    Dragnsbld, Jul 13, 2004
    #8
  9. I must apologize to the ng, as I did not review that code before I posted
    it. All I saw was a request for a function that used to be posted on the
    website, and I did not take the time to review the code's efficiency. Sorry,
    all.
     
    R. Robert Bell, Jul 13, 2004
    #9
  10. Dave F.

    TomD Guest

    None necessary, IMO. Your efforst here are appreciated and helpful to many.
     
    TomD, Jul 13, 2004
    #10
  11. Thanks for the kind post, but I have to agree with Tony, posting bad code is
    just... bad. ;^)
     
    R. Robert Bell, Jul 13, 2004
    #11
  12. Don't feed the troll. There's no need to apologize.

    Did you receive a request for the most efficient way to go about a given
    task or did you receive a request for a specific routine?
     
    Frank Oquendo, Jul 13, 2004
    #12
  13. It always amazes me how you can offer the most insightful analysis
    combined with the most odious attitude known to man.

    Don't ever change.

    P.S.

    Thanks for the tips.
     
    Frank Oquendo, Jul 13, 2004
    #13
  14. Dave F.

    Dave F. Guest

    Ooook

    Are you an Orangutan Librarian?
    (Not sure if you'll know what I mean. There's a British Author called Terry
    Pratchett whose written numerous hilarious books set in a fantasy world
    called Discworld. One of the characters is the librarian at the wizards
    university who accidentally got turned into an orangutan by an unstable
    spell. The only word he uses to comunicate is Ooook, in all it's various
    inflections.)

    If you like a laugh, I suggest you check him out.

    Cheers
    Dave F.
     
    Dave F., Jul 13, 2004
    #14
  15. Dave F.

    Dave F. Guest

    Here, here.

     
    Dave F., Jul 13, 2004
    #15
  16. Dave F.

    TomD Guest

    No, I'm not, but sounds interesting............thanks. I think I'll check
    that out.

    Terry
     
    TomD, Jul 13, 2004
    #16
  17. Dave F.

    TomD Guest

    Yeah, I agree, but..............Just know that it is appreciated.
     
    TomD, Jul 13, 2004
    #17
  18. No. But collections are just a generic feature that can be used
    like an array, except that they're better at operations involving
    indeterminate quantities.



    within the DWG?
     
    Tony Tanzillo, Jul 13, 2004
    #18
  19. Dave F.

    TomD Guest

    I don't mean to beat a dead horse, or anything, but generally speaking, is
    there any major performance issues?
     
    TomD, Jul 13, 2004
    #19
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.