;;;--- Draw Chain.lsp - Draws a 2D chain like the line command.
;;;
;;;
;;;--- Copyright 2005 by JefferyPSanders.com
;;; All rights reserved.
;;;
;;;
;;;
;;;--- Requires two blocks: CHAIN1.dwg and CHAIN2.dwg
;;; Blocks can be found here: https://onedrive.live.com/redir?resid=2FFF32951ECEE9AC%2121452

(defun C:CHC()

;;;--- Turn the command echo off
(setvar "cmdecho" 0)

;;;--- Get the scale factor....
(setq sc(getreal "\n Scale Factor? : "))

;;;--- Set the default if the user presses the enter key
(if(= sc nil)(setq sc 1.0))

;;;--- Get the start point
(if(setq pt(getpoint "\n Start Point: "))
(progn

;;;--- Count each line as it is created...
(setq lineCntr 0)

;;;--- While the user selects another point...
(while (setq pt2(getpoint pt "\n Next Point: "))

;;;--- Check to see if this not the first line
(if(> linecntr 0)
(progn

;;;--- If it is not the first line, calculate where the joining link should be
(setq lastCh2Pt(polar lastCh1Pt oldAng linkDis))

;;;--- Calculate where the new starting point should be
(setq newPt(polar lastCh2Pt oldAng (* sc 0.5625)))

;;;--- Reset the start point so the two links line up
(setq pt(polar newPt (angle newPt pt2) (* sc 0.25)))
)
)

;;;--- Get the angle of the chain
(setq ang(angle pt pt2))

;;;--- Get the length of the chain
(setq dis(distance pt pt2))

;;;--- See how long each link will be after multiplying by the scale factor
(setq linkDis(* sc 0.8125))

;;;--- See how many open links will fit on the line
(setq numLinks(+ 1 (fix(/ (/ dis linkDis) 2.0))))

;;;--- Reset the end of the chain to end on an open link
(setq pt2(polar pt ang (* (* numLinks 2.0) linkDis)))

;;;--- Get a temporary start point to place the links and start a counter
(setq stpt pt cnt 0)

;;;--- Repeat for every open link
(repeat numLinks

;;;--- Save the last open link insertion point
(setq lastCh1Pt stpt)

;;;--- Insert the open link
(command "insert" "CHAIN1" stpt sc sc (angtos ang))

;;;--- If this is not the first line and it is the first link then...
(if(and (= cnt 0)(> linecntr 0))

;;;--- Insert the joining closed link
(command "insert" "CHAIN2" lastCh2Pt sc sc (angtos oldAng))
)

;;;--- Increment the counter to keep from inserting more than one joining link
(setq cnt(+ cnt 1))

;;;--- Reset the start point each loop to the insertion point of the next link
(setq stpt(polar stpt ang (* linkDis 2.0)))
)

;;;--- Reset the start point to be the insertion point of the first closed link
(setq stpt2 (polar pt ang linkDis))

;;;--- Insert all of the closed links
(repeat (- numLinks 1)

;;;--- Save the last insertion point
(setq lastCh2Pt stpt2)

;;;--- Insert the link
(command "insert" "CHAIN2" stpt2 sc sc (angtos ang))

;;;--- Move to the next insertion point
(setq stpt2(polar stpt2 ang (* linkDis 2.0)))
)

;;;--- Count the line
(setq lineCntr(+ lineCntr 1))

;;;--- Save the angle of the last line
(setq oldAng ang)

;;;--- Reset the start point of the line to the end of the line for the next loop
(setq pt pt2)
)
)
)

;;;--- Turn the command echo back on
(setvar "cmdecho" 1)

;;;--- Suppress the last echo for a clean exit
(princ)
)
(c:chc)

Advertisements