S
Stuart Coutts (gtx_viper)
Guest
I'm converting all my VBA macros to a dll class library. Due to the number of subs and functions that work on their own and together I had to make a common structure to keep it tidy.
In VBA I would create a module called... "bbb_PublicVariables" ... and under that I would have all my common variables.
And then in a different module called "aaa_MasterFunctions" I would have a function called "mfSetVariables" that would, amazingly enough, set the variables.
At the start of a sub I would call the "mfSetVariables" then when I create a new file or switch to a different file etc I would call on the "mfSetVariables" function again.
In VB I have done the same but the Public variables don't update. It's almost like they are static?
Public Class NCT_Toolkit_Addin_AC
Private Shared myroutine As Action
<CommandMethod("Micromat_DrillFile_Import")>
Public Sub DrillFile_Import()
If mfSetVariables() = False Then Exit Sub
Dim answer = MsgBox("This tool will import a drill file!", vbOKCancel, "Drill File Import Readiness Check")
If answer = vbCancel Then Exit Sub
RetryGetFilePath:
Dim OpenFilePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop
Dim DrillFilePath = mfOpenDialog(OpenFilePath, "Text Based Files (*.txt;*.asc)|*.txt;*.asc|All files (*.*)|*.*")
If TypeName(DrillFilePath) = "Boolean" Then If DrillFilePath = False Then Exit Sub
Dim DrillFile() As Object = mfLoadFile_Txt(DrillFilePath, 1)
If DrillFile Is Nothing Then
MsgBox("Please select another file!", vbOKOnly + vbExclamation, "Selected File is Empty")
GoTo RetryGetFilePath
Else
Micromat_ToolSetup()
mfCreateNewDrawing()
DrawTemplate_Genrad()
Micromat_DrillFile_Read(DrillFile)
End If
End Sub
End Class
Module bbb_PublicVariables
Public acApp As Object
Public acDocMgr As DocumentCollection
Public acActiveDoc As Document
Public acDatabase As Database
Public acTransaction As Transaction
Public acCircle As Circle
Public acText As MText
Public acLine As Line
Public acLayerTable As LayerTable
Public acLayerTable_Record As LayerTableRecord
Public acBlockTable As BlockTable
Public acBlockTable_Record As BlockTableRecord
Public acView As ViewTableRecord
Public acSpace As Integer
Public mfFilePath As Object
Public mfFolderPath As String
Public mfProjNumber As String
Public mfFileName_wExt As String
Public mfFileExt As String
Public mfFileName As String
Public mfFileStatus As String
Public mfTemplatePath As Object
Public Micromat_DrillArray As Object '(0 = Tool# 1 = X 2 = Y, HoleCount)
Public Micromat_ToolArray As Object
end module
Module aaa_MasterFunctions
Function mfSetVariables()
acApp = Application.AcadApplication
acDocMgr = Application.DocumentManager
acActiveDoc = acDocMgr.MdiActiveDocument
acDatabase = acActiveDoc.Database
acSpace = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"))
Return True
End Function
Function mfCreateNewDrawing(Optional TemplateType As Integer = 0) ', Optional MakeDrawingActive As Boolean = True)
aMsgBox("mfCreateNewDrawing1 = " & acActiveDoc.Name)
acDocMgr.MdiActiveDocument = acDocMgr.Add(mfTemplatePath(TemplateType))
acActiveDoc = acDocMgr.MdiActiveDocument
acDatabase = acActiveDoc.Database
MsgBox("mfCreateNewDrawing2 = " & acActiveDoc.Name)
Return True
End Function
End Module
The "mfCreateNewDrawing" above will activate the new drawing but will not update the public variable.
This then messes with the future code by drawing in the previous drawing file.
Am I right is saying that these variables in vb are now static?
Can you suggest a more appropriate structure where I can have a single place to dim common "UPDATABLE" variables?
My Autocad dll will have only about a dozen main subs but when I do my solidworks one... I have hundreds.
Continue reading...
In VBA I would create a module called... "bbb_PublicVariables" ... and under that I would have all my common variables.
And then in a different module called "aaa_MasterFunctions" I would have a function called "mfSetVariables" that would, amazingly enough, set the variables.
At the start of a sub I would call the "mfSetVariables" then when I create a new file or switch to a different file etc I would call on the "mfSetVariables" function again.
In VB I have done the same but the Public variables don't update. It's almost like they are static?
Public Class NCT_Toolkit_Addin_AC
Private Shared myroutine As Action
<CommandMethod("Micromat_DrillFile_Import")>
Public Sub DrillFile_Import()
If mfSetVariables() = False Then Exit Sub
Dim answer = MsgBox("This tool will import a drill file!", vbOKCancel, "Drill File Import Readiness Check")
If answer = vbCancel Then Exit Sub
RetryGetFilePath:
Dim OpenFilePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop
Dim DrillFilePath = mfOpenDialog(OpenFilePath, "Text Based Files (*.txt;*.asc)|*.txt;*.asc|All files (*.*)|*.*")
If TypeName(DrillFilePath) = "Boolean" Then If DrillFilePath = False Then Exit Sub
Dim DrillFile() As Object = mfLoadFile_Txt(DrillFilePath, 1)
If DrillFile Is Nothing Then
MsgBox("Please select another file!", vbOKOnly + vbExclamation, "Selected File is Empty")
GoTo RetryGetFilePath
Else
Micromat_ToolSetup()
mfCreateNewDrawing()
DrawTemplate_Genrad()
Micromat_DrillFile_Read(DrillFile)
End If
End Sub
End Class
Module bbb_PublicVariables
Public acApp As Object
Public acDocMgr As DocumentCollection
Public acActiveDoc As Document
Public acDatabase As Database
Public acTransaction As Transaction
Public acCircle As Circle
Public acText As MText
Public acLine As Line
Public acLayerTable As LayerTable
Public acLayerTable_Record As LayerTableRecord
Public acBlockTable As BlockTable
Public acBlockTable_Record As BlockTableRecord
Public acView As ViewTableRecord
Public acSpace As Integer
Public mfFilePath As Object
Public mfFolderPath As String
Public mfProjNumber As String
Public mfFileName_wExt As String
Public mfFileExt As String
Public mfFileName As String
Public mfFileStatus As String
Public mfTemplatePath As Object
Public Micromat_DrillArray As Object '(0 = Tool# 1 = X 2 = Y, HoleCount)
Public Micromat_ToolArray As Object
end module
Module aaa_MasterFunctions
Function mfSetVariables()
acApp = Application.AcadApplication
acDocMgr = Application.DocumentManager
acActiveDoc = acDocMgr.MdiActiveDocument
acDatabase = acActiveDoc.Database
acSpace = System.Convert.ToInt32(Application.GetSystemVariable("CVPORT"))
Return True
End Function
Function mfCreateNewDrawing(Optional TemplateType As Integer = 0) ', Optional MakeDrawingActive As Boolean = True)
aMsgBox("mfCreateNewDrawing1 = " & acActiveDoc.Name)
acDocMgr.MdiActiveDocument = acDocMgr.Add(mfTemplatePath(TemplateType))
acActiveDoc = acDocMgr.MdiActiveDocument
acDatabase = acActiveDoc.Database
MsgBox("mfCreateNewDrawing2 = " & acActiveDoc.Name)
Return True
End Function
End Module
The "mfCreateNewDrawing" above will activate the new drawing but will not update the public variable.
This then messes with the future code by drawing in the previous drawing file.
Am I right is saying that these variables in vb are now static?
Can you suggest a more appropriate structure where I can have a single place to dim common "UPDATABLE" variables?
My Autocad dll will have only about a dozen main subs but when I do my solidworks one... I have hundreds.
Continue reading...