求助:怎么直接给实体SETXDATA属性
本帖最后由 zhangtaosp 于 2012-2-20 15:08 编辑小弟初学.net二次开发AUTOCAD,一个问题琢磨了好几天了弄不明白,还请那位老师指点。比如说我创建了一个单行文本
<CommandMethod("CreateText")> _
Public Sub CreateText()
'' 获得当前文档和数据库 Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' 以只读方式打开块表 Open the Block table 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 single-line text object
Dim acText As DBText = New DBText()
acText.SetDatabaseDefaults()
acText.Position = New Point3d(2, 2, 0)
acText.Height = 0.5
acText.TextString = "Hello, World."
acText.Layer = "ZHANGTAO"
Dim ed As Editor
acBlkTblRec.AppendEntity(acText)
acTrans.AddNewlyCreatedDBObject(acText, True)
''保存更改并销毁事务 Save the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
就是创建个单行文本后我怎么让程序自动给此文本setxdata属性。也就是运行下面的那个方法给文本加属性
<CommandMethod("SetXD")> _
Public Shared Sub SetXData()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim peo As New PromptEntityOptions(vbLf & "请选择实体:")
Dim per As PromptEntityResult = ed.GetEntity(peo)
If per.Status = PromptStatus.OK Then
Dim trans As Transaction = doc.TransactionManager.StartTransaction()
Dim obj As DBObject = trans.GetObject(per.ObjectId, OpenMode.ForWrite)
AddRegAppTableRecord("Test")
Dim rb As New ResultBuffer()
rb.Add(New TypedValue(1001, "Test"))
rb.Add(New TypedValue(1000, "only ascii under AutoCAD 2006"))
rb.Add(New TypedValue(1000, "max length is 255"))
'
' * 1000 扩展数据中的ASCII串(最长字节)
' * 1001 扩展数据的已注册应用程序名(ASCII串,最长字节)。
'
obj.XData = rb
rb.Dispose()
trans.Commit()
trans.Dispose()
End If
End Sub
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 trans As Transaction = doc.TransactionManager.StartTransaction()
Dim rat As RegAppTable = DirectCast(trans.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)
trans.AddNewlyCreatedDBObject(ratr, True)
End If
trans.Commit()
trans.Dispose()
End Sub
我想实现的目标就是,程序画完线后能直接SETXDATA属性,而不用我再去选择
''创建单行文本对象 Create a single-line text object
Dim acText As DBText = New DBText()
acText.SetDatabaseDefaults()
acText.Position = New Point3d(2, 2, 0)
acText.Height = 0.5
acText.TextString = "Hello, World."
acText.Layer = "ZHANGTAO"
Dim ed As Editor
acBlkTblRec.AppendEntity(acText)
就是怎么写语句在这之后直接就给这行文本SETXDATA属性
盯着 Private Shared Sub AddRegAppTableRecord(ByVal regAppName As String) 这个函数多看几下,或许你就明白了.
本帖最后由 zhangtaosp 于 2012-2-20 16:34 编辑
sieben 发表于 2012-2-20 16:09 http://bbs.mjtd.com/static/image/common/back.gif
盯着 Private Shared Sub AddRegAppTableRecord(ByVal regAppName As String) 这个函数多看几下,或许你就明 ...
呵呵,我还是看不太明白,麻烦这位老师直接给个代码行吗
Dim obj As DBObject = trans.GetObject(per.ObjectId, OpenMode.ForWrite)
主要是这句我直接这样的话会出错;
Dim obj As DBObject = trans.GetObject(acText.ObjectId, OpenMode.ForWrite)
1,不要叫我老师,我不是老师,我也未曾收你的学费
2,我不清楚你所说的 "主要是这句我直接这样的话会出错" 是怎样一回事,不过若你明白函数封装是怎么回事的话,你可以往下看,不过只是我的一些猜测而已,不一定是问题所在.
3,Dim obj As DBObject = trans.GetObject(acText.ObjectId, OpenMode.ForWrite),这一句应该是多余的,应该是你直接使用acText即可,不必重新打开,这里可能涉及几个问题
1,若新建的实体acText未提交,其ObjectId是空,打开空会报错
2,若新建的实体acText已提交或已打开,则会因为重复打开而报错
3,还有一个一下子忘了,不好意思!
另, acBlkTblRec.AppendEntity(acText)
acTrans.AddNewlyCreatedDBObject(acText, True)
这两句一般的放在最后,你的SetXData的代码最后放在这前面,这只是建议,不是错误. 本帖最后由 zhangtaosp 于 2012-2-21 13:41 编辑
sieben 发表于 2012-2-20 18:01 http://bbs.mjtd.com/static/image/common/back.gif
1,不要叫我老师,我不是老师,我也未曾收你的学费
2,我不清楚你所说的 "主要是这句我直接这样的话会出错" 是 ...
呵呵,凡是帮助我的,我都会尊称一句老师。谢谢您的指点,按你说的我已经搞定了。希望以后多多帮助!
我刚刚接触 autocad.net开发。
谢谢您!!您的帮助太给力了!
页:
[1]