明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2960|回复: 9

[基础] 求助 vb.net 无法插入块

[复制链接]
发表于 2014-6-10 16:41 | 显示全部楼层 |阅读模式
本帖最后由 hrbustmqc 于 2014-6-10 16:47 编辑

做个简单的程序,在cad中插入直线,在直线的端点插入一个块(“断路器”),运行后只能生成直线,却不能插入块
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.ApplicationServices
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.Geometry
  5. Imports Autodesk.AutoCAD.EditorInput
  6. Public Class Class1

  7.     <CommandMethod("AddLine")> _
  8.     Public Sub AddLine()
  9.         '' 获取当前文档和数据库
  10.         Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  11.         Dim acCurDb As Database = acDoc.Database
  12.         Dim acEditor As Editor = acDoc.Editor
  13.         Using (acDoc.LockDocument)
  14.             '' 启动事务
  15.             Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  16.                 '' 以读模式打开 块表
  17.                 Dim acBlkTbl As BlockTable
  18.                 acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
  19.                 '' 以写模式打开 块表记录模型空间
  20.                 Dim acBlkTblRec As BlockTableRecord
  21.                 acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
  22.                 OpenMode.ForWrite)

  23.                 '' 拾取点
  24.                 Dim pPtRes As PromptPointResult
  25.                 Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
  26.                 pPtOpts.Message = vbLf & "Insert Point of Block:"
  27.                 pPtRes = acEditor.GetPoint(pPtOpts)
  28.                 Dim InsertPt As Point3d = pPtRes.Value
  29.                 '' 创建直线
  30.                 Dim acLine As Line = New Line(New Point3d(InsertPt.X, InsertPt.Y, 0), _
  31.                 New Point3d(InsertPt.X + 1000, InsertPt.Y, 0))

  32.                 Dim FilePath As String = "G:\断路器.dwg"
  33.                 Dim FileName As String = "断路器.dwg"
  34.                 Dim BlockName As String = "断路器"

  35.                 Dim Block As BlockReference = New BlockReference(New Point3d(InsertPt.X + 1000, InsertPt.Y, 0), acBlkTbl(BlockName))
  36.                 Block.ScaleFactors = New Scale3d(1, 1, 1)
  37.                 Block.Rotation = 0

  38.                 '' 将新对象添加到块表记录和事务
  39.                 acBlkTblRec.AppendEntity(Block)
  40.                 acTrans.AddNewlyCreatedDBObject(Block, True)
  41.                 acBlkTblRec.AppendEntity(acLine)
  42.                 acTrans.AddNewlyCreatedDBObject(acLine, True)
  43.                 '' 将新对象保存到数据库
  44.                 acTrans.Commit()
  45.             End Using
  46.         End Using
  47.     End Sub
  48. End Class
发表于 2014-6-10 21:30 | 显示全部楼层
如果你当前图面中没有定义"断路器"这个图块,就不可能将"断路器"图块插入到模型空间!

我猜你是想将外部保持的图块插入到当前图面中吧,这个方法可不行啊!
 楼主| 发表于 2014-6-10 21:53 | 显示全部楼层
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.ApplicationServices
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.Geometry
  5. Imports Autodesk.AutoCAD.EditorInput
  6. Public Class Class1
  7.   
  8.     <CommandMethod("InserBlock")> _
  9.     Public Sub InserBlock()
  10.         Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  11.         Dim acDatabase As Database = acDoc.Database
  12.         Dim acEditor As Editor = acDoc.Editor
  13.         Dim pPtRes As PromptPointResult
  14.         Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")

  15.         pPtOpts.Message = vbLf & "Insert Point of Block:"
  16.         pPtRes = acEditor.GetPoint(pPtOpts)
  17.         Dim InsertPt As Point3d = pPtRes.Value

  18.         If pPtRes.Status = PromptStatus.Cancel Then
  19.             Exit Sub
  20.         End If

  21.         Using (acDoc.LockDocument)
  22.             Using trans As Transaction = acDatabase.TransactionManager.StartTransaction
  23.                 Dim acBlockTable As BlockTable = trans.GetObject(acDatabase.BlockTableId, OpenMode.ForWrite)
  24.                 Dim FilePath As String = "G:\断路器.dwg"
  25.                 Dim FileName As String = "断路器.dwg"
  26.                 Dim BlockName As String = "断路器"

  27.                 If acBlockTable.Has(BlockName) = True Then
  28.                     Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
  29.                     Block.ScaleFactors = New Scale3d(1, 1, 1)
  30.                     Block.Rotation = 0

  31.                     Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
  32.                     ModelSpace.AppendEntity(Block)

  33.                     trans.AddNewlyCreatedDBObject(Block, True)
  34.                     trans.Commit()
  35.                 Else
  36.                     Dim db As New Database(False, False)
  37.                     'sourceDb.ReadDwgFile("c:\\xxx.dwg", System.IO.FileShare.Read, true, "");
  38.                     db.ReadDwgFile(FilePath, System.IO.FileShare.Read, False, "")
  39.                     Dim id As ObjectId = acDatabase.Insert(FileName, db, False)

  40.                     If id.IsValid = False Then
  41.                         Exit Sub
  42.                     End If

  43.                     Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
  44.                     Block.ScaleFactors = New Scale3d(1, 1, 1)
  45.                     Block.Rotation = 0

  46.                     Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
  47.                     ModelSpace.AppendEntity(Block)

  48.                     trans.AddNewlyCreatedDBObject(Block, True)
  49.                     trans.Commit()

  50.                 End If
  51.             End Using
  52.         End Using
  53.     End Sub

  54. End Class
断路器图块是个单独的文件。这个代码可以执行。跟第一个例子结合后就不能插入了,不知道为什么,请大家帮忙看看
发表于 2014-6-10 22:49 | 显示全部楼层
自己仔细看看上下代码有没有什么不同?
 楼主| 发表于 2014-6-10 22:51 | 显示全部楼层
我就看出来几个定义的名字不一样,我都统一了,其他的没看出来,还望明鉴
发表于 2014-6-11 08:46 | 显示全部楼层
                Dim db As New Database(False, False)
43.                    'sourceDb.ReadDwgFile("c:\\xxx.dwg", System.IO.FileShare.Read, true, "");
44.                    db.ReadDwgFile(FilePath, System.IO.FileShare.Read, False, "")
------------------------------------
一楼代码里面有没有这些?
 楼主| 发表于 2014-6-11 10:55 | 显示全部楼层
唉!刚开始学二次开发差的太多了,大概明白这段代码的意思了,如若当前文件包含“断路器块”,就直接插入,如果当前文件不包含块,按照路径打开“断路器块”。显然我的“断路器块”是外部文件,所以我之前的代码不可能成功,晚上我试着改一改。谢谢sieben,热心网友。
发表于 2015-2-18 00:09 | 显示全部楼层
hrbustmqc 发表于 2014-6-10 21:53
断路器图块是个单独的文件。这个代码可以执行。跟第一个例子结合后就不能插入了,不知道为什么,请大家帮忙 ...

已经试了,这个办法没问题。谢谢!
发表于 2018-11-29 13:15 | 显示全部楼层
自己仔细看看上下代码有没有什么不同?
发表于 2018-12-10 16:03 | 显示全部楼层
初次二开这个问题必然接触啊,我刚开始也琢磨了好几天
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-20 13:36 , Processed in 0.441107 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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