Message 1 of 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
i have a combo box which choose from 3 items one is blank. the triggered item changed event will erase the old set of corresponding xdata in the selected object and populate with new ones read from different text boxes. i was able to set the xdata for the first time ok. but when trying to dispose it won't dispose.
Imports System.Runtime
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports AcApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports Create_Pipe_Data.ActiveDrawing
Public Class Container1
Public oSelectedObjID As ObjectId
Public WithEvents doc As Document
Public AppName As String = "CreatePipeData"
Private m_DocData As MyDocData = Nothing
Friend Shared ps As Autodesk.AutoCAD.Windows.PaletteSet = Nothing
Dim myPalette As Container1
<CommandMethod("tp", CommandFlags.Transparent)> _
Public Sub DoIt()
If m_DocData Is Nothing Then m_DocData = New MyDocData
AddHandler AcApp.DocumentManager.DocumentActivated, AddressOf Me.DocumentManager_DocumentActivated
AddHandler AcApp.DocumentManager.DocumentToBeDeactivated, AddressOf Me.DocumentManager_DocumentToBeDeactivated
'check to see if paletteset is already created
If ps Is Nothing Then
'no so create it
ps = New Autodesk.AutoCAD.Windows.PaletteSet("Create Pipe Data")
'create new instance of user control
myPalette = New Container1()
'add it to the paletteset
ps.Add("Create Pipe Data", myPalette)
End If
'turn it on
ps.Visible = True
End Sub
Private Sub AddRegAppTableRecord(ByVal regAppName As String)
Dim db As Database = doc.Database
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Try
Using tr
Dim rat As RegAppTable = DirectCast(tr.GetObject(db.RegAppTableId, OpenMode.ForRead, False), RegAppTable)
If Not rat.Has(regAppName) Then
rat.UpgradeOpen()
Dim ratr As New RegAppTableRecord()
ratr.Name = regAppName
rat.Add(ratr)
tr.AddNewlyCreatedDBObject(ratr, True)
End If
tr.Commit()
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Container1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If m_DocData Is Nothing Then m_DocData = New MyDocData
doc = Application.DocumentManager.MdiActiveDocument
'doc.LockDocument(DocumentLockMode.Write, Nothing, Nothing, True)
' AddHandler doc.ImpliedSelectionChanged, AddressOf ObjectSelected
AddHandler AcApp.DocumentManager.DocumentActivated, AddressOf Me.DocumentManager_DocumentActivated
AddHandler AcApp.DocumentManager.DocumentToBeDeactivated, AddressOf Me.DocumentManager_DocumentToBeDeactivated
'AddHandler doc.ImpliedSelectionChanged, AddressOf ObjectSelected
End Sub
Private Sub DocumentManager_DocumentActivated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
'display the current active document
Try
If Not m_DocData Is Nothing Then
txtADwg.Text = m_DocData.Current.Stuff
doc = Application.DocumentManager.MdiActiveDocument
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub DocumentManager_DocumentToBeDeactivated(ByVal sender As Object, ByVal e As DocumentCollectionEventArgs)
'store the current contents
Try
If Not m_DocData Is Nothing Then
m_DocData.Current.Stuff = txtADwg.Text
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ObjectSelectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles doc.ImpliedSelectionChanged
ObjectSelected()
End Sub
Private Sub ObjectSelected()
'Dim myDB As Database = HostApplicationServices.WorkingDatabase
Dim myEditor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim PossibleSelectionSet As PromptSelectionResult = myEditor.SelectImplied
Dim mySelectionSet As SelectionSet = Nothing
Dim myObjectIDs As New List(Of ObjectId)
Dim oSelectedObj As DBObject
If PossibleSelectionSet.Status = PromptStatus.OK Then
Using tr As Transaction = doc.TransactionManager.StartTransaction
mySelectionSet = PossibleSelectionSet.Value
For Each mySelObj As SelectedObject In mySelectionSet
myObjectIDs.Add(mySelObj.ObjectId)
Next
End Using
End If
'SET TO LAST OBJECT SELECTED
If myObjectIDs.Count <> 0 Then
Try
Using oLock As DocumentLock = doc.LockDocument() '(DocumentLockMode.Write, Nothing, Nothing, True)
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
oSelectedObj = tr.GetObject(myObjectIDs.Item(myObjectIDs.Count - 1), OpenMode.ForRead, False)
oSelectedObjID = oSelectedObj.ObjectId
'AFTER SELECT OBJECT, POPULATE FORM
PopulateXdataToForm(oSelectedObjID)
End Using
End Using
GroupBox1.Enabled = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
GroupBox1.Enabled = False
End If
End Sub
Private Sub txtType_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtType.MouseLeave
PopulateFormDataToObjectXdata(oSelectedObjID)
End Sub
Private Sub txtMaterial_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMaterial.MouseLeave, TextBox6.MouseLeave, TextBox5.MouseLeave, TextBox4.MouseLeave, TextBox3.MouseLeave, TextBox2.MouseLeave, TextBox1.MouseLeave
PopulateFormDataToObjectXdata(oSelectedObjID)
End Sub
Private Sub txtLength_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLength.MouseLeave
PopulateFormDataToObjectXdata(oSelectedObjID)
End Sub
Private Sub txtInvOut_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInvOut.MouseLeave
PopulateFormDataToObjectXdata(oSelectedObjID)
End Sub
Private Sub txtInvIn_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInvIn.MouseLeave
PopulateFormDataToObjectXdata(oSelectedObjID)
End Sub
Private Sub txtGrade_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtGrade.MouseLeave
PopulateFormDataToObjectXdata(oSelectedObjID)
End Sub
Private Sub TxtDiameter_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDiameter.MouseLeave
PopulateFormDataToObjectXdata(oSelectedObjID)
End Sub
Private Sub cmbPipeOrStructure_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPipeOrStructure.SelectionChangeCommitted
PipeStructureChanged()
End Sub
Private Sub PipeStructureChanged()
If cmbPipeOrStructure.Text = "Pipe" Then
gpbPipe.Enabled = True
gpbStructure.Enabled = False
Using oLock As DocumentLock = doc.LockDocument()
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
'Dim oSelectedObj As DBObject = tr.GetObject(oSelectedObjID, OpenMode.ForWrite, False)
'If oSelectedObj.XData <> Nothing Then
' Dim rb As ResultBuffer = oSelectedObj.XData
' rb.Dispose()
'End If
'tr.Commit()
PopulateFormDataToObjectXdata(oSelectedObjID)
End Using
End Using
ElseIf cmbPipeOrStructure.Text = "Structure" Then
gpbStructure.Enabled = True
gpbPipe.Enabled = False
Using oLock As DocumentLock = doc.LockDocument()
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
'Dim oSelectedObj As DBObject = tr.GetObject(oSelectedObjID, OpenMode.ForWrite, False)
'If oSelectedObj.XData <> Nothing Then
' Dim rb As ResultBuffer = oSelectedObj.XData
' rb.Dispose()
'End If
'tr.Commit()
PopulateFormDataToObjectXdata(oSelectedObjID)
End Using
End Using
ElseIf cmbPipeOrStructure.Text = "" Then
ClearTextbox()
gpbStructure.Enabled = False
gpbPipe.Enabled = False
Using oLock As DocumentLock = doc.LockDocument()
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
'Dim oSelectedObj As DBObject = tr.GetObject(oSelectedObjID, OpenMode.ForWrite, False)
'If oSelectedObj.XData <> Nothing Then
' Dim rb As ResultBuffer = oSelectedObj.XData
' rb.Dispose()
'End If
'tr.Commit()
PopulateFormDataToObjectXdata(oSelectedObjID)
End Using
End Using
End If
End Sub
Private Sub ClearTextbox()
txtGrade.Text = ""
txtInvIn.Text = ""
txtInvOut.Text = ""
txtDiameter.Text = ""
txtLength.Text = ""
txtMaterial.Text = ""
txtType.Text = ""
End Sub
'ADD PROPERTY FROM RESULTBUFFER TO TEXTBOX
Private Sub PopulateXdataToForm(ByVal oSelectedObjID As ObjectId)
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
Dim oSelectedObj As DBObject = tr.GetObject(oSelectedObjID, OpenMode.ForWrite, False)
If oSelectedObj.XData = Nothing Then
'IF FRESH OBJECT, CLEAR FORM
ClearTextbox()
Else
'check to see if created by same application
Dim oTypeValueApp As TypedValue = oSelectedObj.XData.AsArray(0)
If oTypeValueApp.Value = AppName Then
For Each oTypedValue In oSelectedObj.XData.AsArray
If oTypedValue.Value <> AppName Then
Dim arSplitPipeData() As String = Split(oTypedValue.Value, ",")
Select Case arSplitPipeData(0).ToString
Case "pipe-structure"
Select Case arSplitPipeData(1).ToString
Case ""
cmbPipeOrStructure.Text = cmbPipeOrStructure.Items(0)
gpbStructure.Enabled = False
gpbPipe.Enabled = False
Case "Pipe"
cmbPipeOrStructure.Text = cmbPipeOrStructure.Items(1)
gpbStructure.Enabled = False
gpbPipe.Enabled = True
Case "Structure"
cmbPipeOrStructure.Text = cmbPipeOrStructure.Items(2)
gpbStructure.Enabled = True
gpbPipe.Enabled = False
End Select
Case "type"
txtType.Text = arSplitPipeData(1).ToString
Case "material"
txtMaterial.Text = arSplitPipeData(1).ToString
Case "diameter"
txtDiameter.Text = arSplitPipeData(1).ToString
Case "invertin"
txtInvIn.Text = arSplitPipeData(1).ToString
Case "invertout"
txtInvOut.Text = arSplitPipeData(1).ToString
Case "length"
txtLength.Text = arSplitPipeData(1).ToString
Case "grade"
txtGrade.Text = arSplitPipeData(1).ToString
End Select
End If
Next
End If
End If
' doc.LockDocument.Dispose()
End Using
End Sub
Private Sub PopulateFormDataToObjectXdata(ByVal oSelectedObjID As ObjectId)
Try
Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using oLock As DocumentLock = doc.LockDocument()
Using tr
'doc.LockDocument() '(DocumentLockMode.Write, Nothing, Nothing, True)
Dim oSelectedObj As DBObject = tr.GetObject(oSelectedObjID, OpenMode.ForWrite, False)
'clear xdata
If cmbPipeOrStructure.Text = "" Then
'clear info
oSelectedObj.XData.Dispose()
Exit Sub
ElseIf oSelectedObj.XData <> Nothing Then
oSelectedObj.XData.Dispose()
End If
AddRegAppTableRecord(AppName)
Dim rb As New ResultBuffer
rb.Add(New TypedValue(1001, AppName))
rb.Add(New TypedValue(1000, "pipe-structure," & cmbPipeOrStructure.Text))
rb.Add(New TypedValue(1000, "type," & txtType.Text))
rb.Add(New TypedValue(1000, "material," & txtMaterial.Text))
rb.Add(New TypedValue(1000, "diameter," & txtDiameter.Text))
rb.Add(New TypedValue(1000, "invertin," & txtInvIn.Text))
rb.Add(New TypedValue(1000, "invertout," & txtInvOut.Text))
rb.Add(New TypedValue(1000, "length," & txtLength.Text))
rb.Add(New TypedValue(1000, "grade," & txtGrade.Text))
oSelectedObj.XData = rb
rb.Dispose()
tr.Commit()
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Stantec
Dell Precision 5530, Prism M320PU, C3D 14/17/19
Dell Precision 5530, Prism M320PU, C3D 14/17/19
Solved! Go to Solution.