algo
This commit is contained in:
92
Project/GenerateExternalDocument.py
Normal file
92
Project/GenerateExternalDocument.py
Normal file
@@ -0,0 +1,92 @@
|
||||
import FreeCAD
|
||||
import FreeCADGui
|
||||
from PySide2 import QtWidgets
|
||||
import os
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
import FreeCADGui
|
||||
from PySide import QtCore, QtGui, QtWidgets
|
||||
from PySide.QtCore import QT_TRANSLATE_NOOP
|
||||
|
||||
import os
|
||||
else:
|
||||
# \cond
|
||||
def translate(ctxt, txt):
|
||||
return txt
|
||||
|
||||
|
||||
def QT_TRANSLATE_NOOP(ctxt, txt):
|
||||
return txt
|
||||
# \endcond
|
||||
|
||||
__title__ = "PVPlant Export to DXF"
|
||||
__author__ = "Javier Braña"
|
||||
__url__ = "http://www.sogos-solar.com"
|
||||
|
||||
from PVPlantResources import DirIcons as DirIcons
|
||||
|
||||
|
||||
def copy_object_with_reference():
|
||||
try:
|
||||
# Verificar selección
|
||||
selected = FreeCADGui.Selection.getSelection()
|
||||
if len(selected) != 1:
|
||||
QtWidgets.QMessageBox.critical(None, "Error", "Selecciona exactamente un objeto")
|
||||
return
|
||||
|
||||
original_doc = FreeCAD.ActiveDocument
|
||||
original_obj = selected[0]
|
||||
original_center = original_obj.Shape.BoundBox.Center
|
||||
|
||||
# Crear nuevo documento
|
||||
new_doc = FreeCAD.newDocument(f"{original_doc.Name} - {original_obj.Label}")
|
||||
|
||||
# Copiar objeto al nuevo documento
|
||||
new_obj = new_doc.copyObject(original_obj, True)
|
||||
new_obj.Label = f"Linked_{original_obj.Label}"
|
||||
new_obj.Placement.Base = original_obj.Placement.Base - original_center
|
||||
|
||||
# Guardar el documenton nuevp
|
||||
path = os.path.dirname(FreeCAD.ActiveDocument.FileName)
|
||||
new_doc.saveAs(os.path.join(path, new_doc.Name))
|
||||
|
||||
# Mantener posición original en el nuevo documento
|
||||
# new_obj.Placement = original_obj.Placement
|
||||
|
||||
# Crear referencia (App::Link) en el documento original
|
||||
link = original_doc.addObject("App::Link", f"Link_{new_obj.Label}")
|
||||
link.LinkedObject = new_obj
|
||||
|
||||
# Mantener posición original del objeto
|
||||
link.Placement = original_obj.Placement
|
||||
|
||||
# Actualizar vistas
|
||||
original_doc.recompute()
|
||||
new_doc.recompute()
|
||||
|
||||
# Regresar al documento original
|
||||
FreeCAD.setActiveDocument(original_doc.Name)
|
||||
|
||||
#QtWidgets.QMessageBox.information(None, "Éxito", "Operación completada correctamente")
|
||||
|
||||
except Exception as e:
|
||||
QtWidgets.QMessageBox.critical(None, "Error", f"Error: {str(e)}")
|
||||
|
||||
|
||||
# Ejecutar la función
|
||||
class CommandGenerateExternalDocument:
|
||||
def GetResources(self):
|
||||
return {'Pixmap': str(os.path.join(DirIcons, "dxf.svg")),
|
||||
'Accel': "P, E",
|
||||
'MenuText': "Export to DXF",
|
||||
'ToolTip': QT_TRANSLATE_NOOP("Placement", "Export choosed layers to dxf")}
|
||||
|
||||
def Activated(self):
|
||||
''' '''
|
||||
copy_object_with_reference()
|
||||
|
||||
def IsActive(self):
|
||||
if FreeCAD.ActiveDocument:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
@@ -21,6 +21,10 @@
|
||||
# ***********************************************************************
|
||||
|
||||
import FreeCAD
|
||||
import FreeCADGui
|
||||
from PySide import QtGui, QtCore
|
||||
import datetime
|
||||
import getpass
|
||||
|
||||
if FreeCAD.GuiUp:
|
||||
import FreeCADGui, os
|
||||
@@ -43,51 +47,99 @@ except AttributeError:
|
||||
import PVPlantResources
|
||||
from PVPlantResources import DirIcons as DirIcons
|
||||
|
||||
class SafeDict(dict):
|
||||
"""Diccionario seguro para manejar placeholders no definidos"""
|
||||
|
||||
def rename(objects, mask, mode=0):
|
||||
'''
|
||||
mode = 0/1/2/3
|
||||
0: izquierda a derecha - arriba a abajo
|
||||
1: arriba a abajo - izquierda a derecha
|
||||
'''
|
||||
|
||||
# sort:
|
||||
tmp = sorted(objects, key=lambda x: (x.Placement.Base.x,
|
||||
x.Placement.Base.y))
|
||||
|
||||
for idx, obj in tmp:
|
||||
obj.Name = name
|
||||
|
||||
class renamerTaskPanel:
|
||||
def __init__(self, obj=None):
|
||||
self.obj = obj
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------
|
||||
# Module widget form
|
||||
# -------------------------------------------------------------------------------------------------------------
|
||||
self.formRack = FreeCADGui.PySideUic.loadUi(PVPlantResources.__dir__ + "/PVPlantFrame.ui")
|
||||
self.formRack.widgetTracker.setVisible(False)
|
||||
self.formRack.comboFrameType.currentIndexChanged.connect(self.selectionchange)
|
||||
|
||||
self.formPiling = FreeCADGui.PySideUic.loadUi(PVPlantResources.__dir__ + "/PVPlantRackFixedPiling.ui")
|
||||
self.formPiling.editBreadthwaysNumOfPost.valueChanged.connect(self.editBreadthwaysNumOfPostChange)
|
||||
self.formPiling.editAlongNumOfPost.valueChanged.connect(self.editAlongNumOfPostChange)
|
||||
|
||||
self.form = [self.formRack, self.formPiling]
|
||||
|
||||
def accept(self):
|
||||
self.closeForm()
|
||||
return True
|
||||
|
||||
def reject(self):
|
||||
self.closeForm()
|
||||
return False
|
||||
|
||||
def closeForm(self):
|
||||
FreeCADGui.Control.closeDialog()
|
||||
def __missing__(self, key):
|
||||
return f'{{{key}}}'
|
||||
|
||||
|
||||
class _CommandRenamer:
|
||||
class RenameDialog(QtGui.QDialog):
|
||||
def __init__(self):
|
||||
super(RenameDialog, self).__init__()
|
||||
self.setupUI()
|
||||
|
||||
def setupUI(self):
|
||||
self.setWindowTitle("Renombrar objetos con plantilla")
|
||||
self.setMinimumWidth(400)
|
||||
layout = QtGui.QVBoxLayout(self)
|
||||
|
||||
# Campo para la plantilla
|
||||
layout.addWidget(QtGui.QLabel("Plantilla de nombre:"))
|
||||
self.template_input = QtGui.QLineEdit()
|
||||
self.template_input.setPlaceholderText("Ej: {label}_mod_{index:03d}_{date:%Y%m%d}")
|
||||
layout.addWidget(self.template_input)
|
||||
|
||||
# Info de placeholders
|
||||
info = QtGui.QLabel(
|
||||
"Placeholders disponibles:\n"
|
||||
"• {index} - Número en orden\n"
|
||||
"• {label} - Nombre actual del objeto\n"
|
||||
"• {name} - Nombre interno\n"
|
||||
"• {date} - Fecha actual\n"
|
||||
"• {time} - Hora actual\n"
|
||||
"• {user} - Usuario del sistema\n"
|
||||
"• {datetime} - Fecha y hora completa\n"
|
||||
"Formatos: {date:%Y/%m/%d}, {index:03d}, etc."
|
||||
)
|
||||
layout.addWidget(info)
|
||||
|
||||
# Botones
|
||||
btn_box = QtGui.QDialogButtonBox()
|
||||
btn_box.addButton(QtGui.QDialogButtonBox.Apply)
|
||||
btn_box.addButton(QtGui.QDialogButtonBox.Close)
|
||||
btn_box.clicked.connect(self.on_button_click)
|
||||
layout.addWidget(btn_box)
|
||||
|
||||
def on_button_click(self, button):
|
||||
if button == btn_box.button(QtGui.QDialogButtonBox.Apply):
|
||||
self.rename_objects()
|
||||
else:
|
||||
self.close()
|
||||
|
||||
def rename_objects(self):
|
||||
template = self.template_input.text()
|
||||
if not template:
|
||||
QtGui.QMessageBox.warning(self, "Error", "¡La plantilla no puede estar vacía!")
|
||||
return
|
||||
|
||||
selected_objects = FreeCADGui.Selection.getSelection()
|
||||
if not selected_objects:
|
||||
QtGui.QMessageBox.warning(self, "Error", "¡No hay objetos seleccionados!")
|
||||
return
|
||||
|
||||
now = datetime.datetime.now()
|
||||
user_name = getpass.getuser()
|
||||
errors = []
|
||||
|
||||
for idx, obj in enumerate(selected_objects, 1):
|
||||
try:
|
||||
placeholders = SafeDict({
|
||||
'index': idx,
|
||||
'label': obj.Label,
|
||||
'name': obj.Name,
|
||||
'date': now.date(),
|
||||
'time': now.time(),
|
||||
'datetime': now,
|
||||
'user': user_name
|
||||
})
|
||||
|
||||
new_name = template.format_map(placeholders)
|
||||
obj.Label = new_name
|
||||
except Exception as e:
|
||||
errors.append(f"{obj.Name}: {str(e)}")
|
||||
|
||||
FreeCAD.ActiveDocument.recompute()
|
||||
|
||||
if errors:
|
||||
error_msg = "\n".join(errors)
|
||||
QtGui.QMessageBox.critical(self, "Errores", f"Error(es) encontrado(s):\n{error_msg}")
|
||||
else:
|
||||
QtGui.QMessageBox.information(self, "Éxito", "¡Objetos renombrados correctamente!")
|
||||
|
||||
|
||||
|
||||
class CommandRenamer:
|
||||
"the Arch Building command definition"
|
||||
|
||||
def GetResources(self):
|
||||
|
||||
Reference in New Issue
Block a user