以下是通过首先使用对话框选择文件来插入块参照的示例。
它是适度基本的;例如,可以通过“跳汰”进一步扩展,以允许在插入过程中进行视觉反馈。这将增加相当多的复杂性。
观看文字换行!
- Public Sub InsertFromFileCommand()
- Dim ofd As OpenFileDialog = New OpenFileDialog()
- ofd.DefaultExt = ".dwg"
- ofd.Filter = "Drawing Files (*.dwg)|*.dwg"
- If ofd.ShowDialog() <> DialogResult.OK Then
- Return
- End If
- Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
- Dim db As Database = doc.Database
- Dim ed As Editor = doc.Editor
- Dim ppo As New PromptPointOptions(vbLf & "Insertion point: ")
- Dim ppr As PromptPointResult = ed.GetPoint(ppo)
- If ppr.Status <> PromptStatus.OK Then
- Return
- End If
- Using xDb As New Database(False, True)
- xDb.ReadDwgFile(ofd.FileName, FileShare.Read, True, Nothing)
- Using tr As Transaction = doc.TransactionManager.StartTransaction()
- Dim name As String = SymbolUtilityServices.GetBlockNameFromInsertPathName(ofd.FileName)
- Dim id As ObjectId = db.Insert(name, xDb, True)
- If id.IsNull Then
- ed.WriteMessage(vbLf & "Failed to insert block")
- Return
- End If
- Dim currSpace As BlockTableRecord = CType(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
- Dim p3d As Point3d = ppr.Value
- Dim coordS As CoordinateSystem3d = New CoordinateSystem3d(p3d, db.Ucsxdir, db.Ucsydir) 'Determine UCS
- Dim insert As New BlockReference(p3d, id)
- insert.Normal = coordS.Zaxis 'Align to UCS
- currSpace.AppendEntity(insert)
- insert.SetDatabaseDefaults()
- tr.AddNewlyCreatedDBObject(insert, True)
- tr.Commit()
- End Using
- End Using
- End Sub
|