创建不带属性的块,并马上在屏幕上插入一个块参照
大家好,我想点击鼠标,然后同时绘制两条直线,这两条直线也同时被选中。要实现这个目的,好像只有使用“块”。下面是我到处搜到的代码,然后组合在一起的。我想实现的是首先判断是否已经有了这个块,如果有,就插入这个块的一个块参照,如果没有,则新建一个块,同时新建后立即插入一个块参照,以避免鼠标点击后什么也看不见。我想请各位帮我看看,还有什么可以改进的。
'插入块,没有就新建一个,并立即插入一个块参照,有了就直接插入一个块参照
<CommandMethod("InsertBlock")> _
Public Sub InsertBlock()
'' 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 & "选择块插入点: "
pPtRes = acDoc.Editor.GetPoint(pPtOpts)
Dim InsertPt As Point3d = pPtRes.Value
'' Exit if the user presses ESC or cancels the command
If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
Using trans As Transaction = acCurDb.TransactionManager.StartTransaction
'以写的方式打开块表
Dim bt As BlockTable = trans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite)
'设置块名
Dim Name As String = "Cross"
If bt.Has(Name) = True Then '判断是否存在名为"Room"的块
'以读的方式打开Name表示的块
Dim block As BlockTableRecord = trans.GetObject(bt(Name), OpenMode.ForRead)
'创建一个块参照并设置插入点
Dim blockref As BlockReference = New BlockReference(InsertPt, bt(Name))
'blockref.ScaleFactors = scale '设置块参照的缩放比例
'blockref.Rotation = rotateAngle '设置块参照的旋转角度
'以写的方式打开当前空间(模型空间或图纸空间)
Dim btr As BlockTableRecord = trans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)
btr.AppendEntity(blockref) '在当前空间加入创建的块参照
'通知事务处理加入创建的块参照
trans.AddNewlyCreatedDBObject(blockref, True)
trans.Commit() '提交事务处理以实现块参照的真实加入
Else
'创建一个BlockTableRecord类的对象,表示所要创建的块
Dim record As New BlockTableRecord()
record.Name = Name
record.Origin = New Point3d(50, 0, 0)
Dim LineOne As Line = New Line(Point3d.Origin, New Point3d(100, 0, 0)) : LineOne.ColorIndex = 1
record.AppendEntity(LineOne)
Dim LineTwo As Line = New Line(New Point3d(50, 50, 0), New Point3d(50, -50, 0)) : LineTwo.ColorIndex = 6
record.AppendEntity(LineTwo)
bt.Add(record) '在块表中加入"Cross"块
trans.AddNewlyCreatedDBObject(record, True) '通知事务处理
'创建块以后马上插入一个块,以避免第一次执行命令时再屏幕上什么也看不见
'以读的方式打开Name表示的块
Dim block As BlockTableRecord = trans.GetObject(bt(Name), OpenMode.ForRead)
'创建一个块参照并设置插入点
Dim blockref As BlockReference = New BlockReference(InsertPt, bt(Name))
'blockref.ScaleFactors = scale '设置块参照的缩放比例
'blockref.Rotation = rotateAngle '设置块参照的旋转角度
'以写的方式打开当前空间(模型空间或图纸空间)
Dim btr As BlockTableRecord = trans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)
btr.AppendEntity(blockref) '在当前空间加入创建的块参照
'通知事务处理加入创建的块参照
trans.AddNewlyCreatedDBObject(blockref, True)
trans.Commit() '提交事务
End If
End Using
End Sub
知道Mline吗? sieben 发表于 2013-4-11 07:49 static/image/common/back.gif
知道Mline吗?
你好,我用两条线画个叉叉只是举了个简单的例子,我可能还要添加文字,再添加几个多段线啊什么的。这不是简单采用MLine可以解决的。 看着不象很难
页:
[1]