Author Topic: Xdata in C#  (Read 4881 times)

0 Members and 1 Guest are viewing this topic.

JimAndi

  • Guest
Xdata in C#
« on: August 12, 2011, 09:44:58 PM »
Hi All,

I am very new to .NET C# and I am try to rewrite a application I wrote in AutoLisp to .NET C#.  I am looking for a working xdata tool to Add, Edit and Remove xdata to an object. I have been trying to get this to work but the compiler tell me to go find ResultList, List, and Cast missing assembly references.

Can someone... anyone help?

Here is the code:
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

namespace Xdata
{
    public class MyXdata
    {
        [CommandMethod("PUTXDATA")]
        public void test2()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            var resEnt = ed.GetEntity("Select a Entity");
            if (resEnt.Status == PromptStatus.OK)
            {

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

                    Entity ent = tr.GetObject(resEnt.ObjectId, OpenMode.ForWrite) as Entity;
                    var rb = ent.GetXDataForApplication("MyTestApp");

                    if (rb == null)
                    {
                        var rat = tr.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable;
                        if (!rat.Has("MyTestApp"))
                        {
                            var ratr = new RegAppTableRecord();
                            ratr.Name = "MyTestApp";
                            rat.Add(ratr);
                            tr.AddNewlyCreatedDBObject(ratr, true);
                        }

                        ent.XData = new ResultList { { 1001, "MyTestApp" }, { 1000, "value1" } };
                    }
                    else
                    {
                        List<TypedValue> values = rb.Cast<TypedValue>().ToList();
                        values.Add(new TypedValue(1000, "value" + values.Count.ToString()));
                        ent.XData = new ResultBuffer(values.ToArray());
                    }

                    tr.Commit();
                }
            }
        }
    }
}

 

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: Xdata in C#
« Reply #1 on: August 12, 2011, 10:57:28 PM »
Welcome to the Swamp!
You just needed to add a couple "using..." statements to the beginning and you had a ResultList where ResultBuffer is needed, plus it's constructor was not quite right. But it was pretty close!
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

namespace Xdata
{
    public class MyXdata
    {
        [CommandMethod("PUTXDATA")]
        public void test2()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            var resEnt = ed.GetEntity("Select a Entity");
            if (resEnt.Status == PromptStatus.OK)
            {

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

                    Entity ent = tr.GetObject(resEnt.ObjectId, OpenMode.ForWrite) as Entity;
                    var rb = ent.GetXDataForApplication("MyTestApp");

                    if (rb == null)
                    {
                        var rat = tr.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable;
                        if (!rat.Has("MyTestApp"))
                        {
                            var ratr = new RegAppTableRecord();
                            ratr.Name = "MyTestApp";
                            rat.Add(ratr);
                            tr.AddNewlyCreatedDBObject(ratr, true);
                        }

                        ent.XData = new ResultBuffer(new TypedValue(1001, "MyTestApp"), new TypedValue(1000, "value1"));
                    }
                    else
                    {
                        List<TypedValue> values = rb.AsArray().ToList<TypedValue>();
                        values.Add(new TypedValue(1000, "value" + values.Count.ToString()));
                        ent.XData = new ResultBuffer(values.ToArray());
                    }

                    tr.Commit();
                }
            }
        }
    }
}

JimAndi

  • Guest
Re: Xdata in C#
« Reply #2 on: August 13, 2011, 02:29:56 PM »
THANK YOU VERY MUCH!!!!