Clone layout from Template to drawing that is NOT open

Clone layout from Template to drawing that is NOT open

aeshleman
Enthusiast Enthusiast
295 Views
2 Replies
Message 1 of 3

Clone layout from Template to drawing that is NOT open

aeshleman
Enthusiast
Enthusiast

I'm trying to get a method to work that clones a layout from a layout template file and copies it into a file that was just created. I eventually want to incorporate this into a larger app loops through a csv file with columns for the drawing name and the layout to import and creates a drawing file for each name in the list and imports the correct layout but I can't seem to get this bit to work. I CAN get it to copy from a template into an open drawing but I don't want to have to open 14+ drawings if i don't have to. Is it possible to do what I'm trying to do or do I need to open every new drawing. Thanks for any insight, I'm just getting into the AutoCAD's .NET API.

 

 

0 Likes
Accepted solutions (1)
296 Views
2 Replies
Replies (2)
Message 2 of 3

aeshleman
Enthusiast
Enthusiast

After some more research, I was able to get it to work. I'll post the working code in case any body else can use it.

 

0 Likes
Message 3 of 3

aeshleman
Enthusiast
Enthusiast
Accepted solution
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();
                }
            }
        }
    }
}
0 Likes