.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create Layout without LayoutManager

5 REPLIES 5
Reply
Message 1 of 6
jimmie_fulton
733 Views, 5 Replies

Create Layout without LayoutManager

I need to be able to make layouts in external drawings, and therefore cannot use LayoutManager. I've got it working but it seems to me that my solution may not be the best or the "right" way to do it. I found out quickly that each layout needs it's own *Paper_Space# BlockTableRecord and I've resorted to creating one myself. In the code below, you can see that I name my new layout "*Paper_Space". It ends up getting named "*Paper_Space#" in the database, where the # gets incremented automatically. Is this the right way to do it?

[CommandMethod("test")]
public void Test()
{
Database db = HostApplicationServices.WorkingDatabase;
Layout layout = new Layout();
layout.LayoutName = "Testing";

using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord btr = new BlockTableRecord();
btr.Name = "*Paper_Space";
ObjectId paperSpaceId = bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
layout.AddToLayoutDictionary(db, paperSpaceId);
layout.Dispose(); // Crashes without this...

trans.Commit();
}
}
5 REPLIES 5
Message 2 of 6
hatak
in reply to: jimmie_fulton

Try this it works for me:

Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();

try
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId,OpenMode.ForWrite );
if(lt.Has(nazwa))
{
powId = lt[nazwa];
}
else
{
LayerTableRecord ltr = new LayerTableRecord() ;
ltr.Name = nazwa;
ltr.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(kolor.R ,kolor.G ,kolor.B );
//ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci ,2);
powId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr,true) ;
trans.Commit() ;
}
}
catch
{

}
finally
{

trans.Dispose() ;
}
Message 3 of 6

I'm refering to Layouts, not Layers. 🙂 A little more tricky than adding layers.

I've already figured it out. Thanks for the response!
Message 4 of 6
hatak
in reply to: jimmie_fulton

sorry, I did't see it correctly 🙂
Message 5 of 6

I am struggling with a similar problem with layouts. Would you mind sharing your solution with me?
Message 6 of 6

This will see if a layout with the supplied name exists if it does then the objectid of the layout is returned. If the layout does not exist then it is created and the objectid is then returned. Depending on what you are doing your function may not be shared. Make sure to regen in order for the layout to be displayed.

 

    Public Shared Function CreateGetLayout(ByVal LayoutName As String) As ObjectId
        ' Lets get the active document
        Dim oDoc As Document = Application.DocumentManager.MdiActiveDocument
        ' Lets get the database
        Dim oDB As Database = oDoc.Database
        ' lock the document
        Dim oLock As DocumentLock = oDoc.LockDocument
        ' Lets create a transaction so we can do some database work
        Dim trans As Transaction = oDB.TransactionManager.StartTransaction
        Try
            ' Lets get the layout dictionary
            Dim oLayoutDict As DBDictionary = trans.GetObject(oDB.LayoutDictionaryId, OpenMode.ForRead)
            Dim oLayout As Layout
            ' Lets see if a layout with that name exist.
            If oLayoutDict.Contains(LayoutName) = True Then
                ' Get the layout object from the objectid which is the value of the entry
                Return oLayoutDict.Item(LayoutName)
            Else
                ' Create the new layout
                oLayout = New Layout
                ' Set the layout name
                oLayout.LayoutName = LayoutName
                ' Get the block table
                Dim oBT As BlockTable = trans.GetObject(oDB.BlockTableId, OpenMode.ForRead)
                ' Create a new paperspace block table record
                Dim oBTR As New BlockTableRecord
                ' Set the name of the block table record
                oBTR.Name = "*Paper_Space"
                ' upgrade the open on the block table
                oBT.UpgradeOpen()
                ' Add the block table record to the block table
                Dim oBTRId As ObjectId = oBT.Add(oBTR)
                ' downgrade the open
                oBT.DowngradeOpen()
                ' notify the transaction about the new object
                trans.AddNewlyCreatedDBObject(oBTR, True)
                ' Add the layout to the layout dictionary associating the databse and block table record to the layout
                oLayout.AddToLayoutDictionary(oDB, oBTRId)
                ' notify the transaction about the layout
                trans.AddNewlyCreatedDBObject(oLayout, True)
                ' commit the changes
                trans.Commit()
                ' return the id of the layout
                Return oLayout.ObjectId
            End If

        Finally
            ' Remove the document lock
            oLock.Dispose()
            ' Dispose of the transaction
            trans.Dispose()
        End Try

    End Function

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost