Save as bmp

Save as bmp

ebaras
Enthusiast Enthusiast
1,164 Views
12 Replies
Message 1 of 13

Save as bmp

ebaras
Enthusiast
Enthusiast

Hello everyone.

 

I created a code based on the following topic and ChatGPT.

 

"Save as jpeg"

 

Sub Main()
    Dim saveName As String
    Dim oDoc As PartDocument
    oDoc = ThisApplication.ActiveDocument
    Dim oActiveView As View
    oActiveView = ThisApplication.ActiveView
    Dim oCamera As Camera
    oCamera = oActiveView.Camera
    oCamera.ViewOrientationType = kIsoTopRightViewOrientation
    oCamera.ApplyWithoutTransition
    ThisApplication.ActiveView.Fit

    saveName = Left$(oDoc.FullDocumentName, Len(oDoc.FullDocumentName) -4)
	
    Dim resolutionX As Integer
    Dim resolutionY As Integer
    resolutionX = 350
    resolutionY = 270

    Call oActiveView.SaveAsBitmap(saveName & ".bmp", resolutionX, resolutionY)
End Sub

 

With this code, I currently have two issues.

 

  1. I cannot run this code in the assembly file. How can we adjust the code to work in assembly files as well?
  2. The quality of the images I'm getting as output is poor. It's important for the output images to have dimensions of 350x270 px, but they look bad at this size. Can we fix this?

I think that's all for now. If I can find solutions to these, I'd like to add the functionality to extract images from tables as well, but that's a job for later, I guess. But if you have any knowledge about this, please share.

0 Likes
Accepted solutions (1)
1,165 Views
12 Replies
Replies (12)
Message 2 of 13

WCrihfield
Mentor
Mentor

Hi @ebaras.  Why is that exact size so important?  If it is super important, then you may have to scale and/or crop the image after capturing it, instead of as you are capturing it.  It is usually best to maintain the aspect ratio of the view window's size (View.Height & View.Width) in the output image file, otherwise the output image may get squished in one direction, or both directions, making it not look good.  You could also try using the Camera.SaveAsBitmap method, instead of the View.SaveAsBitmap method, but I do not recall if that would make much difference in a situation like that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 13

rossano_praderi
Collaborator
Collaborator

@ebaras wrote:

 

With this code, I currently have two issues.

 

  1. I cannot run this code in the assembly file. How can we adjust the code to work in assembly files as well?
  2. The quality of the images I'm getting as output is poor. It's important for the output images to have dimensions of 350x270 px, but they look bad at this size. Can we fix this?

Hi @ebaras,

I'm answering your question about first point ("code in the assembly"), I did a cleanup of your original code taking care about @WCrihfield suggestions (this doesn't change anything about what already said @WCrihfield on image resolution).

 

 

Dim oCamera As Camera = ThisApplication.ActiveView.Camera
oCamera.ViewOrientationType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation
oCamera.ApplyWithoutTransition()
oCamera.Fit()

Dim resolutionX As Integer = 350
Dim resolutionY As Integer = 270

Call oCamera.SaveAsBitmap(ThisDoc.PathAndFileName(False) & ".bmp", resolutionX, resolutionY)

 

 

 



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 4 of 13

WCrihfield
Mentor
Mentor

Yes, that would work pretty well, if the only need is to capture an image of the main assembly or of a part.  But if images of all the assembly's referenced model documents are also needed, that would complicate things a lot more, because each of those referenced model documents would need to be 'visibly' opened as the active document on your screen before capturing the images.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 13

ebaras
Enthusiast
Enthusiast

First of all, thank you both for your responses. :))

Size matters because I'll be uploading the outputs to another system. But as you said, you've given me some ideas about scaling. I've tried scaling a few times, but I got an 'unsupported' error in the outputs. The issue might not be related to scaling, I'm not sure.

 

Additionally, I found the attached code online. The code opens each product in the assembly file, extracts their profiles, and saves them in DXF format.

 

This is what I want. That is, to open the models in the assembly file, capture their images from specific -or home- angles, and save them in BMP format.

 

I'm trying some things but I'm curious if it'll work...

0 Likes
Message 6 of 13

ebaras
Enthusiast
Enthusiast

The code line seems a bit more understandable compared to yesterday. However, functionally, it's still the same. I'll try more today.

 

I tried scaling attempts, but kept getting errors each time.

Also, I still haven't figured out how to open and close the models in the tables within the assembly.

 

Here's the final version;

Sub Main()
    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument

    ' If the active document is not an assembly file, display a warning and exit the subroutine
    If oDoc.DocumentType <> kAssemblyDocumentObject Then
        MsgBox("This rule can only be run from an assembly file.", vbExclamation, "Error")
        Exit Sub
    End If

    ' Get the name and path of the assembly file
    Dim oAsmName As String
    oAsmName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    Dim oPath As String
    oPath = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

    ' Get the view of the assembly
    Dim oActiveView As View
    oActiveView = ThisApplication.ActiveView

    ' Configure camera settings
    Dim oCamera As Camera
    oCamera = oActiveView.Camera
    oCamera.ViewOrientationType = kIsoTopRightViewOrientation
    oCamera.ApplyWithoutTransition
    ThisApplication.ActiveView.Fit

    ' Save the view as a BMP image with specific resolution
    Dim resolutionX As Integer
    Dim resolutionY As Integer
    resolutionX = 350
    resolutionY = 270
    Dim saveName As String
    saveName = oPath & "\" & oAsmName & "_AssemblyImage.bmp"
    Call oActiveView.SaveAsBitmap(saveName, resolutionX, resolutionY)
End Sub

 

0 Likes
Message 7 of 13

rossano_praderi
Collaborator
Collaborator

@ebaras  you assigned "ActiveDocument" to "oDoc" before checking out "DocumentType" and this is not a good idea.

 

I tought to suggest you some adjustments to your code, but you didn't use anything about my last code tips.



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 8 of 13

ebaras
Enthusiast
Enthusiast

I actually tried it, but to be honest I do most of my coding with ChatGPT. It was deleted during the changes.

 

I get an error when I try to add the code. When I write it this way, I get this error.

 

    "Error on line 42 in rule: Rule12, in document: Assembly1.iam

    No public member 'PathAndFileName' of type 'AssemblyDocument' was found."

 

I think I'm doing something wrong somewhere.

 

Sub Main()
    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument

    ' If the active document is not an assembly file, display a warning and exit the subroutine
    If oDoc.DocumentType <> kAssemblyDocumentObject Then
        MsgBox("This rule can only be run from an assembly file.", vbExclamation, "Error")
        Exit Sub
    End If

    ' Get the name and path of the assembly file
    Dim oAsmName As String
    oAsmName = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
    Dim oPath As String
    oPath = System.IO.Path.GetDirectoryName(oDoc.FullFileName)

    ' Get the view of the assembly
    Dim oActiveView As View
    oActiveView = ThisApplication.ActiveView

    ' Configure camera settings
    Dim oCamera As Camera
    oCamera = oActiveView.Camera
    oCamera.ViewOrientationType = kIsoTopRightViewOrientation
    oCamera.ApplyWithoutTransition
    ThisApplication.ActiveView.Fit

    ' Save the view as a BMP image with specific resolution
    Dim resolutionX As Integer
    Dim resolutionY As Integer
    resolutionX = 350
    resolutionY = 270
    Dim saveName As String
    saveName = oPath & "\" & oAsmName & "_AssemblyImage.bmp"
    Call oActiveView.SaveAsBitmap(saveName, resolutionX, resolutionY)

    ' Save the view using the camera settings
    oCamera.ViewOrientationType = ViewOrientationTypeEnum.kIsoTopRightViewOrientation
    oCamera.ApplyWithoutTransition()
    oCamera.Fit()
    Call oCamera.SaveAsBitmap(oDoc.PathAndFileName(False) & ".bmp", resolutionX, resolutionY)
End Sub   

 

0 Likes
Message 9 of 13

rossano_praderi
Collaborator
Collaborator

I did tests of my code before submission and work perfectly, but if chatGPT is the Master.... Ok, go with it and good luck 🙂 



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 10 of 13

ebaras
Enthusiast
Enthusiast

It sounds like I've been misunderstanding myself. I really don't understand the iLogic, and I'm trying to do something.

 

Yes, the code you provided helps me to take a pic, but it doesn't do the other things I want. As I try to add more things, the values change.

 

I saw your code and that's why I'm grateful. But I'm trying to add other things as well.

 

Thank you very much for your well wishes and contribution.

0 Likes
Message 11 of 13

rossano_praderi
Collaborator
Collaborator

Ok, have a nice day.



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
Message 12 of 13

ebaras
Enthusiast
Enthusiast
Accepted solution

After all this time I finally found a solution. It will be almost 1 year. :d

 

I am sharing it below. Maybe it will be useful for someone. :))

 

' Autodesk Inventor BMP Generation for IAM
' 14.03.2025 - ebaras - v2
Dim BMPFolder As String = "C:\Inventor_BMPs\"
Dim oAssemblyDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oFactory As iAssemblyFactory = oAssemblyDoc.ComponentDefinition.iAssemblyFactory
Dim rowCount As Integer = oFactory.TableRows.Count
Dim oCompDef As ComponentDefinition = oAssemblyDoc.ComponentDefinition

' Create BMP folder if it doesn't exist
If Not System.IO.Directory.Exists(BMPFolder) Then
    System.IO.Directory.CreateDirectory(BMPFolder)
End If

' Temporarily hide origin and work features
Dim origPlaneVis As New Dictionary(Of WorkPlane, Boolean)
For Each plane As WorkPlane In oCompDef.WorkPlanes
    origPlaneVis.Add(plane, plane.Visible)
    plane.Visible = False
Next

Dim origAxisVis As New Dictionary(Of WorkAxis, Boolean)
For Each axis As WorkAxis In oCompDef.WorkAxes
    origAxisVis.Add(axis, axis.Visible)
    axis.Visible = False
Next

Dim origPointVis As New Dictionary(Of WorkPoint, Boolean)
For Each point As WorkPoint In oCompDef.WorkPoints
    origPointVis.Add(point, point.Visible)
    point.Visible = False
Next

' Only hide the Origin Indicator (bottom-left XYZ axis)
Dim oDisplayOptions As DisplayOptions = ThisApplication.DisplayOptions
Dim origOriginIndicator As Boolean = oDisplayOptions.Show3DIndicator
oDisplayOptions.Show3DIndicator = False

' Process each iAssembly member
For i As Integer = 1 To rowCount
    oFactory.DefaultRow = oFactory.TableRows.Item(i)
    oAssemblyDoc.Update()
    ThisApplication.ActiveView.Update()

    ' Fit camera view to model
    Dim oCamera As Camera = ThisApplication.ActiveView.Camera
    oCamera.Fit()
    oCamera.Apply()
    ThisApplication.ActiveView.Update()

    ' Save BMP
    Dim currentMemberName As String = oFactory.DefaultRow.MemberName
    Dim BMPName As String = BMPFolder & currentMemberName & ".bmp"
    ThisApplication.ActiveView.SaveAsBitmap(BMPName, 1920, 1080)
Next

' Restore visibility of origin and work features
For Each plane As WorkPlane In oCompDef.WorkPlanes
    plane.Visible = origPlaneVis(plane)
Next

For Each axis As WorkAxis In oCompDef.WorkAxes
    axis.Visible = origAxisVis(axis)
Next

For Each point As WorkPoint In oCompDef.WorkPoints
    point.Visible = origPointVis(point)
Next

' Restore Origin Indicator visibility
oDisplayOptions.Show3DIndicator = origOriginIndicator

' Completion message
MessageBox.Show("BMP files created successfully without Origin Indicator!", "Completed")
Message 13 of 13

rossano_praderi
Collaborator
Collaborator

I would like to suggest you to add some checks to your code, to make it more stable.

 

' Autodesk Inventor BMP Generation for IAM
' 14.03.2025 - ebaras - v2
' -
' 24.03.2025 - Rossano Praderi - Some suggestions to make your code more stable

Dim BMPFolder As String = "C:\Inventor_BMPs\"

' suggestion: A good practice is to check the document type before proceed
If ThisApplication.ActiveDocumentType.Equals(Inventor.DocumentTypeEnum.kAssemblyDocumentObject) Then
	Dim oAssemblyDoc As AssemblyDocument = ThisApplication.ActiveDocument
	
	' suggestion: A good practice is to check if is an IAssemblyFactory before proceed
	If oAssemblyDoc.ComponentDefinition.IsiAssemblyFactory Then
		Dim oFactory As iAssemblyFactory = oAssemblyDoc.ComponentDefinition.iAssemblyFactory
		Dim rowCount As Integer = oFactory.TableRows.Count
		Dim oCompDef As ComponentDefinition = oAssemblyDoc.ComponentDefinition

' ORIGINAL CODE
'
		' Create BMP folder if it doesn't exist
'		If Not System.IO.Directory.Exists(BMPFolder) Then
'		    System.IO.Directory.CreateDirectory(BMPFolder)
'		End If
'
' suggested: Is not necessary to check if a Folder Exists
'			"CreateDirectory" create all the directories unless they already exists
'			good practice is to check it by catching exceptions
'			your original code doesn't catch exceptions, for reference see the follow link
'			https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.createdirectory
' SUGGESTED CODE
'
		Dim result As Boolean
		Try 
			Dim di As System.IO.DirectoryInfo = System.IO.Directory.CreateDirectory(BMPFolder) 
			result = di.Exists
		Catch
			result = False
		End Try
		If result = True Then
' END OF SUGGESTED CODE
	
			' Temporarily hide origin and work features
			Dim origPlaneVis As New Dictionary(Of WorkPlane, Boolean)
			For Each plane As WorkPlane In oCompDef.WorkPlanes
			    origPlaneVis.Add(Plane, Plane.Visible)
			    Plane.Visible = False
			Next

			Dim origAxisVis As New Dictionary(Of WorkAxis, Boolean)
			For Each axis As WorkAxis In oCompDef.WorkAxes
			    origAxisVis.Add(axis, axis.Visible)
			    axis.Visible = False
			Next

			Dim origPointVis As New Dictionary(Of WorkPoint, Boolean)
			For Each point As WorkPoint In oCompDef.WorkPoints
			    origPointVis.Add(Point, Point.Visible)
			    Point.Visible = False
			Next

			' Only hide the Origin Indicator (bottom-left XYZ axis)
			Dim oDisplayOptions As DisplayOptions = ThisApplication.DisplayOptions
			Dim origOriginIndicator As Boolean = oDisplayOptions.Show3DIndicator
			oDisplayOptions.Show3DIndicator = False

			' Process each iAssembly member
			For i As Integer = 1 To rowCount
			    oFactory.DefaultRow = oFactory.TableRows.Item(i)
			    oAssemblyDoc.Update()
			    ThisApplication.ActiveView.Update()

			    ' Fit camera view to model
			    Dim oCamera As Camera = ThisApplication.ActiveView.Camera
			    oCamera.Fit()
			    oCamera.Apply()
			    ThisApplication.ActiveView.Update()

			    ' Save BMP
			    Dim currentMemberName As String = oFactory.DefaultRow.MemberName
			    Dim BMPName As String = BMPFolder & currentMemberName & ".bmp"
			    ThisApplication.ActiveView.SaveAsBitmap(BMPName, 1920, 1080)
			Next

			' Restore visibility of origin and work features
			For Each plane As WorkPlane In oCompDef.WorkPlanes
			    Plane.Visible = origPlaneVis(Plane)
			Next

			For Each axis As WorkAxis In oCompDef.WorkAxes
			    axis.Visible = origAxisVis(axis)
			Next

			For Each point As WorkPoint In oCompDef.WorkPoints
			    Point.Visible = origPointVis(Point)
			Next

			' Restore Origin Indicator visibility
			oDisplayOptions.Show3DIndicator = origOriginIndicator

			' Completion message
			MessageBox.Show("BMP files created successfully without Origin Indicator!", "Completed")
		Else
			MessageBox.Show("The process failed to create the directory!")
		End If
	Else
		MessageBox.Show("This is not an iAssembly!")
	End If
Else
	MessageBox.Show("This is not an Assembly!")
End If

 



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------