Retrieve PropertiesList

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Chairy
Posts: 19
Joined: Mon Mar 17, 2014 8:14 am

Retrieve PropertiesList

Post by Chairy »

Hi all,
I am a FreeCAD beginner, so sorry in advance!
I would like to retrieve the label of each part in an assembly from within python.
If I understand correctly Label is stored within PropertiesList as given in
http://www.freecadweb.org/wiki/index.ph ... Object_API
However I can't access PropertiesList. Is this implemented? How do I do it?

In my mind I should do something like:

Code: Select all

structure = Part.read('assembly_name')
part1_Label = structure.Shells[0].PropertiesList.Label
But as I said, PropertiesList does not exist within Shells...

(I am using Freecad0.14)
Thanks!
mario52
Veteran
Posts: 4693
Joined: Wed May 16, 2012 2:13 pm

Re: Retrieve PropertiesList

Post by mario52 »

hi
can be an interesting little macro for you Code_snippets
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.
Chairy
Posts: 19
Joined: Mon Mar 17, 2014 8:14 am

Re: Retrieve PropertiesList

Post by Chairy »

Hello Mario,
thank you for the macro.
As I am working from step-files my real question was if it is not possible to access this
information from the step-file using only the Part class (if any such labels were
hidden somewhere)? I do not if it is possible though.

Before posting I attempted to follow the macro you linked. I then wanted to make a new
document and import the step file to be able to use the Document class. However how do
I add the info from the step-file correctly?
Idea:

Code: Select all

structure=Part.read("step_file")
myDocument = App.newDocument('document_name')
myDocument.addObject("App::PropertyStringList", "Document with data stored in step file")
Label0 = myDocument.Objects[0].PropertiesList.Label
Of course it is only possible to load property data from documents, but how do I generate a document from a step-file? Is it possible to do from a python script without using GUI-related methods such as .loadfile and .open? How?

Thanks!
mario52
Veteran
Posts: 4693
Joined: Wed May 16, 2012 2:13 pm

Re: Retrieve PropertiesList

Post by mario52 »

hi
Qt example to open and save a file (change the extension .txt) , but you must include your code
Spline Punkte

Code: Select all

f = open(OpenName, "r") # open file in read (OpenName is in the Qt example)
bytes = f.read()              # read the file
print bytes                     # print the text
f.close()                         # close the file

f = open(SaveName, 'w') # open a file in write (SaveName is in the Qt example)
f.write("The Write")      # save "The Write" on disck
f.close()                        # close the file
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.
User avatar
wandererfan
Veteran
Posts: 6321
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Retrieve PropertiesList

Post by wandererfan »

Chairy wrote:However I can't access PropertiesList. Is this implemented? How do I do it?
Is this what you're trying to get?

Code: Select all

Property: Angle0 Value: 0 deg
Property: Angle1 Value: 360 deg
Property: Label Value: Circle
Property: Placement Value: Placement [Pos=(0,0,0), Yaw-Pitch-Roll=(0,0,0)]
Property: Radius Value: 2.0
Property: Shape Value: <Edge object at 0x4ec7c50>
If so, use this:

Code: Select all

o = App.ActiveDocument.ActiveObject
op = o.PropertiesList
for p in op:
    print "Property: ", p, " Value: ", o.getPropertyByName (p)
wf

OS: Ubuntu 12.04.4 LTS
Platform: 64-bit
Version: 0.14.3435 (Git)
Branch: master
Hash: 61919e89abdd7630e3893e33374eb303cd0fe726
Python version: 2.7.3
Qt version: 4.8.1
Coin version: 3.1.3
SoQt version: 1.5.0
OCC version: 6.7.0
Chairy
Posts: 19
Joined: Mon Mar 17, 2014 8:14 am

Re: Retrieve PropertiesList

Post by Chairy »

Hi wf,
yes this is what I am trying to return.
The problem is how to get to the first part as the input is a step file and not an "active document".
This is also why I wonder how I go about saving data in a step-file within a Document so I can access this data.

Essentially I want to do the "save"-feature you can find in the gui - but from within the python script.
User avatar
wandererfan
Veteran
Posts: 6321
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Retrieve PropertiesList

Post by wandererfan »

Chairy wrote:The problem is how to get to the first part as the input is a step file and not an "active document".
You can add a STEP part from a file to a document like this:

Code: Select all

f = QtGui.QFileDialog.getOpenFileName(QtGui.qApp.activeWindow(),'Open A Step File',None,'*.step')
myStep = Part.read(str(f[0]))

myDocument = App.newDocument("NewDoc")
myobj = myDocument.addObject("Part::Feature", "StepObject")
myobj.Shape = myStep

op = myobj.PropertiesList
for p in op:
    print "Property: ", p, " Value: ", myobj.getPropertyByName (p)
The problem is that it won't be a smart shape and will only have Label and Placement properties. If the STEP file contains many shapes, it will probably be a Compound when you load it so you won't get any details.
Chairy
Posts: 19
Joined: Mon Mar 17, 2014 8:14 am

Re: Retrieve PropertiesList

Post by Chairy »

Thanks Wf,
Doesn't this code set the label name when in the reality I want to load it from data in the step file?

I also believe using the input "Part.read" in fact do not read label data from the step file at all. If it
did I should be able to access the label data through the Part module rather than through importing
it into the document module, no? It suggests that the label data would in that scenario actually be "hiding"
somewhere within the Part-module. In that case, where?

When you open a step file containing several parts the gui interprets and gathers the real labels given
in the step file. This should be possible to do with the same level of detail by scripting. (Every part will
be one Shell with faces, edges and surfaces as interpreted by FreeCAD).
Post Reply