Author Topic: Xdata attachment in VB  (Read 6564 times)

0 Members and 1 Guest are viewing this topic.

nburr3551

  • Guest
Xdata attachment in VB
« on: May 08, 2009, 11:35:25 AM »
I'm new to .net and was attempting to attach xdata to an entity. I dont seem to be able to get a value into the result buffer.
Any assitance would be appreciated.

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices

Class XData

    Public Shared Function AddXData(ByVal ObjId As ObjectId, ByVal Appname As String, ByVal DataVal As String, ByVal dataVal1 As String) As Boolean
        Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Dim myT As Transaction = db.TransactionManager.StartTransaction()
        Try
            Dim obj As DBObject = myT.GetObject(ObjId, OpenMode.ForWrite)
            AddRegAppTableRecord(Appname)
            Dim rb As New ResultBuffer(New TypedValue(1001, Appname), New TypedValue(1000, DataVal), New TypedValue(1000, dataVal1))
            obj.XData = rb
            myT.Commit()
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message & vbCrLf)
        Finally
            myT.Dispose()
        End Try
    End Function

    Public Shared Sub AddRegAppTableRecord(ByVal regAppName As String)
        Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
        Dim myT As Transaction = db.TransactionManager.StartTransaction()
        Try
            Dim rat As RegAppTable = CType(myT.GetObject(db.RegAppTableId, OpenMode.ForRead, False), RegAppTable)
            If Not rat.Has(regAppName) Then
                rat.UpgradeOpen()
                Dim ratr As New RegAppTableRecord
                ratr.Name = regAppName
                rat.Add(ratr)
                myT.AddNewlyCreatedDBObject(ratr, True)
            End If
            myT.Commit()
        Catch ex As Exception
            MsgBox(vbCrLf & ex.Message & vbCrLf)

        Finally
            myT.Dispose()
        End Try
    End Sub

End Class

edited by Daniel, added code tags
« Last Edit: May 08, 2009, 11:48:06 AM by Daniel »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #1 on: May 08, 2009, 07:32:44 PM »
I'm new to .net and was attempting to attach xdata to an entity. I dont seem to be able to get a value into the result buffer.
Any assitance would be appreciated.
< .. >

Are you having trouble with the Result Buffer or with adding the XData ??
I glaze over with VB so here's some C# to play with.
Code: [Select]
//CodeHimBelongaKwb   May2009

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(
    kdub_testing.TestCommands))]

namespace kdub_testing
{
    public static partial class kdub_Tools
    {
          public static void SetXData(this ObjectId entId, string appName, ResultBuffer xDataBuff)
        {
            using (Transaction tr =
                HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                RegAppTable table = tr.GetObject(
                    HostApplicationServices.WorkingDatabase.RegAppTableId, OpenMode.ForWrite)
                    as RegAppTable;
                if (!table.Has(appName))
                {
                    RegAppTableRecord record = new RegAppTableRecord { Name = appName };
                    table.Add(record);
                    tr.AddNewlyCreatedDBObject(record, true);
                }
                Entity ent = tr.GetObject(entId, OpenMode.ForWrite) as Entity;
                if (ent != null)
                {
                    ent.XData = xDataBuff;
                }
                tr.Commit();
            }
        }
        //------------------------------------------------------------------------------
        public static ResultBuffer GetXData(this ObjectId entId, string appName)
        {
            ResultBuffer xDataBuff = new ResultBuffer();
            using (Transaction tr =
                HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                Entity entity = tr.GetObject(entId, OpenMode.ForRead) as Entity;
                if (entity != null)
                {
                    xDataBuff = entity.GetXDataForApplication(appName);
                }
                tr.Commit();
            }
            return xDataBuff;
        }
    }
    ///==================================================================================
    ///==================================================================================

    public class TestCommands
    {
        public TestCommands()
        {   //
            //
        }
        ///------------------------------------------------------------------------------
        [CommandMethod("DoIt2")]
        public void TestAdd_XData()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            ObjectId id = ed.GetEntity("Select an entity to attach XDATA").ObjectId;
            string appName = "KDUB_Test_App";

            ResultBuffer xDataRB = new ResultBuffer(
            new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName),
            new TypedValue((int)DxfCode.ExtendedDataAsciiString, "Don't squat when wearing spurs."),

            new TypedValue((int)DxfCode.ExtendedDataReal, 1.7E+3),
            new TypedValue((int)DxfCode.ExtendedDataInteger16, 32766),
            new TypedValue((int)DxfCode.ExtendedDataInteger32, 2147483646)
           );
            //
            ed.WriteMessage("\nXData Input");
            foreach (TypedValue xd in xDataRB)
            {
                ed.WriteMessage(string.Format(
                    "\nTypeCode={0},Value={1}",
                    xd.TypeCode,
                    xd.Value));
            }
            //   
            id.SetXData(appName, xDataRB);
            //
            ResultBuffer xDataReadRB = id.GetXData(appName);
            if (xDataReadRB == null)
            {
                return;
            }
            ed.WriteMessage("\nXData Output");
            foreach (TypedValue xd in xDataReadRB)
            {
                ed.WriteMessage(string.Format(
                    "\nTypeCode={0},Value={1}",
                    xd.TypeCode,
                    xd.Value));
            }
        }       
    }
}

Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #2 on: May 08, 2009, 07:38:42 PM »

note :
if you use the Mercury theme in your profile for this forum the code will display much better :)
ie full width and full height without scroll bars
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

nburr3551

  • Guest
Re: Xdata attachment in VB
« Reply #3 on: May 08, 2009, 11:25:21 PM »
I have an example in C# that works.
        static public void SetXData()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            // Ask the user to select an entity

            // for which to set XData
            PromptEntityOptions opt = new PromptEntityOptions( "\nSelect entity: ");
            PromptEntityResult res = ed.GetEntity(opt);
            if (res.Status == PromptStatus.OK)
            {
                Transaction tr = doc.TransactionManager.StartTransaction();
                using (tr)
                {
                    DBObject obj = tr.GetObject(res.ObjectId,OpenMode.ForWrite);
                    AddRegAppTableRecord("TCB");
                    ResultBuffer rb = new ResultBuffer(new TypedValue(1001, "TCB"), new TypedValue(1000, "PAP013"), new TypedValue(1000, "1"));
                    obj.XData = rb;
                    rb.Dispose();
                    tr.Commit();
                }
            }
        }

        static void AddRegAppTableRecord(string regAppName)
        {
          Document doc =Application.DocumentManager.MdiActiveDocument;
          Editor ed = doc.Editor;
          Database db = doc.Database;
          Transaction tr = doc.TransactionManager.StartTransaction();
            using (tr)
            {
                RegAppTable rat = (RegAppTable)tr.GetObject(db.RegAppTableId,OpenMode.ForRead,false);
                if (!rat.Has(regAppName))
                {
                    rat.UpgradeOpen();
                    RegAppTableRecord ratr =new RegAppTableRecord();
                    ratr.Name = regAppName;
                    rat.Add(ratr);
                    tr.AddNewlyCreatedDBObject(ratr, true);
                }
                tr.Commit();
            }
        }
But I'm trying to convert my VBA programs to VB.net
My time frame for conversion is limited so while I would love to learn a
new programing language I'm just trying to convert the example to vb
so that I can apply it.  The example that VBARXNET shows is attaching
xdata to an xrecord and that example does not work. Or I just don't understand
the example. 

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #4 on: May 09, 2009, 01:51:05 AM »

VBARXNET  ??
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

nburr3551

  • Guest
Re: Xdata attachment in VB
« Reply #5 on: May 09, 2009, 09:30:16 AM »
Its the Objectarx/samples/dotnet vbsample project for xrecord in which they attach xdata to
a xrecord. It appears that the sub addAppTableRecord() is not adding the appname to the
tablerecord. I seem to get the same results from the C# and VB apps until this line in VB
Dim tbl As RegAppTable = CType(tr.GetObject(db.RegAppTableId, OpenMode.ForRead, False), RegAppTable)

from c#
RegAppTable rat = (RegAppTable)tr.GetObject(db.RegAppTableId,OpenMode.ForRead,false);

I have found examples from Autodesk and tried them with the same results. Just can't add a record to
regAppTable

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6965
  • AKA Daniel
Re: Xdata attachment in VB
« Reply #6 on: May 09, 2009, 10:40:47 AM »
check your openMode

nburr3551

  • Guest
Re: Xdata attachment in VB
« Reply #7 on: May 09, 2009, 03:21:48 PM »
This line of code is returning an error.

Dim ratr As New RegAppTableRecord

"Error HRESULT E_FAIL has been returned from a call to a COM component."

I have tried OpenMode.ForRead with a rat.UpgradeOpen()
I have tried OpenMode.ForWrite and both return same result.

nburr3551

  • Guest
Re: Xdata attachment in VB
« Reply #8 on: May 13, 2009, 03:05:36 PM »
I downloaded the C# to Visual Basic Add-in for Visual Studio and converted the C# code to VB and now can attach data. However the selection option is set for a single entity, when I can this to multiple or use a selection set to tag items it does not work.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #9 on: May 13, 2009, 03:35:54 PM »

Do you know how to iterate a selection set ?

hint :
foreach ...
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

MP

  • Seagull
  • Posts: 17529
Re: Xdata attachment in VB
« Reply #10 on: May 13, 2009, 03:47:25 PM »
hint:
for each ...


















:D
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1367
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #11 on: May 13, 2009, 05:53:27 PM »
I was going to say something about VB needing twice as much work as C# ... but thought better of it :)


ps;

not to be confused with for ..


for (int i = 1; i <= 5; i++)
{
      Console.WriteLine(i);
}



int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
foreach (int i in numbers2D)
{
    System.Console.Write("{0} ", i);
}

called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

nburr3551

  • Guest
Re: Xdata attachment in VB
« Reply #12 on: May 14, 2009, 08:19:17 PM »

Do you know how to iterate a selection set ?

hint :
foreach ...

I guess if you look at earlier code in post you find answer. I don't have my complete code listing here but will post in morning. I may be stupid but  I'm not ignorant been writting lisp for 16 years and VbA FOR 5. As I stated earlier I can get the code to work in Com mode so maybe I can iterate. Just having problems with Net.  guess I need to find someone else to ask.

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1367
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #13 on: May 14, 2009, 09:05:17 PM »
I downloaded the C# to Visual Basic Add-in for Visual Studio and converted the C# code to VB and now can attach data. However the selection option is set for a single entity, when I can this to multiple or use a selection set to tag items it does not work.




Do you know how to iterate a selection set ?

hint :
foreach ...

I guess if you look at earlier code in post you find answer. I don't have my complete code listing here but will post in morning. I may be stupid but  I'm not ignorant been writting lisp for 16 years and VbA FOR 5. As I stated earlier I can get the code to work in Com mode so maybe I can iterate. Just having problems with Net.  guess I need to find someone else to ask.

Please yourself, I was just answering your question ... or taking my best guess.
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

nburr3551

  • Guest
Re: Xdata attachment in VB
« Reply #14 on: May 15, 2009, 12:39:43 PM »
Here is the code that I mentioned.


Private Shared Sub AddRegAppTableRecord(ByVal regAppName As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tr As Transaction = doc.TransactionManager.StartTransaction
            Dim rat As RegAppTable = CType(tr.GetObject(db.RegAppTableId, OpenMode.ForRead, False), RegAppTable)
            If Not rat.Has(regAppName) Then
                rat.UpgradeOpen()
                Dim ratr As RegAppTableRecord = New RegAppTableRecord
                ratr.Name = regAppName
                rat.Add(ratr)
                tr.AddNewlyCreatedDBObject(ratr, True)
            End If
            tr.Commit()
        End Sub

        <CommandMethod("TcTag")> _
        Public Sub TCTAG()
            Dim form As New tagform
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form)
            Dim myDB As DatabaseServices.Database
            Dim myDWG As ApplicationServices.Document
            Dim myEd As EditorInput.Editor
            Dim myTransMan As DatabaseServices.TransactionManager
            Dim myTrans As DatabaseServices.Transaction
            myDWG = ApplicationServices.Application.DocumentManager.MdiActiveDocument
            myDB = myDWG.Database
            myEd = myDWG.Editor
            Dim myPSR As EditorInput.PromptSelectionResult
            myPSR = myEd.GetSelection()
            If IsNothing(myPSR.Value) = False Then
                myTransMan = myDWG.TransactionManager
                myTrans = myTransMan.StartTransaction
                Dim MySelObj As EditorInput.SelectedObject
                Dim myObjectId As DatabaseServices.ObjectId
                Dim I As Long
                For I = 1 To myPSR.Value.Count
                    MySelObj = myPSR.Value.Item(I - 1)
                    myObjectId = MySelObj.ObjectId
                    AddxData(myObjectId, Mat, Qty)
                Next
                myTrans.Dispose()
                myTransMan.Dispose()
            End If
        End Sub

        Public Shared Sub AddxData(ByVal objid As ObjectId, ByVal mat As String, ByVal Qty As String)
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim tr As Transaction = doc.TransactionManager.StartTransaction
            Dim obj As DBObject = tr.GetObject(objid, OpenMode.ForWrite)
            AddRegAppTableRecord("TCB")
            Dim rb As ResultBuffer = New ResultBuffer(New TypedValue(1001, "TCB"), New TypedValue(1000, mat), New TypedValue(1000, Qty))
            obj.XData = rb
            rb.Dispose()
            tr.Commit()

        End Sub