get index number of object

Discussion in 'AutoCAD' started by Joe Burke, Feb 16, 2004.

  1. Joe Burke

    Joe Burke Guest

    Wondering if there's a better way to do this. I want the index number of a vla-object
    for use with (vla-item <space> <index no>). I've Goggle searched, but didn't come up
    with much.

    Following looks at the appropriate collection for the object passed. Kinda seems like
    overkill having to examine the entire collection. Though it is fairly fast. About 0.5
    seconds or less with 50k objects in model space.

    ;; 2/15/2004
    ;; argument: vla-object
    ;; returns the index number of vobj in the current space
    (defun GetObjIdx ( vobj / doc cnt idx )
    (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
    (setq cnt 0)
    (if (= "Model" (getvar "ctab"))
    (vlax-for x (vla-get-ModelSpace doc)
    (if (not (equal vobj x))
    (setq cnt (1+ cnt))
    (setq idx cnt)
    )
    )
    (vlax-for x (vla-get-PaperSpace doc)
    (if (not (equal vobj x))
    (setq cnt (1+ cnt))
    (setq idx cnt)
    )
    )
    )
    idx
    ) ;end

    Thanks
    Joe Burke
     
    Joe Burke, Feb 16, 2004
    #1
  2. Hi Joe,

    One thing comes to mind, you could wrap all of
    that in a (vl-catch-all-apply), and once the item you
    want is found issue an (exit) to stop the (vlax-for)

    Tony showed us this a while back in a thread I started
    (forget which one exactly)
     
    Jason Piercey, Feb 16, 2004
    #2
  3. Joe Burke

    Joe Burke Guest

    Hi Jason,

    Thanks. I'll see if I can find that thread.

    Joe Burke
     
    Joe Burke, Feb 17, 2004
    #3
  4. Jason Piercey, Feb 17, 2004
    #4
  5. Joe Burke

    Joe Burke Guest

    Thanks again, Jason.

    I'm reading it now. Good stuff. Google search on "stop vlax-for" found it.

    Joe Burke
     
    Joe Burke, Feb 17, 2004
    #5
  6. Tony Tanzillo, Feb 17, 2004
    #6
  7. Joe Burke

    Joe Burke Guest

    Tony,

    To make a list of the primary objects in the current space after some object. Party
    practical application, partly just to explore alternate methods using ActiveX and
    vlisp. I wanted to do it without using entnext, and things like DXF code 410.

    Maybe I don't need the index number to do it?

    Joe Burke
     
    Joe Burke, Feb 17, 2004
    #7
  8. What's the point to making a list of indices
    into a collection if you can just make a list
    of the vla-objects themselves, or object ids?

    They all consume the same amount of memory.



    AcadX for AutoCAD 2004 Beta 1
    http://mysite.verizon.net/~vze2vjds/acadx/AcadX16.zip
     
    Tony Tanzillo, Feb 17, 2004
    #8
  9. Joe Burke

    Joe Burke Guest

    Tony,

    I'm fairly new at dealing with collections. Something like the following doesn't come
    as second nature to me. I think it crudely demonstrates your point. I don't need
    (vla-item <collection> <index no>) to make a list of objects in a case like this.

    ;; returns a list of primary vla-objects after vobj
    ;; returns nil when last object is passed or
    ;; vobj is not in the current space
    (defun AfterObj ( vobj / doc flag objlst )
    (setq doc (vla-get-ActiveDocument (vlax-get-acad-object)))
    (if (= "Model" (getvar "ctab"))
    (vlax-for x (vla-get-ModelSpace doc)
    (if (equal x vobj)
    (setq flag T)
    )
    (if flag
    (setq objlst (cons x objlst))
    )
    ) ;for
    (vlax-for x (vla-get-PaperSpace doc)
    (if (equal x vobj)
    (setq flag T)
    )
    (if flag
    (setq objlst (cons x objlst))
    )
    ) ;for
    ) ;if
    (cdr (reverse objlst))
    ) ;end

    There's huge holes in my understanding of this stuff. Until a few days ago, I didn't
    know you can access an object with an index number using the Item method. One hole
    plugged. So a trip down the wrong road was still a useful lesson.

    Suggestions for improving the above are welcome as usual.

    Thanks
    Joe Burke
     
    Joe Burke, Feb 17, 2004
    #9
  10. Joe Burke

    Joe Burke Guest

    Tony,

    The AfterObj function is intended to replace something like the following. It also
    deals with the fact in some cases returning a simple list of vla-objects is more
    useful than a selection set. For instance, using the AppendItems method to add
    objects to an existing group.

    ;; argument: an ename
    (defun SSAfterEnt (ent / ss entlst)
    (and
    (setq ss (ssadd))
    (while
    (setq ent (entnext ent))
    (setq entlst (entget ent))
    (if
    (and
    (not (wcmatch (cdr (assoc 0 entlst)) "ATTRIB,VERTEX,SEQEND"))
    (eq (cdr (assoc 410 entlst)) (getvar "ctab"))
    )
    (ssadd ent ss)
    ) ;if
    ) ;while
    ) ;and
    (if (> (sslength ss) 0) ss)
    ) ;end

    Joe Burke
     
    Joe Burke, Feb 17, 2004
    #10
  11. Joe Burke

    Joe Burke Guest

    Jason,

    This thread has strayed a bit from my original question.

    Just wanted to let you know I plugged Tony's vl-catch... and exit idea into the
    GetObjIdx function. Now it's on the order of 20 times faster when the object in
    question is somewhere near the beginning of the collection. And of course, it still
    does the job.

    So thanks to you for the pointer. And thanks to Tony for the idea.

    Regards
    Joe Burke
     
    Joe Burke, Feb 17, 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.