Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write FCStd node should create a FreeCAD file. #4432

Closed
macdroid53 opened this issue Apr 23, 2022 · 22 comments
Closed

Write FCStd node should create a FreeCAD file. #4432

macdroid53 opened this issue Apr 23, 2022 · 22 comments

Comments

@macdroid53
Copy link

Expected:
When Write FCStd node is executed creates file with specified name.

Actual:
When node is executed no file is created. If an empty FCStd file by the specified name exists, the data is written.

@zeffii
Copy link
Collaborator

zeffii commented Apr 24, 2022

https://wiki.freecadweb.org/FreeCAD_API
suggests a function call like F.newDocument(doc_name) would be capable of starting a new empty file. (or in-memory document)
it looks to me like F.open is only for existing Freecad docs?

https://github.com/nortikin/sverchok/blob/master/nodes/exchange/FCStd_write.py

@zeffii
Copy link
Collaborator

zeffii commented Apr 24, 2022

I see the FCStd_write node doesn't offer a way to start a new freecad file from scratch, only a way to pick up an existing file.
I can't be certain without trying the library myself, but it looks like this is how to start a new doc? Some useful snippets found here https://www.programcreek.com/python/example/89887/FreeCAD.newDocument

doc = F.newDocument(path)

# do stuff.
doc.recompute()

F.getDocument(doc.name).save()
F.closeDocument(doc.name)

@zeffii
Copy link
Collaborator

zeffii commented Apr 24, 2022

i propose we add a button to the FCStd_write node, which lets you

  • pick a file location and a new file name,
  • and then the node automatically adds a FilePath Node attached to the FCStd_write node
    • The newly created FilePath will point at the File
    • The newly created File will be initialized as a new empty freecad file.

yay/nay ?

@macdroid53
Copy link
Author

Hmm...I thought what happened when I tried the node was I could indeed select a path and filename (note: it by default offered .blend as the extension not .FCStd).
But, if the file did not exist and the node executed, it did nothing (which was good at that point because it would have overwritten my blend file of the same name?)
IMO, it should act as would be expected of any file output. If the specified name exists, ask if it should be overwritten, otherwise create the file and output the solid.

@zeffii
Copy link
Collaborator

zeffii commented Apr 24, 2022

yeah OK. that makes a lot more sense. FilePath node allows you to type an arbitrary filename into the FileName text box, and it will pass the current path + filename (or actually os.path.join(current_path, filename)) into the FilePath output socket (even if the file does not exist)

@zeffii
Copy link
Collaborator

zeffii commented Apr 24, 2022

perhaps something like this

    try:
        from os.path import exists
        
        Fname = bpy.path.display_name_from_filepath(fc_file)
        
        if not exists(fc_file):
            doc = F.newDocument(Fname)
            doc.FileName = fc_file
            doc.recompute()
            doc.save()
        
        F.open(fc_file)

    except Exception as err:
        info(f'FCStd open error, {err}')
        return

    F.setActiveDocument(Fname)
    fc_root = F.getDocument(Fname)

@macdroid53
Copy link
Author

This seems good.

I don't know if there is a way to do it. When I click on the socket and it creates a File path node, it would be nice if it defaulted to .FCStd rather than .blend. But, the code you show would at least help to prevent and accidental overwrite.

@zeffii
Copy link
Collaborator

zeffii commented Apr 24, 2022

it would be nice if it defaulted to .FCStd

yeah, it may be possible to coerce the instance of the FileOpen dialogue to do that.

@zeffii
Copy link
Collaborator

zeffii commented Apr 25, 2022

@macdroid53 i can make a branch for you and you can tell me if you like how that works.

  • can reject filepaths that don't end in .FCStd
  • can create new file at filepath specified in FilePath Node, if filepath doesn't exist yet.

@macdroid53
Copy link
Author

Sure, I can have a look.
Can I just copy the code to /home/mac/.config/blender/2.93/scripts/addons/sverchok/

@zeffii
Copy link
Collaborator

zeffii commented Apr 25, 2022

yeah, that'll work.. or you could even just replace the nodes/exchange/FCStd_write.py file manually.

@macdroid53
Copy link
Author

Which branch?

@zeffii
Copy link
Collaborator

zeffii commented Apr 25, 2022

sorry. just added : #4437

@zeffii
Copy link
Collaborator

zeffii commented Apr 25, 2022

it is untested as I can not get freecad currently

@zeffii
Copy link
Collaborator

zeffii commented Apr 25, 2022

but for now i only implemented the feature that tells the FCStd_write node to create the FCStd file on-the-fly if it doesn't exist.

@luzpaz
Copy link
Contributor

luzpaz commented Apr 25, 2022

CC
@yorikvanhavre (FreeCAD)
@wassimj (Topologic)
@Moult (BlenderBIM)

@yorikvanhavre
Copy link

yorikvanhavre commented Apr 29, 2022

Note that doc.save() assumes the doc.FileName attribute is set. If you create a doc with FreeCAD.newDocument(docname), the FileName attribute will be an empty string. Rather, better use doc.saveAs(filepath). This also has the effect of setting FileName.

If you need a defautl value, usually what many exporters do is take the path of the current blend file, strip the .blend extension and replace it with .FCStd...

@zeffii
Copy link
Collaborator

zeffii commented May 1, 2022

implemented. seems to work, thanks @yorikvanhavre !

@zeffii zeffii closed this as completed May 1, 2022
@luzpaz
Copy link
Contributor

luzpaz commented May 1, 2022

Nice! What's the next task?

@zeffii
Copy link
Collaborator

zeffii commented May 1, 2022

a solution will need to be found for forcing the FilePath node to look specifically for certain kinds of files, and automatically suffix .FCStd if not provided by user.

@zeffii
Copy link
Collaborator

zeffii commented May 1, 2022

but that's an issue with FilePath node, rather than FreeCAD =)

@luzpaz
Copy link
Contributor

luzpaz commented May 1, 2022

Apropos #4446

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants