AutoLISP: Distance Along a Path

As you can see, these LISP routines are very helpful.

With this one you can pick an object (except LWPOLYLINES) and specify a starting point and then tell it how far along that object you would like to place a node. After the initial node is placed, it measures from the previous node.

In the example animated picture:

  • TRACEPOLYLINE <enter> to start
  • Select object   (Note that it works with polylines and splines as seen in the picture)
  • specify starting point
  • Specify distance of 10 feet <enter>
  • Specify distance of 5 feet <enter>

etc…

(The picture is so large that you may need to click it so that it opens up in a separate window in order for it to load.)

Code criginally posted @ http://www.cadtutor.net by ASMI










(defun c:tracepolyline(/ *error* cCurve curPt dPar dPt

enPt fPar fPt maxLen obType oldDis

oldOsn posDir rClose sFlag stPt

sumDis swMod undoLst vClose whatDo)

(vl-load-com)

(defun asmi_GetActiveSpace(/ actDoc)

(if

(= 1(vla-get-ActiveSpace

(setq actDoc

(vla-get-ActiveDocument

(vlax-get-acad-object)

); end vla-get ActiveSpace

); end setq

); end vla-get-ActiveDocument

); end =

(vla-get-ModelSpace actDoc)

(vla-get-PaperSpace actDoc)

); end if

); end of asmi_GetActiveSpace

(defun RestorePointStyle()

(if

(and xdiv:oldPm xdiv:oldPs)

(progn

(initget "Yes No")

(setq swMod

(getkword

"\nRestore point style? [Yes/No] <No>: "))

(if(null swMod)(setq swMod "No"))

(if

(= swMod "Yes")

(progn

(princ "\nPlease wait... \n")

(setvar "PDMODE" xdiv:oldPm)

(setvar "PDSIZE" xdiv:oldPs)

); end progn

); end if

); end progn

); end if

(princ)

); end of RestorePointStyle

(defun AddPointOrInsert(Mode Block Scale)

(vla-AddPoint

(asmi_GetActiveSpace)

(vlax-3d-point curPt))

); end of AddPointOrInsert

(defun AddPointOrInsert(Mode Block Scale / outObj)

(setq undoLst

(append

(list

(list

(setq outObj

(vla-AddPoint

(asmi_GetActiveSpace)

(vlax-3d-point curPt))); end setq

curPt xdiv:curDis

); end list

); end list

undoLst); end append

); end setq

outObj

); end of AddPointOrInsert

(defun *error*(msg)

(if cCurve

(progn

(vla-Highlight cCurve :vlax-false)

(setvar "OSMODE" oldOsn)

); end progn

); end if

(princ "\n*Cancel*")

(princ)

); end of *error*

(if

(member

(setq xdiv:oldPm(getvar "PDMODE"))

'(0 1)

); end member

(progn

(setq xdiv:oldPs(getvar "PDSIZE"))

(initget "Yes No")

(setq swMod

(getkword

"\nChange points style to good visible? [Yes/No] <Yes>: "))

(if(null swMod)(setq swMod "Yes"))

(if

(= swMod "Yes")

(progn

(princ "\nPlease wait... \n")

(setvar "PDMODE" 35)

(setvar "PDSIZE" -2)

); end progn

); end if

); end progn

); end if

(setq oldOsn

(getvar "OSMODE")); end setq

(if

(not xdiv:curDis)

(setq xdiv:curDis 1.0

xdiv:oldDis 1.0); end setq

); end if

(if

(setq cCurve

(entsel

"\nSelect curve > ")); end setq

(progn

(setq cCurve

(vlax-ename->vla-object

(car cCurve))); end setq

(if

(member

(setq obType

(vla-get-ObjectName cCurve))

'("AcDbLine" "AcDbPolyline" "AcDb3dPolyline"

"AcDbSpline" "AcDbArc" "AcDbCircle" "AcDbEllipse")

); end member

(progn

(vla-Highlight cCurve :vlax-true)

(setvar "OSMODE" 3071)

(setq stPt

(vlax-curve-GetStartPoint cCurve)

enPt

(vlax-curve-GetEndPoint cCurve)

fPt

(getpoint

"\nPick start markup point at curve > ")

); end setq

(if fPt

(setq fPt(trans fPt 1 0)

curPt(vla-AddPoint

(asmi_GetActiveSpace)

(vlax-3d-point fpt)); end vla-AddPoint

undoLst

(list

(list curPt 0.0 0.0)); end list

); end setq

); end if

(if

(and

fPt

(setq fPar

(vlax-curve-GetParamAtPoint cCurve fPt))

); end and

(progn

(if

(and

(not(equal fPt stPt 0.0001))

(not(equal fPt enPt 0.0001))

); end or

(progn

(setq dPt

(getpoint fPt

"\nPick point at curve to specify markup direction > "))

(if dPt

(setq dPt(trans dPt 1 0))); end if

(if

(and

dPt

(setq dPar

(vlax-curve-GetParamAtPoint cCurve dPt))

); end and

(progn

); end progn

(princ "\nEmpty input or point not at curve! ")

); end if

); end progn

); end if

); end progn

(princ "\nEmpty input or point not at curve! ")

); end if

); end progn

(princ "\nInvalid object type! ")

); end if

(setq maxLen

(-

(vlax-curve-GetDistAtPoint cCurve enPt)

(vlax-curve-GetDistAtPoint cCurve stPt)

); end -

rClose

(vlax-curve-IsClosed cCurve)

); end setq

(if(equal fPt stPt 0.0001)

(setq vClose T)

); end if

(if

(or

(equal fPt stPt 0.0001)

(and

dPar

(> dPar fPar)

); end and

); end or

(setq posDir T

sumDis

(vlax-curve-GetDistAtPoint cCurve fPt)

); end setq

(setq sumDis

(- maxLen

(- maxLen

(vlax-curve-GetDistAtPoint cCurve fPt)))

); end setq

); end if

(while(not sFlag)

(setq whatDo

(getstring

(strcat

"\nSpecify distance or [Undo/Quit] <"

(if xdiv:curDis(rtos xdiv:curDis) "not defined")

">: "); end strcat

); end getstring

); end setq

(cond

((or

(= 'REAL(type(distof whatDo)))

(= "" whatDo)

); end or

(if(= "" whatDo)

(setq xdiv:curDis xdiv:oldDis)

(setq xdiv:curDis(distof whatDo))

); end if

(setq xdiv:oldDis xdiv:curDis); end setq

(cond

(posDir

(setq sumDis

(+ sumDis xdiv:curDis)

curPt

(vlax-curve-GetPointAtDist cCurve sumDis)

); end setq

(if curPt

(AddPointOrInsert nil nil nil)

(princ "\n>>> End of line <<< ")

); end if

); end condition # 1

((not posDir)

(setq sumDis

(- sumDis xdiv:curDis)

curPt

(vlax-curve-GetPointAtDist cCurve sumDis)

); end setq

(if curPt

(AddPointOrInsert nil nil nil)

(princ "\n>>> End of line <<< ")

); end if

); end condition # 1

); end cond

); end condition #1

((=(strcase whatDo) "Q")

(if

(= 1(length undoLst))

(vla-Delete(caar undoLst))

); end if

(RestorePointStyle)

(if cCurve

(progn

(vla-Highlight cCurve :vlax-false)

(setvar "OSMODE" oldOsn)

); end progn

); end if

(setq sFlag T)

)

((=(strcase whatDo) "U")

(if

(and

undoLst

(/= 1(length undoLst))

); end and

(progn

(vla-Delete(caar undoLst))

(setq undoLst(cdr undoLst)

curPt(cadar undoLst)

sumDis(- sumDis

(last

(car undoLst)))

); end setq

); end progn

(princ "\n>>> Nothing to undo <<< ")

); end if

)

); end cond

); end while

); end progn

(princ "\nEmpty selection! ")

); end if

(princ)

); end of c:xdiv
Advertisements

About AutoCAD Tips

This blog serves as a knowledge base for myself (and anyone else) so that I can reference tips & tricks that I have learned and also refer others to it as well. I hope that this blog helps you learn at least one tip to make your drafting/design experience better.
This entry was posted in AutoLISP, TIPS. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s