Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Save as bmp

10 REPLIES 10
Reply
Message 1 of 11
ebaras
599 Views, 10 Replies

Save as bmp

ebaras
Contributor
Contributor

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

Save as bmp

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.

Labels (3)
10 REPLIES 10
Message 2 of 11
WCrihfield
in reply to: ebaras

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)

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 11
rossano_praderi
in reply to: ebaras

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!
---------------


@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 11

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)

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 11
ebaras
in reply to: WCrihfield

ebaras
Contributor
Contributor

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

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...

Message 6 of 11
ebaras
in reply to: ebaras

ebaras
Contributor
Contributor

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

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

 

Message 7 of 11
rossano_praderi
in reply to: ebaras

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!
---------------

@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 11
ebaras
in reply to: rossano_praderi

ebaras
Contributor
Contributor

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

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   

 

Message 9 of 11
rossano_praderi
in reply to: ebaras

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

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!
---------------
Message 10 of 11
ebaras
in reply to: rossano_praderi

ebaras
Contributor
Contributor

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

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.

Message 11 of 11
rossano_praderi
in reply to: ebaras

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!
---------------

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!
---------------

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report