- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Good day Everyone. I'm new to AutoCAD C# Programming asking for your kind assistance.
What I'm trying to do is
1. Access Drawing Database without Opening it in the Editor ( Template Drawing )
2. Create Objects Inside It
3. Assign Layer Property to the Created Objects
4. Save as New Drawing ( Output Drawing )
I followed this Solution and It works, I can already Access the drawing database and Create Objects Inside it and then save into a different drawing.
But when I try to assign a layer property in the new object I get eKeyNotFound Error even though the Template drawing that I access has that layer.
I also followed this Link but still no Luck.
https://forums.autodesk.com/t5/net/how-change-layer-of-specific-entity/td-p/8577610
Here is my Source Code.
[CommandMethod("CHANGENEW")]
public static void CreateText()
{
//Get the template plant database
Database acCurDb = new Database(false, true);
string dwgFlpath = @"D:\Users\johnrey.d.malulan\Downloads\Hotaka_New_Type_Test.dwg";
//Set the output Drawing Directory
Document acDoc = Application.DocumentManager.MdiActiveDocument;
string strDWGName = acDoc.Name;
strDWGName = @"D:\Users\johnrey.d.malulan\Downloads\Output.dwg";
acCurDb.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
var layerName = "Character";
// check if the layer already exists
var lt = (LayerTable)acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead);
if (!lt.Has(layerName))
{
// if not create it
var layer = new LayerTableRecord()
{
Name = layerName,
Color = Color.FromRgb(200, 30, 80)
};
lt.UpgradeOpen();
lt.Add(layer);
acTrans.AddNewlyCreatedDBObject(layer, true);
Application.ShowAlertDialog("Layer Created");
}
// Create a single-line text object
DBText acText = new DBText();
//acText.SetDatabaseDefaults();
acText.Position = new Point3d(2, 2, 0);
acText.Layer = layerName;
acText.Height = 500;
acText.TextString = "Hello, World.";
acBlkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
// Save the changes and dispose of the transaction
acTrans.Commit();
}
acCurDb.SaveAs(strDWGName, DwgVersion.Current);
}
Please help me 😞
Solved! Go to Solution.

