Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
New Member
nelsnelson
Posts: 1
Registered: ‎09-14-2013
Message 1 of 2 (1,126 Views)

Insert blocks at XYZ coordinates and x-, y- and z-scales from ASCII

1126 Views, 1 Replies
09-14-2013 06:42 PM

I found this LISP code that can insert a block at coordinates from an ASCII.

 

(defun c:universal( / file f a osmode);PointsToBlocks
(while (not file)
(setq file (getfiled "Input file" "coords" "txt" 4))
)
(setq f (open file "r")
osmode (getvar "OSMODE")
)
(setvar "OSMODE" 0)
(while (setq a (read-line f))
(command "._insert" "block1" a 1 1 0)
)
(setvar "OSMODE" osmode)
(princ)

What I want to be able to do is insert blocks from an ASCII and also set the x-, y-, and z-scales.

 

For example, I have these lines in ASCII, the data points refer to X position, Y position, Z position, X scale, Y scale, and Z scale.

 

30.593589,21.274607,26.235255,0.172826,0.766324,0.066442
30.710182,21.379792,27.054248,0.130816,0.700035,0.043114

 

Thank you in advance for your help, I am a beginner.

*Expert Elite*
Lee_Mac
Posts: 1,168
Registered: ‎12-29-2009
Message 2 of 2 (1,090 Views)

Re: Insert blocks at XYZ coordinates and x-, y- and z-scales from ASCII

09-15-2013 02:31 AM in reply to: nelsnelson

Here is a relatively simple example:

 

;; Insert Block  -  Lee Mac
;; Inserts a selected block at all points in a given comma-delimited text file
;; with format x,y,z,sx,sy,sz

(defun c:insblk ( / *error* blk des itm lst spc txt )

    (defun *error* ( msg )
        (if (= 'file (type des))
            (close des)
        )
        (LM:endundo (LM:acdoc))
        (if (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
    
    (cond
        (   (null (setq txt (getfiled "" "" "txt" 16))))
        (   (null (setq blk (getfiled "Select Block to Insert" "" "dwg" 16))))
        (   (null (setq des (open txt "r")))
            (princ "\nUnable to open the selected file for reading.")
        )
        (   (progn
                (while (setq itm (read-line des))
                    (if
                        (and
                            (apply 'and (setq itm (mapcar 'distof (LM:str->lst itm ","))))
                            (= 6 (length itm))
                        )
                        (setq lst
                            (cons
                                (append
                                    (list (mapcar '(lambda ( a b ) a) itm '(x y z)))
                                    (cons blk (cdddr itm))
                                   '(0.0)
                                )
                                lst
                            )
                        )
                    )
                )
                (setq des (close des))
                (null lst)
            )
            (princ "\nNo valid data found in selected file.")
        )
        (   (setq spc (vlax-get (LM:acdoc) (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)))
            (LM:startundo (LM:acdoc))
            (foreach itm lst (apply 'vlax-invoke (vl-list* spc 'insertblock itm)))
            (LM:endundo (LM:acdoc))
        )
    )
    (princ)
)

;; String to List  -  Lee Mac
;; Separates a string using a given delimiter
;; str - [str] string to process
;; del - [str] delimiter by which to separate the string

(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)

;; Start Undo  -  Lee Mac
;; Opens an Undo Group.

(defun LM:startundo ( doc )
    (LM:endundo doc)
    (vla-startundomark doc)
)

;; End Undo  -  Lee Mac
;; Closes an Undo Group.

(defun LM:endundo ( doc )
    (while (= 8 (logand 8 (getvar 'undoctl)))
        (vla-endundomark doc)
    )
)

;; Active Document  -  Lee Mac
;; Returns the VLA Active Document Object

(defun LM:acdoc nil
    (eval (list 'defun 'LM:acdoc 'nil (vla-get-activedocument (vlax-get-acad-object))))
    (LM:acdoc)
)

(vl-load-com)
(princ)

 

The above could be optimised for better performance if the block is non-attributed.

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.