Copying Entity to Layout

Copying Entity to Layout

Anonymous
Not applicable
1,438 Views
5 Replies
Message 1 of 6

Copying Entity to Layout

Anonymous
Not applicable

Hey All, 

 

I am trying to copy a polyline from the model view to a paperspace layout. I have no problem creating a new object on the paperspace layout, however when I try to clone an entity and the append it to the paperspace blocktable, it does not appear on the paperspace as intended.  I have attached a copy of my code, any thoughts on why this may be failing? 

 

[CommandMethod("stopworking")]
public void copy2paper()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

 

using (Transaction tr = db.TransactionManager.StartTransaction())
{

// Get the primary paperspace from the block table

BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord ps = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);

 

 

PromptEntityResult prEnt = ed.GetEntity("Select an entity: ");

if (prEnt.Status == PromptStatus.OK)
{
// Add each to the paperspace blocktablerecord
// and create/add an associated viewport object
Entity ent = (Entity)tr.GetObject(prEnt.ObjectId, OpenMode.ForWrite);
Entity clone = ent.Clone() as Entity;

if (ent != null)
{
ps.AppendEntity(clone);
tr.AddNewlyCreatedDBObject(clone, true);
}
}
tr.Commit();
}
// Let's take a look at the results in paperspace
db.TileMode = false;
}

0 Likes
Accepted solutions (1)
1,439 Views
5 Replies
Replies (5)
Message 2 of 6

BKSpurgeon
Collaborator
Collaborator
Accepted solution

 

This works just fine for me. Try it and see if it works on your pc?

 

 

 

 

 [CommandMethod("stopworking")]
        public void copy2paper()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // Get the primary paperspace from the block table
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord ps = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);


                PromptEntityResult prEnt = ed.GetEntity("Select an entity: ");
                if (prEnt.Status == PromptStatus.OK)
                {
                    // Add each to the paperspace blocktablerecord
                    // and create/add an associated viewport object
                    Entity ent = (Entity)tr.GetObject(prEnt.ObjectId, OpenMode.ForWrite);
                    Entity clone = ent.Clone() as Entity;
                    ps.AppendEntity(clone);
                    tr.AddNewlyCreatedDBObject(clone, true);                   
                }

                tr.Commit();
            }
            // Let's take a look at the results in paperspace
            db.TileMode = false;
        }

 

 

inPaperspace.JPG

 

Message 3 of 6

ActivistInvestor
Mentor
Mentor

You don't use Clone() to copy an Entity.

 

You use the Database.DeepCloneObjects() method.  There is a big difference. The latter will clone anything attached to the entity along with it (Clone() does a 'shallow' clone, and only clones the entity itself, but nothing attached to it like an ExtensionDictionary). DeepCloneObjects() also preserves object references between multiple objects when they are cloned in the same operation. DeepCloneObjects() is what the COPY command uses.

0 Likes
Message 4 of 6

Anonymous
Not applicable

Thanks, 

that was the solution I used to build my original code. When cloning the object I forgot to transform it onto the paperspace sheet area and a s a result didn't realize the code I posted was working as designed. 

0 Likes
Message 5 of 6

Anonymous
Not applicable

Thanks for the reply, 

 

For my purposes the clone() function was perfectly adequate as I only needed to clone the shape of the object I was cloning in order to create a non rectangular viewport. That does clear up some questions about the usage of deepclone vs clone, so thanks for the reply! 

0 Likes
Message 6 of 6

ActivistInvestor
Mentor
Mentor

@Anonymous wrote:

Thanks for the reply, 

 

For my purposes the clone() function was perfectly adequate as I only needed to clone the shape of the object I was cloning in order to create a non rectangular viewport. That does clear up some questions about the usage of deepclone vs clone, so thanks for the reply! 


That's fine as long as what you're cloning is not a 'heavy' polyline (Polyline2d), as Clone() doesn't clone their vertex subentities.

0 Likes