Public Shared Sub insetBLocka()
Dim obj As Object
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim ppr As PromptPointResult = ed.GetPoint("请选择插入点:")
Dim pt As Point3d = ppr.Value
'utility.WriteToEditor(pt.ToString())
Dim pidBlock As New PIDBlock()
'自己定义的图块类,保存图块的路径和名称
pidBlock.Name = "sample"
pidBlock.Path = "D:\AutoPIS_DQ\OCSNBTK\byq.dwg"
Using blkDb As New Database(False, True)
blkDb.ReadDwgFile(pidBlock.Path, System.IO.FileShare.Read, True, Nothing)
blkDb.CloseInput(True)
Using docLock As DocumentLock = doc.LockDocument()
'多文档要先这样,否则报至命错误
Using t As Transaction = doc.TransactionManager.StartTransaction()
Dim idBTR As ObjectId = doc.Database.Insert(pidBlock.Name, blkDb, False)
'create a ref to the block
Dim bt As BlockTable = DirectCast(t.GetObject(doc.Database.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord = DirectCast(t.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
Using bref As New BlockReference(pt, idBTR)
btr.AppendEntity(bref)
t.AddNewlyCreatedDBObject(bref, True)
t.Commit()
End Using
End Using
End Using
这是一段在CAD中插入块的代码,小弟刚开始入手CAD.net二次开发。想问的一个问题是,这段代码插入的块是以一个外部的文件形式插入的 pidBlock.Path = "D:\AutoPIS_DQ\OCSNBTK\byq.dwg" 。我想把这段代码改成直接插入CAD文档中的一个块。也就是说我已经在CAD文档中做好了一个块,怎样才能用代码实现。
以前用VBA的时候这样插入就行 Set BlockRefObj = ACADDoc.ModelSpace.InsertBlock(InsPnt, BDwgName, 1#, 1#, 1#, InsAng)
BDWGNAME是CAD文档中的一个块。