Issue with Handle References in Copied Polylines in AutoCAD Plugin

Issue with Handle References in Copied Polylines in AutoCAD Plugin

santhoshr151194
Contributor Contributor
244 Views
3 Replies
Message 1 of 4

Issue with Handle References in Copied Polylines in AutoCAD Plugin

santhoshr151194
Contributor
Contributor

Problem Statement:
In my AutoCAD plugin, I associate a text entity with a polyline by storing the text’s handle value inside the polyline’s extension dictionary. This allows me to retrieve the associated text later using the stored handle.

However, when I copy and paste the polyline along with the text, AutoCAD generates new handle values for both the polyline and the text. The problem is that the copied polyline’s extension dictionary still contains the original text’s handle value, instead of the newly generated handle for the pasted text.

Expected Behavior:
When a polyline is copied and pasted along with its associated text, the newly created polyline should store the handle of the newly created text entity in its extension dictionary, rather than retaining the old (source) text handle.

Question:
How can I update the pasted polyline’s extension dictionary to store the new text handle instead of the original one? Is there an event or mechanism in AutoCAD’s API that allows updating handle references for copied entities?

Would appreciate any insights or suggestions!

0 Likes
245 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant

Hi,

Instead of storing the text Handle in the Xrecord, you should store the text ObjectId as SoftPointer.

 

Here's a simple example:

[CommandMethod("TEST")]
public static void Test()
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        var line = new Line(Point3d.Origin, new Point3d(10.0, 0.0, 0.0));
        curSpace.AppendEntity(line);
        tr.AddNewlyCreatedDBObject(line, true);
        var text = new DBText()
        {
            Position = new Point3d(10.0, 0.0, 0.0),
            TextString = "Some text"
        };
        var textId = curSpace.AppendEntity(text);
        tr.AddNewlyCreatedDBObject(text, true);
        if (line.ExtensionDictionary.IsNull)
            line.CreateExtensionDictionary();
        var extDict = (DBDictionary)tr.GetObject(line.ExtensionDictionary, OpenMode.ForWrite);
        var xrecord = new Xrecord();
        xrecord.Data = new ResultBuffer(new TypedValue(330, textId));
        extDict.SetAt("LinkedText", xrecord);
        tr.AddNewlyCreatedDBObject(xrecord, true);
        tr.Commit();
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4

_gile
Consultant
Consultant

And a testing method for the previous example.

[CommandMethod("TEST2")]
public static void Test2()
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;
    var peo = new PromptEntityOptions("\nSelect line: ");
    peo.SetRejectMessage("\nSelected object is not a Line.");
    peo.AddAllowedClass(typeof(Line), true);
    var per = ed.GetEntity(peo);
    if (per.Status != PromptStatus.OK) return;
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var line = (Line)tr.GetObject(per.ObjectId, OpenMode.ForRead);
        if (line.ExtensionDictionary.IsNull)
            return;
        var xDict = (DBDictionary)tr.GetObject(line.ExtensionDictionary, OpenMode.ForRead);
        if (!xDict.Contains("LinkedText"))
            return;
        var xrec = (Xrecord)tr.GetObject(xDict.GetAt("LinkedText"), OpenMode.ForRead);
        var id = (ObjectId)xrec.Data.AsArray()[0].Value;
        ed.SetImpliedSelection(new[] { id });
        tr.Commit();
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

ActivistInvestor
Mentor
Mentor

You should also set the Xrecord's XlateReferences property to true.

 

Also test with COPYCLIP & PASTECLIP

 

 

0 Likes