Why I can not add Xrecord to DBDictionary

Why I can not add Xrecord to DBDictionary

Anonymous
Not applicable
2,121 Views
6 Replies
Message 1 of 7

Why I can not add Xrecord to DBDictionary

Anonymous
Not applicable

I have simple code that creates an entity and extension dictionary for it.

 

This procedure I made with Visual Basic 2010 and used it with AutoCAD 2011. It worked fine. When I tried to update my code with Visual Studio 2015 and use it with AutoCAD 2018 it leads to failure of application.

 

I think that problem occurs when method SatAt is called.

 

Thank you in advance !

 

 

        <CommandMethod("Test_Add")> Public Sub Add_XRec()

            '' Get the current document and database, and start a transaction
            Dim acPoly As Polyline
            Dim acDoc As Autodesk.AutoCAD.ApplicationServices.Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim acCurDb As Database = acDoc.Database

            Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
                '' Open the Block table record for read
                Dim acBlkTbl As BlockTable
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                   OpenMode.ForRead)

                '' Open the Block table record Model space for write
                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),
                                      OpenMode.ForWrite)

                '' Create a polyline
                acPoly = New Polyline()
                acPoly.AddVertexAt(0, New Point2d(1, 1), 0, 0, 0)
                acPoly.AddVertexAt(1, New Point2d(1, 2), 0, 0, 0)
                'acPoly.Closed = False

                '' Add the new object to the block table record and the transaction
                acBlkTblRec.AppendEntity(acPoly)
                acTrans.AddNewlyCreatedDBObject(acPoly, True)

                Dim exd As DBDictionary
                Dim ent As Entity = CType(acTrans.GetObject(acPoly.ObjectId, OpenMode.ForWrite), Polyline)

                ent.CreateExtensionDictionary()
                exd = acTrans.GetObject(ent.ExtensionDictionary, OpenMode.ForWrite)

                '' Create a ResultBuffer and Xrecord
                Dim xr As New Xrecord()
                Dim buff As New ResultBuffer()

                buff.Add(New TypedValue(DxfCode.ExtendedDataAsciiString, "TEXT_1"))
                buff.Add(New TypedValue(DxfCode.ExtendedDataAsciiString, "TEXT_2"))
                xr.Data = buff

                Try
                    '' Add the new Xrecord to the extension dictionary  and the transaction
                    exd.SetAt("XREC_KEY", xr)
                    acTrans.AddNewlyCreatedDBObject(xr, True)

                Catch ex As System.Exception
                    acDoc.Editor.WriteMessage(vbLf & ex.Message)
                    acTrans.Abort()
                End Try


                '' Save objects to the database
                acTrans.Commit()
            End Using

        End Sub
0 Likes
2,122 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

 

It seems to me you use a wrong DxfCode.

DxfCode.ExtendedDataAsciiString (1000) is to be used with extended data, not xrecord.

Use DxfCode.Text (1) instead.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

Anonymous
Not applicable

Hi !

 

I used to think so too, I checked this case.

But it is does not work even I add empty XRecord

0 Likes
Message 4 of 7

_gile
Consultant
Consultant

This seems to work here.

 

        [CommandMethod("Test_Add")]
        public void Add_XRec()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                Polyline pline = new Polyline();
                pline.AddVertexAt(0, new Point2d(1.0, 1.0), 0.0, 0.0, 0.0);
                pline.AddVertexAt(1, new Point2d(1.0, 2.0), 0.0, 0.0, 0.0);
                modelSpace.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);

                pline.CreateExtensionDictionary();
                DBDictionary xdict = (DBDictionary)tr.GetObject(pline.ExtensionDictionary, OpenMode.ForWrite);
                Xrecord xrec = new Xrecord();
                ResultBuffer data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, "TEXT1"),
                    new TypedValue(1, "TEXT2"));
                xrec.Data = data;
                xdict.SetAt("XREC_KEY", xrec);
                tr.AddNewlyCreatedDBObject(xrec, true);
                tr.Commit();
            }
        }

VB conversion with telerik

 

<CommandMethod("Test_Add")>
Public Sub Add_XRec()
    Dim doc As Document = Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Using tr As Transaction = db.TransactionManager.StartTransaction()
        Dim modelSpace As BlockTableRecord = CType(tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite), BlockTableRecord)
        Dim pline As Polyline = New Polyline()
        pline.AddVertexAt(0, New Point2d(1, 1), 0, 0, 0)
        pline.AddVertexAt(1, New Point2d(1, 2), 0, 0, 0)
        modelSpace.AppendEntity(pline)
        tr.AddNewlyCreatedDBObject(pline, True)
        pline.CreateExtensionDictionary()
        Dim xdict As DBDictionary = CType(tr.GetObject(pline.ExtensionDictionary, OpenMode.ForWrite), DBDictionary)
        Dim xrec As Xrecord = New Xrecord()
        Dim data As ResultBuffer = New ResultBuffer(New TypedValue(CInt(DxfCode.Text), "TEXT1"), New TypedValue(1, "TEXT2"))
        xrec.Data = data
        xdict.SetAt("XREC_KEY", xrec)
        tr.AddNewlyCreatedDBObject(xrec, True)
        tr.Commit()
    End Using
End Sub

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by Refactoring Essentials.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 7

ActivistInvestor
Mentor
Mentor

When you updated your code to VS2015, did you change your project's references to the targeted AutoCAD version's assemblies, and did you also update your targeted framework version to the version required by the targeted AutoCAD release?

 

For AutoCAD 2018, your target framework should be .NET 4.6 and you also need to set the references to AutoCAD 2018 assemblies.

 


@Anonymouswrote:

Hi !

 

I used to think so too, I checked this case.

But it is does not work even I add empty XRecord


 

0 Likes
Message 6 of 7

Anonymous
Not applicable

Yes I do. I start new progect to check how does it work but the resulte is the same.

 

No_Name_1.jpgNo_Name_2.jpg

0 Likes
Message 7 of 7

ActivistInvestor
Mentor
Mentor

Post the actual code you say isn't working so someone can verify that it's correct.

 


@Anonymouswrote:

Yes I do. I start new progect to check how does it work but the resulte is the same.

 

 


 

0 Likes