|
|
|
|
@ -7,6 +7,17 @@ try:
|
|
|
|
|
except ImportError:
|
|
|
|
|
Gui = None
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from PySide6 import QtWidgets
|
|
|
|
|
except ImportError:
|
|
|
|
|
try:
|
|
|
|
|
from PySide2 import QtWidgets
|
|
|
|
|
except ImportError:
|
|
|
|
|
try:
|
|
|
|
|
from PySide import QtGui as QtWidgets
|
|
|
|
|
except ImportError:
|
|
|
|
|
QtWidgets = None
|
|
|
|
|
|
|
|
|
|
import TerminalObjects
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -151,6 +162,35 @@ def validate_template_terminals(doc):
|
|
|
|
|
return report
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _fcstd_path(path):
|
|
|
|
|
value = (path or "").strip()
|
|
|
|
|
if not value:
|
|
|
|
|
raise TemplateAuthoringError("A target FCStd path is required.")
|
|
|
|
|
if not value.lower().endswith(".fcstd"):
|
|
|
|
|
value = value + ".FCStd"
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_template_as_fcstd(doc, path):
|
|
|
|
|
if doc is None:
|
|
|
|
|
raise TemplateAuthoringError("An active FreeCAD document is required.")
|
|
|
|
|
|
|
|
|
|
target_path = _fcstd_path(path)
|
|
|
|
|
report = validate_template_terminals(doc)
|
|
|
|
|
if report["total_terminals"] <= 0:
|
|
|
|
|
raise TemplateAuthoringError("At least one template terminal is required before saving.")
|
|
|
|
|
if report["warnings"]:
|
|
|
|
|
raise TemplateAuthoringError(
|
|
|
|
|
"Template terminals must be valid before saving: {0}".format(
|
|
|
|
|
"; ".join(report["warnings"])
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
doc.saveAs(target_path)
|
|
|
|
|
report["path"] = target_path
|
|
|
|
|
return report
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _selection_position():
|
|
|
|
|
if Gui is None:
|
|
|
|
|
return None
|
|
|
|
|
@ -236,6 +276,44 @@ class CommandValidateTemplateTerminals:
|
|
|
|
|
App.Console.PrintWarning("[FreeCADExchange] {0}\n".format(warning))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandSaveTemplateAsFCStd:
|
|
|
|
|
def GetResources(self):
|
|
|
|
|
return {
|
|
|
|
|
"MenuText": "Save Template As FCStd",
|
|
|
|
|
"ToolTip": "Validate and save the current document as a reusable FCStd equipment template",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def IsActive(self):
|
|
|
|
|
return App.ActiveDocument is not None
|
|
|
|
|
|
|
|
|
|
def Activated(self):
|
|
|
|
|
if QtWidgets is None:
|
|
|
|
|
App.Console.PrintError("[FreeCADExchange] Qt file dialog is not available.\n")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
file_path, _selected_filter = QtWidgets.QFileDialog.getSaveFileName(
|
|
|
|
|
None,
|
|
|
|
|
"Save FCStd Equipment Template",
|
|
|
|
|
"",
|
|
|
|
|
"FreeCAD template (*.FCStd *.fcstd);;All files (*.*)",
|
|
|
|
|
)
|
|
|
|
|
if not file_path:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
report = save_template_as_fcstd(App.ActiveDocument, file_path)
|
|
|
|
|
App.Console.PrintMessage(
|
|
|
|
|
"[FreeCADExchange] Saved FCStd template: {0} ({1} terminals)\n".format(
|
|
|
|
|
report["path"],
|
|
|
|
|
report["valid_terminals"],
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
App.Console.PrintError(
|
|
|
|
|
"[FreeCADExchange] template save failed: {0}\n".format(exc)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_COMMANDS_REGISTERED = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -247,6 +325,7 @@ def register_commands():
|
|
|
|
|
return
|
|
|
|
|
Gui.addCommand("QET_Template_AddTerminal", CommandAddTemplateTerminal())
|
|
|
|
|
Gui.addCommand("QET_Template_ValidateTerminals", CommandValidateTemplateTerminals())
|
|
|
|
|
Gui.addCommand("QET_Template_SaveAsFCStd", CommandSaveTemplateAsFCStd())
|
|
|
|
|
_COMMANDS_REGISTERED = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|