Document-Specific scripts?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Bayesian
Posts: 90
Joined: Thu Aug 08, 2019 1:49 pm

Document-Specific scripts?

Post by Bayesian »

I know that I can put Python scripts into macros (or notebooks now). But I increasingly find myself writing code for individual documents, for example to change values, recompute, export and so on. It may even be a good idea in terms of organizing/distributing files to have the script inside the FC file.

Is there any way put a Python script inside a document file? If not, regard this as a Feature suggestion ;-)
User avatar
Chris_G
Veteran
Posts: 2602
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Document-Specific scripts?

Post by Chris_G »

Here is an interesting related topic.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Document-Specific scripts?

Post by vocx »

Bayesian wrote: Tue Sep 03, 2019 12:27 pm ...to have the script inside the FC file.

Is there any way put a Python script inside a document file? If not, regard this as a Feature suggestion ;-)
Besides the thread already mentioned, How to embed a Python code into the document?

I finally found these other two references that I also wanted to mention: In particular, microelly2 mentions using a StringList property. This is actually how the Draft Text object is implemented.

Code: Select all

import FreeCAD as App
obj = App.ActiveDocument.addObject("App::FeaturePython", "Textual_block")
obj.addProperty("App::PropertyStringList", "Text", "Base", "The text content displayed by this object")
Now arbitrary text can be placed inside the Text attribute, maybe even entire lines of code.

Code: Select all

obj.Text="""
import FreeCAD as App
obj = App.ActiveDocument.addObject("App::FeaturePython", "Textual_block")
obj.addProperty("App::PropertyStringList", "Text", "Base", "The text content displayed by this object")

# Some functions
def bla():
    blah, bloh = something()
"""
The text can be simply retrieved by printing to the terminal

Code: Select all

print(obj.Text[0])
Then you could add a button to display the text in the macro editor, or in another text box.

Code: Select all

import PySide
Text = PySide.QtGui.QTextEdit()
Text.setText(obj.Text[0])
Text.setFont(PySide.QtGui.QFont("Mono"))
Text.show()
In the second thread above, Yorik mentions about the FileIncluded property. It can be used to save any file into the .FCstd file, for example, an entire text file (macro code).

Code: Select all

obj.addProperty("App::PropertyFileIncluded", "AttachedTextFile", "Base", "A file to save inside this document")
The important thing is to never run automatically external arbitrary text. Loading is one thing, but running the text must be done manually by the user.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply