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

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 275
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
ACAD 2018

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! :-D

jlogan02

  • Bull Frog
  • Posts: 275
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
ACAD 2018

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
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
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
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 275
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.       (progn
  9.          (setq T_Start (vlax-curve-getStartParam T_Object))
  10.          (setq T_End   (vlax-curve-getEndParam T_Object))
  11.          (while (< T_Start T_End)
  12.             (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))))))
  13.          )
  14.          (setq T_Count 0)
  15.          (foreach T_Item T_SegmentLengths
  16.             (princ (strcat "\nSegment " (itoa (setq T_Count (1+ T_Count))) ": " (rtos T_Item)))
  17.          )        
  18.          (princ (strcat "\n\n ** Total polyline length is " (rtos (vla-get-Length T_Object))))
  19.       )
  20.       (princ "\n ** Nothing selected or not a polyline.")
  21.    )
  22.    (princ)
  23. )
  24. )


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
ACAD 2018

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

mhupp

  • Newt
  • Posts: 172
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.     (progn
  7.       (setq len (vla-get-length (vlax-ename->vla-object x)))
  8.       (prompt (strcat "\nSegment lenght: " (rtos len 2 4)))
  9.       (setq scale (/ len 34))
  10.       (if (equal dim scale 0.0001)
  11.         (prompt (strcat "\nDimscale is Correct: " (rtos dim 2 4)))
  12.         (progn
  13.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
  14.           (setvar 'dimscale scale)
  15.         )
  16.       )
  17.     )
  18.     (prompt "\nNothing Selected try again")
  19.   )    
  20.   (princ)
  21. )

jlogan02

  • Bull Frog
  • Posts: 275
Not quite, that's getting the total length of the polyline, not just the bottom pline segment.
J. Logan
ACAD 2018

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>
Perhaps something like :

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

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.     (progn
  7.       (setq len (vla-get-Length plineSegment))
  8.     )
  9.   )
  10.   ;; return length of plineSegment or NIL
  11.   len
  12. )
  13.  

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
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.     (progn
  7.       (vla-getboundingbox x 'minpt 'maxpt)
  8.       (setq LL (vlax-safearray->list minpt)
  9.             UR (vlax-safearray->list maxpt)
  10.             L&W (mapcar '- UR LL)
  11.       )
  12.       (prompt (strcat "\nSegment lenght: " (rtos (car L&W) 2 4)))
  13.       (setq scale (/ (car L&W) 34))
  14.       (if (equal dim scale 0.0001)
  15.         (prompt (strcat "\nDimscale is Correct: " (rtos dim 2 4)))
  16.         (progn
  17.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
  18.           (setvar 'dimscale scale)
  19.         )
  20.       )
  21.     )
  22.     (prompt "\nNothing Selected try again")
  23.   )
  24.   (princ)
  25. )

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

BIGAL

  • Swamp Rat
  • Posts: 1037
  • 40 + years of using Autocad
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
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
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 275
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.     (progn
  7.       (vla-getboundingbox x 'minpt 'maxpt)
  8.       (setq LL (vlax-safearray->list minpt)
  9.             UR (vlax-safearray->list maxpt)
  10.             L&W (mapcar '- UR LL)
  11.       )
  12.       (prompt (strcat "\nSegment lenght: " (rtos (car L&W) 2 4)))
  13.       (setq scale (/ (car L&W) 34))
  14.       (if (equal dim scale 0.0001)
  15.         (prompt (strcat "\nDimscale is Correct: " (rtos dim 2 4)))
  16.         (progn
  17.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
  18.           (setvar 'dimscale scale)
  19.         )
  20.       )
  21.     )
  22.     (prompt "\nNothing Selected try again")
  23.   )
  24.   (princ)
  25. )

J.

J. Logan
ACAD 2018

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

jlogan02

  • Bull Frog
  • Posts: 275
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.       (progn
  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 length: " (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.         (progn
  16.           (prompt (strcat "\nDimscale Changing from [" (rtos dim 2 4) "] to " (rtos scale 2 4)))
  17.           (setvar 'dimscale scale)
  18.         )
  19.       )
  20.     )
  21.     (prompt "\nNothing Selected try again")
  22.     )
  23.    )
  24.   (princ)
  25. )
« Last Edit: July 07, 2022, 11:42:17 AM by jlogan02 »
J. Logan
ACAD 2018

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