Export transparent background png Image with iLogic

Export transparent background png Image with iLogic

mgaunt3DSG
Enthusiast Enthusiast
1,723 Views
8 Replies
Message 1 of 9

Export transparent background png Image with iLogic

mgaunt3DSG
Enthusiast
Enthusiast

I am trying to create an iLogic rule that will take over what I usually do after completing one of my files.

I already have completed the steps here:

  1. Exports my file as a step file (Based on the preloaded "Custom" snippet "Save As STEP File")
  2. Exports my file as a pdf file (Based on the code found here)
  3. Writes an email to a list of people (Based on the code found here)

What I cannot find is code that will export my camera view (which my code sets to the file's home view) as a transparent background png.

I have found this code that uses SaveAsBitMap, but it keeps a white background on the image.

I have also found the CreateImageWithOptions method, but can't figure out how to work it.

 

Can anyone point me in the right direction with these methods, or suggest an alternative?

I am using Inventor 2024.3

0 Likes
Accepted solutions (2)
1,724 Views
8 Replies
Replies (8)
Message 2 of 9

J_Pfeifer_
Advocate
Advocate

I dug into this a little bit. You're right some of this can be weird to read and understand. Namevaluemaps for options and things in the view? Confusing. 

 

I was able to get the below code to output a PNG, JPG, BMp, TFF, and GIF. You use the .SaveAsBitmap Method but specify a different file type. The documentation reflects this. 

File type specification.jpg

 

In testing this I was able to output a PNG file with the correct part color and correct background. As if I were to win + S or screen shot the active window. 

Attached are two photos, one taken from windows S, the other from the iLogic rule. 

 

 

 

 

Sub Main()
	Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oClientView As Inventor.View = ThisApplication.ActiveView
	Dim oCamera As Camera = oClientView.Camera
	
	Dim oFileString As String = "C:\Users\Something\Something\Something\InventorScreenshot.png"
	
	oCamera.SaveAsBitmap(oFileString, 1920, 1080, Nothing, Nothing)
	
End Sub

 

 

I also misunderstood a key point in your post. You want a transparent background? Give me a bit, and I'll see what more I can come up with. 

0 Likes
Message 3 of 9

mgaunt3DSG
Enthusiast
Enthusiast

Thanks for the help, unfortunately this image still has a background on it, is there some way to remove it?

0 Likes
Message 4 of 9

J_Pfeifer_
Advocate
Advocate

Edited to remove incorrect information, see below. 

0 Likes
Message 5 of 9

J_Pfeifer_
Advocate
Advocate
Accepted solution

Just kidding I found it, with a bit of Chat GPT help. See the below code. 

 

 

 

 

Sub Main()
	
	Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	Dim oClientView As Inventor.View = ThisApplication.ActiveView
	Dim oCamera As Camera = oClientView.Camera
	
	Dim oFileString As String = "C:\Users\something\something\something\InventorScreenshot.png"
	
	Dim oNameValueMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	oNameValueMap.Add("TransparentBackground" , True)

	oClientView.SaveAsBitmapWithOptions(oFileString, 1920, 1080, oNameValueMap)
	'(oFileString, 1920, 1080, Nothing, Nothing)
	
	
End Sub

 

 

 

 

 

I was confused due to how you supply or add the name value map. It must be a string of the value you're placing within. This gave me the following output:

 

InventorScreenshot.png

Transparentbackground.jpg

Message 6 of 9

chris
Advisor
Advisor

If I wanted this to work with an assembly file, what would I need to change, as I got an error on line 3, I assume because it's not a part? is there a way for it to apply for either a part or an assembly or a presentation?

Also, when I try and run it in a part file, I get an error on line 12?

 

0 Likes
Message 7 of 9

Michael.Navara
Advisor
Advisor
You can use this code in assembly.
- Only line 3 is unnecessary and can be removed.
- Variable oFileString must be set to valid output file name to existing directory.
0 Likes
Message 8 of 9

mgaunt3DSG
Enthusiast
Enthusiast
Accepted solution

This works! Thanks so much. I did edit some of the code to work with my assembly, which I will post here:

' Export image
Dim pngName As String = ThisDoc.PathAndFileName(False) & ".png"
' Return View To Home view
ThisApplication.CommandManager.ControlDefinitions.Item("AppViewCubeHomeCmd").Execute

If FileIO.FileSystem.FileExists(pngName) Then
	i = MessageBox.Show("A png file already exists for this Assembly, would you like to overwrite it?", "png File Exists", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
	Select Case i
	Case vbYes
		'continue
	Case vbNo
		GoTo skippng
	Case vbCancel
		Return
	End Select
End If

Dim oClientView As Inventor.View = ThisApplication.ActiveView
Dim oCamera As Camera = oClientView.Camera

Dim oNameValueMap As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
oNameValueMap.Add("TransparentBackground" , True)

oClientView.SaveAsBitmapWithOptions(pngName, 1920, 1080, oNameValueMap)
Message 9 of 9

jsmeker
Participant
Participant

Trying to export png with a transparent background via VBA and having some trouble .  If I export with the background, I get a nice image:

jsmeker_0-1731418336072.png

If I set ("TransparentBackground") = True, and leave everything else the same, I get the transparent background but the image is very pixelated.

jsmeker_1-1731418362853.png

Anybody have any idea why this would happen?  I'm running Inventor 2024.  Thanks for the help!

 

 

0 Likes