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:

Api Tools 2.0


Recommended Posts

Hi,

I upgrade the Api Tools Chook it now will check if a tool fits into the geometry before chain assignment and translate pocket geometry to the nearest flat surface.

 

Link to comment
Share on other sites

C# Source Code :

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Main.cs" company="Peter Evans, Inc.">
//   Copyright (c) 2020 "Peter Evans, Inc.
// </copyright>
// <summary>

// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ApiTools
{
    using Mastercam.App;
    using Mastercam.App.Types;
    using System.Linq;
    public static class I
    {
        private static System.Collections.Generic.List<T> DistinctSorted<T>(
    this System.Collections.Generic.IEnumerable<T> source, System.Func<T, T, bool> areEqual)
        {
            source = source.OrderBy(x => x);
            var lastElement = default(T);
            var result = new System.Collections.Generic.List<T>();
            foreach (var sourceElement in source)
            {
                if (!areEqual(sourceElement, lastElement) || !result.Any())
                {
                    result.Add(sourceElement);
                    lastElement = sourceElement;
                }
            }
            return result;
        }

    }
    public class Main : NetHook3App
    {
        #region Public Override Methods

        /// <summary>
        /// The main entry point for your NETHook.
        /// </summary>
        /// <param name="param">System parameter.</param>
        /// <returns>A <c>MCamReturn</c> return type representing the outcome of your NetHook application.</returns>
        public override MCamReturn Run(int param)
        {
            var ret = CreateGeometryProfile(true,true);
            var tlslot3 = ChookSdkLibrary.OperationManagerUtilities.CreateOperations.makendmil(3, .75, 1.0, .25, .25, 420, 18000, "T3 0.250 endmill", 180.0);
            ChookSdkLibrary.OperationManagerUtilities.MakePocketOpFromChainsParams Params = new ChookSdkLibrary.OperationManagerUtilities.MakePocketOpFromChainsParams();
                Params.level = 12500;
				Params.clearancePlane = 0.25;
				Params.feedPlane = 0.25;
				Params.topOfStock = 0.0;
				Params.depth_incremental = true;
                
				Params.viewId = 0; // ???
				Params.cuttercompdir = Mastercam.Database.Types.CutterCompDir.CutterCompRight;
				Params.chndir = Mastercam.Database.Types.ChainDirectionType.CounterClockwise;
				Params.FeedRate = 420.0;
				Params.spindlespeed = 18000;
				Params.slot = tlslot3;
            ChookSdkLibrary.OperationManagerUtilities.CreateOperations.MakePocketOpFromChains(Params);
            return ret;
        }

        private static void SelectWireframeInListOfList(System.Collections.Generic.List<int[]> BtmWireframeDatabsePointerList)
        {
            foreach (var WireframeDatabasePointerList in BtmWireframeDatabsePointerList)
            {
                SelectWireFramInList(WireframeDatabasePointerList);
            }
        }

        private static void SelectWireFramInList(int[] WireframeDatabasePointerList)
        {
            foreach (var WireframeDatabasePointer in WireframeDatabasePointerList)
            {
                if (!ApiToolsWrapper.GeometryUtilities.DatabasePointerBits.GetDatabasePointerBits(WireframeDatabasePointer, 2))
                {
                    ApiToolsWrapper.GeometryUtilities.DatabasePointerBits.SetDatabasePointerBits(WireframeDatabasePointer, 2, 1);
                }
            }
        }

        public static MCamReturn CreateGeometryProfile(bool Translate,bool CheckToolFit)
        {
            var TopWireframeDatabsePointerList = new System.Collections.Generic.List<int[]>();
            var BtmWireframeDatabsePointerList = new System.Collections.Generic.List<int[]>();
            var SilhbndWireframeDatabsePointerList = new System.Collections.Generic.List<int[]>();
            int[] DataBasePointerList = ApiToolsWrapper.GeometryUtilities.DataBaseSearch.GetEntitiesMask(65536, true, 0, 0);
            //
            if (DataBasePointerList == null)
            {
                System.Windows.Forms.MessageBox.Show(
                "Error (0) selected solids were detected!"
                + System.Environment.NewLine 
                + "Select (1) solid");
                return MCamReturn.ErrorOccurred;
            }
            if (DataBasePointerList.Length > 1)
            {
                System.Windows.Forms.MessageBox.Show(
                "Error  (" 
                + DataBasePointerList.Length.ToString() 
                + ") selected solids were detected!"
                + System.Environment.NewLine 
                + "Select (1) solid");
                return MCamReturn.ErrorOccurred;
            }
            var faceTagLists = ApiToolsWrapper.GeometryUtilities.SolidTag.GetFlatFaceTagArray(DataBasePointerList[0]);

            var MaxDepth = ApiToolsWrapper.GeometryUtilities.SolidInformation.GetMaxzDepthOnSolid(DataBasePointerList[0]);

            var MinDepth = ApiToolsWrapper.GeometryUtilities.SolidInformation.GetMinzDepthOnSolid(DataBasePointerList[0]);

            if (MinDepth < -.002 && MinDepth > .002)
            {
            System.Windows.Forms.MessageBox.Show("Error  (" 
            + DataBasePointerList.Length.ToString() 
            + ") selected solids were detected at the incorrect zDepth!!"
            + System.Environment.NewLine 
            + "Position the top of the 3D model at Z0.");
                return MCamReturn.ErrorOccurred;
            }

            if (faceTagLists.Count != 3 || faceTagLists == null)
            {
                return MCamReturn.ErrorOccurred;
            }
            ApiToolsWrapper.GeometryUtilities.DatabasePointerBits.BlankUnblankAllEntsMask(0, true, true);
            for (var i = 0; i < faceTagLists.Count; i++)
            {
                switch (i)
                {
                    case 0:
                        {
                            CreateEdgesFromFaceTag(BtmWireframeDatabsePointerList, faceTagLists, i);
                            break;
                        }
                    case 1:
                        {
                            CreateEdgesFromFaceTag(TopWireframeDatabsePointerList, faceTagLists, i);
                            break;
                        }
                    case 2:
                        {
                            if (Translate)
                            {
                                foreach (var faceTag in faceTagLists[i])
                                {
                                    Mastercam.IO.LevelsManager.SetMainLevel(7000);
                                    var eptrarray = ApiToolsWrapper.GeometryUtilities.SolidTag.CreateSurfaceFromFaceTag(faceTag);

                                }
                            }
                            break;
                        }
                }
            }
           
            SelectWireframeInListOfList(TopWireframeDatabsePointerList);
            ApiToolsWrapper.GeometryUtilities.Color.ColorInnerOuterSelectedChain(10, 15, 1000, 10000);
            SelectWireframeInListOfList(BtmWireframeDatabsePointerList);
            ApiToolsWrapper.GeometryUtilities.Color.ColorInnerOuterSelectedChain(20, 25, 2000, 20000);
            if (!ApiToolsWrapper.GeometryUtilities.DatabasePointerBits.GetDatabasePointerBits(DataBasePointerList[0], 2))
            {
                ApiToolsWrapper.GeometryUtilities.DatabasePointerBits.SetDatabasePointerBits(DataBasePointerList[0], 2, 1);
            }
            ApiToolsWrapper.GeometryUtilities.SilhouetteBoundaryUtilities.CreateSilhouetteBoundary(500, 50, 55, 5000, 50000);
            ApiToolsWrapper.GeometryUtilities.Spline.SplineAll();
            ApiToolsWrapper.GeometryUtilities.Delete.DeleteDuplicateChains(1000, 5000, false, true, 0);
            ApiToolsWrapper.GeometryUtilities.Delete.DeleteDuplicateChains(2000, 5000, false, true, 0);
            if (Translate == true)
            {
                ApiToolsWrapper.GeometryUtilities.ApiMacros.TranslateEntitiesToSurfaces();
            }
            if (CheckToolFit == true)
            {
                ApiToolsWrapper.GeometryUtilities.ApiMacros.CheckOffsetSize();
            }
            Mastercam.IO.SelectionManager.UnselectAllGeometry();
           
          
            return MCamReturn.NoErrors;
        }
        
 
        private static void CreateEdgesFromFaceTag(System.Collections.Generic.List<int[]> TopWireframeDatabsePointerList, System.Collections.Generic.List<System.Collections.Generic.List<int>> faceTagLists, int i)
        {
            if (faceTagLists[i] != null)
            {
                foreach (var faceTag in faceTagLists[i])
                {
                    Mastercam.IO.LevelsManager.SetMainLevel(100);
                    var eptrarray = ApiToolsWrapper.GeometryUtilities.SolidTag.CreateSolidEdgesArrayFromFaceTag(faceTag);
                    TopWireframeDatabsePointerList.Add(eptrarray);
                }
            }
        }

        #endregion
    }
}

Requires ApiToolsWrapper.dll (C++/Cli)

Link to comment
Share on other sites
On 5/4/2020 at 11:46 AM, byte said:

if (MinDepth < -.002 && MinDepth > .002)

<this if statement should be || not && since this case would never happen where a number is larger and smaller than a given number at the same time..>

In its current state it is ignored..

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