CamBam
News:
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 12, 2019, 19:31:09 pm


Login with username, password and session length


Pages: 1 ... 3 4 [5] 6
  Print  
Author Topic: [VBS - C# - VB .net] parts of code (snippet)  (Read 63523 times)
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5676



View Profile WWW
« Reply #60 on: July 21, 2016, 18:40:08 pm »

Create Silhouettes (vb, vbs)

Calling the same function as the main menu

Code:
Sub main

    Dim su as new SurfaceUtils()

    su.InsertSurfaceSilhouettes(CamBamUI.MainUI.ActiveView)

End Sub

with the CamBam.CAD.SurfaceSilhouette2() function

Code:
'* Vb script
'* create silhouttes from selected surfaces
'* dh42 07/2016
'*****************************************

Sub main()

Dim ent As Entity

For Each ent In CamBamUI.MainUI.ActiveView.SelectedEntities
If TypeOf ent Is Surface Then
DoSilhouette(ent)
End If
Next

End Sub

Sub DoSilhouette(surf)

Dim space as Double = 2

Dim ss2 As SurfaceSilhouette2 = New SurfaceSilhouette2(surf, space)

ss2.BackFaceCulling = True

ss2.CalculateLayers()

Dim MyList As System.Collections.Generic.List(Of Entity) = New System.Collections.Generic.List(Of Entity)()
For i As Integer = 0 To ss2.ZLayers.Length - 1
If ss2.Layers(i) IsNot Nothing Then
ss2.Layers(i).Flatten(ss2.ZLayers(i))
For Each MyShape As ShapeListItem In ss2.Layers(i)
MyList.Add(MyShape.Shape)
Next
End If
Next

CamBamUI.MainUI.ActiveView.CADFile.Add(MyList)

End Sub

++
David
Logged
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5676



View Profile WWW
« Reply #61 on: October 11, 2016, 21:03:27 pm »

PlaneSlice (vb, vbs)

public Polyline[] PlaneSlice(char Axis, double offset);

First select one or more 3D surfaces then run the script.

the surfaces are sliced at +5.0 in the Z axis on this example.

the resulting polylines need to be joined.

Code:
'PlaneSlice  VB script, dh42 2016

    Sub main()

        Dim su As Surface
        Dim pl() As Polyline ' polyline array

        For Each ent As entity In view.SelectedEntities

            If TypeOf ent Is Surface Then
                su = ent
                pl = su.PlaneSlice("Z", 5.0)
                addslice(pl)
            End If
        Next

    End Sub

    Sub addslice(pl_array() As Polyline)

        'adds the polylines to the drawing

        For Each p As polyline In pl_array
            doc.add(p)
        Next

    End Sub
Logged
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5676



View Profile WWW
« Reply #62 on: October 11, 2016, 21:22:46 pm »

ToolpathsToGeometry

public static void ToolpathsToGeometry(ICADView view);
public static void ToolpathsToGeometry(ICADView view, CAMPart part);
public static void ToolpathsToGeometry(ICADView view, List<MachineOp> mops);
public static void ToolpathsToGeometry(ICADView view, MachineOp mop);


Code:
' New CamBam VBScript

sub main
  CamBam.CAM.CAMUtils.ToolpathsToGeometry(view)
end sub
« Last Edit: October 11, 2016, 21:26:07 pm by dh42 » Logged
onekk
Wookie
****
Offline Offline

Posts: 497


View Profile
« Reply #63 on: October 19, 2016, 22:23:28 pm »

Some code to test, but it is C#.

In teory it work with NET 3.5 (4.0 add some other directory)

Code:
string syspath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
CamBam.ThisApplication.AddLogMessage(syspath);

It will output the %appdata% directory in windows and the /home/<user>/.config directory in Linux, if it is portable it will be the code to use to obtain the "user" directory path.

Code:
string path = FileUtils.GetFullPath(Path.GetDirectoryName(Application.ExecutablePath)) + "/plugins/BoxAuto_xL.dll";
string Version = "";
bool exists = File.Exists(path);


if (exists == true) {
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo (path);
Version ="V - " + fvi.ProductVersion;
} else {
Version = String.Format("File {0} not found",path);
}


This code is working on Linux the original code from BoxAuto Plugin gave an error. The other fields in fvi. seems to do nothing in Linux, but I've not investigated well.

Code:
assembly: AssemblyTitle ("BoxAuto_xL")] ==> fvi.fvi.FileDescription (Very Strange)
[assembly: AssemblyDescription ("")]
[assembly: AssemblyConfiguration ("")]
[assembly: AssemblyCompany ("")]  ==> fvi.CompanyName
[assembly: AssemblyProduct ("")] ==>  fvi.ProductName
[assembly: AssemblyCopyright ("carlo")] ==>  fvi.LegalCopyright
[assembly: AssemblyTrademark ("")]  ==>  fvi.LegalTrademark
[assembly: AssemblyCulture ("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion ("0.9.*")]

At least with Monodevelop in the resource directory the field above are "manually" fillable in the Assemblyinfo.cs.

the ==> Above are the name of the corresponding variable, (at least in Linux with the latest stable Mono version)

I have to find the proper place where they are "fillable" in the GUI interface of Monodevelop.

« Last Edit: October 19, 2016, 23:09:10 pm by onekk » Logged

Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam
onekk
Wookie
****
Offline Offline

Posts: 497


View Profile
« Reply #64 on: October 20, 2016, 15:33:36 pm »

A little Plugin to Construct a line perpendicular to another line starting from a line (polyline of two points) and an arbitrary point.

http://www.cambam.co.uk/forum/index.php?topic=2878.msg48689#msg48689

Posted there due to the title of Topic.
« Last Edit: October 20, 2016, 16:31:36 pm by dh42 » Logged

Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam
onekk
Wookie
****
Offline Offline

Posts: 497


View Profile
« Reply #65 on: October 20, 2016, 16:46:00 pm »

Some things obtained when working:

The index and the names of the standard voices of the Menu Edit,

Code:
ui.Menus.mnuEdit.DropDownItems[i]

Obtained with a loop and a Console.WriteLine() (In Linux it's not possible to copy from the LogMessage window of CamBam)

Maybe useful for someone (At least for me "in the beginnings" was hard to guess the names and match them)

Code:
index 0 Name: mnuEditExplode
index 1 Name: mnuEditJoin
index 2 Name: mnuEditOffset
index 3 Name: mnuEditOpenOffset
index 4 Name: mnuEditUnion
index 5 Name: mnuEditSubtract
index 6 Name: mnuEditIntersection
index 7 Name: mnuEditTrim
index 8 Name: mnuEditFillet
index 9 Name: mnuEditIntersectionPoints
index 10 Name: mnuEditBreakAtIntersections
index 11 Name: toolStripMenuItem6
index 12 Name: mnuEditConvertTo
index 13 Name: mnuEditTransform
index 14 Name: mnuEditPolyline
index 15 Name: mnuEditSurface
index 16 Name: toolStripMenuItem7
index 17 Name: mnuEditSelectAll
index 18 Name: mnuEditSelectAllOnLayer
index 19 Name: toolStripMenuItem8
index 20 Name: mnuEditCut
index 21 Name: mnuEditCopy
index 22 Name: mnuEditPaste
index 23 Name: toolStripMenuItem9
index 24 Name: mnuEditUndo
index 25 Name: mnuEditRedo


Same for:

Code:
ui.ViewContextMenus.ViewContextMenu.Items

Code:
index 0 Name: viewToolStripMenuItem
index 1 Name: transformToolStripMenuItem
index 2 Name: editToolStripMenuItem
index 3 Name: mnuDraw
index 4 Name: machineToolStripMenuItem
index 5 Name: toolStripMenuItem2
index 6 Name: mnuSelectAll
index 7 Name: mnuSelectAllOnLayer
index 8 Name: toolStripSeparator2
index 9 Name: mnuEditCut
index 10 Name: mnuCopy
index 11 Name: mnuPaste
index 12 Name: toolStripSeparator3
index 13 Name: mnuUndo
index 14 Name: mnuRedo
« Last Edit: October 20, 2016, 17:28:11 pm by onekk » Logged

Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5676



View Profile WWW
« Reply #66 on: December 09, 2016, 04:15:20 am »

PolylineTrim() & PolylineSubtract()


(C#)
public static Polyline[] PolylineSubtract(Polyline p1, Polyline p2, double tolerance);
public static Polyline[] PolylineTrim(Polyline p1, Polyline p2, double tolerance);


(VB)
Public Shared Function PolylineSubtract(ByVal p1 As Polyline, ByVal p2 As Polyline, ByVal tolerance As Double) As Polyline()
Public Shared Function PolylineTrim(ByVal p1 As Polyline, ByVal p2 As Polyline, ByVal tolerance As Double) As Polyline()


this 2 functions allow to cut a polyline with another and return external or internal resulting polylines. the advantage compared to the trim and trim3D (CamBam.CAD.ShapeList) is that only the given line is cut with the boundary shape. With the function in Shapelist, the objects contained in the boundary cut also the other objects contained in this boundary, and we get thousand of small polylines. (and it's slower of course)

the following VB script show the usage.
just run it, it will create the test polylines the creates the trimed polylines external and internal.

note:

the 2 functions return an array of polyline or nothing ; the "try/endtry" in the code catch the error if nothing is returned.

PolylineTrim() return an array that contain the trimed polylines contained IN the boundary shape (p2)

PolylineSubtract() return the trimed polylines that are OUT of the boundary shape.

In this case, because the polylines used for p1 and p2 are already in the drawing, I must use polyline.clone to add the polyline to the drawing because if a polyline is totally contained or totally out of the boudary shape the same source polyline is returned and if we add it to the drawing we get duplicate object ID ... and problems.

++
David

Code:
' New CamBam VBScript
' dh42 12/2016
' trim polyline
'
' Public Shared Function PolylineTrim(ByVal p1 As Polyline,
' ByVal p2 As Polyline,
' ByVal tolerance As Double) As Polyline()
'
'***************************************************************
' Public Shared Function PolylineSubtract(ByVal p1 As Polyline,
' ByVal p2 As Polyline,
' ByVal tolerance As Double) As Polyline()
'
'****************************************************************
' p1 is the polyline to trim in/out
' p2 is the closed polyline used as boundary
'

sub main

Dim pl1 as polyline = New Polyline()
Dim pl2 as polyline = New Polyline()

Dim p_array_in() as polyline
Dim p_array_out() as polyline

pl1.add (10, 10, 0)
pl1.add (50, 10, 0)
pl1.add (50, 30, 0)
pl1.add (10, 30, 0)

pl1.closed = true

pl2.add(10, 0, 0)
pl2.add(17, 8, 0)
pl2.add(40, 40, 0)
pl2.add(42, 5, 0)

pl2.closed = false

doc.add(pl1)
doc.add(pl2)

Try

' trim IN
p_array_in = polyline.PolylineTrim(pl2, pl1, 0.05)

app.log("array IN max index: " & Ubound(p_array_in))

for n as long = 0 to Ubound(p_array_in)
doc.add(p_array_in(n).clone)
next n

catch ex as exception

app.log("IN empty array - nothing to trim/substract")

end try

Try

' trim OUT
p_array_out = polyline.PolylineSubtract(pl2, pl1, 0.05)

app.log("array OUT max index: " & Ubound(p_array_out))

for n as long = 0 to Ubound(p_array_out)
doc.add(p_array_out(n).clone)
next n

catch ex2 as exception

app.log("OUT empty array - nothing to trim/substract")

end try

end sub


see also
http://www.cambam.co.uk/forum/index.php?topic=3878.msg45187#msg45187
« Last Edit: December 09, 2016, 04:32:28 am by dh42 » Logged
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 4078



View Profile
« Reply #67 on: January 09, 2017, 15:07:14 pm »

Writing and Reading Windows Registry values in Linux and Windows


This a small plugin to test reading and writing Registry values using the same code for Windows and Linux
Previously I had used the VB methods of reading and writing to the Registry;

using Microsoft.VisualBasic;

public static string Interaction.GetSetting(
   string AppName,
   string Section,
   string Key,
   string Default = ""
)

public static void Interaction.SaveSetting(
   string AppName,
   string Section,
   string Key,
   string Setting
)


But they don't work with Linux so I decided to write two replacement methods that made it easy to adapt my existing code, here they are;

using Microsoft.Win32;

public string GetRegSetting(
        string AppName,
        string Section,
        string Key,
        string Default
)

public bool SaveRegSetting(
        string AppName,
        string Section,
        string Key,
        string Setting
)


In GetRegSetting the Default parameter must be at the very least a zero length string, ""
If values are not found in the Registry then the Default value is returned.

The following code shows how to use them.
I've tested it with Windows and Linux and works okay with both.  Cheesy

Code:

using System;
using System.Windows.Forms;
using CamBam.CAD;
using CamBam.UI;
using Microsoft.Win32;

namespace MyNamespace
{
    public class Class1
    {
        public static void InitPlugin(CamBamUI ui)
        {
            ToolStripMenuItem item = new ToolStripMenuItem();
            item.Text = "REG Save";
            item.Click += new EventHandler(plugin_clicked);
            ui.Menus.mnuPlugins.DropDownItems.Add(item);

            ToolStripMenuItem item2 = new ToolStripMenuItem();
            item2.Text = "REG Get";
            item2.Click += new EventHandler(plugin_clicked2);
            ui.Menus.mnuPlugins.DropDownItems.Add(item2);
        }


        public static void plugin_clicked(object sender, EventArgs e)
        {
            Myplugin mp1 = new Myplugin();
            mp1.SetRegistryValues();
        }

        public static void plugin_clicked2(object sender, EventArgs e)
        {
            Myplugin mp1 = new Myplugin();
            mp1.GetRegistryValues();
        }


        public class Myplugin
        {
    
            public void SetRegistryValues()
            {
                SaveRegSetting("REGtest", "Settings", "diameter", "12");
                SaveRegSetting("REGtest", "Settings", "Tip", "Ball Nose");
                SaveRegSetting("REGtest", "Settings", "Speed", "20000");
            }


            public void GetRegistryValues()
            {
                string val1 = "";
                string val2 = "";
                string val3 = "";

                // NOTE: at the very least Default must be a zero length string, ""
                val1 = GetRegSetting("REGtest", "Settings", "diameter", "");
                val2 = GetRegSetting("REGtest", "Settings", "Tip", "V");
                val3 = GetRegSetting("REGtest", "Settings", "Speed", "10000");

                CamBam.ThisApplication.AddLogMessage("val1 = " + val1);
                CamBam.ThisApplication.AddLogMessage("val2 = " + val2);
                CamBam.ThisApplication.AddLogMessage("val3 = " + val3);
            }


            public string GetRegSetting(string AppName, string Section, string Key, string Default)
            {
                // attempt to open the subkey
                RegistryKey regKey = Registry.CurrentUser.OpenSubKey("Software\\" + AppName + "\\" + Section, false);

                // if it fails then return Default value
                if (regKey == null)
                {
                    return Default;
                }

                else
                {
                    try
                    {
                        object setting = regKey.GetValue(Key);
                        if (setting == null) return Default;
                        return setting.ToString();
                    }
                    catch (Exception e)
                    {
                        CamBam.ThisApplication.AddLogMessage("Registry Read Error " + e);
                        return null;
                    }
                }
            }


            public bool SaveRegSetting(string AppName, string Section, string Key, string Setting)
            {
                try
                {
                    // Use CreateSubKey (create or open it if already exits)
                    // located in, HKEY_CURRENT_USER\Software  
                    RegistryKey rootKey = Registry.CurrentUser.CreateSubKey("Software");
                    RegistryKey appKey = rootKey.CreateSubKey(AppName);
                    RegistryKey sectionKey = appKey.CreateSubKey(Section);

                    sectionKey.SetValue(Key, Setting);
                    return true;
                }
                catch (Exception e)
                {
                    CamBam.ThisApplication.AddLogMessage("Registry Write Error " + e);
                    return false;
                }
            }
        }
    }
}

  
« Last Edit: January 09, 2017, 15:14:28 pm by EddyCurrent » Logged

Made in England
onekk
Wookie
****
Offline Offline

Posts: 497


View Profile
« Reply #68 on: January 09, 2017, 16:19:15 pm »

Hello Eddy, did you find out where the settings are saved in Linux?

If your method could be used to retain some prefeences values in windows, I have to rethink all the persistence code of my plugins.

Regards.
Logged

Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 4078



View Profile
« Reply #69 on: January 09, 2017, 16:22:48 pm »

Carlo,

I thought you would like that  Wink
I'm looking to see where they are kept in Linux now.
Logged

Made in England
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 4078



View Profile
« Reply #70 on: January 09, 2017, 16:28:53 pm »

The Registry values in Linux are stored here;

~/.mono/registry/CurrentUser

If you look at the example code, I hard coded a Windows path to;

CurrentUser\Software\

that's because it's the default location for user settings (on my system anyway)

So on my Linux system the path it made was;

~/.mono/registry/CurrentUser/software/
« Last Edit: January 09, 2017, 16:32:37 pm by EddyCurrent » Logged

Made in England
onekk
Wookie
****
Offline Offline

Posts: 497


View Profile
« Reply #71 on: January 09, 2017, 17:25:31 pm »

Thanks,  the decision is hard:

in favour of my ini settings are:

1) 1 file for each plugin
2) the settings could be deleted or retained in case of reinstalling, or could be moved through  different Cambam installation

in favour of "windows registry" solution (your)

1) no files around (if the plugins are many many files have to be retained)

2) No external code has to be added.

but as i see the registry file is a normal text file so (resemble a sort of xml) so maybe an intermediate solution could be a viable solution, an option to export the relevant part of the setting in an external file and a method to reinstall in the registry.

I have some things to investigate.

How do you think?

Regards
Logged

Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam
jk
Wookie
****
Offline Offline

Posts: 262


View Profile
« Reply #72 on: January 09, 2017, 17:41:01 pm »

I thinks the best way is to follow the CamBam's own plugins practice (ExcellonIO, CutViewer etc).
They are saving xml configs at CB system path. This path would be correct for both windows and linux.

Code:
string path = FileUtils.GetFullPath(CamBamConfig.Defaults.SystemPath, "ExcellonIO.config");
Logged
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 4078



View Profile
« Reply #73 on: January 09, 2017, 17:41:11 pm »

Carlo,

In my Linux system;

1) there is 1 file for each plugin
2) the individual settings files can easliy be deleted
Logged

Made in England
onekk
Wookie
****
Offline Offline

Posts: 497


View Profile
« Reply #74 on: January 09, 2017, 18:12:24 pm »

I thinks the best way is to follow the CamBam's own plugins practice (ExcellonIO, CutViewer etc).
They are saving xml configs at CB system path. This path would be correct for both windows and linux.

Code:
string path = FileUtils.GetFullPath(CamBamConfig.Defaults.SystemPath, "ExcellonIO.config");

and  Eddy

Carlo,

In my Linux system;

1) there is 1 file for each plugin
2) the individual settings files can easliy be deleted

Many thanks, I will try this solution and see how it works (at a first glance seem the "perfect" solution).

Regards
Logged

Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam
Pages: 1 ... 3 4 [5] 6
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.153 seconds with 19 queries.

Copyright © 2018 HexRay Ltd. | Sitemap