Path to FeaturePython with PropertyFile

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Path to FeaturePython with PropertyFile

Post by onekk »

viewtopic.php?p=323945#p323945

In this post it seem to say that this is creating the property with the name and then you must assign the file to the created property.

then when saving the FCStd file the added file is saved.

But as it speak about PDF file in next post it says that the file will be opened in a temporary directory and could be accessed read only.

I will try maybe to test something later as it is a thing that interest also my work.

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/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Path to FeaturePython with PropertyFile

Post by keithsloan52 »

onekk wrote: Sun Feb 05, 2023 1:01 pm viewtopic.php?p=323945#p323945

In this post it seem to say that this is creating the property with the name and then you must assign the file to the created property.

then when saving the FCStd file the added file is saved.

But as it speak about PDF file in next post it says that the file will be opened in a temporary directory and could be accessed read only.

I will try maybe to test something later as it is a thing that interest also my work.

Regards.

Carlo D.
Hi Carlo, Yes I had found the post and also the reference to the code in Arch.

Wondering if I have to use two properties, one with FileName which allows me to have a property that is a path to a file I can use with an external editor and also one with FileNameIncluded that points to the same file but seems a bit of a kludge solution and wondering if there is a better way.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Path to FeaturePython with PropertyFile

Post by onekk »

keithsloan52 wrote: Sun Feb 05, 2023 12:35 pm ...
Hello see if this will work for you.
20230205-ks_propertyfile.py
(2.71 KiB) Downloaded 18 times
Saving the file will correctly place the document in the FCstd file.

The only problem I could see is the path is to the temporary directory, so probably something could not be correct.

@yorik how to manage the reopening of the file saved in the FCstd document?

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/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Path to FeaturePython with PropertyFile

Post by keithsloan52 »

onekk wrote: Sun Feb 05, 2023 3:37 pm
keithsloan52 wrote: Sun Feb 05, 2023 12:35 pm ...
Hello see if this will work for you.

20230205-ks_propertyfile.py

Saving the file will correctly place the document in the FCstd file.

The only problem I could see is the path is to the temporary directory, so probably something could not be correct.

@yorik how to manage the reopening of the file saved in the FCstd document?

Regards

Carlo D.
I changed my code based on your attempt
tmpDir = obj.Document.getPropertyByName("TransientDir")

Gets the same directory that I was getting via
tmpDir = obj.Document.TransientDir

On trying to execute i.e. pass the file to OpenSCAD I get as before
20:45:26 Error Message ERROR: Parser error: syntax error line 2
Which is strange given the file is just one line

If I pass the file to Emacs it loads the file but puts out a warning that the file is in a read only file system

If I set up a temp directory with
import tempfile
tmpDir = temple.gettempdir()

and create the file in there, then I can fire up Emacs and again it has the same issues.

If I use Python tempfile to create the directory and use PropertyFile rather than PropertyFileIncluded, then I can fire up Emacs passing the file and it loads okay. I can edit the file and save, run execute to pass the file to OpenSCAD and that all works okay.
But if I save the FreeCAD Doc and load I don't get the edited file, it is the same as created with the initial open and write
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Path to FeaturePython with PropertyFile

Post by onekk »

Probably you have to assign the file just prior to save the FCStd file.

Or maybe see if yorik has some hints.

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/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Path to FeaturePython with PropertyFile

Post by keithsloan52 »

Maybe have to use PropertyFile and add the following functions to the class

Code: Select all

def __getstate__(self):
        """When saving the document this object gets stored using Python's json
        module.
        Since we have some un-serializable parts here -- the Coin stuff --
        we must define this method\
        to return a tuple of all serializable objects or None."""
        if hasattr(self, "Type"):
            return {"type": self.Type}
        else:
            pass

    def __setstate__(self, arg):
        """When restoring the serialized object from document we have the
        chance to set some internals here. Since no data were serialized
        nothing needs to be done here."""
        self.Type = arg["type"]
But question is what to test for and return in the tuple for a file
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Path to FeaturePython with PropertyFile

Post by keithsloan52 »

Okay I tried with the SCAD Object having two properties sourceFile PropertyFile and saveSourceFile PropertyFileIncluded
and after an "execute" where the Shape gets calculated, doing a file copy sourceFile to saveSourceFile but I get a permissions problem

Code: Select all

08:51:37  shapeFrom Source File
08:51:37  Source : Scad_Object.scad
08:51:37  SourceFile : /var/folders/wj/zxk080cs18v44qrc80hm4z7h0000gn/T/Scad_Object.scad
08:51:37  CSG File name /var/folders/wj/zxk080cs18v44qrc80hm4z7h0000gn/T/Scad_Object.csg
08:51:37  Doc work
08:51:37  tokens ('group', 'sphere', 'cylinder', 'cube', 'multmatrix', 'intersection', 'difference', 'union', 'rotate_extrude', 'linear_extrude', 'true', 'false', 'circle', 'square', 'text', 'polygon', 'paths', 'points', 'undef', 'polyhedron', 'triangles', 'faces', 'render', 'surface', 'subdiv', 'glide', 'hull', 'minkowski', 'projection', 'import', 'color', 'offset', 'resize', 'WORD', 'NUMBER', 'LPAREN', 'RPAREN', 'OBRACE', 'EBRACE', 'OSQUARE', 'ESQUARE', 'COMMA', 'SEMICOL', 'EQ', 'STRING', 'ID', 'DOT', 'MODIFIERBACK', 'MODIFIERDEBUG', 'MODIFIERROOT', 'MODIFIERDISABLE')
08:51:37  Start Lex
08:51:37  End Lex
08:51:37  Load Parser
08:51:37  Parser Loaded
08:51:37  Start Parser
08:51:37  End processing CSG file
08:51:37  1
08:51:37  Scad_Object State : ['Up-to-date'] prop : Shape
08:51:37  Copy File /var/folders/wj/zxk080cs18v44qrc80hm4z7h0000gn/T/Scad_Object.scad /Users/keithsloan/Library/Caches/FreeCAD/Cache/FreeCAD_Doc_2b57825f-1fa3-4c9c-9894-6f87967c0cb7_da39a3_1368/Scad_Object.scad
08:51:37  Traceback (most recent call last):
  File "/Applications/FreeCAD_Dev.app/Contents/Resources/Mod/OpenSCAD/OpenSCADFeatures.py", line 807, in onChanged
    self.copyFile(obj.sourceFile, obj.saveSourceFile)
  File "/Applications/FreeCAD_Dev.app/Contents/Resources/Mod/OpenSCAD/OpenSCADFeatures.py", line 833, in copyFile
    fpt = open(trg,'w')
<class 'PermissionError'>: [Errno 13] Permission denied: '/Users/keithsloan/Library/Caches/FreeCAD/Cache/FreeCAD_Doc_2b57825f-1fa3-4c9c-9894-6f87967c0cb7_da39a3_1368/Scad_Object.scad'
Did not like that solution anyway as it meant copying files :(
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Path to FeaturePython with PropertyFile

Post by keithsloan52 »

Okay I think I have a solution

Use PropertyFile and add

Code: Select all

def __getstate__(self):
        """When saving the document this object gets stored using Python's json
        module.
        Since we have some un-serializable parts here -- the Coin stuff --
        we must define this method\
        to return a tuple of all serializable objects or None."""
        if hasattr(self, "obj"):
            if hasattr(self.obj, "sourceFile"):
                print(f"Save Source File {self.obj.sourceFile}")
                sf = open(self.obj.sourceFile, 'r')
                buffer = sf.read()
                return {"sourceFile": [self.obj.sourceFile, buffer]}
        else:
            pass         
Still open to suggestions of a better way.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Path to FeaturePython with PropertyFile

Post by onekk »

It is not very clear as yorik in the post linked was speaking about pdf files and read only in the discussion.

And then at the end there are a phrase about seing how something thst lead to think that the interface is limited to put files in FCStd document.

As example the Text object is more usable as it could be added and edited. And someone has written a Macro to read its content and execute macro code stored on it.

This is the reason why I've poked yorik

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/
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Path to FeaturePython with PropertyFile

Post by keithsloan52 »

onekk wrote: Mon Feb 06, 2023 9:03 am It is not very clear as yorik in the post linked was speaking about pdf files and read only in the discussion.

And then at the end there are a phrase about seing how something thst lead to think that the interface is limited to put files in FCStd document.

As example the Text object is more usable as it could be added and edited. And someone has written a Macro to read its content and execute macro code stored on it.

This is the reason why I've poked yorik

Regards

Carlo D.
Well I now have working code see https://www.dropbox.com/scl/fo/3qg6d02s ... b33a4r000a

I will look at a Text object but I am not sure a separate object is the solution, the source has to be saved with the SCADObject which is a PartFeaturePython object.
Post Reply