Skip to content

Commit fede699

Browse files
committed
python: add CSTransform.copy() method
Exposes the existing C++ copy constructor CSTransform(CSTransform*) as a Python copy() method returning an independent duplicate of the transform. Signed-off-by: Thorsten Liebig <thorsten.liebig@gmx.de> Generated-by: Claude Sonnet 4.6
1 parent 937d700 commit fede699

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

python/CSXCAD/CSTransform.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cdef extern from "CSXCAD/CSTransform.h":
2424
cdef cppclass _CSTransform "CSTransform":
2525
_CSTransform() except +
2626
_CSTransform(_ParameterSet*) except +
27+
_CSTransform(_CSTransform*) except +
2728

2829
void Reset()
2930

python/CSXCAD/CSTransform.pyx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ cdef class CSTransform:
6464
self.thisptr = ptr
6565
CSTransform._instances[<uintptr_t>self.thisptr] = self
6666

67+
def copy(self):
68+
"""Return an independent copy of this transform."""
69+
cdef CSTransform obj = CSTransform.__new__(CSTransform)
70+
obj.thisptr = new _CSTransform(self.thisptr)
71+
obj._SetPtr(obj.thisptr)
72+
return obj
73+
6774
def Reset(self):
6875
"""
6976
Reset all transformations.

python/tests/test_CSTransform.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ def test_multiply_order(self):
100100
# post-multiply: translation applied before rotation → translate [1,0,0] → [2,0,0] then rotate → [0,2,0]
101101
self.assertTrue(compare_coords(self.tr.Transform([1, 0, 0]), [0, 2, 0]))
102102

103+
def test_copy_is_independent(self):
104+
self.tr.Translate([1, 2, 3])
105+
t2 = self.tr.copy()
106+
# same matrix values
107+
self.assertTrue((t2.GetMatrix() == self.tr.GetMatrix()).all())
108+
# distinct Python and C++ objects
109+
self.assertIsNot(t2, self.tr)
110+
# modifying the copy does not affect the original
111+
t2.Translate([10, 0, 0])
112+
self.assertFalse((t2.GetMatrix() == self.tr.GetMatrix()).all())
113+
103114

104115
if __name__ == '__main__':
105116
unittest.main()

0 commit comments

Comments
 (0)