Author Topic: Select objects by Z level  (Read 13266 times)

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 5263
  • Made in England
    • View Profile
Re: Help with Python needed
« Reply #15 on: August 09, 2024, 21:06:18 pm »
There are too many words for me to understand the requirements.
So, in as few a words as possible, what are you trying to do ?
« Last Edit: August 09, 2024, 21:07:52 pm by EddyCurrent »
Filmed in Supermarionation

Offline lloydsp

  • CNC Jedi
  • *****
  • Posts: 8971
    • View Profile
Re: Help with Python needed
« Reply #16 on: August 09, 2024, 21:24:33 pm »
What I 'got' from that is that his first priority is to be able to 'screen select' a drawing object through Python code.

I know a little Python, but not enough about CB internals to tell him how.  It seems like he knows enough about how to examine an object, once it's selected.

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

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7410
    • View Profile
    • Cambam V1.0 French Doc
Re: Help with Python needed
« Reply #17 on: August 09, 2024, 23:31:31 pm »
Hello

Quote
there is a thread on this subject created by myself but I've forgotten
https://cambamcnc.com/forum/index.php?topic=9255.msg72345#msg72345
Would you please merge this one with the old?

Done ;)

As Lloyd, I can't help for python syntax.

Quote
Anyway, I came to the conclusion that the best approach is to 'walk' through entities on-screen, selecting a single one on each iteration (make it selected on screen). But I don't know whether it is possible and how to do it due to lack of knowledge on CB internals. But if an item can be selected with a mouse click, then it should be possible in a script.

Yep ; what I often do is to "scan" the selection list for testing if the entity is suitable for what I want to do ; if it is suitable, I add it in a second list and when the whole selection list has been scanned then I use the second list for the job. (that also can be used to select the objects)

Code: [Select]
sub main
    dim ent as Entity
    dim worklist as ArrayList = new ArrayList()
   
        for each ent in view.SelectedEntities 'scan selection
       
        If ent.ApplyTransformation = true then 'if it can be transformed, add the entity to the working list
            worklist.add(ent)
        end if
         
    next ent
   
    'now the "worklist" list contain only the shapes that can be transformed
    'and you can select only the objects in the new list
   
    view.Select(worklist)
   
end sub

++
David
« Last Edit: August 09, 2024, 23:35:49 pm by dh42 »

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2652
    • View Profile
Re: Help with Python needed
« Reply #18 on: August 11, 2024, 08:09:42 am »
There are too many words for me to understand the requirements.
So, in as few a words as possible, what are you trying to do ?
I want all primitives lying in X/Y plane and at the same Z level to be selected and copied to a new layer. Then I can select all on that layer and apply 'Join' to obtain closed contours for milling.

@David
One thing I discovered while trying to make the script work:
The attached file is a simple part fresh from opening a .STEP file into CB. The only modification I did is create a new layer (3d), select all surfaces and join them into a single object on that layer. This leaves all wireframe primitives on a separate layer and with more complex parts reduces the total number of primitives.
- If you select the circle as on the picture the properties panel shows its center at 0,0,0 and an active T-Matrix. The actual Z is +3mm and X/Y coordinates are not zero. The same applies to other circles and corner arcs. Lines are OK.
- If I do 'Select all' -> 'Transform' -> 'Align' and do the alignment (I use Center,Center,Top) now those elements, upon selection, show 'Identity' and true coordinates. Which means that during the alignment CB also brings them to 'Identity' (applies the transformation). After that the script makes a correct selection.

I am attaching the .cb file without modifications.

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2652
    • View Profile
Re: Select objects by Z level
« Reply #19 on: August 11, 2024, 08:18:14 am »
@David
Because Python does not support arrays directly I used another method for testing.
When the program detects an object with T-Matrix it creates another clone of it and does the transformation test on the second. If the object is transformable the first one (unmodified) is added to the selection.
Code: [Select]
if mtrx != "Identity":
     ne1=ent.Clone()
     Valid = ne1.ApplyTransformation()