Author Topic: Export each Layout and Modelspace to new Drawing  (Read 1225 times)

0 Members and 1 Guest are viewing this topic.

Offline Vario

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
Export each Layout and Modelspace to new Drawing
« on: February 03, 2012, 07:34:24 AM »
Hi!
I wanna do the following things.
Drawing with X Layouts.

Export each Layout to a new Drawing with Modelspace.
How did i try it:
Iterrate all Layouts in a loop.
Delete all except Modelspace and one Layout. Remember the Layout and save the current database to a new File.
so now repeat this process for the rest of the Layouts.

the layouts are exported, but if i open a drawing, i have to recover it except the drawing with the layout which was on the first position of the original drawing.
so my question, where is the difference between the drawings?
is there a special property in a layout if its the first in a drawing?


Offline frits432

  • Newbie
  • *
  • Posts: 15
  • Karma: +1/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Export each Layout and Modelspace to new Drawing
« Reply #1 on: February 03, 2012, 05:12:09 PM »
Doesn't contribute to any award and not the best way to do it, but it might get you get started

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

using System.Collections.Generic;
using System.Linq;
using System.IO;


// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AcadExport.MyCommands))]

namespace AcadExport
{
    public class MyCommands
    {
        [CommandMethod("ExportLayout", CommandFlags.Modal | CommandFlags.Session)]
        public void ExportLayout() // This method can have any name
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            //if (doc.Name.Length < 1)
            //{
            //    ed.WriteMessage("\nDrawing not saved yet");
            //    return;
            //}
            //string docPath = Path.GetDirectoryName(doc.Name);
            //string docFilename = Path.GetFileNameWithoutExtension(doc.Name);

            foreach (string lytName in GetLayoutNames(db))
            {
                CopyLayout(doc.Name, lytName);
            }
            Application.DocumentManager.MdiActiveDocument=doc;

        }

 


        public List<string> GetLayoutNames(Database db)
        {
            var layoutNames = new List<string>();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary lytDct = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry lytDe in lytDct)
                {
                    if (lytDe.Key != "Model")
                    {
                        //Return the names of Non Empty Layouts only
                        Layout lyt = (Layout)tr.GetObject(lytDe.Value, OpenMode.ForRead);
                        BlockTableRecord ltr = (BlockTableRecord)tr.GetObject(lyt.BlockTableRecordId, OpenMode.ForRead);
                        if (ltr.Cast<ObjectId>().Count() > 1)
                            layoutNames.Add(lyt.LayoutName);
                    }
                }
                tr.Commit();
            }
            return layoutNames;
        }


        public void CopyLayout(string docPathname, string layoutName)
        {
            string tmpPathname = Path.Combine(Path.GetTempPath(), Path.GetTempFileName() + ".dwg");
            string expDocPathname = Path.Combine(
            Path.GetDirectoryName(docPathname),
            Path.GetFileNameWithoutExtension(docPathname) +
            "_" + layoutName + ".dwg");

            File.Copy(docPathname, tmpPathname, true);

            Document doc = Application.DocumentManager.Open(tmpPathname, false);
            Application.DocumentManager.MdiActiveDocument = doc;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (doc.LockDocument())
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    LayoutManager.Current.CurrentLayout = layoutName;
                    DBDictionary lytDct = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
                    foreach (DBDictionaryEntry dictEntry in lytDct)
                    {
                        if (dictEntry.Key != "Model" && dictEntry.Key != layoutName)
                        {
                            Layout lyt = (Layout)tr.GetObject(dictEntry.Value, OpenMode.ForWrite);
                            //if (!lyt.ModelType)
                            lyt.Erase();
                        }
                    }
                    tr.Commit();
                    ed.Regen();
                }

                db.SaveAs(expDocPathname,true, DwgVersion.Newest,db.SecurityParameters);
            }
            //File.Delete(tmpPathname);
        }

    }
}

Frits

Offline Vario

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
Re: Export each Layout and Modelspace to new Drawing
« Reply #2 on: February 06, 2012, 07:56:38 AM »
hi!
thank you for your reply and ideas, maybe i have to say a little bit more.
i want to include the functionality into a summary of different conversions. so i dont know it its possible to open drawings during an other active command.
my first idea was to iterrate through the layouts delete each except de modelspace and one layout and then saveas. but this is not possible, because the transaction is still opened and then i cannot save the database.
 
here is my last try, but it works not properly enough. the layouts are exported thats fine, but if i open the drawings i have errors in it and i need a recover.
this errors are in each drawing except the one with the first layout from the original.
so the first layouts is exported correctly, the drawing has no errors and works fine, but the rest needs a recover...

the next questions is, if it is good to use the Wblock Function to get a temporary Database. i'll loose the template settings from the original or?
maybe anybody has another good idea.

[UPDATE] i got it that the files are opened correctly without errors, but i still have 2 layouts in each new drawing. the one which should be there and a new layout named Layout1 with the content of the original layout with tabindex 1. i deleted it in the code, but it remains in the drawing ???
should i set the taborder property of the layout which should remain in the drawing to 1?

Code: [Select]
public void ExportLayouts(Document acDwg)
      DocumentLock acDocLock = null;
            int i = 0;
            Database acDB = null;
            Transaction acTrans = null;
            BlockTable acBT = null;
            BlockTableRecord acBTR = null;
            SymbolTableEnumerator acBTRE = null;
            Layout acLayout = null;
            Layout acLayoutToExport = null;
            List<String> liExportedLayouts = new List<string>();
            FileInfo fiDrawing = new FileInfo(mydwg.Name);
            try
            {
                //acDwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
               
                acDocLock = acDwg.LockDocument();
                acDB = HostApplicationServices.WorkingDatabase;

                acBT = (BlockTable)(acDB.BlockTableId.GetObject(OpenMode.ForWrite));
                acBTRE = acBT.GetEnumerator();

                while (acBTRE.MoveNext())
                {
                    if (acBTRE.Current.IsErased == false)
                    {
                        acBTR = (BlockTableRecord)(acBTRE.Current.GetObject(OpenMode.ForWrite));
                        if (acBTR.IsLayout)
                        {
                            acLayout = (Layout)(acBTR.LayoutId.GetObject(OpenMode.ForWrite));
                            if (acLayout.ModelType == false)
                            {
                                i += 1;
                            }
                        }
                    }
                }
                LayoutManager.Current.CurrentLayout = "Model";

                for (int j = 1; j <= i; j++)
                {
                    Database acTmpDatabase = new Database(true, false);
                    acLayoutToExport = null;

                    acTmpDatabase = acDB.Wblock();
                    acTrans = acTmpDatabase.TransactionManager.StartTransaction();

                    using (acTrans)
                    {
                        acBT = (BlockTable)(acTrans.GetObject(acTmpDatabase.BlockTableId, OpenMode.ForRead));
                        acBTRE = acBT.GetEnumerator();

                        while (acBTRE.MoveNext())
                        {
                            if (acBTRE.Current.IsErased == false)
                            {
                                acBTR = (BlockTableRecord)(acTrans.GetObject(acBTRE.Current, OpenMode.ForRead));
                                if (acBTR.IsLayout)
                                {
                                    acLayout = (Layout)(acTrans.GetObject(acBTR.LayoutId, OpenMode.ForWrite));
                                    if (acLayout.ModelType == false)
                                    {
                                        if (acLayoutToExport == null)
                                        {
                                            if (liExportedLayouts.Contains(acLayout.LayoutName) == false)
                                            {
                                                acLayoutToExport = acLayout;
                                                liExportedLayouts.Add(acLayout.LayoutName);
                                                acLayout.DowngradeOpen();
                                                continue;
                                            }
                                            else
                                                acLayout.Erase();
                                        }
                                        else
                                            acLayout.Erase();
                                    }
                                }
                            }
                        }
                        acTrans.Commit();
                    }
                    acTmpDatabase.CloseInput(true);

                    if (acLayoutToExport != null)
                    {
                        if (File.Exists(fiDrawing.Directory.FullName + "\\" + acLayoutToExport.LayoutName + fiDrawing.Extension) == false)
                            acTmpDatabase.SaveAs(fiDrawing.Directory.FullName + "\\" + acLayoutToExport.LayoutName + fiDrawing.Extension, DwgVersion.AC1024);
                        else
                            acTmpDatabase.SaveAs(fiDrawing.Directory.FullName + "\\" + acLayoutToExport.LayoutName + sTimeStamp + fiDrawing.Extension, DwgVersion.AC1024);
                    }
                    else
                    {
                        acTmpDatabase.SaveAs(fiDrawing.Directory.FullName + "\\" + Path.GetFileNameWithoutExtension(fiDrawing.FullName) + fiDrawing.Extension, DwgVersion.AC1024);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.Stacktrace);
            }
            finally
            {
                if (acDocLock != null)
                {
                    acDocLock.Dispose();
                }
            }
        }
       
« Last Edit: February 06, 2012, 10:01:02 AM by Vario »