Hrmm i think i missing something, i was unable to get this to work using either way. although i am thinking selection set my be the way to go afterall as i want to be able to delete these blocks even if they were already in teh drawing at the start of teh acad session (if i started placing, but then interrupted and had to come back later). so heres the insertion code as it stood before i attempted your suggestions. thoughts? and thanks again for the help, i am definetly learning things from this
Code:
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Public Class StallCounterInterface
Private Sub StallCounterInterface_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Counter()
End Sub
Public Sub blocktest(ByVal BlockName As String)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
'test if block exists
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
'insert source file if block not found
If Not bt.Has(BlockName) Then
Dim origin As New Point3d(0, 0, 0)
Dim filename As String = "C:\mkacadd_Hybrid\Civil3D\CAD data\Support\Blocks\StallCount.dwg"
ed.WriteMessage(vbLf & BlockName & " block does not exist, now inserting source file!")
Using dbdwg As New Database(False, True)
dbdwg.ReadDwgFile(filename, IO.FileShare.Read, True, "")
Dim blkid As ObjectId
blkid = db.Insert(filename, dbdwg, True)
Dim obr As New BlockReference(origin, blkid)
Dim obtr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
obtr.AppendEntity(obr)
tr.AddNewlyCreatedDBObject(obr, True)
'erases source file
obr.Erase(True)
tr.Commit()
End Using
End If
End Using
'initiate loop for cotninous block placement unitl user cancels
Dim LoopControl As Boolean = True
Do While LoopControl
doc.LockDocument()
Me.Hide()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim id As ObjectId = bt(BlockName)
' Get insertion point
Dim ppo As New PromptPointOptions(vbLf & "Specify insertion point, press [ESC] to return:")
Dim ppr As PromptPointResult = ed.GetPoint(ppo)
' Exit if the user presses ESC or cancels the command
If ppr.Status = PromptStatus.Cancel Then Exit Do
If ppr.Status = PromptStatus.OK Then
Dim pts As Point3d = ppr.Value
Dim ptstr As New Point2d(ppr.Value.X, ppr.Value.Y)
Dim ppor As New PromptPointOptions(vbLf & "Specify Rotation point:")
'rubber band to show rotation
ppor.UseBasePoint = True
ppor.BasePoint = pts
Dim pprr As PromptPointResult = ed.GetPoint(ppor)
Dim ptend As New Point2d(pprr.Value.X, pprr.Value.Y)
' Create a block reference
Dim br As New BlockReference(pts, id)
' Get Model space
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
' Add the block reference to Model space
btr.AppendEntity(br)
tr.AddNewlyCreatedDBObject(br, True)
'Rotate block based on insertion and rotation points
Dim curucsmat As Matrix3d = ed.CurrentUserCoordinateSystem
Dim curUCS As CoordinateSystem3d = curucsmat.CoordinateSystem3d
Dim ang = ptstr.GetVectorTo(ptend).Angle.ToString()
Dim rot = Math.Truncate(ang * 10000) / 10000
br.TransformBy(Matrix3d.Rotation(rot, curUCS.Zaxis, New Point3d(ppr.Value.X, ppr.Value.Y, 0)))
tr.Commit()
End If
End Using
Loop
Counter()
Me.Show()
End Sub
Private Sub Counter()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim numS As Integer = 0
Dim numC As Integer = 0
Dim numH As Integer = 0
Dim numV As Integer = 0
Dim numT As Integer = 0
Using tr As Transaction = doc.TransactionManager.StartTransaction()
Dim mytvs(0) As TypedValue
mytvs(0) = New TypedValue(0, "Insert")
Dim myfilter As New SelectionFilter(mytvs)
Dim mypsr As PromptSelectionResult = ed.SelectAll(myfilter)
If mypsr.Status = PromptStatus.OK Then
Dim sset As SelectionSet = mypsr.Value
For Each id As ObjectId In mypsr.Value.GetObjectIds
Dim br As BlockReference = id.GetObject(OpenMode.ForRead)
Select Case br.Name
Case "Standard"
numS = numS + 1
Case "Compact"
numC = numC + 1
Case "Handicap"
numH = numH + 1
Case "Van"
numV = numV + 1
Case Else
End Select
numT = numS + numC + numH + numV
Next
End If
tr.Commit()
End Using
StdCnt.Text = numS
ComCnt.Text = numC
HanCnt.Text = numH
VanCnt.Text = numV
TtlCnt.Text = numT
End Sub
Private Sub eraser()
End Sub
Private Sub StdBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StdBtn.Click
Dim BlockName As String = "Standard"
blocktest(BlockName)
End Sub
Private Sub CompBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompBtn.Click
Dim BlockName As String = "Compact"
blocktest(BlockName)
End Sub
Private Sub VanBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VanBtn.Click
Dim BlockName As String = "Van"
blocktest(BlockName)
End Sub
Private Sub HandBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HandBtn.Click
Dim BlockName As String = "Handicap"
blocktest(BlockName)
End Sub
Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitBtn.Click
Me.Close()
'set current layer to layer before app was run.
Application.SetSystemVariable("clayer", Laycurrent.laycur)
End Sub
Private Sub Erasebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Erasebtn.Click
eraser()
End Sub
End Class