Author Topic: Need help implementing this in Python  (Read 5433 times)

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2666
    • View Profile
Re: Need help implementing this in Python
« on: January 29, 2025, 11:37:18 am »
I started this thread some time back but the forum did not allow to post.

Can someone help me how to do the same in Python?  Python does not support arrays like in VB. It has lists. I successfully iterate through a selection and fill list.
But after that I could not find a way to make this list back to a CB Selection.
The VB code is from snippets published by David and it works. But I'd like to use Python because it works in CB for Linux.
The lines in red are what I need to do in Python.

   view.SelectAllVisibleGeometry()
    dim newselection as ArrayList = new ArrayList()
    for each ent in view.SelectedEntities
         ....................
    next
   
       
    view.Select(newselection.ToArray(GetType(Entity)))   
    view.RefreshView()

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1854
    • View Profile
Re: Need help implementing this in Python
« Reply #1 on: January 29, 2025, 12:48:24 pm »
Like C#  for CB Python has shapelists , here are some examples I've kept for reference.
If I find anything else I'll post it.
Dave

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2666
    • View Profile
Re: Need help implementing this in Python
« Reply #2 on: February 01, 2025, 16:57:45 pm »
Thank you Dave! There are definitely things in your code that I can use.
However, I saw what I need in another script - File Merge script. And my script now does what it is intended to do.
It was due to my very basic knowledge of Python data types and methods for list initialization.

I needed this important directive at the start:
Code: [Select]
from System.Collections.Generic import List
Code: [Select]
# here I define the list as follows
sel = List[Entity]()
   
CamBamUI.MainUI.ActiveView.SelectAllVisibleGeometry()
if (view.Selection.Count > 0):
    for ent in view.Selection:

        # iterate through selection and analyze

        if (IsMatch()== true):
            sel.Add(ent)

    CamBamUI.MainUI.ActiveView.ClearSelections()
    app.log(sel.Count.ToString())

    CamBamUI.MainUI.ActiveView.Select(sel.ToArray())  #now the list supports ToArray() metod.

    view.RefreshView()

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1854
    • View Profile
Re: Need help implementing this in Python
« Reply #3 on: February 02, 2025, 00:56:24 am »
Glad you got it sorted, If you intend to do more than a few python scripts, I had a look to see if
VS Studio or VS Code would run on linux and it can, why I mention this, is that they provide
tools so that you can see  CB's “Python functions” (does not explain them though).
Its difficult to start scripting\plugins as there's not a lot of documentation or examples
to learn from.

Dave

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2666
    • View Profile
Re: Need help implementing this in Python
« Reply #4 on: May 28, 2025, 18:48:25 pm »
Glad you got it sorted, If you intend to do more than a few python scripts, I had a look to see if
VS Studio or VS Code would run on linux and it can, why I mention this, is that they provide
tools so that you can see  CB's “Python functions” (does not explain them though).
Its difficult to start scripting\plugins as there's not a lot of documentation or examples
to learn from.

Dave
Any hint or clue is valuable.
For example I now need to invoke Edit -> Join from the script so that selected shapes are joined. But I don't know how.
So currently after the script does the selection I do it manually with the mouse from the menu bar. Ctrl-J does not work as the main window has lost the focus.

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5296
  • Made in England
    • View Profile
Re: Need help implementing this in Python
« Reply #5 on: May 28, 2025, 19:01:28 pm »
Try this, it was in a script called mop-automate.py

# Select All...
view.SelectAllVisibleGeometry()

# Convert to polylines...
PolylineUtils.PolyLinesConvert(view)
# The gui operation runs in a worker process, so wait for the
# thinking message to disapear...
while view.CurrentEditMode is not None:
   app.Sleep(1)

# Edit - Join
PolylineUtils.JoinPolyLines(view,0.001)
# wait for the thinking message to disapear...
while view.CurrentEditMode is not None:
   app.Sleep(1)


Also found this in a C# script, InvoluteGearGenerator.cs


Polyline[] array6 = new Polyline[num_teeth];
// put some polylines into the array

// now define a new Polyline array to hold those joined
Polyline[] array7 = Polyline.Join(array6, 1E-05);
« Last Edit: May 28, 2025, 19:11:36 pm by EddyCurrent »
Filmed in Supermarionation

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2666
    • View Profile
Re: Need help implementing this in Python
« Reply #6 on: May 29, 2025, 07:32:55 am »
Thank you so much, Eddy! Very valuable piece of code.
Yet, mop-automate.py comes with CB package but I somehow missed to study it.