求助: cad2010插入块的问题.......
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文档中的一个块。
bnameStr是你要插入块的名字 Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForWrite)
'blockId = bt(bnameStr)
'Dim pt As Point3d
'pt = insPoint.TransformBy(ed.CurrentUserCoordinateSystem)
'// Create a block reference
'Dim br As BlockReference = New BlockReference(pt, blockId)
'// Add the block reference to Model space
'oBTR.AppendEntity(br)
'trans.AddNewlyCreatedDBObject(br, True) 本帖最后由 zhangtaosp 于 2012-2-25 21:13 编辑
shirazbj 发表于 2012-2-25 19:46 http://bbs.mjtd.com/static/image/common/back.gif
bnameStr是你要插入块的名字
太感谢你了!呵呵,刚入手,很多不明白。都不知道从那儿入手,只有找个代码读读,再练一下子。
自我感觉这种插入块的办法,比直接从外部插入文件速度会快一些,我自己想是这样子的,不知道结果会不会是这样子。
页:
[1]