set transparency to layer - other result then input

set transparency to layer - other result then input

jan_tappenbeck
Collaborator Collaborator
872 Views
2 Replies
Message 1 of 3

set transparency to layer - other result then input

jan_tappenbeck
Collaborator
Collaborator

hi !

 

i want to set value of transparency to layer.

 

my function is

 

 Public Function ChangeTransOfLayer(ByVal LayerFilter As String, _
                                       ByVal LyTransValue As Integer, _
                                                                                   Optional ByRef Log As String = "",
                                          Optional ByRef Execute As Boolean = True) As Boolean
        'http://ma22-wiki-001/eblwiki/index.php?title=Acad_(Klasse_von_EBL.Service)#ChangeTransOfLayer
        AcReInit()

        If LayerFilter.Length = 0 Then
            Log += "**** Fehler - kein Layer definiert" & vbCrLf
            Return False
        End If

        ' Todo - wenn einmal ein Filter in der Layerliste verfügbar ist
        Dim Layers As List(Of String) = GetLayerList(LyFilter:=LayerFilter, IgnoreXrefLayer:=True)

        Try
            Using tr As Transaction = _Database.TransactionManager.StartTransaction()

                '' Open the Layer table for read
                Dim acLyrTbl As LayerTable
                acLyrTbl = tr.GetObject(_Database.LayerTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)

                If Execute = False Then _Editor.WriteMessage("** Befehlssimulation **" & vbCrLf)

                ' Todo - wenn einmal ein Filter in der Layerliste verfügbar ist
                ''For Each sLayerName As String In Layers
                Dim sLayerName As String = LayerFilter
                Try
                    If Execute = True Then
                        If acLyrTbl.Has(sLayerName) = True Then
                            '' Open the layer if it already exists for write
                            Dim acLyrTblRec As LayerTableRecord = tr.GetObject(acLyrTbl(sLayerName), _
                                                                                    Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)

                            '' Set the color of the layer
                            acLyrTblRec.Transparency = New Autodesk.AutoCAD.Colors.Transparency(CByte(LyTransValue))

                        End If

                    Else
                        _Editor.WriteMessage("Layer " & sLayerName & " - Farbe geändert in " & LyTransValue.ToString & " ..." & vbCrLf)
                    End If
                Catch ex As Exception
                    Log += "unerwarteter Fehler in EBL.Service > Acad > ChangeTransOfLayer - Loop" & vbCrLf & _
                        "Color:= " & LyTransValue.ToString & vbCrLf & _
                        "LayerFilter:= " & LayerFilter & vbCrLf & _
                        "Log:= " & Log & vbCrLf & _
                                "Execute:= " & Execute.ToString & vbCrLf & _
                    ex.ToString()

                End Try

                ''                Next
                tr.Commit()

            End Using
        Catch ex As Exception
            Log += "unerwarteter Fehler in EBL.Service > Acad > ChangeTransOfLayer - Loop" & vbCrLf & _
                "Color:= " & LyTransValue.ToString & vbCrLf & _
                "Layername:= " & LayerFilter & vbCrLf & _
                "Log:= " & Log & vbCrLf & _
                        "Execute:= " & Execute.ToString & vbCrLf & _
            ex.ToString()

            Return False
        End Try

        Return True
    End Function

when i run the function by LyTransValue=80 the transparency in layer-Manager will be 68 !!!

 

why?? could someone help to me?

 

regards Jan

 

0 Likes
Accepted solutions (1)
873 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

The argument for the Transparency ctor is a byte (in the range [0-255]) representing the Apha value in the ARGB color encoding where 255 means 100% transparency.

 

So if you want to set 80% transprency you have to compute 255 * 20% as a byte value:

 

acLyrTblRec.Transparency = New Autodesk.AutoCAD.Colors.Transparency(CByte(255 * (100 - LyTransValue) / 100.0)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

_gile
Consultant
Consultant
Accepted solution

Closer to the way AutoCAD works:

byte alpha = (byte)(255 * (100 - transparencyValue) / 100);

I'm not very cumfortable with VB, but I think the VB equivalent should be:

Dim alpha As Byte = CByte(255 * (100 - transprencyValue) \ 100)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub