.NET

Reply
Contributor
NikWach
Posts: 17
Registered: ‎09-19-2013
Message 1 of 4 (215 Views)

Attaching an XRecord

215 Views, 3 Replies
09-25-2013 02:36 AM

Hello,

 

could someone show me a very simple code-example, how to attach an extension dictionary to an AutoCAD Object und add a XRecord? 

 

I will be grateful to you, if you would write your own examle and not copy one from the internet.

Best regards,

*Expert Elite*
_gile
Posts: 2,077
Registered: ‎04-29-2006
Message 2 of 4 (187 Views)

Re : Attaching an XRecord

09-25-2013 11:06 AM in reply to: NikWach

Hi,

 

Here're some extension methods from my library (but you shouldn't copy them as there're now on the internet).

 

Those methods have to be called from within an active transaction.

 

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Gile.Extensions
{
    public static class Extensions
    {
        public static T GetObject<T>(this ObjectId id) where T : DBObject
        {
	    return (T)id.GetObject(OpenMode.ForRead);
        }
        
        public static T GetObject<T>(this ObjectId id, OpenMode mode) where T : DBObject
        {
            return (T)id.GetObject(mode);
        } public static void SetXrecordData(this DBDictionary dict, string key, params TypedValue[] values) { dict.SetXrecordData(key, new ResultBuffer(values)); } public static void SetXrecordData(this DBDictionary dict, string key, ResultBuffer data) { Xrecord xrec; if (dict.Contains(key)) { xrec = ((ObjectId)dict[key]).GetObject<Xrecord>(OpenMode.ForWrite); } else { dict.UpgradeOpen(); xrec = new Xrecord(); dict.SetAt(key, xrec); dict.Database.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(xrec, true); } xrec.Data = data; } public static DBDictionary GetOrCreateExtensionDictionary(this DBObject obj) { if (obj.ExtensionDictionary == ObjectId.Null) { obj.UpgradeOpen(); obj.CreateExtensionDictionary(); } return obj.ExtensionDictionary.GetObject<DBDictionary>(); } public static void SetXDictionaryXrecordData(this DBObject obj, string key, params TypedValue[] values) { obj.SetXDictionaryXrecordData(key, new ResultBuffer(values)); } public static void SetXDictionaryXrecordData(this DBObject obj, string key, ResultBuffer data) { obj.GetOrCreateExtensionDictionary().SetXrecordData(key, data); } } }

 

Gilles Chanteau
Contributor
NikWach
Posts: 17
Registered: ‎09-19-2013
Message 3 of 4 (162 Views)

Re : Attaching an XRecord

09-25-2013 11:57 PM in reply to: _gile
thanks
Board Manager
StephenPreston
Posts: 368
Registered: ‎05-22-2006
Message 4 of 4 (133 Views)

Re : Attaching an XRecord

09-30-2013 10:53 AM in reply to: NikWach

 If you were happy to use code already posted in public, then I'd also point you to the .NET Training Labs on www.autodesk.com/developautocad. (Note that this page is teporarily offline today - come back tomorrow if you get a  page not found error.

 

 

Cheers,

Stephen Preston
Autodesk Developer Network

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community