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

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #15 on: May 15, 2009, 06:49:56 PM »

what is happening in your form ?

what is or isn't happening in your code differently to what you expected ?


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 #16 on: May 15, 2009, 09:23:45 PM »
Well the xdata is not getting attached to the entities.  I get the error that is listed in an earlier post.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #17 on: May 15, 2009, 10:33:25 PM »

ex
Well the xdata is not getting attached to the entities.  I get the error that is listed in an earlier post.

EXACTLY what error message ?
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 #18 on: May 15, 2009, 10:35:09 PM »

and what is happenning in the form code ?
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 #19 on: May 16, 2009, 12:02:46 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.

I do not have a form in the class.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #20 on: May 16, 2009, 06:19:49 PM »
< .. >

I do not have a form in the class.
Quote from: nburr3551
<CommandMethod("TcTag")> _
        Public Sub TCTAG()
            Dim form As New tagform
            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form)

really ??
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 #21 on: May 18, 2009, 03:01:57 PM »
Look,

The code from line 1 to line 23 does not work!

The code from line 25 to line 43 does.

Line 15 is sending the same info as line 36.

The question is WHY won't the code from line 1 to 23 process the AddRegAppTable correctly while the other code does???


1  Public Sub SetXData()
2        Dim form As New tagform
3        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form)
4        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
5        Dim ed As Editor = doc.Editor
6        ' Ask the user to select an entity
7        ' for which to set XData
8        Dim opt As PromptEntityOptions = New PromptEntityOptions("" & vbLf & "Select entity: ")
9        Dim res As PromptSelectionResult = ed.GetSelection
10       Dim I As Long
11       If IsNothing(res.Value) = False Then
12           For I = 1 To res.Value.Count
13              Dim tr As Transaction = doc.TransactionManager.StartTransaction
14                Dim myObject As EditorInput.SelectedObject = res.Value.Item(I - 1)
15                Dim obj As DBObject = tr.GetObject(myObject.ObjectId, OpenMode.ForWrite)
16                AddRegAppTableRecord("TCB")
17                Dim rb As ResultBuffer = New ResultBuffer(New TypedValue(1001, "TCB"), New TypedValue(1000, Mat), New TypedValue(1000, Qty))
18                obj.XData = rb
19                rb.Dispose()
20                tr.Commit()
21            Next
22        End If
23    End Sub


25    Public Sub SetXData()
26        Dim form As New tagform
27        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form)
28        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
29        Dim ed As Editor = doc.Editor
30        ' Ask the user to select an entity
31        ' for which to set XData
32        Dim opt As PromptEntityOptions = New PromptEntityOptions("" & vbLf & "Select entity: ")
33        Dim res As PromptEntityResult = ed.GetEntity(opt)
34        If (res.Status = PromptStatus.OK) Then
35            Dim tr As Transaction = doc.TransactionManager.StartTransaction
36            Dim obj As DBObject = tr.GetObject(res.ObjectId, OpenMode.ForWrite)
37            AddRegAppTableRecord("TCB")
38            Dim rb As ResultBuffer = New ResultBuffer(New TypedValue(1001, "TCB"), New TypedValue(1000, Mat), New TypedValue(1000, Qty))
39            obj.XData = rb
40            rb.Dispose()
41            tr.Commit()
42        End If
43    Private Sub AddRegAppTableRecord(ByVal regAppName As String)
44 End Sub

    Private 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
       

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #22 on: May 19, 2009, 12:40:21 AM »

I don't see anything that would cause

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


... In fact, I don't see any COM code in the stuff you posted.

////

You have mixed up the Prompts .. You are using Entity Prompt Options with GetSelection
.. but the result of that will be that your Prompt will be the default "Select objects:" instead of the  "Select entity: " you would expect.

You don't need to call  AddRegAppTableRecord("TCB") for each Object loop, just do it once ..

I don't do VB, so I'll knock together something in C# you can have a look at. ... I should be able to recycle the code I've already posted

/// kdub



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 #23 on: May 19, 2009, 01:02:21 AM »
Perhaps try something like this ...
DoIt3 at the Command Line

Code: [Select]
//CodeHimBelongaKdub   May2009

using System;

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 class TestCommands
    {
        public TestCommands()
        {   //
            //
        }
        ///------------------------------------------------------------------------------
        [CommandMethod("DoIt3")]
        public void TestAdd_XDataM()
        {
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

            PromptSelectionOptions selOpts = new PromptSelectionOptions();
            selOpts.MessageForAdding = "\n" + "Select objects to Add XDATA: ";
            selOpts.AllowDuplicates = false;
            PromptSelectionResult selRes = ed.GetSelection(selOpts);
            switch (selRes.Status)
            {
                case PromptStatus.Cancel:
                    ed.WriteMessage("\nUser cancelled.");
                    return;
                case PromptStatus.Error:
                    ed.WriteMessage("\nUser input error.");
                    return;
                case PromptStatus.OK:
                    break;
            }
            string appName = "KDUB_Test_App3";
            kdub_Tools.AssertAppName(appName);

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

            foreach (ObjectId id in selRes.Value.GetObjectIds())
            {
                id.SetXData(appName, xDataRB);
            }
        }
    }

    ///==================================================================================
    ///==================================================================================
    /// <Library Stuff>
 
    public static partial class kdub_Tools
    {
        public static void AssertAppName(string appName)
        {
            using (Transaction tr =
                HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                RegAppTable rat = tr.GetObject(
                                    HostApplicationServices.WorkingDatabase.RegAppTableId,
                                    OpenMode.ForWrite)
                                    as RegAppTable;
                if (!rat.Has(appName))
                {
                    RegAppTableRecord ratr = new RegAppTableRecord { Name = appName };
                    rat.Add(ratr);
                    tr.AddNewlyCreatedDBObject(ratr, true);
                }
                tr.Commit();
            }
        }
        //------------------------------------------------------------------------------
        public static void SetXData(this ObjectId entId, string appName, ResultBuffer xDataBuff)
        {
            using (Transaction tr =
                HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                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;
        }
    }
}
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<--

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 7041
  • AKA Daniel
Re: Xdata attachment in VB
« Reply #24 on: May 19, 2009, 01:35:17 AM »
Quote
Don't squat when wearing spurs
  :laugh:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xdata attachment in VB
« Reply #25 on: May 25, 2009, 10:59:16 PM »

wonder what happened to Nick ??
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<--