<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Get Layer Transparency As a Readable Value in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9190946#M20674</link>
    <description>&lt;P&gt;Thanks Ajilal. By the way, Drawing Purge is an amazing program! it's saved me about 500,000 hours of busy work over the years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do have a slight issue with the solution you posted and could use some more help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have implemented it as such:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;      byte alphaval = layerTableRecord.Transparency.Alpha;
      int trans = transvalue(alphaval);
      layer.Transparency = trans.ToString();&lt;/PRE&gt;&lt;P&gt;I had to throw the byte declaration in front of alphaval otherwise VS was telling me it doesn't exist in the context. The error I am now getting occurs on that line. I receive an exception of eInvalidKey when I try to run this code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried the following and I am now getting results, it's just that the numbers are wrong:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;string trans = layerTableRecord.Transparency.ToString();
string trimmedTransparency = trans.TrimStart('(').TrimEnd(')');
int intSetting = Int32.Parse(trimmedTransparency);
byte alpha = (byte)(255 * (100 - intSetting) / 100);
int readableTrans = transvalue(alpha);
layer.Transparency = readableTrans.ToString();&lt;/PRE&gt;&lt;P&gt;For example, in AutoCAD Layer1 has transparency of 12, but this code returns a value of 51.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Dec 2019 13:46:39 GMT</pubDate>
    <dc:creator>richardpangburn</dc:creator>
    <dc:date>2019-12-09T13:46:39Z</dc:date>
    <item>
      <title>Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9188222#M20672</link>
      <description>&lt;P&gt;I am trying to get the transparency of a layer so I can see it's value (0-90) but I'm having a hard time getting that to come out right.&lt;/P&gt;&lt;PRE&gt;string trans = layerTableRecord.Transparency.ToString();
string trimmedTransparency = trans.TrimStart('(').TrimEnd(')');
int intSetting = Int32.Parse(trimmedTransparency);
byte alpha = (byte)(255 * (100 - intSetting) / 100);&lt;BR /&gt;&lt;SPAN&gt;layer&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;Transparency&amp;nbsp;&lt;SPAN&gt;=&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;intSetting&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;ToString&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;P&gt;layerTableRecord.Transparency.ToString(); returns an 8 digit number inside of parentheses that I trim and convert into an int. I then pass that integer into the line that creates 'alpha' and gives me a value between 0-255 I believe.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In this case &lt;SPAN&gt;layer&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;Transparency&amp;nbsp;&lt;SPAN&gt;=&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;intSetting&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;ToString&lt;/SPAN&gt;(); returns the 8 digit number.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I don't want it to return the 8 digit number I want it to return the same value which would be seen by a user looking at their layer manager in AutoCAD, a number between 0 and 90.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;layer.Transparency is a string property on a my LayerModel. It is bound to my UI so I would like to set that string as a value a drafter will implicitly understand.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Dec 2019 22:23:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9188222#M20672</guid>
      <dc:creator>richardpangburn</dc:creator>
      <dc:date>2019-12-06T22:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9189290#M20673</link>
      <description>&lt;P&gt;convert back the Alpha value&lt;/P&gt;
&lt;P&gt;use the Alpha value of Transparency&lt;BR /&gt;alphaval = layerTableRecord.Transparency.Alpha&lt;BR /&gt;int trans =&amp;nbsp; transvalue(alphaval)&lt;/P&gt;
&lt;PRE&gt; private int transvalue(byte trans) {           
     double tr = byte.Parse(trans.ToString());
     return System.Math.Abs( Convert.ToInt16 ((tr / 255) * 100 - 100));
  }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Dec 2019 06:09:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9189290#M20673</guid>
      <dc:creator>Ajilal.Vijayan</dc:creator>
      <dc:date>2019-12-08T06:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9190946#M20674</link>
      <description>&lt;P&gt;Thanks Ajilal. By the way, Drawing Purge is an amazing program! it's saved me about 500,000 hours of busy work over the years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do have a slight issue with the solution you posted and could use some more help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have implemented it as such:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;      byte alphaval = layerTableRecord.Transparency.Alpha;
      int trans = transvalue(alphaval);
      layer.Transparency = trans.ToString();&lt;/PRE&gt;&lt;P&gt;I had to throw the byte declaration in front of alphaval otherwise VS was telling me it doesn't exist in the context. The error I am now getting occurs on that line. I receive an exception of eInvalidKey when I try to run this code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried the following and I am now getting results, it's just that the numbers are wrong:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;string trans = layerTableRecord.Transparency.ToString();
string trimmedTransparency = trans.TrimStart('(').TrimEnd(')');
int intSetting = Int32.Parse(trimmedTransparency);
byte alpha = (byte)(255 * (100 - intSetting) / 100);
int readableTrans = transvalue(alpha);
layer.Transparency = readableTrans.ToString();&lt;/PRE&gt;&lt;P&gt;For example, in AutoCAD Layer1 has transparency of 12, but this code returns a value of 51.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 13:46:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9190946#M20674</guid>
      <dc:creator>richardpangburn</dc:creator>
      <dc:date>2019-12-09T13:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191197#M20675</link>
      <description>&lt;P&gt;no need to convert transparency to string.&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;string trans = layerTableRecord.Transparency.ToString();&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;you could call the function with alpha value directly.&lt;/P&gt;
&lt;PRE&gt;int readableTrans = transvalue(layerTableRecord.Transparency.Alpha);&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#3366FF"&gt;&lt;EM&gt;&amp;nbsp;For example, in AutoCAD Layer1 has transparency of 12, but this code returns a value of 51.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#3366FF"&gt;&lt;EM&gt;I receive an exception of eInvalidKey when I try to run this code.&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I will check these and update tomorrow.&lt;/P&gt;
&lt;P&gt;-------&lt;/P&gt;
&lt;P&gt;Glad to hear that Drawing Purge helped you !! &lt;span class="lia-unicode-emoji" title=":smiling_face_with_heart_eyes:"&gt;😍&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 15:00:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191197#M20675</guid>
      <dc:creator>Ajilal.Vijayan</dc:creator>
      <dc:date>2019-12-09T15:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191240#M20676</link>
      <description>&lt;P&gt;Thanks Ajilal. I tried passing the layerTableRecord.Transparency.Alpha as you suggested but got another eInvalidKey error. From my previous looks into getting transparency it seemed as though the line:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;byte alpha = (byte)(255 * (100 - intSetting) / 100);&lt;/PRE&gt;&lt;P&gt;Was necessary to get a usable value out of AutoCADs transparency, but I could be off somewhere. I was hoping Transparency would be as easy as setting a property between 0-90 but no such luck! Thanks for taking the time to look into it.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 15:22:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191240#M20676</guid>
      <dc:creator>richardpangburn</dc:creator>
      <dc:date>2019-12-09T15:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191345#M20677</link>
      <description>&lt;P&gt;I wanted to add some additional information. The following code absolutely works to &lt;STRONG&gt;SET&lt;/STRONG&gt; the layer transparency to 50:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;int desiredTransparency = 50;
byte alpha = (byte)(255 * (100 - desiredTransparency) / 100);
//Need to find out how to get desiredTransparency from the getting part of the code instead of setting it myself.
Transparency transparency = new Transparency(alpha);
layerTableRecord.Transparency = transparency;&lt;/PRE&gt;&lt;P&gt;However I need to actually read the layer transparency from a drawing to get my desiredTransparency value and not the weird 6 digit byte values, or the 3 digit values that are being returned with my original post.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 15:59:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191345#M20677</guid>
      <dc:creator>richardpangburn</dc:creator>
      <dc:date>2019-12-09T15:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191479#M20678</link>
      <description>&lt;P&gt;Another update. I have found a way to get the transparency value as it's displayed in AutoCAD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if(layerTableRecord.Transparency.Alpha != 0)
{
  int percentage = (int)(((255 - layerTableRecord.Transparency.Alpha) * 100) / 255);
  layer.Transparency = percentage.ToString();
}
else
{
  layer.Transparency = "0";
}&lt;/PRE&gt;&lt;P&gt;Of course this has unleashed a new problem. If the layer's transparency is set to 0 in AutoCAD, then accessing it will throw an exception. I can't do my if check because it throws a runtime exception. Something about layerTableRecord.Transparency.Alpha being zero makes it unreadable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can read any other value between 1-90 but if it's zero I crash out. So i just need help now getting around this issue. Basically if that setting is actually zero I don't want to do anything but say that layer.Transparency = "0"; in this situation.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 17:00:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191479#M20678</guid>
      <dc:creator>richardpangburn</dc:creator>
      <dc:date>2019-12-09T17:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191654#M20679</link>
      <description>&lt;P&gt;OK. I think i have found a solution to my problem in the previous post (sorry for replying to myself 300 times to anyone who might find this annoying). Here is the method to get the layer transparency setting as a readable value:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if(layerTableRecord.Transparency.IsByAlpha)
{
 int percentage = (int)(((255 - layerTableRecord.Transparency.Alpha) * 
 100) / 255);
 layer.Transparency = percentage.ToString();
}
else
{
 layer.Transparency = "0";
}&lt;/PRE&gt;&lt;P&gt;Of course this code depends on IsByAlpha being true. However, since I have never heard of any other way to set the transparency of a layer, and IsByAlpha is working on every layer in my drawing, I am prepared to accept this as good enough.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll wait a while to see if anyone else has input but if not I'll mark this as the solution later.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much Ajilal for getting me on the right track.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2019 18:28:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9191654#M20679</guid>
      <dc:creator>richardpangburn</dc:creator>
      <dc:date>2019-12-09T18:28:57Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9192471#M20680</link>
      <description>&lt;P&gt;It seems that checking 'IsByAlpha' is the correct way to avoid the 'eInvalidKey' error.&lt;/P&gt;
&lt;P&gt;So I think your solution seems working. &lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Not sure whether the below affects your program.&lt;/P&gt;
&lt;P&gt;You may need to check the MdiActiveDocument.Database.Cetransparency.IsByLayer value&lt;/P&gt;
&lt;P&gt;If this transparency is not by layer, then this will override the layer transparency value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 06:02:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9192471#M20680</guid>
      <dc:creator>Ajilal.Vijayan</dc:creator>
      <dc:date>2019-12-10T06:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9739182#M20681</link>
      <description>&lt;P&gt;It seems to be everywhere so I really don't know the origin of this 255 myth, but by my calculations 250 is the magic number to (exactly) match the AutoCAD layer dialog.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have not tested this on objects, but I'd be surprised if it's any different. I am sure its probably something like 251-255 carrying special meanings on objects; IE, byblock/bylayer&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 06:13:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9739182#M20681</guid>
      <dc:creator>jhoward-HOB</dc:creator>
      <dc:date>2020-09-10T06:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9739403#M20682</link>
      <description>&lt;P&gt;Not quite sure why I couldn't edit my last one, to fix it.&amp;nbsp; The 250 did give me a nice clean export from the native layer transparency to my own dialog that lined up without truncating/rounding.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, it doesn't perfectly convert back for lower values, and more importantly, the value can be set to 255 and performing that kind of math would create a negative number...&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 08:03:07 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9739403#M20682</guid>
      <dc:creator>jhoward-HOB</dc:creator>
      <dc:date>2020-09-10T08:03:07Z</dc:date>
    </item>
    <item>
      <title>Re: Get Layer Transparency As a Readable Value</title>
      <link>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9818735#M20683</link>
      <description>&lt;P&gt;I really couldn't stand an inability to get consistent bidirectional conversions and decided to just map them all into a json file. It wouldn't let me attach json files so I put the txt extension on it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Notes:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;This usage code assumes the Transparencies.json file was marked as an embedded resource.&lt;/LI&gt;&lt;LI&gt;This was used for layers and you may have to make yourself constants that represent byblock/bylayer for object transparencies.&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;UnPack&amp;lt;&amp;gt; extension method in the code below is a pretty much just doing&amp;nbsp;JsonConvert.DeserializeObject&amp;lt;T&amp;gt;(str);&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;The Key's in the attached are the visual indicators and the values are the byte codes that represent them.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static class Resources
{
    private static Dictionary&amp;lt;int, byte&amp;gt; _transparencies = null;
    private static Dictionary&amp;lt;int, byte&amp;gt; Transparencies { get { return readTransparencies(); } }
    private static Dictionary&amp;lt;int, byte&amp;gt; readTransparencies()
    {
        if (_transparencies == null)
        {
            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            var resourceName = assembly.FullName.Split(',')[0] + "." + "Transparencies.json";
            using (var stream = assembly.GetManifestResourceStream(resourceName))
            using (var reader = new System.IO.StreamReader(stream))
            { _transparencies = reader.ReadToEnd().UnPack&amp;lt;Dictionary&amp;lt;int, byte&amp;gt;&amp;gt;(); }
        }
        return _transparencies;
    }

    public static int getTransparencyVisualIndex(Autodesk.AutoCAD.DatabaseServices.LayerTableRecord lay)
    {
        if (lay.Transparency.IsInvalid == false &amp;amp;&amp;amp; lay.Transparency.IsByAlpha == true)
        {
            int smallestDiff = byte.MaxValue, foundValue = 0;
            foreach (byte key in Transparencies.Keys)
            {
                if (Math.Abs(Transparencies[key] - lay.Transparency.Alpha) &amp;lt; smallestDiff)
                {
                    smallestDiff = Math.Abs(Transparencies[key] - lay.Transparency.Alpha);
                    foundValue = key;
                }
            }
            return foundValue;
        }
        else
            return 0;
    }
    public static Autodesk.AutoCAD.Colors.Transparency getTransparencyValue(int index)
    {
        if (Transparencies.ContainsKey(index) == true)
            return new Autodesk.AutoCAD.Colors.Transparency(Transparencies[index]);
        else
            return new Autodesk.AutoCAD.Colors.Transparency(byte.MaxValue);
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Oct 2020 17:43:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/get-layer-transparency-as-a-readable-value/m-p/9818735#M20683</guid>
      <dc:creator>jhoward-HOB</dc:creator>
      <dc:date>2020-10-22T17:43:29Z</dc:date>
    </item>
  </channel>
</rss>

