Name of a solid in a compound - from selection

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
mario52
Veteran
Posts: 4690
Joined: Wed May 16, 2012 2:13 pm

Re: Name of a solid in a compound - from selection

Post by mario52 »

hi
ok this is the object type internal, not name

here other info by object

Code: Select all

# -*- coding: utf-8 -*-
#liste les objets du projet en cours

import os, ImportGui
import FreeCAD
from Draft import *

doc = FreeCAD.ActiveDocument
App.Console.PrintMessage("Document Name : " + str(doc.Name) + "\n")
objs = FreeCAD.ActiveDocument.Objects

print objs
i = 0
for obj in objs:
    i += 1
    print "___ Object num : ",i," ______________________________________________________"
    App.Console.PrintMessage("Type     : " + str(obj) + "\n")        
    App.Console.PrintMessage("TypeId   : " + str(obj.TypeId) + "\n")        
    App.Console.PrintMessage("Object   : " + str(obj.Shape) + "\n")
    App.Console.PrintMessage("Name     : " + str(obj.Name) + "\n")
    App.Console.PrintMessage("RealName : " + str(getRealName(obj.Name)) + "\n")
    App.Console.PrintMessage("Label    : " + str(obj.Label) + "\n")
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Name of a solid in a compound - from selection

Post by HoWil »

The question left is how did bernd the trick in FEM-wb mesh-Groups (e.g. selectionParser in _TaskPanelFemMeshGroup.py)?
I did study his code a bit but could not find the final clue.

@Bernd ....

BR,
HoWil
mario52
Veteran
Posts: 4690
Joined: Wed May 16, 2012 2:13 pm

Re: Name of a solid in a compound - from selection

Post by mario52 »

hi
extract to _TaskPanelFemMeshGroup.py

Code: Select all

selection  = FreeCADGui.Selection.getSelection()
print('selection element changed to Solid: ', selection[0].Shape.ShapeType, '  ', selection[0].Name)

for i, s in enumerate(selection[0].Shape.Solids):
    for e in s.Edges:
        print e
print
for i, s in enumerate(selection[0].Shape.Solids):
    for e in s.Faces:
        print e
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Name of a solid in a compound - from selection

Post by HoWil »

mario52 wrote:hi
extract to _TaskPanelFemMeshGroup.py

Code: Select all

selection  = FreeCADGui.Selection.getSelection()
print('selection element changed to Solid: ', selection[0].Shape.ShapeType, '  ', selection[0].Name)

for i, s in enumerate(selection[0].Shape.Solids):
    for e in s.Edges:
        print e
print
for i, s in enumerate(selection[0].Shape.Solids):
    for e in s.Faces:
        print e
mario
Hi mario,

This 'only' lists only all Edges and Fases of all Solids. It does not give a clue how the Solids 's' are named.

Code: Select all

>>> selection  = FreeCADGui.Selection.getSelection()
>>> print('selection element changed to Solid: ', selection[0].Shape.ShapeType, '  ', selection[0].Name)
('selection element changed to Solid: ', 'Compound', '  ', 'BooleanFragments')
>>> 
>>> for i, s in enumerate(selection[0].Shape.Solids):
...     for e in s.Edges:
...         print e
... 
<Edge object at 0x28ba760>
<Edge object at 0x2930cd0>
<Edge object at 0x2931810>
<Edge object at 0x292ddb0>
<Edge object at 0x2932640>
<Edge object at 0x292e2b0>
<Edge object at 0x28a00b0>
<Edge object at 0x29243f0>
<Edge object at 0x2821080>
<Edge object at 0x275fb70>
<Edge object at 0x1b91140>
<Edge object at 0x28c7da0>
<Edge object at 0x2931cb0>
<Edge object at 0x289de20>
<Edge object at 0x29326f0>
<Edge object at 0x2936860>
<Edge object at 0x2932ab0>
<Edge object at 0x281d2b0>
<Edge object at 0x292d7f0>
<Edge object at 0x292dcb0>
<Edge object at 0x289d9e0>
<Edge object at 0x289e0c0>
<Edge object at 0x28b9160>
<Edge object at 0x2936ee0>
<Edge object at 0x289c500>
<Edge object at 0x291a040>
<Edge object at 0x28c22f0>
<Edge object at 0x29bd0c0>
<Edge object at 0x2930c00>
<Edge object at 0x2931d90>
<Edge object at 0x2929c80>
<Edge object at 0x292dbe0>
<Edge object at 0x28bc6b0>
>>> for i, s in enumerate(selection[0].Shape.Solids):
...     for e in s.Faces:
...         print e
... 
<Face object at 0x1d1ac10>
<Face object at 0x28c5810>
<Face object at 0x2929260>
<Face object at 0x28acd20>
<Face object at 0x2a537d0>
<Face object at 0x28bc6b0>
<Face object at 0x28d8a00>
<Face object at 0x292bcf0>
<Face object at 0x29378f0>
<Face object at 0x28b5c00>
<Face object at 0x2933d50>
<Face object at 0x2939290>
<Face object at 0x2929260>
<Face object at 0x28d65e0>
<Face object at 0x28e40b0>
<Face object at 0x28adfc0>
<Face object at 0x28ae290>
>>> 
BR,
HoWil

ps.: I removed the single print in the middle without output.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Name of a solid in a compound - from selection

Post by bernd »

I haven't had the time to read all, will do this later on the weekend. But this may help ... viewtopic.php?f=10&t=18937
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Name of a solid in a compound - from selection

Post by bernd »

HoWil wrote:I also included a min. .fcstd ...
with your file ...

The name of Solid[0] is Solid1 same name system as for Vertexes, Edges and Faces! The problem is how to get the solid, becaus getElement does not return the solid (see my last post), thus I implemented some own get_element in FemMeshTools which returns solids too.

Code: Select all

# App.ActiveDocument.Fusion.Shape.Solids[0] --> 'Solid1' of Fusion

import FemMeshTools
FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1')

App.ActiveDocument.Fusion.Shape.Solids[0].isSame(FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1'))
output:

Code: Select all

>>>
>>> # App.ActiveDocument.Fusion.Shape.Solids[0] --> 'Solid1' of Fusion
>>> 
>>> import FemMeshTools
>>> FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1')
<Solid object at 0x31fb4a0>
>>> 
>>> App.ActiveDocument.Fusion.Shape.Solids[0].isSame(FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1'))
True
>>>
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Name of a solid in a compound - from selection

Post by HoWil »

bernd wrote:
HoWil wrote:I also included a min. .fcstd ...
with your file ...

The name of Solid[0] is Solid1 same name system as for Vertexes, Edges and Faces! The problem is how to get the solid, becaus getElement does not return the solid (see my last post), thus I implemented some own get_element in FemMeshTools which returns solids too.

Code: Select all

# App.ActiveDocument.Fusion.Shape.Solids[0] --> 'Solid1' of Fusion

import FemMeshTools
FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1')

App.ActiveDocument.Fusion.Shape.Solids[0].isSame(FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1'))
output:

Code: Select all

>>>
>>> # App.ActiveDocument.Fusion.Shape.Solids[0] --> 'Solid1' of Fusion
>>> 
>>> import FemMeshTools
>>> FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1')
<Solid object at 0x31fb4a0>
>>> 
>>> App.ActiveDocument.Fusion.Shape.Solids[0].isSame(FemMeshTools.get_element(App.ActiveDocument.Fusion, 'Solid1'))
True
>>>
Hi Bernd,
So you search through all solids in Fusion and look for the generic names 'Solid1', 'Solid2', ... ?
I don't like it... :D but if this is the only solution than I take it 8-) .
BR,
HoWil
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Name of a solid in a compound - from selection

Post by bernd »

I might have not 100% understood what exactly your problem is about.
HoWil wrote:So you search through all solids in Fusion and look for the generic names 'Solid1', 'Solid2', ... ?
I don't like it... :D but if this is the only solution than I take it 8-) .
To select an solid out of an CompSolid or an Compound I activate a solid selection mode. In this mode the user has to select an edge or vertex which only belongs to one solid. Thus the solid is found. The reason is, there is no solid selection mode in FreeCAD selection system. See post viewtopic.php?f=10&t=18937#p147550 To save the solid in the reference shape list for later use I use the position in the solids list of the shape. Solids[0] is 'Solid1', Solids[1] is 'Solid2'

Just realized the system might not work if a solid is inside another solid, because all elements of the inner solid belong to the outer solid too. I need to try this on the mesh region object.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Name of a solid in a compound - from selection

Post by DeepSOIC »

bernd wrote:Just realized the system might not work if a solid is inside another solid, because all elements of the inner solid belong to the outer solid too.
Then I can suggest this approach: "if in doubt, choose one with the smaller volume".
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Name of a solid in a compound - from selection

Post by bernd »

DeepSOIC wrote:
bernd wrote:Just realized the system might not work if a solid is inside another solid, because all elements of the inner solid belong to the outer solid too.
Then I can suggest this approach: "if in doubt, choose one with the smaller volume".
Good idea, but could return the wrong one, because the inner could have less volume than the outer (the outer solid has not the volume of the inner, in case of a CompSolid). The bbox might be better approach. I really need to test ...
Post Reply