Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor Dim doc As Document = Application.DocumentManager.MdiActiveDocument Dim db As Database = doc.Database ' Dim bt as '获取当前的选择集对象 Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection() Dim ss As SelectionSet = ed.SelectImplied().Value Using trans As Transaction = db.TransactionManager.StartTransaction() '循环遍历选择集中的对象
For Each id As ObjectId In ss.GetObjectIds() Dim obj As DBObject = trans.GetObject(id, OpenMode.ForRead) If TypeOf (obj) Is BlockReference Then Dim br As BlockReference = trans.GetObject(id, OpenMode.ForRead) 'CType(obj, BlockReference) Dim btr As BlockTableRecord = trans.GetObject(br.BlockTableRecord, OpenMode.ForRead) If (btr.IsFromExternalReference) Then Dim xrefId As ObjectId = br.BlockTableRecord If (Not xrefId = ObjectId.Null) Then acObjIdColl.Add(xrefId) 'db.DetachXref(xrefId) 'db.UnloadXrefs() ed.WriteMessage(vbCrLf & "Xref:" & btr.Name & " Unload!") End If db.UnloadXrefs(acObjIdColl) trans.Commit() End If End If Next End Using |