TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Grrr1337 on January 01, 2017, 05:51:23 PM

Title: Set Layer Transparency
Post by: Grrr1337 on January 01, 2017, 05:51:23 PM
Hi guys and happy new year!
Ontopic:
Refering to this thread (http://www.theswamp.org/index.php?topic=44517.0).
I've just "found" a vanilla way about setting the layer transparency, and heres an example to assign a value of 25 % transparency on the current layer:

Code - Auto/Visual Lisp: [Select]
  1.   (setq enx
  2.     (append
  3.       (entget (tblobjname "LAYER" (getvar 'clayer)))
  4.       (list (list -3 (cons "AcCmTransparency" (list (cons 1071 33554623))))); 25 %
  5.     )
  6.   )
  7. )
  8. (entupd (cdr (assoc -1 enx)))

Well yeah, it works simple as that, by appending an xdata to the layer's elist.

The problem is that I have no idea about bit handling and I'm wondering could anyone reverse engineer this part of LM's suggestion in that thread:
Code - Auto/Visual Lisp: [Select]
  1. (fix (- 100 (/ (logand trn -33554433) 2.55)))
So it would be possible to write a subfunction that accepts integer in 0-90 range to set the transparency on layer x (without command-calls). :idea:
P.S. I know its doable with an assoc list, but the code may be less-effectively written.
Title: Re: Set Layer Transparency
Post by: Lee Mac on January 01, 2017, 06:23:10 PM
Here are my functions for transparency conversion:
Code - Auto/Visual Lisp: [Select]
  1. (defun LM:trans->dxf ( x )
  2.     (logior (fix (* 2.55 (- 100 x))) 33554432)
  3. )
  4. (defun LM:dxf->trans ( x )
  5.     (fix (- 100 -1e-8 (/ (logand x (~ 33554432)) 2.55)))
  6. )

Getting/Setting Layer Transparency:
Code - Auto/Visual Lisp: [Select]
  1. (defun LM:setlayertransparency ( lay trn / ent )
  2.     (if (setq ent (tblobjname "layer" lay))
  3.         (progn
  4.             (regapp "accmtransparency")
  5.             (entmod
  6.                 (append (entget ent)
  7.                     (list
  8.                         (list -3
  9.                             (list "accmtransparency"
  10.                                 (cons 1071 (LM:trans->dxf trn))
  11.                             )
  12.                         )
  13.                     )
  14.                 )
  15.             )
  16.         )
  17.     )
  18. )
  19. (defun LM:getlayertransparency ( lay / ent dxf )
  20.     (if (and (setq ent (tblobjname "layer" lay))
  21.              (setq dxf (cdr (assoc 1071 (cdadr (assoc -3 (entget ent '("accmtransparency")))))))
  22.         )
  23.         (LM:dxf->trans dxf)
  24.     )
  25. )

Where entity transparency is concerned, use DXF group 440 with 0 for ByLayer & 16777216 for ByBlock.
Title: Re: Set Layer Transparency
Post by: Grrr1337 on January 01, 2017, 06:53:42 PM
This is awesome, certainly a good start for 2017 !
Thanks alot Lee, wishing you a benefitable and healthy year!
Title: Re: Set Layer Transparency
Post by: Lee Mac on January 01, 2017, 07:15:23 PM
You're most welcome Grrr1337 - all the best for 2017 to you too.
Title: Re: Set Layer Transparency
Post by: gile on January 02, 2017, 03:23:52 AM
Hi,

What about simply doing:
Code - Auto/Visual Lisp: [Select]
  1. (setpropertyvalue (getvar 'clayer) "Transparency" 25)

Happy new year.
Title: Re: Set Layer Transparency
Post by: kdub_nz on January 02, 2017, 03:29:44 AM
Hi,

What about simply doing:
Code - Auto/Visual Lisp: [Select]
  1. (setpropertyvalue (getvar 'clayer) "Transparency" 25)

Happy new year.


:)

Title: Re: Set Layer Transparency
Post by: roy_043 on January 02, 2017, 05:04:36 AM
My conversion functions:
Code - Auto/Visual Lisp: [Select]
  1. ; (KGA_Sys_Transparency_Num_To_Perc 33554661) => 0.1 (= 10%)
  2. ; (KGA_Sys_Transparency_Num_To_Perc (cdr (assoc 440 (entget (car (entsel))))))
  3. (defun KGA_Sys_Transparency_Num_To_Perc (num)
  4.   (* 0.01 (fix (/ (- 33554687 num) 2.55)))
  5. )
  6.  
  7. ; (KGA_Sys_Transparency_Perc_To_Num 0.1) => 33554661
  8. (defun KGA_Sys_Transparency_Perc_To_Num (perc)
  9.   (fix (- 33554687 (* perc 255)))
  10. )
Title: Re: Set Layer Transparency
Post by: Grrr1337 on January 02, 2017, 05:39:36 AM
Hi,

What about simply doing:
Code - Auto/Visual Lisp: [Select]
  1. (setpropertyvalue (getvar 'clayer) "Transparency" 25)

Happy new year.

Thanks Gile,
Actually I tried something similar before and I thought it didn't worked, since I didn't saw any graphical changes in model space.
Just gave it a second shot, and the layer properties manager confirms that the transparency is changed!
BTW setpropertyvalue requires the layer's ename (not the actual name):
Code - Auto/Visual Lisp: [Select]
  1. _$ (setpropertyvalue (tblobjname "LAYER" (getvar 'clayer)) "Transparency" 60)
  2. nil
  3.  
  4. _$ (setpropertyvalue (getvar 'clayer) "Transparency" 25)
  5.  
  6. Error: ADS request error
  7. _1$

Sooo.. another subfunction is created:
Code - Auto/Visual Lisp: [Select]
  1. ; (SetLayerTransparency (getvar 'clayer) 90)
  2. (defun SetLayerTransparency ( LayerName Transparency / lyr )
  3.   (and
  4.     (<= 0 Transparency 90)
  5.     (setq lyr (tblobjname "LAYER" LayerName))
  6.     (not (setpropertyvalue lyr "Transparency" Transparency))
  7.     (not (entupd lyr))
  8.   )
  9. )

For the pure Visual way one might dig here (not sure what method must be invoked):
Code - Auto/Visual Lisp: [Select]

Cheers guys! 8)
Title: Re: Set Layer Transparency
Post by: alanjt on January 04, 2017, 03:21:55 PM
Hi,

What about simply doing:
Code - Auto/Visual Lisp: [Select]
  1. (setpropertyvalue (getvar 'clayer) "Transparency" 25)

Happy new year.
Whoa. When were setpropertyvalue / getpropertyvalue introduced?
Title: Re: Set Layer Transparency
Post by: Grrr1337 on January 04, 2017, 04:47:03 PM
Whoa. When were setpropertyvalue / getpropertyvalue introduced?
Its not the first time that Gile writes/suggest about them. For example in this (https://www.theswamp.org/index.php?topic=52340.0;nowap) thread.
Heres the thread (https://www.theswamp.org/index.php?topic=39259.msg444841#msg444841) where he spreads the word.  :-)
Title: Re: Set Layer Transparency
Post by: alanjt on January 04, 2017, 04:58:14 PM
Whoa. When were setpropertyvalue / getpropertyvalue introduced?
Its not the first time that Gile writes/suggest about them. For example in this (https://www.theswamp.org/index.php?topic=52340.0;nowap) thread.
Heres the thread (https://www.theswamp.org/index.php?topic=39259.msg444841#msg444841) where he spreads the word.  :-)
*quickly deletes post #4 from thread* How did I miss that one?!?