[solved] "All"-File management / database with text files in the directory tree for linux

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
thomas-neemann
Veteran
Posts: 11921
Joined: Wed Jan 22, 2020 6:03 pm
Location: Osnabrück DE 🇩🇪
Contact:

Re: File management / database with text files in the directory tree for linux

Post by thomas-neemann »

onekk wrote: Tue Nov 16, 2021 5:48 pm
I don't know if I understand correctly. if it comes to e.g. to eliminate chaos after unintentional file shifts, you only need to write a script which moves fcstd and txt files of the same name back into a common directory.
Last edited by thomas-neemann on Tue Nov 16, 2021 7:09 pm, edited 1 time in total.
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: File management / database with text files in the directory tree for linux

Post by onekk »

No it is different thing, you could do many things.

  • you load an ipotetic
    project.py
    it opens in the "macro editor" and you could launch it with the "big green arrow"
  • It show up a "Gui window" on which you could choose what you want, one options could be "load project files"
  • project files could be loaded using some other data stored in the "project.py" that is a readable python source code file
so a complex project could reside in a articulated directory, where in the root you have simply a project.py and in subdirs that could may be Archive Actual Document and so on will reside all the projects files maybe even backups and so on.

You could have in the Gui a button that will "zip the directory" and put the zip into a predetemined position, as you could issue shell commands.

All without touching anything on the FCStd files that are containing parts.

This way you could even have a sqlite3 dump object that could be used to hold all the "relations between files".

It's up to you to make all things, using python.

A sort of "Customized way to do things".

I usually have a minimal Gui that show up, like:
huma-main.png
huma-main.png (15.78 KiB) Viewed 1435 times
That will load the ini file containing the information about the thing I want to make.

and in this case:
  • It will create the model
  • if there is the appropriate flag in the ini file it will create also:
    • Draft Files to be modified maybe to add some holes or similar things
    • automatically make Draft Files and export them to DXF to be fed to a CAM program that will produce code to cu the parts on a CNC machine
Obviously, it is a complex program made of around 12.000 or 13.000 lines of code, but the Gui and some simple "button chest" could be implemented in roughly 1000 lines of code (GUI is hand made so no Qt helpers that will make things more complex), all in one file.

Hope to have been clear.

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/
User avatar
thomas-neemann
Veteran
Posts: 11921
Joined: Wed Jan 22, 2020 6:03 pm
Location: Osnabrück DE 🇩🇪
Contact:

Re: File management / database with text files in the directory tree for linux

Post by thomas-neemann »

onekk wrote: Tue Nov 16, 2021 6:18 pm
i have great respect for the work of jriegel wmayer yorik and all the other programmers. i don't want to cause you any more work or create new problems, so i try shell scripts and shell tools. i have been working with shell since the 80s. i'm just starting with python. make my first attempts at walking.
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: File management / database with text files in the directory tree for linux

Post by onekk »

No problem at all, I'm not a developer in strict sense, only a "importune" that sometimes will make some post and sometimes will "reach the target", maybe suggesting some thing to more skilled developers. (Only for this reason I'm on FreeCAD Credits page).

Said so, if you want some code to test, let me know?

I could put together some code with a "simple Gui", and some button to test "the way" of doing things I have illustrated.

And maybe with some "concrete code" here discussion will be more "constructive", but I think that it has already been "constructive".

At the very end I usually try to make things works, without asking too much, a part of asking for clarification about some internals to skilled developers. :D

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/
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: File management / database with text files in the directory tree for linux

Post by onekk »

Simple test code:

Code: Select all

"""test-gui.py

    Copyright 2021 Carlo Dormeletti
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
    WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

"""

import os
import sys
from datetime import datetime

import FreeCAD

from FreeCAD import Placement, Rotation, Vector

import Draft
import Part

import math

from PySide import QtGui, QtCore
from functools import partial

DOC_NAME = "test_gui"
HOME = u"/home/carlo"
DOC = FreeCAD.activeDocument()

def clear_doc(doc_name):
    """
    Clear the document deleting all the objects
    """
    doc = FreeCAD.getDocument(doc_name)
    doc.recompute()
    objects = doc.Objects
    for obj in objects:
        # debug info do not delete
        #print("Tipo : {}".format(obj.TypeId))
        # fake if when i find what is causing the failure
        if obj.TypeId in ("App::"):
            pass
        else:
            doc.removeObject(obj.Name)

def ensure_document(doc_name, action = 0):
    """Ensure existence of document with doc_name and clear the doc

    Returns:
        string   Document name from obj.Name

    Parameters:

        doc_name   string  document name
        action     integer clear action if document exist
                            0: close and reopen the document with the same name
                            1: delete all objects

    """
    doc_root = FreeCAD.listDocuments()
    doc_exist = False

    for d_name, doc in doc_root.items():
        # debug info do not delete
        print("ED: doc name = {}".format(d_name))

        if d_name == doc_name:
            print("Match name")
            if action == 0:
                # when using clear_doc() is not possible like in Part.Design
                FreeCAD.closeDocument(doc_name)
                doc_exist = False
            else:
                doc_exist = True
                clear_doc(doc_name)
                FreeCAD.setActiveDocument(d_name)

    if doc_exist == False:
        # debug info do not delete
        #print("ED: Create = {}".format(doc_name))
        FreeCAD.newDocument(doc_name)

    return FreeCAD.activeDocument().Name


def setview(doc_name, t_v = 0):
    FreeCAD.setActiveDocument(doc_name)

    VIEW = FreeCAD.Gui.ActiveDocument.ActiveView

    if t_v == 0:
        VIEW.viewTop()
    else:
        VIEW.viewAxometric()
    VIEW.setAxisCross(True)
    VIEW.fitAll()

class main_dia(QtGui.QDialog):
    """Create Main window input box

    """
    if_fields = ()
    tab_names = (
        ('Main', 'mn'), ("Log", "lg"))

    ap_style  = "font-size: 14px"
    tx_style  = "font-size: 16px"
    # needed to not trow error prior to push validate button,
    # dictionary is totally overwritten by validate_data
    vals = {'validate': ["KO"]}
    config = None
    base_name = ""
    action = ""
    main_doc = None

    def __init__(self):
        super(main_dia, self).__init__()
        now = datetime.now()
        run_time = now.strftime("%y-%m-%d-%H-%M")
        self.base_name = "test_gui_" + run_time
        self.initUI()

    def initUI(self):
        self.result = ""
        # create our window
        sc_wid = QtGui.QDesktopWidget().screenGeometry().width()
        sc_hei = QtGui.QDesktopWidget().screenGeometry().height()
        # get dimensions for available space on screen
        av_wid = QtGui.QDesktopWidget().availableGeometry().width()
        av_hei = QtGui.QDesktopWidget().availableGeometry().height()
        w_wid = av_wid * 0.33
        w_hei = av_hei * 0.60
        x_loc = (sc_wid - w_wid) * 0.5
        y_loc = (sc_hei - w_hei) * 0.5
        # define window        xLoc,yLoc,xDim,yDim
        self.setGeometry(x_loc, y_loc, w_wid, w_hei)
        self.setWindowTitle("Test Gui main Window")
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)

        self.lay = QtGui.QVBoxLayout(self)

        self.tabw = QtGui.QTabWidget(self)

        for tab_o in self.tab_names:
            tab = QtGui.QWidget()
            tab.setObjectName('t_' + tab_o[1])
            lay = QtGui.QGridLayout(tab)
            lay.setObjectName(tab_o[1])
            self.tabw.addTab(tab, tab_o[0])

        wid = self.tabw.findChild(QtCore.QObject, "mn")
        self.Btn_acp = QtGui.QPushButton('Load ', self)
        self.Btn_acp.clicked.connect(self.load_file)
        self.Btn_acp.setAutoDefault(False)
        wid.addWidget(self.Btn_acp, 1, 0)

        self.txt_win = QtGui.QTextEdit()
        self.txt_win.setStyleSheet(self.tx_style)
        self.txt_win.setReadOnly(True)
        self.txt_win.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)

        wid = self.tabw.findChild(QtCore.QObject, "lg")
        wid.addWidget(self.txt_win, 0, 0)

        self.lay.addWidget(self.tabw)

        self.setLayout(self.lay)

        self.show()

    def load_file(self):
        ensure_document(DOC_NAME)
        Lmsg = (("file {} created".format(DOC_NAME),))
        self.write_log(Lmsg)

    def closeEvent(self, event):
        event.accept()

    def write_log(self, Lmsg):
        self.txt_win.clear()
        out_msg = "\n".join(Lmsg)
        self.txt_win.setText(out_msg)

if __name__ == "__main__":
    main_f = main_dia()
    main_f.show()

It will create a persistent window from which you could control something, there is only a button that will create a new empty file and write in the Log Tab a simple phrase.

execution code is in
load_file()
and his very simple.

Code: Select all

   def load_file(self):
        ensure_document(DOC_NAME)
        Lmsg = (("file {} created".format(DOC_NAME),))
        self.write_log(Lmsg)
it is not doing what the name is telling, as I've reused code from one existing project making minimal changes, but this will show you a way of doing things.

place maybe the code in a text file named "project.py" and place in a directory.

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/
user1234
Veteran
Posts: 3511
Joined: Mon Jul 11, 2016 5:08 pm

Re: File management / database with text files in the directory tree for linux

Post by user1234 »

onekk wrote: Tue Nov 16, 2021 4:00 pm PostgresSQL is great, but this is an additional program.
True. But sqlite will not handle the amount CAD files. If you only took screws from
- size: M4 - M30
- lenght
- material from 8.8, 10.9, 12.9, A2-50, A2-70, A4-50, A4-70, C1-50, .....
- surface treatment: A2F, PHR, tzn
- head and drive: ISO4014, ISO4017, ISO4762, ISO7984, ISO10642, ....

then you will get many many screws. If you want to make a database, then all parts must be separated. To think, that you can load only one screw and adapt this with a table in the screw, will not work in real life, since you will send a BoM to an ERP system, which need all parts separated. And when you can configure it in a table, then often in the background a new screw will be loaded or generated. And now think about nuts, washers, bearings, taper pins, ......

At least you can do it like Tryton and make it open, which databasesystem you use. Tryton is able to use sqlite, postgresql and a third one, which can not find it the docs at the moment (i think it was mysql).


thomas-neemann wrote: Tue Nov 16, 2021 4:21 pm I have been able to secure some companies to the intrnet, but I think it is impossible to secure a company internally. that can only be based on trust. in my opinion
I mean internal sabotage, the reason why you need logs (also have seen this recently). Also someone can accidentally make parts of the text files invalid. Also then you will need logs.

Greetings
user1234
User avatar
thomas-neemann
Veteran
Posts: 11921
Joined: Wed Jan 22, 2020 6:03 pm
Location: Osnabrück DE 🇩🇪
Contact:

Re: File management / database with text files in the directory tree for linux

Post by thomas-neemann »

user1234 wrote: Tue Nov 16, 2021 7:41 pm ... someone can accidentally make parts of the text files invalid.
my customers create data backups for accidental damage
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
user1234
Veteran
Posts: 3511
Joined: Mon Jul 11, 2016 5:08 pm

Re: File management / database with text files in the directory tree for linux

Post by user1234 »

And you know, all parts the person edited in the session? Without logs it will be pure guessing. Also a backup can only save and actual state, which means while someone edited parts false, 100 other people have done all correct. With a backup you will delete all work from the others, when you do not know the diffs. So you need the logs to correct only the faulty one.

Greetings
user1234
User avatar
thomas-neemann
Veteran
Posts: 11921
Joined: Wed Jan 22, 2020 6:03 pm
Location: Osnabrück DE 🇩🇪
Contact:

Re: File management / database with text files in the directory tree for linux

Post by thomas-neemann »

user1234 wrote: Tue Nov 16, 2021 7:52 pm
they are empirical values ​​from over 30 years. as soon as this turns out to be no longer usable, you can upgrade
Gruß Dipl.-Ing. (FH) Thomas Neemann

https://www.youtube.com/@thomasneemann5 ... ry=freecad
user1234
Veteran
Posts: 3511
Joined: Mon Jul 11, 2016 5:08 pm

Re: File management / database with text files in the directory tree for linux

Post by user1234 »

Yes that i heard this often. Then you get an other ERP system, which can not handle this sort of "table" and so you must edit 100000+ per hand, also have seen this. This is not about the empirical datas, this is about to maintain the datas!

Greetings
user1234
Post Reply