Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports System.IO
<Assembly: ExtensionApplication(GetType(savehandler_2013.MyPlugin))>
Namespace savehandler_2013
Public Class MyPlugin
Implements IExtensionApplication
Private _DocumentManager As DocumentCollection
Private Shared _Controllers As Dictionary(Of Document, PerDocController)
Public Sub Initialize() Implements IExtensionApplication.Initialize
'#### document controller ####
_DocumentManager = Application.DocumentManager
_Controllers = New Dictionary(Of Document, PerDocController)
For Each document As Document In _DocumentManager
_Controllers.Add(document, New PerDocController(document))
Next
AddHandler _DocumentManager.DocumentActivated, AddressOf DocumentActivated
AddHandler _DocumentManager.DocumentCreated, AddressOf DocumentCreated
AddHandler _DocumentManager.DocumentToBeDestroyed, AddressOf DocumentToBeDestroyed
End Sub
Public Sub Terminate() Implements IExtensionApplication.Terminate
RemoveHandler _DocumentManager.DocumentActivated, AddressOf DocumentActivated
RemoveHandler _DocumentManager.DocumentCreated, AddressOf DocumentCreated
RemoveHandler _DocumentManager.DocumentToBeDestroyed, AddressOf DocumentToBeDestroyed
End Sub
Private Sub DocumentActivated(sender As Object, e As DocumentCollectionEventArgs)
If Not (_Controllers.ContainsKey(e.Document)) Then
_Controllers.Add(e.Document, New PerDocController(e.Document))
e.Document.Editor.WriteMessage(vbCrLf + " - Document Activated! ")
End If
End Sub
Private Sub DocumentToBeDestroyed(sender As Object, e As DocumentCollectionEventArgs)
If (_Controllers.ContainsKey(e.Document)) Then
_Controllers(e.Document).Destroy()
_Controllers.Remove(e.Document)
End If
End Sub
Private Sub DocumentCreated(sender As Object, e As DocumentCollectionEventArgs)
If Not (_Controllers.ContainsKey(e.Document)) Then
_Controllers.Add(e.Document, New PerDocController(e.Document))
e.Document.Editor.WriteMessage(vbCrLf + " - Document Created! ")
End If
End Sub
End Class
Public Class PerDocController
Private _Document As Document
Public Sub New(ByVal document As Document)
_Document = document
If Not Getblockid("sqldataholder") = ObjectId.Null Then
AddHandler _Document.Database.BeginSave, AddressOf BeginSave
AddHandler _Document.Database.SaveComplete, AddressOf SaveComplete
_Document.Editor.WriteMessage(vbCrLf + " - Savehandler Added! ")
End If
End Sub
Public Sub Destroy()
RemoveHandler _Document.Database.BeginSave, AddressOf BeginSave
RemoveHandler _Document.Database.SaveComplete, AddressOf SaveComplete
End Sub
Function Getblockid(ByVal blockName As String) As ObjectId
Dim RetBlkId As ObjectId = ObjectId.Null
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Try
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim bt As BlockTable = db.BlockTableId.GetObject(OpenMode.ForRead)
Dim btr As BlockTableRecord = bt(blockName).GetObject(OpenMode.ForRead)
Dim brefCount As Integer = 0
For Each objId As ObjectId In btr.GetBlockReferenceIds(True, True)
brefCount = btr.GetBlockReferenceIds(True, True).Count
If brefCount > 1 Then
ed.WriteMessage(vbCrLf & " - mutiple blocks found, check drawing!")
Exit Function
ElseIf brefCount = 0 Then
ed.WriteMessage(vbCrLf & " - Block, " & blockName & " not found on screen")
Exit Function
Else
Dim ent As Entity = objId.GetObject(OpenMode.ForRead)
RetBlkId = ent.ObjectId
End If
Next
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(vbCrLf & " - Block, " & blockName & " not found")
End Try
Return RetBlkId
End Function
Sub SetAttribute(ByVal blockId As ObjectId, ByVal attTag As String, ByVal attVal As String)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Try
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim br As BlockReference
Dim ac As AttributeCollection
br = blockId.GetObject(OpenMode.ForWrite)
ac = br.AttributeCollection
Dim ent As ObjectId
Dim ar As AttributeReference
For Each ent In ac
ar = ent.GetObject(OpenMode.ForWrite)
If String.Compare(ar.Tag.ToUpper, attTag.ToUpper, True) = 0 Then
ar.TextString = attVal
End If
Next
tr.Commit()
tr.Dispose()
End Using
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(vbCrLf & "Error: {0}" & vbCrLf & "Stack trace: {1}", ex.Message, ex.StackTrace)
End Try
End Sub
Private Sub BeginSave(sender As Object, e As DatabaseIOEventArgs)
_Document.Editor.WriteMessage(vbCrLf + " - Begin Save! ")
If Not Getblockid("sqldataholder") = ObjectId.Null Then
_Document.Editor.WriteMessage(vbCrLf + " - step 1! ")
Try
SetCustomProperty("sql", "true")
Catch ex As Autodesk.AutoCAD.Runtime.Exception
_Document.Editor.WriteMessage(vbCrLf & "Error: {0}" & vbCrLf & "Stack trace: {1}", ex.Message, ex.StackTrace)
End Try
_Document.Editor.WriteMessage(vbCrLf + " - step 2! ")
Try
SetCustomProperty("user", Environ$("USERNAME"))
Catch ex As Autodesk.AutoCAD.Runtime.Exception
_Document.Editor.WriteMessage(vbCrLf & "Error: {0}" & vbCrLf & "Stack trace: {1}", ex.Message, ex.StackTrace)
End Try
_Document.Editor.WriteMessage(vbCrLf + " - step 3! ")
Try
SetCustomProperty
("DATEUPD",
Format(Date.Now,
"dd-MM-yy")) Catch ex As Autodesk.AutoCAD.Runtime.Exception
_Document.Editor.WriteMessage(vbCrLf & "Error: {0}" & vbCrLf & "Stack trace: {1}", ex.Message, ex.StackTrace)
End Try
_Document.Editor.WriteMessage(vbCrLf + " - step 4! ")
Try
SetAttribute(Getblockid("testblock"), "tag", "value")
Catch ex As Autodesk.AutoCAD.Runtime.Exception
_Document.Editor.WriteMessage(vbCrLf & "Error: {0}" & vbCrLf & "Stack trace: {1}", ex.Message, ex.StackTrace)
End Try
End If
'' send block data to sql db.
'sendtosqldb("sqldataholder")
Dim w As StreamWriter = File.AppendText("c:\temp\savehandlerlog.txt")
logger(_Document.Name, w)
End Sub
Private Sub SaveComplete(sender As Object, e As DatabaseIOEventArgs)
_Document.Editor.WriteMessage(vbCrLf + " - Save Complete! ")
End Sub
Private Sub logger(ByVal logMessage As String, ByVal w As TextWriter)
w.Write(ControlChars.CrLf & "Log Entry : ")
w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString())
w.WriteLine(" :")
w.WriteLine(" :{0}", logMessage)
w.WriteLine("-------------------------------")
' Update the underlying file.
w.Flush()
End Sub
Sub SetCustomProperty(ByVal Name As String, ByVal Value As String)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim Builder As DatabaseSummaryInfoBuilder = New DatabaseSummaryInfoBuilder(db.SummaryInfo)
If Builder.CustomPropertyTable.Contains(Name) Then
Builder.CustomPropertyTable.Item(Name) = Value
Else
Builder.CustomPropertyTable.Add(Name, Value)
End If
db.SummaryInfo = Builder.ToDatabaseSummaryInfo
End Sub
End Class
End Namespace