Layers can be assigned a transparency value using the AutoCAD's layer dialog.
Here is sample code to show you how a similar result can be achieved using the AutoCAD .Net API.
But before trying this code, dont forget to set the "TRANSPARENCYDISPLAY" system variable to 1.
[CommandMethod("Test")]
public void TestMethod()
{
SetLayerTransparency("Autodesk", 50);
}
/// sets the layer transparency
// Can range from 0 (opaque) to 90 (almost transparent)
/// returns ObjectId of the layer
private ObjectId SetLayerTransparency
(
string layerName,
Byte layerTransparency
)
{
Document activeDoc
= Application.DocumentManager.MdiActiveDocument;
Database db = activeDoc.Database;
ObjectId layerId = ObjectId.Null;
bool done = false;
using (Transaction tr
= db.TransactionManager.StartTransaction())
{
LayerTable lt
= tr.GetObject
(
db.LayerTableId,
OpenMode.ForRead
) as LayerTable;
if (lt.Has(layerName))
{
layerId = lt[layerName];
LayerTableRecord ltr
= tr.GetObject(
layerId,
OpenMode.ForWrite
) as LayerTableRecord;
// The color is being set here to ensure that
// a regen will consider redrawing all the entities
// belonging to this layer.
ltr.Color = ltr.Color;
Byte alpha = (Byte)
(255 * (100 - layerTransparency) / 100);
Transparency trans = new Transparency(alpha);
ltr.Transparency = trans;
done = true;
}
tr.Commit();
}
if (done)
{
RefreshEntities(layerId);
// (OR)
//activeDoc.Editor.Regen();
}
return layerId;
}
// Marks the entities referencing a
// certain layer as "Modified"
private void RefreshEntities(ObjectId layerId)
{
Document activeDoc =
Application.DocumentManager.MdiActiveDocument;
Database db = activeDoc.Database;
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject
(
db.BlockTableId,
OpenMode.ForRead
) as BlockTable;
BlockTableRecord btr =
tr.GetObject
(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead
) as BlockTableRecord;
foreach (ObjectId entityId in btr)
{
Entity ent
= tr.GetObject
(
entityId,
OpenMode.ForRead
) as Entity;
if (ent.LayerId.Equals(layerId))
{
ent.UpgradeOpen();
ent.RecordGraphicsModified(true);
}
}
tr.Commit();
}