Hi.
I want to draw a rectangle to represent a room. I use a PolyLine3d to do this. And I want to add a Xrecord to this rectangle. I define a function which will return the ObjectId. So sometime later, I will need to obtain the Xrecord through the ObjectId. Here are the codes I collected form some books. They can be compiled, but when I run the command, I get nothing on the screen. Where is the problem?
Public Shared Function AppendEntity(ByVal ent As Entity) As ObjectId
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim entId As ObjectId
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
entId = btr.AppendEntity(ent)
trans.AddNewlyCreatedDBObject(ent, True)
trans.Commit()
End Using
Return entId
End Function
Public Shared Function AddRectangle(ByVal cenPt As Point3d, ByVal Height As Double, _
ByVal Length As Double, ByVal Name As String, ByVal Size As String) As ObjectId
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim db As Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim Xrec As New Xrecord()
Xrec.Data = New ResultBuffer( _
New TypedValue(DxfCode.Text, Size), _
New TypedValue(DxfCode.Text, Name))
'Define the rectangle
Dim Pt(4) As Point3d
Pt(0) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
Pt(1) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y - Height * 0.5, 0)
Pt(2) = New Point3d(cenPt.X + Length * 0.5, cenPt.Y + Height * 0.5, 0)
Pt(3) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y + Height * 0.5, 0)
Pt(4) = New Point3d(cenPt.X - Length * 0.5, cenPt.Y - Height * 0.5, 0)
Dim Pts As New Point3dCollection(Pt)
Dim ent As New Polyline3d(Poly3dType.SimplePoly, Pts, False)
'Add the rectangle polyline entity to model space
Dim EntId As ObjectId = AppendEntity(ent)
ent.CreateExtensionDictionary()
EntId = ent.ExtensionDictionary()
Dim entXrecord As DBDictionary = trans.GetObject(EntId, OpenMode.ForWrite)
entXrecord.SetAt("MyXrecode", Xrec)
trans.AddNewlyCreatedDBObject(Xrec, True)
Return EntId
trans.Commit()
End Using
End Function
Private RectIds As ObjectIdCollection = New ObjectIdCollection() 'Hold the ObjectId for later use
<CommandMethod("DrawRectXRecord")> _
Public Sub InsertRectWithXRecord()
'' Get the current database and start the Transaction Manager
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim acEd As Editor = acDoc.Editor
Dim pPtRes As PromptPointResult
Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
pPtOpts.Message = vbLf & "Click to insert "
pPtRes = acDoc.Editor.GetPoint(pPtOpts)
Dim ptStart As Point3d = pPtRes.Value
'' Exit if the user presses ESC or cancels the command
If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
'' Start a transaction
Using Trans As Transaction = acCurDb.TransactionManager.StartTransaction()
'Draw the Rectangle
Dim RectId As ObjectId = AddRectangle(ptStart, 1000, 2000, "RectWithXRecord", "1000X2000")
RectIds.Add(RectId)
acEd.WriteMessage(vbCrLf + RectId.ToString()) 'I can get the ObjectId showed on the command line panel
Trans.Commit()
End Using
End Sub