- 积分
- 549
- 明经币
- 个
- 注册时间
- 2014-5-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2014-6-10 21:53:30
|
显示全部楼层
- Imports Autodesk.AutoCAD.Runtime
- Imports Autodesk.AutoCAD.ApplicationServices
- Imports Autodesk.AutoCAD.DatabaseServices
- Imports Autodesk.AutoCAD.Geometry
- Imports Autodesk.AutoCAD.EditorInput
- Public Class Class1
-
- <CommandMethod("InserBlock")> _
- Public Sub InserBlock()
- Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
- Dim acDatabase As Database = acDoc.Database
- Dim acEditor As Editor = acDoc.Editor
- Dim pPtRes As PromptPointResult
- Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
- pPtOpts.Message = vbLf & "Insert Point of Block:"
- pPtRes = acEditor.GetPoint(pPtOpts)
- Dim InsertPt As Point3d = pPtRes.Value
- If pPtRes.Status = PromptStatus.Cancel Then
- Exit Sub
- End If
- Using (acDoc.LockDocument)
- Using trans As Transaction = acDatabase.TransactionManager.StartTransaction
- Dim acBlockTable As BlockTable = trans.GetObject(acDatabase.BlockTableId, OpenMode.ForWrite)
- Dim FilePath As String = "G:\断路器.dwg"
- Dim FileName As String = "断路器.dwg"
- Dim BlockName As String = "断路器"
- If acBlockTable.Has(BlockName) = True Then
- Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
- Block.ScaleFactors = New Scale3d(1, 1, 1)
- Block.Rotation = 0
- Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
- ModelSpace.AppendEntity(Block)
- trans.AddNewlyCreatedDBObject(Block, True)
- trans.Commit()
- Else
- Dim db As New Database(False, False)
- 'sourceDb.ReadDwgFile("c:\\xxx.dwg", System.IO.FileShare.Read, true, "");
- db.ReadDwgFile(FilePath, System.IO.FileShare.Read, False, "")
- Dim id As ObjectId = acDatabase.Insert(FileName, db, False)
- If id.IsValid = False Then
- Exit Sub
- End If
- Dim Block As BlockReference = New BlockReference(InsertPt, acBlockTable(BlockName))
- Block.ScaleFactors = New Scale3d(1, 1, 1)
- Block.Rotation = 0
- Dim ModelSpace As BlockTableRecord = trans.GetObject(acDatabase.CurrentSpaceId, OpenMode.ForWrite)
- ModelSpace.AppendEntity(Block)
- trans.AddNewlyCreatedDBObject(Block, True)
- trans.Commit()
- End If
- End Using
- End Using
- End Sub
- End Class
断路器图块是个单独的文件。这个代码可以执行。跟第一个例子结合后就不能插入了,不知道为什么,请大家帮忙看看 |
|