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

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

1 REPLY 1
Reply
Message 1 of 2
nelsnelson
3437 Views, 1 Reply

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

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.

1 REPLY 1
Message 2 of 2
Lee_Mac
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.

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

Post to forums  

Autodesk Design & Make Report

”Boost