Jump to content

Modifying layer properties in AutoCAD


Jyunga

Recommended Posts

Hello, esteemed colleagues,

I am currently navigating the complexities of Visual LISP programming within AutoCAD with the aim to enhance my project's efficiency and customization. My endeavor involves crafting a generic code capable of altering existing layer properties. The focal point of this initiative is to enable the adjustment of layer attributes such as color (specifically to True Color format, e.g., 150,10,90), line thickness (setting it to 0.30), line type (changing it to 'continuous'), toggling print locking, and activating the layer. Despite my efforts, I am encountering a roadblock in applying True Color, leading to errors and halting progress. I am reaching out for your wisdom and guidance to rectify this issue or to approach this task more effectively. Below is the code snippet I've been grappling with.

 

(defun c:ModCapa (/ capa colorR colorG colorB lineweight linetype plot activar)
  (vl-load-com)
  
  ;; Ejemplo de cómo llamar este comando:
  ;; (ModCapa "JY1" 125 20 50 0.25 "Continuous" "No" "Si")

  ;; Asignar valores directamente desde los argumentos
  (setq capa "NombreCapa") ; Reemplaza "NombreCapa" con el nombre real de la capa a modificar
  (setq colorR 125) ; Valor rojo del color
  (setq colorG 20)  ; Valor verde del color
  (setq colorB 50)  ; Valor azul del color
  (setq lineweight 0.25) ; Ancho de línea en milímetros
  (setq linetype "Continuous") ; Tipo de línea
  (setq plot "No") ; Si la capa debe ser ploteada
  (setq activar "Si") ; Si la capa debe ser establecida como activa

  ;; Verificar si la capa existe
  (if (not (tblsearch "LAYER" capa))
    (princ (strcat "\nLa capa " capa " no existe."))
    (progn
      ;; Modificar la capa
      (command "-LAYER" "M" capa "")
      (command "C" (strcat "C" (itoa colorR) "," (itoa colorG) "," (itoa colorB)) "")
      (command "L" linetype "")
      (if (= (strcase plot) "NO") (command "P" "No") (command "P" "Yes"))
      (command "W" lineweight "")
      (command "")

      ;; Establecer como capa activa si se solicita
      (if (= (strcase activar) "SI")
        (command "-LAYER" "S" capa "")
      )
      
      (princ (strcat "\nLa capa " capa " ha sido modificada y activada."))
    )
  )
  (princ)
)

 

Link to comment
Share on other sites

It looks like your code is going in the right direction, but the syntax does not do what you want it to do.

 

First, (command "-LAYER" "M" capa "") creates a layer with the specified name. The last argument, though, ends the LAYER command.

 

The next few statements do not apply to the new layer. The statement (command "C" ...) will run as if you're back at the command prompt, which means it will try to run the Circle command, (command "L"...) will try to draw a line, and so on.

 

Your first step is to remove the last argument in the first command statement. That will keep the LAYER command prompt open.

 

You can test your function by typing each line in sequence. That will give you a better idea of what is really happening.

Link to comment
Share on other sites

A suggestion.

 

(command "-LAYER" "M" capa
"C" (strcat (itoa colorR) "," (itoa colorG) "," (itoa colorB)) ""
"LT" linetype ""
"P" plot ""
"LW" lineweight ""
"")

 

(setvar 'clayer SI)

 

Link to comment
Share on other sites

I would go the entmake route its a lot cleaner and less spam in the command prompt.

 

@ronjonp made a good one. it also "attempt to load the linetype if found in the acad*.lin file and if the layer exists, it will update the properties."

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-layer-with-true-color-in-lisp/m-p/7904814/highlight/true#M367339

 

(_addlayer "NewLayerName" '(69 69 69) "3Dash2" 1)

changes to

(_addlayer "NewLayerName" (list colorR colorG colorB) "3Dash2" 1)

 

 

  • Like 1
Link to comment
Share on other sites

An example entmake layer.

 

(defun Layer (Name Col Ltyp LWgt Plt)
 (entmake (list (cons 0 "LAYER")
                (cons 100 "AcDbSymbolTableRecord")
                (cons 100 "AcDbLayerTableRecord")
                (cons 2  Name)
                (cons 70 0)
                (cons 62 Col)
                (cons 6 Ltyp)
                (cons 290 Plt)
                (cons 370 LWgt))))

 

  • Like 1
Link to comment
Share on other sites

fyi if your using entmake 62 will only works with colors 1 - 256 for true colors you need to use 420

 

you can have the user select what color they want with

 

(setq col (acad_truecolordlg (62 . 1)) ;true color menu defaults to red
(setq col (acad_truecolordlg (420 . 8227074)) ;true color menu defaults to 125 20 50


;; RGB -> True  -  Lee Mac
;; Args: r,g,b - [int] Red, Green, Blue values
(defun LM:RGB->True ( r g b )
    (logior (lsh (fix r) 16) (lsh (fix g) 8) (fix b))
)

;; True -> RGB  -  Lee Mac
;; Args: c - [int] True Colour
(defun LM:True->RGB ( c )
    (mapcar '(lambda ( x ) (lsh (lsh (fix c) x) -24)) '(8 16 24))
)

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...