Auto Project Formatter Macro up for review

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
BvdM
Posts: 1
Joined: Fri Mar 22, 2019 8:27 am

Auto Project Formatter Macro up for review

Post by BvdM »

Hi,

For a beginner in 3d drawing it can be hard to organise files and select the correct workbench to work with. This macro is built to organise files and folders for you. It puts you directly into the "Part Design" workbench. No worries about structuring files and filenames, just start drawing!
This is especially usefull in a workshop environment to give everybody the same starting position.

This is my first FreeCAD script.
English is not my native language.
I hope you find it useful and/or you have some suggestions to improve it!

I want to eventually include this script into the main FreeCAD Macro repo.
But I might need some help with Github, already reading the guides on the forum :geek:

Code: Select all

# -*- coding: utf-8 -*-
# Type: FileCreation
__Name__ = 'AutoProjectFormatter'
__Comment__ = 'This macro structures project folders ,creates a first file and activates the "Part Design" workbench.'
__Author__ = 'BvdM'
__Version__ = '0.0.1'
__Date__ = '2019-03-22'
__License__ = 'Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) - https://creativecommons.org/licenses/by-nc/4.0/'
__Web__ = ''
__Wiki__ = 'Will be made if this macro is accepted in FreeCAD repository'
__Icon__ = 'Will be made if this macro is accepted in FreeCAD repository'
__Help__ = 'Select your FreeCAD documents folder'
__Status__ = 'Up for review'
__Requires__ = 'Tested FreeCAD = v0.17'
__Communication__ = ''
__Files__ = ''

"""
***************************************************************************
*   Copyright (c) 2019  <BvdM>                          *
***************************************************************************
# SHORT DESCRIPTION
# This Macro structures project folders,creates a first file and activates the "Part Design" workbench.
#
# MOTIVATION
# For a beginner in 3d drawing it can be hard to organise files and select the correct workbench to work with.
# This macro is built to organise files and folders for you. After that it puts you directly into the "Part Design" workbench.
# No worries about structuring files and filenames, just start drawing!
# This is especially usefull in a workshop environment to give everybody the same starting position.
#
# FUNCTION OF THE MACRO
# Required inputs are:
# - The location you usually store your FreeCAD documents.
# - The name of the project you want to create.
# The plugin will create a project folder in the selected documents folder.
# The projectfolder is named using the format: "date-ProjectName".
# Inside the folder it will create a first document file.
# This file is named using the format: "date-ProjectName-part001.FCStd"
#
# USAGE EXAMPLE
# We want to create a project for making a Chair.
# Start the macro.
# A prompt shows up and ask four your documents folder.
# Select your documents folder.
# In this example: C:\FreeCAD Documents.
# Give your project a name in the text prompt e.g. Chair.
# Click Ok.
# The macro creates a Chair folder using the current date C:\FreeCAD Documents\20190326-Chair .
# The Macro creates a file named 20190326-Chair-part001.FCStd in the newly created folder C:\FreeCAD Documents\20190326-Chair\ .
# The file is made active in FreeCAD and the document is switched to the "Part Design" workbench.
"""

# IMPORT STATEMENTS

from PySide import QtGui, QtCore
import os
import shutil
import time

# FIXED PARAMETERS

# A divider to separate folder names.
Divider = '/'

# The extension of FreeCAD combined with the clarification this is part one.
extension = "-part001.FCStd"

# MAIN SCRIPT STARTS HERE

# Define BasePath
BasePath = QtGui.QFileDialog.getExistingDirectory(None, 'Select a folder:', '', QtGui.QFileDialog.ShowDirsOnly)

# Define ProjectName
ProjectName = QtGui.QInputDialog.getText(None, "Get text", "Create a project name:")[0]

# Replace spaces in the project name with - . This prevents folder naming errors.
ProjectName = ProjectName.replace(' ', '-')

# Retrieve the current date.
DateToday = time.strftime("%Y%m%d")

# Create the string that will become the folder name using:
# 1. The project using the defined path
# 2. The current date
# 3. Use '-' as a divider between the date and ProjectName.

# 4 The defined project name
FolderOfDay = BasePath + Divider + DateToday + '-' + ProjectName

# Create the folder.
os.makedirs(FolderOfDay)

# Create a new document in FreeCAD.
App.newDocument("Unnamed")

# Retrieve the name of the newly created document.
ActiveDocumentName = App.ActiveDocument.Name

# Make the new document active and display it in FreeCAD
App.setActiveDocument(ActiveDocumentName)
App.ActiveDocument = App.getDocument(ActiveDocumentName)
Gui.ActiveDocument = Gui.getDocument(ActiveDocumentName)

# Create a string containing the full save path of the newly created file using
# 1. The BasePath.
# 2. A divider to make sure you are inside the BasePath folder.
# 3. The current date
# 4. Use '-' as a divider between the date and ProjectName.
# 5 The defined project name
# 6. A divider to make sure you are inside the project folder.
# 7. The filename starting with the date.
# 8. Use '-' as a divider between the date and ProjectName in the filename.
# 9. The extension of a FreeCAD file, designating this is part 001 of a project, to complete the path.


# FullDocumentPathString=BasePath+Divider+DateToday+'-'+ProjectName+Divider+DateToday+'-'+ProjectName+extension
FullDocumentPathString = FolderOfDay + Divider + DateToday + '-' + ProjectName + extension

# Save the file using the path made above.
App.getDocument(ActiveDocumentName).saveAs(str(FullDocumentPathString))

# Switch the saved file to the "Part Design" workbench.
Gui.activateWorkbench("PartDesignWorkbench")
Post Reply