How to read Layer in AutoLisp - autocad

How to read layer from poline?
e.g with this i can get (8. "LAYER_NAME") but i wanna see onyl LAYER_NAME because I want to later check by name something
(defun c:test ( / ent enx )
(if (setq ent (car (entsel)))
(progn
(setq enx (entget ent))
(assoc 8 enx)
)
)
)

Change the line
(assoc 8 enx)
to
(cdr (assoc 8 enx))

Related

Autocad Autolisp variable Start Z / End Z

What variable will we extract the START Z value from the line.
e.g.
getvar "perimeter" - get the length of the selected polyline
You will need to obtain the list of coordinates associated with DXF group 10 from the DXF data for the line.
You can obtain the DXF data for the line entity using the entget function:
(defun c:test ( / ent enx )
(if (setq ent (car (entsel)))
(setq enx (entget ent))
)
)
You can then obtain DXF group 10 using the assoc function:
(defun c:test ( / ent enx )
(if (setq ent (car (entsel)))
(progn
(setq enx (entget ent))
(assoc 10 enx)
)
)
)

how to change attribute of block using ObjectDBX

I need to change an attribute of drawing with the ObjectDBX method using AutoLISP. this routine run properly but not change the attribute,can you suggest any change in code or any other method to achieve this task?
Thank you.
;;;;;;;;;;;;;;;;;;;
(defun DBX_ATT_CHANGE (f)
(vl-load-com)
(setq cadver (substr (getvar "acadver") 1 2))
(setq id (strcat "objectdbx.AxDbDocument." cadver))
(setq dbx (vlax-create-object id))
(vla-open dbx f)
(vlax-for n_object (vla-get-modelspace dbx)
(setq dbx_en (vlax-vla-object->ename n_object))
(setq upc_blkobj (vlax-ename->vla-object dbx_en))
(if (vlax-method-applicable-p upc_blkobj 'GetAttributes)
(progn
(setq upc_attlist
(vlax-invoke upc_blkobj 'GetAttributes)
)
(foreach upc_att upc_attlist
(progn
(if (= (vla-get-tagstring upc_att) (strcase "P_TAG1"))
(vlax-put-property
upc_att
'TextString
"555"
)
)
)
)
)
)
(vlax-release-object upc_blkobj)
)
(vla-saveas dbx dwgfile)
(vlax-release-object dbx)
(prin1)
)
(defun c:test ()
(DBX_ATT_CHANGE
"D:/6. R&D/Delet Group LispDBXapi/7-EU-FE-48-AC-CIOC-SA - Copy.dwg"
)
)
;;;;;;;;;;;;;;;
There are a couple of oddities present in your current code:
(setq dbx_en (vlax-vla-object->ename n_object))
(setq upc_blkobj (vlax-ename->vla-object dbx_en))
You are converting the vla-object n_object into an entity name dbx_en, and then converting this entity name back into a vla-object upc_blkobj. These two lines are redundant, as you can work with the n_object variable directly.
(= (vla-get-tagstring upc_att) (strcase "P_TAG1"))
You are using strcase to convert a literal uppercase string P_TAG1 to uppercase, and then comparing this uppercase string to a string which may or may not be uppercase - I believe this line should be:
(= (strcase (vla-get-tagstring upc_att)) "P_TAG1")
To offer you an alternative for this task, you could make use of my ObjectDBX Wrapper function, which provides a way to evaluate a given function on another drawing or set of drawings, without opening such drawings in the AutoCAD Editor.
I would personally write your code in the following way:
(defun c:test ( )
(LM:DBXAttChange
"D:\\6. R&D\\Delet Group LispDBXapi\\7-EU-FE-48-AC-CIOC-SA - Copy.dwg"
'(("P_TAG1" . "555"))
)
(princ)
)
(defun LM:DBXAttChange ( dwg lst / doc flg val )
(if (setq doc (LM:GetDocumentObject dwg))
(progn
(vlax-for lyt (vla-get-layouts doc)
(vlax-for obj (vla-get-block lyt)
(if (and (= "AcDbBlockReference" (vla-get-objectname obj))
(= :vlax-true (vla-get-hasattributes obj))
)
(foreach att (vlax-invoke obj 'getattributes)
(if (and (setq val (cdr (assoc (strcase (vla-get-tagstring att)) lst)))
(vlax-write-enabled-p att)
)
(progn
(vla-put-textstring att val)
(setq flg t)
)
)
)
)
)
)
(if flg (vla-saveas doc dwg))
(vlax-release-object doc)
flg
)
(prompt (strcat "\nThe drawing \"" dwg "\" was not found or could not be accessed."))
)
)
;; Get Document Object - Lee Mac
;; Retrieves the VLA Document Object for the supplied filename.
;; The Document Object may be present in the Documents collection, or obtained through ObjectDBX.
;; It is the callers responsibility to release such object.
(defun LM:GetDocumentObject ( dwg / app dbx dwl err vrs )
(cond
( (not (setq dwg (findfile dwg))) nil)
( (cdr
(assoc (strcase dwg)
(vlax-for doc (vla-get-documents (setq app (vlax-get-acad-object)))
(setq dwl (cons (cons (strcase (vla-get-fullname doc)) doc) dwl))
)
)
)
)
( (progn
(setq dbx
(vl-catch-all-apply 'vla-getinterfaceobject
(list app
(if (< (setq vrs (atoi (getvar 'acadver))) 16)
"objectdbx.axdbdocument" (strcat "objectdbx.axdbdocument." (itoa vrs))
)
)
)
)
(or (null dbx) (vl-catch-all-error-p dbx))
)
(prompt "\nUnable to interface with ObjectDBX.")
)
( (vl-catch-all-error-p (setq err (vl-catch-all-apply 'vla-open (list dbx dwg))))
(prompt (strcat "\n" (vl-catch-all-error-message err)))
)
( dbx )
)
)
(vl-load-com) (princ)

Change 3d polyline to spline in Autocad with lisp

I have a working lisp which creates spline from 3d polylines. My problem is I cant make it work to select multiple 3d polylines or entire layer, and also a lisp changes the result layer to the default layer. It should kept on the original one.
Here is my working lisp:
(defun c:3p2spl ( / *error* line2spl loop pl e s ss sss )
(vl-load-com)
(defun *error* ( msg )
(vla-endundomark (vla-get-activedocument (vlax-get-acad-object)))
)
(defun line2spl ( e / sp ep d )
(setq sp (cdr (assoc 10 (entget e)))
ep (cdr (assoc 11 (entget e)))
d (distance sp ep)
)
(entdel e)
(entmakex
(list
'(0 . "SPLINE") '(100 . "AcDbEntity") '(100 . "AcDbSpline") '(210 0.0 0.0 1.0) '(71 . 1) '(73 . 2)
'(42 . 1.0e-010) '(43 . 1.0e-010) '(40 . 0.0) '(40 . 0.0) (cons 40 d) (cons 40 d) (cons 10 sp) (cons 10 ep)
)
)
)
(vla-startundomark (vla-get-activedocument (vlax-get-acad-object)))
(setq loop T)
(setq sss (ssget "_I"))
(if (and sss (eq (cdr (assoc 0 (entget (setq pl (ssname sss 0))))) "POLYLINE") (< 7 (cdr (assoc 70 (entget pl))) 14)) (setq loop nil))
(while loop
(setq pl (car (entsel "\nPick 3DPOLYLINE to convert it to SPLINE")))
(if (and (eq (cdr (assoc 0 (entget pl))) "POLYLINE") (< 7 (cdr (assoc 70 (entget pl))) 14)) (setq loop nil))
)
(setq e (entlast))
(command "_.explode" pl "")
(setq ss (ssadd))
(while (setq e (entnext e))
(if (eq (cdr (assoc 0 (entget e))) "LINE")
(progn
(setq s (line2spl e))
(ssadd s ss)
)
)
)
(command "_.join" (ssname ss 0) ss "")
(*error* nil)
(princ)
)
if I change the ssget "_I" to ssget "_:E" I can select multiple lines however it will change to spline only the first one.
I didn't test this code, only read, so maybe I'm wrong, but I thing the problem is because:
...
(setq pl (ssname sss 0))
...
(command "_.explode" pl "")
You explode only first entity.
I think
(command "_.explode" pl "")
should be used inside loop by all sss items.

autocad : script that runs autolisp functions

I've got a working batch-file that runs a script on a bunch of drawings.
The script is supposed to run a lisp-function but that function appears to only run after the main function has ran.
since I don't know much about lisps, I'll try to give the information I have.
the lsp :
(princ "\nLoading AREAS...")
(defun c:areas(); Start the program.
(setvar "cmdecho" 0)
(if (= (getvar "tilemode") 1)
(progn
(command "_.ucs" "_world")
(setq osnp (getvar "osmode"))
(setq laag (getvar "clayer"))
(setvar "osmode" 0)
(setq dimz (getvar "dimzin"))
(setvar "dimzin" 0)
(ge_dellay ladeptmp)
(if (>= (substr (getvar "acadver") 1 2) "15")
(ge_convert)
)
(setq allsel (list (cons 0 "POLYLINE")'(-4 . "<OR")(cons 8 ladeppoly)(cons 8 ladeptraf)'(-4 . "OR>")'(-3 ("COSBI"))))
(setq depsel (list (cons 0 "POLYLINE")(cons 8 ladeppoly)'(-3 ("COSBI"))))
(setq areasel (list (cons 0 "POLYLINE")(cons 8 ladeptraf)'(-3 ("COSBI"))))
(setq textsel (list (cons 0 "TEXT")(cons 8 ladeptext)'(-3 ("COSBI"))))
(setq dcl_area (load_dialog "areas"))
(setq dcl_gen (load_dialog "general"))
(setq intp nil dparea nil seltot nil)
(ar_setdep)
(ar_dia)
(if (= what_next 1)(ar_setlay))
(if (= what_next 2)(ar_check))
(if (= what_next 3)(ar_startcheck))
(unload_dialog dcl_area)
(unload_dialog dcl_gen)
(setvar "osmode" osnp)
(setvar "clayer" laag)
(setvar "dimzin" dimz)
(ge_dellay ladeptmp)
)
(alert "Only allowed in original drawing...")
)
(princ)
)
is followed by a few other (not sure) less important functions, like the ar_dia - which opens a dialog box with buttons to call other functions.
One of the other functions is AR_LIST, which is the one I need to run on each file the batch-file opens, in a script.
the ar_list is a few blocks down and looks like this
(defun ar_list(); Make department/areas list.
(setq sel (ssget "x" allsel))
(if sel
(progn
(setq temp (findfile "template.sqm"))
(if temp
(progn
(command "_.zoom" "_all")
(setq rowlist nil deplist nil)
(setq bestand (open temp "r"))
(setq row (read-line bestand))
(while row
(setq row (read-line bestand))
(if row
(progn
(setq rowlist (cons (strcase (strcat (spatie (substr row 23 14)) "_-")) rowlist))
(setq deplist (cons (strcase (spatie (substr row 23 14))) deplist))
)
)
)
(setq country (ge_dir 3 "Country"))
(ge_dwg)
(if (= (strlen dwgnaam) 9); 3to4storenr
(progn
(setq store (substr dwgnaam (- (strlen dwgnaam) 3))); 3to4storenr
(setq floor (substr dwgnaam (- (strlen dwgnaam) 5) 2)); 3to4storenr
(setq num 0)
(repeat (sslength sel)
(setq depname (cdr (cadadr (assoc -3 (entget (ssname sel num)'("COSBI"))))))
(if (not (wcmatch depname "*`island*"))
(progn
(setq ename (ssname sel num))
(command "_.area" "_a" "_o" ename "")
(ge_puntlist ename)
(setq numpol 0)
(setq selpol (ssget "_wp" puntlist allsel))
(if selpol
(repeat (sslength selpol)
(setq islname (cdr (cadadr (assoc -3 (entget (ssname selpol numpol)'("COSBI"))))))
(if (= islname (strcat depname "-island"))
(command "_s" "_o" (ssname selpol numpol) "")
)
(setq numpol (1+ numpol))
)
)
(command "")
(setq deparea (/ (getvar "area") 1000000))
(if (not (member (strcase depname) deplist))
(progn
(setq deplist (cons (strcase depname) deplist))
(setq rowlist (cons (strcase (strcat depname "_-")) rowlist))
)
)
(setq nummem (- (length deplist)(length (member (strcase depname) deplist))))
(setq deptot (nth nummem rowlist))
(vindpos "_" deptot)
(setq depareaold (substr deptot (+ pos 1)))
(if (/= depareaold "-")
(setq deparea (+ (atof depareaold) deparea))
)
(setq rowlist (subst (strcase (strcat depname "_" (rtos deparea 2 1))) (nth nummem rowlist) rowlist))
)
)
(setq num (1+ num))
)
(command "_.zoom" "_previous")
(setq rowlist (acad_strlsort rowlist)); 13-10-2014
(setq deplist (acad_strlsort deplist)); 13-10-2014
;(setq rowlist (reverse rowlist))
;(setq deplist (reverse deplist))
(ar_write)
)
(alert (strcat "Drawing name " dwgnaam " not correct, must be 9 characters.")); 3to4storenr
)
)
(alert "File TEMPLATE.SQM not found...")
)
)
(alert "No department or traffic found...")
)
)
the script only needs to run this command, close the drawing, and don't save.
so I tried (test.scr)
(ar_list)
quit
n
but that gives me the error:
Command: (ar_list)
bad argument type: stringp nil
I think the ar_list needs something from the defun c:areas , but I don't know what. The ar_list works after entering areas in the command bar.
So I also tried
areas
(ar_list)
quit
n
, but that opened the areas dialog box, does not close it, blocking the loop.
Also, when I cancel the dialog box, the ar_list works, but it again opens the areas dialog box. I think the code repeats itself in the script.
Any help would be very welcome. I received related help on here
stringp nil give us sugestion that some variable which should be text string in fact is nil. Probably because it reads value from dialog control (which is unavaliable while dialog is not active).
there is few places which may cause such problem:
we don't know what happes in ge_dir ge_dwg ge_puntlist vindpos ar_write
ar_write Maybe want to write something to dialog?
variable dwgnaam is used as string but never initialized in this function, (maybe somewhere else it is?)
(setq depname (cdr (cadadr (assoc -3 (entget (ssname sel num)'("COSBI")))))) (if (not (wcmatch depname "*island*")) if selected entity not contains XData "cosbi", it can be problem, but if I'm not wrong, there is other error message.

resize image using script-fu gimp

I'm trying to prepare a script for auto-resizing image files.
I found this LINK but I cannot figure out how to use it.
Anyone can provide a working script that I can use as a starting point?
The following function resizes the image:
(define (resize-image filename-in filename-out new-width new-height)
(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename-in "")))
(drawable (car (gimp-image-active-drawable image)))
)
(gimp-image-scale image new-width new-height)
(gimp-file-save RUN-NONINTERACTIVE image drawable filename-out "")
)
)
Now, resizing all jpg's in a directory:
(define (file-basename filename)
(let*
(
(broken-up (strbreakup filename "."))
(wo-last-r (cdr (reverse broken-up)))
(wo-last (reverse wo-last-r))
(result "")
)
(while wo-last
(set! result (string-append result (car wo-last) ))
(set! wo-last (cdr wo-last))
(if (> (length wo-last) 0) (set! result (string-append result ".")))
)
result
)
)
(define (ex_09 file-pattern new-width new-height )
(let* ( (filelist (cadr (file-glob file-pattern 1))))
(while (not (null? filelist))
(let* ( (cur-file (car filelist)) )
(resize-image
cur-file
(string-append (file-basename cur-file) "_resized.jpg")
100
100
)
(set! filelist (cdr filelist))
)
)
)
)
I think that this is your answer.
The code come from this address.
http://www.adp-gmbh.ch/misc/tools/script_fu/ex_09.html
Out of the box it doesn't work for me.
I made some changes :
In the file_basename.scm file I remove some stuff I didn't get around with.
So the resized files are created in the same directory than the original files :
(define (file-basename filename)
(let*
(
(broken-up (strbreakup filename "."))
(wo-last-r (cdr (reverse broken-up)))
(wo-last (reverse wo-last-r))
(car broken-up)
)
)
In the ex_09.scm file :
I just used the new-width and the new-height variables.
(define (ex_09 file-pattern new-width new-height )
(let* ( (filelist (cadr (file-glob file-pattern 1))))
(while (not (null? filelist))
(let* ( (cur-file (car filelist)) )
(resize-image
cur-file
(string-append (file-basename cur-file) "_resized.jpg")
new-width
new-height
)
(set! filelist (cdr filelist))
)
)
)
)
Hop this helps !
and thank yo René Nyffenegger for the code.
:)

Resources