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:

Passing Data from Nethook to post?


Recommended Posts

Hello,

 

There are couple of options.

 

One is to create a text file that could be later read by the post in the form of buffer file.

 

The other is to use arguments in the dll() post function (as I know this method only works if the nethook is called from the postprocessor).

 

I use this snippet of code to read the computer user name and pass it to the postprocessor:

 

 

         	
               // PostingManager is a class that contains a single Property -
           	// 'PostProcessorArguments' and this property is a "ValueType".
           	// So we 'get' the data from this property.     	
           	ValueType postArgs = Mastercam.Posting.PostingManager.PostProcessorArguments;
           	// Now we cast postArgs (a ValueType) to 'PostProcessorArgumentsType'
           	// So we can access that data within...
           	// PostProcessorArgumentsType is a structure containing (3) strings.
           	Mastercam.Posting.Types.PostProcessorArgumentsType args = (Mastercam.Posting.Types.PostProcessorArgumentsType)postArgs;
           	args.Argument1 = MyUserName;
           	// We can also 'set' using this property to send data back to the PST !
           	Mastercam.Posting.PostingManager.PostProcessorArguments = args; // Update the values back to the PST

 

 

 

But I am sure Roger will be able you to help you better :)

 

regards

Link to comment
Share on other sites

UPDATE (14-May-2013)

See this posting if you're attempting to retrieve values back into the PST from your NETHook (or CHook) DLL !

 

---------------------------------------------------------------------------------------------------------------------------------------

 

To call a NETHook (or CHook) from a running PST, you use the dll() command

 

# PST code...

#============================================================================
sDLL : "C:\Program Files\mcamx5\chooks\ReturnValueToPost.dll" # The NETHook (CHook) dll to be called
sSPACE : " " # Contains a SPACE character
sSQ : "'" # Contains a Single Quote character
sDQ : '"' # Contains a Double Quote character

# There are (3) special string variables that are used to send/receive the arguments
sARG1 : "arg1" # Load with the arg#1 value you wish to pass to the AddOn
sARG2 : "arg2" # Load with the arg#2 value you wish to pass to the AddOn
sARG3 : "arg3" # Load with the arg#2 value you wish to pass to the AddOn

sPARAMS : "" # This string variable will be the command parameter line passed to the DLL

result : 0 # Needed for the dll() command call

pcallDLL # This post block is called from "somewhere" within your PST

# Build up the parameter string (the following is really ONE long line of code).
# Each argument is enclosed in (double) quotes and separated by a [space].
# This means the the entire command line passed in dll() must be enclosed in (single) quotes!

sPARAMS = sSQ + sDQ + sARG1 + sDQ + sSPACE + sDQ + sARG2 + sDQ + sSPACE + sDQ + sARG3 + sDQ + sSQ

# Now make the call. 
result = dll (sDLL, sPARAMS)

# AFTER the dll() call, the (3) special post variables (sARG1, sARG2, sARG3) will contain either;
# The original values, if the NETHook (or CHook) did NOT update them.
# Or with the *new* values, if the NETHook (or CHook) did update them.

 

 

 

Now in your NETHook (called from a PST using the dll() command),

you can retrieve the arguments that were passed in

and if you wish, set the values that will be "returned" to the PST.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Security.Principal;

using Mastercam.App;
using Mastercam.App.Types;

namespace NetHookApp
{
#region class NETHOOK

public class ReturnValueToPost : NetHook2App
{
public override MCamReturn Run(int param)
{
bool ret = true; // Assume succcess 
string username = string.Empty;

// Get the post args
ValueType postArgs = Mastercam.Posting.PostingManager.PostProcessorArguments;

// We MUST cast the 'postAregs' to the appropriate type in order to gain access to them!
Mastercam.Posting.Types.PostProcessorArgumentsType args = (Mastercam.Posting.Types.PostProcessorArgumentsType)postArgs;

// Show the (3) arguments passed in from the PST
string msg = string.Format("\narg1={0}\narg2={1}\narg3={2}", args.Argument1, args.Argument2, args.Argument3);
MessageBox.Show(msg, "ReturnValueToPost - NetHook", MessageBoxButtons.OK, MessageBoxIcon.Information);

try
{
username = WindowsIdentity.GetCurrent().Name; // Get the "name" of the currently logged in user
MessageBox.Show(username, "NetHook Run", MessageBoxButtons.OK, MessageBoxIcon.Information); // <DEBUG>
}
catch (Exception e)
{
Mastercam.IO.DialogManager.OK(e.ToString(), "Error while calling ReturnValueToPost");
ret = false;
}

if (ret)
{
args.Argument1 = username;
}
else
{
args.Argument1 = "-unknown-";
}

// Fill in the other 2, just because we can...
args.Argument2 = "I'm 2nd";
args.Argument3 = "I'm 3rd";

// We MUST update the systems's PostProcessorArguments with our (local) values
// in order for them to get back to the PST !!!
Mastercam.Posting.PostingManager.PostProcessorArguments = args;

return (ret ? MCamReturn.NoErrors : MCamReturn.ErrorOccurred);
}
}
#endregion class NETHOOK
}

Link to comment
Share on other sites

Thanks a bunch! that helps me alot in my endevours.

 

now one more question, i tried reading from a buffer in the post but couldn't quite get this to work.

 

do you have an example of a post file reading form a user defined buffer(text file)

 

thanks!

Link to comment
Share on other sites
  • 1 year later...

I've found that while I'm able to pass string literals from my post, to my dll, when I attempt to pass a variable, it comes up empty.

 

example post code:



pcallDLL # DONE PROCESSING, CALL MasterCam .DLL WITH PARAMETERS
#============================================================================
sDLL : "GetPostParams.dll" # The NETHook (CHook) dll to be called
sSPACE : " " # Contains a SPACE character
sSQ : "'" # Contains a Single Quote character
sDQ : '"' # Contains a Double Quote character
sncfilename : "-unknown-" # default file name
sncfilename = spathnc$ + snamenc$ + sextnc$ # path + name + extension


# There are (3) special string variables that are used to send/receive the arguments
sARG1 : sncfilename # Load with the arg#1 value you wish to pass to the AddOn
sARG2 : "test one" # Load with the arg#2 value you wish to pass to the AddOn
sARG3 : "test two" # Load with the arg#2 value you wish to pass to the AddOn

sPARAMS : "" # This string variable will be the command parameter line passed to the DLL

result : 0 # Needed for the dll() command call

pcallDLL # This post block is called from "somewhere" within your PST


# Build up the parameter string (the following is really ONE long line of code).
# Each argument is enclosed in (double) quotes and separated by a [space].
# This means the the entire command line passed in dll() must be enclosed in (single) quotes!

sPARAMS = sSQ + sDQ + sARG1 + sDQ + sSPACE + sDQ + sARG2 + sDQ + sSPACE + sDQ + sARG3 + sDQ + sSQ

# Now make the call. 
result = dll (sDLL, sPARAMS)

# AFTER the dll() call, the (3) special post variables (sARG1, sARG2, sARG3) will contain either;
# The original values, if the NETHook (or CHook) did NOT update them.
# Or with the *new* values, if the NETHook (or CHook) did update them.

#============================================================================

 

when the code is posted, my .dll pops up with a message box of the params it has grabbed from the post ....

 

arg1 =

arg2 = test one

arg3 = test two

 

 

 

any ideas what Im doing wrong here ?

Link to comment
Share on other sites

Did you test to see what you think you're sending the DLL, is what is actually happening?

Add some diagnostics to your PST file ->

 

pcallDLL # This post block is called from "somewhere" within your PST

   "- Executing pcallDLL -", e$
   "sARG1 = ", sARG1, e$
   "sARG2 = ", sARG2, e$
   "sARG3 = ", sARG3, e$

 

The arguments passed from the PST to the DLL are strings.

So if you need to pass a numeric PST variable, you have to convert it into a string

 

sARG1 = no2str(myNumberVar)

 

And the reverse is true.

If you are taking values back from the DLL into the PST and need that has numeric data,

you'll have to parse that string into a numeric value.

 

myNewNumber = rpar(sARG1, 1)

Link to comment
Share on other sites
  • 8 months later...

Roger,

 

Thanks for the sample code. It's been a big help.

 

I used this code to pass the NC-code revision to a .csv file to then be read, incremented, saved, then passed back to the PST. I have everything working except the passing back to the PST. I have the message box displaying the correct data before and after the "Mastercam.Posting.PostingManager.PostProcessorArguments = args;" yet the "sARG3 = ", sARG3, e$ is not updated in the PST.

 

What am I doing wrong?

 

Thanks,

-Pat

Link to comment
Share on other sites

What version of Mastercam ?

 

Can you give me your code?

 

I've not looked at the data passing from PST->NETHook and NETHook data->PST stuff in a long time, but it should work.

 

well, PST->NETHook gets used here daily, and never had a problem.

Link to comment
Share on other sites

well, PST->NETHook gets used here daily, and never had a problem.

 

I have the PST->NETHook working, the problem is that its not passing the updated value back to the PST. I'm on X5 so not sure if that is the problem.

Link to comment
Share on other sites

For anyone that stumbles upon this topic in the future.

There is some erroneous information in my previous posts. ->

 

# AFTER the dll() call, the (3) special post variables (sARG1, sARG2, sARG3) will contain either;

# The original values, if the NETHook (or CHook) did NOT update them.

# Or with the *new* values, if the NETHook (or CHook) did update them.

 

This would make you believe that the values coming back from your NETHook (or CHook) DLL to the PST

would be accessed within your (.PST) Post Processor via these (3) variables ->

"sARG1 = ", sARG1, e$

"sARG2 = ", sARG2, e$

"sARG3 = ", sARG3, e$

 

^This is incorrect.^

 

To be able to get the (string) values returned to the PST you must use these (3) pre-defined post string variables ->

These are these post processor variables that receive the data back from the NETHook (or CHook) DLL

spost_arg_0$

spost_arg_1$

spost_arg_2$

 

Example of retrieving in the PST the values passed back from the NETHook (or CHook) DLL -->

 

# Postblock that we call to “execute” our NETHook (or CHook) DLL
pcallDLL
  # This builds the “command line arguments" that will be passed to the NETHook (or CHook)
  sPARAMS = sSQ + sDQ + sARG1 + sDQ + sSPACE + sDQ + sARG2 + sDQ + sSPACE + sDQ + sARG3 + sDQ + sSQ
  # It will be a string composed of (3) strings, each separated with a (single) space, like this ->
  # ' "Arg0" "Arg1" "Arg2" '
  # Call the NETHook (or CHook) DLL [ sDLL here is the FULL PATH to your DLL ]
  result = dll (sDLL, sPARAMS)
  # DEBUG – Show the result of the call to the DLL
  "DLL call result (1 = success) = ", *result, e$
  "ReturnedArg1 = ", spost_arg_0$, e$
  "ReturnedArg2 = ", spost_arg_1$, e$
  "ReturnedArg3 = ", spost_arg_2$, e$

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