Polyline(Clone) problem

Polyline(Clone) problem

brianchapmandesign
Collaborator Collaborator
511 Views
2 Replies
Message 1 of 3

Polyline(Clone) problem

brianchapmandesign
Collaborator
Collaborator

Strange...when using this it's literally exploding my original polyline. Is clone inttended to work this way?

 

            Polyline newpoly = new Polyline();
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityResult per = ed.GetEntity("Select a polyline");
            if (per.Status == PromptStatus.OK)

            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                // Open the Block table for read
                BlockTable acBlkTbl;
                acBlkTbl = Tx.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                // Open the Block table record Model space for write
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = Tx.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                DBObject obj = Tx.GetObject(per.ObjectId, OpenMode.ForRead);
                Polyline lwp = obj as Polyline;
                newpoly = lwp.Clone() as Polyline;
                newpoly.ColorIndex = 1;
                acBlkTblRec.AppendEntity(newpoly);
                Tx.AddNewlyCreatedDBObject(newpoly, true);
                Tx.Commit();
            }

"Very funny, Scotty. Now beam down my clothes.
0 Likes
Accepted solutions (1)
512 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

I cannot reproduce what you're describing with your code.

Anyway, you create a new Polyline:

Polyline newpoly = new Polyline();

 but never add it to the database nor dispose it.

 

IMO, this way should be safer:

 

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline: ");
            peo.SetRejectMessage("Selected object is not a polyline.");
            peo.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                using (Polyline clone = (Polyline)pline.Clone())
                {
                    clone.ColorIndex = 1;
                    space.AppendEntity(clone);
                    tr.AddNewlyCreatedDBObject(clone, true);
                }
                tr.Commit();
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

brianchapmandesign
Collaborator
Collaborator
Accepted solution

Thanks giles, as always.


"Very funny, Scotty. Now beam down my clothes.
0 Likes