Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
<Assembly: CommandClass(GetType(AutoCAD_VB_plug_in1.MyCommands))>
Namespace AutoCAD_VB_plug_in1
Public Class MyCommands
<CommandMethod("test3")> _
Public Sub test3()
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim docloc As DocumentLock = acDoc.LockDocument()
Using docloc
Dim wCol As ObjectIdCollection = CrEnt()
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim reg1, reg2, reg3 As Region
reg1 = acTrans.GetObject(wCol(0), OpenMode.ForRead).Clone
reg2 = acTrans.GetObject(wCol(1), OpenMode.ForRead).Clone
' Так как вычитать имеет смысл только из большей области меньшую
' то проведём небольшое исследование и упорядочим области
If (reg2.Area > reg1.Area) Then
reg3 = reg2 : reg2 = reg1
Else
reg3 = reg1
End If
reg3.BooleanOperation(BooleanOperationType.BoolSubtract, reg2)
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
acBlkTblRec.AppendEntity(reg3)
reg3.Close()
acTrans.Commit()
End Using
End Using
acCurDb = Nothing
acDoc = Nothing
End Sub
Public Function CrEnt() As ObjectIdCollection
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Dim res As New ObjectIdCollection
'Dim docloc As MyAcAs.DocumentLock = acDoc.LockDocument()
'Using docloc
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
'
Dim wCol As DBObjectCollection = CrReg(CrPoly())
For I = 0 To wCol.Count - 1
acBlkTblRec.AppendEntity(wCol(I))
acTrans.AddNewlyCreatedDBObject(wCol(I), True)
res.Add(wCol(I).ObjectId)
Next I
acTrans.Commit()
End Using
acCurDb = Nothing
acDoc = Nothing
Return res
End Function
Public Function CrPoly() As Polyline
Dim res As New Polyline()
'
res.AddVertexAt(0, New Point2d(0, 0), 0, 0, 0)
res.AddVertexAt(1, New Point2d(0, 10), 0, 0, 0)
res.AddVertexAt(2, New Point2d(5, 15), 0, 0, 0)
res.AddVertexAt(3, New Point2d(45, 15), 0, 0, 0)
res.AddVertexAt(4, New Point2d(45, 0), 0, 0, 0)
res.Closed = True
'
Return res
End Function
Public Function CrReg(wPoly As Polyline) As DBObjectCollection
Dim wCol As New DBObjectCollection
wCol.Add(wPoly)
'Dim res As New Region
wCol.Add(New Circle(New Point3d(35, 7, 0), New Vector3d(0, 0, 1), 2))
wCol = Region.CreateFromCurves(wCol)
Return wCol
End Function
End Class
End Namespace