How to select a drawing view after hiding a dialog?

How to select a drawing view after hiding a dialog?

JBerns
Advisor Advisor
875 Views
3 Replies
Message 1 of 4

How to select a drawing view after hiding a dialog?

JBerns
Advisor
Advisor

Community,

 

I am developing an external iLogic rule that uses a form to complete a drawing title block.

 

A simplified version is show here:

 

PickDrawingViewTest_2020-11-17_8-34-14.png

 

 

 

 

 

 

 

I would like the 'Pick View' button to allow the user to select a drawing view.

 

I used portions of code by @dgreatice  from this post to attempt the drawing view selection:

https://forums.autodesk.com/t5/inventor-customization/select-drawing-view/td-p/8342217 

The code by @dgreatice works well since no dialog box is involved.

 

I added the 'CommandManager.Pick' code to my btnPickView_Click Sub, but Inventor locks-up or freezes after the dialog box is hidden.

 

myForm.Hide
oSelectView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Pick View")
myForm.Show

 

I am not certain if CommandManager.Pick is the best code to use when a dialog is hidden.

 

The goal:

  • Display the dialog box
  • Wait for user to click a button
  • When 'Pick View' is clicked:
    • hide dialog
    • wait for user to pick a drawing view
    • show dialog again
    • process the selected view
  • Wait for next button

I have attached my code for convenience. Any assistance would be appreciated.

Thank you for your time and attention. I look forward to your reply.

 


Kind regards,
Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Accepted solutions (1)
876 Views
3 Replies
Replies (3)
Message 2 of 4

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @JBerns 

I think the problem is that you use ShowDialog. A dialog locks everything else up until it's closed and you never close it - you just hide it 🙂

 

You can set the form to topmost and make it run in the Inventor applications main frame...

Try this 🙂

 

AddReference "System.Drawing"
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
'Option Explicit On

' Pick Drawing View Test
' Rule to allow user to pick a drawing view from a dialog box
'
' Written 2020-11-17  Ver 1.0  by J. Berns


' Class to define the user form
Public Class FormClass

	Inherits System.Windows.Forms.Form

	' Public declarations
	Public myForm As New Form
	Public oDwgDoc As DrawingDocument
	Public oModelDoc As Document

	Public cboScale As New System.Windows.Forms.ComboBox

	Public oLargeFont As System.Drawing.Font = New System.Drawing.Font("Arial", 14)
	Public oSmallFont As System.Drawing.Font = New System.Drawing.Font("Arial", 8)

	Public lblScale As New Label

	Public btnPickView As New Button
	Public btnOK As New Button
	Public btnCancel As New Button

	' When PickView button is clicked
	Public Sub btnPickView_Click(ByVal sender As System.Object, ByVal E As System.EventArgs)
		' Initialize
		''		Dim oDone As Boolean = False
		''		Dim oSelectView As DrawingView
		''		' Loop until NO
		''		Do Until oDone = True
		myForm.Hide

		' ' ' CAUTION - Next line will freeze Inventor

		oSelectView = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Pick View")
		' ' '
		myForm.Show
		'			MyLabel = InputBox("Input label view", "Input Label View", oSelectView.Label.Text)
		'			oSelectView.ShowLabel = True
		'			oSelectView.Name = UCase(MyLabel)
		''			' More views?
		''			WantSelectMore = MsgBox("Want to edit more label views?", vbYesNo, "Input More Labels")
		''			If WantSelectMore = vbYes Then
		''				oDone = False
		''			Else
		''				oDone = True	
		''			End If
		''		Loop
	End Sub

	' When OK button is clicked
	Public Sub btnOK_Click(ByVal sender As System.Object, ByVal E As System.EventArgs)
		myForm.Close
	End Sub

	' When Cancel button is clicked
	Public Sub btnCancel_Click(ByVal sender As System.Object, ByVal E As System.EventArgs)
		myForm.Close
	End Sub

	Public Sub IntializeUserForm

		' Build the scale list
		Dim ScaleList() As String = New String() {"SEE VIEW SCALE", _
		"1"" = 1""", "3/4"" = 1""", "1/2"" = 1""", "1/4"" = 1""", "1/8"" = 1""", _
		"1"" = 1'", "3/4"" = 1'", "1/2"" = 1'", "3/8"" = 1'", "1/4"" = 1'", "3/16"" = 1'", "1/8"" = 1'" }

		'' Comboboxes and Textboxes

		' Scale
		lblScale = New Label
		With lblScale
			.Name = "lblScale"
			.Location = New System.Drawing.Point(10, 20)
			.Size = New System.Drawing.Size(100, 30)
			.Font = oLargeFont
			.Text = "SCALE:"
		End With

		'		cboScale = New ComboBox
		With cboScale
			.Name = "cboScale"
			.Location = New System.Drawing.Point(10, 50)
			.Items.AddRange(ScaleList)
			.Size = New System.Drawing.Size(230, 30)
			.Font = oLargeFont
		End With

		'' BUTTONS

		btnPickView = New Button
		With btnPickView
			.Name = "btnPickView"
			.Location = New System.Drawing.Point(160, 25)
			.Width = 80
			.Height = 20
			.Font = oSmallFont
			.Text = "Pick View"
			AddHandler.Click, AddressOf btnPickView_Click
		End With

		btnOK = New Button
		With btnOK
			.Name = "btnOK"
			.Location = New System.Drawing.Point(20, 100)
			.Width = 100
			.Height = 30
			.Font = oLargeFont
			.Text = "OK"
			AddHandler.Click, AddressOf btnOK_Click
		End With

		btnCancel = New Button
		With btnCancel
			.Name = "btnCancel"
			.Location = New System.Drawing.Point(135, 100)
			.Width = 100
			.Height = 30
			.Font = oLargeFont
			.Text = "Cancel"
			AddHandler.Click, AddressOf btnCancel_Click
		End With

		' Format the UserForm
		With myForm
			' Set up form	
			.FormBorderStyle = FormBorderStyle.FixedToolWindow
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 270
			.Height = 190
			.TopMost = True
			.Text = "Pick Drawing View Test"
			.Name = "PickDrawingViewTest"
			.IsMdiContainer = False

			' Add controls
			'' Labels
			.Controls.Add(lblScale)
			'' Edit boxes
			.Controls.Add(cboScale)
			'' Buttons
			.Controls.Add(btnPickView)
			.Controls.Add(btnOK)
			.Controls.Add(btnCancel)
		End With

	End Sub

	Public Sub Main()

		' Verify that the active document is a drawing, notify otherwise
		If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then

			' Get the active document
			oDwgDoc = ThisApplication.ActiveDocument

			' Initialize the user form
			IntializeUserForm
			myForm.TopMost = True
			Dim wrapper = New HwndWrapper(ThisApplication.MainFrameHWND)

			' Display the form
			myForm.Show(wrapper)

			' Update document
			InventorVb.DocumentUpdate()

		Else
			MessageBox.Show("Please run this routine from a drawing file.", _
			"WARNING", _
			MessageBoxButtons.OK, MessageBoxIcon.Warning _
			)
			Exit Sub
		End If

	End Sub

End Class
Class HwndWrapper

	Implements IWin32Window



	Private ReadOnly m_hWnd As IntPtr



	Public Sub New(ByVal hWnd As IntPtr)

		m_hWnd = hWnd

	End Sub



	Public Sub New(ByVal hWnd As Integer)

		m_hWnd = CType(hWnd, IntPtr)

	End Sub



	Public ReadOnly Property Handle As IntPtr Implements IWin32Window.Handle

		Get

			Return m_hWnd

		End Get

	End Property

End Class
Message 3 of 4

JBerns
Advisor
Advisor

@JhoelForshav,

 

Awesome solution! You continue to impress me, Jhoel! Your Expert Elite status is well deserved.

 

Kind regards,

Jerry

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
Message 4 of 4

JhoelForshav
Mentor
Mentor
0 Likes