using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[assembly: CommandClass(typeof(CADProject.DWGUtilities))]
namespace CADProject
{
public class DWGUtilities
{
string drawingTemplatePath = @"C\Path\File.dwt";
string layoutTemplatePath = @"C\Path\File.dwt";
string newDrawingPath = @"C\Path\File.dwg";
string layoutName = "Plan Sheet";
[CommandMethod("CreateDWG")]
public void CreateDWG()
{
try
{
// Create and save new drawing based on specified drawing template
using (Database newDb = new Database(false, true))
{
newDb.ReadDwgFile(drawingTemplatePath, FileShare.Read, true, "");
// Calls method to clone layout from template
CloneLayout(newDb);
// Saves new drawing
newDb.SaveAs(newDrawingPath, DwgVersion.Current);
}
}
catch (System.Exception ex)
{
Application.ShowAlertDialog($"An error occurred: {ex.Message}");
}
}
// Method called to clone the layout tab from the layout template
private void CloneLayout(Database newDb)
{
// Create database for layout template drawing
using (Database layoutDb = new Database(false, true))
{
layoutDb.ReadDwgFile(layoutTemplatePath, FileOpenMode.OpenForReadAndAllShare, true, "");
// Create a transaction for the external drawing
using (Transaction acTransLayout = layoutDb.TransactionManager.StartTransaction())
{
// Get the layouts dictionary
DBDictionary layoutsEx =
acTransLayout.GetObject(layoutDb.LayoutDictionaryId,
OpenMode.ForRead) as DBDictionary;
// Check to see if the layout exists in the external drawing
if (layoutsEx.Contains(layoutName) == true)
{
// Get the layout and block objects from the external drawing
Layout layEx =
layoutsEx.GetAt(layoutName).GetObject(OpenMode.ForRead) as Layout;
BlockTableRecord blkBlkRecEx =
acTransLayout.GetObject(layEx.BlockTableRecordId,
OpenMode.ForRead) as BlockTableRecord;
// Get the objects from the block associated with the layout
ObjectIdCollection idCol = new ObjectIdCollection();
foreach (ObjectId id in blkBlkRecEx)
{
idCol.Add(id);
}
// Create a transaction for the current drawing
using (Transaction acTrans = newDb.TransactionManager.StartTransaction())
{
// Get the block table and create a new block
// then copy the objects between drawings
BlockTable blkTbl =
acTrans.GetObject(newDb.BlockTableId,
OpenMode.ForWrite) as BlockTable;
using (BlockTableRecord blkBlkRec = new BlockTableRecord())
{
int layoutCount = layoutsEx.Count - 1;
blkBlkRec.Name = "*Paper_Space" + layoutCount.ToString();
blkTbl.Add(blkBlkRec);
acTrans.AddNewlyCreatedDBObject(blkBlkRec, true);
layoutDb.WblockCloneObjects(idCol,
blkBlkRec.ObjectId,
new IdMapping(),
DuplicateRecordCloning.Ignore,
false);
// Create a new layout and then copy properties between drawings
DBDictionary layouts =
acTrans.GetObject(newDb.LayoutDictionaryId,
OpenMode.ForWrite) as DBDictionary;
using (Layout lay = new Layout())
{
lay.LayoutName = layoutName;
lay.AddToLayoutDictionary(newDb, blkBlkRec.ObjectId);
acTrans.AddNewlyCreatedDBObject(lay, true);
lay.CopyFrom(layEx);
DBDictionary plSets =
acTrans.GetObject(newDb.PlotSettingsDictionaryId,
OpenMode.ForRead) as DBDictionary;
// Check to see if a named page setup was assigned to the layout,
// if so then copy the page setup settings
if (lay.PlotSettingsName != "")
{
// Check to see if the page setup exists
if (plSets.Contains(lay.PlotSettingsName) == false)
{
acTrans.GetObject(newDb.PlotSettingsDictionaryId, OpenMode.ForWrite);
using (PlotSettings plSet = new PlotSettings(lay.ModelType))
{
plSet.PlotSettingsName = lay.PlotSettingsName;
plSet.AddToPlotSettingsDictionary(newDb);
acTrans.AddNewlyCreatedDBObject(plSet, true);
DBDictionary plSetsEx =
acTransLayout.GetObject(layoutDb.PlotSettingsDictionaryId,
OpenMode.ForRead) as DBDictionary;
PlotSettings plSetEx =
plSetsEx.GetAt(lay.PlotSettingsName).GetObject(
OpenMode.ForRead) as PlotSettings;
plSet.CopyFrom(plSetEx);
}
}
}
}
}
// Save the changes made
acTrans.Commit();
}
}
else
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nLayout '" + layoutName + "' could not be fouond. '" );
}
// Discard the changes made to the external drawing file
acTransLayout.Abort();
}
}
}
}
}