objects in macro

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
lagunax
Posts: 55
Joined: Wed May 25, 2022 5:12 pm

objects in macro

Post by lagunax »

hi, next one question. working with macro and i need to know where is object is not under any 'parent' object.

For example i have Body1 from Sketch1 in top of list in Model under file name, and i have body2 from sketch2 placed in Group2.
i can get Group2 by getiing body2.InList and searching for temp in body2.inList: if (temp.TypeId == 'App::DocumentObjectGroup') | (temp.TypeId == 'App::DocumentObjectGroupPython'):
but how can i get to know that object have no any above but only file_name? For example for temp in Body1.inList: will return some object (in my case i have linked one and getting it with this)

PS in short: i need to go from selected object to top of tree in Model
edwilliams16
Veteran
Posts: 3111
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: objects in macro

Post by edwilliams16 »

You are much more likely to get help if you posted an example file and referenced the actual object names in it while re-phrasing your question.
lagunax
Posts: 55
Joined: Wed May 25, 2022 5:12 pm

Re: objects in macro

Post by lagunax »

im working on this macro and need to create Group under nearest one (where object selected or in group if group selected)

in attached file i can create any folders if group exists, but i have object "wtf" and link "wtf" - when i running script i can't know that "wtf" is top object, but i getting 'wtf001" as object above (wtf.inList returns wtf001 object)

Code: Select all

import FreeCAD as App
import math


# ==================================================================
#
# Functions for getting Group

def get_parent_object(object):
  list = object.InList
# #below replace
  found = False
  print("test---edit this to prevent going in infinity loop. need to go to top level only")
  for tmp in list:
    print(tmp.Module)
    print(tmp.Label)
    print(tmp.TypeId)
    print(tmp.Parents)
    #length = len(tmp)
    #rightside = tmp[(length - 7):(length -1)]
    #print(rightside)
  return False
# #above replace as below
#  if len(list) == 1:
#    return list
#  return False

def get_parent_group(object):
  list = object.InList
  print(list)
  for tmp_obj in list:
    if (tmp_obj.TypeId == 'App::DocumentObjectGroup') | (tmp_obj.TypeId == 'App::DocumentObjectGroupPython'):
      print(tmp_obj)
      return tmp_obj
  return False

def find_parent_group(object):
  result = get_parent_group(object)
  if result == False:
    result = get_parent_object(object)
    if result ==False:
      return False
    result = find_parent_group(object)
  return result
#
# ==================================================================

# ==================================================================
#
# Function creates main Group for roof

def create_group(object, name = ""):
  label = "_Ext"
  numeric=0
  groups = object.Group
  numgroups = len(groups)
  for numobjs in range(0, numgroups):
    tmpobj = object.Group[numobjs]
    if (tmpobj.TypeId == 'App::DocumentObjectGroup') | (tmpobj.TypeId == 'App::DocumentObjectGroupPython'):
      leftside=tmpobj.Label[0:4]
      if leftside == label:
        length = len(tmpobj.Label)
        rightside = tmpobj.Label[4:length]
        if rightside == '':
          rightside = '0'
          int(rightside)
        numeric = max(numeric, int(rightside))  
  numeric = numeric + 1
  label=label+str(numeric)
  print("-")
  # Now 'label' = first free name started with _Ext and numeric suffix
  # Creating Group with new name 'label'
  #selFolder.Label
  # selFolder.Group - contains all objects in group
  # Creating new Group
  doc = App.ActiveDocument
  newGroup = doc.addObject("App::DocumentObjectGroupPython", label)
  object.addObject(newGroup)
  print(newGroup)
  return newGroup
#
# ==================================================================



# Prepare workspace
doc = App.activeDocument()

GroupFound = False

# check selection
selected = Gui.Selection.getSelection()
selectedCount = len(selected)
if selectedCount > 0:
  object = selected[0]
  if (object.TypeId != 'App::DocumentObjectGroup') & (object.TypeId != 'App::DocumentObjectGroupPython'):
    # First selected is other object type
    result = find_parent_group(object)
    if result == False:
      print ("False")
    else:
      print(result.Label)
      GroupFound = True
      object = result
  else: 
    GroupFound = True

if GroupFound == True:
  print(object.Label)
  ng=create_group(object)
  print(ng.Label)
else:
  print("group not found")

Attachments
test.FCStd
(41.11 KiB) Downloaded 8 times
lagunax
Posts: 55
Joined: Wed May 25, 2022 5:12 pm

Re: objects in macro

Post by lagunax »

tested a little bit and i think this is like a bug

when link to object created then object added as subobject of link (totaly it is so becouse linked object made from original one). but how in this case i can determine that original object placed in top of tree?
chrisb
Veteran
Posts: 53933
Joined: Tue Mar 17, 2015 9:14 am

Re: objects in macro

Post by chrisb »

Moved from Help forum.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: objects in macro

Post by onekk »

I suspect that when you copy the list it is duplicated so as in FC a duplicate name will become Name001 as there could not be two identical names.

probably iterating using the obj.InList should not produce the list duplication.

InList and OutList are somewhat delicate to manipulate but being on mobile I have not access to my notes (supposed that I'm be able to find them and they are relevant :D)


Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
lagunax
Posts: 55
Joined: Wed May 25, 2022 5:12 pm

Re: objects in macro

Post by lagunax »

onekk wrote: Mon May 30, 2022 3:26 pm I suspect that when you copy the list it is duplicated so as in FC a duplicate name will become Name001 as there could not be two identical names.

probably iterating using the obj.InList should not produce the list duplication.

InList and OutList are somewhat delicate to manipulate but being on mobile I have not access to my notes (supposed that I'm be able to find them and they are relevant :D)


Regards

Carlo D.
first, thanks for moving :D im new in this forum, will try to be more attentive

i'm wrote above not about copy, but about linking.
problem is when i created object in top of Model tree.
reproduce:

Code: Select all

create any object in top of tree
running object.InList and got [[]]
creating link to object (linkobject)
running object.InList and got [[<App::Link object>]]
but i want to know where is object in top of list and have no any objects above.
is there any function or property that i can get to know?
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: objects in macro

Post by heda »

from wiki:
"Note that InList and OutList have nothing to do with the tree view of the document model that is presented in the GUI. At any time, a parent in that tree view may contain children that are part of the InList, the OutList, or neither."

if you really want to go from graphical selection in the treeview, afaik you will have to do that in the qt-treeview, search the forum for pyside treeview and you should be able to find out how to get hold of it. in order to traverse that one you will probably have to study pyside docs to figure things out..., the good news is that it knows what is graphically selected in it and it is obviously aware of what and how things are rendered there...



"in short: i need to go from selected object to top of tree in Model"

Code: Select all

>>> obj = App.ActiveDocument.Wire
>>> for o in obj.InListRecursive:
...   print(o.Label, o.InList)
... 
Wire (mirrored) [<Part object>]
wtf [<App::Link object>]
wtf001 [<group object>]
links []
so far so good, now the bummer is that this is the result regardless if you in the treeview select the wire in wtf001 (the "linked" one) or the wire in wtf (the "original"). the whole point of link is not to recreate the object, so it is what it is...

"but how in this case i can determine that original object placed in top of tree?"

lets see what it reports as in and out...

Code: Select all

>>> for o in obj.InListRecursive:
...   print('###', o.Label)
...   print('in', [q.Label for q in o.InList])
...   print('out', [q.Label for q in o.OutList])
... 
### Wire (mirrored)
in ['wtf']
out ['Wire']
### wtf
in ['wtf001']
out ['Origin', 'Wire (mirrored)']
### wtf001
in ['links']
out ['wtf']
### links
in []
out ['wtf001', '_Ext1']
>>> 


in other words, you can't in a general case, you could in this particular case just filter out "links" from InList, like...

Code: Select all

[o for o in obj.InList if not 'App::Link' in o.TypeId]
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: objects in macro

Post by onekk »

heda wrote: Mon May 30, 2022 11:26 pm from wiki:
"Note that InList and OutList have nothing to do with the tree view of the document model that is presented in the GUI. At any time, a parent in that tree view may contain children that are part of the InList, the OutList, or neither."
...
Hello could you kindly put the links to the page from which you extracted this text?

I'm interested too in this sort if things so it may be a good thingbto note down the page.

TIA and Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: objects in macro

Post by heda »

Post Reply