Author Topic: Create and copy Layouts?  (Read 1810 times)

0 Members and 1 Guest are viewing this topic.

Offline rom1

  • Visual Basic
  • *
  • Posts: 21
  • Karma: +3/-0
  • Gender: Male
    • prefered language: VB
    • Prog expertise: Good
    • View Profile
Create and copy Layouts?
« on: November 23, 2010, 07:48:26 AM »

Hi,

If you had some examples in order to create a new layout and insert some elements into or in order to copy an existing layout, that would be great since i have some difficulties in order to find good examples.

Thanks!

target audience:{advanced}

Offline (gile)

  • C#
  • *
  • Posts: 87
  • Karma: +8/-0
  • Gender: Male
    • prefered language: F
    • Prog expertise: Good
    • View Profile
Re: Create and copy Layouts?
« Reply #1 on: November 23, 2010, 08:59:26 AM »
Hi,

Here's a little example to ceate a new layout with a viewport (maximum size according to paper margins).

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace LayoutAndViewportSample
{
    public class CreateVpCommand
    {
        [CommandMethod("Test")]
        public void Test()
        {
            Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
           
            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    DBDictionary layouts = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForWrite);

                    // Get a layout name from the user
                    PromptStringOptions pso = new PromptStringOptions("\nEnter the layout name: ");
                    pso.AllowSpaces = true;
                    string name;
                    do
                    {
                        PromptResult pr = ed.GetString(pso);
                        if (pr.Status != PromptStatus.OK)
                            return;
                        name = pr.StringResult;
                    }
                    while (layouts.Contains(name));

                    // Create and activate the new layout
                    ObjectId layoutId = LayoutManager.Current.CreateLayout(name);
                    LayoutManager.Current.CurrentLayout = name;

                    // Calculate the maximum viewport size
                    Layout lay = (Layout)tr.GetObject(layoutId, OpenMode.ForWrite);
                    Point2d
                        minPt = lay.PlotPaperMargins.MinPoint,
                        maxPt = lay.PlotPaperMargins.MaxPoint,
                        pSize = lay.PlotPaperSize;
                    double width, height;
                    if ((int)lay.PlotRotation % 2 == 0)
                    {
                        width = pSize.X - maxPt.X - minPt.X;
                        height = pSize.Y - maxPt.Y - minPt.Y;
                    }
                    else
                    {
                        width = pSize.Y - maxPt.Y - minPt.Y;
                        height = pSize.X - maxPt.X - minPt.X;
                    }

                    // add the viewport to the layout BlockTableRecord
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(lay.BlockTableRecordId, OpenMode.ForWrite);
                    Viewport vp = new Viewport();
                    vp.Width = width;
                    vp.Height = height;
                    vp.CenterPoint = new Point3d(width / 2.0, height / 2.0, 0.0);
                    btr.AppendEntity(vp);
                    tr.AddNewlyCreatedDBObject(vp, true);
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: " + ex.Message);
            }
        }
    }
}

target audience:{beginner}
« Last Edit: November 23, 2010, 09:22:28 AM by (gile) »

Offline Jeff H

  • Newbie
  • *
  • Posts: 10
  • Karma: +3/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Create and copy Layouts?
« Reply #2 on: November 24, 2010, 03:00:09 PM »
Here is a simple VB & C# example of copying a layout from a external drawing,
There is no error handling or checking if the drawing, layout exist to keep it short.

This is pretty much the same as copying the PlotSettings or the "Page Setup"
You must have a drawing and layout named the same or change it.

A simple App example would let the user select a location or drawing where they setup all the different "Page Setups" from a context menu or something similiar. Then you could fill a listbox or whatever with the names on a palette, and on the click event bring in the copy the layout.
This example is hard-coded for simplicity


C#

Code: [Select]
[CommandMethod("CopyExternalLayout")]
        public static void CopyExternalLayout()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Database stdDb = new Database();
            stdDb.ReadDwgFile(@"C:\Test\PageSetup.dwg", FileOpenMode.OpenForReadAndAllShare, false,"");

            using (Transaction trx = db.TransactionManager.StartTransaction())
            using (Transaction stdTrx = stdDb.TransactionManager.StartTransaction())
            {
                DBDictionary stdLayoutDictionary = stdDb.LayoutDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;
                Layout stdLayout = stdLayoutDictionary.GetAt("TestPageSetUp").GetObject(OpenMode.ForRead) as Layout;
                BlockTableRecord paperSpace = SymbolUtilityServices.GetBlockPaperSpaceId(db).GetObject(OpenMode.ForRead) as BlockTableRecord;
                Layout layout = paperSpace.LayoutId.GetObject(OpenMode.ForWrite) as Layout;
                layout.CopyFrom(stdLayout);
                trx.Commit();
            }     
       
       
        }


VB

Code: [Select]
<CommandMethod("CopyExternalLayout")> _
    Public Shared Sub CopyExternalLayout()

        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim stdDb As Database = New Database()
        stdDb.ReadDwgFile("C:\Test\PageSetup.dwg", FileOpenMode.OpenForReadAndAllShare, False, "")

        Using trx As Transaction = db.TransactionManager.StartTransaction()
            Using stdTrx As Transaction = stdDb.TransactionManager.StartTransaction()

                Dim stdLayoutDictionary As DBDictionary = stdDb.LayoutDictionaryId.GetObject(OpenMode.ForRead)
                Dim stdLayout As Layout = stdLayoutDictionary.GetAt("TestPageSetUp").GetObject(OpenMode.ForRead)
                Dim paperspace As BlockTableRecord = SymbolUtilityServices.GetBlockPaperSpaceId(db).GetObject(OpenMode.ForRead)
                Dim layout As Layout = paperspace.LayoutId.GetObject(OpenMode.ForWrite)
                layout.CopyFrom(stdLayout)

            End Using

            trx.Commit()

        End Using

    End Sub

You can also find your answers @TheSwamp