Hi all,
Does anyone know of a Lisp Routine that could replace a line at a specified maximum length with a block at the lines midpoint.
I would love to be able to replace all lines I select which are say less than 0.5m in length with a block at the lines
midpoint.
Very Much Appreciate Anyones Feedback
Hi,
You can try this crude routine (no error checking, just plain autolisp):
(defun ReplaceLineWithBlock (maxlinelength blkName / ss i n dxf p1 p2 line d mp) (setq ss (ssget "X" '((0 . "LINE")))) (if ss (progn (setq i 0) (setq n (sslength ss)) (while (< i n) (setq line (ssname ss i)) (setq dxf (entget line)) (setq p1 (cdr (assoc 10 dxf))) (setq p2 (cdr (assoc 11 dxf))) (setq d (distance p1 p2)) (if (<= d maxlinelength) (progn (entdel line) (setq mp (polar p1 (angle p1 p2) (* d 0.5))) (command "-insert" blkName mp 1 1 0) ) ) (setq i (+ i 1)) ) ) ) )
Gaston Nunez
This..... ? replace the Block name of your needed one as shown in Blue below
(defun c:Test (/ Blk s i sn p1 p2) (setq blk "MyBlock") ;;; Tharwat 22. may. 2012 ;;; (if (tblsearch "BLOCK" blk) (if (setq s (ssget "_:L" '((0 . "LINE")))) (repeat (setq i (sslength s)) (setq sn (ssname s (setq i (1- i)))) (if (< (distance (setq p1 (cdr (assoc 10 (entget sn)))) (setq p2 (cdr (assoc 11 (entget sn)))) ) 0.5 ) (entmakex (list '(0 . "INSERT") (cons 2 Blk) (cons 10 (mapcar '(lambda (a b) (/ (+ a b) 2.)) p1 p2)) '(41 . 1.0) '(42 . 1.0) '(43 . 1.0) ) ) ) ) (princ) ) (princ "\n Block not found in Drawing ") ) (princ) )
Thank you very happy to know this may be indeed possible.
Am I doing something obviously wrong here when trying this routine, bit of newcomer to lisp routines.
Copied your text exactly, pasted into notepad
Saved as ReplaceLineWithBlock.lsp (file type = all files)
Run appload, loaded lsp successfully
In command line typed 'ReplaceLineWithBlock'
No luck, the command is always after the defun text in routine is this right?
Thanx Again
have you tried mine my friend ?
Just change the the name of the block and load the code and invoke it by the its name Test
Hi,
Try yhis:
(defun c:rpl()
(setq myblockname (getstring"\nBlock Name: "))
(setq maxlength (getreal "\nMax distance: "))
(ReplaceLineWithBlock maxlength myblockname) ;<---- You need to load ReplaceLineWithBlock first
)
use: Type RPL at command line
Gaston Nunez
@Anonymous wrote:....
In command line typed 'ReplaceLineWithBlock'
No luck, the command is always after the defun text in routine is this right?
....
To elaborate a little:
When the (defun... line has a C: before the command name, it is defining a command that can be used by typing the command name [without the C:], as in Tharwat's suggestion.
When it has a function name without C: at the beginning, as in gasty1001's suggestion, it is defining a function or sub-routine. The terms before the / in the following parentheses are arguments that must be supplied, along with the function name, and inside parentheses, to use it. Type something like:
(ReplaceLineWithBlock 0.5 "YourBlockName")
Absolutlely brilliant, thanx Gaston, I now love lisp routines. I am now able to complete a task which would
normally take about 45 mintues in about 5 minutes. Of course my employer will be totally unaware of my new found skills and it will enable me to undertake more surfing of the web and contributing more in forums such as this.
Thanx Again