Author Topic: [VBS - C# - VB .net] parts of code (snippet)  (Read 74103 times)

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8331
    • View Profile
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #45 on: May 04, 2015, 21:39:56 pm »
Ooooo!  That's heady stuff, Vasco!  Thank you!

And thanks, David, for keeping up with all the documentation.

One recommendation, David, if you haven't already thought of it.  Don't rely on those links.  Get the text of what they point to, and build your own pages in the site to contain them.  Links might expire, eventually.

Lloyd
"Pyro for Fun and Profit for More Than Fifty Years"

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #46 on: May 07, 2015, 23:13:02 pm »
Hello Lloyd,

Sorry, I forgot to reply  :-[

Yes, I also add a snippet page on the API, ... when time permit ..

++
David

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #47 on: May 07, 2015, 23:45:48 pm »
Create a specific Translation Key

When we create translation with the TextTranslation.Translate(string) Method, we can have a little problem that is that the same word can be used for 2 different things in English, but in another language, we need 2 different words.  In this case, the word appears only one time in the translation list and of course, we get the same translated word in other language .. I'm sure that Ralf and other translators have already encounter this problem.

Ex: If I use something like TextTranslation.Translate("already exists"), in the translation table, i get:



I've found another way to create the translation item where the key is not the word to translate but a different word.



The Method used is:

static TextTranslationItem SetCacheItem(LanguageCode lang, string key, string english, string translation);


The code in VBS

Code: [Select]
sub main

Dim LgCode as LanguageCode

'read the langage code used in the current config
LgCode = CamBamConfig.Defaults.Language

' create an entry with the KEY: AAA_EXIST, the English text: already exist
' and let the translated entry empty

CamBam.Util.TextTranslation.SetCacheItem(LgCode ,"AAA_EXIST","already exist","")

end sub

For plugin, maybe it can be a good thing to create all translation with key where the key name start with the plugin name.

++
David
« Last Edit: May 07, 2015, 23:50:09 pm by dh42 »

Offline BaNoBi

  • Droid
  • **
  • Posts: 51
    • View Profile
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #48 on: May 08, 2015, 10:44:11 am »
Good ideia.

In the plugin "Move multiple points in a polylines" I'll use this code:
Code: [Select]
       public static string Translate(string text)
        {
            string translateKey = string.Format("PointsMove:{0}", text);
            TextTranslationItem translate = TextTranslation.GetCacheItem(CamBamConfig.Defaults.Language, translateKey);
            string translateText = text;

            if (translate != null)
            {
                if (!string.IsNullOrEmpty(translate.Translation))
                {
                    translateText = translate.Translation;
                }
            }
            else
            {
                TextTranslation.SetCacheItem(CamBamConfig.Defaults.Language, translateKey, text, "");
            }

            return translateText;
        }

With this code I check if the key already exists, if not exists add a new key, if exists get the translation.

Best regards,
Vasco Cruz
« Last Edit: May 08, 2015, 10:49:52 am by BaNoBi »

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #49 on: May 08, 2015, 13:38:55 pm »
Hello,

I'm not sure that it's nedded to test for existing key ; it is not added twice even if it already exists if I run my script more than one time ; Andy's code certainly handle this case internally.

Now, we need to understand how to use it as easily than the other method .. but yesterday it was too late to go further ...

++
David

Offline BaNoBi

  • Droid
  • **
  • Posts: 51
    • View Profile
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #50 on: May 08, 2015, 15:13:59 pm »
Hello,

I use this code for add the key (PluginName:TextKey) and get the translation.

In the rest of code I just need to call this method with TextKey.

Best regards,
Vasco Cruz

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #51 on: February 06, 2016, 22:50:01 pm »
Get info about CamBam and about a plugin.

Hello,

the following codes (Vb script/VB.net) allow to obtain info about CamBam version.

Code: [Select]
' New CamBam VBScript

sub main

'display some info about CamBam
CamBam.ThisApplication.AddLogMessage("CompanyName: " & My.Application.Info.CompanyName)
CamBam.ThisApplication.AddLogMessage("Copyright: " & My.Application.Info.Copyright)
    CamBam.ThisApplication.AddLogMessage("Description: " & My.Application.Info.Description)
    CamBam.ThisApplication.AddLogMessage("DirectoryPath: " & My.Application.Info.DirectoryPath)
    CamBam.ThisApplication.AddLogMessage("ProductName: " & My.Application.Info.ProductName)
    CamBam.ThisApplication.AddLogMessage("assembly name: " & My.Application.Info.AssemblyName)
    CamBam.ThisApplication.AddLogMessage("Major: " & My.Application.Info.Version.Major)
    CamBam.ThisApplication.AddLogMessage("Minor: " & My.Application.Info.Version.Minor)
    CamBam.ThisApplication.AddLogMessage("Build: " & My.Application.Info.Version.Build)
    CamBam.ThisApplication.AddLogMessage("Revision: " & My.Application.Info.Version.Revision)

end sub

the following codes (Vb script/VB.net) allow to obtain info about a given plugin. (here with the NumMove plugin)

Code: [Select]
' New CamBam VBScript

sub main

'get info about a plugin (ex with the NumMove plugin)

    Dim fvi As System.Diagnostics.FileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(My.Application.Info.DirectoryPath + "\plugins\NumMove.dll")

    CamBam.ThisApplication.AddLogMessage(fvi.FileName)
    CamBam.ThisApplication.AddLogMessage(fvi.CompanyName)
    CamBam.ThisApplication.AddLogMessage(fvi.LegalCopyright)
    CamBam.ThisApplication.AddLogMessage(fvi.Comments)
    CamBam.ThisApplication.AddLogMessage(fvi.ProductName)

    CamBam.ThisApplication.AddLogMessage(fvi.ProductVersion)

end sub

C# version
http://www.cambam.co.uk/forum/index.php?topic=3878.msg48681#msg48681

++
David
« Last Edit: October 19, 2016, 21:35:26 pm by dh42 »

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8331
    • View Profile
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #52 on: February 06, 2016, 23:17:20 pm »
OOoo!  Thanks, David.

That now makes it possible to make "version adaptable" plugins!

Lloyd
"Pyro for Fun and Profit for More Than Fifty Years"

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #53 on: February 07, 2016, 00:24:06 am »
Yes,

In the last version of BoxAuto plugin, I use it to display automatically the plugin version/creator name in the form.

++
David




Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4468
    • View Profile
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #54 on: February 07, 2016, 08:55:01 am »
Good idea  ;) thanks for the code.
Made in England

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #55 on: April 24, 2016, 16:57:04 pm »
Trim Drawing Entities (3D)  VB script example

CamBam.CAD.ShapeList.Trim3D(ByVal trimmers As ShapeList, ByVal inout As Char) As ShapeList

++
David

Code: [Select]
' CamBam VBScript dh42 04/2016
' polyline 3D trim test

sub main

dim ptool as polyline = new polyline() ' a closed polyline as tool

dim p1 as polyline = new polyline() ' the polylines to trim
dim p2 as polyline = new polyline()
dim p3 as polyline = new polyline()

'******** erase doc

View.SelectAllVisibleGeometry()
CamBamUI.MainUI.DeleteEntities(true)

'********** create the polyline for the test ********

'**** draw a CLOSED polyline as tool ********

ptool.Add(18,22,0,0)
ptool.Add(23,14,0,0)
ptool.Add(23,-5,0,0)
ptool.Add(24,-22,0,0)
ptool.Add(-9,3,0,0)
ptool.Add(-12,16,0,0)
ptool.Add(-4,21,0,0)
ptool.Add(-5,30,0,0)

ptool.closed = true

doc.Add(ptool)

'**** draw the 3 polylines to trim

p1.Add(-18,18,-5,0)
p1.Add(1,6,-2,0)
p1.Add(12.5,-2.57,-3,0)
p1.Add(32.25,-21,-4,0)

p2.Add(-14,24,-2,0)
p2.Add(5,12,-3,0)
p2.Add(16,4,-3,0)
p2.Add(41,-21,-4,0)

p3.Add(-8,29,-1,0)
p3.Add(7,21,-3,0)
p3.Add(19,12,-2,0)
p3.Add(47,-16,0,0)

doc.Add(p1)
doc.Add(p2)
doc.Add(p3)

'*** 2 shapelist that will contain the lists of polylines to trim and the list of polylines used to trim
'*** and another shapelist that will contain the resulting polylines

Dim sl_trim as ShapeList = New shapelist
Dim sl_trimers as ShapeList = New shapelist

Dim sl_dest as ShapeList = New shapelist

'*** 2 lists of polyline

Dim polylist_totrim As System.Collections.Generic.List(Of Polyline) = New System.Collections.Generic.List(Of Polyline)
Dim polylist_trimers As System.Collections.Generic.List(Of Polyline) = New System.Collections.Generic.List(Of Polyline)

'******* add the polylines to the list of polylines to trim

polylist_totrim.Add(p1)
polylist_totrim.Add(p2)
polylist_totrim.Add(p3)

'******* add the polylines to the list of polylines used to trim (tool)

polylist_trimers.Add(ptool)

'** add the polyline lists to the ShapeList

sl_trim.AddEntities(polylist_totrim)
sl_trimers.AddEntities(polylist_trimers)

'******* trim the polylines, the result falls in sl_dest)

sl_dest = sl_trim.Trim3D(sl_trimers ,"o") 'trim external, warning the char in the function is case sensitive

View.RefreshView()

'***** remove all the test polylines

View.SelectAllVisibleGeometry()
CamBamUI.MainUI.DeleteEntities(true)

'**** add the resulting polylines to the doc

For Each item As ShapeListItem In sl_dest
     Doc.Add(item.Shape)
    Next

View.RefreshView()

end sub

see also
http://www.cambam.co.uk/forum/index.php?topic=3878.msg49782#msg49782
« Last Edit: December 09, 2016, 04:19:24 am by dh42 »

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4468
    • View Profile
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #56 on: April 25, 2016, 15:36:28 pm »
David,

Good example of using 'Shapelist' and 'System.Collections.Generic.List'

I don't think I've used those before.
Made in England

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8331
    • View Profile
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #57 on: April 25, 2016, 17:38:21 pm »
Me either!

David, thanks.  I just LOVE "code snippets"!

Lloyd
"Pyro for Fun and Profit for More Than Fifty Years"

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #58 on: April 25, 2016, 22:20:39 pm »
Hello,

Another that give me some headache today !!  ;D

create REGION from polylines

This script creates two circles and two concentric rectangles (as polylines) then creates regions from these 4 polylines.

The "Region.Create From Polylines ()" function needs an array of Polylines as an argument and returns an array of Regions.
The array of polylines passed to the function must not contain empty elements, otherwise it returns an error.

Code: [Select]
' New CamBam VBScript
' region creation

sub main

'****** create 4 polylines ************************

dim p1 as polyline = new polyline() '2 concentric circles
dim p2 as polyline = new polyline()

dim p3 as polyline = new polyline() '2 rectangles
dim p4 as polyline = new polyline()

p1.Add(15,0,0,0.577350269189625)
p1.Add(-7.5,12.99,0,0.577350269189625)
p1.Add(-7.5,-12.99,0,0.577350269189625)
p1.closed = true

p2.Add(30,0,0,0.577350269189625)
p2.Add(-15,25.98,0,0.577350269189625)
p2.Add(-15,-25.98,0,0.577350269189625)
p2.closed = true

p3.Add(26,52,0,0)
p3.Add(46,52,0,0)
p3.Add(46,61,0,0)
p3.Add(26,61,0,0)
p3.closed = true

p4.Add(10,45,0,0)
p4.Add(61,45,0,0)
p4.Add(61,71,0,0)
p4.Add(10,71,0,0)
p4.closed = true

doc.Add(p1)
doc.Add(p2)
doc.Add(p3)
doc.Add(p4)

View.RefreshView()

'*********************************

dim p_array(3) as polyline ' an array of polylines (it must not have empty elements)

p_array(0) = p1 'fit the array with the polylines
p_array(1) = p2
p_array(2) = p3
p_array(3) = p4

'**** create an array of region entities
'**** "CamBam.CAD.Region" must be used instead to only "Region" to avoid ambiguity with "System.Drawing.Region"

dim rg() as CamBam.CAD.Region 'an array of regions (more than one can be returned)

rg = CamBam.CAD.region.CreateFromPolylines(p_array) 'create the regions from the polylines

        for n as long = 0 to UBound(rg) 'add the created regions to the document (more than one region can be created)
doc.Add(rg(n))
next n

View.RefreshView()

end sub
« Last Edit: April 26, 2016, 15:07:53 pm by dh42 »

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VBS - C# - VB .net] parts of code (snippet)
« Reply #59 on: April 30, 2016, 16:26:43 pm »
Write polylines datas in a text file. (VB)

This example write the polyline XYZ points and bulge of a selection of polylines in the log window and in a text file (C:\polyligne.txt)

++
David

Code: [Select]
'polyline data to .txt
' dh42 04/2016

    Sub main()

        ThisApplication.ClearLogMessages()

        Dim fname As String

        fname = "C:\polyligne.txt"

        Dim stream As System.IO.StreamWriter = New System.IO.StreamWriter(fname)

        Dim ent As Entity

        For Each ent In CamBamUI.MainUI.ActiveView.SelectedEntities

            If TypeOf ent Is Polyline Then

                'call the sub to scan the polyline
                WritePolyData(CType(ent, Polyline), stream)
                ThisApplication.AddLogMessage(vbNewLine)
                stream.WriteLine(vbNewLine)

            End If

        Next ent

        stream.Close()

    End Sub

    Sub WritePolyData(p As Polyline, s As System.IO.StreamWriter)

        Dim polypoint As Point3F    'XYZ coordinate of a polyline point
        Dim blg As Double   'bulge

        'scan the polyline for each item in the PolylineItemList
        For Each it As PolylineItem In p.Points

            polypoint = it.Point
            blg = it.Bulge

            ThisApplication.AddLogMessage(polypoint.X & " " & polypoint.Y & " " & polypoint.Z & " " & blg)

            s.WriteLine(polypoint.X & " " & polypoint.Y & " " & polypoint.Z & " " & blg)

        Next

    End Sub
« Last Edit: April 30, 2016, 16:35:40 pm by dh42 »