Author Topic: Automatic Centerline  (Read 19020 times)

Offline GaetanG70

  • Ewok
  • *
  • Posts: 2
    • View Profile
Automatic Centerline
« on: February 19, 2019, 13:16:51 pm »
Hi everyone !

So what i'm looking for, is a way to get easily the "centerline" of a letter.
The goal is to do some drilling on this line to put some led !

I join a picture of what i managed to do (left) and what i'm looking for (right (it's just an hand drawing))

Thank's ahead for your help,

Gaƫtan

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5262
  • Made in England
    • View Profile
Re: Automatic Centerline
« Reply #1 on: February 19, 2019, 15:17:02 pm »
I do not think an easy way exists at the moment.

Members have posted scripts for a centre line but they are for engraving and will not give the required result but they may be okay as a starting point.
Here's one; http://www.cambam.co.uk/forum/index.php?topic=7501.msg60310#msg60310
and another; http://www.cambam.co.uk/forum/index.php?topic=6221.msg49388#msg49388

Unless you require a particular font, these stick fonts could be used; http://www.cambam.co.uk/forum/index.php?topic=1557.msg8614#msg8614

Or, if you look at the attached image;

1. I made some text in CamBam (letter S)
2. Use, Edit->Explode a few times until it results in lines and arcs
3. select lines and arcs that form one 'side' of the letter and Edit->Join (the purple line)
4. use, Edit->Offset to create a rough centre line
5. double click the new centre line and move the highlighted points to give a better line.(the red line)
6. If you need more points in the line use this plugin;  http://www.atelier-des-fougeres.fr/Cambam/Aide/Plugins/polyaddpoints.html

To position the drill holes with equal spacing use one of these;
Draw->Pointlist->Divide Geometry
Draw->Pointlist->Step Around Geometry
« Last Edit: February 19, 2019, 15:22:14 pm by EddyCurrent »
Filmed in Supermarionation

Offline imr700x

  • Droid
  • **
  • Posts: 99
    • View Profile
Re: Automatic Centerline
« Reply #2 on: February 20, 2019, 22:42:06 pm »
I have done something similar. I used the Vengrave plugin to carve the letter on the back of my board using a 15 degree engraving bit but telling CB that it was a 90 degree bit then drilled the holes for the LEDS  on the centreline created from the v bit. When the board it too thin then I cut the letters on MDF to create a template and drill the holes with a hand drill through both of them.

Offline GaetanG70

  • Ewok
  • *
  • Posts: 2
    • View Profile
Re: Automatic Centerline
« Reply #3 on: March 04, 2019, 10:31:29 am »
Hi,

Thank's both for your answer ! We can't use sticky font, 'cause we use specific typo everytime.
I've try your method ( @EddyCurrent ) but it's not easy with letter like A, O,...


Maybe there is an other software that can handle this centerline things more easly ?


Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5262
  • Made in England
    • View Profile
Re: Automatic Centerline
« Reply #4 on: March 04, 2019, 14:37:42 pm »
With letters like A,O you could use the outer profile then scale it but I think the best starting point is one of those scripts I linked to in my previous post.
« Last Edit: March 04, 2019, 14:44:59 pm by EddyCurrent »
Filmed in Supermarionation

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1826
    • View Profile
Re: Automatic Centerline
« Reply #5 on: March 06, 2019, 03:19:21 am »
I've installed a new version of VS Studio, and thought i would test it out by converting the script
to a plugin.
I've got to this point in the picture.
I've had no luck with this bit though
 
Code: [Select]
#call the polyline smooth .dll or make a new module 
# something like this
#skeleton = skeleton.Smooth(.1)
I can't seen to import this lib libcambam.dll does anyone have a clue how to do this.
Dave

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5262
  • Made in England
    • View Profile
Re: Automatic Centerline
« Reply #6 on: March 06, 2019, 12:28:56 pm »
Dave,

I think the dll you refer to is for Linux.

This works, it's a plugin version of Andy's script from here; http://www.cambam.co.uk/forum/index.php?topic=7501.msg60310#msg60310

Code: [Select]
using System;
using CamBam.CAD;
using CamBam.UI;
using System.Windows.Forms;

namespace skeleton
{
    public class Class1
    {
        public static ICADView view = CamBamUI.MainUI.ActiveView;
        public static CADFile myfile = view.CADFile;

        public static void InitPlugin(CamBamUI ui)
        {
            ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
            toolStripMenuItem.Text = "Skeleton";
            toolStripMenuItem.Click += new EventHandler(plugin_clicked);
            ui.Menus.mnuPlugins.DropDownItems.Add(toolStripMenuItem);
        }

        public static void plugin_clicked(object sender, EventArgs e)
        {
            ShapeList shapes = new ShapeList();
            ShapeList skeleton = new ShapeList();

            shapes.ApplyTransformations = true;
            shapes.AddEntities(myfile.ActiveLayer.Entities);
            skeleton = CAD3DUtils.GetShapeListSkeleton(shapes, 0.1);
            skeleton = skeleton.Join(0.01);
            skeleton = skeleton.ArcFit(0.1);
            myfile.Add(skeleton.ToEntities());
            view.RefreshView();
        }
    }
}

Plugin attached,

1. works on CamBam v1.0 only
2. appears in the 'Plugins' menu as "Skeleton"
3. when the plugin runs it acts upon all entities in the currently active layer, no need to select anything first.

« Last Edit: March 06, 2019, 14:19:37 pm by EddyCurrent »
Filmed in Supermarionation

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1826
    • View Profile
Re: Automatic Centerline
« Reply #7 on: March 06, 2019, 23:02:53 pm »
Hi Eddy thanks for the reply

Yep that code you posted is pretty much the same as I have now, But I think to make it
better (regarding the other lines rather than the centre line) which you would want to remove
as well as smoothing up the centre line it'self, and in the comments Andy wrote this.

#call the polyline smooth .dll or make a new module 
# something like this
#skeleton = skeleton.Smooth(.1)

I tried import the libcambam.dll thinking the smooth function might be in there, but can't do so.
I tried to find it (the smooth function) with ilspy and came up empty handed and now am thinking it might be
in the system 2d drawing area.
I'll get a few hours later today to have a bit more of a dig around, you see Andy wrote "polyline smooth"
whereas the skeleton would be a shapelist, but there are no shapelist smooth functions?

Anyway thanks for the input, and if you have an Epiphany let me know as I may have been barking up the wrong tree.(wouldn't be the first time). ::)

Dave

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5262
  • Made in England
    • View Profile
Re: Automatic Centerline
« Reply #8 on: March 07, 2019, 09:30:28 am »
, and in the comments Andy wrote this.

#call the polyline smooth .dll or make a new module  
# something like this
#skeleton = skeleton.Smooth(.1)

Dave

Dave,

Do you have link to where Andy said that ? I can't seem to find it.
Filmed in Supermarionation

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1826
    • View Profile
Re: Automatic Centerline
« Reply #9 on: March 07, 2019, 10:50:02 am »
HI Eddy
No link necessary, it's in the script.

Code: [Select]
# New CamBam Python Script
app.log("Hello")
source_layer = 'Layer1'
shapes = ShapeList()
shapes.ApplyTransformations = True
shapes.AddEntities(doc.Layers[source_layer].Entities)
regions = shapes.DetectRegions()
skeleton = CAD3DUtils.GetShapeListSkeleton(regions,0.1)
skeleton = skeleton.Join(0.01)
skeleton = skeleton.ArcFit(0.1)
#call the polyline smooth .dll or make a new module  
# something like this
#skeleton = skeleton.Smooth(.1)
doc.Add(skeleton.ToEntities())

And with the .dll they are usually windows shared libs, linux ones are usually .so
I ran an dissembler over the .dll and couldn't see anything useful either. (could have missed something though)
edit opps here is the dissembler file
Dave
« Last Edit: March 07, 2019, 10:59:55 am by dave benson »

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7408
    • View Profile
    • Cambam V1.0 French Doc
Re: Automatic Centerline
« Reply #10 on: March 07, 2019, 19:38:34 pm »
Hello

I can't find the .smooth method nor the polyline_smooth.dll too.  ???

Maybe this syntax is only for Python ?

I sent a message to Andy for more explanations ;)

++
David

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1826
    • View Profile
Re: Automatic Centerline
« Reply #11 on: March 07, 2019, 21:34:18 pm »
Thanks David.

I'm using .net 4.5, and am wondering if I should use a higher version and will try some more things
later on when I get a quiet moment.
I tried to install one of the best Dissemblers going round (IDA) but it's for 64 bit OS's, so I'll have to do
with what I've got.
Anyway the answer is probably simple once you know.

Dave

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1826
    • View Profile
Re: Automatic Centerline
« Reply #12 on: March 09, 2019, 01:29:03 am »
I had a bit of a look around at various methods employed for 2d polyline smoothing
and found a couple (catmull/rom and  Chaikin), and over at CodeProject  there's a project
that has both and a demonstration of each, however I have fiddled around a bit omitting some code and adding
a very small amount to get the results in the pic.
Although I may code the others, at this stage of testing, I think it's pretty good.

Dave

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5262
  • Made in England
    • View Profile
Re: Automatic Centerline
« Reply #13 on: March 10, 2019, 14:55:51 pm »
Updated plugin attached, Works on CamBam v1.0 only.

v1.0

Appears in the Plugin menu as "Skeleton"

1. Added a form to adjust parameters
2. added an option to remove lines shorter than specified. This helps clean up the centre line if required.
3. added translation
4. added undo point

Dave,

Source project added for VS 2017, in case you want to add some smoothing code  :D

I found the region related code was not required because, CAD3DUtils.GetShapeListSkeleton, calls it anyway if a region is found.
« Last Edit: March 10, 2019, 15:05:20 pm by EddyCurrent »
Filmed in Supermarionation

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1826
    • View Profile
Re: Automatic Centerline
« Reply #14 on: March 10, 2019, 23:32:20 pm »
Hi Eddy

After writing this:
Quote
Although I may code the others, at this stage of testing, I think it's pretty good.
It seems I spoke too soon as when I loaded up more fonts to check (I have a huge amount of fonts from my textile machine patterning program) and tried at least 20 or more, I found the methods (the origional code from the script) varied significantly in it's output depending on the font selected.

I then found some more methods in CAD3DUtils:
eg.
CAD3DUtils.GetShapeListSkeleton(MyShapeList, 0.1);
CAD3DUtils.GetPolylineSkeleton(my_polyline, 0.1);
CAD3DUtils.GetRegionSkeleton(My_Region, 0.1);

And if I left out the AcrFit things got a lot better.

           ICADView view = CamBamUI.MainUI.ActiveView;
            CADFile file = view.CADFile;

            MyShapeList.Clear();
            MyShapeList.AddSelectedEntities(view);
            MyShapeList.CloneEntities = true;
            MyShapeList.ApplyTransformations = true;;
            string S = MyShapeList.Count.ToString();
            label2.Text = "Shapes in MyShapeList_Fill success Count  " + S;
            MyShapeList.DetectRegions();
            MyShapeList = CAD3DUtils.GetShapeListSkeleton(MyShapeList, 0.1);
            MyShapeList = MyShapeList.Join(0.01);
            //MyShapeList = MyShapeList.ArcFit(0.1);

            //call the polyline smooth .dll or make a new module 
            //something like this
            foreach (ShapeListItem sli in MyShapeList)
            {
                if (sli.Shape is Polyline)
                {
                    my_polyline.CloneFrom(sli.Shape);
                    CAD3DUtils.GetPolylineSkeleton(my_polyline, 0.1);
                    MyShapeList2.AddEntity(my_polyline);
                }
                else {
                    if (sli.Shape is CamBam.CAD.Region)
                    {
                        My_Region.CloneFrom(sli.Shape);
                        CAD3DUtils.GetRegionSkeleton(My_Region, 0.1);
                        MyShapeList2.AddEntity(My_Region);
                    }

                }



            }

The  Chaikin is a more geometric approach and is fast and the  catmull/rom is more mathematical, these seem to be the standard fare for doing this
but there are other ways for example you could construct lines perpendicular to the centre line and use least squares regression or root sum squares regression(statistical) methods to pull the outlying points towards the centre line.
the trouble is that the new line may be (although smooth) not very representative of the original intent of the text.

I also noticed that when loading up the solution that you have not set CB as the startup solution, what I do is set CB as the Startup program
and VS studio automatically puts the .dll in the CB plugins directory, which means that I compile the code I'm working on and press start on the debugger and CB is opened in Debug mode. It's not a big thing in the scheme of things but I find it handy in all of my other projects as well as  CB plugins.
I only really did this to set up VS Studio 2017, which I like a lot (It's a bit more picky about naming conventions) and a few other things and before an update managed to 'Do a Dynamo' and make it completely Disappear leaving me looking like a stunned mullet for a while.

For  some time now I've become interested in AI and ML for my turrets (Auto touch off, tool wear compensation) among other things so plugins have been put to the sidelines for now, If you are keen to do more with this plugin, I'll point you to the sites I looked at UCBerkley and Code Project
as I have too many projects and too little time right now.

Dave