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

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2652
    • 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: 1829
    • 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: 2652
    • 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: 1829
    • 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