### Author Topic: Edited: Check my work for: Find length of line and compare it to dimscale  (Read 430 times)

0 Members and 1 Guest are viewing this topic.

#### jlogan02

• Bull Frog
• Posts: 275
##### Edited: Check my work for: Find length of line and compare it to dimscale
« on: June 23, 2022, 03:47:23 PM »
I've adjusted this code written by Clindner on AutoDesk's forums.

I've adjusted it to divide a line by 34. The last (if... is not working.
I want to compare the current dimscale with the result of dividing the line by 34.

If they are equal tell me they are equal
if not equal set dimscale to what the div_factor returned.

Edit:
I've got this to work but want to make sure I'm doing it correctly.

Code - Auto/Visual Lisp: [Select]
1. (defun C:Foo ( / dim div_factor line_ent line_length)
2.   (setq dim (getvar 'dimscale))
3.   (setq line_ent (ssget "X" '((0 . "LINE")(8 . "GetLine")))
4.           line_length (vla-get-length (vlax-ename->vla-object (cdar (entget (ssname line_ent  0))))))
5.   (princ "\nLength of selected line: ")(princ line_length)
6.   (setq div_factor (atoi "34"))
7.   (princ "\nDivided length: ")(setq ss (princ (/ line_length div_factor)))
8.   (if (= dim ss)
9.     (prompt "\nDimscale is correct")
10.     (command "dimscale" ss)
11.   )
12. )
« Last Edit: June 23, 2022, 04:30:29 PM by jlogan02 »
J. Logan

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

#### kreaten_vhar

• Mosquito
• Posts: 3
##### Re: find length of line and compare it to dimscale
« Reply #1 on: June 23, 2022, 04:27:51 PM »
I had an issue with the selection set, so in the statement to set line_length I changed line_ent to just a (ssget) call (essentially making the lin_ent stuff pointless in my case). Then (command "dimscale" div_factor) at the end did the job.

so:
Code - Auto/Visual Lisp: [Select]
1. (defun C:Foo ( / )
2.   (setq dim (getvar 'dimscale))
3.   (setq line_ent (ssget "X" '((0 . "LINE")(8 . "GetLine")))
4.           line_length (vla-get-length (vlax-ename->vla-object (cdar (entget (ssname (ssget)  0)))))) ;changed line_ent to (ssget) because issues
5.   (princ "\nLength of selected line: ")(princ line_length)
6.   (setq div_factor (atoi "34"))
7.   ;; get returned value of div_factor
8.   (princ "\nDivided length: ")(princ (/ line_length div_factor))
9.   (if (= div_factor dim)
10.     (prompt "\nDimscale is correct")
11.     (command "dimscale" div_factor)
12.   )
13. )

Hope that helps!

#### jlogan02

• Bull Frog
• Posts: 275
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #2 on: June 23, 2022, 06:05:57 PM »
Did you create that line and layer for your test?

Code - Auto/Visual Lisp: [Select]
1.  (setq line_ent (ssget "X" '((0 . "LINE")(8 . "GetLine")))

I get a pick box with your change. I'm trying to avoid user input.
J. Logan

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

#### BIGAL

• Swamp Rat
• Posts: 1037
• 40 + years of using Autocad
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #3 on: June 23, 2022, 08:07:03 PM »
The equal function would be better

(equal val1 val2 fuzz) so fuzz may be like 0.01 note real numbers
A man who never made a mistake never made anything

#### jlogan02

• Bull Frog
• Posts: 275
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #4 on: June 24, 2022, 10:49:45 AM »
See, that's why I have folks look at things. I was looking at (=) and equal as being the same and I really didn't consider Fuzz.

Thanks again BigAl
J. Logan

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

#### jlogan02

• Bull Frog
• Posts: 275
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #5 on: June 24, 2022, 12:21:28 PM »
Let's expand my routine a bit. The object I'm using it on never changes it's properties except for size.

Known properties...
Always a polyline
Is rectangular
Always on the same layer
The bottom horizontal line segment is always divisible by 34
The rectangle always starts at and ends at 0,0
all segments are in the positive X and Y axis.

I only need the length of the bottom polyline segment

I found this piece of code that works for find all four segments.

Code - Auto/Visual Lisp: [Select]
1. Recognition goes to DannyNL off of AutoDesk customization forums for this
2. (defun c:Test (/ T_Entity T_Object T_Start T_End T_SegmentLengths T_Count)
3.    (if
4.       (and
5.          (setq T_Entity (car (entsel "\nSelect polyline: ")))
6.          (= (vla-get-ObjectName (setq T_Object (vlax-ename->vla-object T_Entity))) "AcDbPolyline")
7.       )
8.          (setq T_Start (vlax-curve-getStartParam T_Object))
9.          (setq T_End   (vlax-curve-getEndParam T_Object))
10.          (while (< T_Start T_End)
11.             (setq T_SegmentLengths (append T_SegmentLengths (list (- (vlax-curve-getDistAtParam T_Object (setq T_Start (1+ T_Start))) (vlax-curve-getDistAtParam T_Object (1- T_Start))))))
12.          )
13.          (setq T_Count 0)
14.          (foreach T_Item T_SegmentLengths
15.             (princ (strcat "\nSegment " (itoa (setq T_Count (1+ T_Count))) ": " (rtos T_Item)))
16.          )
17.          (princ (strcat "\n\n ** Total polyline length is " (rtos (vla-get-Length T_Object))))
18.       )
19.       (princ "\n ** Nothing selected or not a polyline.")
20.    )
21.    (princ)
22. )
23. )

Here I tested it on my polyline

Command: TEST
Select polyline:
Segment 1: 22.0000
Segment 2: 34.0000
Segment 3: 22.0000
Segment 4: 34.0000

How do I use the resulting "Segment 4: X" length in place of
Code - Auto/Visual Lisp: [Select]
1.  line_ent (ssget "_X"
in my original code?

« Last Edit: June 24, 2022, 01:13:26 PM by jlogan02 »
J. Logan

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

#### mhupp

• Newt
• Posts: 172
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #6 on: June 24, 2022, 02:51:04 PM »
this will allow you to just select the segment you want to check. and will prompt you if the  dimscale is changing or not.

Code - Auto/Visual Lisp: [Select]
1. ;;----------------------------------------------------------------------------;;
2. ;; sets dimscale
3. (defun C:Test2 (/ dim x len scale)
4.   (setq dim (getvar 'dimscale))
5.   (if (setq x (car (nentsel "\nSelect Polyline Segment: ")))
6.       (setq len (vla-get-length (vlax-ename->vla-object x)))
7.       (prompt (strcat "\nSegment lenght: " (rtos len 2 4)))
8.       (setq scale (/ len 34))
9.       (if (equal dim scale 0.0001)
10.         (prompt (strcat "\nDimscale is Correct: " (rtos dim 2 4)))
11.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
12.           (setvar 'dimscale scale)
13.         )
14.       )
15.     )
16.     (prompt "\nNothing Selected try again")
17.   )
18.   (princ)
19. )

#### jlogan02

• Bull Frog
• Posts: 275
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #7 on: June 24, 2022, 04:13:24 PM »
Not quite, that's getting the total length of the polyline, not just the bottom pline segment.
J. Logan

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

#### kdub

• Mesozoic keyThumper
• SuperMod
• Swamp Rat
• Posts: 1463
• class keyThumper<T>:ILazy<T>
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #8 on: June 24, 2022, 07:37:26 PM »
Perhaps something like :

Code - Auto/Visual Lisp: [Select]
1. (defun c:Test ( / len fuzzFactor)
2.   (if (setq len (SelectPlineSegmentLength))
3.       (setq fuzzFactor 0.001)
4.       (if (equal (rem len 34) 0 fuzzFactor)
5.         (alert (strcat " Length " (rtos len) " is divisable by 34"))
6.         ;; else
7.         (alert (strcat " Length " (rtos len) " IS NOT divisable by 34"))
8.       )
9.     )
10.     (alert "Pline length IS NOT available")
11.   )
12. )
13.

Code - Auto/Visual Lisp: [Select]
1. (defun SelectPlineSegmentLength (/ ent plineSegment len)
2.   (if (and (setq ent (entsel "\nSelect Polyline Segment : "))
3.            (setq plineSegment (vlax-ename->vla-object (car ent)))
4.            (= (vla-get-ObjectName plineSegment ) "AcDbPolyline")
5.       )
6.       (setq len (vla-get-Length plineSegment))
7.     )
8.   )
9.   ;; return length of plineSegment or NIL
10.   len
11. )
12.

Regards,
« Last Edit: June 25, 2022, 01:46:33 AM by kdub »
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

#### mhupp

• Newt
• Posts: 172
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #9 on: June 25, 2022, 01:19:20 AM »
Not quite, that's getting the total length of the polyline, not just the bottom pline segment.

nentsel (AutoLISP)
A lightweight polyline (lwpolyline entity) is defined in the drawing database as a single entity; it does not contain subentities.

oops thought that would get the segment like a subentity of line.

--edit
You said
Quote
Known properties...
Always a polyline
Is rectangular
Always on the same layer
The bottom horizontal line segment is always divisible by 34
The rectangle always starts at and ends at 0,0
all segments are in the positive X and Y axis.

I only need the length of the bottom polyline segment

Dose that mean the rectangle is always horizontal? then we could use bounding box to find length and width.
Code - Auto/Visual Lisp: [Select]
1. ;;----------------------------------------------------------------------------;;
2. ;; sets dimscale
3. (defun C:Test2 (/ dim x len scale)
4.   (setq dim (getvar 'dimscale))
5.   (if (setq x (vlax-ename->vla-object (car (entsel "\nSelect Polyline: "))))
6.       (vla-getboundingbox x 'minpt 'maxpt)
7.       (setq LL (vlax-safearray->list minpt)
8.             UR (vlax-safearray->list maxpt)
9.             L&W (mapcar '- UR LL)
10.       )
11.       (prompt (strcat "\nSegment lenght: " (rtos (car L&W) 2 4)))
12.       (setq scale (/ (car L&W) 34))
13.       (if (equal dim scale 0.0001)
14.         (prompt (strcat "\nDimscale is Correct: " (rtos dim 2 4)))
15.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
16.           (setvar 'dimscale scale)
17.         )
18.       )
19.     )
20.     (prompt "\nNothing Selected try again")
21.   )
22.   (princ)
23. )

« Last Edit: June 25, 2022, 02:06:21 AM by mhupp »

#### BIGAL

• Swamp Rat
• Posts: 1037
• 40 + years of using Autocad
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #10 on: June 25, 2022, 08:36:35 PM »
You can select a pline and get the segment I thinks its V1 - V2, now where is it. I know I have often used it.

Code: [Select]
`; Pline segment with angle and length(defun c:plseg()(setq plent (entsel "\nSelect Pline  "))(setvar "osmode" 0)(setq      pick (cadr plent)      plObj (vlax-ename->vla-object (car plent))      pick2 (vlax-curve-getclosestpointto plobj pick)      param (vlax-curve-getparamatpoint plObj pick2)      segment (fix param)   co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent)))))(setq pt1 (nth segment co-ord))(setq pt2 (nth (+ segment 1) co-ord))(if (= pt2 nil)(setq pt2 (nth 0 co-ord)))(setq len (distance pt1 pt2))(setq ang (angle pt1 pt2))(alert (strcat "angle is "  (rtos (/ (* ang 180.0) pi) 2 2) " Length is " (rtos len 2 3))))`
A man who never made a mistake never made anything

#### jlogan02

• Bull Frog
• Posts: 275
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #11 on: June 29, 2022, 12:59:22 PM »
Sorry guys, I've been off working on other things. I'll take a closer look at this on Friday. Thanks for all the responses.
J. Logan

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

#### jlogan02

• Bull Frog
• Posts: 275
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #12 on: June 29, 2022, 07:42:30 PM »
Thanks guys for all for your help.

mhupp has the result I was looking for. I may make some additions but this is a really good start.

Well, there were other results were good too, but this one feels right.

Code - Auto/Visual Lisp: [Select]
1. ;;----------------------------------------------------------------------------;;
2. ;; sets dimscale
3. (defun C:Test2 (/ dim x len scale)
4.   (setq dim (getvar 'dimscale))
5.   (if (setq x (vlax-ename->vla-object (car (entsel "\nSelect Polyline: "))))
6.       (vla-getboundingbox x 'minpt 'maxpt)
7.       (setq LL (vlax-safearray->list minpt)
8.             UR (vlax-safearray->list maxpt)
9.             L&W (mapcar '- UR LL)
10.       )
11.       (prompt (strcat "\nSegment lenght: " (rtos (car L&W) 2 4)))
12.       (setq scale (/ (car L&W) 34))
13.       (if (equal dim scale 0.0001)
14.         (prompt (strcat "\nDimscale is Correct: " (rtos dim 2 4)))
15.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
16.           (setvar 'dimscale scale)
17.         )
18.       )
19.     )
20.     (prompt "\nNothing Selected try again")
21.   )
22.   (princ)
23. )

J.

J. Logan

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

#### jlogan02

• Bull Frog
• Posts: 275
##### Re: Edited: Check my work for: Find length of line and compare it to dimscale
« Reply #13 on: July 06, 2022, 04:58:33 PM »
EDITED: Started new thread.
titled: VLA SSGET

I'm handling something wrong here. I'm trying to avoid user input by selecting the lower left had corner of the border at 0,0 with a crossing window.

It acts like it runs but I get no printed result and the dimscale isn't updated if it has been.
I'm clearly doing something wrong with my
Code - Auto/Visual Lisp: [Select]
statement. Am I'm even approaching it right?

I've tried other variations and did get a selection set established but with it I got "bad point argument" errors.

Code - Auto/Visual Lisp: [Select]
1. (defun c:TB_Dim (/ dim x len scale)
2.   (setq dim (getvar 'dimscale))
3.     (while (setq y (ssget "c" '(1 1) '(-1 -1) '((0 . "polyline"))))
4.      (if (setq x (vlax-ename->vla-object y)) ;;(car (entsel "\nSelect Polyline: "))))
5.         (vla-getboundingbox x 'minpt 'maxpt)
6.         (setq LL (vlax-safearray->list minpt)
7.             UR (vlax-safearray->list maxpt)
8.             L&W (mapcar '- UR LL)
9.         )
10.       (prompt (strcat "\nSegment length: " (rtos (car L&W) 2 4)))
11.       (setq scale (/ (car L&W) 34))
12.       (if (equal dim scale 0.0001)
13.         (prompt (strcat "\nDimscale is Correct: " (rtos dim 2 4)))
14.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
15.           (setvar 'dimscale scale)
16.         )
17.       )
18.     )
19.     (prompt "\nNothing Selected try again")
20.     )
21.    )
22.   (princ)
23. )
« Last Edit: July 07, 2022, 11:42:17 AM by jlogan02 »
J. Logan