Lisp to draw circles and lines as blocks

Lisp to draw circles and lines as blocks

Anonymous
Not applicable
1,724 Views
9 Replies
Message 1 of 10

Lisp to draw circles and lines as blocks

Anonymous
Not applicable

1.is any lisp available to draw blocks(circular) where i can give the x y z of a individual block and its diameter in text file?.

 

Name     X        Y        Z         Diameter

A1        100    -50       0                10

A2        -100    50       0                15

A3        -100    -50      0                13

A3         100     50       0                8

 

A1 is the block name which is given at center of the circular block

(x,y,z) - (100,-50,0) is the loaction where the circular block have to be created

10 is the diameter of the circular block at their corresponding location.

 

2. Draw a line(block) between locations(coordinates) which can be given in notepad(text)file.

 

X            Y          Z         name

 100      -50        0          A1-A2   /* Start point

-100       50        0          A1-A2    /* End  point

 

line drawn between (100,-50,0) to (-100,50,0) 

the name of the line will be "A1-A2"

 

0 Likes
1,725 Views
9 Replies
Replies (9)
Message 2 of 10

Ranjit_Singh
Advisor
Advisor

Hi Vivek, format the lines file to remove the /* Start Point and /* End Point. Try the below routine for example. I also assume that you have one header line in each file. I have not added any error trap, but you should. Let me know how it goes.

(defun c:somefunc  (/ curvar list1 list2 file1 pt1)
  (setq file1  (open (getfiled "Select Circles File" "" "txt" 0) "r")
        curvar (mapcar 'getvar (list 'cmdecho 'nomutt)))
  (mapcar 'setvar (list 'nomutt 'cmdecho) '(1 0))
  (read-line file1)
  (while (setq list1 (read-line file1))
    (setq list1 (mapcar '(lambda (x)
                           (if (= (type x) 'sym)
                             (vl-symbol-name x)
                             x))
                        (read (strcat "(" list1 ")"))))
    (entmake (list '(0 . "BLOCK")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbBlockBegin")
                   (cons 2 (car list1))
                   (cons 10 (setq pt1 (reverse (cdr (reverse (cdr list1))))))
                   '(70 . 0)))
    (entmake (list '(0 . "CIRCLE")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbCircle")
                   '(8 . "0")
                   (cons 10 pt1)
                   (cons 40 (/ (last list1) 2.0))))
    (entmake (list '(0 . "ENDBLK") '(100 . "AcDbEntity") '(100 . "AcDbBlockEnd")))
    (vl-cmdf "._-insert" (car list1) pt1 1 1 0))
  (mapcar 'setvar (list 'cmdecho 'nomutt) curvar)
  (setq file1  (open (getfiled "Select Lines File" "" "txt" 0) "r")
        curvar (mapcar 'getvar (list 'cmdecho 'nomutt)))
  (mapcar 'setvar (list 'nomutt 'cmdecho) '(1 0))
  (read-line file1)
  (while (setq list1 (read-line file1))
    (setq list1 (mapcar '(lambda (x)
                           (if (= (type x) 'sym)
                             (vl-symbol-name x)
                             x))
                        (read (strcat "(" list1 ")")))
          list2 (read-line file1)
          list2 (mapcar '(lambda (x)
                           (if (= (type x) 'sym)
                             (vl-symbol-name x)
                             x))
                        (read (strcat "(" list2 ")"))))
    (entmake (list '(0 . "BLOCK")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbBlockBegin")
                   (cons 2 (last list1))
                   (cons 10 (setq pt1 (reverse (cdr (reverse list1)))))
                   '(70 . 0)))
    (entmake (list '(0 . "LINE")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbLine")
                   '(8 . "0")
                   (cons 10 pt1)
                   (cons 11 (reverse (cdr (reverse list2))))))
    (entmake (list '(0 . "ENDBLK") '(100 . "AcDbEntity") '(100 . "AcDbBlockEnd")))
    (vl-cmdf "._-insert" (last list1) pt1 1 1 0))
  (mapcar 'setvar (list 'cmdecho 'nomutt) curvar)
  (princ))

 

0 Likes
Message 3 of 10

Anonymous
Not applicable

Yes bro.. It worked.. but finally I'm getting this as an error (see"error.jpg"). I don't know what it means..

 

And another one problem,

Drawn circles having names but not visible. Im Using properties or "Edit in block place" to view the names of circles.Is there any Possibility to show the block name at center of the circlar block..

 

(Suggestion -can we give the text of a particular block as an attribute.. see Attribute.jpg)..

0 Likes
Message 4 of 10

Ranjit_Singh
Advisor
Advisor

I am not getting any errors. Seems like after calling the routine you are undoing a bunch of actions and then calling the routine again. That could be the problem. The code needs an error trap. I recommended in my last post that you should add it. I will add it. Also the attribute option can be added. But I might not be able to get to it until tomorrow. I will definitely try if I can get it done soon.

Message 5 of 10

Ranjit_Singh
Advisor
Advisor

Try the below. I added error trap and attribute.

(defun c:somefunc (/ adoc curvar list1 list2 file1 pt1)
  (defun *error*  (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (mapcar 'setvar (list 'cmdecho 'nomutt) curvar)
    (vla-endundomark adoc)
    (princ))
  (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
  (setq file1  (open (getfiled "Select Circles File" "" "txt" 0) "r")
        curvar (mapcar 'getvar (list 'cmdecho 'nomutt)))
  (mapcar 'setvar (list 'nomutt 'cmdecho) '(1 0))
  (read-line file1)
  (while (setq list1 (read-line file1))
    (setq list1 (mapcar '(lambda (x)
                           (if (= (type x) 'sym)
                             (vl-symbol-name x)
                             x))
                        (read (strcat "(" list1 ")"))))
    (entmake (list '(0 . "BLOCK")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbBlockBegin")
                   (cons 2 (car list1))
                   (cons 10 (setq pt1 (reverse (cdr (reverse (cdr list1))))))
                   '(70 . 2)))
    (entmake (list '(0 . "ATTDEF")
                   '(8 . "0")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbText")
                   (cons 10 pt1)
                   (cons 40 (getvar 'textsize))
                   '(1 . "DUT1.MT5")
                   '(100 . "AcDbAttributeDefinition")
                   '(3 . "Enter Drill Name")
                   '(2 . "Drill Name")
                   '(70 . 0)))
    (entmake (list '(0 . "TEXT")
                   '(100 . "AcDbEntity")
                   '(8 . "0")
                   '(100 . "AcDbText")
                   (cons 10 (subst (1+ (cadr pt1)) (cadr pt1) pt1))
                   (cons 40 (* (getvar 'textsize) 2))
                   '(1 . "123")
                   '(100 . "AcDbText")))
    (entmake (list '(0 . "CIRCLE")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbCircle")
                   '(8 . "0")
                   (cons 10 pt1)
                   (cons 40 (/ (last list1) 2.0))))
    (entmake (list '(0 . "ENDBLK") '(100 . "AcDbEntity") '(100 . "AcDbBlockEnd")))
    (vl-cmdf "._-insert" (car list1) pt1 1 1 0))
  (mapcar 'setvar (list 'cmdecho 'nomutt) curvar)
  (setq file1  (open (getfiled "Select Lines File" "" "txt" 0) "r")
        curvar (mapcar 'getvar (list 'cmdecho 'nomutt)))
  (mapcar 'setvar (list 'nomutt 'cmdecho) '(1 0))
  (read-line file1)
  (while (setq list1 (read-line file1))
    (setq list1 (mapcar '(lambda (x)
                           (if (= (type x) 'sym)
                             (vl-symbol-name x)
                             x))
                        (read (strcat "(" list1 ")")))
          list2 (read-line file1)
          list2 (mapcar '(lambda (x)
                           (if (= (type x) 'sym)
                             (vl-symbol-name x)
                             x))
                        (read (strcat "(" list2 ")"))))
    (entmake (list '(0 . "BLOCK")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbBlockBegin")
                   (cons 2 (last list1))
                   (cons 10 (setq pt1 (reverse (cdr (reverse list1)))))
                   '(70 . 0)))
    (entmake (list '(0 . "LINE")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbLine")
                   '(8 . "0")
                   (cons 10 pt1)
                   (cons 11 (reverse (cdr (reverse list2))))))
    (entmake (list '(0 . "ENDBLK") '(100 . "AcDbEntity") '(100 . "AcDbBlockEnd")))
    (vl-cmdf "._-insert" (last list1) pt1 1 1 0))
  (mapcar 'setvar (list 'cmdecho 'nomutt) curvar)
  (*error* "end"))
0 Likes
Message 6 of 10

Anonymous
Not applicable
I'll try and let you know. Can you post the format of text file for both circles and lines you used. Because I'm a bit confused to use "z Co ordinate" or not..
0 Likes
Message 7 of 10

Ranjit_Singh
Advisor
Advisor

I used the exact same format that you posted (post 1). I only took out the comment statements in the line file. I can post the text files but I m on my phone right now. Will try to post them later.

0 Likes
Message 8 of 10

Ranjit_Singh
Advisor
Advisor

OK. See attachments. I have added the circles and lines files . They are exactly what you have posted in post1. I tested the routine. Adding a z value works as intended.

0 Likes
Message 9 of 10

john.uhden
Mentor
Mentor

That's a clever way of turning a space-delimited string into a list.  I don't think I have ever used it.

John F. Uhden

0 Likes
Message 10 of 10

Ranjit_Singh
Advisor
Advisor

Smiley Very Happy I use it almost always when processing text files. Else it's a pain to catch all the spaces.

0 Likes