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:

F# NETHOOKS?


Recommended Posts

Hi,

I'm currently looking for any templates if they exist to create a NETHOOK using f#. :) I think I could put it to good use but so far I haven't got it working with mastercam and I am hesitant to start interoping right out of the gate I would like to do testing using F# only if that is possible.

Thanks,

Link to comment
Share on other sites

Sounds good! I will proceed then, I wasn't sure if this was actually viable or not, I'm trying to write my entry function now here is what I have so far:

#light                         // optional these days
module fsharplib
open System
open Mastercam.App;
open Mastercam.App.Types;
open Mastercam.Database;
open Mastercam.Database.Types;
open Mastercam.GeometryUtility;
open Mastercam.IO;
open System.Numerics 
//F#:
    type ovrrclss =
      abstract Run : param : int -> MCamReturn
      default x.Run(param : int) = MCamReturn.NoErrors

    type Main () = 
     inherit NetHook3App ()
     override x.Run(param : int) = MCamReturn.NoErrors

//this decompiles to:

//C#:

// fsharplib
using Mastercam.App;
using Mastercam.App.Types;
using Microsoft.FSharp.Core;
using System;

[CompilationMapping(SourceConstructFlags.Module)]
public static class fsharplib
{
	[Serializable]
	[CompilationMapping(SourceConstructFlags.ObjectType)]
	public class ovrrclss
	{
		public override MCamReturn Run(int param)
		{
			return MCamReturn.NoErrors;
		}
	}

	[Serializable]
	[CompilationMapping(SourceConstructFlags.ObjectType)]
	public class Main : NetHook3App
	{
		public override MCamReturn Run(int param)
		{
			return MCamReturn.NoErrors;
		}
	}
}

Which looks about right, although we shall see!! 😎

Link to comment
Share on other sites
On 10/7/2019 at 3:53 PM, Roger Martin from CNC Software said:

Peter,

There is not a project template for F#.

Since F# is .NET, you can create a NET-Hook Add-In using F# that uses the NET-Hook API.

I did get it working, finally, here is the source code of my project. It creates a blank winform. 📜 I will try to translate some of the more complicated Mastercam functions now. I only startled looking into F# recently, it is very strongly typed and has some other features such as default immutability. I am hoping it will help  me cut down on amount of code I have to deal with, and to help develop apps in a more time efficient manner.(I'm not a very fast typer.

#light                         // optional these days
namespace fsharplib
open System
open Mastercam.App;
open Mastercam.App.Types;
open Mastercam.Database;
open Mastercam.Database.Types;
open Mastercam.GeometryUtility;
open Mastercam.IO;
open System.Numerics 
open System.Drawing
open System.Windows.Forms
open FSharp.Configuration

// reference the type provider dll
open FSharp.Configuration

// Let the type provider do it's work
type Resource1 = ResXProvider<file="Resource1.resx">
     
     //
    type Main () = 
     inherit NetHook3App ()
     ///the nethook entry point
     override x.Run(param : int) = 
          //body
          let obj1 = new Form()//Declare a Windows Form (Winform)
          let crtwnfrm = Application.Run(obj1)//create an empty form
          MCamReturn.NoErrors // return value
     

)

Link to comment
Share on other sites

yeah converting code from c# to f# is straightforward

  //
    type Main () = 
     inherit NetHook3App ()
     ///the nethook entry point
     override x.Run(param : int) = 
          //body
          let leveltochain = 1//chain on level 1
          //chain all closed wireframe entities on level one
          let chains = ChainManager.ChainAll(true, false, null, Mastercam.Database.Types.ChainDirectionType.Clockwise, leveltochain)
          //handle errors
          if chains.Length = 0 || chains = null then MCamReturn.ErrorOccurred
          else 
          //sort chains by area
          let err = ChainManager.SortChains(chains, ChainManager.SortType.Area, ChainManager.SortOrder.Descending) = false 
          //handle errors
          if not err then MCamReturn.ErrorOccurred
          else 
          for i in chains do
          //convert all closed chains to parametric splines
            let geom = GeometryCreationManager.CreateParametricSplineFromChain(i, 0.005, Mastercam.GeometryUtility.Types.OriginalCurvesDisposition.Delete, 500)
            //commit the new geometry to the database
            geom.Commit()
          MCamReturn.NoErrors // return value
         

 

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