Skip to content

Instantly share code, notes, and snippets.

@sancarn
Last active May 5, 2018 19:02
Show Gist options
  • Save sancarn/95cc024887c829077cfd5cccd47f176d to your computer and use it in GitHub Desktop.
Save sancarn/95cc024887c829077cfd5cccd47f176d to your computer and use it in GitHub Desktop.
Runs in ISE, Doesn't run in powershell
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:20 char:20
+ $parent = [System.Windows.Forms.TreeNode]$global:database.Ite ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:27 char:36
+ ... [void]$node.nodes.add([System.Windows.Forms.TreeNode]::new(" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
At C:\Users\sancarn\AppData\Local\Temp\script.ps1:33 char:45
+ ... PSCustomObject]IWDBGetChildren([System.Windows.Forms.TreeNode]$node) ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.TreeNode].
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TypeNotFound
# Called with [DTP]::exec("return ""hello world""")
class DTP {
static [String]$IN = "C:\Users\sancarn\AppData\Local\Temp\IN_19432.txt"
static [String]$OUT = "C:\Users\sancarn\AppData\Local\Temp\OUT_19432.txt"
static [String]exec([String]$script) {
$OLD = (Get-FileHash -Path $([DTP]::OUT)).Hash
$script |Out-File -FilePath $([DTP]::IN) -Encoding ascii
while($OLD -eq (Get-FileHash -Path $([DTP]::OUT)).Hash){
Start-Sleep -Milliseconds 100
}
return Get-Content -Path $([DTP]::OUT)
}
}
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
class API {
static [void]addModelObject($obj){
$parent = [System.Windows.Forms.TreeNode]$global:database.Item($obj.parent)
$node = New-Object System.Windows.Forms.TreeNode
$node.text = $obj.name
$node.Tag = $obj
if($obj.type -eq "MODEL GROUP"){
#add dummy child
[void]$node.nodes.add([System.Windows.Forms.TreeNode]::new("4e84b5c3-1864-4e51-9381-6405f9d68faf"))
}
[void]$parent.nodes.add($node)
[void]$global:database.add($obj.id,$node)
}
static [PSCustomObject]IWDBGetChildren([System.Windows.Forms.TreeNode]$node) {
$cmd = "
require 'json'
iwdb = WSApplication.current_database
mo = iwdb.model_object_from_type_and_id(""$($node.tag.type)"",$($node.tag.id))
arr = []
mo.children.each do |child|
arr.push({
:name=>child.name,
:id=>child.id,
:type=>child.type,
:parent=>child.parent
})
end
return arr.to_json
"
return ConvertFrom-Json $([DTP]::exec($cmd))
}
static [PSCustomObject]IWDBGetRootObjects(){
$cmd = "
require 'json'
iwdb = WSApplication.current_database
arr = []
iwdb.root_model_objects.each do |child|
arr.push({
:name=>child.name,
:id=>child.id,
:type=>child.type,
:parent=>child.parent
})
end
return arr.to_json
"
return ConvertFrom-Json $([DTP]::exec($cmd))
}
}
# DEBUG
$rootObjects = "[{""name"":""Temp1"",""id"":1, ""type"":""MODEL GROUP"",""parent"":0},{""name"":""Temp2"",""id"":2, ""type"":""MODEL GROUP"",""parent"":0}]"
# DIST
# $rootObjects = [API]::IWDBGetRootObjects()
$rootObjects = ConvertFrom-Json $rootObjects
$form = New-Object System.Windows.Forms.Form
$form.Width = 600
$form.Height = 600
$treeView1 = New-Object System.Windows.Forms.TreeView
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 300
$System_Drawing_Size.Height = 300
$treeView1.Size = $System_Drawing_Size
$treeView1.Name = "treeView1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 10
$treeView1.Location = $System_Drawing_Point
$treeView1.DataBindings.DefaultDataSourceUpdateMode = 0
$treeView1.TabIndex = 0
$treeView1.Font = 'Microsoft Sans Serif,14'
$treeView1.add_BeforeExpand({
$node = [System.Windows.Forms.TreeNode]$_.Node
1..$node.Nodes.count | % {$node.Nodes.RemoveAt(0)}
$children = [API]::IWDBGetChildren($node)
ForEach($child in $children){
[void][API]::addModelObject($child)
}
})
$global:database = @{}
$root = New-Object System.Windows.Forms.TreeNode
$root.text = "Master Database"
$root.Name = "Master Database"
$root.Tag = "root"
[void]$treeView1.Nodes.add($root)
[void]$database.Add(0,$root)
ForEach($obj in $rootObjects){
[void][API]::addModelObject($obj)
}
$okButton = New-Object System.Windows.Forms.Button
$okButton.width = 300
$okButton.height = 100
$okButton.top = 310
$okButton.left = 10
$okButton.add_click({
$json = ConvertTo-Json $($treeView1.SelectedNode.Tag.id)
Write-Host $json
})
[void]$form.Controls.Add($treeView1)
[void]$form.Controls.Add($okButton)
[void]$Form.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment