Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is there a Lisp Routine For Replacing a Line with a Block

7 REPLIES 7
Reply
Message 1 of 8
Anonymous
476 Views, 7 Replies

Is there a Lisp Routine For Replacing a Line with a Block

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

 

7 REPLIES 7
Message 2 of 8
hgasty1001
in reply to: Anonymous

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

Message 3 of 8
_Tharwat
in reply to: Anonymous

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)
)

 

Message 4 of 8
Anonymous
in reply to: Anonymous

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

Message 5 of 8
_Tharwat
in reply to: Anonymous

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

Message 6 of 8
hgasty1001
in reply to: Anonymous

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

Message 7 of 8
Kent1Cooper
in reply to: Anonymous


@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")

Kent Cooper, AIA
Message 8 of 8
Anonymous
in reply to: Anonymous

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

Man LOL

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost