Author Topic: I would like to create a custom distance  (Read 5610 times)

0 Members and 2 Guests are viewing this topic.

CECE_CAD

  • Guest
I would like to create a custom distance
« on: October 25, 2007, 10:01:50 AM »
I would like to create a custom distance command.  Would it be easier to begin with the regular distance command or to use "getdist"..?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: I would like to create a custom distance
« Reply #1 on: October 25, 2007, 10:08:17 AM »
It would be easier to begin with getpoint.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CECE_CAD

  • Guest
Re: I would like to create a custom distance
« Reply #2 on: October 25, 2007, 10:13:42 AM »
ok, I will try that..

I have started reading George O. Head "AutoLISP" in plain english.  So I'm really just learning how to do this.

Thanks

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: I would like to create a custom distance
« Reply #3 on: October 25, 2007, 11:13:09 AM »
Here is what a search pulled up.
Get Distance

Get Length

Look at some of the shorter routines.

BTW, we love questions. :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CECE_CAD

  • Guest
Re: I would like to create a custom distance
« Reply #4 on: October 25, 2007, 11:35:58 AM »
Cab, those are more advanced then me... :laugh:  I will have to use the book I'm reading to follow them.. I will do my best, all I'm really trying to do is make a distance command that allows more then two points..  I saw a lisp with the links you provided me that I'm sure will work I just have to understand how it works..

Thanks


Here is what a search pulled up.
Get Distance

Get Length

Look at some of the shorter routines.

BTW, we love questions. :-)
« Last Edit: October 25, 2007, 11:40:20 AM by CECE_CAD »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: I would like to create a custom distance
« Reply #5 on: October 25, 2007, 12:07:57 PM »
Here is a simple distance adding routine using picked points...each line is commented to show what is going on:

Code: [Select]
(defun c:test (/ pt1 pt2 dist cntr);;localize variables pt1 pt2 dist cntr
  (setq cntr 0);;set distance counter to 0
  (while (if pt2;;if pt2 exists set pt1 = pt2 for start point
   (setq pt1 pt2);;pt2 exists so set pt1 = pt2
   (setq pt1 (getpoint "\nSelect point: "));;pt2 does not exist yet so select point
)
    (progn
      (setq pt2 (getpoint pt1 "\nSelect next point: ");;next point to select
    dist (distance pt1 pt2);;get the distance between the points
    cntr (+ dist cntr);;add the distance to the counter
      )
      (grdraw pt1 pt2 1);;visually show line between picked points
      (princ (rtos cntr));;print the results to command line using "rtos" to convert the counter to a string
    )
  )
)


Hope this helps you understand.

Ron
« Last Edit: October 25, 2007, 12:19:38 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Josh Nieman

  • Guest
Re: I would like to create a custom distance
« Reply #6 on: October 25, 2007, 12:15:25 PM »
Here is a simple distance adding routine using picked points...each line is commented to show what is going on:

Code: [Select]
(defun c:test (/ pt1 pt2 dist cntr);;localize variables pt1 pt2 dist cntr
  (setq cntr 0);;set distance counter to 0
  (while (if pt2;;if pt2 exists set pt1 = pt2 for start point
   (setq pt1 pt2);;pt2 exists
   (setq pt1 (getpoint "\nSelect point: "));;pt2 does not exist yet so select point
)
    (progn
      (setq pt2 (getpoint pt1 "\nSelect next point: ");;next point to select
    dist (distance pt1 pt2);;get the distance between the points
    cntr (+ dist cntr);;add the distance to the counter
      )
      (grdraw pt1 pt2 1);;visually show line between picked points
      (princ (rtos cntr));;print the results to command line using "rtos" to convert the counter to a string
    )
  )
)


Hope this helps you understand.

Ron


Excellent post!  Thanks much for the break down.  I'm learnededing.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: I would like to create a custom distance
« Reply #7 on: October 25, 2007, 12:21:27 PM »
Good to hear  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: I would like to create a custom distance
« Reply #8 on: October 25, 2007, 12:31:12 PM »
Hi,

here's the one I use.

You can specify 2 points or select an object, if the selected object is a polyline boyh total length and selacted segment length are returned.

Code: [Select]
(defun c:dist (/ p1 p2 ent obj tot par)
  (vl-load-com)
  (if (setq p1
     (getpoint
       "\nSpecify the first point or <Select an object>: "
     )
      )
    (progn
      (while
(not (setq p2 (getpoint p1 "Specify the second point: "))
)
      )
      (princ (strcat
       "\nDistance : "
       (rtos (distance p1 p2))
     )
      )
    )
    (if (and (setq ent (entsel))
     (not (vl-catch-all-error-p
    (setq p2
   (vl-catch-all-apply
     'vlax-curve-getEndParam
     (list (setq obj (vlax-ename->vla-object (car ent))))
   )
    )
  )
     )
)
      (progn
(setq tot (vlax-curve-getDistAtParam obj p2))
(if (wcmatch (cdr (assoc 0 (entget (car ent)))) "*POLYLINE")
  (progn
    (setq p1  (vlax-curve-getClosestPointToProjection
obj
(trans (cadr ent) 1 0)
(mapcar '-
(trans (getvar "viewdir") 1 0)
(trans '(0 0 0) 1 0)
)
      )
  par (vlax-curve-getParamAtPoint obj p1)
    )
    (princ
      (strcat
"\nTotal distance: "
(rtos tot)
" Selected segment: "
(rtos
  (- (vlax-curve-getDistAtParam obj (1+ (fix par)))
     (vlax-curve-getDistAtParam obj (fix par))
  )
)
      )
    )
  )
  (princ (strcat "\nDistance : " (rtos tot)))
)
      )
      (princ "\nNon valid entity.")
    )
  )
  (princ)
)
Speaking English as a French Frog

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: I would like to create a custom distance
« Reply #9 on: October 25, 2007, 01:46:12 PM »
my variation of ronjonp's routine
Code: [Select]
(defun test (/ p1 p2 Dist)
  (setq Dist 0.0)
  (while (and (or (setq p1 p2)
  (setq p1 (getpoint "\nFirst point: "))
  (setq p1 (getvar "LASTPOINT"))
      )
      (setq p2 (getpoint p1 "\nNext point: "))
)
    (setq Dist (+ Dist (distance p1 p2)))
  )
  (prompt (strcat "\nTotal distance: " (rtos Dist)))
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: I would like to create a custom distance
« Reply #10 on: October 25, 2007, 02:57:10 PM »
And my variation.
Code: [Select]
(defun c:test (/ p1 p2 Dist vecs)
  (setq Dist 0.0)
  (while (and (or p1
                  (setq p1 (getpoint "\nFirst point: "))
  (setq p1 (getvar "LASTPOINT"))
      )
      (setq p2 (getpoint p1 "\nNext point: "))
)
    (setq Dist (+ Dist (distance p1 p2))
          vecs (append vecs (list p1 p2))
          p1 p2)
      (grvecs (append vecs '(1)))
      (print (strcat "  Running total is " (rtos Dist)))
  )
  (prompt (strcat "\nTotal distance: " (rtos Dist)))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: I would like to create a custom distance
« Reply #11 on: October 25, 2007, 03:02:29 PM »
Nice CAB :) I like the grvecs portion so the line is redrawn if you pan.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: I would like to create a custom distance
« Reply #12 on: October 25, 2007, 03:51:32 PM »
a "dirty" variation, but with all "pline's" cool stuff :)
Code: [Select]
(vl-load-com)
(defun c:test (/ Obj)
  (setq Obj (entlast))
  (command "._PLINE")
  (while (/= (getvar "CMDACTIVE") 0) (command PAUSE))
  (if (not (equal Obj (setq Obj (entlast))))
    (progn (prompt
     (strcat "\nTotal distance: "
     (rtos (vla-get-Length (setq Obj (vlax-ename->vla-object Obj))))
     )
   )
   (vla-Delete Obj)
   (vlax-release-object Obj)
    )
  )
)

CECE_CAD

  • Guest
Re: I would like to create a custom distance
« Reply #13 on: November 12, 2007, 10:19:40 AM »
CAB, This works great.. Could you help me understand it a little more?

(/ p1 p2 Dist vecs)  = your setting p1 p2 as your local variables, is this correct? "dist" is for distance? what is "vecs"

(setq Dist 0.0) = starting with a zero point?

(while (and (or p1 = are you telling the program to look for the first point I pick?

(setq p1 (getpoint "\nFirst point: ")) = then telling it to use p1 as starting messuring point?

(setq p1 (getvar "LASTPOINT")) = Does this set the first point picked to be your starting point?

(setq Dist (+ Dist (distance p1 p2)) = your adding the first two points together

vecs (append vecs (list p1 p2)) = not to sure, but does this allow you to keep picking other points for the distance?
          p1 p2)

     (grvecs (append vecs '(1))) = again not sure..

      (print (strcat "  Running total is " (rtos Dist))) = showing my distance as I'm picking points?  What is "rtos"?
  )
  (prompt (strcat "\nTotal distance: " (rtos Dist))) = then total of all points after I select enter?


Not sure how I did, but can you let me know? 

Thanks..

And my variation.
Code: [Select]
(defun c:test (/ p1 p2 Dist vecs)
  (setq Dist 0.0)
  (while (and (or p1
                  (setq p1 (getpoint "\nFirst point: "))
  (setq p1 (getvar "LASTPOINT"))
      )
      (setq p2 (getpoint p1 "\nNext point: "))
)
    (setq Dist (+ Dist (distance p1 p2))
          vecs (append vecs (list p1 p2))
          p1 p2)
      (grvecs (append vecs '(1)))
      (print (strcat "  Running total is " (rtos Dist)))
  )
  (prompt (strcat "\nTotal distance: " (rtos Dist)))
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: I would like to create a custom distance
« Reply #14 on: November 12, 2007, 11:06:22 AM »
You did good.
Here is a commented version & I fixed two mistakes that I did in my haste.

Code: [Select]
(defun c:test (/ ; Local Variables
               p1    ; First point
               p2    ; Next point:
               Dist  ; Distance acumlated
               vecs  ; vectors to draw
               )
  (setq Dist 0.0)
  (while  ; loop until p2 is nil, i.e. ENTER pressed
    (and  ; make sure p1 & p2 are not nil
      (or p1 ; get p1 only on the first time
                  (setq p1 (getpoint "\nFirst point: "))
  (setq p1 (getvar "LASTPOINT"))
      )
      (setq p2 (getpoint p1 "\nNext point: "))
    )
    (setq Dist (+ Dist (distance p1 p2))  ; accululate the distance
          vecs (append vecs (list p1 p2)) ; get more vectors
          p1 p2)                          ; shift the point p1 to next point p2
      (grvecs (append '(1) vecs))         ; add color & draw vectors
      (prompt (strcat "  Running total is " (rtos Dist)))
  )
  (prompt (strcat "\nTotal distance: " (rtos Dist)))
  (princ) ; don't leave anything extra on the command line
)
 
 
Let me help you figure things out on your own.
At the command line type VLIDE [enter]
Expand the FILE menu, select New File
Paste this routine in the <Untitled-1> window.
With your cursor highlight the "rtos" function, then press Ctrl-Shift-A
The Apropos window appears. In that window Highlight RTOS and click on the Question Mark button
The Help window will appear.

HTH
« Last Edit: November 12, 2007, 11:08:14 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: I would like to create a custom distance
« Reply #15 on: November 12, 2007, 11:12:03 AM »
PS.  You get extra credit if you can figure out the two mistakes I made in my first post of the routine.
 :evil:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CECE_CAD

  • Guest
Re: I would like to create a custom distance
« Reply #16 on: November 12, 2007, 11:59:21 AM »
Ok, I will try that.  Thanks

CECE_CAD

  • Guest
Re: I would like to create a custom distance
« Reply #17 on: November 13, 2007, 04:25:25 PM »
ok lets see..
Thanks by the way.  I like how that works, the vlide

Code: [Select]
(grvecs (append vecs '(1)))  you changed the way the program returns the value..?  to this (grvecs (append '(1) vecs))

Then added the way the program closes with (princ).  Did you change (print) to (prompt) for consistency?


PS.  You get extra credit if you can figure out the two mistakes I made in my first post of the routine.
 :evil:
« Last Edit: November 13, 2007, 04:28:25 PM by CECE_CAD »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: I would like to create a custom distance
« Reply #18 on: November 13, 2007, 05:23:46 PM »
Good job. 8-)

The princ keeps nil or any other value from showing up at the command line when the routine finishes.

Yes the prompt was a change too as the number was not displaying properly.

As for grvecs, I had the color '(1) in the wrong place. It was displaying white when I wanted red.

I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.