Folder Browser: change to Windows.Forms to Inventor Panel

Folder Browser: change to Windows.Forms to Inventor Panel

Anonymous
Not applicable
2,168 Views
6 Replies
Message 1 of 7

Folder Browser: change to Windows.Forms to Inventor Panel

Anonymous
Not applicable

I have an code that use windows forms to browse for folder, but I want to change it to Inventor menu because it's so painful to look for folder now when I have about 10000 folders existing.

    Private Sub btnSetOutputFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnSetOutputFolder.Click
        Try
            Dim dlgFolder As New System.Windows.Forms.FolderBrowserDialog
            dlgFolder.Description = "Select the folder to save output files"
            If dlgFolder.ShowDialog() = DialogResult.OK Then
                Dim outFolder As String = dlgFolder.SelectedPath
                Dim odirectory As DirectoryInfo = New DirectoryInfo(outFolder)
                Dim dSecurity As DirectorySecurity = odirectory.GetAccessControl()
                If odirectory.Exists Then
                    Dim fileSec = System.IO.File.GetAccessControl(outFolder, Security.AccessControl.AccessControlSections.Access)
                    Dim accessRules = fileSec.GetAccessRules(True, True, GetType(System.Security.Principal.NTAccount))
                    For Each rule As System.Security.AccessControl.FileSystemAccessRule In accessRules
                        If (rule.AccessControlType.ToString = "Allow") Then
                            txtOutputFolder.Text = dlgFolder.SelectedPath
                        End If
                    Next
                End If
            End If
            Dim Inv As New mInventor()
        Catch ex As Exception
            txtOutputFolder.Text = ""
            MsgBox("Write Permission denied", MsgBoxStyle.Critical)
        End Try
    End Sub

Is there a way to convert this old school Folder Browser into Inventor Save As style?

nguye486_0-1611335420057.png

Thanks!

 

0 Likes
Accepted solutions (1)
2,169 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

I agree that the FolderBrowserDialog isn't as nice as a regular FileDialog, but it allows you to just select a folder without having to open or save an Inventor document, as part of the process.  And it retrieves the selected folder's info, instead of a specific file's info.  When using FileDialog, not only do you have to deal with either opening or saving something, but now you've got to go another step to extract just the folder/directory info from the full file name.

When using the Save dialog you have to already have an Inventor file open, then you have to select a different file before it will update/put its data into the 'File name"' textbox.  Then the only options are Save or Cancel.  It won't let you Save unless a name is in that textbox, and if there is something in that textbox it will save the currently open document to that location with that name.  I'm not sure how you would retain folder information, without either opening or saving a document.

Are you sure that's what you want?

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 7

Anonymous
Not applicable

@WCrihfield wrote:

I agree that the FolderBrowserDialog isn't as nice as a regular FileDialog, but it allows you to just select a folder without having to open or save an Inventor document, as part of the process.  And it retrieves the selected folder's info, instead of a specific file's info.  When using FileDialog, not only do you have to deal with either opening or saving something, but now you've got to go another step to extract just the folder/directory info from the full file name.

When using the Save dialog you have to already have an Inventor file open, then you have to select a different file before it will update/put its data into the 'File name"' textbox.  Then the only options are Save or Cancel.  It won't let you Save unless a name is in that textbox, and if there is something in that textbox it will save the currently open document to that location with that name.  I'm not sure how you would retain folder information, without either opening or saving a document.

Are you sure that's what you want?


This is what I camp up right now. It is kind hack by using FileDialog with 

dlgFolder.ValidateNames = False
dlgFolder.CheckFileExists = False
dlgFolder.CheckPathExists = True

with a empty file name.

 

It works but not really ideal.

 

Private Sub btnSetOutputFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnSetOutputFolder.Click
        Try
            Dim dlgFolder As New System.Windows.Forms.OpenFileDialog
            dlgFolder.ValidateNames = False
            dlgFolder.CheckFileExists = False
            dlgFolder.CheckPathExists = True
            dlgFolder.FileName = "Select Folder"
            If dlgFolder.ShowDialog() = DialogResult.OK Then
                Dim outFolder As String ' = dlgFolder.FileName                
                outFolder = IO.Path.GetDirectoryName(dlgFolder.FileName)
                txtOutputFolder.Text = outFolder                
            End If
            Dim Inv As New mInventor()
        Catch ex As Exception
            txtOutputFolder.Text = ""
            MsgBox("Write Permission denied", MsgBoxStyle.Critical)
        End Try
    End Sub

 

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor

I think the Windows.Forms version may be a little easier to work with than the Inventor version, but the Inventor version includes the Options button stuff.  This is one of the codes I had for an Inventor.FileDialog (very similar though).

Dim oFileName As String
Dim oDoc As Inventor.Document
Dim oFileDialog As Inventor.FileDialog
ThisApplication.CreateFileDialog(oFileDialog)
'oFileDialog.DialogTitle = "Browse To Target Location And Specify New File Name."
oFileDialog.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
oFileDialog.Filter = "Autodesk Inventor Files (*.iam;*.dwg;*.idw;*.ipt:*.ipn;*.ide) | *.iam;*.dwg;*.idw;*ipt;*.ipn;*.ide | All files (*.*)|*.*"
'oFileDialog.OptionsEnabled = True
oFileDialog.CancelError = True
'oFileDialog.InsertMode = False
On Error Resume Next
oFileDialog.ShowSave
If Err.Number <> 0 Then
	MsgBox("Dialog was canceled. Exiting.", vbOKOnly, "CANCELED")
Else	
	If oFileDialog.FileName <> vbNullString Then
		oFileName = oFileDialog.FileName
	Else
		MsgBox("No file name was specified. Exiting.", vbOKOnly + vbExclamation, "FILE NOT SAVED")
	End If
End If
'MsgBox(oFileName,,"")

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 7

greggeorgi
Contributor
Contributor

If you happen to use Microsoft Excel, you are welcome to try using this workaround I came up with when i had the same struggle with the old folder dialog.

https://forums.autodesk.com/t5/inventor-customization/ilogic-excel-driven-folder-selection-dialog/m-...

(Only tested using iLogic, I see no reason it wont work in a Addin once the code is converted)

It only works for single folder selections.

 

On the technical side, although the better folder dialog does exist as an option for Windows.Forms.FolderBrowserDialog, the way inventor uses .net or some compiler setting it uses makes it not usable.

Message 6 of 7

WCrihfield
Mentor
Mentor
Accepted solution

That's one way @greggeorgi.

However, after a little more research on this topic, I found a trick that we can use with the Windows.Forms.OpenFileDialog that I believe will make it work just as intended (allow us to select a Folder, and return its full path).  It won't work with the SaveFileDialog.  It's all in the settings.

- set 'CheckFileExists' to False

- set 'CheckPathExists' to True

- set 'ValidateNames' to False

- set a place holder string to the 'FileName' property (I used "Selection")

- set 'Filter' to ""

- set 'AddExtension' to False

- set 'DefaultExt' to ""

 

- then after you use 'ShowDialog":

     - (if you want) you you can check 'FileName' to make sure it isn't empty, and check value of the 'DialogResult' to make sure you didn't Cancel out of the dialog.

- Then I simply use a Replace() method to remove the place holder string from the end of the resulting folder's path

- Then return what's left of the string to the calling parent block of code

 

Here's a  simple iLogic example which uses all of this:

Sub Main
	MsgBox(GetFolder,,"")
End Sub

Function GetFolder() As String
	Dim oFolder As String
	Dim oPlaceHolder As String = "Selection"
	Dim oOpenDlg As New System.Windows.Forms.OpenFileDialog
	oOpenDlg.Title = "Select A Folder."
	oOpenDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	oOpenDlg.Filter = ""
	oOpenDlg.Multiselect = False
	oOpenDlg.RestoreDirectory = False

	oOpenDlg.CheckFileExists = False
	oOpenDlg.CheckPathExists = True
	oOpenDlg.ValidateNames = False
	oOpenDlg.FileName = oPlaceHolder
	oOpenDlg.Filter = ""
	oOpenDlg.AddExtension = False
	oOpenDlg.DefaultExt = ""

	Dim oResult = oOpenDlg.ShowDialog
	If oResult = vbOK Then
		If oOpenDlg.FileName <> vbNullString Then
			oFolder = oOpenDlg.FileName
		Else
			MsgBox("No file was selected. Exiting.", vbOKOnly + vbExclamation, "FILE NOT SELECTED")
		End If
	ElseIf oResult = vbCancel Then
		MsgBox("The dialog was Canceled. Exiting.", vbOKOnly + vbInformation, "CANCELED")
	End If
	oFolder = Replace(oFolder, oPlaceHolder, "")
	Return oFolder
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)

0 Likes
Message 7 of 7

greggeorgi
Contributor
Contributor

That works pretty well!, the only major issue I would say is that if the user clicks on a regular file accidentally (spamming double click on folders) or brushes their keyboard, it will return the file name, or whatever non-empty string is in the input box that is not the placeholder. (If it is empty it appears to not close the dialog anyways).

 

It looks like the returned path always has a slash (\) at, so it may be better to cut off up to the last slash instead of using a static replace. (Still necessary to pre-fill the path with your placeholer)

oFolder = System.IO.Path.GetDirectoryName(oFolder) & "\"

This does not appear to break anything since the other caveat is that to select the folder, you have to navigate into the folder first.

0 Likes