TransformGeometry Ruins Contents

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

TransformGeometry Ruins Contents

Post by duckmug »

I am having a bit of a difficulty with a macro that I am working on.

I am able to manipulate the true location of a wire by doing this:

Code: Select all

m = FreeCAD.Matrix()
m.move(obj.Shape.BoundBox.Center * -1)
new_shape = obj.Shape.transformGeometry(m)
Unfortunately if I convert this to a sketch afterwards, everything will get parsed as a b-spline rather than lines as lines, arcs as arcs etc.

If I convert the wire to a sketch prior to doing this, everything gets parsed correctly.

Is there a way to do the exact same thing without this side effect?
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: TransformGeometry Ruins Contents

Post by Chris_G »

Hi,
It is clearly stated in the transformGeometry() doc :

Code: Select all

help(Part.Shape.transformGeometry)
Help on method_descriptor:

transformGeometry(...)
Apply geometric transformation on this or a copy the shape.
transformGeometry(matrix) -> Shape
--
This method returns a new shape.
The transformation to be applied is defined as a 4x4 matrix.
The underlying geometry of the following shapes may change:
- a curve which supports an edge of the shape, or
- a surface which supports a face of the shape;

For example, a circle may be transformed into an ellipse when
applying an affinity transformation. It may also happen that
the circle then is represented as a B-spline curve.

The transformation is applied to:
- all the curves which support edges of the shape, and
- all the surfaces which support faces of the shape.

Note: If you want to transform a shape without changing the
underlying geometry then use the methods translate or rotate.
So you should have better luck with methods translate(), rotate() and transformShape()
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Re: TransformGeometry Ruins Contents

Post by duckmug »

Thanks a lot! I will run that help command in the future.

Update: I've tried that now both with translate and transformShape and it complains:

Code: Select all

ReferenceError: This object is immutable, you can not set any attribute or call a non const method
Is there any workaround to this?
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: TransformGeometry Ruins Contents

Post by openBrain »

You can get a separate instance of the shape with

Code: Select all

obj.Shape.copy()
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Re: TransformGeometry Ruins Contents

Post by duckmug »

Perfect, thank you!
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: TransformGeometry Ruins Contents

Post by heda »

Code: Select all

>>> App.ActiveDocument.addObject("Part::Box","Box")
>>> App.ActiveDocument.ActiveObject.Label = "Cube"
>>>
>>> ## use gui to move box out of 0,0,0 for demonstration purpose
>>>
>>> App.ActiveDocument.Box.Shape.BoundBox
BoundBox (-15, 10, 0, -5, 20, 10)
>>> App.ActiveDocument.Box.Shape.BoundBox.Center
Vector (-10.0, 15.0, 5.0)
>>>
>>> ## using Draft.move to translate objects
>>>
>>> import Draft
>>> obj = App.ActiveDocument.Box
>>> center_vector = obj.Shape.BoundBox.Center
>>> center_vector
Vector (-10.0, 15.0, 5.0)
>>> center_vector*-1
Vector (10.0, -15.0, -5.0)
>>> Draft.move(obj, center_vector *-1, copy=False)
<Part::PartFeature>
>>> App.ActiveDocument.recompute()
0
>>> ## not sure if you have mentioned what the starting point
>>> ## of the problem is and the desired end state
>>> ## like, "move line from a to b", "mirror wire and translate", etc
duckmug
Posts: 126
Joined: Mon Jun 07, 2021 12:26 am

Re: TransformGeometry Ruins Contents

Post by duckmug »

Thanks for the example!
Post Reply