Announcements
Notice: Upload of PDF documents is currently unavailable across the Community due to an ongoing issue.

Get a Grey Background with adjusted View-size, when more than one Document is open.

Get a Grey Background with adjusted View-size, when more than one Document is open.

benedikt_seitzLXJD2
Explorer Explorer
176 Views
7 Replies
Message 1 of 8

Get a Grey Background with adjusted View-size, when more than one Document is open.

benedikt_seitzLXJD2
Explorer
Explorer

I have the following Problem:

If I adjust the active view with the Code below with only one document open, it works and i have a grey background with a clear difference to the adjusted view. But if more than one document is open there is always a remaining blue background of the same or another document without a clear difference to the adjusted view. How can I get the other documents to stay minimized and the background to stay gray like if only one Document is open?

This is the Code I use:

(VisualBasic)

Public Sub subWNQuadrat()
'go on only with an active document
Set oWNDoc = ThisApplication.ActiveDocument
Dim oWNview As View
Dim strWNMeldung As String
If (oWNDoc Is Nothing) Then
strWNMeldung = "This Makro only functions with open documents! "
MsgBox strWNMeldung, vbCritical
Exit Sub
End If
ThisApplication.ScreenUpdating = False
ThisApplication.ActiveView.WindowState = kNormalWindow
ThisApplication.ActiveView.WindowState = kMaximize
'ask for length
Dim lgWnt As Long
Dim lgWnl As Long
Dim lgWnh As Long
Dim lgWnw As Long
Call ThisApplication.ActiveView.GetWindowExtents(lgWnt, lgWnl, lgWnh, lgWnw)
'set all to greater length
If lgWnw >= lgWnh Then
ThisApplication.ActiveView.width = ThisApplication.ActiveView.height
Else
ThisApplication.ActiveView.height = ThisApplication.ActiveView.width
End If
ThisApplication.ScreenUpdating = True
ThisApplication.ActiveView.Update
If ThisApplication.Views.Count = 1 Then
subWNAppExtents
Else
For Each oWNview In ThisApplication.Views
If Not (oWNview = ThisApplication.ActiveView) Then
oWNview.WindowState = kMinimize
End If
Next
End If
'debug.print "Resize"
'Call subWNshowAkiveViewSize
End Sub
Public Sub subWNVoll()
Dim strWNMeldung As String

Set oWNDoc = ThisApplication.ActiveDocument
If (oWNDoc Is Nothing) Then
strWNMeldung = "This Makro only functions with open documents! "
MsgBox strWNMeldung, vbCritical
Exit Sub
End If
'Fenster Maximieren
If ThisApplication.Views.Count = 1 Then
Call subWNViewMaxEinView
Else

ThisApplication.ScreenUpdating = False
ThisApplication.ActiveView.WindowState = kNormalWindow
ThisApplication.ActiveView.WindowState = kMaximize
ThisApplication.ScreenUpdating = True
ThisApplication.ActiveView.Update
End If
End Sub
Sub subWNAppExtents()
'Aktuelle Größe Dokumentfenster abfragen
Dim lgWnt As Long
Dim lgWnl As Long
Dim lgWnh As Long
Dim lgWnw As Long
Call ThisApplication.GetAppFrameExtents(lgWnt, lgWnl, lgWnh, lgWnw)

'move to destination
Call ThisApplication.Move(lgWnt, lgWnl, lgWnh, lgWnw)
End Sub
Sub subWNViewMaxEinView()
ThisApplication.ActiveView.Visible = False
Call subWNAppExtents
Call ThisApplication.Views.Item(1).Activate
End Sub

Accepted solutions (1)
177 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @benedikt_seitzLXJD2.  What is the purpose or goal of your code above?  Maybe if we better understand what you are trying to achieve with it, it will help us understand how to fix it the way you want.

At first glance, I see two things which may be a problem.  The first is near the start of the code at the following line:

Dim oWNview As View

I would change that to this:

Dim oWNview As Inventor.View

...to make sure your code understands that it is supposed to represent the type of View defined in Inventor's API (Inventor.View) and not the System.Windows.Forms.View, which is an Enum.  There are several Types of objects which have 'common' names among Inventor's API, the greater Visual Basic coding language, and other referenced API's, so sometimes we should be a bit more specific about which one we are referring to, or the code reader can get confused.

The next thing I see is in the following lines of code:

For Each oWNview In ThisApplication.Views
If Not (oWNview = ThisApplication.ActiveView) Then

I believe you should change the equals sign (=) to the 'Is' operator, because you are comparing 2 objects, not two numerical values or text Strings.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

benedikt_seitzLXJD2
Explorer
Explorer

Thanks for the Reply.

The code should set the size of the view to a square, as big as possible. The rest of the window should be colored in another way so that this square is clearly visable and can be separated from the rest of the window with the human eye. The general purpose is to be able to make a Screenshot of the window and to cut out evreything exept  the square because of the clear difference.

I changed the parts you mentioned and it worked for a second to get a square with the rest of the window having the standart grey background, but then it changed back to the full size.

When I repeat the code a new view is generated in the shape of the square but the original view is still in the background of the window and visable outside the square.

When I move the mouse the original view is replaced by the other opened document and there is no clear difference, so the border of the square can´t be seen because both documents have a blue background.

If I reset the view to its original size there is the one second result one time again and the rest is like described before.

If I go through the code step by step at first a new view is generated in the shape of the square but the original view is still in the background of the window and visable outside the square, then after oWNview.WindowState = kMinimize for the only first time and before the folowing End If, the square appears as the only visable view, with the rest of the background grey, just to reset to its original size.

This is happening only for the first time I go through the code or if I reset the view.

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

Hi @benedikt_seitzLXJD2.  I finally found time to revisit this topic this morning.  If your main goal is to get a screen captured images of what is showing in Inventor's active View, and you need it to be square, instead of its natural rectangular shape, then there are likely other ways of achieving that which are less complicated.  Inventor's API has multiple methods for capturing images of what is showing within its views.  When we use those methods, we can specify the size of the image we want it to create, and the type of image file it will create.  Sometimes we can even dictate the 'background', or make it transparent, when it captures the image.  And if that were not enough, we could always just edit the resulting image after it is created too, if needed.  That can be done entirely by code, if needed.  The methods for creating images directly associated with an Inventor View are View.SaveAsBitmap, View.SaveAsBitmapWithOptions, and View.SaveAsBitmapWithRayTracing.  Then there are similar methods of the Camera object that we can get from the View through its View.Camera property.  The Camera.CreateImage & Camera.CreateImageWithOptions methods are both for creating IPictureDisp type images, for active internal digital use.  But the Camera.SaveAsBitmap & Camera.SaveAsBitmapWithOptions methods are similar to the View methods, but with a couple more options for dictating specific color backgrounds.

Beyond that, there are a lot of code examples available online for editing or modifying existing images.

I will post an example below, for starters, but it will be in vb.net, which can be used in an iLogic rule, not in VBA.  I stopped using VBA several years ago, around the same time Autodesk stopped including it in their standard installations for security purposes, because Microsoft stopped maintaining it.  The VB.NET coding system used in our iLogic rules has a lot of similarities, because it is still in the 'Visual Basic' language family, but is much newer, and offers more functionality and advantages.  If you are using an older version of Inventor where VBA macros were the only way to put a custom button into Inventor's ribbon, then I can understand that, but a VBA macro can simply call an iLogic rule to run, instead of containing all the code for the task in VBA.  The last several versions of Inventor have allowed us to put buttons in Inventor's ribbons that will run external iLogic rules or show global iLogic Forms, which was long anticipated in the Inventor automation world.

I wrote the 'Main' block of code, and customized some of the other blocks of code a bit, but the core functionality of the image editing block of code I got from GPT 5.4 nano (an AI source).  I tested it locally on one of my Inventor models.  It saved a screen shot image of my model to a PNG file with a transparent background first, that was the full size of my View.  Then it opened that image file invisibly, and cropped it to a square, using the smaller dimension of either the original images width or its height, as the size for both sides of the square, then saved the result to a new file.  The second image was identical to the first image, and one of its dimensions is the same as the original, but with the longer dimension cropped towards the center of the image.  That sounds like what you were hoping to achieve, so I hope this will help you reach your goal.  And maybe help some others along the way too.

AddReference "System.Drawing"
Imports System.Drawing
Imports System.Drawing.Imaging

Sub Main
	'hard-coded full file names...this can be changed later, if needed
	'could show an interactive file browser here instead
	Dim sOriginalImageFile As String = "C:\Temp\Inventor ScreenShot.png"
	Dim sCroppedImageFile As String = "C:\Temp\Inventor ScreenShot - Cropped To Square.png"
	'get Inventor's active View (assume the view is ready for screen capture)
	Dim oView As Inventor.View = ThisApplication.ActiveView
	'specify an Option for the image to have a transparent background
	'this only works for the PNG or TIFF Image types, not for GIF, BMP, or JPG
	Dim oOptions As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
	oOptions.Value("TransparentBackground") = True
	'using zero values (default) gets the entire view width & height
	oView.SaveAsBitmapWithOptions(sOriginalImageFile, 0, 0, oOptions)
	'allow Inventor to finish processing stuff in its queue
	ThisApplication.UserInterfaceManager.DoEvents()
	'call the custom Sub routine to Crop the image file from its original rectangular shape,
	'to a centered square, without squishing or deforming the image
	CropImageToCenteredSquare(sOriginalImageFile, sCroppedImageFile)
End Sub

Sub CropImageToCenteredSquare(imageFile As String, _
	Optional resultImageFile As String = Nothing)
	
	If String.IsNullOrWhiteSpace(resultImageFile) Then
		resultImageFile = imageFile
	End If

	Using oOrigImg As System.Drawing.Image = System.Drawing.Image.FromFile(imageFile)
		Dim dW As Integer = oOrigImg.Width
		Dim dH As Integer = oOrigImg.Height
		Dim dMinDim As Integer = Math.Min(oOrigImg.Width, oOrigImg.Height)
		Dim dX_Ctr As Integer = (dW - dMinDim) / 2
		Dim dY_Ctr As Integer = (dH - dMinDim) / 2
		Using bmp As New Bitmap(dMinDim, dMinDim, PixelFormat.Format24bppRgb)
			Using g As Graphics = Graphics.FromImage(bmp)
				g.CompositingMode = Drawing2D.CompositingMode.SourceCopy
				g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
				g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
				g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
				g.DrawImage(oOrigImg, New Rectangle(0, 0, dMinDim, dMinDim), _
				New Rectangle(dX_Ctr, dY_Ctr, dMinDim, dMinDim), GraphicsUnit.Pixel)
				'call other custom Sub routine to save this modified image to file
				SaveImageToFileByExtension(bmp, resultImageFile)
			End Using 'g Graphics
		End Using 'bmp
	End Using 'oOrigImg
End Sub

Sub SaveImageToFileByExtension(img As System.Drawing.Image, outPath As String)
	Dim sExt As String = System.IO.Path.GetExtension(outPath).ToLowerInvariant()
	Select Case ext
		Case ".png"
			img.Save(outPath, ImageFormat.Png)
		Case ".bmp"
			img.Save(outPath, ImageFormat.Bmp)
		Case ".gif"
			img.Save(outPath, ImageFormat.Gif)
		Case ".tif", ".tiff"
			img.Save(outPath, ImageFormat.Tiff)
		Case Else
			img.Save(outPath, ImageFormat.Jpeg)
	End Select
End Sub

The image editing code could even take it a step further, if needed (with more complex code), to crop as much of a solid or transparent background away as possible, while still keeping the image square, and can even leave a margin of space around it.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 8

benedikt_seitzLXJD2
Explorer
Explorer

Thanks for the Reply.

I like the idea of this so Thanks again, but it still lacks the possebility to adjust the objekts in the View so that they could be seen in the square before taking the picture. For this I would have to use the photo cutting code after my original code has adjusted the view then there is no need for the border of the View to be visable for the human eye.

Half of the problem is solved, but there is another problem for the first time of running the code: How do i prevent this jumping back to the original size I mentioned earlier.

It replaces the objeckts within the view back, outside the square. 

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @benedikt_seitzLXJD2.  I do see your point now, after a few tests.  If the model was long and slim, then it may not fit nicely in a square, so the 'Crop' process may actually be cutting off some of the model in the image.  So, the plan is to make the view square first, then 'fit' the model within that square view, then capture the image, then restore the view back to its original size & location when done.  I changed my code example to do just that.  My updated rule also 'manages' the View's parent ViewFrame, just in case that may be necessary.  And I added more code for helping with the full file name of the image file it is to create.  In doing so, just after I capture Inventor's active View at the start, I then capture the Document that is associated with that View.  If that Document has not been saved yet, it will not have a 'File' associated with it yet, so it will not have a path or file name yet.  I added a check just for that.  And I created 3 variables near the start, for capturing the path, and file name of that document, then using those to assemble the full file name of the image.  Using that assembled image full file name would save the new image in the same folder, and with the same name as the document's File, but with the '.png' file extension.  However, I also added a custom standalone Function just for allowing a FileDialog prompt to the user, so they can manually browse to the folder, type in a file name and file extension.  But the way I designed it below, that method will only be used if you did not change the 'Default' (empty) values of those variables, and the document has not been saved yet.  When those two things are true, the String variable for the image's full file name will still be empty, which causes that prompt to show.  But you can change that however you want.  Just including all the 'tools' for the task, to make it easier later.  The updated iLogic rule code example is below.  I added lots of 'comments' in there, to explain what each block of code is for.

Sub Main
	'get Inventor's active View
	Dim oView As Inventor.View = ThisApplication.ActiveView
	'get the View's Document
	Dim oDoc As Inventor.Document = oView.Document
	If oDoc Is Nothing Then Exit Sub
	
	Dim sPath As String = Nothing 'or a Default value
	Dim sFileName As String = Nothing 'or a Default value
	Dim sImageFile As String = Nothing 'or a Default value
	
	'if the Document has never been saved, it will not have a File (no path or file name)
	If Not oDoc.FileSaveCounter = 0 Then
		Dim sFFN As String = oDoc.FullFileName
		'gets the full file path, without file name
		sPath = System.IO.Path.GetDirectoryName(sFFN)
		'gets just the file name, without file extension (no file path)
		sFileName = System.IO.Path.GetFileNameWithoutExtension(sFFN)
		'combine path & file name, by adding your system's directory separator character
		sImageFile = System.IO.Path.Combine(sPath, sFileName & ".png")
	End If
		
	'<<< VIEW FRAME >>>
	'get the View's Parent ViewFrame
	Dim oVF As Inventor.ViewFrame = oView.ViewFrame
	'record its size, location, & state, so we can restore it later
	Dim oOrigVFBounds As List(Of Integer)
	Dim eOrigVFState As Inventor.WindowsSizeEnum
	Dim bVF_Max As Boolean = (oVF.WindowState = Inventor.WindowsSizeEnum.kMaximize)
	If Not bVF_Max Then
		oOrigVFBounds = New List(Of Integer) From {oVF.Top, oVF.Left, oVF.Height, oVF.Width}
		eOrigVFState = oVF.WindowState
		oVF.WindowState = Inventor.WindowsSizeEnum.kMaximize
	End If
	
	'<<< VIEW >>>
	'maximize View, then make it square, then Zoom All, Save Image
	'record View size, location, & state, so we can restore it later
	Dim iTop, iLeft, iHeight, iWidth As Integer
	oView.GetWindowExtents(iTop, iLeft, iHeight, iWidth)
	Dim oOrigVBounds As List(Of Integer) = New List(Of Integer) From {iTop, iLeft, iHeight, iWidth }
	Dim eOrigViewState As Inventor.WindowsSizeEnum = oView.WindowState
	If Not oView.WindowState = Inventor.WindowsSizeEnum.kMaximize Then
		oView.WindowState = Inventor.WindowsSizeEnum.kMaximize
	End If
	
	'now make the View square by shrinking its largest size to match its other size
	'no need to move the view's top left corner
	If iHeight > iWidth Then
		iHeight = iWidth
	ElseIf iHeight < iWidth Then
		iWidth = iHeight
	End If
	oView.Height = iHeight
	oView.Width = iWidth
		
	'now 'Fit' the model within this square View or Zoom All
	oView.Fit(True)
	
	'if no 'Default' Image full file name was specified, and the Document has not been saved,
	'then prompt the user with a FileDialog, so they can manually specify the full file name
	If String.IsNullOrWhiteSpace(sImageFile) Then
		sImageFile = UseFileDialog(True, "Specify Full Path, Name, & Image File Type")
	End If
	
	'now capture the Image
	'specify an Option for the image to have a transparent background
	'this only works for the PNG or TIFF Image types, not for GIF, BMP, or JPG
	Dim oOptions As Inventor.NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap()
	oOptions.Value("TransparentBackground") = True
	'using zero values (default) gets the entire view width & height
	oView.SaveAsBitmapWithOptions(sImageFile, 0, 0, oOptions)
	'allow Inventor to finish processing stuff in its queue
	ThisApplication.UserInterfaceManager.DoEvents()
	
	'now restore ViewFrame and View back to their original state, location, & size
	If Not bVF_Max Then
		oVF.WindowState = eOrigVFState
		oVF.Move(oOrigVFBounds.Item(0), oOrigVFBounds.Item(1), oOrigVFBounds.Item(2), oOrigVFBounds.Item(3))
	End If
	oView.WindowState = eOrigViewState
	oView.Move(oOrigVBounds.Item(0), oOrigVBounds.Item(1), oOrigVBounds.Item(2), oOrigVBounds.Item(3))
	oView.Fit(True)
End Sub

Function UseFileDialog(Optional save As Boolean = False, Optional title As String = Nothing) As String
	Dim oFDlg As Inventor.FileDialog
	ThisApplication.CreateFileDialog(oFDlg)
	oFDlg.DialogTitle = IIf(String.IsNullOrWhiteSpace(title), "Specify Full File Name", title)
	oFDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	'oFDlg.Filter = "Autodesk Inventor Files (*.iam;*.dwg;*.idw;*.ipt) | *.iam;*.dwg;*.idw;*ipt | All files (*.*)|*.*"
	oFDlg.Filter = "All files (*.*)|*.*"
	oFDlg.MultiSelectEnabled = False
	oFDlg.OptionsEnabled = False
	oFDlg.InsertMode = False
	oFDlg.CancelError = True
	Try
		If save = True Then
			oFDlg.ShowSave()
		Else
			oFDlg.ShowOpen()
		End If
	Catch
	End Try
	Return oFDlg.FileName
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 8

benedikt_seitzLXJD2
Explorer
Explorer

Thank you verry much for helping me out of this problem.

0 Likes
Message 8 of 8

iva.btblan
Advocate
Advocate

Wonderfuul!

0 Likes