Polyline Length as Attribute

Polyline Length as Attribute

heatlink
Participant Participant
617 Views
8 Replies
Message 1 of 9

Polyline Length as Attribute

heatlink
Participant
Participant

Hello guys!, this is my first post ever.

 

I've a LISP that takes the Length of the Polyline and place it as value on a block attribute.

 

But the length format is; 59500 (example) and I want it to be 59+500,...59+459 and not 59459.... etc, depending on the length of the polyline selected.

heatlink_0-1657095601824.png

 

I'll also post the Rutine in case someone else finds it useful

 

PD: I'll also wanted to add PK XXXX(length of the polyline) but I not able to be able to add it,

 

Any help will be highly appreciated!.

 

 

0 Likes
Accepted solutions (1)
618 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

Replace this much:

 

(setq leng (rtos
  (vlax-get-property obj "Length"
    )
  2
  0 ; <--precison 3 decimals
  )
  )

 

with this:

 

(setq

  meas (rtos (vlax-get-property obj "Length") 2 0)

  leng

    (strcat

      "PK "

      (substr meas 1 (- (strlen meas) 3))

      "+"

      (substr meas (- (strlen meas) 2))

    ); strcat & leng
); setq

 

EDIT:  That assumes that you want only the last 3 digits separated with a +.  If the measurement ever came to more than 6 digits, the above would change "1234567" to "PK 1234+567".  If you want groups of 3 separated, to have that as "PK 1+234+567" more code would be required.

 

[Also, add meas to the localized variables list at the top:

(defun c:LTA (/ atent attobj ent leng meas obj)

Kent Cooper, AIA
0 Likes
Message 3 of 9

ВeekeeCZ
Consultant
Consultant
Accepted solution

The question is what if the value is less than 1000. Whether it should be +500 or 0+500. The lower code does the latter.

 

(vl-load-com)

(defun c:LTA ( / *error* dnz e l a)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if dzn (setvar 'dimzin dzn))
    (princ))
  
  (setq dzn (getvar 'dimzin))
  (setvar 'dimzin 0)
  
  
  (while (setq e (car (entsel "\nSelect polyline: ")))
    (if (and (or (not (vl-catch-all-error-p (setq l (vl-catch-all-apply 'getpropertyvalue (list e "Length")))))
		 (prompt "\nError: Wrong selection, need a polyline. Try again!"))
	     (setq a (car (nentsel "\nPick attribute: ")))
	     (setq a (entget a))
	     )
      (vl-catch-all-apply 'setpropertyvalue (list (cdr (assoc 330 a))
						  (cdr (assoc 2 a))
						  (strcat "PK = " (vl-string-translate "." "+" (rtos (/ l 1000) 2 3)))))))
  (princ)
  )

 

Also, if you want to try Kent's suggestion, replace both what with meas in his code.

0 Likes
Message 4 of 9

heatlink
Participant
Participant
I tried Kent suggestion but I'm getting it didn't work by replacing the code and adding the variables at the top.

Yours works fine!, But I can't quite figure out how to add the "PK = " at the beginning of the value of the polyline length on the attribute
0 Likes
Message 5 of 9

ВeekeeCZ
Consultant
Consultant

The code updated.

0 Likes
Message 6 of 9

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

Also, if you want to try Kent's suggestion, replace both what with meas in his code.


Thanks for catching that -- a quickie temporary variable name for trial, which I didn't manage to correct to something more meaningful in all locations.  I've fixed in my Message above, so I hope it works now.

Kent Cooper, AIA
0 Likes
Message 7 of 9

ВeekeeCZ
Consultant
Consultant

@Kent1Cooper wrote:

@ВeekeeCZ wrote:

Also, if you want to try Kent's suggestion, replace both what with meas in his code.


Thanks for catching that -- a quickie temporary variable name for trial, which I didn't manage to correct to something more meaningful in all locations.  I've fixed in my Message above, so I hope it works now.


One more still there... 🙂

0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

One more still there... 


Dag-nab-it....  Fixed now.  Thanks again.

Kent Cooper, AIA
0 Likes
Message 9 of 9

heatlink
Participant
Participant

Thanks so much to both you gys Kent and Beekee, for your help!.

 

Much appreciated!.

0 Likes