Create layer with true Color in Lisp

Create layer with true Color in Lisp

Anonymous
Not applicable
9,598 Views
25 Replies
Message 1 of 26

Create layer with true Color in Lisp

Anonymous
Not applicable

I have a routine that creates layers & assigns the color & linetype but now i have to create a few that have the true color instead of the standard autocad color index such as "0-255". Normally i would create a layer, assign it color 4 with a continuous linetype. How can i create a layer & assign it the color of "255,209,0" in lisp?

 

Here is a sample:

 

("K-BEV" "4" "CONTINUOUS")
("K-BEV-MISC" "11" "CONTINUOUS")
("K-CENT-CKEP" "4" "CONTINUOUS")
("K-CENT-XXEX" "4" "CONTINUOUS")
("K-CENT-XXXP" "4" "CONTINUOUS")
("K-CHAS-CKEP" "4" "CONTINUOUS")
("K-DEMO" "255,209,0" "HIDDEN4")

0 Likes
Accepted solutions (1)
9,599 Views
25 Replies
Replies (25)
Message 21 of 26

Anonymous
Not applicable

We usually a have about 300 layers in our xrefed drawing. We call it xplan & it has all the layers for walls, doors, etc & it get xrefed into a plan drawing. Sometimes either a layer gets purged or something or we just want to restore all of our standards into a drawing.

0 Likes
Message 22 of 26

Anonymous
Not applicable

Thanks very much

0 Likes
Message 23 of 26

hak_vz
Advisor
Advisor

@Kent1Cooperwrote:

@Anonymouswrote:


In (entmake)ing a Layer [at least in Acad2016 I have here], it doesn't even do that, but simply does not create the Layer at all.  It does  at least create it, and default to Continuous if it doesn't find the specified linetype, when using (command "_.layer" "_make" ....) [or "_new"].

 



Entmake-ing  create layers but it needs a little change. It works with Acad 2014 at least

(defun truecolor (col)
   (+ (lsh (fix (nth 0 col)) 16)
    (lsh (fix (nth 1 col)) 8)
    (fix (nth 2 col))
   )
)

(defun create_layer (name linetype col)
  (setq lyr (tblsearch "layer" name))
  (if (listp col) (setq col (cons 420 (truecolor col))) (setq col (cons 62 col)))
  (if (not lyr)
     (entmake (list '(0 . "LAYER")
			 '(100 . "AcDbSymbolTableRecord")
			 '(100 . "AcDbLayerTableRecord")
			 '(70 . 0)
			 (cons 2 name)
			 col
			 (cons 6
			       (if (tblsearch "ltype" linetype)
				 linetype
				 "continuous"
			       )
			 )))
  (tblobjname "layer" name)
))

(create_layer "truecolored" "border" '(255 0 255))
(create_layer "indexed" "border" 150)

Problem is obviously in coexistence of (cons 62 index) and (420 truecolor). In older versions you would
define indexed color and override it (filter it) with true color. If opened with version introduced before True color incorporation
into ACAD user would have entities presented with indexed color. In new versions you can define entity (layer) with either indexed or truecolor.


Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 24 of 26

tamas.halasz
Enthusiast
Enthusiast

Could anyone modify this for me so it changes the lineweight instead of the plot status? Heart

0 Likes
Message 25 of 26

3dwannab
Advocate
Advocate

Here you  go.

 

Original code by ronjonp which I then used LeeMacs code that I further modified. See comments in the code below.

 

Modified by 3dwannab on 2024.05.23 to add transparency, description and plot to Lee Macs function.

 

 

 

 

 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; _createOrUpdateLayer
;;
;; Creates a new layer or updates an existing one if it exists.
;; Will attempt to load the linetype if found in the acad*.lin file and if the layer exists, it will update the properties.
;; Original code by ronjonp, taken from: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/create-layer-with-true-color-in-lisp/m-p/7904814#M367339
;;
;; USAGE:
;; (_createOrUpdateLayer "Test1" 22 "Dashed" aclnwtbylwdefault 33 :vlax-true "Test1 Description")
;; (_createOrUpdateLayer "Test2" '(100 150 200) "Continuous" aclnwtbylwdefault 33 :vlax-false "Test2 Description")
;;
;; The :vlax-true or :vlax-false in examples above is for plot on or off.
;; The 33 values represent the transparency of the layer.
;; Original help from mhupp here to get it to update layer if it exists: https://www.cadtutor.net/forum/topic/75414-defun-to-create-or-update-layers-not-updating-them/?do=findComment&comment=596266
;;
;; ARGUMENTS:                                ARGUMENTS SYTAX:
;; 1st arg: Layers Name                  =   STRING
;; 2nd arg: Layers Colour                =   22 or '(100 150 200) (Can be an indexed colour or true colour)
;; 3rd arg: Layers Linetype              =   STRING
;; 4th arg: Layers Lineweight            =   acLnWtByLayer, acLnWtByBlock, acLnWtByLwDefault, acLnWt000, acLnWt005, acLnWt009, acLnWt013, acLnWt015, acLnWt018, acLnWt020, acLnWt025, acLnWt030, acLnWt035, acLnWt040, acLnWt050, acLnWt053, acLnWt060, acLnWt070, acLnWt080, acLnWt090, acLnWt100, acLnWt106, acLnWt120, acLnWt140, acLnWt158, acLnWt200, acLnWt211
;; 5th arg: Layers Transparency          =   INTEGER (Between 0-90)
;; 6th arg: Layers is Plottable on not   =   :vlax-true = Plottable or :vlax-false = Not Plottable
;; 7th arg: Layers Description           =   STRING
;;
;; For Linewiight in vlisp, see this reply by CAB: https://www.theswamp.org/index.php?topic=22438.msg270124#msg270124
;;
;; MODIFICATIONS:
;; New code By Leemac with small modifications by 3dwannab.
;; Modified by 3dwannab on 2024.05.22 to add if the layers is plottable and also added the possibility to add a description to the layer.
;; Lee Macs code: https://www.cadtutor.net/forum/topic/66765-vla-put-truecolor/?do=findComment&comment=546879
;; Modified by 3dwannab on 2024.05.22 to add transparency, description and plot to Lee Macs function.
;; Help from mhupp here to get it to update layer if it exists: https://www.cadtutor.net/forum/topic/75414-defun-to-create-or-update-layers-not-updating-them/?do=findComment&comment=596266

(defun _createOrUpdateLayer (name color linetype lineweight transparency plottable description / _loadlinetype lay tru) 

  ;; Credit to Grr1337
  ;; https://www.theswamp.org/index.php?topic=52473.msg574008#msg574008
  ;; (_SetLayerTransparency (getvar 'clayer) 90)
  (defun _SetLayerTransparency (LayerName Transparency / lyr) 
    (and 
      (<= 0 Transparency 90)
      (setq lyr (tblobjname "LAYER" LayerName))
      (not (setpropertyvalue lyr "Transparency" Transparency))
      (not (entupd lyr))
    )
  )

  (defun _loadlinetype (ltype / lt out) 
    (cond 
      ((tblobjname "ltype" ltype) t)
      ((setq lt (vla-get-linetypes (vla-get-activedocument (vlax-get-acad-object))))
       (setq out (vl-catch-all-apply 
                   'vla-load
                   (list lt 
                         ltype
                         (findfile 
                           (if (= 0 (getvar 'measurement)) 
                             "acad.lin"
                             "acadiso.lin"
                           )
                         )
                   )
                 )
       )
       (not (vl-catch-all-error-p out))
      )
    )
  ) ;; defun _loadlinetype

  ;; Set the layer properties
  (setq lay (vla-add (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) name)) ;; 1st arg
  (if (listp color) 
    (progn 
      (setq tru (vla-get-truecolor lay))
      (apply 'vla-setrgb (cons tru color))
      (vla-put-truecolor lay tru)
    )
    (vla-put-color lay color) ;; 2nd arg
  )
  (vla-put-linetype lay 
                    (if (_loadlinetype linetype) 
                      linetype
                      "continuous"
                    )
  ) ;; 3rd arg
  (vla-put-lineweight lay lineweight) ;; 4th arg
  (_SetLayerTransparency name transparency) ;; 5th arg
  (vla-put-Plottable lay plottable) ;; 6th arg
  (vla-put-description lay description) ;; 7th arg
  lay
) ;; _createOrUpdateLayer defun

(_createOrUpdateLayer "Test1" 22 "Dashed" aclnwtbylwdefault 65 :vlax-true "Test1 Description")
(_createOrUpdateLayer "Test2" '(100 150 200) "Continuous" acLnWt013 65 :vlax-true "Test2 Description")

 

 

 

 

 

 

0 Likes
Message 26 of 26

Sea-Haven
Mentor
Mentor

Just a suggestion sometimes just want simple make layer.

 

(_createOrUpdateLayer2 = (_createOrUpdateLayer2 "beams" 1) simple layer with color red and Continuous

(_createOrUpdateLayer3 = (_createOrUpdateLayer3 "beams" 1 "Dashed") simple layer color red dashed linetype

 

Dont always need all 6 options.

0 Likes