明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2333|回复: 3

创建不带属性的块,并马上在屏幕上插入一个块参照

[复制链接]
发表于 2013-4-10 21:53 | 显示全部楼层 |阅读模式
大家好,我想点击鼠标,然后同时绘制两条直线,这两条直线也同时被选中。要实现这个目的,好像只有使用“块”。下面是我到处搜到的代码,然后组合在一起的。我想实现的是首先判断是否已经有了这个块,如果有,就插入这个块的一个块参照,如果没有,则新建一个块,同时新建后立即插入一个块参照,以避免鼠标点击后什么也看不见。
我想请各位帮我看看,还有什么可以改进的。

  1. '插入块,没有就新建一个,并立即插入一个块参照,有了就直接插入一个块参照
  2.     <CommandMethod("InsertBlock")> _
  3.     Public Sub InsertBlock()
  4.         '' Get the current database and start the Transaction Manager
  5.         Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  6.         Dim acCurDb As Database = acDoc.Database
  7.         Dim acEd As Editor = acDoc.Editor
  8.         Dim pPtRes As PromptPointResult
  9.         Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
  10.         '' 提示用户选择点
  11.         pPtOpts.Message = vbLf & "选择块插入点: "
  12.         pPtRes = acDoc.Editor.GetPoint(pPtOpts)
  13.         Dim InsertPt As Point3d = pPtRes.Value
  14.         '' Exit if the user presses ESC or cancels the command
  15.         If pPtRes.Status = PromptStatus.Cancel Then Exit Sub
  16.         Using trans As Transaction = acCurDb.TransactionManager.StartTransaction
  17.             '以写的方式打开块表
  18.             Dim bt As BlockTable = trans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite)
  19.             '设置块名
  20.             Dim Name As String = "Cross"
  21.             If bt.Has(Name) = True Then '判断是否存在名为"Room"的块
  22.                 '以读的方式打开Name表示的块
  23.                 Dim block As BlockTableRecord = trans.GetObject(bt(Name), OpenMode.ForRead)
  24.                 '创建一个块参照并设置插入点
  25.                 Dim blockref As BlockReference = New BlockReference(InsertPt, bt(Name))
  26.                 'blockref.ScaleFactors = scale '设置块参照的缩放比例
  27.                 'blockref.Rotation = rotateAngle '设置块参照的旋转角度
  28.                 '以写的方式打开当前空间(模型空间或图纸空间)
  29.                 Dim btr As BlockTableRecord = trans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)
  30.                 btr.AppendEntity(blockref) '在当前空间加入创建的块参照
  31.                 '通知事务处理加入创建的块参照
  32.                 trans.AddNewlyCreatedDBObject(blockref, True)
  33.                 trans.Commit() '提交事务处理以实现块参照的真实加入
  34.             Else
  35.                 '创建一个BlockTableRecord类的对象,表示所要创建的块
  36.                 Dim record As New BlockTableRecord()
  37.                 record.Name = Name
  38.                 record.Origin = New Point3d(50, 0, 0)
  39.                 Dim LineOne As Line = New Line(Point3d.Origin, New Point3d(100, 0, 0)) : LineOne.ColorIndex = 1
  40.                 record.AppendEntity(LineOne)
  41.                 Dim LineTwo As Line = New Line(New Point3d(50, 50, 0), New Point3d(50, -50, 0)) : LineTwo.ColorIndex = 6
  42.                 record.AppendEntity(LineTwo)
  43.                 bt.Add(record) '在块表中加入"Cross"块
  44.                 trans.AddNewlyCreatedDBObject(record, True) '通知事务处理
  45.                 '创建块以后马上插入一个块,以避免第一次执行命令时再屏幕上什么也看不见
  46.                 '以读的方式打开Name表示的块
  47.                 Dim block As BlockTableRecord = trans.GetObject(bt(Name), OpenMode.ForRead)
  48.                 '创建一个块参照并设置插入点
  49.                 Dim blockref As BlockReference = New BlockReference(InsertPt, bt(Name))
  50.                 'blockref.ScaleFactors = scale '设置块参照的缩放比例
  51.                 'blockref.Rotation = rotateAngle '设置块参照的旋转角度
  52.                 '以写的方式打开当前空间(模型空间或图纸空间)
  53.                 Dim btr As BlockTableRecord = trans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)
  54.                 btr.AppendEntity(blockref) '在当前空间加入创建的块参照
  55.                 '通知事务处理加入创建的块参照
  56.                 trans.AddNewlyCreatedDBObject(blockref, True)
  57.                 trans.Commit() '提交事务
  58.             End If
  59.         End Using

  60.     End Sub

发表于 2013-4-11 07:49 | 显示全部楼层
知道Mline吗?
 楼主| 发表于 2013-4-11 14:55 | 显示全部楼层
sieben 发表于 2013-4-11 07:49
知道Mline吗?

你好,我用两条线画个叉叉只是举了个简单的例子,我可能还要添加文字,再添加几个多段线啊什么的。这不是简单采用MLine可以解决的。
发表于 2013-4-17 19:45 | 显示全部楼层
看着不象很难
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-2 13:11 , Processed in 0.179332 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表