.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Attaching an XRecord

3 REPLIES 3
Reply
Message 1 of 4
NikWach
552 Views, 3 Replies

Attaching an XRecord

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,

3 REPLIES 3
Message 2 of 4
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4
NikWach
in reply to: _gile

thanks
Message 4 of 4
StephenPreston
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost