Select last 'x' line lines created

Select last 'x' line lines created

bobbyrubyii
Contributor Contributor
1,292 Views
10 Replies
Message 1 of 11

Select last 'x' line lines created

bobbyrubyii
Contributor
Contributor

I need to select the last "x" lines created.

 

Background....

I have a command that draws 'x' # of lines on the same color and layer to a dynamic # of insertion points . The command then selects all lines of that color in the drawing and puts their length at the midpoint in an MTEXT, which works fantastic for new drawings. Just not ones that are in process.

 

It would be better if I could just access the last lines created so it does not duplicate footage's elsewhere.

0 Likes
1,293 Views
10 Replies
Replies (10)
Message 2 of 11

dlanorh
Advisor
Advisor
Is this "command" a lisp routine? If so please attach it. How many lines do you want to "recall"?

I am not one of the robots you're looking for

0 Likes
Message 3 of 11

bobbyrubyii
Contributor
Contributor

The amount of lines will be dynamic based of the amount of blocks the lines are drew between.  You will find the lisp put together from various pieces of code found on the net below.  I have other things loaded in the environment so I think all the parts are included here that make it work.

 

(defun C:CBBIP ; = CONNECT BLOCKS BY INSERTION POINTS - ORIGINALLY LBS
  (/ *error* ent fmt idx ins ocs par sel spc txt typ uxa ss blk att blkinslist)
  (prompt "\nTo Link Blocks with Lines in Attribute Sequence,")
  (if (setq ss (ssget '((0 . "INSERT") (66 . 1) (2 . "ACMAP_ANN_TEMPLATE_PERMIT-FEATURES") ))); could omit (66 . 1)*
    (progn ; then
      (repeat (setq n (sslength ss)); step through selection set
        (setq
          blk (ssname ss (setq n (1- n))); Block entity name
          att (entnext blk); Attribute entity name
          blkinslist ; list of integer equivalents of Attribute values paired with insertion points
             (cons
              (list 
				(atoi 
				    (LM:getattributevalue att "COMMENT_1")
					;(cdr 
					;	(assoc 1 (entget att)
					;	); assoc
					;); cdr
				); atoi - turns string into integer
				; gets the insertion point here
				(trans 
					(cdr 
						(assoc 10 
							(entget blk)
						); assoc
					); cdr
					0 1
				); trans
				; finish getting the insertion points
			  ); list
              blkinslist
            ); cons
        ); setq
      ); repeat
      (setq blkinslist ; replace former content
        (mapcar 'cadr ; keep only insertion points after:
          (vl-sort blkinslist '(lambda (a b) (< (car a) (car b)))); sorting in Attribute-value order
        ); mapcar
      ); setq
      (command "_.line"); consider Polyline instead, possibly with specified width
      (apply 'command blkinslist); feed points to Line command
      (command ""); end Line command
	  (setq fmt "%lu6") ;; Field Formatting

    (defun *error* ( msg )
        (LM:endundo (LM:acdoc))
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
    
    (if
        (setq sel
            (ssget "_A"
                (list
				   (cons 62 6) ; MAGENTA
                   '(0 . "LINE")
                   '(-4 . "<NOT")
                       '(-4 . "<AND")
                           '(0 . "POLYLINE")
                           '(-4 . "&")
                           '(70 . 80)
                       '(-4 . "AND>")
                   '(-4 . "NOT>")
                    (if (= 1 (getvar 'cvport))
                        (cons 410 (getvar 'ctab))
                       '(410 . "Model")
                    )
                )
            )
        )
        (progn
            (setq spc
                (vlax-get-property (LM:acdoc)
                    (if (= 1 (getvar 'cvport))
                        'paperspace
                        'modelspace
                    )
                )
            )
            (setq ocs (trans '(0.0 0.0 1.0) 1 0 t)
                  uxa (angle '(0.0 0.0) (trans (getvar 'ucsxdir) 0 ocs t))
            )
            (LM:startundo (LM:acdoc))
            (repeat (setq idx (sslength sel))
                (setq ent (ssname sel (setq idx (1- idx)))
                      par (vlax-curve-getparamatdist ent (/ (+ 0 (vlax-curve-getdistatparam ent (vlax-curve-getendparam ent))) 2.0)) ;edit # here
                      ins (vlax-curve-getpointatparam ent par)
                      typ (cdr (assoc 0 (entget ent)))
                )
				
                (setq txt
                    (vlax-invoke spc 'addmtext ins 1.0
                        (strcat
                            "%<\\AcExpr (%<\\AcObjProp Object(%<\\_ObjId " (LM:objectid (vlax-ename->vla-object ent)) ">%)."
                            (cond
                                (   (= "CIRCLE" typ) "Circumference")
                                (   (= "ARC"    typ) "ArcLength")
                                (   "Length"   )
                            )
                            " \\f \"" fmt "\">% + 0) \\f \"%lu5%pr0\">%'"  ;edit # here
                        )
                    )
                )
				;(vla-put-textstring txt(rtos(atof(vla-get-textstring txt))2 0)) ;Edited by Bobby - http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/need-to-result-to-be-rounded-to-whole-no-decimal-places/m-p/5908206
                (vla-put-backgroundfill  txt :vlax-false) ; edited by Bobby
                (vla-put-attachmentpoint txt acattachmentpointmiddlecenter)
                (vla-put-insertionpoint  txt (vlax-3D-point ins))
                (vla-put-rotation txt (LM:readable (- (angle '(0.0 0.0 0.0) (trans (vlax-curve-getfirstderiv ent par) 0 ocs t)) uxa)))
            )
            (LM:endundo (LM:acdoc))
        )
    )
    (princ)
    ); progn
    (prompt "\nNo such Blocks selected."); else
  ); if
); defun

;; Get Attribute Value  -  Lee Mac
;; Returns the value held by the specified tag within the supplied block, if present.
;; blk - [ent] Block (Insert) Entity Name
;; tag - [str] Attribute TagString
;; Returns: [str] Attribute value, else nil if tag is not found.


(defun LM:getattributevalue ( blk tag / val enx )
    (while
        (and
            (null val)
            (setq blk (entnext blk))
            (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk)))))
        )
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (setq val (cdr (assoc 1 (reverse enx))))
        )
    )
)
;; Start Undo  -  Lee Mac
;; Opens an Undo Group.

(defun LM:startundo ( doc )
    (LM:endundo doc)
    (vla-startundomark doc)
)

;; End Undo  -  Lee Mac
;; Closes an Undo Group.

(defun LM:endundo ( doc )
    (while (= 8 (logand 8 (getvar 'undoctl)))
        (vla-endundomark doc)
    )
)

;; Active Document  -  Lee Mac
;; Returns the VLA Active Document Object

(defun LM:acdoc nil
    (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
    (LM:acdoc)
)
0 Likes
Message 4 of 11

dlanorh
Advisor
Advisor

Unfortunately these lines in the code

 

      (command "_.line"); consider Polyline instead, possibly with specified width
      (apply 'command blkinslist); feed points to Line command
      (command ""); end Line command

Constructs all the lines in one command, making it impossible to access them all. However the comment after the first line above suggests using pline. You could try making the first line read

 

      (command "_.pline")
     

and testing if this still works. If so this will construct a single entity which can be accessed immediately after with

 

(setq myline (entlast))

I am not one of the robots you're looking for

0 Likes
Message 5 of 11

bobbyrubyii
Contributor
Contributor

Wow, is there a way I could structure some sort of loop to pass the points 1 at time to the line command?

 

I could then restructure the code to draw the line, measure it, then create the text as it goes.

0 Likes
Message 6 of 11

ВeekeeCZ
Consultant
Consultant

Try this test function... you can use what ever command you want. That 'apply function should not stop you.

 

(defun c:Test ( / enl ss blkinslist pt)
  
  (setq enl (entlast)) ; mark !
  
  (while (setq pt (getpoint "\rPoint for line: "))
    (setq blkinslist (cons pt blkinslist)))
  
  (command "_.line")
  (apply 'command blkinslist)
  (command "")
  
  ;;; whatever you do here.
  
  (setq ss (ssadd))
  
  (while (setq enl (entnext enl)) ; next entity to mark
    (if (= "LINE" (cdr (assoc 0 (entget enl))))  ; if its a line
      (ssadd enl ss))) ; add to selection set
  
  (if (> (sslength ss) 0)
    (sssetfirst nil ss)) ; highlight ss.
  
  (princ)
  )
0 Likes
Message 7 of 11

dlanorh
Advisor
Advisor

If making the polyline works, It would be easier to explode the polyline using the visual lisp explode method.

 

(setq n_objs (vlax-invoke (vlax-ename->vla-object (entlast)) 'explode))

n_objs should be a list of all the individual lines in the polyline as objects, whilst preserving the polyline. If you don't want the original polyline, you can then delete it.

I am not one of the robots you're looking for

0 Likes
Message 8 of 11

bobbyrubyii
Contributor
Contributor

The polyline would work but I would want it to be exploded else the footage's won't be placed in their proper place, at least I do not think.

 

Could you provide an example of where this piece of code would go?

0 Likes
Message 9 of 11

dlanorh
Advisor
Advisor
      (command "_.pline"); consider Polyline instead, possibly with specified width
      (apply 'command blkinslist); feed points to Line command
      (command ""); end Line command
      
      (setq tmp (entlast)
            n_objs (vlax-invoke (vlax-ename->vla-object tmp) 'explode))
      );end_setq      
      (command "_erase" tmp "")
      
      (setq fmt "%lu6") ;; Field Formatting

See above

If the "_.pline" works, the red text should be inserted immediately after as indicated. The blue text, if you want to delete the polyline.

 

Don't forget

1. to add tmp and n_objs to the local variable list at the start of the lisp

2. This is a list of objects, not entity names

3. Test thoroughly on a copy of a drawing as there may be bugs to iron out

I am not one of the robots you're looking for

0 Likes
Message 10 of 11

bobbyrubyii
Contributor
Contributor

Under the gun so I will test and accept as solution if this works.  Might take up to a week before I get more time to play with the code as it works as is for right now.

 

Thank you all very much for the assistance.

0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

[Unfortunate double posting -- if you get a good solution in either this thread or the other one, at least refer to it in the one that it's not in, so people who find the "wrong" thread in a Search will be able to find the solution.]

Kent Cooper, AIA
0 Likes