Update VBA to iLogic for extracting command icons

Update VBA to iLogic for extracting command icons

pball
Mentor Mentor
434 Views
5 Replies
Message 1 of 6

Update VBA to iLogic for extracting command icons

pball
Mentor
Mentor

I don't have VBA installed as I didn't install after my previous upgrade since it doesn't get installed by default any more. So I'm working on updating some little snippets to iLogic. This snippet is a pain and even two AIs can't properly make it work. The problem is the StdFunctions is a VBA thing that doesn't exist in .Net it seems. Anyone know how to get a replacement command that works in iLogic?

Sub ExtractBuiltInIconSample()
    Dim oDef1 As ButtonDefinition
    Dim Icon As String
    'Change Icon to command name
    Icon = "AppParametersCmd"
    Set oDef1 = ThisApplication.CommandManager.ControlDefinitions.Item(Icon)
    
    Dim oIcon As IPictureDisp
    Set oIcon = oDef1.LargeIcon
    
    StdFunctions.SavePicture oIcon, "C:\misc\" & Icon & ".ico"
End Sub

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Accepted solutions (1)
435 Views
5 Replies
Replies (5)
Message 2 of 6

Curtis_Waguespack
Consultant
Consultant
0 Likes
Message 3 of 6

g.georgiades
Advocate
Advocate
Accepted solution

@pball Here is a iLogic I use to dump all icons.

I wrote it a long time ago - but I guess some commands had invalid characters for windows hence the regex to clean them.

 

Imports Inventor
Imports System.Drawing
Imports System.IO
Imports System.Runtime.InteropServices
Imports Autodesk.iLogic.Interfaces

'Block For iLogic AddRefernce calls
#If False Then 'Hides VB.Net Compiler Error in Visual Studio, but still works with iLogic
AddReference "System.Drawing.dll"
AddReference "stdole.dll"
#End If

'''''''''''''''''''''''''''''''''''''''
''' Author: Greg G.
'''''''''''''''''''''''''''''''''''''''
''' Dumps all images to file
''' renames invalid windows names for files
'''
'''

Public Class commandimagedump
	Sub main()
		Dim cm As ControlDefinitions = ThisApplication.CommandManager.ControlDefinitions

		Dim fpath = "C:\Workspace\CM Img Dump\"
		Directory.CreateDirectory(fpath)

		Dim kl As Boolean = False
		For Each c As ControlDefinition In cm
			If kl Then Exit For 'allows for debugger loop kill

			Dim ico As stdole.IPictureDisp
			Try
				If c.LargeIcon IsNot Nothing Then ico = c.LargeIcon 'with IPictureDisp, have to wrap in try catch as cheking nullable causes error.
			Catch
				Try
					If c.StandardIcon IsNot Nothing Then ico = c.StandardIcon
				Catch
					Continue For
				End Try
			End Try

			Using img As Image = Icon.FromHandle(ico.Handle).ToBitmap 'just saving icon doesnt retain all collor? bitmap seems to save colors
				Dim cname = System.Text.RegularExpressions.Regex.Replace(c.InternalName, "[!@#\$%\^&\*?|<>+\-/\\\[\];'`~{}:\""""\.]", "_", System.Text.RegularExpressions.RegexOptions.CultureInvariant Or System.Text.RegularExpressions.RegexOptions.Compiled)
				img.Save(fpath & cname & ".png") 'saves as png?
			End Using
		Next
	End Sub
End Class

 

Message 4 of 6

pball
Mentor
Mentor

Thanks for sharing that @g.georgiades. That helps a lot. 

I realized after I posted this I already had some ilogic code for saving thumbnails, but playing with that just now either I was too sloppy or something about thumbnails is different. Oh well this works great.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor

Thanks @g.georgiades, I liked that too.  I was familiar with a couple other ways of obtaining the Image object, then using the Image.Save method, but I do not recall seeing that Icon.FromHandle(IPictureDisp.Handle).ToBitmap Method combination before.  I like it.  The Regex portion is a bit hard to read, so I usually use something like the following for deriving the file name from the ControlDefinition.InternalName property value.  Maybe you will like it too.  It is likely not as efficient as using Regex, but is definitely shorter, and a little bit easier to read/understand later.

'replace any invalid file name characters with a single space
fileName = String.Join(" ", cd.InternalName.Split(System.IO.Path.GetInvalidFileNameChars()))

I used both to quickly create a similar Boolean Function for processing a single ControlDefinition to export one of its icons as a PNG.  I will post that here also, for others to reference as well.  Naturally, I designed it in a way it could be used in a regular iLogic rule, but I did take the time to add in some helpful method & and method parameter documentation.  Then added a simplistic Sub Main block in there so that folks could quickly & easily test it.

Imports Inventor
Imports System.Drawing
Imports System.IO
Imports System.Runtime.InteropServices
Imports Autodesk.iLogic.Interfaces
AddReference "System.Drawing.dll"
AddReference "stdole.dll"

Sub Main
	Dim oCD As Inventor.ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.Item("iLogic.Configuration")
	Dim sImage_File As String
	Dim bWorked As Boolean = SaveControlDefinitionIconToImageFile(oCD, , , sImage_File)
	Logger.Info(vbCrLf & "Image File:  " & sImage_File)
End Sub

''' <summary>
''' Saves either the ControlDefinition's Large or Standard Icon as an external PNG file
''' </summary>
''' <param name="cd">The source Inventor.ControlDefinition to get the Icon from</param>
''' <param name="filePath">The suggested file path for the new PNG file</param>
''' <param name="fileName">The suggested file name, without extension, for the new PNG file</param>
''' <param name="FullFileName">Returns the resulting full path & file name, with extension</param>
''' <returns>A Boolean indicating if this method was successful (True = successful)</returns>
Public Function SaveControlDefinitionIconToImageFile(ByVal cd As Inventor.ControlDefinition, _
	Optional ByVal filePath As String = Nothing, _
	Optional ByVal fileName As String = Nothing, _
	Optional ByRef FullFileName As String = Nothing) As Boolean
	
	'<<< verify or set filePath
	If String.IsNullOrWhiteSpace(filePath) Then
		filePath = "C:\Temp\Inventor ControlDefinition Icon Image Files\"
	End If
	'<<< make sure filePath exists
	If Not System.IO.Directory.Exists(filePath) Then
		Try
			System.IO.Directory.CreateDirectory(filePath)
		Catch
			Logger.Error(vbCrLf & "Could Not Create Following Directory:  " & vbCrLf & filePath)
			Return False
		End Try
	End If
	'<<< verify or set fileName
	If String.IsNullOrWhiteSpace(fileName) Then
		'replace any invalid file name characters with a single space
		fileName = String.Join(" ", cd.InternalName.Split(System.IO.Path.GetInvalidFileNameChars()))
	End If
	'<<< combine filePath, fileName, and fileExtension to set value of ByRef FullFileName param
	FullFileName = System.IO.Path.Combine(filePath, fileName & ".png")
	'<<< try to obtain image data from one of the ControlDefinition's Icons
	Dim oIcon As stdole.IPictureDisp = Nothing
	Try : If (cd.LargeIcon IsNot Nothing) Then oIcon = cd.LargeIcon
	Catch : End Try
	If oIcon Is Nothing Then
		Try : If (cd.StandardIcon IsNot Nothing) Then oIcon = cd.StandardIcon
		Catch : End Try
	End If
	If oIcon Is Nothing Then Return False
	'<<< try to create an Icon from above image, then convert it to a Bitmap (a type of Image)
	Using oImage As Image = Icon.FromHandle(oIcon.Handle).ToBitmap()
		'<<< try to save this Image to an external PNG file
		Try
			oImage.Save(FullFileName, System.Drawing.Imaging.ImageFormat.Png)
			Return True
		Catch
			Logger.Error(vbCrLf & "Error saving Image to following PNG file:" & vbCrLf & FullFileName)
			Return False
		End Try
	End Using
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

g.georgiades
Advocate
Advocate

@WCrihfield Don't worry, just about everything is more efficient than Regex, whether in speed or effort trying to figure out the right syntax.

 

Basically my thinking was to remove all special characters, even if windows allows them because I don't like them in file names.

If I clean it up and put the "Import" at the top then it looks easier.

Dim example = Regex.Replace(<string>, "[!@#\$%\^&\*?|<>+\-/\\\[\];'`~{}:\""""\.]", "_")

 

I have a Java background where generic String.Replace defaults to regex so I am used to it.