Message 1 of 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I want to add a new layer A to a side db.
And there are some dbtexts in the side db.
I want to set the dbtext's layers to A.
thanks.
[CommandMethod("W7")]
public void W7()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
using (Database dbInsert = new Database(false, true))
{
string sDwg = @"D:\B.dwg";
dbInsert.ReadDwgFile(sDwg, System.IO.FileShare.ReadWrite, true, null);
Autodesk.AutoCAD.DatabaseServices.TransactionManager sideTm = dbInsert.TransactionManager;
using (Transaction sideTr = sideTm.StartTransaction())
{
ObjectId id = dbInsert.NewLayer("A", 5, "continues", "acadiso.lin", LineWeight.LineWeight015, false, false, false, false, false);
sideTr.Commit();
}
using (Transaction sideTr = sideTm.StartTransaction())
{
BlockTable bt = (BlockTable)sideTm.GetObject(dbInsert.BlockTableId, OpenMode.ForRead, false);
BlockTableRecord modelspace = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);
foreach (ObjectId id in modelspace)
{
try
{
if (id.IsErased) continue;
Entity ent = (Entity)sideTr.GetObject(id, OpenMode.ForWrite, false);
ent.Layer = "B";
ent.DowngradeOpen();
}
catch
{
}
}
sideTr.Commit();
}
dbInsert.RetainOriginalThumbnailBitmap = true;
dbInsert.SaveAs(sDwg, DwgVersion.AC1027);
trans.Commit();
}
}
catch
{
}
}
}
public static ObjectId NewLayer(this Database db, string layerName, short colorIndex, string lineType, string lineTypeFile, LineWeight lineWeight, bool bIsPlot, bool bIsOff, bool bIsFreezed, bool bIsLocked, bool bIsCurrent)
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
colorIndex = (short)(colorIndex % 256);
ObjectId idRet = ObjectId.Null;
try
{
using (Transaction trans = (Transaction)db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = layerName;
ltr.IsOff = bIsOff;
ltr.IsFrozen = bIsFreezed;
ltr.IsLocked = bIsLocked;
ltr.IsPlottable = bIsPlot;
ltr.Color = CadColor.FromColorIndex(CadColors.ColorMethod.ByColor, colorIndex);
ltr.LineWeight = lineWeight;
ltr.LinetypeObjectId = db.LoadLineType(lineType, lineTypeFile);
idRet = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
if (bIsCurrent)
db.Clayer = idRet;
trans.Commit();
}
}
catch (System.Exception ex)
{
}
return idRet;
}
Solved! Go to Solution.