Files
create/src/Mod/CAM/App/Command.h
Billy Huddleston a970235484 CAM: Enhance Path.Command annotations with variant type, type-safe API, and robust persistence
- Refactored `Annotations` member to use `std::variant<std::string, double>` for type-safe storage of both string and numeric values.
- Implemented C++ methods:
	- `setAnnotation(key, value)`: overloaded for string and double types.
	- `getAnnotation(key)`: returns annotation value as string.
	- `getAnnotationString(key)`: returns string annotation.
	- `getAnnotationDouble(key, fallback)`: returns numeric annotation.
	- `getAnnotationValue(key)`: returns raw variant value.
	- `hasAnnotation(key)`: checks for annotation existence.
	- `setAnnotations(annotationString)`: parses and stores values as double if possible, otherwise as string.
- Improved XML serialization (`Save`) and deserialization (`Restore`) to persist annotation types and values, including annotation count for robust restoration.
- Updated Python bindings:
	- `Annotations` property now supports mixed-type values (str/float).
	- Values are returned as native Python types.
	- Type errors are raised for invalid assignments.
- Expanded tests in `TestPathCommandAnnotations.py`:
	- Added cases for mixed-type annotations, edge cases, and in-memory persistence using `dumpContent`/`restoreContent`.
	- Verified type preservation and correct restoration.
- Ensured backward compatibility for string-only annotations and improved error handling.

**How to use annotations in Python:**

```import Path

c = Path.Command('G1', {'X': 10.0, 'Y': 20.0, 'F': 1000.0})
c.Annotations = {
	'tool_name': '6mm_endmill',      # string
	'spindle_speed': 12000.0,        # float
	'feed_rate': 1500,               # int (stored as float)
	'operation': 'pocket',           # string
	'depth_of_cut': -2.5,            # negative float
}
print(c.Annotations)  # {'tool_name': '6mm_endmill', 'spindle_speed': 12000.0, ...}
print(type(c.Annotations['spindle_speed']))  # <class 'float'>
print(type(c.Annotations['tool_name']))      # <class 'str'>

xml = c.dumpContent()
c2 = Path.Command()
c2.restoreContent(xml)
print(c2.Annotations)  # Restored with correct types

c.addAnnotations('speed:1000 operation:drill')
print(c.Annotations['speed'])        # 1000.0 (float)
print(c.Annotations['operation'])    # 'drill' (str)
```
2025-10-15 14:26:13 -04:00

104 lines
5.0 KiB
C++

// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2014 Yorik van Havre <yorik@uncreated.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef PATH_COMMAND_H
#define PATH_COMMAND_H
#include <map>
#include <string>
#include <variant>
#include <Base/Persistence.h>
#include <Base/Placement.h>
#include <Base/Vector3D.h>
#include <Mod/CAM/PathGlobal.h>
namespace Path
{
/** The representation of a cnc command in a path */
class PathExport Command: public Base::Persistence
{
TYPESYSTEM_HEADER_WITH_OVERRIDE();
public:
// constructors
Command();
Command(const char* name, const std::map<std::string, double>& parameters);
~Command() override;
// from base class
unsigned int getMemSize() const override;
void Save(Base::Writer& /*writer*/) const override;
void Restore(Base::XMLReader& /*reader*/) override;
// specific methods
Base::Placement getPlacement(const Base::Vector3d pos = Base::Vector3d())
const; // returns a placement from the x,y,z,a,b,c parameters
Base::Vector3d getCenter() const; // returns a 3d vector from the i,j,k parameters
void setCenter(const Base::Vector3d&,
bool clockwise = true); // sets the center coordinates and the command name
std::string
toGCode(int precision = 6,
bool padzero = true) const; // returns a GCode string representation of the command
void setFromGCode(
const std::string&); // sets the parameters from the contents of the given GCode string
void setFromPlacement(
const Base::Placement&); // sets the parameters from the contents of the given placement
bool
has(const std::string&) const; // returns true if the given string exists in the parameters
Command transform(const Base::Placement&); // returns a transformed copy of this command
double getValue(const std::string& name) const; // returns the value of a given parameter
void scaleBy(double factor); // scales the receiver - use for imperial/metric conversions
// annotation methods
void setAnnotation(const std::string& key,
const std::string& value); // sets a string annotation
void setAnnotation(const std::string& key,
double value); // sets a numeric annotation
std::string getAnnotation(const std::string& key) const; // gets an annotation value as string
std::string getAnnotationString(const std::string& key) const; // gets string annotation
double getAnnotationDouble(const std::string& key,
double fallback = 0.0) const; // gets numeric annotation
std::variant<std::string, double>
getAnnotationValue(const std::string& key) const; // gets raw annotation value
bool hasAnnotation(const std::string& key) const; // checks if annotation exists
Command&
setAnnotations(const std::string& annotationString); // sets annotations from string and
// returns reference for chaining
// this assumes the name is upper case
inline double getParam(const std::string& name, double fallback = 0.0) const
{
auto it = Parameters.find(name);
return it == Parameters.end() ? fallback : it->second;
}
// attributes
std::string Name;
std::map<std::string, double> Parameters;
std::map<std::string, std::variant<std::string, double>> Annotations;
};
} // namespace Path
#endif // PATH_COMMAND_H