- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am writing a C#.NET program that interacts with AutoCAD through the AutoCAD .NET API.
The program changes the objects from one layer to another, if the layer does not exist it creates a new one with some parameters that are passed through a document. To open the document we use the ReadDwgFile method. We open the document in a hidden way, so that it does not run. I have tried to change the transparency of the document that I have active, and it does it without problems, it gives me an error when I do it in a file that I do not have active. So I think the error is there. Is there a way to change the transparency of a layer whose file I have opened with the ReadDwgFile method?
The program gives an error when I want to change the transparency of the layer of a document that I do not have open.
using (Database destinationDatabase = new Database(false, true))
{
destinationDatabase.ReadDwgFile(file.FullName, FileShare.ReadWrite, true, "");
using (Transaction destinationTransaction = destinationDatabase.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)destinationTransaction.GetObject(destinationDatabase.BlockTableId, OpenMode.ForWrite);
BlockTableRecord record = destinationTransaction.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
LayerTable layertable;
layertable = destinationTransaction.GetObject(destinationDatabase.LayerTableId,OpenMode.ForWrite) as LayerTable;
foreach (ObjectId ob in record)
{
Entity eo = (Entity)ob.GetObject(OpenMode.ForWrite);
if (DicLayer.Keys.Contains(eo.Layer))
{
ClassLayer layer = DicLayer[eo.Layer];
if (!layertable.Has(layer.LayerNewName))
{
using (LayerTableRecord layertablerecord = new LayerTableRecord())
{
Type enumType = typeof(LineWeight);
System.Reflection.FieldInfo[] fieldInfos = enumType.GetFields();
Dictionary<string, FieldInfo> lineWeigthDict = new Dictionary<string, FieldInfo>();
foreach (System.Reflection.FieldInfo fieldInfo in fieldInfos)
{
lineWeigthDict.Add(fieldInfo.Name, fieldInfo);
}
LinetypeTable acLinTbl;
acLinTbl = destinationTransaction.GetObject(destinationDatabase.LinetypeTableId,
OpenMode.ForWrite) as LinetypeTable;
// Line Weight
string lineWeigtName = "";
String lwn = layer.LineWeight;
switch (lwn)
{
case "Default":
lineWeigtName = "ByLineWeightDefault";
break;
default:
lineWeigtName = "LineWeight" + layer.LineWeight;
break;
}
LineWeight lineWeight = (LineWeight)lineWeigthDict[lineWeigtName].GetValue(null);
layertablerecord.Color = Color.FromColorIndex(ColorMethod.ByAci, short.Parse(layer.Color.ToString()));
layertablerecord.Name = layer.LayerNewName;
layertablerecord.LineWeight = lineWeight;
layertablerecord.LinetypeObjectId = acLinTbl[layer.LineType];
Byte alpha = (Byte)(255 * (100 - Int32.Parse(layer.Transpaerncy)) / 100);
Transparency transparency = new Transparency(alpha);
layertablerecord.Transparency = transparency;
// Upgrade the Layer table for write
destinationTransaction.GetObject(destinationDatabase.LayerTableId, OpenMode.ForWrite);
// Append the new layer to the Layer table and the transaction
layertable.Add(layertablerecord);
destinationTransaction.AddNewlyCreatedDBObject(layertablerecord, true);
}
eo.Layer = layer.LayerNewName;
}
else
{
eo.Layer = layer.LayerNewName;
}
}
}
destinationTransaction.Commit();
}
destinationDatabase.RetainOriginalThumbnailBitmap = true;
destinationDatabase.SaveAs(file.FullName, true, DwgVersion.Current, null);
}
Solved! Go to Solution.