TheSwamp
Code Red => .NET => Topic started by: nburr3551 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.
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
-
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.
//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));
}
}
}
}
-
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
-
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.
-
VBARXNET ??
-
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
-
check your openMode
-
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 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 ...
-
hint:
for each ...
:D
-
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);
}
-
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.
-
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.
-
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
-
what is happening in your form ?
what is or isn't happening in your code differently to what you expected ?
-
Well the xdata is not getting attached to the entities. I get the error that is listed in an earlier post.
-
exWell the xdata is not getting attached to the entities. I get the error that is listed in an earlier post.
EXACTLY what error message ?
-
and what is happenning in the form code ?
-
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.
-
< .. >
I do not have a form in the class.
<CommandMethod("TcTag")> _
Public Sub TCTAG()
Dim form As New tagform
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(form)
really ??
-
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
-
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
-
Perhaps try something like this ...
DoIt3 at the Command Line
//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;
}
}
}
-
Don't squat when wearing spurs
:laugh:
-
wonder what happened to Nick ??