Layout of newly created xref is null

Layout of newly created xref is null

ottosson_mathias
Advocate Advocate
163 Views
0 Replies
Message 1 of 1

Layout of newly created xref is null

ottosson_mathias
Advocate
Advocate

I'm creating a new xref definition and instances on specified layouts (usually just one) in a sideDb:

 

var path = ...;
var xrefName = ...;
var toCreate = ...; // list of instances to be created

using (Transaction transaction = db.TransactionManager.StartTransaction())
{
    ObjectId newXrefId = db.OverlayXref(path, xrefName);

    foreach (var createInstance in toCreate)
    {
        var position = new Point3d(0, 0, 0);
        var scale = new Scale3d(1, 1, 1);

        var newXrefBref = new BlockReference(position, newXrefId);
        var xrefDefinition = (BlockTableRecord)transaction.GetObject(newXrefId, OpenMode.ForWrite, false, true);
        var layout = GetLayout(db, createInstance.LayoutHandle); // getting layout to add xref to
        var layoutBtr = (BlockTableRecord)transaction.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite, false, true);

        layoutBtr.AppendEntity(newXrefBref);
        layoutBtr.LayoutId = layout.BlockTableRecordId;
        xrefDefinition.PathName = xref.Path;
        xrefDefinition.IsUnloaded = xref.LoadStatus == LoadStatus.Unloaded;
        newXrefBref.Layer = createInstance.LayerName;
        newXrefBref.Rotation = createInstance.Rotation
        newXrefBref.ScaleFactors = scale;

        transaction.AddNewlyCreatedDBObject(newXrefBref, true);
    }

    transaction.Commit();
}

 

After creating the new xref I try to load all xrefs in the current db. But layout for the newly created xref is null and the function crashes when I use layout..

 

internal static void GetXrefs(Database db)
{
    var xrg = db.GetHostDwgXrefGraph(true);

    for (int i = 1; i < xrg.NumNodes; i++)
    {
        var xrgn = xrg.GetXrefNode(i);

        using (Transaction transaction = db.TransactionManager.StartTransaction())
        {
            BlockTableRecord xrefDefinition = (BlockTableRecord)transaction.GetObject(xrgn.BlockTableRecordId, OpenMode.ForRead);
            var xrefInstanceIds = xrefDefinition.GetBlockReferenceIds(true, true);

            foreach (ObjectId id in xrefInstanceIds)
            {
                var xrefInstance = transaction.GetObject(id, OpenMode.ForRead) as BlockReference;
                var layoutBlock = transaction.GetObject(xrefInstance.BlockId, OpenMode.ForRead) as BlockTableRecord;
                var layout = transaction.GetObject(layoutBlock.LayoutId, OpenMode.ForRead) as Layout; // layout is null for new xrefs, but works for all others!

                // ...
            }

            transaction.Abort();
        }
    }

    return xrefs;
}

 

It always works just fine for all other xrefs and if I re-open the file I can use the newly created xref to retrieve the layout as with every other xref...

 

What is wrong here. Do I need to refresh the db after creating the xref or something? I would prefer not to have to close and open the file again...

 

Thanks!

0 Likes
164 Views
0 Replies
Replies (0)