Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

Focus change between Mastercam and NET-Hook


Recommended Posts

HI Guys!

 

I just realized now, that after many C-Hook and NET-Hook, I always made a straight forward runoffs without any interactivity between Mastercam and my app.

They always run in one direction: Set all stuff -> User input -> Draw and/or Draw -> Import OPs

 

Now I working on a project, which asks for geometry from user and after selection it needs to be to return to my Windows Form.

What I would to do (and I did halfway) after pushing a button on my Form it should be minimize/hide, then Mastercam ask for a geo then return

to my Form (maximize to original size or unhide).

Code for Selecting a geo and for button is OK, no problem with that. But when I push the button, Mastercam ask for a geo but I can not to select any

because my Form remains and not allow me to select anything in MCAM.

I found many possible solution for this issue, but which is the best and safe practice?

Thank you for any input!

 

Visual Basic, VS Express 2013, MCAM X7, NET_Hook3_0

 

Private Sub LineSelect_Click(sender As Object, e As EventArgs) Handles LineSelect.Click

           Dim GeoMask As GeometryMask = New GeometryMask(False)
           GeoMask.Lines = True
           Dim SelectedGeo As Mastercam.Database.Geometry = SelectionManager.AskForGeometry("Select one line", GeoMask)

End Sub

Link to comment
Share on other sites

Well, if your Form is shown as non-modal (not ShowDialog), you should be able to click on the entities, just tried it, and it works ......

 

however ...

 

you have to click on the graphics screen first to set the focus, so your first click is wasted.

 

There is another way to set the focus to the graphics window beforehand, using Interop API calls. This type of coding is not guaranteed to work between major versions, tho. Lemme know if you are interested ...

  • Like 1
Link to comment
Share on other sites

Well, if your Form is shown as non-modal (not ShowDialog), you should be able to click on the entities, just tried it, and it works ......

It is ("Dialog.ShowDialog()") and make sense for me as a possible way.

 

There is another way to set the focus to the graphics window beforehand, using Interop API calls. This type of coding is not guaranteed to work between major versions, tho. Lemme know if you are interested ...

I am Rocheey!

I open for everything, thank you in advance and your effort.

Link to comment
Share on other sites

If you dont show the form modally, then your form will disappear behind Mastercam when the user selects.

So, you set your form to TopMost, but then you lose the first click when the user is prompted to select onscreen.

 

 

Here is a module I use for such things. Create a new module in VB, erase all text in it, and paste this in:

 

Imports System.Runtime.InteropServices
Imports System.Diagnostics

Module modMcxHwnd
Delegate Function EnumChildWindProc(ByVal hWnd As System.IntPtr, ByVal lParam As Integer) As Boolean

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function EnumChildWindows(ByVal hWndParent As System.IntPtr, ByVal lpEnumFunc As EnumChildWindProc, ByVal lParam As Integer) As Boolean
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Function GetClassName(ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function

<DllImport("user32.dll", SetLastError:=True)> _
Function SetActiveWindow(ByVal hWnd As IntPtr) As IntPtr
End Function

Private lstChildrenHandles As New System.Collections.Generic.List(Of IntPtr)
Public McxMainHwnd As IntPtr = IntPtr.Zero	 ' Handle to main MasterCam window
Public McxOpMgrHwnd As IntPtr = IntPtr.Zero	 ' Handle to the Op Manager
Public McxGfxHwnd As IntPtr = IntPtr.Zero	 ' Handle to the Graphics Window

Sub GetMcxHandles()
 Dim RetVal As Integer
 Dim procC As New EnumChildWindProc(AddressOf EnumChild)
 Dim procD As New EnumChildWindProc(AddressOf EnumChild)
 Dim stringBuffer As String = New String(Chr(0), 256)

 ' get the current MasterCam Process
 Dim myProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess()

 ' Get the window handle to main mastercam window
 McxMainHwnd = myProcess.MainWindowHandle

 ' loop thru and look at all the Mcx Child Windows
 lstChildrenHandles.Clear()
 EnumChildWindows(McxMainHwnd, procC, 0)

 For Each wHndChild As IntPtr In lstChildrenHandles
	 RetVal = GetWindowText(wHndChild, stringBuffer, 256)
	 ' Get the window title of each window
	 Dim sClassName As New System.Text.StringBuilder("", 256)
	 Dim childTitle As String = stringBuffer.Substring(0, RetVal)
	 Call GetClassName(wHndChild, sClassName, 256)

	 If childTitle = "Op Manager" Then ' found Op Manager
		 McxOpMgrHwnd = wHndChild
	 ElseIf childTitle = "" And sClassName.ToString = "Afx:0000000140000000:ab:0000000000000000:0000000001900011:0000000000000000" Then ' main ghraphics window
		 McxGfxHwnd = wHndChild
	 End If

 Next
End Sub

Private Function EnumChild(ByVal hWnd As IntPtr, ByVal lParam As Int32) As Boolean
 lstChildrenHandles.Add(hWnd)
 EnumChild = True
End Function

End Module

 

 

When you call the sub "GetMcxHandles", it finds Mastercam in memory, and, specifically, the Graphics window.

 

To use in your code, modify your sub like the following:

 

 Dim GeoMask As GeometryMask = New GeometryMask(False)
 GeoMask.Lines = True

 GetMcxHandles()				 ' call main routine to find mastercam in memory
 SetActiveWindow(McxGfxHwnd)	 ' set the focus to MasterCam gfx window

 Dim SelectedGeo As Mastercam.Database.Geometry = SelectionManager.AskForGeometry("Select one line", GeoMask)

 SetActiveWindow(Me.Handle)	 ' set the focus back to your form

 

This code was tested under MasterCam X7, and 64bit. The calls should work in 32 bit, tho.

  • Like 2
Link to comment
Share on other sites

Peter,

Is this what you are wanting to do?

Your NETHook will display a modeless Form.

User clicks a “select” button on your Form.

You now want your dialog to hide itself and now all the user sees is Mastercam asking them to “Select one line”.

Once the user has made the pick, your dialog reappears.

Correct?

 

Unless I do not understand correctly, this appears to be do-able without needing to Enumerate Windows.

I created a simple tester NETHook (X7 using NETHook2) and I don’t see this (TopMost is enabled on my Form) ->

you have to click on the graphics screen first to set the focus, so your first click is wasted.

 

This worked for me ->

*My NETHook was done in C#, but that shouldn't make a difference.

 

Private Sub SelectGeometryButtonclick(sender As Object, e As EventArgs)
Me.Hide()
Dim GeoMask As GeometryMask = New GeometryMask(False)
GeoMask.Lines = True
Me.Geometry = SelectionManager.AskForGeometry("Select one line", geoMask)
Me.Show()
Me.ShowData()
End Sub

 

Notes:

"Geometry" being set by the AskForGeometry call is a data property of the Form class.

"ShowData" is just a method that displays a message box showing the line's coordinate data

This pops up as soon as I select a line on the Graphics screen, no "extra" mouse click in the Grpahic screen before being able to select a line was required.

  • Like 1
Link to comment
Share on other sites

I had thought, rightly or wrongly (is that a real word?) that he wanted the window to remain visible, and without .TopMost, the window will disappear behind MasterCam on user selection, much like hiding the form. And once .Topmost is used, I needed an extra click to set the focus back to MasterCam.

 

Also, the IDE gives me the error that "Geometry' is not a member of 'Form.Form1". The only non-mcx reference I could find in the object browser for 'Geometry' was "System.Windows.Media.Geometry", but it doesn't contain any Public Members.

 

A search for .Showdata in the Object browser didn't return any references. :(

Link to comment
Share on other sites
I had thought, rightly or wrongly (is that a real word?) that he wanted the window to remain visible, and without .TopMost, the window will disappear behind MasterCam on user selection, much like hiding the form. And once .Topmost is used, I needed an extra click to set the focus back to MasterCam.

 

I was thinking he wanted to hide his Form while the user was making the geometry selection.

That's why I was asking Peter for details - Is this... what you wanted your NETHook to do?

 

A modeless dialog will disappear "behind" Mastercam when you click on Mastercam, which makes Mastercam the active window, so it comes up front.

This is because your modeless Form does not really have a "parent" window

Mastercam is not the parent window to your Form!

To prevent that from happening you set the TopMost property on your Form = true.

Now you can control when your Form gets hidden and shown.

When I did this -> Hide Form / AskForGeometry / Show Form.

I did not need an "extra" click in the Mastercam graphics area before I could select the geometry.

 

But...

If you leave the your Form on-screen, you don't do -> Me.Hide())

Then you will need that extra click to make Mastercam the "active" window.

 

Isn't Windows just great!!! :X

You will find more gotchas the more you use modeless dialog!

 

As for the notes in my previous post, some clarification...

"Geometry" being set by the AskForGeometry call is a data property of the Form class.

 

The "Geometry" variable being set in my sample code snippet is a property I defined in my Form's class,

So you would need to define it your Form's class.

Public Property Geometry() As Geometry
Get
Return m_Geometry
End Get
Private Set
m_Geometry = Value
End Set
End Property
Private m_Geometry As Geometry

 

"ShowData" is just a method that displays a message box showing the line's coordinate data

This is a method I defined in my Form's class.

I used it to display the data about this line on-screen, so that I would know exactly which mouse click did the geometry selection.

I was trying to find if this "extra" mouse click was really needed. So this would pop the instant the AskForGeometry call "did the pick"

telling me if it was the 1st or 2nd click on a line in the graphics screen was "doing the pick" of the line.

 

Private Function FormatDecimal(value As Double) As String
Return String.Format(value.ToString("F4"))
End Function

Private Function FormatPoint(pt As Mastercam.Math.Point3D) As String
Return String.Format("X {0}, Y {1}, Z {2}", Me.FormatDecimal(pt.x), Me.FormatDecimal(pt.y), Me.FormatDecimal(pt.z))
End Function

Private Sub ShowData()
If Me.Geometry IsNot Nothing Then
Dim line As Mastercam.Curves.LineGeometry = TryCast(Me.Geometry, Mastercam.Curves.LineGeometry)
If line IsNot Nothing Then
MessageBox.Show(String.Format(vbLf & "PT#1:{0}" & vbLf & "PT#2:{1}", Me.FormatPoint(line.Data.Point1), Me.FormatPoint(line.Data.Point2)), "Line selected:", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub

 

*I assume the above code snippets are "good" VB, I converted them from my C# code.

Link to comment
Share on other sites

Peter,

Is this what you are wanting to do?

Your NETHook will display a modeless Form.

User clicks a “select” button on your Form.

You now want your dialog to hide itself and now all the user sees is Mastercam asking them to “Select one line”.

Once the user has made the pick, your dialog reappears.

Correct?

 

Correct Roger! Your's so simple but works well also. Rocheey's solution is also perfect.

Guys, I've learned a lot with these again and I had a good chance to play with all the options.

I also played with Modal/Modeless dialogs and with TopMost property.

 

The other isde of the coin is that I just came back to play with NET-Hooks after a long time, so probably this is why I

missed the .Hide() - .Show() methods... :turned:

But thank you both for helps, clear explanations and effort!!! :unworthy:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...