By Adam Nagy
Why doesn't the following code work? vla-get-transparency highlights as blue in the VLIDE, but I get this error:
"Error: ActiveX Server returned the error: unknown name: Transparency"
(setq ENT (entsel))
(if ENT
(progn
(setq VLA_OBJ_NAME (vlax-ename->vla-object (car ENT))
VLA_OBJ_LAYER (vla-get-layer VLA_OBJ_NAME)
VLA_ACT_DOC (vla-get-ActiveDocument (vlax-get-Acad-Object))
LAYERNAME (vla-Item (vla-get-Layers VLA_ACT_DOC) VLA_OBJ_LAYER)
)
(if (>= (atof (substr (getvar "acadver") 1 4)) 18.1)
(setq PROPTRAN (vla-get-transparency LAYERNAME))
)
)
)
Solution
The properties that you access when using the COM helpers of LISP are based on the ActiveX API.
If you look for the Transparency property in the ActiveX API, then you’ll find that it is only available for AcadRasterImage and AcadWipeout entities.
The Transparency for entities (which is only available in AutoCAD 2011) is provided by new interfaces like IAcadEntity2 and they are called EntityTransparency.
In case of layers however, there is no property like that.
That transparency value can be calculated from the layer’s XData. If it’s not there, then it’s the default 0%
; gets transparency in percentage
(defun getLayerTransparency (layerName / layer transparency)
(setq layer (tblobjname "LAYER" layerName))
; get the XData of AcCmTransparency
(setq transparency (cdr (assoc 1071 (cdar (cdr (assoc -3
(entget layer '("AcCmTransparency"))))))))
(if (= transparency nil)
; if we did not get a value it must be the default 0%
(setq transparency 0)
; if we got a value then calculate from it
(progn
; get the lower byte of the value 0..255
; (100%..0% in the AutoCAD user interface)
(setq transparency (lsh (lsh transparency 24) -24))
; convert the value to a percentage
(setq transparency (fix (- 100 (/ transparency 2.55))))
) ; (progn
) ; (if
)
(defun c:testGet (/ ent layerName transparency)
(setq ent (car (entsel)))
(setq layerName (cdr (assoc 8 (entget ent))))
(setq transparency (getLayerTransparency layerName))
(princ transparency)
(princ)
)
It does not seem possible to set the value the same way - by adjusting the XData value -, but you can use the _LAYER command for that:
(defun c:testSet (/ ent layerName transparency)
(setq ent (car (entsel)))
(setq transparency (getint "Transparency value"))
(setq layerName (cdr (assoc 8 (entget ent))))
(command "_LAYER" "_TR" transparency layerName "")
(princ)
)
Code has been updated based on BlackBox's comments. Now it is not using unnecessary ActiveX function calls.
Recent Comments