FreeCAD-library/Architectural Parts/Doors_Windows/Save_parametric_models.FCMacro
2024-12-29 18:26:06 -03:00

181 lines
No EOL
7.4 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#***************************************************************************
#* *
#* Copyright (c) 2024 Francisco Rosa *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
# Macro Save models +++++++++++++++++++++++++++++++++++++++++++++++++
import FreeCAD
import FreeCADGui as Gui
import Spreadsheet
# ----------------------------------------------------------------------
## SAVE MODEL FROM PROPERTIES TO SPREADSHEET
# ----------------------------------------------------------------------
# 2. Get and save properties
# 2.1. Organize the properties
name = "None"
value = "None"
obj = None
group = "None"
group1 = "None"
group2 = "None"
propName = 'None'
objects = []
SpreadsheetModels = None
# Get the property name with the initial information
propName = input("Confirme the name of the properties set or VarSet wich Label is 'Initial_information' (Ex.: Prop, Prop001, Prop002... or VarSet, VarSet001, VarSet002...)")
if not FreeCAD.ActiveDocument.getObject(propName):
print(str(propName) + " is not correct!" + "\n")
# Get the spreadsheet name for saving model
SpreadsheetModels = FreeCAD.ActiveDocument.getObject(propName).A_Spreadsheet_models
# objects = [Prop, Prop001, ...]
objects = FreeCAD.ActiveDocument.getObject(propName).L_Property_objects_list
def saveComponentModel(column = "None", objects = None, SpreadsheetModels = None):
groupAnt = "None"
cellName = "None"
cellValue = "None"
N = 0
T = 0
cellNumber = 0
cellValue = column + str(1)
SpreadsheetModels.setBackground(cellValue, (0.666667,0.666667,0.000000))
for i in range(len(objects)):
# Prop, Prop001, ...
obj = objects[i]
name = "Label"
value = obj.getPropertyByName(name)
# Prop to SpreadsheetModels:
cellValue = column + str(3 + T)
SpreadsheetModels.set(cellValue, value)
SpreadsheetModels.setStyle(cellValue, 'bold', 'add')
SpreadsheetModels.setBackground(cellValue, (0.666667,0.666667,0.000000))
App.ActiveDocument.recompute()
N += 1
T += 1
App.ActiveDocument.recompute()
# End save Prop to SpreadsheetModels
propertyNames = []
propertyNames = obj.PropertiesList
groups = []
# groups = sorted(groups)
for n in range(len(propertyNames)):
name = propertyNames[n]
group = obj.getGroupOfProperty(name)
if group not in ("", "Base"):
if group != groupAnt and group not in groups:
groups.append(group)
groups = sorted(groups)
# Save properties
for i in range(len(groups)):
group2 = groups[i]
cellValue = column + str(3 + T)
# group background
SpreadsheetModels.setBackground(cellValue, (0.666667,0.666667,0.000000))
App.ActiveDocument.recompute()
N += 1
T += 1
counter = 0
# Save value properties by group
for n in range(len(propertyNames)):
name = propertyNames[n]
group1 = obj.getGroupOfProperty(name)
value = obj.getPropertyByName(name)
propertyType = None
propertyType = obj.getTypeIdOfProperty(name)
if group1 == group2 and name[0] != "D" and propertyType not in ('App::PropertyLink', 'App::PropertyLinkList', 'App::PropertyStringList'):
# Property to SpreadsheetModels
cellValue = column + str(3 + T)
if obj.Visibility == False:
value = "Not used"
SpreadsheetModels.set(cellValue, value)
SpreadsheetModels.setForeground(cellValue, (0.501961,0.501961,0.501961))
else:
value = str(obj.getPropertyByName(name))
SpreadsheetModels.set(cellValue, value)
N += 1
T += 1
counter += 1
App.ActiveDocument.recompute()
# Delete group is no usefull data
if counter == 0:
N -= 1
T -= 1
cells = "None"
cells = column + str(2) + ":" + column + str(2000)
SpreadsheetModels.setAlignment(cells, 'center', 'keep')
SpreadsheetModels.setAlignment(cells, 'vcenter', 'keep')
App.ActiveDocument.recompute()
T = N
T += 1
# 3. Include the model name in the list of options.
obj = None
listModels = []
modelName = "None"
obj = objects[0]
modelName = column + " - " + nameTitle + " " + obj.A_General_label
listModels = obj.A_Model_list
listModels.append(modelName)
obj.A_Model_list = listModels
obj.A_Model_name = [f for f in obj.A_Model_list]
App.ActiveDocument.recompute()
# 1. Models spreadsheet - create model title
nameTitle = "None"
cellTitle = "None"
column = None
# Condition to save the model
if objects[0].B_Confirm_values == True:
column = objects[0].A_New_model_column_spreadsheet
cellTitle = column + str(2)
nameTitle = objects[0].A_New_model_name
SpreadsheetModels.set(cellTitle, nameTitle)
App.ActiveDocument.recompute()
SpreadsheetModels.setStyle(cellTitle, 'bold', 'add')
App.ActiveDocument.recompute()
objects[0].B_Confirm_values = False
saveComponentModel(column, objects, SpreadsheetModels)
# Select the SpreadsheetModels
Gui.Selection.clearSelection()
Gui.Selection.addSelection(SpreadsheetModels)
App.ActiveDocument.recompute()
else:
print("First you must write or confirm the spreadsheet column in 'Prop_General_configurations or VarSet_General_configurations> I2_Save_new_model'> A_New_model_column_spreadsheet', the model name in 'A_New_model_name', then choose 'true' in 'B_Confirm_values!")
# ------------------------------------------------------------------------------------------------